@atlaskit/editor-plugin-selection
Version:
Selection plugin for @atlaskit/editor-core
46 lines • 1.74 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
import { markBoundaryCursorPluginKey } from './mark-boundary-cursor-plugin-key';
import { createMarkBoundaryCursorDecoration } from './mark-boundary-cursor/ui/mark-boundary-cursor-decoration';
import { getActiveMarksSide } from './mark-boundary-cursor/utils/active-marks-side';
export var createMarkBoundaryCursorPlugin = function createMarkBoundaryCursorPlugin() {
return new SafePlugin({
key: markBoundaryCursorPluginKey,
state: {
init: function init() {
return {
decorations: DecorationSet.empty
};
},
apply: function apply(tr, currentState) {
var selection = tr.selection,
storedMarks = tr.storedMarks,
doc = tr.doc,
selectionSet = tr.selectionSet,
storedMarksSet = tr.storedMarksSet,
docChanged = tr.docChanged;
if (!selectionSet && !storedMarksSet && !docChanged) {
return currentState;
}
var side = getActiveMarksSide({
selection: selection,
storedMarks: storedMarks
});
if (!side) {
return {
decorations: DecorationSet.empty
};
}
return {
decorations: DecorationSet.create(doc, [createMarkBoundaryCursorDecoration(selection.head, side)])
};
}
},
props: {
decorations: function decorations(state) {
var _markBoundaryCursorPl;
return (_markBoundaryCursorPl = markBoundaryCursorPluginKey.getState(state)) === null || _markBoundaryCursorPl === void 0 ? void 0 : _markBoundaryCursorPl.decorations;
}
}
});
};