Skip to content

Instantly share code, notes, and snippets.

@ThariqS
Created January 16, 2017 18:04
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ThariqS/72cb138d33e7bcfd15d0440cc4bd555a to your computer and use it in GitHub Desktop.
Prosemirror Selection Plugin
import {Decoration, DecorationSet} from "prosemirror-view";
import {Plugin} from 'prosemirror-state';
const SelectPlugin = new Plugin({
state: {
init(config, instance) {
return { deco: DecorationSet.empty };
},
apply(transaction, state, prevEditorState, editorState) {
const sel = transaction.curSelection;
if (sel) {
const decos = [Decoration.inline(sel.$from.pos, sel.$to.pos,
{class: 'selection-marker'},
{ inclusiveLeft: true,
inclusiveRight: true,
}
)];
const deco = DecorationSet.create(editorState.doc, decos);
return {deco};
}
return state;
}
},
props: {
decorations(state) {
if (state && this.getState(state)) {
return this.getState(state).deco;
}
return null;
}
}
});
export default SelectPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment