mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-18 07:30:12 +01:00
This commit is contained in:
parent
1063f50375
commit
2dae1200b4
8 changed files with 97 additions and 51 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue