浏览代码

:art: https://github.com/siyuan-note/siyuan/issues/9368 open new window

Vanessa 1 年之前
父节点
当前提交
56f4b6274d
共有 6 个文件被更改,包括 35 次插入31 次删除
  1. 16 0
      app/electron/main.js
  2. 3 0
      app/src/boot/onGetConfig.ts
  3. 1 0
      app/src/constants.ts
  4. 15 22
      app/src/editor/util.ts
  5. 0 4
      app/src/types/index.d.ts
  6. 0 5
      app/src/window/index.ts

+ 16 - 0
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;

+ 3 - 0
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);

+ 1 - 0
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";

+ 15 - 22
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) {

+ 0 - 4
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

+ 0 - 5
app/src/window/index.ts

@@ -148,8 +148,3 @@ class App {
 }
 
 new App();
-
-// 再次点击新窗口已打开的 PDF 时,需进行定位
-window.newWindow = {
-    openFile: openFile,
-};