@atlaskit/editor-plugin-selection
Version:
Selection plugin for @atlaskit/editor-core
48 lines (46 loc) • 2.13 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { AllSelection, PluginKey, TextSelection } from '@atlaskit/editor-prosemirror/state';
import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
export const inlineNodeInRangeSelectionClassName = 'ak-editor-inline-node-in-range-selection';
/** Inline atom nodes that show a selection treatment instead of the native text highlight. */
export const inlineNodeRangeSelectionNodeTypeNames = ['inlineCard', 'mention', 'status', 'date', 'emoji'];
const pluginKey = new PluginKey('inlineNodeRangeSelectionPlugin');
const isNonEmptyRangeSelection = selection => !selection.empty && (selection instanceof TextSelection || selection instanceof AllSelection);
/**
* Marks every node of the given inline atom types that sits inside a text or all selection,
* so it can show a selection treatment instead of the native text highlight on its internals.
*/
const getInlineNodeRangeSelectionDecorations = ({
doc,
schema,
selection
}, nodeTypeNames) => {
if (!isNonEmptyRangeSelection(selection)) {
return DecorationSet.empty;
}
const nodeTypes = new Set(nodeTypeNames.map(name => schema.nodes[name]).filter(Boolean));
if (nodeTypes.size === 0) {
return DecorationSet.empty;
}
const decorations = [];
doc.nodesBetween(selection.from, selection.to, (node, pos) => {
if (!nodeTypes.has(node.type)) {
return true;
}
decorations.push(Decoration.node(pos, pos + node.nodeSize, {
class: inlineNodeInRangeSelectionClassName
}));
return false;
});
return DecorationSet.create(doc, decorations);
};
export const createInlineNodeRangeSelectionPlugin = (nodeTypeNames = inlineNodeRangeSelectionNodeTypeNames) => new SafePlugin({
key: pluginKey,
state: {
init: (_config, state) => getInlineNodeRangeSelectionDecorations(state, nodeTypeNames),
apply: (tr, decorations, _oldState, newState) => tr.docChanged || tr.selectionSet ? getInlineNodeRangeSelectionDecorations(newState, nodeTypeNames) : decorations
},
props: {
decorations: state => pluginKey.getState(state)
}
});