An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve touch interactions inside tooltips https://github.com/siyuan-note/siyuan/issues/20324
Danieldbe946f77bcd
diff --git a/app/src/block/popover.ts b/app/src/block/popover.tsindex caad0ab0f0..1470e5c3d7 100644--- a/app/src/block/popover.ts+++ b/app/src/block/popover.ts@@ -2,7 +2,7 @@ import {BlockPanel} from "./Panel";import {isAbove} from "../util/zIndex";import {hasClosestByAttribute, hasClosestByClassName,} from "../protyle/util/hasClosest";import {fetchPost, fetchSyncPost} from "../util/fetch";-import {hideTooltip, showTooltip} from "../dialog/tooltip";+import {hideTooltip, isInteractiveTooltipTarget, showTooltip} from "../dialog/tooltip";import {isEncryptedBox, isLocalPath, parseSiYuanUriInfo} from "../util/pathName";import type {App} from "../index";import {Constants} from "../constants";@@ -141,6 +141,12 @@ export const initBlockPopover = (app: App) => {// 编辑器内容块引用/backlinks/tag/bookmark/套娃中使用document.addEventListener("mouseover", (event: MouseEvent & { target: HTMLElement, path: HTMLElement[] }) => {logAndroidInputEvent(event);+ if (isTouchHoverInput() && isInteractiveTooltipTarget(event.target)) {+ cancelPopoverTimers();+ tooltipAbortController?.abort();+ tooltipAbortController = null;+ return;+ }if (!waitForMove) {pointerX = event.clientX;pointerY = event.clientY;diff --git a/app/src/block/popoverInteraction.test.ts b/app/src/block/popoverInteraction.test.tsindex 51ee2d6eb4..c0f7e8c33c 100644--- a/app/src/block/popoverInteraction.test.ts+++ b/app/src/block/popoverInteraction.test.ts@@ -92,6 +92,7 @@ const createHarness = (android = false) => {return false;};let touch = false;+ let tooltipHides = 0;const globals = {window: {siyuan, JSAndroid: android ? {} : undefined, setTimeout: setTimer, addEventListener() {}},document: {@@ -128,7 +129,11 @@ const createHarness = (android = false) => {"../util/pathName": {isEncryptedBox: () => false, isLocalPath: () => false,parseSiYuanUriInfo: (href: string) => ({id: href.split("/").pop()})},"../protyle/render/av/cellOverflow": {shouldMeasureAVCellContentOverflow: () => false},- "../dialog/tooltip": {hideTooltip() {}, showTooltip() {}},+ "../dialog/tooltip": {+ hideTooltip: () => { tooltipHides++; }, showTooltip() {},+ isInteractiveTooltipTarget: (target: TestElement) => Boolean(closest(target,+ item => item.classes.has("tooltip--interactive") && !item.classes.has("fn__none"))),+ },"../constants": {Constants: {TIMEOUT_INPUT: 5}},"../util/functions": {isTouchDevice: () => touch},"../util/hoverInput": {isTouchHoverInput: () => touch},@@ -168,6 +173,7 @@ const createHarness = (android = false) => {};return {element, ref, fire, advance, api, lifecycle, panels, requests, siyuan, handlers, open, close,pendingTimers: () => timers.size,+ tooltipHides: () => tooltipHides,setTouch: (value: boolean) => { touch = value; }};};@@ -187,6 +193,21 @@ test("touch-generated mouseover does not schedule hover timers and cancels pendiassert.ok(h.pendingTimers() > 0);});+test("touching an interactive tooltip preserves its content and cancels hover timers", () => {+ const h = createHarness();+ h.fire("mouseover");+ assert.ok(h.pendingTimers() > 0);+ const tooltip = h.element({}, ["tooltip", "tooltip--interactive"]);+ const link = h.element({}, [], tooltip);+ const hides = h.tooltipHides();+ h.setTouch(true);+ h.fire("mouseover", link);+ assert.equal(h.pendingTimers(), 0);+ assert.equal(h.tooltipHides(), hides);+ h.fire("mouseover", h.ref);+ assert.equal(h.tooltipHides(), hides + 1);+});+test("clicks cancel pending previews until actual motion, including within the same target", async () => {for (const button of [0, 1, 2]) {const h = createHarness();diff --git a/app/src/dialog/tooltip.ts b/app/src/dialog/tooltip.tsindex b4330baa8f..cbf05d45e7 100644--- a/app/src/dialog/tooltip.ts+++ b/app/src/dialog/tooltip.ts@@ -2,6 +2,15 @@ import {isMobile} from "../util/functions";import {emitToPlugins, forEachPluginSubscriber} from "../plugin/EventBusCore";import {initHoverInput, isTouchHoverInput} from "../util/hoverInput";+const INTERACTIVE_TOOLTIP_SELECTOR = "a, button, input, select, textarea, [contenteditable='true'], [tabindex], [role='button']";++// 已显示的交互提示保留内部操作,不因切换输入源而提前关闭。+export const isInteractiveTooltipTarget = (target: EventTarget | null) => {+ const tooltip = document.getElementById("tooltip");+ return target instanceof Node && Boolean(tooltip?.classList.contains("tooltip--interactive")) &&+ !tooltip.classList.contains("fn__none") && tooltip.contains(target);+};+// 无编辑器的窗口复用提示框渲染,不注册块预览事件。export const initTooltips = () => {initHoverInput();@@ -38,9 +47,16 @@ export const showTooltip = (space: number = 0.5,positionOverride?: string,) => {- if (isMobile() || !message || (isTouchHoverInput() && event && tooltipClass !== "error")) {+ if (isMobile() || !message) {return;}+ if (isTouchHoverInput() && event && tooltipClass !== "error") {+ const content = document.createElement("div");+ content.innerHTML = window.DOMPurify.sanitize(message);+ if (!content.querySelector(INTERACTIVE_TOOLTIP_SELECTOR)) {+ return;+ }+ }const messageElement = document.getElementById("tooltip");const showDetail = {message,@@ -80,12 +96,14 @@ export const showTooltip = (return;}messageElement.className = tooltipClass ? `tooltip tooltip--${tooltipClass}` : "tooltip";- messageElement.classList.toggle("tooltip--hover", !!event && tooltipClass !== "error");messageElement.innerHTML = window.DOMPurify.sanitize(message);+ const interactive = Boolean(messageElement.querySelector(INTERACTIVE_TOOLTIP_SELECTOR));+ messageElement.classList.toggle("tooltip--interactive", interactive);+ messageElement.classList.toggle("tooltip--hover", !!event && tooltipClass !== "error" && !interactive);// 避免原本的 top 和 left 影响计算messageElement.removeAttribute("style");// 普通提示不拦截目标点击,包含链接或控件的提示仍可交互。- if (!messageElement.querySelector("a, button, input, select, textarea, [contenteditable='true'], [tabindex], [role='button']")) {+ if (!interactive) {messageElement.style.pointerEvents = "none";}const position = positionOverride || target.getAttribute("data-position");diff --git a/app/src/dialog/tooltipInput.test.ts b/app/src/dialog/tooltipInput.test.tsindex afe7f6c954..da1f3bbd56 100644--- a/app/src/dialog/tooltipInput.test.ts+++ b/app/src/dialog/tooltipInput.test.ts@@ -82,6 +82,36 @@ body {margin:0}.file-tree {position:absolute;top:40px;left:40px;width:232px}}touch("pointerover", "pen");check.equal(input.isTouchHoverInput(), false, "pen hover remains available");+ tooltip.showTooltip('<a id="tipLink" href="#test">Open</a><button id="tipButton">Action</button>' ++ '<input id="tipInput" value="Editable">', more, "memo", new MouseEvent("mouseover"));+ check.equal(tip.classList.contains("tooltip--interactive"), true);+ check.equal(tip.classList.contains("tooltip--hover"), false);+ let linkClicks = 0;+ let buttonClicks = 0;+ document.getElementById("tipLink").addEventListener("click", event => {+ event.preventDefault();+ linkClicks++;+ });+ document.getElementById("tipButton").addEventListener("click", () => { buttonClicks++; });+ await new Promise(resolve => setTimeout(resolve, 500));+ for (const id of ["tipLink", "tipButton", "tipInput"]) {+ const element = document.getElementById(id);+ check.equal(tooltip.isInteractiveTooltipTarget(element), true);+ const rect = element.getBoundingClientRect();+ await require("electron").ipcRenderer.invoke("tap", rect.left + rect.width / 2, rect.top + rect.height / 2);+ await new Promise(resolve => setTimeout(resolve, 30));+ check.notEqual(getComputedStyle(tip).display, "none");+ }+ check.equal(linkClicks, 1, "a touch tap follows the link once");+ check.equal(buttonClicks, 1, "a touch tap activates the button once");+ check.equal(document.activeElement.id, "tipInput", "touch can focus the tooltip input");+ tooltip.showTooltip('<a href="#test">Interactive</a>', more, "memo", new MouseEvent("mouseover"));+ check.equal(tip.classList.contains("tooltip--interactive"), true, "interactive content remains available in touch mode");+ tooltip.hideTooltip();+ check.equal(tooltip.isInteractiveTooltipTarget(tip.firstElementChild), false, "hidden tooltips are not interactive targets");+ touch("pointerover", "mouse");+ tooltip.showTooltip("Plain", more, undefined, new MouseEvent("mouseover"));+ check.equal(tip.classList.contains("tooltip--interactive"), false, "plain content clears the interactive state");touch("pointermove", "touch");assertTouch();return "Touch hover input cases passed";@@ -111,6 +141,11 @@ app.whenReady().then(async () => {await win.webContents.debugger.sendCommand("Emulation.setTouchEmulationEnabled", {enabled:true});ipcMain.handle("hover", (_event, x, y) => win.webContents.debugger.sendCommand("Input.dispatchMouseEvent",{type:"mouseMoved",x,y}));+ ipcMain.handle("tap", async (_event, x, y) => {+ await win.webContents.debugger.sendCommand("Input.dispatchTouchEvent",+ {type:"touchStart",touchPoints:[{x,y}]});+ await win.webContents.debugger.sendCommand("Input.dispatchTouchEvent", {type:"touchEnd",touchPoints:[]});+ });const result = await win.webContents.executeJavaScript(${JSON.stringify("const __name = value => value; (" +browserCases.toString() + ")(" + JSON.stringify(sources) + "," + JSON.stringify(css) + ")")});if (result !== "Touch hover input cases passed") {throw new Error(result);}