Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve explicit DevTools options https://github.com/siyuan-note/siyuan/pull/21055

Daniel663443a0f563
diff --git a/app/electron/devToolsDock.js b/app/electron/devToolsDock.js
index 157e136c60..2591b19ef5 100644
--- a/app/electron/devToolsDock.js
+++ b/app/electron/devToolsDock.js
@@ -1,11 +1,23 @@
-// Windows 的窗口控制按钮由系统绘制在页面之上,开发者工具停靠在右侧会被遮挡,这里固定停靠到底部。
-// 其他平台不带参数打开,沿用 Electron 上次使用的停靠位置。
-const openDevTools = (contents, platform = process.platform) => {
+// Windows 默认停靠到底部,关闭重开即可从可能被窗口控制按钮遮挡的位置恢复。
+// 保留调用方显式指定的选项;其他平台未指定位置时沿用 Electron 上次使用的位置。
+const openDevTools = (contents, platform = process.platform, options = {}) => {
if (!contents || contents.isDestroyed()) {
return;
}
- if (platform === "win32") {
- contents.openDevTools({mode: "bottom"});
+ const normalized = platform === "win32" ? {mode: "bottom"} : {};
+ if (options && typeof options === "object") {
+ if (["left", "right", "bottom", "undocked", "detach"].includes(options.mode)) {
+ normalized.mode = options.mode;
+ }
+ if (typeof options.activate === "boolean") {
+ normalized.activate = options.activate;
+ }
+ if (typeof options.title === "string") {
+ normalized.title = options.title;
+ }
+ }
+ if (Object.keys(normalized).length) {
+ contents.openDevTools(normalized);
} else {
contents.openDevTools();
}
diff --git a/app/electron/devToolsDock.test.js b/app/electron/devToolsDock.test.js
index 6d71c706fb..cebf03a663 100644
--- a/app/electron/devToolsDock.test.js
+++ b/app/electron/devToolsDock.test.js
@@ -1,5 +1,8 @@
const test = require("node:test");
const assert = require("node:assert/strict");
+const fs = require("node:fs");
+const path = require("node:path");
+const vm = require("node:vm");
const {openDevTools, toggleDevTools} = require("./devToolsDock");
const createContents = ({destroyed = false, opened = false} = {}) => ({
@@ -26,6 +29,51 @@ test("other platforms keep the last dock position", () => {
}
});
+test("explicit dock modes and activation/title options are preserved on every platform", () => {
+ for (const platform of ["win32", "linux", "darwin"]) {
+ for (const mode of ["left", "right", "bottom", "undocked", "detach"]) {
+ const contents = createContents();
+ const options = Object.freeze({mode, activate: false, title: "Custom DevTools", ignored: true});
+ openDevTools(contents, platform, options);
+ assert.deepEqual(contents.calls, [["open", {mode, activate: false, title: "Custom DevTools"}]]);
+ }
+ }
+});
+
+test("activation and title options retain the platform default dock mode", () => {
+ for (const platform of ["win32", "linux", "darwin"]) {
+ const contents = createContents();
+ openDevTools(contents, platform, {activate: true, title: ""});
+ assert.deepEqual(contents.calls, [["open", {
+ ...(platform === "win32" ? {mode: "bottom"} : {}), activate: true, title: "",
+ }]]);
+ }
+});
+
+test("invalid options are ignored without losing platform defaults", () => {
+ for (const platform of ["win32", "linux", "darwin"]) {
+ for (const options of [null, false, "right", {}, {mode: "invalid", activate: "false", title: 123, ignored: true}]) {
+ const contents = createContents();
+ openDevTools(contents, platform, options);
+ assert.deepEqual(contents.calls, [["open", platform === "win32" ? {mode: "bottom"} : undefined]]);
+ }
+ }
+});
+
+test("openDevTools IPC forwards options through the docking helper", () => {
+ const source = fs.readFileSync(path.join(__dirname, "main.js"), "utf8");
+ const start = source.indexOf('case "openDevTools":');
+ const handler = source.slice(start, source.indexOf('case "toggleDevTools":', start));
+ for (const platform of ["win32", "linux", "darwin"]) {
+ const contents = createContents();
+ vm.runInNewContext(`switch ("openDevTools") { ${handler} }`, {
+ devToolsDock: {openDevTools}, event: {sender: contents}, process: {platform},
+ data: {mode: "detach", activate: false, title: "IPC DevTools"},
+ });
+ assert.deepEqual(contents.calls, [["open", {mode: "detach", activate: false, title: "IPC DevTools"}]]);
+ }
+});
+
test("toggling closes an open developer tools panel", () => {
const contents = createContents({opened: true});
toggleDevTools(contents, "win32");
diff --git a/app/electron/main.js b/app/electron/main.js
index ec14fd1d85..3826740408 100644
--- a/app/electron/main.js
+++ b/app/electron/main.js
@@ -3953,7 +3953,7 @@ app.whenReady().then(() => {
shell.openPath(data.filePath);
break;
case "openDevTools":
- devToolsDock.openDevTools(event.sender);
+ devToolsDock.openDevTools(event.sender, process.platform, data);
break;
case "toggleDevTools":
devToolsDock.toggleDevTools(event.sender);