diff --git a/app/src/boot/globalEvent/keydown.ts b/app/src/boot/globalEvent/keydown.ts index 069c55bbb31..556b20de310 100644 --- a/app/src/boot/globalEvent/keydown.ts +++ b/app/src/boot/globalEvent/keydown.ts @@ -1182,7 +1182,10 @@ export const windowKeyDown = (app: App, event: KeyboardEvent) => { return; } if (matchHotKey(window.siyuan.config.keymap.general.newFile.custom, event)) { - newFile(app, undefined, undefined, undefined, true); + newFile({ + app, + useSavePath: true + }); event.preventDefault(); return; } diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts index 26cbebe8af9..68527112c02 100644 --- a/app/src/layout/Wnd.ts +++ b/app/src/layout/Wnd.ts @@ -102,7 +102,10 @@ export class Wnd { while (target && !target.isEqualNode(this.headersElement)) { if (target.classList.contains("block__icon") && target.getAttribute("data-type") === "new") { setPanelFocus(this.headersElement.parentElement.parentElement); - newFile(app, undefined, undefined, undefined, true); + newFile({ + app, + useSavePath: true + }); break; } else if (target.classList.contains("block__icon") && target.getAttribute("data-type") === "more") { this.renderTabList(target); diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts index e8e606a914c..4b5e50a754a 100644 --- a/app/src/layout/dock/Files.ts +++ b/app/src/layout/dock/Files.ts @@ -257,7 +257,12 @@ export class Files extends Model { const pathString = target.parentElement.getAttribute("data-path"); if (!window.siyuan.config.readonly) { if (type === "new") { - newFile(options.app, notebookId, pathString); + newFile({ + app: options.app, + notebookId, + currentPath:pathString, + useSavePath: false + }); } else if (type === "more-root") { initNavigationMenu(options.app, target.parentElement).popup({ x: event.clientX, diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts index 646e7c4452c..7f0e0f283be 100644 --- a/app/src/layout/util.ts +++ b/app/src/layout/util.ts @@ -1028,7 +1028,10 @@ export const newCenterEmptyTab = (app: App) => { event.preventDefault(); break; } else if (target.id === "editorEmptyFile") { - newFile(app, undefined, undefined, undefined, true); + newFile({ + app, + useSavePath: true + }); event.stopPropagation(); event.preventDefault(); break; diff --git a/app/src/menus/navigation.ts b/app/src/menus/navigation.ts index 903e5791dd7..79eb9da928f 100644 --- a/app/src/menus/navigation.ts +++ b/app/src/menus/navigation.ts @@ -364,7 +364,13 @@ export const initFileMenu = (app: App, notebookId: string, pathString: string, l paths.push(item.getAttribute("data-path")); } }); - newFile(app, notebookId, pathPosix().dirname(pathString), paths); + newFile({ + app, + notebookId, + currentPath: pathPosix().dirname(pathString), + paths, + useSavePath: false + }); } }).element); window.siyuan.menus.menu.append(new MenuItem({ @@ -380,7 +386,13 @@ export const initFileMenu = (app: App, notebookId: string, pathString: string, l } } }); - newFile(app, notebookId, pathPosix().dirname(pathString), paths); + newFile({ + app, + notebookId, + currentPath: pathPosix().dirname(pathString), + paths, + useSavePath: false + }); } }).element); window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element); diff --git a/app/src/mobile/dock/MobileFiles.ts b/app/src/mobile/dock/MobileFiles.ts index f0e7c61af69..444a5dfda30 100644 --- a/app/src/mobile/dock/MobileFiles.ts +++ b/app/src/mobile/dock/MobileFiles.ts @@ -193,7 +193,12 @@ export class MobileFiles extends Model { const notebookId = ulElement.getAttribute("data-url"); if (!window.siyuan.config.readonly) { if (type === "new") { - newFile(app, notebookId, pathString); + newFile({ + app, + notebookId, + currentPath:pathString, + useSavePath: false + }); } else if (type === "more-root") { initNavigationMenu(app, target.parentElement); window.siyuan.menus.menu.fullscreen("bottom"); diff --git a/app/src/mobile/util/setEmpty.ts b/app/src/mobile/util/setEmpty.ts index f4f1c63b30c..de535073ca2 100644 --- a/app/src/mobile/util/setEmpty.ts +++ b/app/src/mobile/util/setEmpty.ts @@ -52,11 +52,21 @@ export const setEmpty = (app: App) => { break; } else if (target.id === "emptyNewFile") { if (window.siyuan.mobile.editor) { - newFile(app, window.siyuan.mobile.editor.protyle.notebookId, window.siyuan.mobile.editor.protyle.path, undefined, true); + newFile({ + app, + notebookId: window.siyuan.mobile.editor.protyle.notebookId, + currentPath: window.siyuan.mobile.editor.protyle.path, + useSavePath: true + }); } else { window.siyuan.notebooks.find(item => { if (!item.closed) { - newFile(app, item.id, "/", undefined, true); + newFile({ + app, + notebookId: item.id, + currentPath: "/", + useSavePath: true + }); return true; } }); diff --git a/app/src/util/newFile.ts b/app/src/util/newFile.ts index 99fd1196934..32c0018d854 100644 --- a/app/src/util/newFile.ts +++ b/app/src/util/newFile.ts @@ -63,44 +63,61 @@ export const getNewFilePath = (useSavePath: boolean) => { return {notebookId, currentPath}; }; -export const newFile = (app: App, notebookId?: string, currentPath?: string, paths?: string[], useSavePath = false) => { +export const newFile = (optios: { + app: App, + notebookId?: string, + currentPath?: string, + paths?: string[], + useSavePath: boolean, + name?: string +}) => { if (getOpenNotebookCount() === 0) { showMessage(window.siyuan.languages.newFileTip); return; } - if (!notebookId) { - const resultData = getNewFilePath(useSavePath); - notebookId = resultData.notebookId; - currentPath = resultData.currentPath; + if (!optios.notebookId) { + const resultData = getNewFilePath(optios.useSavePath); + optios.notebookId = resultData.notebookId; + optios.currentPath = resultData.currentPath; } - fetchPost("/api/filetree/getDocCreateSavePath", {notebook: notebookId}, (data) => { - if (data.data.path.indexOf("/") > -1 && useSavePath) { - if (data.data.path.startsWith("/") || currentPath === "/") { + fetchPost("/api/filetree/getDocCreateSavePath", {notebook: optios.notebookId}, (data) => { + if ((data.data.path.indexOf("/") > -1 && optios.useSavePath) || optios.name) { + if (data.data.path.startsWith("/") || optios.currentPath === "/") { fetchPost("/api/filetree/createDocWithMd", { - notebook: notebookId, - path: data.data.path, + notebook: optios.notebookId, + path: pathPosix().join(data.data.path, optios.name || ""), + // 根目录时无法确定 parentID markdown: "" }, response => { /// #if !MOBILE - openFileById({app, id: response.data, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]}); + openFileById({ + app: optios.app, + id: response.data, + action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT] + }); /// #else - openMobileFileById(app, response.data, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]); + openMobileFileById(optios.app, response.data, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]); /// #endif }); } else { fetchPost("/api/filetree/getHPathByPath", { - notebook: notebookId, - path: currentPath.endsWith(".sy") ? currentPath : currentPath + ".sy" + notebook: optios.notebookId, + path: optios.currentPath.endsWith(".sy") ? optios.currentPath : optios.currentPath + ".sy" }, (responseHPath) => { fetchPost("/api/filetree/createDocWithMd", { - notebook: notebookId, - path: pathPosix().join(responseHPath.data, data.data.path), + notebook: optios.notebookId, + path: pathPosix().join(responseHPath.data, data.data.path, optios.name || ""), + parentID: getDisplayName(optios.currentPath, true, true), markdown: "" }, response => { /// #if !MOBILE - openFileById({app, id: response.data, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]}); + openFileById({ + app: optios.app, + id: response.data, + action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT] + }); /// #else - openMobileFileById(app, response.data, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]); + openMobileFileById(optios.app, response.data, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]); /// #endif }); }); @@ -112,21 +129,21 @@ export const newFile = (app: App, notebookId?: string, currentPath?: string, pat return; } const id = Lute.NewNodeID(); - const newPath = pathPosix().join(getDisplayName(currentPath, false, true), id + ".sy"); - if (paths) { - paths[paths.indexOf(undefined)] = newPath; + const newPath = pathPosix().join(getDisplayName(optios.currentPath, false, true), id + ".sy"); + if (optios.paths) { + optios.paths[optios.paths.indexOf(undefined)] = newPath; } fetchPost("/api/filetree/createDoc", { - notebook: notebookId, + notebook: optios.notebookId, path: newPath, title, md: "", - sorts: paths + sorts: optios.paths }, () => { /// #if !MOBILE - openFileById({app, id, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]}); + openFileById({app: optios.app, id, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]}); /// #else - openMobileFileById(app, id, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]); + openMobileFileById(optios.app, id, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]); /// #endif }); } @@ -160,22 +177,10 @@ export const getSavePath = (pathString: string, notebookId: string, cb: (p: stri }; export const newFileByName = (app: App, value: string) => { - const newData = getNewFilePath(true); - fetchPost("/api/filetree/getHPathByPath", { - notebook: newData.notebookId, - path: newData.currentPath, - }, (responsePath) => { - fetchPost("/api/filetree/createDocWithMd", { - notebook: newData.notebookId, - path: pathPosix().join(responsePath.data, replaceFileName(value.trim()) || "Untitled"), - markdown: "" - }, response => { - hideElements(["dialog"]); - /// #if MOBILE - openMobileFileById(app, response.data, [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]); - /// #else - openFileById({app, id: response.data, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]}); - /// #endif - }); + hideElements(["dialog"]); + newFile({ + app, + useSavePath: true, + name: replaceFileName(value.trim()) || "Untitled" }); };