Skip to content

Commit

Permalink
🎨 #9316
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Sep 30, 2023
1 parent 1063f50 commit 2dae120
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 51 deletions.
5 changes: 4 additions & 1 deletion app/src/boot/globalEvent/keydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/layout/Wnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion app/src/layout/dock/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion app/src/layout/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 14 additions & 2 deletions app/src/menus/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion app/src/mobile/dock/MobileFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
14 changes: 12 additions & 2 deletions app/src/mobile/util/setEmpty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
Expand Down
89 changes: 47 additions & 42 deletions app/src/util/newFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
Expand All @@ -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
});
}
Expand Down Expand Up @@ -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"
});
};

0 comments on commit 2dae120

Please sign in to comment.