Skip to content

Commit d92351e

Browse files
committed
support css scaling
1 parent 0d9dfd2 commit d92351e

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/block-cursor.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,19 @@ export class BlockCursorPlugin {
6060
measureReq: {read: () => Measure, write: (value: Measure) => void}
6161
cursorLayer: HTMLElement
6262
cm: CodeMirror
63+
scaleX = 1
64+
scaleY = 1
6365

6466
constructor(readonly view: EditorView, cm: CodeMirror) {
6567
this.cm = cm;
6668
this.measureReq = {read: this.readPos.bind(this), write: this.drawSel.bind(this)}
6769
this.cursorLayer = view.scrollDOM.appendChild(document.createElement("div"))
6870
this.cursorLayer.className = "cm-cursorLayer cm-vimCursorLayer"
6971
this.cursorLayer.setAttribute("aria-hidden", "true")
72+
this.cursorLayer.style.position = "absolute"
73+
this.scale()
7074
view.requestMeasure(this.measureReq)
71-
this.setBlinkRate()
75+
this.setBlinkRate()
7276
}
7377

7478
setBlinkRate() {
@@ -78,11 +82,20 @@ export class BlockCursorPlugin {
7882
}
7983

8084
update(update: ViewUpdate) {
81-
if (update.selectionSet || update.geometryChanged || update.viewportChanged) {
85+
if (update.selectionSet || update.geometryChanged || update.viewportChanged) {
86+
this.scale()
8287
this.view.requestMeasure(this.measureReq)
8388
this.cursorLayer.style.animationName = this.cursorLayer.style.animationName == "cm-blink" ? "cm-blink2" : "cm-blink"
84-
}
85-
if (configChanged(update)) this.setBlinkRate();
89+
}
90+
if (configChanged(update)) this.setBlinkRate();
91+
}
92+
93+
scale() {
94+
let { scaleX, scaleY } = this.view
95+
if (scaleX != this.scaleX || scaleY != this.scaleY) {
96+
this.scaleX = scaleX; this.scaleY = scaleY
97+
this.cursorLayer.style.transform = `scale(${1 / scaleX}, ${1 / scaleY})`
98+
}
8699
}
87100

88101
scheduleRedraw() {
@@ -146,7 +159,7 @@ export const hideNativeSelection = Prec.highest(EditorView.theme(themeSpec))
146159
function getBase(view: EditorView) {
147160
let rect = view.scrollDOM.getBoundingClientRect()
148161
let left = view.textDirection == Direction.LTR ? rect.left : rect.right - view.scrollDOM.clientWidth
149-
return {left: left - view.scrollDOM.scrollLeft, top: rect.top - view.scrollDOM.scrollTop}
162+
return {left: left - view.scrollDOM.scrollLeft * view.scaleX, top: rect.top - view.scrollDOM.scrollTop * view.scaleY}
150163
}
151164

152165
function measureCursor(cm: CodeMirror, view: EditorView, cursor: SelectionRange, primary: boolean): Piece | null {
@@ -207,10 +220,7 @@ function measureCursor(cm: CodeMirror, view: EditorView, cursor: SelectionRange,
207220
letter += view.state.sliceDoc(head + 1, head + 2);
208221
}
209222
let h = (pos.bottom - pos.top);
210-
return new Piece(left - base.left, pos.top - base.top + h * (1 - hCoeff), h * hCoeff,
211-
style.fontFamily, style.fontSize, style.fontWeight, style.color,
212-
primary ? "cm-fat-cursor cm-cursor-primary" : "cm-fat-cursor cm-cursor-secondary",
213-
letter, hCoeff != 1)
223+
return new Piece(left - base.left, pos.top - base.top + h * (1 - hCoeff), h * hCoeff, style.fontFamily, parseFloat(style.fontSize) * view.scaleX + "px", style.fontWeight, style.color, primary ? "cm-fat-cursor cm-cursor-primary" : "cm-fat-cursor cm-cursor-secondary", letter, hCoeff != 1)
214224
} else {
215225
return null;
216226
}

0 commit comments

Comments
 (0)