Notes/src/public/app/services/branches.ts

297 lines
8.8 KiB
TypeScript
Raw Normal View History

import utils from './utils.js';
import server from './server.js';
import toastService, { 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";
2024-10-20 11:45:53 +03:00
import { t } from './i18n.js';
import { Node } from './tree.js';
import { 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 (['root', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(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;
}
2022-11-25 15:29:57 +01:00
const forbiddenNoteIds = [
'root',
hoistedNoteService.getHoistedNoteId(),
2022-12-21 16:11:00 +01:00
'_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;
}
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);
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;
}
2021-09-16 14:38:09 +02:00
let proceed, deleteAllClones, eraseNotes;
if (utils.isMobile()) {
proceed = true;
deleteAllClones = false;
}
else {
({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();
}
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;
2021-09-16 14:38:09 +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();
const parentNotePathArr = activeContext.notePathArray.slice(0, -1);
if (parentNotePathArr.length > 0) {
activeContext.setNote(parentNotePathArr.join("/"));
}
}
async function moveNodeUpInHierarchy(node: Node) {
if (hoistedNoteService.isHoistedNode(node)
2020-02-10 20:57:56 +01:00
|| 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[]) {
return branchIds.filter(branchId => !branchId.startsWith('virt-'));
}
function filterRootNote(branchIds: string[]) {
2020-02-10 20:57:56 +01:00
const hoistedNoteId = hoistedNoteService.getHoistedNoteId();
2020-01-12 09:57:28 +01: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
return branch.noteId !== 'root'
&& branch.noteId !== hoistedNoteId;
});
}
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"
};
}
ws.subscribeToMessages(async message => {
2023-05-09 00:05:27 +02:00
if (message.taskType !== 'deleteNotes') {
2019-10-18 23:19:16 +02:00
return;
}
2021-04-24 11:39:44 +02:00
if (message.type === 'taskError') {
2019-10-20 10:00:18 +02:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2021-04-24 11:39:44 +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 })));
2021-04-24 11:39:44 +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
}
});
2020-01-03 13:14:43 +01:00
ws.subscribeToMessages(async message => {
2021-04-24 11:39:44 +02:00
if (message.taskType !== 'undeleteNotes') {
2020-01-03 13:14:43 +01:00
return;
}
2021-04-24 11:39:44 +02:00
if (message.type === 'taskError') {
2020-01-03 13:14:43 +01:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2021-04-24 11:39:44 +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 })));
2021-04-24 11:39:44 +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);
}
}
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,
2023-04-15 00:06:13 +02:00
cloneNoteToParentNote,
2020-05-30 10:30:21 +02:00
};