Skip to content

Commit

Permalink
Prevent a bad height estimate in line-wrapped editors from moving the…
Browse files Browse the repository at this point in the history
… initial scroll position

FIX: Fix a bug where in a line-wrapped editor, with some content, the initial
scroll position would be off from the top of the document.

Closes codemirror/dev#1360
  • Loading branch information
marijnh committed Mar 25, 2024
1 parent e05dae1 commit 3c923a3
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/heightmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ class HeightMapGap extends HeightMap {
blockAt(height: number, oracle: HeightOracle, top: number, offset: number) {
let {firstLine, lastLine, perLine, perChar} = this.heightMetrics(oracle, offset)
if (oracle.lineWrapping) {
let guess = offset + Math.round(Math.max(0, Math.min(1, (height - top) / this.height)) * this.length)
let guess = offset + (height < oracle.lineHeight ? 0
: Math.round(Math.max(0, Math.min(1, (height - top) / this.height)) * this.length))
let line = oracle.doc.lineAt(guess), lineHeight = perLine + line.length * perChar
let lineTop = Math.max(top, height - lineHeight / 2)
return new BlockInfo(line.from, line.length, lineTop, lineHeight, 0)
Expand Down

0 comments on commit 3c923a3

Please sign in to comment.