Vanessa 2023-09-30 13:57:40 +08:00
parent 1063f50375
commit 2dae1200b4
8 changed files with 97 additions and 51 deletions

View file

@ -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"
});
};