Skip to content
Closed
Show file tree
Hide file tree
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
20 changes: 6 additions & 14 deletions app/src/block/popover.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {BlockPanel} from "./Panel";
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName,} from "../protyle/util/hasClosest";
import {fetchPost, fetchSyncPost} from "../util/fetch";
import {hideTooltip, showTooltip} from "../dialog/tooltip";
import {hideTooltip, showTooltip, showTooltipIfPointerOver} from "../dialog/tooltip";
import {getIdFromSYProtocol, isLocalPath} from "../util/pathName";
import {App} from "../index";
import {Constants} from "../constants";
Expand All @@ -10,7 +10,6 @@ import {isTouchDevice} from "../util/functions";
import {escapeAriaLabel, escapeHtml} from "../util/escape";

let popoverTargetElement: HTMLElement;
let notebookItemElement: HTMLElement | false;
export const initBlockPopover = (app: App) => {
let timeout: number;
let timeoutHide: number;
Expand Down Expand Up @@ -110,9 +109,9 @@ export const initBlockPopover = (app: App) => {
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}`;
}
try {
showTooltip(decodeURIComponent(assetTip), aElement, tooltipClass, event, tooltipSpace);
showTooltipIfPointerOver(decodeURIComponent(assetTip), aElement, tooltipClass, event, tooltipSpace);
} catch (e) {
showTooltip(assetTip, aElement, tooltipClass, event, tooltipSpace);
showTooltipIfPointerOver(assetTip, aElement, tooltipClass, event, tooltipSpace);
}
});
tip = "";
Expand All @@ -121,20 +120,13 @@ export const initBlockPopover = (app: App) => {
}
}

notebookItemElement = hasClosestByClassName(event.target, "b3-list-item__text");
const notebookItemElement = hasClosestByClassName(event.target, "b3-list-item__text");
if (notebookItemElement && notebookItemElement.parentElement.getAttribute("data-type") === "navigation-root") {
fetchPost("/api/notebook/getNotebookInfo", {notebook: notebookItemElement.parentElement.parentElement.getAttribute("data-url")}, (response) => {
const boxData = response.data.boxInfo;
const tip = `${boxData.name} <small class='ft__on-surface'>${boxData.hSize}</small>${boxData.docCount !== 0 ? window.siyuan.languages.includeSubFile.replace("x", boxData.docCount) : ""}<br>${window.siyuan.languages.modifiedAt} ${boxData.hMtime}<br>${window.siyuan.languages.createdAt} ${boxData.hCtime}`;
const scopeNotebookItemElement = hasClosestByClassName(event.target, "b3-list-item__text");
if (notebookItemElement && scopeNotebookItemElement && (notebookItemElement === scopeNotebookItemElement)) {
showTooltip(tip, notebookItemElement);
}
if (scopeNotebookItemElement &&
scopeNotebookItemElement.parentElement.getAttribute("data-type") === "navigation-root" &&
scopeNotebookItemElement.parentElement.parentElement.getAttribute("data-url") === boxData.id) {
scopeNotebookItemElement.setAttribute("aria-label", tip);
}
showTooltipIfPointerOver(tip, notebookItemElement);
notebookItemElement.setAttribute("aria-label", tip);
});
}

Expand Down
25 changes: 25 additions & 0 deletions app/src/dialog/tooltip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import {isMobile} from "../util/functions";

export const isPointerOverElement = (target: Element) => {
const coords = window.siyuan.coordinates;
if (!coords) {
return false;
}
const underMouse = document.elementFromPoint(coords.clientX, coords.clientY) as HTMLElement;
return !!underMouse && (target === underMouse || target.contains(underMouse));
};
Comment on lines +3 to +10

/**
* 在异步回调中显示 tooltip。回调返回时会检查鼠标指针是否仍在 target 上,避免鼠标已离开后仍弹出 tooltip。
*/
export const showTooltipIfPointerOver = (
message: string,
target: Element,
tooltipClass?: string,
event?: MouseEvent,
space: number = 0.5,
) => {
if (!isPointerOverElement(target)) {
return;
}
showTooltip(message, target, tooltipClass, event, space);
};

export const showTooltip = (
message: string,
target: Element,
Expand Down
4 changes: 2 additions & 2 deletions app/src/layout/Tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Constants} from "../constants";
import {escapeHtml, escapeLessThans} from "../util/escape";
import {unicode2Emoji} from "../emoji";
import {fetchPost} from "../util/fetch";
import {hideTooltip, showTooltip} from "../dialog/tooltip";
import {hideTooltip, showTooltipIfPointerOver} from "../dialog/tooltip";
/// #if !BROWSER
import {openNewWindow} from "../window/openNewWindow";
import {ipcRenderer} from "electron";
Expand Down Expand Up @@ -73,7 +73,7 @@ export class Tab {
id
}, (response) => {
if (!this.headElement.getAttribute("aria-label")) {
showTooltip(escapeLessThans(response.data), this.headElement);
showTooltipIfPointerOver(escapeLessThans(response.data), this.headElement);
}
this.headElement.setAttribute("aria-label", escapeLessThans(response.data));
});
Expand Down
Loading