diff --git a/app/electron/main.js b/app/electron/main.js index 84e7fe17d..68452d5fc 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -683,6 +683,22 @@ app.whenReady().then(() => { if (data.cmd === "printToPDF") { return getWindowByContentId(data.webContentsId).webContents.printToPDF(data.pdfOptions); } + if (data.cmd === "siyuan-open-file") { + let hasMatch = false; + BrowserWindow.getAllWindows().find(item => { + if (item.webContents.id === event.sender.id) { + return; + } + const ids = decodeURIComponent(new URL(item.webContents.getURL()).hash.substring(1)).split("\u200b"); + if (ids.includes(data.options.rootID) || ids.includes(data.options.assetPath)) { + item.focus(); + item.webContents.send("siyuan-open-file", data.options); + hasMatch = true; + return true; + } + }); + return hasMatch + } }); ipcMain.on("siyuan-cmd", (event, data) => { let cmd = data; diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts index fdb31d56f..d2c628909 100644 --- a/app/src/boot/onGetConfig.ts +++ b/app/src/boot/onGetConfig.ts @@ -297,6 +297,9 @@ export const initWindow = (app: App) => { } }); } + ipcRenderer.on(Constants.SIYUAN_OPEN_FILE, (event, data) => { + openFile(data); + }); ipcRenderer.on(Constants.SIYUAN_SAVE_CLOSE, (event, close) => { if (isWindow()) { closeWindow(app); diff --git a/app/src/constants.ts b/app/src/constants.ts index 5d1d6c5f4..ae373d75e 100644 --- a/app/src/constants.ts +++ b/app/src/constants.ts @@ -39,6 +39,7 @@ export abstract class Constants { public static readonly SIYUAN_OPEN_URL: string = "siyuan-open-url"; public static readonly SIYUAN_OPEN_WINDOW: string = "siyuan-open-window"; public static readonly SIYUAN_OPEN_FOLDER: string = "siyuan-open-folder"; + public static readonly SIYUAN_OPEN_FILE: string = "siyuan-open-file"; public static readonly SIYUAN_EXPORT_PDF: string = "siyuan-export-pdf"; public static readonly SIYUAN_EXPORT_NEWWINDOW: string = "siyuan-export-newwindow"; diff --git a/app/src/editor/util.ts b/app/src/editor/util.ts index d8955db61..6f1ed73af 100644 --- a/app/src/editor/util.ts +++ b/app/src/editor/util.ts @@ -12,8 +12,7 @@ import {fetchPost, fetchSyncPost} from "../util/fetch"; import {focusBlock, focusByRange} from "../protyle/util/selection"; import {onGet} from "../protyle/util/onGet"; /// #if !BROWSER -import {shell} from "electron"; -import {BrowserWindow, getCurrentWindow} from "@electron/remote"; +import {ipcRenderer, shell} from "electron"; import {newCardModel} from "../card/newCardTab"; /// #endif import {pushBack} from "../util/backForward"; @@ -75,7 +74,7 @@ export const openAsset = (app: App, assetPath: string, page: number | string, po }); }; -export const openFile = (options: IOpenFileOptions) => { +export const openFile = async (options: IOpenFileOptions) => { if (typeof options.removeCurrentTab === "undefined") { options.removeCurrentTab = true; } @@ -172,24 +171,18 @@ export const openFile = (options: IOpenFileOptions) => { /// #if !BROWSER // https://github.com/siyuan-note/siyuan/issues/7491 - const currentWindowId = getCurrentWindow().id; - const hasMatch = BrowserWindow.getAllWindows().find(item => { - if (item.id === currentWindowId) { - return; - } - const ids = decodeURIComponent(new URL(item.webContents.getURL()).hash.substring(1)).split(Constants.ZWSP); - if (ids.includes(options.rootID) || ids.includes(options.assetPath)) { - item.focus(); - const optionsClone = Object.assign({}, options); - delete optionsClone.app; - item.webContents.executeJavaScript(`window.newWindow.openFile(${JSON.stringify(optionsClone)});`); - if (options.afterOpen) { - options.afterOpen(); - } - return true; - } - }); + let hasMatch = false; + const optionsClone = Object.assign({}, options); + delete optionsClone.app; + delete optionsClone.afterOpen; + hasMatch = await ipcRenderer.invoke(Constants.SIYUAN_GET, { + cmd: Constants.SIYUAN_OPEN_FILE, + options: optionsClone, + }) if (hasMatch) { + if (options.afterOpen) { + options.afterOpen(); + } return; } /// #endif @@ -366,9 +359,9 @@ const switchEditor = (editor: Editor, options: IOpenFileOptions, allModels: IMod updateBacklinkGraph(allModels, editor.editor.protyle); }); } else { - if (options.action.includes(Constants.CB_GET_HL)) { + if (options.action?.includes(Constants.CB_GET_HL)) { highlightById(editor.editor.protyle, options.id, true); - } else if (options.action.includes(Constants.CB_GET_FOCUS)) { + } else if (options.action?.includes(Constants.CB_GET_FOCUS)) { if (nodeElement) { const newRange = focusBlock(nodeElement); if (newRange) { diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index d06185dfb..02c98f529 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -138,10 +138,6 @@ interface Window { getBlockURL(): string } - newWindow: { - openFile(options: IOpenFileOptions): void - } - Protyle: import("../protyle/method").default goBack(): void diff --git a/app/src/window/index.ts b/app/src/window/index.ts index c9fd8f451..3c30c6c0b 100644 --- a/app/src/window/index.ts +++ b/app/src/window/index.ts @@ -148,8 +148,3 @@ class App { } new App(); - -// 再次点击新窗口已打开的 PDF 时,需进行定位 -window.newWindow = { - openFile: openFile, -};