Created
January 16, 2017 18:04
Star
You must be signed in to star a gist
Prosemirror Selection Plugin
This file contains 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