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

View file

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

View file

@ -231,7 +231,7 @@ 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(notebookId, pathString, true); newFile(notebookId, pathString);
} else if (type === "more-root") { } else if (type === "more-root") {
initNavigationMenu(target.parentElement).popup({x: event.clientX, y: event.clientY}); initNavigationMenu(target.parentElement).popup({x: event.clientX, y: event.clientY});
} }

View file

@ -735,7 +735,7 @@ export const newCenterEmptyTab = () => {
newNotebook(); newNotebook();
}); });
tab.panelElement.querySelector("#editorEmptyFile").addEventListener("click", () => { tab.panelElement.querySelector("#editorEmptyFile").addEventListener("click", () => {
newFile(undefined, undefined, true); newFile(undefined, undefined, undefined, true);
}); });
} }
} }

View file

@ -206,7 +206,7 @@ export const initFileMenu = (notebookId: string, pathString: string, liElement:
paths.push(item.getAttribute("data-path")); paths.push(item.getAttribute("data-path"));
} }
}); });
newFile(notebookId, pathPosix().dirname(pathString), true, paths); newFile(notebookId, pathPosix().dirname(pathString), paths);
} }
}).element); }).element);
window.siyuan.menus.menu.append(new MenuItem({ window.siyuan.menus.menu.append(new MenuItem({
@ -222,7 +222,7 @@ export const initFileMenu = (notebookId: string, pathString: string, liElement:
} }
} }
}); });
newFile(notebookId, pathPosix().dirname(pathString), true, paths); newFile(notebookId, pathPosix().dirname(pathString), paths);
} }
}).element); }).element);
window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element); window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);

View file

@ -174,7 +174,7 @@ 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(notebookId, pathString, true); newFile(notebookId, pathString);
} else if (type === "more-root") { } else if (type === "more-root") {
initNavigationMenu(target.parentElement).popup({x, y}); initNavigationMenu(target.parentElement).popup({x, y});
window.siyuan.menus.menu.element.style.zIndex = "310"; window.siyuan.menus.menu.element.style.zIndex = "310";

View file

@ -21,11 +21,11 @@ export const setEmpty = () => {
</div>`; </div>`;
document.getElementById("emptyNewFile").addEventListener(getEventName(), () => { document.getElementById("emptyNewFile").addEventListener(getEventName(), () => {
if (window.siyuan.mobile.editor) { if (window.siyuan.mobile.editor) {
newFile(window.siyuan.mobile.editor.protyle.notebookId, window.siyuan.mobile.editor.protyle.path, true); newFile(window.siyuan.mobile.editor.protyle.notebookId, window.siyuan.mobile.editor.protyle.path, undefined, true);
} else { } else {
window.siyuan.notebooks.find(item => { window.siyuan.notebooks.find(item => {
if (item.closed) { if (item.closed) {
newFile(item.id, "/", true); newFile(item.id, "/", undefined, true);
} }
}); });
} }

View file

@ -597,7 +597,7 @@ export const globalShortcut = () => {
return; return;
} }
if (matchHotKey(window.siyuan.config.keymap.general.newFile.custom, event)) { if (matchHotKey(window.siyuan.config.keymap.general.newFile.custom, event)) {
newFile(undefined, undefined, true); newFile(undefined, undefined, undefined, true);
event.preventDefault(); event.preventDefault();
return; return;
} }

View file

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