2025-01-09 18:07:02 +02:00
|
|
|
import utils from "./utils.js";
|
|
|
|
|
import server from "./server.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import toastService, { type ToastOptions } from "./toast.js";
|
2021-04-16 23:01:56 +02:00
|
|
|
import froca from "./froca.js";
|
2019-04-16 21:40:04 +02:00
|
|
|
import hoistedNoteService from "./hoisted_note.js";
|
2019-10-18 23:19:16 +02:00
|
|
|
import ws from "./ws.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../components/app_context.js";
|
2025-01-09 18:07:02 +02:00
|
|
|
import { t } from "./i18n.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { ResolveOptions } from "../widgets/dialogs/delete_notes.js";
|
2018-01-01 22:28:19 -05:00
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
// TODO: Deduplicate type with server
|
|
|
|
|
interface Response {
|
|
|
|
|
success: boolean;
|
|
|
|
|
message: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function moveBeforeBranch(branchIdsToMove: string[], beforeBranchId: string) {
|
2020-02-10 20:57:56 +01:00
|
|
|
branchIdsToMove = filterRootNote(branchIdsToMove);
|
2021-02-18 22:30:55 +01:00
|
|
|
branchIdsToMove = filterSearchBranches(branchIdsToMove);
|
2018-10-21 22:42:20 +02:00
|
|
|
|
2023-04-08 20:49:25 +08:00
|
|
|
const beforeBranch = froca.getBranch(beforeBranchId);
|
2024-12-21 15:34:07 +02:00
|
|
|
if (!beforeBranch) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-28 13:09:49 +01:00
|
|
|
|
2025-01-04 21:52:41 +02:00
|
|
|
if (beforeBranch.noteId === "root" || utils.isLaunchBarConfig(beforeBranch.noteId)) {
|
2024-10-20 02:19:47 +03:00
|
|
|
toastService.showError(t("branches.cannot-move-notes-here"));
|
2018-10-21 22:42:20 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 09:57:28 +01:00
|
|
|
for (const branchIdToMove of branchIdsToMove) {
|
2024-12-21 15:34:07 +02:00
|
|
|
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-before/${beforeBranchId}`);
|
2017-11-19 23:12:39 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
2018-01-01 18:29:06 -05:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
2018-01-19 18:34:21 -05:00
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
async function moveAfterBranch(branchIdsToMove: string[], afterBranchId: string) {
|
2020-02-10 20:57:56 +01:00
|
|
|
branchIdsToMove = filterRootNote(branchIdsToMove);
|
2021-02-18 22:30:55 +01:00
|
|
|
branchIdsToMove = filterSearchBranches(branchIdsToMove);
|
2020-01-12 09:57:28 +01:00
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
const afterNote = await froca.getBranch(afterBranchId)?.getNote();
|
|
|
|
|
if (!afterNote) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-21 22:42:20 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const forbiddenNoteIds = ["root", hoistedNoteService.getHoistedNoteId(), "_lbRoot", "_lbAvailableLaunchers", "_lbVisibleLaunchers"];
|
2022-11-25 15:29:57 +01:00
|
|
|
|
|
|
|
|
if (forbiddenNoteIds.includes(afterNote.noteId)) {
|
2024-10-20 02:19:47 +03:00
|
|
|
toastService.showError(t("branches.cannot-move-notes-here"));
|
2018-10-21 22:42:20 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-12 09:57:28 +01:00
|
|
|
branchIdsToMove.reverse(); // need to reverse to keep the note order
|
2018-01-01 22:28:19 -05:00
|
|
|
|
2020-01-12 09:57:28 +01:00
|
|
|
for (const branchIdToMove of branchIdsToMove) {
|
2024-12-21 15:34:07 +02:00
|
|
|
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-after/${afterBranchId}`);
|
2017-11-19 23:12:39 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
2018-01-01 18:29:06 -05:00
|
|
|
}
|
2017-11-04 22:10:41 -04:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-04 22:10:41 -04:00
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
async function moveToParentNote(branchIdsToMove: string[], newParentBranchId: string) {
|
2023-04-08 20:49:25 +08:00
|
|
|
const newParentBranch = froca.getBranch(newParentBranchId);
|
2024-12-21 15:34:07 +02:00
|
|
|
if (!newParentBranch) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-23 20:40:58 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (newParentBranch.noteId === "_lbRoot") {
|
2024-10-20 02:19:47 +03:00
|
|
|
toastService.showError(t("branches.cannot-move-notes-here"));
|
2022-11-25 15:29:57 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 20:57:56 +01:00
|
|
|
branchIdsToMove = filterRootNote(branchIdsToMove);
|
2020-01-12 09:57:28 +01:00
|
|
|
|
|
|
|
|
for (const branchIdToMove of branchIdsToMove) {
|
2021-04-16 22:57:37 +02:00
|
|
|
const branchToMove = froca.getBranch(branchIdToMove);
|
2018-10-21 22:42:20 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!branchToMove || branchToMove.noteId === hoistedNoteService.getHoistedNoteId() || (await branchToMove.getParentNote())?.type === "search") {
|
2019-11-04 22:41:06 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-to/${newParentBranchId}`);
|
2018-01-01 22:28:19 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2017-11-19 23:12:39 -05:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-04 22:10:41 -04:00
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
async function deleteNotes(branchIdsToDelete: string[], forceDeleteAllClones = false) {
|
2020-02-10 20:57:56 +01:00
|
|
|
branchIdsToDelete = filterRootNote(branchIdsToDelete);
|
2018-08-30 22:38:34 +02:00
|
|
|
|
2020-01-12 10:35:33 +01:00
|
|
|
if (branchIdsToDelete.length === 0) {
|
2019-08-17 02:49:08 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 10:53:29 +03:00
|
|
|
const { proceed, deleteAllClones, eraseNotes } = await new Promise<ResolveOptions>((res) =>
|
|
|
|
|
appContext.triggerCommand("showDeleteNotesDialog", { branchIdsToDelete, callback: res, forceDeleteAllClones })
|
|
|
|
|
);
|
2021-03-14 19:54:40 +01:00
|
|
|
|
2021-03-18 23:23:35 +01:00
|
|
|
if (!proceed) {
|
|
|
|
|
return false;
|
2020-01-12 10:35:33 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-28 23:08:44 +01:00
|
|
|
try {
|
|
|
|
|
await activateParentNotePath();
|
2025-01-09 18:07:02 +02:00
|
|
|
} catch (e) {
|
2024-01-28 23:08:44 +01:00
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 23:19:16 +02:00
|
|
|
const taskId = utils.randomString(10);
|
|
|
|
|
|
2019-10-19 00:11:07 +02:00
|
|
|
let counter = 0;
|
|
|
|
|
|
2020-01-12 10:35:33 +01:00
|
|
|
for (const branchIdToDelete of branchIdsToDelete) {
|
2019-10-19 00:11:07 +02:00
|
|
|
counter++;
|
|
|
|
|
|
2020-01-12 10:35:33 +01:00
|
|
|
const last = counter === branchIdsToDelete.length;
|
2025-01-09 18:07:02 +02:00
|
|
|
const query = `?taskId=${taskId}&eraseNotes=${eraseNotes ? "true" : "false"}&last=${last ? "true" : "false"}`;
|
2019-10-19 00:11:07 +02:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
const branch = froca.getBranch(branchIdToDelete);
|
2020-01-12 10:35:33 +01:00
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
if (deleteAllClones && branch) {
|
2022-12-21 15:19:05 +01:00
|
|
|
await server.remove(`notes/${branch.noteId}${query}`);
|
2024-12-21 15:34:07 +02:00
|
|
|
} else {
|
2022-12-21 15:19:05 +01:00
|
|
|
await server.remove(`branches/${branchIdToDelete}${query}`);
|
2019-09-01 20:57:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 19:47:44 +02:00
|
|
|
if (eraseNotes) {
|
|
|
|
|
utils.reloadFrontendApp("erasing notes requires reload");
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 20:53:59 +02:00
|
|
|
return true;
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2018-01-01 22:28:19 -05:00
|
|
|
|
2024-01-28 23:08:44 +01:00
|
|
|
async function activateParentNotePath() {
|
|
|
|
|
// this is not perfect, maybe we should find the next/previous sibling, but that's more complex
|
|
|
|
|
const activeContext = appContext.tabManager.getActiveContext();
|
2025-03-03 21:02:18 +01:00
|
|
|
const parentNotePathArr = activeContext?.notePathArray.slice(0, -1);
|
2024-01-28 23:08:44 +01:00
|
|
|
|
2025-03-03 21:02:18 +01:00
|
|
|
if (parentNotePathArr && parentNotePathArr.length > 0) {
|
|
|
|
|
activeContext?.setNote(parentNotePathArr.join("/"));
|
2024-01-28 23:08:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 10:10:34 +02:00
|
|
|
async function moveNodeUpInHierarchy(node: Fancytree.FancytreeNode) {
|
2025-01-09 18:07:02 +02:00
|
|
|
if (hoistedNoteService.isHoistedNode(node) || hoistedNoteService.isTopLevelNode(node) || node.getParent().data.noteType === "search") {
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2017-11-28 15:17:11 -05:00
|
|
|
|
2022-08-07 13:23:03 +02:00
|
|
|
const targetBranchId = node.getParent().data.branchId;
|
|
|
|
|
const branchIdToMove = node.data.branchId;
|
|
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-after/${targetBranchId}`);
|
2017-11-28 15:17:11 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
2017-12-17 16:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-10 20:57:56 +01:00
|
|
|
if (!hoistedNoteService.isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
|
2018-03-25 11:09:17 -04:00
|
|
|
node.getParent().folder = false;
|
2021-08-24 22:59:51 +02:00
|
|
|
node.getParent().renderTitle();
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-11-04 22:10:41 -04:00
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
function filterSearchBranches(branchIds: string[]) {
|
2025-01-09 18:07:02 +02:00
|
|
|
return branchIds.filter((branchId) => !branchId.startsWith("virt-"));
|
2021-02-18 22:30:55 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
function filterRootNote(branchIds: string[]) {
|
2020-02-10 20:57:56 +01:00
|
|
|
const hoistedNoteId = hoistedNoteService.getHoistedNoteId();
|
2019-04-16 21:40:04 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return branchIds.filter((branchId) => {
|
2023-04-08 20:49:25 +08:00
|
|
|
const branch = froca.getBranch(branchId);
|
2024-12-21 15:34:07 +02:00
|
|
|
if (!branch) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-01-12 09:57:28 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return branch.noteId !== "root" && branch.noteId !== hoistedNoteId;
|
2020-01-12 09:57:28 +01:00
|
|
|
});
|
2019-04-16 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-21 15:34:07 +02:00
|
|
|
function makeToast(id: string, message: string): ToastOptions {
|
2019-10-18 23:19:16 +02:00
|
|
|
return {
|
|
|
|
|
id: id,
|
2024-10-20 11:45:53 +03:00
|
|
|
title: t("branches.delete-status"),
|
2019-10-18 23:19:16 +02:00
|
|
|
message: message,
|
|
|
|
|
icon: "trash"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
ws.subscribeToMessages(async (message) => {
|
|
|
|
|
if (message.taskType !== "deleteNotes") {
|
2019-10-18 23:19:16 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (message.type === "taskError") {
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.closePersistent(message.taskId);
|
|
|
|
|
toastService.showError(message.message);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (message.type === "taskProgressCount") {
|
2024-10-20 11:45:53 +03:00
|
|
|
toastService.showPersistent(makeToast(message.taskId, t("branches.delete-notes-in-progress", { count: message.progressCount })));
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (message.type === "taskSucceeded") {
|
2024-10-20 11:45:53 +03:00
|
|
|
const toast = makeToast(message.taskId, t("branches.delete-finished-successfully"));
|
2019-10-18 23:19:16 +02:00
|
|
|
toast.closeAfter = 5000;
|
|
|
|
|
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.showPersistent(toast);
|
2019-10-18 23:19:16 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
ws.subscribeToMessages(async (message) => {
|
|
|
|
|
if (message.taskType !== "undeleteNotes") {
|
2020-01-03 13:14:43 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (message.type === "taskError") {
|
2020-01-03 13:14:43 +01:00
|
|
|
toastService.closePersistent(message.taskId);
|
|
|
|
|
toastService.showError(message.message);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (message.type === "taskProgressCount") {
|
2024-10-20 11:45:53 +03:00
|
|
|
toastService.showPersistent(makeToast(message.taskId, t("branches.undeleting-notes-in-progress", { count: message.progressCount })));
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (message.type === "taskSucceeded") {
|
2024-10-20 11:45:53 +03:00
|
|
|
const toast = makeToast(message.taskId, t("branches.undeleting-notes-finished-successfully"));
|
2020-01-03 13:14:43 +01:00
|
|
|
toast.closeAfter = 5000;
|
|
|
|
|
|
|
|
|
|
toastService.showPersistent(toast);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-21 16:48:14 +02:00
|
|
|
async function cloneNoteToBranch(childNoteId: string, parentBranchId: string, prefix?: string) {
|
2024-12-21 15:34:07 +02:00
|
|
|
const resp = await server.put<Response>(`notes/${childNoteId}/clone-to-branch/${parentBranchId}`, {
|
2021-12-27 23:39:46 +01:00
|
|
|
prefix: prefix
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2021-12-27 23:39:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-16 00:45:46 +02:00
|
|
|
async function cloneNoteToParentNote(childNoteId: string, parentNoteId: string, prefix?: string) {
|
2024-12-21 15:34:07 +02:00
|
|
|
const resp = await server.put<Response>(`notes/${childNoteId}/clone-to-note/${parentNoteId}`, {
|
2020-02-17 19:42:52 +01:00
|
|
|
prefix: prefix
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2020-02-17 19:42:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 23:41:11 +02:00
|
|
|
// beware that the first arg is noteId and the second is branchId!
|
2024-12-21 15:34:07 +02:00
|
|
|
async function cloneNoteAfter(noteId: string, afterBranchId: string) {
|
|
|
|
|
const resp = await server.put<Response>(`notes/${noteId}/clone-after/${afterBranchId}`);
|
2020-02-17 19:42:52 +01:00
|
|
|
|
|
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2020-02-17 19:42:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
export default {
|
2020-01-26 11:41:40 +01:00
|
|
|
moveBeforeBranch,
|
|
|
|
|
moveAfterBranch,
|
|
|
|
|
moveToParentNote,
|
2020-02-12 22:25:52 +01:00
|
|
|
deleteNotes,
|
2020-02-17 19:42:52 +01:00
|
|
|
moveNodeUpInHierarchy,
|
|
|
|
|
cloneNoteAfter,
|
2021-12-27 23:39:46 +01:00
|
|
|
cloneNoteToBranch,
|
2025-01-09 18:07:02 +02:00
|
|
|
cloneNoteToParentNote
|
2020-05-30 10:30:21 +02:00
|
|
|
};
|