UNPKG

rhino-editor

Version:

A custom element wrapped rich text editor

93 lines (90 loc) 2.14 kB
import { Decoration, DecorationSet, Extension } from "./chunk-4CJ3F2XK.js"; import { Plugin, PluginKey } from "./chunk-XG3W5CFT.js"; // src/exports/extensions/selection.ts var selectionPlugin = (options) => { const plugin = new Plugin({ key: new PluginKey("rhino-selection"), state: { init() { return DecorationSet.empty; }, apply(tr, set) { if (!tr.getMeta(plugin)) { return set; } set = set.map(tr.mapping, tr.doc); const selections = set.find(void 0, void 0, (deco2) => { return deco2.rhinoSelection === true; }); if (selections?.length >= 1) { set = set.remove(selections); } const { doc, selection } = tr; let deco = null; if (selection.to !== selection.from) { deco = Decoration.inline( selection.from, selection.to, options.HTMLAttributes || {}, { rhinoSelection: true } ); } if (deco) { set = set.add(doc, [deco]); } return set; } }, props: { handleDOMEvents: { blur: (view) => { const { tr } = view.state; const transaction = tr.setMeta(plugin, { from: tr.selection.from, to: tr.selection.to }); view.dispatch(transaction); }, focus: (view) => { const { tr } = view.state; const transaction = tr.setMeta(plugin, { from: tr.selection.from, to: tr.selection.to }); view.dispatch(transaction); } }, decorations(state) { return this.getState(state); } } }); return plugin; }; var SelectionPlugin = Extension.create({ name: "rhino-selection", addOptions() { return { HTMLAttributes: { class: "rhino-selection", readonly: "" } }; }, addProseMirrorPlugins() { return [selectionPlugin(this.options)]; } }); export { SelectionPlugin }; //# sourceMappingURL=chunk-A3CW73KJ.js.map