Vanessa 2023-02-03 22:29:34 +08:00
parent 8660a2b214
commit 86311447d1
8 changed files with 59 additions and 30 deletions

View file

@ -11,7 +11,7 @@ import {getDisplayName, getOpenNotebookCount, pathPosix} from "./pathName";
import {Constants} from "../constants";
import {validateName} from "../editor/rename";
export const newFile = (notebookId?: string, currentPath?: string, open?: boolean, paths?: string[]) => {
export const newFile = (notebookId?: string, currentPath?: string, paths?: string[], useSavePath = false) => {
if (getOpenNotebookCount() === 0) {
showMessage(window.siyuan.languages.newFileTip);
return;
@ -54,27 +54,56 @@ export const newFile = (notebookId?: string, currentPath?: string, open?: boolea
});
}
fetchPost("/api/filetree/getDocCreateSavePath", {notebook: notebookId}, (data) => {
const id = Lute.NewNodeID();
const newPath = pathPosix().join(getDisplayName(currentPath, false, true), id + ".sy");
if (paths) {
paths[paths.indexOf(undefined)] = newPath;
}
if (!validateName(data.data.path)) {
return;
}
fetchPost("/api/filetree/createDoc", {
notebook: notebookId,
path: newPath,
title: data.data.name || "Untitled",
md: "",
sorts: paths
}, () => {
/// #if !MOBILE
if (open) {
openFileById({id, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]});
if (data.data.path.indexOf("/") > -1 && useSavePath) {
if (data.data.path.startsWith("/")) {
fetchPost("/api/filetree/createDocWithMd", {
notebook: notebookId,
path: data.data.path,
markdown: ""
}, response => {
/// #if !MOBILE
openFileById({id: response.data, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]});
/// #endif
});
} else {
fetchPost("/api/filetree/getHPathByPath", {
notebook: notebookId,
path: currentPath
}, (responseHPath) => {
fetchPost("/api/filetree/createDocWithMd", {
notebook: notebookId,
path: pathPosix().join(responseHPath.data, data.data.path),
markdown: ""
}, response => {
/// #if !MOBILE
openFileById({id: response.data, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]});
/// #endif
});
});
}
/// #endif
});
} else {
let title = data.data.path || "Untitled"
title = title.substring(title.lastIndexOf("/") + 1);
if (!validateName(title)) {
return;
}
const id = Lute.NewNodeID();
const newPath = pathPosix().join(getDisplayName(currentPath, false, true), id + ".sy");
if (paths.length > 0) {
paths[paths.indexOf(undefined)] = newPath;
}
fetchPost("/api/filetree/createDoc", {
notebook: notebookId,
path: newPath,
title,
md: "",
sorts: paths
}, () => {
/// #if !MOBILE
openFileById({id, action: [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT]});
/// #endif
});
}
});
};