UNPKG

@atlaskit/editor-plugin-highlight

Version:

Highlight plugin for @atlaskit/editor-core

57 lines 1.67 kB
import { Decoration } from '@atlaskit/editor-prosemirror/view'; import { isHighlightedTextNode, shouldPadLeft, shouldPadRight } from './utils'; var createPaddingDecoration = function createPaddingDecoration(_ref) { var pos = _ref.pos, node = _ref.node, padLeft = _ref.padLeft, padRight = _ref.padRight; var classes = []; var baseClass = 'background-color'; var padLeftClass = "".concat(baseClass, "-padding-left"); var padRightClass = "".concat(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 var addPaddingDecorations = function addPaddingDecorations(_ref2) { var decorationSet = _ref2.decorationSet, state = _ref2.state, from = _ref2.from, to = _ref2.to; var newDecorationSet = decorationSet; state.doc.nodesBetween(from, to, function (node, pos) { if (!isHighlightedTextNode(node)) { return; } var nodeStart = pos; var nodeEnd = pos + node.nodeSize; var padLeft = shouldPadLeft({ state: state, nodeStart: nodeStart }); var padRight = shouldPadRight({ state: state, nodeEnd: nodeEnd }); if (padLeft && padRight) { var newDecoration = createPaddingDecoration({ pos: nodeStart, node: node, padLeft: padLeft, padRight: padRight }); newDecorationSet = newDecorationSet.add(state.doc, [newDecoration]); } }); return newDecorationSet; };