Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Prevent panel item activation during slide transitions https://github.com/siyuan-note/siyuan/issues/20185

Vanessa342c5c511c64
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 626d0c7276..2c2caf339c 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -32,6 +32,7 @@ import {getDockHotkey} from "./hotkey";
import {resolveDockPanelVisibility} from "./panelVisibility";
import {syncDockEntryOrders} from "../../config/entryVisibility/runtime";
import {isWindow} from "../../util/functions";
+import {bindPanelTransitionGuard} from "../../util/panelTransition";
const TYPES = ["file", "outline", "inbox", "bookmark", "tag", "graph", "globalGraph", "backlink", "agentChat"];
const DEFAULT_DOCK_SIZE = 232;
@@ -89,6 +90,7 @@ export class Dock {
this.app = options.app;
this.position = options.position;
this.pin = options.data.pin;
+ bindPanelTransitionGuard(this.layout.element, () => this.isFloating());
this.data = {};
if (options.data.data[0]) {
this.genButton(options.data.data[0], 0);
diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts
index df957d9eab..c81916baa7 100644
--- a/app/src/mobile/index.ts
+++ b/app/src/mobile/index.ts
@@ -67,6 +67,7 @@ import {nbsp2space} from "../protyle/util/normalizeText";
import {armKeyboardLock, callMobileAppShowKeyboard, canInput, setWebViewFocusable} from "./util/mobileAppUtil";
import {hideAllElements} from "../protyle/ui/hideElements";
import {initTouchDragBridge} from "../util/touchDragBridge";
+import {bindPanelTransitionGuard} from "../util/panelTransition";
import {appearanceConfigApi} from "../config/tabs/appearanceRuntime";
import {openByMobile} from "../editor/openLink";
import {initHarmonyTextSelectionMenu} from "../util/harmonyTextSelectionMenu";
@@ -298,6 +299,9 @@ class App {
}
}
});
+ ["sidebar", "sidebarRight"].forEach(id => {
+ bindPanelTransitionGuard(document.getElementById(id));
+ });
initTouchDragBridge();
});
}
diff --git a/app/src/util/panelTransition.test.ts b/app/src/util/panelTransition.test.ts
new file mode 100644
index 0000000000..d8bd1c824d
--- /dev/null
+++ b/app/src/util/panelTransition.test.ts
@@ -0,0 +1,102 @@
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {it} from "node:test";
+import * as assert from "node:assert/strict";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+class Transition {
+ transitionProperty = "transform";
+ playState = "running";
+ pending = false;
+}
+
+class MouseAction extends Event {
+ constructor(type: string, public detail = 1) {
+ super(type, {cancelable: true});
+ }
+}
+
+const fixture = () => {
+ const exports: {bindPanelTransitionGuard?: (element: object, enabled: () => boolean) => void} = {};
+ const source = transpileModule(readFileSync(join(__dirname, "panelTransition.ts"), "utf8"), {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2020},
+ }).outputText;
+ runInNewContext(source, {exports, CSSTransition: Transition, MouseEvent: MouseAction});
+ const animations: Transition[] = [];
+ const element = Object.assign(new EventTarget(), {getAnimations: () => animations});
+ let enabled = true;
+ let activations = 0;
+ exports.bindPanelTransitionGuard(element, () => enabled);
+ element.addEventListener("click", () => activations++);
+ const send = (type: string, detail = 1) => {
+ const event = new MouseAction(type, detail);
+ element.dispatchEvent(event);
+ return event.defaultPrevented;
+ };
+ const command = () => element.dispatchEvent(Object.assign(new Event("click", {cancelable: true}), {detail: "file"}));
+ return {animations, send, command, disable: () => enabled = false, activations: () => activations};
+};
+
+it("blocks item activation while a panel slides in or out", () => {
+ for (const type of ["pointerdown", "touchstart", "mousedown", "mouseup", "click", "dblclick", "contextmenu"]) {
+ const panel = fixture();
+ panel.animations.push(new Transition());
+ assert.equal(panel.send(type), true, type);
+ assert.equal(panel.activations(), 0);
+ }
+});
+
+it("consumes a press started during movement even when its click arrives after settling", () => {
+ for (const type of ["pointerdown", "touchstart", "mousedown"]) {
+ const panel = fixture();
+ panel.animations.push(new Transition());
+ panel.send(type);
+ panel.animations.length = 0;
+ assert.equal(panel.send("mouseup"), true);
+ assert.equal(panel.send("click"), true);
+ assert.equal(panel.activations(), 0);
+ assert.equal(panel.send("pointerdown"), false);
+ assert.equal(panel.send("click"), false);
+ assert.equal(panel.activations(), 1);
+ }
+});
+
+it("allows stable, cancelled, and zero-duration panels without a delay", () => {
+ for (const playState of [undefined, "finished", "idle"]) {
+ const panel = fixture();
+ if (playState) {
+ const transition = new Transition();
+ transition.playState = playState;
+ panel.animations.push(transition);
+ }
+ assert.equal(panel.send("click"), false);
+ assert.equal(panel.activations(), 1);
+ }
+});
+
+it("preserves stable keyboard activation and ignores opacity-only or disabled guards", () => {
+ const panel = fixture();
+ const transition = new Transition();
+ panel.animations.push(transition);
+ panel.send("touchstart");
+ panel.animations.length = 0;
+ assert.equal(panel.send("click", 0), false);
+ assert.equal(panel.activations(), 1);
+ transition.transitionProperty = "opacity";
+ panel.animations.push(transition);
+ panel.send("pointerdown");
+ assert.equal(panel.send("click"), false);
+ transition.transitionProperty = "transform";
+ panel.disable();
+ assert.equal(panel.send("click"), false);
+ assert.equal(panel.activations(), 3);
+});
+
+it("keeps mobile sidebar rendering commands available during movement", () => {
+ const panel = fixture();
+ panel.animations.push(new Transition());
+ panel.send("touchstart");
+ assert.equal(panel.command(), true);
+ assert.equal(panel.activations(), 1);
+});
diff --git a/app/src/util/panelTransition.ts b/app/src/util/panelTransition.ts
new file mode 100644
index 0000000000..fa0a1616dd
--- /dev/null
+++ b/app/src/util/panelTransition.ts
@@ -0,0 +1,37 @@
+export const bindPanelTransitionGuard = (element: HTMLElement, isEnabled = () => true) => {
+ let blockedPress = false;
+ const isMoving = () => isEnabled() && element.getAnimations().some(animation =>
+ animation instanceof CSSTransition && animation.transitionProperty === "transform" &&
+ (animation.playState === "running" || animation.pending));
+ const cancel = (event: Event) => {
+ event.preventDefault();
+ event.stopImmediatePropagation();
+ };
+ const startPress = (event: Event) => {
+ blockedPress = isMoving();
+ if (blockedPress) {
+ cancel(event);
+ }
+ };
+ element.addEventListener("pointerdown", startPress, true);
+ element.addEventListener("touchstart", startPress, {capture: true, passive: false});
+ const activate = (event: MouseEvent) => {
+ if (!(event instanceof MouseEvent)) {
+ return;
+ }
+ if (!isEnabled()) {
+ blockedPress = false;
+ return;
+ }
+ const moving = isMoving();
+ // 动画期间开始的按压不激活条目,延迟到达的点击保留到下一次按压前拦截。
+ if (!moving && (!blockedPress || event.detail === 0)) {
+ return;
+ }
+ blockedPress = true;
+ cancel(event);
+ };
+ ["mousedown", "mouseup", "click", "dblclick", "contextmenu"].forEach(type => {
+ element.addEventListener(type, activate, true);
+ });
+};