Skip to content

Commit c79da23

Browse files
authored
fix(Regions): region overlap detection code (#4270)
If calls to addRegion() came in all at once, we'd end up in a situation where all overlapping regions moved themselves to avoid each other (because they were all working with the same list of regions). This is incorrect, the first region shouldn't move at all. We leverage the ordering of the `regions` array to ensure that only the later overlapping regions move.
1 parent bf2f42f commit c79da23

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/plugins/regions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,16 +639,20 @@ class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPluginOptions
639639
setTimeout(() => {
640640
// Check that the label doesn't overlap with other labels
641641
// If it does, push it down until it doesn't
642+
// only check regions that are before us in the list -- otherwise
643+
// both overlapping regions will try to move down away from each other.
642644
const div = region.content as HTMLElement
643645
const box = div.getBoundingClientRect()
644646

645-
const overlap = this.regions
647+
const regionIndex = this.regions.indexOf(region)
648+
649+
const overlap = this.regions.slice(0, regionIndex)
646650
.map((reg) => {
647651
if (reg === region || !reg.content) return 0
648652

649653
const otherBox = reg.content.getBoundingClientRect()
650654
if (box.left < otherBox.left + otherBox.width && otherBox.left < box.left + box.width) {
651-
return otherBox.height
655+
return otherBox.height + 2
652656
}
653657
return 0
654658
})

0 commit comments

Comments
 (0)