Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/18593

Vanessaf2fdb5f46f21
diff --git a/app/src/util/touchDragBridge.ts b/app/src/util/touchDragBridge.ts
index 7239f2335c..55596c30b4 100644
--- a/app/src/util/touchDragBridge.ts
+++ b/app/src/util/touchDragBridge.ts
@@ -1,6 +1,13 @@
import {stopScrollAnimation} from "../boot/globalEvent/dragover";
import {Constants} from "../constants";
import {isInAndroid} from "../plugin/platformUtils";
+import {
+ completeDrag,
+ dispatchWithNativeDragEnabled,
+ restoreNativeDrag,
+ shouldSuppressNativeContextMenu,
+ suspendNativeDrag,
+} from "./touchDragBridgeCore";
// 长按门槛共享状态:触摸后短时间内滑动视为滚动放行原生滚动,长按静止后再滑动才进入拖拽
interface LongPressGate {
@@ -109,8 +116,7 @@ const handleTouchStart = (e: TouchEvent) => {
if (draggable) {
dragState = createDragState(draggable, touch, "touch", isMouseInput(touch));
// WebKit 会接管 draggable 元素的长按并取消触摸序列,临时关闭原生拖拽以保留 touchend。
- draggable.setAttribute("draggable", "false");
- dragState.restoreDraggable = true;
+ suspendNativeDrag(dragState);
return;
}
}
@@ -208,9 +214,8 @@ const handleTouchEnd = (e: TouchEvent) => {
if (dragState) {
if (dragState.isDragging) {
e.preventDefault();
- endBridgeDrag(e.changedTouches[0]);
}
- cleanupDrag();
+ completeBridgeDrag(e.changedTouches[0], false);
return;
}
if (!manualState) return;
@@ -221,7 +226,7 @@ const handleTouchEnd = (e: TouchEvent) => {
const handleContextMenu = (event: MouseEvent) => {
// WebView 会在手指仍按住时派发原生长按菜单,菜单遮罩会截获后续 drop。
// 松手后由 event.ts 合成的 contextmenu 不是可信事件,需要保留以打开正常的长按菜单。
- if (event.isTrusted && (dragState || manualState)) {
+ if (shouldSuppressNativeContextMenu(event.isTrusted, !!(dragState || manualState))) {
event.preventDefault();
event.stopImmediatePropagation();
}
@@ -279,8 +284,7 @@ const handlePointerDown = (event: PointerEvent) => {
dragState = createDragState(draggable, event, "pointer", true, event.pointerId);
// Android WebView 会在原生 dragstart 后取消 Pointer 流,临时关闭 draggable 以保留 pointermove 和 pointerup。
- draggable.setAttribute("draggable", "false");
- dragState.restoreDraggable = true;
+ suspendNativeDrag(dragState);
};
const handlePointerMove = (event: PointerEvent) => {
@@ -288,8 +292,7 @@ const handlePointerMove = (event: PointerEvent) => {
return;
}
if ((event.buttons & 1) === 0) {
- dispatchBridgeDragEnd(event);
- cleanupDrag();
+ completeBridgeDrag(event, true);
return;
}
if (!dragState.isDragging) {
@@ -311,9 +314,8 @@ const handlePointerUp = (event: PointerEvent) => {
const isDragging = dragState.isDragging;
if (isDragging) {
event.preventDefault();
- endBridgeDrag(event);
}
- cleanupDrag();
+ completeBridgeDrag(event, false);
if (isDragging) {
suppressMouseClick = true;
setTimeout(() => {
@@ -324,8 +326,7 @@ const handlePointerUp = (event: PointerEvent) => {
const handlePointerCancel = (event: PointerEvent) => {
if (dragState?.inputType === "pointer" && dragState.pointerId === event.pointerId) {
- dispatchBridgeDragEnd(event);
- cleanupDrag();
+ completeBridgeDrag(event, true);
}
};
@@ -380,14 +381,8 @@ const startBridgeDrag = (touch: DragPoint) => {
dataTransfer: dt,
view: window,
});
- if (dragState.restoreDraggable) {
- // 部分拖拽处理会在 dragstart 中再次检查 draggable 属性,派发合成事件时短暂恢复。
- dragState.draggableElement.setAttribute("draggable", "true");
- }
- dragState.draggableElement.dispatchEvent(dragStartEvent);
- if (dragState.restoreDraggable) {
- dragState.draggableElement.setAttribute("draggable", "false");
- }
+ // 部分拖拽处理会在 dragstart 中再次检查 draggable 属性,派发合成事件时短暂恢复。
+ dispatchWithNativeDragEnabled(dragState, () => dragState.draggableElement.dispatchEvent(dragStartEvent));
dragState.ghostElement = window.siyuan.touchDragGhost || null;
if (dragState.ghostElement) {
@@ -462,7 +457,7 @@ const continueBridgeDrag = (touch: DragPoint) => {
positionGhost(touch.clientX, touch.clientY);
};
-const endBridgeDrag = (touch: DragPoint) => {
+const dispatchBridgeDrop = (touch: DragPoint) => {
if (!dragState.isDragging) return;
const elementUnderTouch = getElementUnderPoint(touch.clientX, touch.clientY);
@@ -477,8 +472,6 @@ const endBridgeDrag = (touch: DragPoint) => {
});
elementUnderTouch.dispatchEvent(dropEvent);
}
-
- dispatchBridgeDragEnd(touch);
};
const dispatchBridgeDragEnd = (point?: DragPoint) => {
@@ -495,13 +488,23 @@ const dispatchBridgeDragEnd = (point?: DragPoint) => {
dragState.draggableElement.dispatchEvent(dragEndEvent);
};
+const completeBridgeDrag = (point: DragPoint | undefined, canceled: boolean) => {
+ completeDrag(!!dragState?.isDragging, canceled, {
+ drop: () => {
+ if (point) {
+ dispatchBridgeDrop(point);
+ }
+ },
+ dragEnd: () => dispatchBridgeDragEnd(point),
+ cleanup: cleanupDrag,
+ });
+};
+
const cleanupDrag = () => {
stopScrollAnimation();
clearDragoverClasses();
- if (dragState?.restoreDraggable) {
- dragState.draggableElement.setAttribute("draggable", "true");
- }
+ restoreNativeDrag(dragState);
if (dragState?.ghostElement) {
dragState.ghostElement.remove();
}
@@ -513,9 +516,8 @@ const cleanupDrag = () => {
};
const handleCancel = () => {
- dispatchBridgeDragEnd();
// touchcancel 时两条路径都需无条件清理(cleanupDrag/cancelManualTouch 内部均做空状态处理)
- cleanupDrag();
+ completeBridgeDrag(undefined, true);
cancelManualTouch();
};
@@ -544,8 +546,7 @@ export const initTouchDragBridge = () => {
document.addEventListener("click", handleMouseClick, {capture: true, passive: false});
window.addEventListener("blur", () => {
if (dragState?.inputType === "pointer") {
- dispatchBridgeDragEnd();
- cleanupDrag();
+ completeBridgeDrag(undefined, true);
}
});
}
diff --git a/app/src/util/touchDragBridgeCore.test.ts b/app/src/util/touchDragBridgeCore.test.ts
new file mode 100644
index 0000000000..f7441516df
--- /dev/null
+++ b/app/src/util/touchDragBridgeCore.test.ts
@@ -0,0 +1,95 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {
+ completeDrag,
+ dispatchWithNativeDragEnabled,
+ restoreNativeDrag,
+ shouldSuppressNativeContextMenu,
+ suspendNativeDrag,
+} from "./touchDragBridgeCore";
+
+const createGuard = () => {
+ let draggable = "true";
+ const state = {
+ draggableElement: {
+ setAttribute: (name: string, value: string) => {
+ if (name === "draggable") {
+ draggable = value;
+ }
+ },
+ },
+ restoreDraggable: false,
+ };
+ return {
+ state,
+ getDraggable: () => draggable,
+ };
+};
+
+describe("touch drag native guard", () => {
+ it("suspends native dragging until the synthetic dragstart is dispatched", () => {
+ const guard = createGuard();
+
+ suspendNativeDrag(guard.state);
+ assert.equal(guard.getDraggable(), "false");
+
+ let draggableDuringDispatch = "";
+ dispatchWithNativeDragEnabled(guard.state, () => {
+ draggableDuringDispatch = guard.getDraggable();
+ });
+
+ assert.equal(draggableDuringDispatch, "true");
+ assert.equal(guard.getDraggable(), "false");
+ });
+
+ it("keeps native dragging suspended when dragstart dispatch throws", () => {
+ const guard = createGuard();
+ suspendNativeDrag(guard.state);
+
+ assert.throws(() => dispatchWithNativeDragEnabled(guard.state, () => {
+ throw new Error("dragstart failed");
+ }));
+ assert.equal(guard.getDraggable(), "false");
+ });
+
+ it("restores native dragging during cleanup", () => {
+ const guard = createGuard();
+ suspendNativeDrag(guard.state);
+
+ restoreNativeDrag(guard.state);
+
+ assert.equal(guard.getDraggable(), "true");
+ });
+});
+
+describe("touch drag completion", () => {
+ const runCompletion = (isDragging: boolean, canceled: boolean) => {
+ const calls: string[] = [];
+ completeDrag(isDragging, canceled, {
+ drop: () => calls.push("drop"),
+ dragEnd: () => calls.push("dragend"),
+ cleanup: () => calls.push("cleanup"),
+ });
+ return calls;
+ };
+
+ it("dispatches drop and dragend before cleanup after a successful drag", () => {
+ assert.deepEqual(runCompletion(true, false), ["drop", "dragend", "cleanup"]);
+ });
+
+ it("dispatches dragend before cleanup when a drag is canceled", () => {
+ assert.deepEqual(runCompletion(true, true), ["dragend", "cleanup"]);
+ });
+
+ it("only cleans up when the long press did not become a drag", () => {
+ assert.deepEqual(runCompletion(false, false), ["cleanup"]);
+ });
+});
+
+describe("touch drag context menu", () => {
+ it("only suppresses a trusted native menu during an active gesture", () => {
+ assert.equal(shouldSuppressNativeContextMenu(true, true), true);
+ assert.equal(shouldSuppressNativeContextMenu(false, true), false);
+ assert.equal(shouldSuppressNativeContextMenu(true, false), false);
+ });
+});
diff --git a/app/src/util/touchDragBridgeCore.ts b/app/src/util/touchDragBridgeCore.ts
new file mode 100644
index 0000000000..9e5bc9b8a2
--- /dev/null
+++ b/app/src/util/touchDragBridgeCore.ts
@@ -0,0 +1,48 @@
+interface NativeDragGuard {
+ draggableElement: Pick<HTMLElement, "setAttribute">;
+ restoreDraggable?: boolean;
+}
+
+interface DragCompletionCallbacks {
+ drop: () => void;
+ dragEnd: () => void;
+ cleanup: () => void;
+}
+
+export const suspendNativeDrag = (state: NativeDragGuard) => {
+ state.draggableElement.setAttribute("draggable", "false");
+ state.restoreDraggable = true;
+};
+
+export const dispatchWithNativeDragEnabled = (state: NativeDragGuard, dispatch: () => void) => {
+ if (!state.restoreDraggable) {
+ dispatch();
+ return;
+ }
+ state.draggableElement.setAttribute("draggable", "true");
+ try {
+ dispatch();
+ } finally {
+ state.draggableElement.setAttribute("draggable", "false");
+ }
+};
+
+export const restoreNativeDrag = (state: NativeDragGuard | null) => {
+ if (state?.restoreDraggable) {
+ state.draggableElement.setAttribute("draggable", "true");
+ }
+};
+
+export const completeDrag = (isDragging: boolean, canceled: boolean, callbacks: DragCompletionCallbacks) => {
+ if (isDragging) {
+ if (!canceled) {
+ callbacks.drop();
+ }
+ callbacks.dragEnd();
+ }
+ callbacks.cleanup();
+};
+
+export const shouldSuppressNativeContextMenu = (isTrusted: boolean, hasActiveGesture: boolean) => {
+ return isTrusted && hasActiveGesture;
+};