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: Relay block dragging across desktop windows https://github.com/siyuan-note/siyuan/issues/10096
Daniel7e5aaf638a9c
diff --git a/app/electron/main.js b/app/electron/main.jsindex dbe8b81e36..dc0d2af617 100644--- a/app/electron/main.js+++ b/app/electron/main.js@@ -99,6 +99,7 @@ const pendingRemoteOpenURLs = [];const blockDragSessions = new Map();const blockDragWindowFocusOrder = new Map();let blockDragWindowFocusSequence = 0;+const blockDragCursorPollInterval = 16;const getBlockDragSessionKey = (sourceWebContentsId, dragId) => {return sourceWebContentsId + ":" + dragId;@@ -174,7 +175,7 @@ const isPointInBounds = (point, bounds) => {point.screenY >= bounds.y && point.screenY < bounds.y + bounds.height;};-const getBlockDragTarget = (dragSession) => {+const getBlockDragTarget = (dragSession, expectedTargetWebContentsId) => {if (!dragSession.point) {return;}@@ -189,6 +190,9 @@ const getBlockDragTarget = (dragSession) => {!initializedWindowIds.has(item.webContents.id) || !item.isVisible() || item.isMinimized()) {return false;}+ if (expectedTargetWebContentsId !== undefined && item.webContents.id !== expectedTargetWebContentsId) {+ return false;+ }const kernelTarget = windowKernelTargets.get(item.webContents.id);return kernelTarget?.origin === dragSession.kernelOrigin && isPointInBounds(dragSession.point,item.getContentBounds());@@ -202,7 +206,7 @@ const getBlockDragTarget = (dragSession) => {(blockDragWindowFocusOrder.get(first.webContents.id) || 0);});const targetWindow = candidates[0];- if (!targetWindow || (sourceContainsPoint &&+ if (!targetWindow || (expectedTargetWebContentsId === undefined && sourceContainsPoint &&(sourceWindow.isAlwaysOnTop() || !targetWindow.isAlwaysOnTop()))) {// 普通目标窗口位于源窗口后方时,不应让指针穿透源窗口。return;@@ -239,8 +243,8 @@ const sendBlockDragRoute = (dragSession, remote, sequence = dragSession.point?.s});};-const routeBlockDragMove = (dragSession, notifySource = true) => {- const target = getBlockDragTarget(dragSession);+const routeBlockDragMove = (dragSession, notifySource = true, expectedTargetWebContentsId) => {+ const target = getBlockDragTarget(dragSession, expectedTargetWebContentsId);if (!target) {leaveBlockDragTarget(dragSession);if (notifySource) {@@ -293,8 +297,26 @@ const routeBlockDragMove = (dragSession, notifySource = true) => {return {target, ready: true};};+const startBlockDragCursorPolling = (sessionKey, dragSession) => {+ dragSession.cursorPollInterval = setInterval(() => {+ if (blockDragSessions.get(sessionKey) !== dragSession || dragSession.dropping || !dragSession.point) {+ return;+ }+ const cursorPoint = screen.getCursorScreenPoint();+ if (cursorPoint.x === dragSession.point.screenX && cursorPoint.y === dragSession.point.screenY) {+ return;+ }+ dragSession.point = {+ ...dragSession.point,+ screenX: cursorPoint.x,+ screenY: cursorPoint.y,+ };+ routeBlockDragMove(dragSession);+ }, blockDragCursorPollInterval);+};+const dispatchBlockDragDrop = (dragSession) => {- const dragRoute = routeBlockDragMove(dragSession, false);+ const dragRoute = routeBlockDragMove(dragSession, false, dragSession.dropTargetWebContentsId);if (dragRoute?.target && !dragRoute.ready) {dragSession.waitingForDropPayload = true;return;@@ -317,10 +339,23 @@ const dispatchBlockDragDrop = (dragSession) => {}const dropSequence = dragSession.pendingDropSequence;dragSession.pendingDropSequence = undefined;+ if (dragSession.dropTargetWebContentsId !== undefined) {+ sendBlockDragMessage(dragSession.sourceWebContentsId, {+ phase: "complete",+ dragId: dragSession.dragId,+ canceled: true,+ });+ finishBlockDragSession(getBlockDragSessionKey(dragSession.sourceWebContentsId, dragSession.dragId),+ dragSession);+ return;+ }sendBlockDragRoute(dragSession, false, dropSequence);};const finishBlockDragSession = (sessionKey, dragSession) => {+ if (dragSession.cursorPollInterval) {+ clearInterval(dragSession.cursorPollInterval);+ }if (dragSession.targetWebContentsId) {sendBlockDragMessage(dragSession.targetWebContentsId, {phase: "end",@@ -330,6 +365,33 @@ const finishBlockDragSession = (sessionKey, dragSession) => {blockDragSessions.delete(sessionKey);};+const dropBlockDragFromTarget = (targetWebContentsId, data) => {+ const targetKernel = windowKernelTargets.get(targetWebContentsId);+ const point = normalizeBlockDragPoint(data.point);+ if (!targetKernel || !initializedWindowIds.has(targetWebContentsId) || !point) {+ return;+ }+ for (const dragSession of blockDragSessions.values()) {+ if (dragSession.sourceWebContentsId === targetWebContentsId || dragSession.dropping ||+ dragSession.kernelOrigin !== targetKernel.origin) {+ continue;+ }+ updateBlockDragCursorPoint(dragSession, {+ ...point,+ sequence: dragSession.point?.sequence || 0,+ });+ const target = getBlockDragTarget(dragSession, targetWebContentsId);+ if (!target) {+ continue;+ }+ dragSession.dropping = true;+ dragSession.dropTargetWebContentsId = targetWebContentsId;+ dragSession.pendingDropSequence = dragSession.point?.sequence || 0;+ dispatchBlockDragDrop(dragSession);+ return;+ }+};+const cleanupBlockDragSessions = (webContentsId) => {for (const [sessionKey, dragSession] of blockDragSessions) {if (dragSession.sourceWebContentsId === webContentsId) {@@ -3621,7 +3683,14 @@ app.whenReady().then(() => {});});ipcMain.on("siyuan-block-drag", (event, data) => {- if (!data || typeof data !== "object" || typeof data.dragId !== "string" || !data.dragId ||+ if (!data || typeof data !== "object") {+ return;+ }+ if (data.phase === "release") {+ dropBlockDragFromTarget(event.sender.id, data);+ return;+ }+ if (typeof data.dragId !== "string" || !data.dragId ||!["begin", "move", "payload", "drop", "drop-ack", "end"].includes(data.phase)) {return;}@@ -3637,6 +3706,15 @@ app.whenReady().then(() => {}sourceDragSession.pendingDropSequence = undefined;sourceDragSession.waitingForDropPayload = false;+ if (sourceDragSession.dropTargetWebContentsId !== undefined) {+ sendBlockDragMessage(sourceDragSession.sourceWebContentsId, {+ phase: "complete",+ dragId: sourceDragSession.dragId,+ remote: true,+ });+ finishBlockDragSession(sourceSessionKey, sourceDragSession);+ return;+ }sendBlockDragRoute(sourceDragSession, true, data.sequence);return;}@@ -3654,7 +3732,7 @@ app.whenReady().then(() => {}const point = normalizeBlockDragPoint(data.point);const payload = normalizeBlockDragPayload(data.payload);- blockDragSessions.set(sessionKey, {+ const dragSession = {dragId: data.dragId,sourceWebContentsId,kernelOrigin: kernelTarget.origin,@@ -3662,7 +3740,9 @@ app.whenReady().then(() => {payload,payloadRequested: false,targetWebContentsId: undefined,- });+ };+ blockDragSessions.set(sessionKey, dragSession);+ startBlockDragCursorPolling(sessionKey, dragSession);return;}const dragSession = blockDragSessions.get(sessionKey);diff --git a/app/src/util/touchDragBridge.ts b/app/src/util/touchDragBridge.tsindex e92caf68bf..bb0f716ce4 100644--- a/app/src/util/touchDragBridge.ts+++ b/app/src/util/touchDragBridge.ts@@ -13,7 +13,8 @@ import {hasActiveTouchGesture,isDragRelaySource,restoreNativeDrag,- shouldCancelPointerDragOnBlur,+ shouldCancelPointerDragAfterWindowExit,+ shouldRequestForeignMouseDrop,shouldRequireLongPress,shouldSuppressNativeContextMenu,suspendNativeDrag,@@ -89,8 +90,9 @@ interface BlockDragRelayPayload {}interface BlockDragRelayMessage {- phase: "request" | "route" | "enter" | "move" | "leave" | "drop" | "drop-ack" | "end";+ phase: "request" | "route" | "enter" | "move" | "leave" | "drop" | "drop-ack" | "complete" | "end";dragId: string;+ canceled?: boolean;point?: DragPoint;payload?: BlockDragRelayPayload;remote?: boolean;@@ -128,6 +130,7 @@ interface ForeignDragState {dataTransfer: DataTransfer;dragId: string;dropped: boolean;+ dropRequested: boolean;idleTimeout?: number;lastDragOverElement: Element | null;lastPoint?: DragPoint;@@ -861,6 +864,12 @@ const handleBlockDragRelay = (message: BlockDragRelayMessage) => {}return;}+ if (message.phase === "complete") {+ if (dragState?.relayId === message.dragId) {+ completeBridgeDrag(undefined, !!message.canceled, !!message.remote);+ }+ return;+ }if (message.phase === "enter") {const dataTransfer = createForeignDataTransfer(message.payload);if (!dataTransfer) {@@ -875,6 +884,7 @@ const handleBlockDragRelay = (message: BlockDragRelayMessage) => {dataTransfer,dragId: message.dragId,dropped: false,+ dropRequested: false,lastDragOverElement: null,};resetForeignDragIdleTimeout();@@ -917,6 +927,18 @@ const handleBlockDragRelay = (message: BlockDragRelayMessage) => {}};+const handleForeignMouseUp = (event: MouseEvent) => {+ if (!shouldRequestForeignMouseDrop(!!isInAndroid(), event.button, !!foreignDragState,+ !!foreignDragState?.dropRequested)) {+ return;+ }+ foreignDragState.dropRequested = true;+ sendBlockDragRelay({+ phase: "release",+ point: copyDragPoint(event),+ });+};+let stopScrollAfterRefresh = false;const dragRefreshQueue = createDragRefreshQueue(() => {if (pendingWheelScroll) {@@ -1005,7 +1027,8 @@ const handleDragKey = (event: KeyboardEvent) => {const handleLostPointerCapture = (event: PointerEvent) => {if (dragState?.inputType === "pointer" && dragState.pointerId === event.pointerId &&- !dragState.pendingDropPoint) {+ !dragState.pendingDropPoint &&+ shouldCancelPointerDragAfterWindowExit(!!isInAndroid(), dragState.isDragging, !!dragState.relayId)) {completeBridgeDrag(undefined, true);}};@@ -1063,9 +1086,11 @@ export const initTouchDragBridge = () => {document.documentElement.addEventListener("pointerleave", handlePointerLeave, {passive: true});document.addEventListener("lostpointercapture", handleLostPointerCapture, {capture: true, passive: true});document.addEventListener("click", handleMouseClick, {capture: true, passive: false});+ document.addEventListener("mouseup", handleForeignMouseUp, {capture: true, passive: true});window.addEventListener("blur", () => {if (dragState?.inputType === "pointer" &&- shouldCancelPointerDragOnBlur(!!isInAndroid(), dragState.isDragging, !!dragState.relayId)) {+ shouldCancelPointerDragAfterWindowExit(!!isInAndroid(), dragState.isDragging,+ !!dragState.relayId)) {completeBridgeDrag(undefined, true);}});diff --git a/app/src/util/touchDragBridgeCore.test.ts b/app/src/util/touchDragBridgeCore.test.tsindex a0a52b8124..fd2bd67644 100644--- a/app/src/util/touchDragBridgeCore.test.ts+++ b/app/src/util/touchDragBridgeCore.test.ts@@ -9,7 +9,8 @@ import {hasActiveTouchGesture,isDragRelaySource,restoreNativeDrag,- shouldCancelPointerDragOnBlur,+ shouldCancelPointerDragAfterWindowExit,+ shouldRequestForeignMouseDrop,shouldRequireLongPress,shouldSuppressNativeContextMenu,suspendNativeDrag,@@ -139,11 +140,19 @@ describe("desktop mouse drag candidates", () => {assert.equal(shouldRequireLongPress(true, false, false), true);});- it("keeps an active desktop relay alive when its source window loses focus", () => {- assert.equal(shouldCancelPointerDragOnBlur(false, true, true), false);- assert.equal(shouldCancelPointerDragOnBlur(false, false, true), true);- assert.equal(shouldCancelPointerDragOnBlur(false, true, false), true);- assert.equal(shouldCancelPointerDragOnBlur(true, true, true), true);+ it("keeps an active desktop relay alive after its source window loses capture", () => {+ assert.equal(shouldCancelPointerDragAfterWindowExit(false, true, true), false);+ assert.equal(shouldCancelPointerDragAfterWindowExit(false, false, true), true);+ assert.equal(shouldCancelPointerDragAfterWindowExit(false, true, false), true);+ assert.equal(shouldCancelPointerDragAfterWindowExit(true, true, true), true);+ });++ it("requests a drop when the mouse is released over a foreign desktop window", () => {+ assert.equal(shouldRequestForeignMouseDrop(false, 0, true, false), true);+ assert.equal(shouldRequestForeignMouseDrop(false, 1, true, false), false);+ assert.equal(shouldRequestForeignMouseDrop(false, 0, false, false), false);+ assert.equal(shouldRequestForeignMouseDrop(false, 0, true, true), false);+ assert.equal(shouldRequestForeignMouseDrop(true, 0, true, false), false);});});diff --git a/app/src/util/touchDragBridgeCore.ts b/app/src/util/touchDragBridgeCore.tsindex cbb8bfa643..84fb1f8de4 100644--- a/app/src/util/touchDragBridgeCore.ts+++ b/app/src/util/touchDragBridgeCore.ts@@ -34,10 +34,15 @@ export const shouldRequireLongPress = (isLongPressTarget: boolean, isMouse: boolreturn isLongPressTarget && (!isMouse || isAndroid);};-export const shouldCancelPointerDragOnBlur = (isAndroid: boolean, isDragging: boolean, hasRelay: boolean) => {+export const shouldCancelPointerDragAfterWindowExit = (isAndroid: boolean, isDragging: boolean, hasRelay: boolean) => {return isAndroid || !isDragging || !hasRelay;};+export const shouldRequestForeignMouseDrop = (isAndroid: boolean, button: number, hasForeignDrag: boolean,+ dropRequested: boolean) => {+ return !isAndroid && button === 0 && hasForeignDrag && !dropRequested;+};+export const getWheelScrollDelta = (delta: number, deltaMode: number, lineSize: number, pageSize: number) => {if (deltaMode === 1) {return delta * lineSize;