This commit is contained in:
Vanessa 2023-09-10 16:42:24 +08:00
parent 64b6001947
commit 7e8e6167ff
4 changed files with 6 additions and 48 deletions

View file

@ -180,12 +180,10 @@ export const openFile = (options: IOpenFileOptions) => {
} }
const ids = decodeURIComponent(new URL(item.webContents.getURL()).hash.substring(1)).split(Constants.ZWSP); const ids = decodeURIComponent(new URL(item.webContents.getURL()).hash.substring(1)).split(Constants.ZWSP);
if (ids.includes(options.rootID) || ids.includes(options.assetPath)) { if (ids.includes(options.rootID) || ids.includes(options.assetPath)) {
let execJS = `window.newWindow.switchTabById("${options.rootID || options.assetPath}");`;
if (options.assetPath) {
execJS += `window.newWindow.positionPDF("${options.assetPath}", ${typeof options.page === "number" ? options.page : `"${options.page}"`})`;
}
item.focus(); item.focus();
item.webContents.executeJavaScript(execJS); const optionsClone = Object.assign({}, options);
delete optionsClone.app
item.webContents.executeJavaScript(`window.newWindow.openFile(${JSON.stringify(optionsClone)});`);
if (options.afterOpen) { if (options.afterOpen) {
options.afterOpen(); options.afterOpen();
} }

View file

@ -95,8 +95,7 @@ interface Window {
} }
newWindow: { newWindow: {
positionPDF(pathStr: string, page: string | number): void openFile(options: IOpenFileOptions): void
switchTabById(id: string): void
} }
Protyle: import("../protyle/method").default Protyle: import("../protyle/method").default

View file

@ -1,37 +0,0 @@
import {getAllTabs} from "../../layout/getAll";
import {Asset} from "../../asset";
import {Editor} from "../../editor";
export const positionPDF = (pathStr: string, page: string | number) => {
getAllTabs().forEach((tab) => {
if (tab.model instanceof Asset && tab.model.pdfObject && tab.model.path === pathStr) {
tab.parent.switchTab(tab.headElement);
tab.model.goToPage(page);
}
});
};
export const switchTabById = (id: string) => {
getAllTabs().find((tab) => {
if (!tab.model) {
const initTab = tab.headElement.getAttribute("data-initdata");
if (initTab) {
const initTabData = JSON.parse(initTab);
if (initTabData.instance === "Editor" && initTabData.rootId === id) {
tab.parent.switchTab(tab.headElement);
return true;
}
}
} else if (tab.model instanceof Editor) {
if (tab.model.editor.protyle.block.rootID === id) {
tab.parent.switchTab(tab.headElement);
return true;
}
} else if (tab.model instanceof Asset) {
if (tab.model.path === id) {
tab.parent.switchTab(tab.headElement);
return true;
}
}
});
};

View file

@ -7,7 +7,7 @@ import {addScript, addScriptSync} from "../protyle/util/addScript";
import {genUUID} from "../util/genID"; import {genUUID} from "../util/genID";
import {fetchGet, fetchPost} from "../util/fetch"; import {fetchGet, fetchPost} from "../util/fetch";
import {addBaseURL, setNoteBook} from "../util/pathName"; import {addBaseURL, setNoteBook} from "../util/pathName";
import {openFileById} from "../editor/util"; import {openFile, openFileById} from "../editor/util";
import { import {
processSync, progressBackgroundTask, processSync, progressBackgroundTask,
progressLoading, progressLoading,
@ -19,7 +19,6 @@ import {initMessage} from "../dialog/message";
import {getAllTabs} from "../layout/getAll"; import {getAllTabs} from "../layout/getAll";
import {getLocalStorage} from "../protyle/util/compatibility"; import {getLocalStorage} from "../protyle/util/compatibility";
import {init} from "../window/init"; import {init} from "../window/init";
import {positionPDF, switchTabById} from "./global/function";
import {loadPlugins} from "../plugin/loader"; import {loadPlugins} from "../plugin/loader";
class App { class App {
@ -152,6 +151,5 @@ new App();
// 再次点击新窗口已打开的 PDF 时,需进行定位 // 再次点击新窗口已打开的 PDF 时,需进行定位
window.newWindow = { window.newWindow = {
positionPDF: positionPDF, openFile: openFile,
switchTabById: switchTabById
}; };