Vanessa 2025-12-16 11:14:04 +08:00
parent db4fb41024
commit 43d2654213
2 changed files with 21 additions and 15 deletions

View file

@ -81,19 +81,20 @@ export const initBlockPopover = (app: App) => {
} }
} }
let tooltipSpace: number | undefined; let tooltipSpace: number | undefined;
if (!tip) { if (!tip && aElement.getAttribute("data-type") === "inline-memo") {
tip = escapeHtml(aElement.getAttribute("data-inline-memo-content")); tip = escapeHtml(aElement.getAttribute("data-inline-memo-content"));
if (tip) { tooltipClass = "memo"; // 为行级备注添加 class https://github.com/siyuan-note/siyuan/issues/6161
tooltipClass = "memo"; // 为行级备注添加 class https://github.com/siyuan-note/siyuan/issues/6161 tooltipSpace = 0; // tooltip 和备注元素之间不能有空隙 https://github.com/siyuan-note/siyuan/issues/14796#issuecomment-3649757267
tooltipSpace = 0; // tooltip 和备注元素之间不能有空隙 https://github.com/siyuan-note/siyuan/issues/14796#issuecomment-3649757267
}
} }
if (!tip) { if (!tip) {
if (aElement.getAttribute("data-type") === "a") {
tooltipClass = "href"; // 为超链接添加 class https://github.com/siyuan-note/siyuan/issues/11440#issuecomment-2119080691
tooltipSpace = 0;
}
const href = aElement.getAttribute("data-href") || ""; const href = aElement.getAttribute("data-href") || "";
// 链接地址强制换行 https://github.com/siyuan-note/siyuan/issues/11539 // 链接地址强制换行 https://github.com/siyuan-note/siyuan/issues/11539
if (href) { if (href) {
tip = `<span style="word-break: break-all">${href.substring(0, Constants.SIZE_TITLE)}</span>`; tip = `<span style="word-break: break-all">${href.substring(0, Constants.SIZE_TITLE)}</span>`;
tooltipClass = "href"; // 为超链接添加 class https://github.com/siyuan-note/siyuan/issues/11440#issuecomment-2119080691
} }
const title = aElement.getAttribute("data-title"); const title = aElement.getAttribute("data-title");
if (tip && isLocalPath(href) && !aElement.classList.contains("b3-tooltips")) { if (tip && isLocalPath(href) && !aElement.classList.contains("b3-tooltips")) {
@ -106,7 +107,7 @@ export const initBlockPopover = (app: App) => {
} else { } else {
assetTip += ` ${response.data.hSize}${title ? '<div class="fn__hr"></div><span>' + title + "</span>" : ""}<br>${window.siyuan.languages.modifiedAt} ${response.data.hUpdated}<br>${window.siyuan.languages.createdAt} ${response.data.hCreated}`; assetTip += ` ${response.data.hSize}${title ? '<div class="fn__hr"></div><span>' + title + "</span>" : ""}<br>${window.siyuan.languages.modifiedAt} ${response.data.hUpdated}<br>${window.siyuan.languages.createdAt} ${response.data.hCreated}`;
} }
showTooltip(assetTip, aElement, tooltipClass); showTooltip(assetTip, aElement, tooltipClass, event, tooltipSpace);
}); });
tip = ""; tip = "";
} else if (title) { } else if (title) {

View file

@ -1,16 +1,22 @@
import {isMobile} from "../util/functions"; import {isMobile} from "../util/functions";
export const showTooltip = (message: string, target: Element, tooltipClass?: string, event?: MouseEvent, space: number = 0.5) => { export const showTooltip = (
if (isMobile()) { message: string,
target: Element,
tooltipClass?: string,
event?: MouseEvent,
space: number = 0.5
) => {
if (isMobile() || !message) {
return; return;
} }
let targetRect = target.getBoundingClientRect(); let targetRect = target.getBoundingClientRect();
// 跨行元素 // 跨行元素
const clientRects = target.getClientRects(); const clientRects = Array.from(target.getClientRects());
if (clientRects.length > 1) { if (clientRects.length > 1) {
if (event) { if (event) {
// 选择包含鼠标的矩形 // 选择鼠标附近的矩形
Array.from(clientRects).forEach(item => { clientRects.forEach(item => {
if (event.clientY >= item.top - 3 && event.clientY <= item.bottom) { if (event.clientY >= item.top - 3 && event.clientY <= item.bottom) {
targetRect = item; targetRect = item;
} }
@ -18,7 +24,7 @@ export const showTooltip = (message: string, target: Element, tooltipClass?: str
} else { } else {
// 选择宽度最大的矩形 // 选择宽度最大的矩形
let lastWidth = 0; let lastWidth = 0;
Array.from(clientRects).forEach(item => { clientRects.forEach(item => {
if (item.width > lastWidth) { if (item.width > lastWidth) {
targetRect = item; targetRect = item;
} }
@ -26,11 +32,10 @@ export const showTooltip = (message: string, target: Element, tooltipClass?: str
}); });
} }
} }
if (targetRect.height === 0 || !message) { if (targetRect.height === 0) {
hideTooltip(); hideTooltip();
return; return;
} }
const messageElement = document.getElementById("tooltip"); const messageElement = document.getElementById("tooltip");
messageElement.className = tooltipClass ? `tooltip tooltip--${tooltipClass}` : "tooltip"; messageElement.className = tooltipClass ? `tooltip tooltip--${tooltipClass}` : "tooltip";
messageElement.innerHTML = message; messageElement.innerHTML = message;