-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
editorExtension.ts
119 lines (94 loc) · 2.93 KB
/
editorExtension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Line, Range } from "@codemirror/state";
import { EditorView, ViewUpdate, Decoration, DecorationSet, ViewPlugin } from "@codemirror/view";
function getActiveWordBounds(line: Line, pos: number) {
const lineStart = line.from;
const lineText = line.text;
let start = lineText.slice(0, pos - lineStart).search(/\S+$/);
if (start < 0) start = pos - lineStart;
let end = lineText.slice(pos - lineStart).search(/\s/);
if (end < 0) end = lineText.length;
else end += pos - lineStart;
return { start: start + lineStart, end: end + lineStart };
}
function getParagraphDecos(view: EditorView) {
const widgets: Range<Decoration>[] = [];
const selection = view.state.selection.main;
const pos = selection.from;
const line = view.state.doc.lineAt(pos);
const lineText = line.text;
let match;
const wordRegex = /(\S+)/g;
while ((match = wordRegex.exec(lineText)) !== null) {
const start = match.index + line.from;
const end = start + match[1].length;
widgets.push(
Decoration.mark({
inclusive: true,
attributes: {},
class: "word-span",
}).range(start, end)
);
}
return Decoration.set(widgets, true);
}
function getActiveWordDecos(view: EditorView) {
const widgets: Range<Decoration>[] = [];
const selection = view.state.selection.main;
const pos = selection.from;
const line = view.state.doc.lineAt(pos);
const activeWordBounds = getActiveWordBounds(line, pos);
const start = activeWordBounds.start;
const end = activeWordBounds.end;
function addWidget(from: number, to: number, className: string) {
widgets.push(
Decoration.mark({
inclusive: true,
attributes: {},
class: className,
}).range(from, to)
);
}
if (start != end) {
addWidget(start, end, "focus-word-active");
}
return Decoration.set(widgets, true);
}
export const WordSpanViewPlugin = ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = getParagraphDecos(view);
}
update(update: ViewUpdate) {
if (update.docChanged || update.selectionSet) {
this.decorations = getParagraphDecos(update.view);
}
}
},
{ decorations: (v) => v.decorations }
);
export const FocusActiveWordViewPlugin = ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = getActiveWordDecos(view);
}
update(update: ViewUpdate) {
if (update.docChanged || update.selectionSet) {
this.decorations = getActiveWordDecos(update.view);
if (this.editorElement) {
this.editorElement.addClass("focus-word");
}
}
}
},
{ decorations: (v) => v.decorations }
);
export const scrollEventHandler = EditorView.domEventHandlers({
scroll(event, view) {
const editorElement = view.contentDOM.parentElement;
if (editorElement) {
editorElement.removeClass("focus-word");
}
}
});