This commit is contained in:
Vanessa 2023-10-08 23:17:51 +08:00
parent 797716fa1d
commit 56f4b6274d
6 changed files with 35 additions and 31 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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";

View file

@ -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) {

View file

@ -138,10 +138,6 @@ interface Window {
getBlockURL(): string
}
newWindow: {
openFile(options: IOpenFileOptions): void
}
Protyle: import("../protyle/method").default
goBack(): void

View file

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