Skip to content

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

:bug: Restore document creation in empty new windows https://github.com/siyuan-note/siyuan/issues/19895

Daniel86474ce70245
diff --git a/app/src/util/newFile.test.ts b/app/src/util/newFile.test.ts
new file mode 100644
index 0000000000..c68262acb0
--- /dev/null
+++ b/app/src/util/newFile.test.ts
@@ -0,0 +1,36 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";
+import type {App} from "../index";
+
+test("an empty detached window creates documents using the first open notebook", () => {
+ const methods = {} as typeof import("./newFile");
+ const requests: Array<{url: string, data: unknown}> = [];
+ const source = readFileSync("src/util/newFile.ts", "utf8")
+ .replace(/\/\/\/ #else[\s\S]*?\/\/\/ #endif/g, "");
+ runInNewContext(transpileModule(source, {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2022},
+ }).outputText, {
+ exports: methods,
+ window: {siyuan: {notebooks: [{id: "closed", closed: true}, {id: "open", closed: false}]}},
+ require: (name: string) => ({
+ "../layout/tabUtil": {getActiveTab() {}, getDockByType() {}},
+ "../layout/dock/Files": {Files: class {}},
+ "../editor": {Editor: class {}},
+ "./pathName": {getOpenNotebookCount: () => 1},
+ "./fetch": {fetchPost: (url: string, data: unknown, callback: (response: unknown) => void) => {
+ requests.push({url, data});
+ if (url === "/api/filetree/getDocCreateSavePath") {
+ callback({data: {box: "open", path: "", docCreateTemplatePath: ""}});
+ }
+ }},
+ })[name] || {},
+ });
+ methods.newFile({} as App);
+ assert.equal(requests[0].url, "/api/filetree/getDocCreateSavePath");
+ assert.equal(JSON.stringify(requests[0].data), JSON.stringify({notebook: "open"}));
+ assert.equal(requests[1].url, "/api/filetree/getHPathByPath");
+ assert.equal(JSON.stringify(requests[1].data), JSON.stringify({notebook: "open", path: "/"}));
+});
diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.ts
index 555e0e9a7c..611568225f 100644
--- a/app/src/util/newFile.ts
+++ b/app/src/util/newFile.ts
@@ -248,7 +248,7 @@ function getNewFilePath(): Pick<NewDocRequest, "notebookId" | "currentPath" | "h
hasFocusTarget = true;
}
if (!notebookId) {
- const fileModel = getDockByType("file").data.file;
+ const fileModel = getDockByType("file")?.data.file;
if (fileModel instanceof Files) {
const currentElement = fileModel.element.querySelector(".b3-list-item--focus");
if (currentElement) {