Skip to content

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

:bug: Guard layout adjustment before initialization

Daniel2a2b1d72ad37
diff --git a/app/src/layout/adjustLayout.test.ts b/app/src/layout/adjustLayout.test.ts
new file mode 100644
index 0000000000..59d3b47d22
--- /dev/null
+++ b/app/src/layout/adjustLayout.test.ts
@@ -0,0 +1,35 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {resolve} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, transpileModule} from "typescript";
+
+test("layout adjustment waits for initialization and still accepts an explicit layout", () => {
+ const source = readFileSync(resolve(process.cwd(), "src/layout/util.ts"), "utf8");
+ const code = transpileModule(source, {compilerOptions: {module: ModuleKind.CommonJS}}).outputText;
+ const layout: {centerLayout?: {parent?: unknown}} = {};
+ let adjustments = 0;
+ const moduleExports = {} as {adjustLayout: (layout?: unknown) => void};
+ runInNewContext(code, {
+ exports: moduleExports,
+ window: {siyuan: {layout}},
+ require: (name: string) => name === "./dock/responsive" ? {
+ requestResponsiveDockLayout: () => { adjustments++; },
+ } : {},
+ });
+
+ assert.doesNotThrow(() => moduleExports.adjustLayout());
+ layout.centerLayout = {};
+ assert.doesNotThrow(() => moduleExports.adjustLayout());
+ assert.equal(adjustments, 0);
+
+ const readyLayout = {direction: "tb", children: [] as unknown[], element: {closest: (): Element | null => null}};
+ layout.centerLayout.parent = readyLayout;
+ moduleExports.adjustLayout();
+ assert.equal(adjustments, 1);
+
+ delete layout.centerLayout;
+ moduleExports.adjustLayout(readyLayout);
+ assert.equal(adjustments, 2);
+});
diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts
index 8ece05aeca..8167b7264d 100644
--- a/app/src/layout/util.ts
+++ b/app/src/layout/util.ts
@@ -1266,7 +1266,11 @@ export const addResize = (obj: Layout | Wnd, after = true) => {
});
};
-export const adjustLayout = (layout: Layout = window.siyuan.layout.centerLayout.parent) => {
+export const adjustLayout = (layout: Layout = window.siyuan.layout.centerLayout?.parent) => {
+ // 启动期间的窗口尺寸变化可能早于中央布局初始化。
+ if (!layout) {
+ return;
+ }
const sizeProperty = layout.direction === "lr" ? "width" : "height";
if (layout.element.closest(".layout__center") &&
layout.children.some((item) => item.element.style[sizeProperty].endsWith("px"))) {