@atlaskit/editor-plugin-highlight
Version:
Highlight plugin for @atlaskit/editor-core
59 lines • 1.42 kB
JavaScript
import { Decoration } from '@atlaskit/editor-prosemirror/view';
import { isHighlightedTextNode, shouldPadLeft, shouldPadRight } from './utils';
const createPaddingDecoration = ({
pos,
node,
padLeft,
padRight
}) => {
const classes = [];
const baseClass = 'background-color';
const padLeftClass = `${baseClass}-padding-left`;
const padRightClass = `${baseClass}-padding-right`;
if (padLeft) {
classes.push(padLeftClass);
}
if (padRight) {
classes.push(padRightClass);
}
return Decoration.inline(pos, pos + node.nodeSize, {
class: classes.join(' ')
});
};
/**
* Adds padding decorations to highlighted text
* within the specified range.
*/
export const addPaddingDecorations = ({
decorationSet,
state,
from,
to
}) => {
let newDecorationSet = decorationSet;
state.doc.nodesBetween(from, to, (node, pos) => {
if (!isHighlightedTextNode(node)) {
return;
}
const nodeStart = pos;
const nodeEnd = pos + node.nodeSize;
const padLeft = shouldPadLeft({
state,
nodeStart
});
const padRight = shouldPadRight({
state,
nodeEnd
});
if (padLeft && padRight) {
const newDecoration = createPaddingDecoration({
pos: nodeStart,
node,
padLeft,
padRight
});
newDecorationSet = newDecorationSet.add(state.doc, [newDecoration]);
}
});
return newDecorationSet;
};