Skip to content

Commit a6086db

Browse files
authored
Fix: [Regions] improve overlapping content (#3373)
* Fix: [Regions] improve overlapping content * Refactor
1 parent e1550f3 commit a6086db

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/plugins/regions.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ class SingleRegion extends EventEmitter<RegionEvents> {
294294
this.content = document.createElement('div')
295295
const isMarker = this.start === this.end
296296
this.content.style.padding = `0.2em ${isMarker ? 0.2 : 0.4}em`
297+
this.content.style.display = 'inline-block'
297298
this.content.textContent = content
298299
} else {
299300
this.content = content
@@ -428,17 +429,18 @@ class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPluginOptions
428429
// Check that the label doesn't overlap with other labels
429430
// If it does, push it down until it doesn't
430431
const div = region.content as HTMLElement
431-
const labelLeft = div.getBoundingClientRect().left
432-
const labelWidth = region.element.scrollWidth
432+
const box = div.getBoundingClientRect()
433433

434434
const overlap = this.regions
435-
.filter((reg) => {
436-
if (reg === region || !reg.content) return false
437-
const left = reg.content.getBoundingClientRect().left
438-
const width = reg.element.scrollWidth
439-
return labelLeft < left + width && left < labelLeft + labelWidth
435+
.map((reg) => {
436+
if (reg === region || !reg.content) return 0
437+
438+
const otherBox = reg.content.getBoundingClientRect()
439+
if (box.left < otherBox.left + otherBox.width && otherBox.left < box.left + box.width) {
440+
return otherBox.height
441+
}
442+
return 0
440443
})
441-
.map((reg) => reg.content?.getBoundingClientRect().height || 0)
442444
.reduce((sum, val) => sum + val, 0)
443445

444446
div.style.marginTop = `${overlap}px`

0 commit comments

Comments
 (0)