Notes/apps/client/src/services/branches.ts

277 lines
8.6 KiB
TypeScript
Raw Normal View History

2025-01-09 18:07:02 +02:00
import utils from "./utils.js";
import server from "./server.js";
import toastService, { type ToastOptions } from "./toast.js";
2021-04-16 23:01:56 +02:00
import froca from "./froca.js";
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";
import type { ResolveOptions } from "../widgets/dialogs/delete_notes.js";
2018-01-01 22:28:19 -05: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);
branchIdsToMove = filterSearchBranches(branchIdsToMove);
2023-04-08 20:49:25 +08:00
const beforeBranch = froca.getBranch(beforeBranchId);
if (!beforeBranch) {
return;
}
2022-12-28 13:09:49 +01: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"));
return;
}
2020-01-12 09:57:28 +01:00
for (const branchIdToMove of branchIdsToMove) {
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-before/${beforeBranchId}`);
2017-11-19 23:12:39 -05:00
if (!resp.success) {
2022-08-24 23:20:05 +02:00
toastService.showError(resp.message);
return;
}
}
}
async function moveAfterBranch(branchIdsToMove: string[], afterBranchId: string) {
2020-02-10 20:57:56 +01:00
branchIdsToMove = filterRootNote(branchIdsToMove);
branchIdsToMove = filterSearchBranches(branchIdsToMove);
2020-01-12 09:57:28 +01:00
const afterNote = await froca.getBranch(afterBranchId)?.getNote();
if (!afterNote) {
return;
}
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"));
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) {
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-after/${afterBranchId}`);
2017-11-19 23:12:39 -05:00
if (!resp.success) {
2022-08-24 23:20:05 +02:00
toastService.showError(resp.message);
return;
}
2017-11-04 22:10:41 -04:00
}
}
2017-11-04 22:10:41 -04:00
async function moveToParentNote(branchIdsToMove: string[], newParentBranchId: string) {
2023-04-08 20:49:25 +08:00
const newParentBranch = froca.getBranch(newParentBranchId);
if (!newParentBranch) {
return;
}
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);
2025-01-09 18:07:02 +02:00
if (!branchToMove || branchToMove.noteId === hoistedNoteService.getHoistedNoteId() || (await branchToMove.getParentNote())?.type === "search") {
continue;
}
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-to/${newParentBranchId}`);
2018-01-01 22:28:19 -05:00
if (!resp.success) {
2022-08-24 23:20:05 +02:00
toastService.showError(resp.message);
return;
}
2017-11-19 23:12:39 -05:00
}
}
2017-11-04 22:10:41 -04: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) {
return false;
}
const { proceed, deleteAllClones, eraseNotes } = await new Promise<ResolveOptions>((res) =>
appContext.triggerCommand("showDeleteNotesDialog", { branchIdsToDelete, callback: res, forceDeleteAllClones })
);
2021-03-18 23:23:35 +01:00
if (!proceed) {
return false;
2020-01-12 10:35:33 +01:00
}
try {
await activateParentNotePath();
2025-01-09 18:07:02 +02:00
} catch (e) {
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
if (deleteAllClones && branch) {
await server.remove(`notes/${branch.noteId}${query}`);
} else {
await server.remove(`branches/${branchIdToDelete}${query}`);
}
}
if (eraseNotes) {
utils.reloadFrontendApp("erasing notes requires reload");
}
2019-05-22 20:53:59 +02:00
return true;
}
2018-01-01 22:28:19 -05: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);
2025-03-03 21:02:18 +01:00
if (parentNotePathArr && parentNotePathArr.length > 0) {
activeContext?.setNote(parentNotePathArr.join("/"));
}
}
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") {
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;
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-after/${targetBranchId}`);
2017-11-28 15:17:11 -05:00
if (!resp.success) {
2022-08-24 23:20:05 +02:00
toastService.showError(resp.message);
return;
}
2020-02-10 20:57:56 +01:00
if (!hoistedNoteService.isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
node.getParent().folder = false;
2021-08-24 22:59:51 +02:00
node.getParent().renderTitle();
}
}
2017-11-04 22:10:41 -04:00
function filterSearchBranches(branchIds: string[]) {
2025-01-09 18:07:02 +02:00
return branchIds.filter((branchId) => !branchId.startsWith("virt-"));
}
function filterRootNote(branchIds: string[]) {
2020-02-10 20:57:56 +01:00
const hoistedNoteId = hoistedNoteService.getHoistedNoteId();
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);
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
});
}
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);
}
});
async function cloneNoteToBranch(childNoteId: string, parentBranchId: string, prefix?: string) {
const resp = await server.put<Response>(`notes/${childNoteId}/clone-to-branch/${parentBranchId}`, {
prefix: prefix
});
if (!resp.success) {
2022-08-24 23:20:05 +02:00
toastService.showError(resp.message);
}
}
2025-03-16 00:45:46 +02:00
async function cloneNoteToParentNote(childNoteId: string, parentNoteId: string, prefix?: string) {
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!
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
}
}
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,
cloneNoteToBranch,
2025-01-09 18:07:02 +02:00
cloneNoteToParentNote
2020-05-30 10:30:21 +02:00
};