Skip to content

Commit 48842d3

Browse files
committed
Fix bug in measuring of gap sizes for nested block wrappers
1 parent b93ffdc commit 48842d3

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/buildtile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class TileBuilder {
193193
if (this.wrappers[i].to < this.pos) this.wrappers.splice(i, 1)
194194
for (let cur = this.blockWrappers; cur.value && cur.from <= this.pos; cur.next()) if (cur.to >= this.pos) {
195195
let wrap = new OpenWrapper(cur.from, cur.to, cur.value, cur.rank), i = this.wrappers.length
196-
while (i > 0 && this.wrappers[i - 1].rank < wrap.rank) i--
196+
while (i > 0 && (this.wrappers[i - 1].rank - wrap.rank || this.wrappers[i - 1].to - wrap.to) < 0) i--
197197
this.wrappers.splice(i, 0, wrap)
198198
}
199199
this.wrapperPos = this.pos

src/docview.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,14 @@ export class DocView {
391391
for (let i = 0; i < tile.children.length; i++) {
392392
if (pos > to) break
393393
let child = tile.children[i], end = pos + child.length
394+
let childRect = (child.dom as HTMLElement).getBoundingClientRect(), {height} = childRect
395+
if (measureBounds && !i) spaceAbove += childRect.top - measureBounds.top
394396
if (child instanceof BlockWrapperTile) {
395-
if (end > from) scan(child, pos, child.dom.getBoundingClientRect())
397+
if (end > from) scan(child, pos, childRect)
396398
} else if (pos >= from) {
397-
let childRect = (child.dom as HTMLElement).getBoundingClientRect(), {height} = childRect
398-
if (measureBounds && !i) spaceAbove += childRect.top - measureBounds.top
399399
if (spaceAbove > 0) result.push(-spaceAbove)
400400
result.push(height + spaceAbove)
401401
spaceAbove = 0
402-
if (measureBounds && i == tile.children.length - 1) spaceAbove += measureBounds.bottom - childRect.bottom
403402
if (isWider) {
404403
let last = child.dom.lastChild
405404
let rects = last ? clientRectsFor(last) : []
@@ -415,6 +414,7 @@ export class DocView {
415414
}
416415
}
417416
}
417+
if (measureBounds && i == tile.children.length - 1) spaceAbove += measureBounds.bottom - childRect.bottom
418418
pos = end + child.breakAfter
419419
}
420420
}

test/webtest-draw-decoration.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {EditorView, Decoration, BlockWrapper, DecorationSet, WidgetType, ViewPlugin, BlockType} from "@codemirror/view"
1+
import {EditorView, Decoration, BlockWrapper, DecorationSet, WidgetType, ViewPlugin, BlockInfo, BlockType} from "@codemirror/view"
22
import {tempView, requireFocus} from "./tempview.js"
33
import {EditorSelection, StateEffect, StateField, Range, RangeSet} from "@codemirror/state"
44
import ist from "ist"
@@ -933,5 +933,18 @@ describe("EditorView decoration", () => {
933933
ist(Array.isArray(blocks[1].type))
934934
ist(Array.isArray(blocks[3].type))
935935
})
936+
937+
it("properly measures nested wrapper padding", () => {
938+
let cm = wrapEditor("a\nb\nc\nd", [
939+
BlockWrapper.create({tagName: "div", attributes: {style: "border: 2px solid blue"}}).range(2, 5),
940+
BlockWrapper.create({tagName: "div", attributes: {style: "border: 3px solid red"}}).range(2, 2),
941+
BlockWrapper.create({tagName: "div", attributes: {style: "border: 1px solid orange"}}).range(4, 4)
942+
])
943+
cm.measure()
944+
let gapAbove = (line: BlockInfo) => Array.isArray(line.type) ? line.type[0].height : 0
945+
ist(gapAbove(cm.viewportLineBlocks[1]), 5)
946+
ist(gapAbove(cm.viewportLineBlocks[2]), 4)
947+
ist(gapAbove(cm.viewportLineBlocks[3]), 3)
948+
})
936949
})
937950
})

0 commit comments

Comments
 (0)