a tool for shared writing and social publishing
1import { Decoration, DecorationSet } from "prosemirror-view";
2import { Plugin } from "prosemirror-state";
3export const highlightSelectionPlugin = new Plugin({
4 state: {
5 init(_, { doc }) {
6 return DecorationSet.empty;
7 },
8 apply(tr, oldDecorations, oldState, newState) {
9 let decorations = [];
10
11 // Check if there's a selection
12 const { from, to } = newState.selection;
13 if (from !== to) {
14 decorations.push(
15 Decoration.inline(from, to, { class: "selection-highlight" }),
16 );
17 }
18
19 return DecorationSet.create(newState.doc, decorations);
20 },
21 },
22 props: {
23 decorations(state) {
24 return this.getState(state);
25 },
26 },
27});