Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit 49d72c4

Browse files
committed
Improve posAtCoords in non-uniform height lines
FIX: Fix an issue where `EditorView.posAtCoords` could incorrectly return a position near a higher element on the line, in mixed-font-size lines.
1 parent 4935d24 commit 49d72c4

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/cursor.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ class InlineCoordsScan {
327327
this.y = (side.top + side.bottom) / 2
328328
return this.scan(positions, getRects)
329329
}
330+
// Handle the case where closest matched a higher element on the
331+
// same line as an element below/above the coords
332+
if (closestDx) {
333+
if (above && above.bottom > closestRect.top) {
334+
this.y = above.bottom - 1
335+
return this.scan(positions, getRects)
336+
}
337+
if (below && below.top < closestRect.bottom) {
338+
this.y = below.top + 1
339+
return this.scan(positions, getRects)
340+
}
341+
}
330342
let ltr = (bidi ? this.dirAt(positions[closestI], 1) : this.baseDir) == Direction.LTR
331343
return {
332344
i: closestI,

test/webtest-coords.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ describe("EditorView coords", () => {
243243
let c4 = cm.coordsAtPos(4)!
244244
ist(cm.posAtCoords({x: c4.left + 5, y: c4.bottom + 7}), 9)
245245
})
246+
247+
it("can handle coordinates above small text in a non-uniform line", () => {
248+
let cm = tempView("abcdefgh", [deco(Decoration.mark({attributes: {style: "font-size: 200%"}}).range(6, 7))])
249+
let c4 = cm.coordsAtPos(4)!
250+
ist(cm.posAtCoords({x: c4.left, y: c4.top - 4}), 4)
251+
})
246252
})
247253

248254
describe("coordsForChar", () => {

0 commit comments

Comments
 (0)