Skip to content

Commit

Permalink
fix: Improve bubble tooltip positioning in multiline ranges (slab#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
mumpo committed Nov 16, 2020
1 parent 54d6141 commit 9804d46
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions themes/bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,8 @@ class BubbleTooltip extends BaseTooltip {
this.root.style.left = '0px';
this.root.style.width = '';
this.root.style.width = `${this.root.offsetWidth}px`;
const lines = this.quill.getLines(range.index, range.length);
if (lines.length === 1) {
this.position(this.quill.getBounds(range));
} else {
const lastLine = lines[lines.length - 1];
const index = this.quill.getIndex(lastLine);
const length = Math.min(
lastLine.length() - 1,
range.index + range.length - index,
);
const indexBounds = this.quill.getBounds(new Range(index, length));
this.position(indexBounds);
}
const rangePosition = this.calculateRangePosition(range);
this.position(rangePosition);
} else if (
document.activeElement !== this.textbox &&
this.quill.hasFocus()
Expand All @@ -60,7 +49,8 @@ class BubbleTooltip extends BaseTooltip {
if (this.root.classList.contains('ql-hidden')) return;
const range = this.quill.getSelection();
if (range != null) {
this.position(this.quill.getBounds(range));
const rangePosition = this.calculateRangePosition(range);
this.position(rangePosition);
}
}, 1);
});
Expand All @@ -70,6 +60,21 @@ class BubbleTooltip extends BaseTooltip {
this.show();
}

calculateRangePosition(range) {
const lines = this.quill.getLines(range.index, range.length);
if (lines.length === 1) {
return this.quill.getBounds(range);
}

const lastLine = lines[lines.length - 1];
const index = this.quill.getIndex(lastLine);
const length = Math.min(
lastLine.length() - 1,
range.index + range.length - index,
);
return this.quill.getBounds(new Range(index, length));
}

position(reference) {
const shift = super.position(reference);
const arrow = this.root.querySelector('.ql-tooltip-arrow');
Expand Down

0 comments on commit 9804d46

Please sign in to comment.