From 649ab0caf0f774261936cd56a3f9e4a3a7ac2481 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Sun, 21 Dec 2025 19:41:57 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/pull/16579 --- app/electron/main.js | 45 ++++-------------------------------------- app/src/editor/util.ts | 1 + 2 files changed, 5 insertions(+), 41 deletions(-) diff --git a/app/electron/main.js b/app/electron/main.js index 6550f9e54..9f8e845ce 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -824,35 +824,12 @@ app.whenReady().then(() => { } if (data.cmd === "siyuan-open-file") { let hasMatch = false; - const senderWindow = BrowserWindow.fromWebContents(event.sender); - let senderPort = ""; - try { - if (senderWindow && !senderWindow.isDestroyed()) { - const senderUrl = new URL(senderWindow.getURL()); - senderPort = senderUrl.port; - } - } catch (e) { - // 如果无法获取发送消息的窗口的端口,则不进行工作空间检查 - } BrowserWindow.getAllWindows().find(item => { - if (item.webContents.id === event.sender.id) { + const url = new URL(item.webContents.getURL()); + if (item.webContents.id === event.sender.id || data.port !== url.port) { return; } - // 只检查属于同一个工作空间的窗口(通过端口判断) - // 修复:在工作空间 B 中点击块引用时,会激活工作空间 A 的新窗口 - // https://github.com/siyuan-note/siyuan/issues/16559 - if (senderPort) { - try { - const itemUrl = new URL(item.webContents.getURL()); - if (itemUrl.port !== senderPort) { - return; // 不同工作空间,跳过 - } - } catch (e) { - // 如果无法解析 URL,跳过该窗口 - return; - } - } - const ids = decodeURIComponent(new URL(item.webContents.getURL()).hash.substring(1)).split("\u200b"); + const ids = decodeURIComponent(url.hash.substring(1)).split("\u200b"); const options = JSON.parse(data.options); if (ids.includes(options.rootID) || ids.includes(options.assetPath)) { item.focus(); @@ -1093,7 +1070,6 @@ app.whenReady().then(() => { mainWindow.show(); }); ipcMain.on("siyuan-open-window", (event, data) => { - const senderWindow = BrowserWindow.fromWebContents(event.sender); const mainWindow = BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0]; const mainBounds = mainWindow.getBounds(); const mainScreen = screen.getDisplayNearestPoint({x: mainBounds.x, y: mainBounds.y}); @@ -1126,20 +1102,7 @@ app.whenReady().then(() => { } win.webContents.userAgent = "SiYuan/" + appVer + " https://b3log.org/siyuan Electron " + win.webContents.userAgent; win.webContents.session.setSpellCheckerLanguages(["en-US"]); - // 始终使用发送消息的窗口的 origin(协议和主机),确保新窗口连接到正确的工作空间 - // 修复:在工作空间 A 中以新窗口打开文档后,在工作空间 B 无法再打开该文档 - // https://github.com/siyuan-note/siyuan/issues/16559 - let finalUrl = data.url; - try { - if (senderWindow && !senderWindow.isDestroyed()) { - const senderUrlObj = new URL(senderWindow.getURL()); - const receivedUrlObj = new URL(data.url); - finalUrl = data.url.replace(receivedUrlObj.origin, senderUrlObj.origin); - } - } catch (e) { - // 如果 URL 解析失败,使用原始 URL - } - win.loadURL(finalUrl); + win.loadURL(data.url); windowNavigate(win); win.on("close", (event) => { if (win && !win.isDestroyed()) { diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts index ad9d2c7e0..86b7922c8 100644 --- a/app/src/editor/util.ts +++ b/app/src/editor/util.ts @@ -213,6 +213,7 @@ export const openFile = async (options: IOpenFileOptions) => { hasMatch = await ipcRenderer.invoke(Constants.SIYUAN_GET, { cmd: Constants.SIYUAN_OPEN_FILE, options: JSON.stringify(optionsClone), + port: location.port, }); if (hasMatch) { if (options.afterOpen) {