Created
January 16, 2017 18:04
-
-
Save ThariqS/72cb138d33e7bcfd15d0440cc4bd555a to your computer and use it in GitHub Desktop.
Prosemirror Selection Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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