I want to highlight the codemirror6 select text, this is the hightlight function look like:
const hightlightSelection = (selectedText: string, editorView: EditorView) => {
let cursor = new SearchCursor(editorView.state.doc, selectedText);
const highlight_effect = StateEffect.define<Range<Decoration>[]>();
cursor.next();
const highlight_decoration = Decoration.mark({
attributes: { style: "background-color: yellow" }
});
if (editorView) {
editorView.dispatch({
effects: highlight_effect.of([highlight_decoration.range(1, 4)])
});
}
}
but how to capture the selection event in codemirror6? I have tried like this:
if(editorView){
editorView.dom.addEventListener("selectionchange", () => {
debugger
const selection = editorView.state.selection;
// handle selection change
console.log("selection text:" + selection);
});
}