2022-05-26 15:18:53 +08:00
|
|
|
import {setPosition} from "../util/setPosition";
|
|
|
|
|
import {isMobile} from "../util/functions";
|
|
|
|
|
|
2022-11-15 10:55:05 +08:00
|
|
|
export const showTooltip = (message: string, target: Element, error = false) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
if (isMobile()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const targetRect = target.getBoundingClientRect();
|
|
|
|
|
if (targetRect.height === 0 || !message) {
|
|
|
|
|
hideTooltip();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let messageElement = document.getElementById("tooltip");
|
|
|
|
|
if (!messageElement) {
|
|
|
|
|
document.body.insertAdjacentHTML("beforeend", `<div class="tooltip" id="tooltip">${message}</div>`);
|
|
|
|
|
messageElement = document.getElementById("tooltip");
|
|
|
|
|
} else {
|
|
|
|
|
messageElement.innerHTML = message;
|
|
|
|
|
}
|
2023-07-12 15:13:37 +08:00
|
|
|
if (target.parentElement.getAttribute("data-type") === "navigation-file") {
|
|
|
|
|
const parentRect = target.parentElement.getBoundingClientRect();
|
2023-07-12 18:11:21 +08:00
|
|
|
setPosition(messageElement, parentRect.right + 8, parentRect.top);
|
2023-07-12 11:46:58 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2022-11-15 10:55:05 +08:00
|
|
|
if (error) {
|
|
|
|
|
messageElement.classList.add("tooltip--error");
|
|
|
|
|
} else {
|
|
|
|
|
messageElement.classList.remove("tooltip--error");
|
|
|
|
|
}
|
|
|
|
|
if (target.getAttribute("data-inline-memo-content")) {
|
2022-10-12 15:47:40 +08:00
|
|
|
messageElement.classList.add("tooltip--memo"); // 为行级备注添加 class https://github.com/siyuan-note/siyuan/issues/6161
|
2023-09-13 09:36:50 +08:00
|
|
|
} else {
|
|
|
|
|
messageElement.classList.remove("tooltip--memo");
|
2022-10-12 15:47:40 +08:00
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
let left = targetRect.left;
|
2023-09-13 09:36:50 +08:00
|
|
|
if (target.getAttribute("data-position") === "right") {
|
2022-05-26 15:18:53 +08:00
|
|
|
left = targetRect.right - messageElement.clientWidth;
|
|
|
|
|
}
|
2023-09-13 12:07:17 +08:00
|
|
|
const bottomHeight = window.innerHeight - targetRect.bottom;
|
2023-09-13 09:36:50 +08:00
|
|
|
messageElement.style.maxHeight = Math.max(targetRect.top, bottomHeight) + "px";
|
|
|
|
|
if (targetRect.top > bottomHeight) {
|
|
|
|
|
messageElement.style.top = (targetRect.top - messageElement.clientHeight) + "px";
|
|
|
|
|
} else {
|
|
|
|
|
messageElement.style.top = targetRect.bottom + "px";
|
|
|
|
|
}
|
|
|
|
|
if (left + messageElement.clientWidth > window.innerWidth) {
|
|
|
|
|
messageElement.style.left = (window.innerWidth - messageElement.clientWidth) + "px";
|
|
|
|
|
} else {
|
|
|
|
|
messageElement.style.left = Math.max(0, left) + "px";
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const hideTooltip = () => {
|
|
|
|
|
const messageElement = document.getElementById("tooltip");
|
|
|
|
|
if (messageElement) {
|
|
|
|
|
messageElement.remove();
|
|
|
|
|
}
|
|
|
|
|
};
|