rhino-editor
Version:
A custom element wrapped rich text editor
56 lines (54 loc) • 1.32 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) => {
return new Plugin({
key: new PluginKey("rhino-selection"),
state: {
init() {
return DecorationSet.empty;
},
apply(tr, set) {
set = set.map(tr.mapping, tr.doc);
set = set.remove(set.find());
const { doc, selection } = tr;
let deco = null;
if (selection.to !== selection.from) {
deco = Decoration.inline(
selection.from,
selection.to,
options.HTMLAttributes || {}
);
}
if (deco) {
return DecorationSet.create(doc, [deco]);
}
return DecorationSet.empty;
}
},
props: {
decorations(state) {
return this.getState(state);
}
}
});
};
var SelectionPlugin = Extension.create({
name: "rhino-selection",
addOptions() {
return {
HTMLAttributes: {
class: "rhino-selection",
readonly: ""
}
};
},
addProseMirrorPlugins() {
return [selectionPlugin(this.options)];
}
});
export {
SelectionPlugin
};
//# sourceMappingURL=chunk-D4EITHP5.js.map