Skip to content

Commit b55fe95

Browse files
committed
fix(search): Fix search result position.
1 parent bdf139c commit b55fe95

File tree

1 file changed

+34
-49
lines changed

1 file changed

+34
-49
lines changed

assets/webpage.util.txt.js

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -427,68 +427,53 @@ class SuggestionsList {
427427

428428
window.SuggestionsList = SuggestionsList;
429429

430-
function positionTooltip(originRect, tooltipEl, options) {
430+
function positionTooltip(targetPosition, tooltip, options) {
431431
options = options || {};
432432

433-
const gap = options.gap || 0;
434-
const verticalPreference = options.preference || 'bottom';
435-
const containerEl = options.offsetParent || tooltipEl.offsetParent || tooltipEl.doc.documentElement;
436-
const horizontalAlignment = options.horizontalAlignment || 'left';
433+
tooltip.style.display = 'block';
437434

438-
const containerScrollTop = containerEl.scrollTop + 10;
439-
const containerBottom = containerEl.scrollTop + containerEl.clientHeight - 10;
435+
const gap = options.gap !== undefined ? options.gap : 0;
436+
const preference = options.preference !== undefined ? options.preference : 'bottom';
437+
const offsetParent = options.offsetParent !== undefined ? options.offsetParent : tooltip.offsetParent || tooltip.ownerDocument.documentElement;
438+
const horizontalAlignment = options.horizontalAlignment !== undefined ? options.horizontalAlignment : 'left';
440439

441-
const tooltipTop = Math.min(originRect.top, containerBottom);
442-
const tooltipBottom = Math.max(originRect.bottom, containerScrollTop);
440+
const topBound = Math.min(targetPosition.top, offsetParent.scrollTop + offsetParent.clientHeight - tooltip.offsetHeight - 10);
441+
const bottomBound = Math.max(targetPosition.bottom, offsetParent.scrollTop + 10);
443442

444-
const tooltipHeight = tooltipEl.offsetHeight;
443+
const isAboveTarget = targetPosition.top - offsetParent.scrollTop >= tooltip.offsetHeight + gap;
444+
const isBelowTarget = offsetParent.scrollTop + offsetParent.clientHeight - targetPosition.bottom >= tooltip.offsetHeight + gap;
445445

446-
let tooltipVerticalPosition = 0;
447-
let result;
446+
let top = 0;
447+
let result = '';
448448

449-
if (tooltipTop - containerScrollTop >= tooltipHeight + gap) {
450-
// Fits on top
451-
result = 'top';
452-
tooltipVerticalPosition = tooltipTop - gap - tooltipHeight;
453-
} else if (containerBottom - originRect.bottom >= tooltipHeight + gap) {
454-
// Fits on bottom
455-
result = 'bottom';
456-
tooltipVerticalPosition = tooltipBottom + gap;
457-
} else {
458-
// Overlap
459-
result = 'overlap';
460-
461-
if (verticalPreference === 'top') {
462-
tooltipVerticalPosition = containerScrollTop + gap;
449+
if (!isAboveTarget || (preference === 'bottom' && isBelowTarget)) {
450+
if (offsetParent.clientHeight < tooltip.offsetHeight + gap) {
451+
top = offsetParent.scrollTop;
452+
result = 'overlap';
453+
} else if (preference === 'top') {
454+
top = offsetParent.scrollTop + gap;
455+
result = 'overlap';
463456
} else {
464-
tooltipVerticalPosition = containerBottom - tooltipHeight;
457+
top = bottomBound + gap;
458+
result = 'bottom';
465459
}
466-
}
467-
468-
let horizontalPosition
469-
470-
const containerLeft = containerEl.scrollLeft + 10;
471-
const containerRight = containerEl.scrollLeft + containerEl.clientWidth - 10;
472-
473-
const tooltipWidth = tooltipEl.offsetWidth;
474-
475-
if (horizontalAlignment === 'left') {
476-
horizontalPosition = originRect.left;
477460
} else {
478-
horizontalPosition = originRect.right - tooltipWidth;
461+
top = topBound - gap;
462+
result = 'top';
479463
}
480464

481-
horizontalPosition = Math.max(horizontalPosition, containerLeft);
482-
horizontalPosition = Math.min(horizontalPosition, containerRight - tooltipWidth);
465+
let left = horizontalAlignment === 'left' ? targetPosition.left : targetPosition.right - tooltip.offsetWidth;
466+
467+
if (left < offsetParent.scrollLeft + 10) {
468+
left = offsetParent.scrollLeft + 10;
469+
} else if (left + tooltip.offsetWidth > offsetParent.scrollLeft + offsetParent.clientWidth - 10) {
470+
left = offsetParent.scrollLeft + offsetParent.clientWidth - tooltip.offsetWidth - 10;
471+
}
483472

484-
tooltipEl.style.top = `${tooltipVerticalPosition}px`;
485-
tooltipEl.style.left = `${horizontalPosition}px`;
473+
tooltip.style.top = `${top}px`;
474+
tooltip.style.left = `${left}px`;
486475

487-
return {
488-
top: tooltipVerticalPosition,
489-
left: horizontalPosition,
490-
result
491-
};
476+
return { top, left, result };
492477
}
493478

494479
class SearchView {
@@ -518,7 +503,7 @@ class SearchView {
518503
document.addEventListener("click", this.onDocumentClick.bind(this))
519504
}
520505

521-
addMessage(text) {
506+
addMessage(text) {
522507
const messageEl = document.createElement('div');
523508
messageEl.classList.add("search-message");
524509
this.resultEl.appendChild(messageEl);

0 commit comments

Comments
 (0)