mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-20 08:30:12 +01:00
❤️ 完整开源界面和内核 https://github.com/siyuan-note/siyuan/issues/5013
This commit is contained in:
parent
e650b8100c
commit
f40ed985e1
1214 changed files with 345766 additions and 9 deletions
94
app/src/util/newFile.ts
Normal file
94
app/src/util/newFile.ts
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import {showMessage} from "../dialog/message";
|
||||
import {getAllModels} from "../layout/getAll";
|
||||
import {hasTopClosestByTag} from "../protyle/util/hasClosest";
|
||||
import {getDockByType} from "../layout/util";
|
||||
import {Files} from "../layout/dock/Files";
|
||||
import {fetchPost} from "./fetch";
|
||||
import {getDisplayName, getOpenNotebookCount, pathPosix} from "./pathName";
|
||||
import {openFileById} from "../editor/util";
|
||||
import {Constants} from "../constants";
|
||||
|
||||
export const newFile = (notebookId?: string, currentPath?: string, open?: boolean) => {
|
||||
if (getOpenNotebookCount() === 0) {
|
||||
showMessage(window.siyuan.languages.newFileTip);
|
||||
return;
|
||||
}
|
||||
if (!notebookId) {
|
||||
getAllModels().editor.find((item) => {
|
||||
const currentElement = item.parent.headElement;
|
||||
if (currentElement.classList.contains("item--focus")) {
|
||||
notebookId = item.editor.protyle.notebookId;
|
||||
currentPath = pathPosix().dirname(item.editor.protyle.path);
|
||||
if (currentElement.parentElement.parentElement.classList.contains("layout__wnd--active")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!notebookId) {
|
||||
const fileModel = getDockByType("file").data.file;
|
||||
if (fileModel instanceof Files) {
|
||||
const currentElement = fileModel.element.querySelector(".b3-list-item--focus");
|
||||
if (currentElement) {
|
||||
const topElement = hasTopClosestByTag(currentElement, "UL");
|
||||
if (topElement) {
|
||||
notebookId = topElement.getAttribute("data-url");
|
||||
}
|
||||
const selectPath = currentElement.getAttribute("data-path");
|
||||
currentPath = pathPosix().dirname(selectPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!notebookId) {
|
||||
window.siyuan.notebooks.find(item => {
|
||||
if (!item.closed) {
|
||||
notebookId = item.id;
|
||||
currentPath = "/";
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
fetchPost("/api/filetree/getDocNameTemplate", {notebook: notebookId}, (data) => {
|
||||
const id = Lute.NewNodeID();
|
||||
fetchPost("/api/filetree/createDoc", {
|
||||
notebook: notebookId,
|
||||
path: pathPosix().join(getDisplayName(currentPath, false, true), id + ".sy"),
|
||||
title: data.data.name || window.siyuan.languages.untitled,
|
||||
md: "",
|
||||
}, () => {
|
||||
if (open) {
|
||||
openFileById({id, hasContext: true, action: [Constants.CB_GET_HL]});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const getSavePath = (pathString: string, notebookId: string, cb: (p: string) => void) => {
|
||||
fetchPost("/api/notebook/getNotebookConf", {
|
||||
notebook: notebookId
|
||||
}, (data) => {
|
||||
let savePath = data.data.conf.refCreateSavePath;
|
||||
if (!savePath) {
|
||||
savePath = window.siyuan.config.fileTree.refCreateSavePath;
|
||||
}
|
||||
if (savePath) {
|
||||
if (savePath.startsWith("/")) {
|
||||
cb(getDisplayName(savePath, false, true));
|
||||
} else {
|
||||
fetchPost("/api/filetree/getHPathByPath", {
|
||||
notebook: notebookId,
|
||||
path: pathString
|
||||
}, (response) => {
|
||||
cb(getDisplayName(pathPosix().join(response.data, savePath), false, true));
|
||||
});
|
||||
}
|
||||
} else {
|
||||
fetchPost("/api/filetree/getHPathByPath", {
|
||||
notebook: notebookId,
|
||||
path: pathString
|
||||
}, (response) => {
|
||||
cb(getDisplayName(response.data, false, true));
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue