@atlaskit/editor-plugin-selection-marker
Version:
Selection marker plugin for @atlaskit/editor-core.
44 lines (43 loc) • 2.09 kB
JavaScript
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { Decoration } from '@atlaskit/editor-prosemirror/view';
import { fg } from '@atlaskit/platform-feature-flags/fg';
var decorationStyle = "\n background-color: ".concat("var(--ds-background-neutral, #0515240F)", ";\n ");
var decorationHighlightStyle = "\n background-color: ".concat("var(--ds-background-accent-blue-subtlest, #E9F2FE)", ";\n border-bottom: ", "var(--ds-border-width-selected, 2px)", " solid ", "var(--ds-background-accent-blue-subtler, #CFE1FD)", ";\n ");
/**
* A node-selected inline atom keeps its own selected-node styling instead of the marker.
*/
var isMarkableInlineAtom = function isMarkableInlineAtom(node, selection) {
return node.isInline && node.isAtom && !(selection instanceof NodeSelection && selection.node === node);
};
export var selectionDecoration = function selectionDecoration(doc, selection, isHighlight) {
var selectionDecorations = [];
var style = isHighlight ? decorationHighlightStyle : decorationStyle;
var shouldMarkInlineAtoms = fg('platform_editor_selection_marker_blur_fix');
doc.nodesBetween(selection.from, selection.to, function (currentNode, nodePos) {
if (!currentNode.isText) {
if (shouldMarkInlineAtoms && isMarkableInlineAtom(currentNode, selection)) {
selectionDecorations.push(Decoration.node(nodePos, nodePos + currentNode.nodeSize, {
style: style,
'data-testid': 'selection-marker-inline-node',
'data-selection-marker': ''
}));
}
return true;
}
var decorationFrom = selection.from;
var decorationTo = selection.to;
if (nodePos > selection.from) {
decorationFrom = nodePos;
}
if (nodePos + currentNode.nodeSize < selection.to) {
decorationTo = nodePos + currentNode.nodeSize;
}
selectionDecorations.push(Decoration.inline(decorationFrom, decorationTo, {
style: style,
'data-testid': 'selection-marker-selection',
'data-selection-marker': ''
}));
return true;
});
return selectionDecorations;
};