Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/block-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@ export class BlockCursorPlugin {
measureReq: {read: () => Measure, write: (value: Measure) => void}
cursorLayer: HTMLElement
cm: CodeMirror
scaleX = 1
scaleY = 1

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")
this.cursorLayer.style.position = "absolute"
this.scale()
view.requestMeasure(this.measureReq)
this.setBlinkRate()
this.setBlinkRate()
}

setBlinkRate() {
Expand All @@ -78,11 +82,20 @@ export class BlockCursorPlugin {
}

update(update: ViewUpdate) {
if (update.selectionSet || update.geometryChanged || update.viewportChanged) {
if (update.selectionSet || update.geometryChanged || update.viewportChanged) {
this.scale()
this.view.requestMeasure(this.measureReq)
this.cursorLayer.style.animationName = this.cursorLayer.style.animationName == "cm-blink" ? "cm-blink2" : "cm-blink"
}
if (configChanged(update)) this.setBlinkRate();
}
if (configChanged(update)) this.setBlinkRate();
}

scale() {
let { scaleX, scaleY } = this.view
if (scaleX != this.scaleX || scaleY != this.scaleY) {
this.scaleX = scaleX; this.scaleY = scaleY
this.cursorLayer.style.transform = `scale(${1 / scaleX}, ${1 / scaleY})`
}
}

scheduleRedraw() {
Expand Down Expand Up @@ -146,7 +159,7 @@ 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}
return {left: left - view.scrollDOM.scrollLeft * view.scaleX, top: rect.top - view.scrollDOM.scrollTop * view.scaleY}
}

function measureCursor(cm: CodeMirror, view: EditorView, cursor: SelectionRange, primary: boolean): Piece | null {
Expand Down Expand Up @@ -207,10 +220,7 @@ function measureCursor(cm: CodeMirror, view: EditorView, cursor: SelectionRange,
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)
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please keep the old formatting for readability.
As for parseFloat(style.fontSize) * view.scaleX, this does not work when scaleX and scaleY are different, and when they are the same, it still causes small mismatch in the appearance of letter inside cursor and inside the editor, so i think we need to find a different way of applying the scale. E.g. maybe we can skip applying the transform online 97 and change the way we calculate sizes?

} else {
return null;
}
Expand Down