Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
Make sure tooltips don't overlap
Browse files Browse the repository at this point in the history
FIX: Move tooltips to avoid overlapping between them, when necessary.

See https://discuss.codemirror.net/t/cm6-merging-lint-and-hover-tooltips-over-the-same-range/3010
  • Loading branch information
marijnh committed Mar 17, 2021
1 parent 2519b69 commit e7bd397
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/tooltip.ts
Expand Up @@ -87,7 +87,8 @@ const tooltipPlugin = ViewPlugin.fromClass(class {

writeMeasure(measured: Measured) {
let {editor} = measured
for (let i = 0; i < this.tooltipViews.length; i++) {
let others = []
for (let i = 0; i < this.tooltips.length; i++) {
let tooltip = this.tooltips[i], tView = this.tooltipViews[i], {dom} = tView
let pos = measured.pos[i], size = measured.size[i]
// Hide tooltips that are outside of the editor.
Expand All @@ -102,14 +103,18 @@ const tooltipPlugin = ViewPlugin.fromClass(class {
if (!tooltip.strictSide &&
(above ? pos.top - (size.bottom - size.top) < 0 : pos.bottom + (size.bottom - size.top) > measured.innerHeight))
above = !above
let top = above ? pos.top - height : pos.bottom, right = left + width
for (let r of others) if (r.left < right && r.right > left && r.top < top + height && r.bottom > top)
top = above ? r.top - height : r.bottom
if (ios) {
dom.style.top = ((above ? pos.top - height : pos.bottom) - editor.top) + "px"
dom.style.top = (top - editor.top) + "px"
dom.style.left = (left - editor.left) + "px"
dom.style.position = "absolute"
} else {
dom.style.top = (above ? pos.top - height : pos.bottom) + "px"
dom.style.top = top + "px"
dom.style.left = left + "px"
}
others.push({left, top, right, bottom: top + height})
dom.classList.toggle("cm-tooltip-above", above)
dom.classList.toggle("cm-tooltip-below", !above)
if (tView.positioned) tView.positioned()
Expand Down

0 comments on commit e7bd397

Please sign in to comment.