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

View file

@ -1182,7 +1182,10 @@ export const windowKeyDown = (app: App, event: KeyboardEvent) => {
return; return;
} }
if (matchHotKey(window.siyuan.config.keymap.general.newFile.custom, event)) { if (matchHotKey(window.siyuan.config.keymap.general.newFile.custom, event)) {
newFile(app, undefined, undefined, undefined, true); newFile({
app,
useSavePath: true
});
event.preventDefault(); event.preventDefault();
return; return;
} }

View file

@ -102,7 +102,10 @@ export class Wnd {
while (target && !target.isEqualNode(this.headersElement)) { while (target && !target.isEqualNode(this.headersElement)) {
if (target.classList.contains("block__icon") && target.getAttribute("data-type") === "new") { if (target.classList.contains("block__icon") && target.getAttribute("data-type") === "new") {
setPanelFocus(this.headersElement.parentElement.parentElement); setPanelFocus(this.headersElement.parentElement.parentElement);
newFile(app, undefined, undefined, undefined, true); newFile({
app,
useSavePath: true
});
break; break;
} else if (target.classList.contains("block__icon") && target.getAttribute("data-type") === "more") { } else if (target.classList.contains("block__icon") && target.getAttribute("data-type") === "more") {
this.renderTabList(target); this.renderTabList(target);

View file

@ -257,7 +257,12 @@ export class Files extends Model {
const pathString = target.parentElement.getAttribute("data-path"); const pathString = target.parentElement.getAttribute("data-path");
if (!window.siyuan.config.readonly) { if (!window.siyuan.config.readonly) {
if (type === "new") { if (type === "new") {
newFile(options.app, notebookId, pathString); newFile({
app: options.app,
notebookId,
currentPath:pathString,
useSavePath: false
});
} else if (type === "more-root") { } else if (type === "more-root") {
initNavigationMenu(options.app, target.parentElement).popup({ initNavigationMenu(options.app, target.parentElement).popup({
x: event.clientX, x: event.clientX,

View file

@ -1028,7 +1028,10 @@ export const newCenterEmptyTab = (app: App) => {
event.preventDefault(); event.preventDefault();
break; break;
} else if (target.id === "editorEmptyFile") { } else if (target.id === "editorEmptyFile") {
newFile(app, undefined, undefined, undefined, true); newFile({
app,
useSavePath: true
});
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
break; break;

View file

@ -364,7 +364,13 @@ export const initFileMenu = (app: App, notebookId: string, pathString: string, l
paths.push(item.getAttribute("data-path")); paths.push(item.getAttribute("data-path"));
} }
}); });
newFile(app, notebookId, pathPosix().dirname(pathString), paths); newFile({
app,
notebookId,
currentPath: pathPosix().dirname(pathString),
paths,
useSavePath: false
});
} }
}).element); }).element);
window.siyuan.menus.menu.append(new MenuItem({ 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); }).element);
window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element); window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);

View file

@ -193,7 +193,12 @@ export class MobileFiles extends Model {
const notebookId = ulElement.getAttribute("data-url"); const notebookId = ulElement.getAttribute("data-url");
if (!window.siyuan.config.readonly) { if (!window.siyuan.config.readonly) {
if (type === "new") { if (type === "new") {
newFile(app, notebookId, pathString); newFile({
app,
notebookId,
currentPath:pathString,
useSavePath: false
});
} else if (type === "more-root") { } else if (type === "more-root") {
initNavigationMenu(app, target.parentElement); initNavigationMenu(app, target.parentElement);
window.siyuan.menus.menu.fullscreen("bottom"); window.siyuan.menus.menu.fullscreen("bottom");

View file

@ -52,11 +52,21 @@ export const setEmpty = (app: App) => {
break; break;
} else if (target.id === "emptyNewFile") { } else if (target.id === "emptyNewFile") {
if (window.siyuan.mobile.editor) { 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 { } else {
window.siyuan.notebooks.find(item => { window.siyuan.notebooks.find(item => {
if (!item.closed) { if (!item.closed) {
newFile(app, item.id, "/", undefined, true); newFile({
app,
notebookId: item.id,
currentPath: "/",
useSavePath: true
});
return true; return true;
} }
}); });

View file

@ -63,44 +63,61 @@ export const getNewFilePath = (useSavePath: boolean) => {
return {notebookId, currentPath}; 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) { if (getOpenNotebookCount() === 0) {
showMessage(window.siyuan.languages.newFileTip); showMessage(window.siyuan.languages.newFileTip);
return; return;
} }
if (!notebookId) { if (!optios.notebookId) {
const resultData = getNewFilePath(useSavePath); const resultData = getNewFilePath(optios.useSavePath);
notebookId = resultData.notebookId; optios.notebookId = resultData.notebookId;
currentPath = resultData.currentPath; optios.currentPath = resultData.currentPath;
} }
fetchPost("/api/filetree/getDocCreateSavePath", {notebook: notebookId}, (data) => { fetchPost("/api/filetree/getDocCreateSavePath", {notebook: optios.notebookId}, (data) => {
if (data.data.path.indexOf("/") > -1 && useSavePath) { if ((data.data.path.indexOf("/") > -1 && optios.useSavePath) || optios.name) {
if (data.data.path.startsWith("/") || currentPath === "/") { if (data.data.path.startsWith("/") || optios.currentPath === "/") {
fetchPost("/api/filetree/createDocWithMd", { fetchPost("/api/filetree/createDocWithMd", {
notebook: notebookId, notebook: optios.notebookId,
path: data.data.path, path: pathPosix().join(data.data.path, optios.name || ""),
// 根目录时无法确定 parentID
markdown: "" markdown: ""
}, response => { }, response => {
/// #if !MOBILE /// #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 /// #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 /// #endif
}); });
} else { } else {
fetchPost("/api/filetree/getHPathByPath", { fetchPost("/api/filetree/getHPathByPath", {
notebook: notebookId, notebook: optios.notebookId,
path: currentPath.endsWith(".sy") ? currentPath : currentPath + ".sy" path: optios.currentPath.endsWith(".sy") ? optios.currentPath : optios.currentPath + ".sy"
}, (responseHPath) => { }, (responseHPath) => {
fetchPost("/api/filetree/createDocWithMd", { fetchPost("/api/filetree/createDocWithMd", {
notebook: notebookId, notebook: optios.notebookId,
path: pathPosix().join(responseHPath.data, data.data.path), path: pathPosix().join(responseHPath.data, data.data.path, optios.name || ""),
parentID: getDisplayName(optios.currentPath, true, true),
markdown: "" markdown: ""
}, response => { }, response => {
/// #if !MOBILE /// #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 /// #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 /// #endif
}); });
}); });
@ -112,21 +129,21 @@ export const newFile = (app: App, notebookId?: string, currentPath?: string, pat
return; return;
} }
const id = Lute.NewNodeID(); const id = Lute.NewNodeID();
const newPath = pathPosix().join(getDisplayName(currentPath, false, true), id + ".sy"); const newPath = pathPosix().join(getDisplayName(optios.currentPath, false, true), id + ".sy");
if (paths) { if (optios.paths) {
paths[paths.indexOf(undefined)] = newPath; optios.paths[optios.paths.indexOf(undefined)] = newPath;
} }
fetchPost("/api/filetree/createDoc", { fetchPost("/api/filetree/createDoc", {
notebook: notebookId, notebook: optios.notebookId,
path: newPath, path: newPath,
title, title,
md: "", md: "",
sorts: paths sorts: optios.paths
}, () => { }, () => {
/// #if !MOBILE /// #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 /// #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 /// #endif
}); });
} }
@ -160,22 +177,10 @@ export const getSavePath = (pathString: string, notebookId: string, cb: (p: stri
}; };
export const newFileByName = (app: App, value: string) => { export const newFileByName = (app: App, value: string) => {
const newData = getNewFilePath(true); hideElements(["dialog"]);
fetchPost("/api/filetree/getHPathByPath", { newFile({
notebook: newData.notebookId, app,
path: newData.currentPath, useSavePath: true,
}, (responsePath) => { name: replaceFileName(value.trim()) || "Untitled"
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
});
}); });
}; };