-
Notifications
You must be signed in to change notification settings - Fork 27
/
block-cursor.ts
193 lines (174 loc) · 6.43 KB
/
block-cursor.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { SelectionRange, Prec } from "@codemirror/state"
import { ViewUpdate, EditorView, Direction } from "@codemirror/view"
import { CodeMirror } from "."
type Measure = {cursors: Piece[]}
class Piece {
constructor(readonly left: number, readonly top: number,
readonly height: number,
readonly fontFamily: string,
readonly fontSize: string,
readonly fontWeight: string,
readonly color: string,
readonly className: string,
readonly letter: string,
readonly partial: boolean) {}
draw() {
let elt = document.createElement("div")
elt.className = this.className
this.adjust(elt)
return elt
}
adjust(elt: HTMLElement) {
elt.style.left = this.left + "px"
elt.style.top = this.top + "px"
elt.style.height = this.height + "px"
elt.style.lineHeight = this.height + "px"
elt.style.fontFamily = this.fontFamily;
elt.style.fontSize = this.fontSize;
elt.style.fontWeight = this.fontWeight;
elt.style.color = this.partial ? "transparent" : this.color;
elt.className = this.className;
elt.textContent = this.letter;
}
eq(p: Piece) {
return this.left == p.left && this.top == p.top && this.height == p.height &&
this.fontFamily == p.fontFamily && this.fontSize == p.fontSize &&
this.fontWeight == p.fontWeight && this.color == p.color &&
this.className == p.className &&
this.letter == p.letter;
}
}
export class BlockCursorPlugin {
rangePieces: readonly Piece[] = []
cursors: readonly Piece[] = []
measureReq: {read: () => Measure, write: (value: Measure) => void}
cursorLayer: HTMLElement
cm: CodeMirror
constructor(readonly view: EditorView, cm: CodeMirror) {
this.cm = cm;
this.measureReq = {read: this.readPos.bind(this), write: this.drawSel.bind(this)}
this.cursorLayer = view.scrollDOM.appendChild(document.createElement("div"))
this.cursorLayer.className = "cm-cursorLayer cm-vimCursorLayer"
this.cursorLayer.setAttribute("aria-hidden", "true")
view.requestMeasure(this.measureReq)
this.setBlinkRate()
}
setBlinkRate() {
this.cursorLayer.style.animationDuration = 1200 + "ms"
}
update(update: ViewUpdate) {
if (update.selectionSet || update.geometryChanged || update.viewportChanged) {
this.view.requestMeasure(this.measureReq)
this.cursorLayer.style.animationName = this.cursorLayer.style.animationName == "cm-blink" ? "cm-blink2" : "cm-blink"
}
}
scheduleRedraw() {
this.view.requestMeasure(this.measureReq)
}
readPos(): Measure {
let {state} = this.view
let cursors = []
for (let r of state.selection.ranges) {
let prim = r == state.selection.main
let piece = measureCursor(this.cm, this.view, r, prim)
if (piece) cursors.push(piece)
}
return {cursors}
}
drawSel({cursors}: Measure) {
if (cursors.length != this.cursors.length || cursors.some((c, i) => !c.eq(this.cursors[i]))) {
let oldCursors = this.cursorLayer.children
if (oldCursors.length !== cursors.length) {
this.cursorLayer.textContent = ""
for (const c of cursors)
this.cursorLayer.appendChild(c.draw())
} else {
cursors.forEach((c, idx) => c.adjust(oldCursors[idx] as HTMLElement))
}
this.cursors = cursors
}
}
destroy() {
this.cursorLayer.remove()
}
}
const themeSpec = {
".cm-vimMode .cm-line": {
"& ::selection": {backgroundColor: "transparent !important"},
"&::selection": {backgroundColor: "transparent !important"},
caretColor: "transparent !important",
},
".cm-fat-cursor": {
position: "absolute",
background: "#ff9696",
border: "none",
whiteSpace: "pre",
},
"&:not(.cm-focused) .cm-fat-cursor": {
background: "none",
outline: "solid 1px #ff9696",
color: "transparent !important",
},
}
export const hideNativeSelection = Prec.highest(EditorView.theme(themeSpec))
function getBase(view: EditorView) {
let rect = view.scrollDOM.getBoundingClientRect()
let left = view.textDirection == Direction.LTR ? rect.left : rect.right - view.scrollDOM.clientWidth
return {left: left - view.scrollDOM.scrollLeft, top: rect.top - view.scrollDOM.scrollTop}
}
function measureCursor(cm: CodeMirror, view: EditorView, cursor: SelectionRange, primary: boolean): Piece | null {
let head = cursor.head;
let fatCursor = false;
let hCoeff = 1;
let vim = cm.state.vim;
if (vim && (!vim.insertMode || cm.state.overwrite)) {
fatCursor = true;
if (vim.visualBlock && !primary)
return null;
if (cursor.anchor < cursor.head) head--;
if (cm.state.overwrite) hCoeff = 0.2;
else if (vim.status) hCoeff = 0.5;
}
if (fatCursor) {
let letter = head < view.state.doc.length && view.state.sliceDoc(head, head + 1);
if (letter && (/[\uDC00-\uDFFF]/.test(letter) && head > 1)) {
// step back if cursor is on the second half of a surrogate pair
head--;
letter = view.state.sliceDoc(head, head + 1);
}
let pos = view.coordsAtPos(head, 1);
if (!pos) return null;
let base = getBase(view);
let domAtPos = view.domAtPos(head);
let node = domAtPos ? domAtPos.node : view.contentDOM;
while (domAtPos && domAtPos.node instanceof HTMLElement) {
node = domAtPos.node;
domAtPos = {node: domAtPos.node.childNodes[domAtPos.offset], offset: 0};
}
if (!(node instanceof HTMLElement)) {
if (!node.parentNode) return null;
node = node.parentNode;
}
let style = getComputedStyle(node as HTMLElement);
let left = pos.left;
if (!letter || letter == "\n" || letter == "\r") {
letter = "\xa0";
} else if (letter == "\t") {
letter = "\xa0";
var nextPos = view.coordsAtPos(head + 1, -1);
if (nextPos) {
left = nextPos.left - (nextPos.left - pos.left) / parseInt(style.tabSize);
}
} else if ((/[\uD800-\uDBFF]/.test(letter) && head < view.state.doc.length - 1)) {
// include the second half of a surrogate pair in cursor
letter += view.state.sliceDoc(head + 1, head + 2);
}
let h = (pos.bottom - pos.top);
return new Piece(left - base.left, pos.top - base.top + h * (1 - hCoeff), h * hCoeff,
style.fontFamily, style.fontSize, style.fontWeight, style.color,
primary ? "cm-fat-cursor cm-cursor-primary" : "cm-fat-cursor cm-cursor-secondary",
letter, hCoeff != 1)
} else {
return null;
}
}