rhino-editor
Version:
A custom element wrapped rich text editor
86 lines (84 loc) • 2.15 kB
JavaScript
// src/exports/extensions/selection.ts
import { Extension } from "@tiptap/core";
import { Plugin, PluginKey } from "@tiptap/pm/state";
import { Decoration, DecorationSet } from "@tiptap/pm/view";
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-CNIINXXE.js.map