Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve bubble tooltip positioning in multiline ranges (#706) #3228

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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