2022-06-29 15:36:44 +08:00
|
|
|
import {fetchPost} from "../util/fetch";
|
2022-11-03 11:12:23 +08:00
|
|
|
import {getDisplayName, getNotebookName} from "../util/pathName";
|
2022-06-29 15:36:44 +08:00
|
|
|
import {confirmDialog} from "../dialog/confirmDialog";
|
2022-11-03 11:12:23 +08:00
|
|
|
import {hasTopClosestByTag} from "../protyle/util/hasClosest";
|
|
|
|
|
import {Constants} from "../constants";
|
|
|
|
|
import {showMessage} from "../dialog/message";
|
2023-04-13 15:45:26 +08:00
|
|
|
import {escapeHtml} from "../util/escape";
|
2022-06-29 15:36:44 +08:00
|
|
|
|
2023-04-13 15:45:26 +08:00
|
|
|
export const deleteFile = (notebookId: string, pathString: string) => {
|
2022-06-29 15:36:44 +08:00
|
|
|
if (window.siyuan.config.fileTree.removeDocWithoutConfirm) {
|
|
|
|
|
fetchPost("/api/filetree/removeDoc", {
|
|
|
|
|
notebook: notebookId,
|
|
|
|
|
path: pathString
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fetchPost("/api/block/getDocInfo", {
|
|
|
|
|
id: getDisplayName(pathString, true, true)
|
|
|
|
|
}, (response) => {
|
2023-04-13 15:45:26 +08:00
|
|
|
const fileName = escapeHtml(response.data.name);
|
|
|
|
|
let tip = `${window.siyuan.languages.confirmDelete} <b>${fileName}</b>?`;
|
2022-06-29 15:36:44 +08:00
|
|
|
if (response.data.subFileCount > 0) {
|
2023-04-13 15:45:26 +08:00
|
|
|
tip = `${window.siyuan.languages.confirmDelete} <b>${fileName}</b> ${window.siyuan.languages.andSubFile.replace("x", response.data.subFileCount)}?`;
|
2022-06-29 15:36:44 +08:00
|
|
|
}
|
2022-10-06 16:01:02 +08:00
|
|
|
confirmDialog(window.siyuan.languages.deleteOpConfirm, tip, () => {
|
2022-06-29 15:36:44 +08:00
|
|
|
fetchPost("/api/filetree/removeDoc", {
|
|
|
|
|
notebook: notebookId,
|
|
|
|
|
path: pathString
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
2022-11-03 11:12:23 +08:00
|
|
|
|
|
|
|
|
export const deleteFiles = (liElements: Element[]) => {
|
|
|
|
|
if (liElements.length === 1) {
|
|
|
|
|
const itemTopULElement = hasTopClosestByTag(liElements[0], "UL");
|
|
|
|
|
if (itemTopULElement) {
|
2022-11-04 21:50:03 +08:00
|
|
|
const itemNotebookId = itemTopULElement.getAttribute("data-url");
|
2022-11-03 11:12:23 +08:00
|
|
|
if (liElements[0].getAttribute("data-type") === "navigation-file") {
|
2023-04-13 15:45:26 +08:00
|
|
|
deleteFile(itemNotebookId, liElements[0].getAttribute("data-path"));
|
2022-11-03 11:12:23 +08:00
|
|
|
} else {
|
|
|
|
|
confirmDialog(window.siyuan.languages.deleteOpConfirm,
|
|
|
|
|
`${window.siyuan.languages.confirmDelete} <b>${Lute.EscapeHTMLStr(getNotebookName(itemNotebookId))}</b>?`, () => {
|
|
|
|
|
fetchPost("/api/notebook/removeNotebook", {
|
|
|
|
|
notebook: itemNotebookId,
|
|
|
|
|
callback: Constants.CB_MOUNT_REMOVE
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-11-04 21:50:03 +08:00
|
|
|
const paths: string[] = [];
|
2022-11-03 11:12:23 +08:00
|
|
|
liElements.forEach(item => {
|
2022-11-04 21:50:03 +08:00
|
|
|
const dataPath = item.getAttribute("data-path");
|
2022-11-03 22:47:19 +08:00
|
|
|
if (dataPath !== "/") {
|
2022-11-03 14:45:06 +08:00
|
|
|
paths.push(item.getAttribute("data-path"));
|
|
|
|
|
}
|
2022-11-04 21:50:03 +08:00
|
|
|
});
|
2022-11-03 14:45:06 +08:00
|
|
|
if (paths.length === 0) {
|
2022-11-03 11:12:23 +08:00
|
|
|
showMessage(window.siyuan.languages.notBatchRemove);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
confirmDialog(window.siyuan.languages.deleteOpConfirm,
|
2023-03-24 10:09:56 +08:00
|
|
|
window.siyuan.languages.confirmRemoveAll.replace("${count}", paths.length), () => {
|
2022-11-03 14:46:09 +08:00
|
|
|
fetchPost("/api/filetree/removeDocs", {
|
2022-11-03 11:12:23 +08:00
|
|
|
paths
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-11-04 21:50:03 +08:00
|
|
|
};
|