| 
									
										
										
										
											2022-08-05 16:44:26 +02:00
										 |  |  | import treeService from '../services/tree.js'; | 
					
						
							|  |  |  | import froca from "../services/froca.js"; | 
					
						
							|  |  |  | import clipboard from '../services/clipboard.js'; | 
					
						
							|  |  |  | import noteCreateService from "../services/note_create.js"; | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  | import contextMenu from "./context_menu.js"; | 
					
						
							| 
									
										
										
										
											2022-12-01 13:07:23 +01:00
										 |  |  | import appContext from "../components/app_context.js"; | 
					
						
							| 
									
										
										
										
											2022-08-05 16:44:26 +02:00
										 |  |  | import noteTypesService from "../services/note_types.js"; | 
					
						
							| 
									
										
										
										
											2023-05-02 23:04:41 +02:00
										 |  |  | import server from "../services/server.js"; | 
					
						
							|  |  |  | import toastService from "../services/toast.js"; | 
					
						
							|  |  |  | import dialogService from "../services/dialog.js"; | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  | import { t } from "../services/i18n.js"; | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 16:44:26 +02:00
										 |  |  | export default class TreeContextMenu { | 
					
						
							| 
									
										
										
										
											2020-01-12 09:12:13 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param {NoteTreeWidget} treeWidget | 
					
						
							|  |  |  |      * @param {FancytreeNode} node | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     constructor(treeWidget, node) { | 
					
						
							|  |  |  |         this.treeWidget = treeWidget; | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         this.node = node; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-17 20:58:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |     async show(e) { | 
					
						
							|  |  |  |         contextMenu.show({ | 
					
						
							|  |  |  |             x: e.pageX, | 
					
						
							|  |  |  |             y: e.pageY, | 
					
						
							|  |  |  |             items: await this.getMenuItems(), | 
					
						
							| 
									
										
										
										
											2023-01-15 21:04:17 +01:00
										 |  |  |             selectMenuItemHandler: (item, e) => this.selectMenuItemHandler(item) | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |     async getMenuItems() { | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |         const note = await froca.getNote(this.node.data.noteId); | 
					
						
							|  |  |  |         const branch = froca.getBranch(this.node.data.branchId); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         const isNotRoot = note.noteId !== 'root'; | 
					
						
							| 
									
										
										
										
											2021-05-22 12:35:41 +02:00
										 |  |  |         const isHoisted = note.noteId === appContext.tabManager.getActiveContext().hoistedNoteId; | 
					
						
							| 
									
										
										
										
											2021-04-16 22:57:37 +02:00
										 |  |  |         const parentNote = isNotRoot ? await froca.getNote(branch.parentNoteId) : null; | 
					
						
							| 
									
										
										
										
											2019-06-17 22:25:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-30 11:18:34 +02:00
										 |  |  |         // some actions don't support multi-note, so they are disabled when notes are selected,
 | 
					
						
							| 
									
										
										
										
											2019-06-17 22:25:22 +02:00
										 |  |  |         // the only exception is when the only selected note is the one that was right-clicked, then
 | 
					
						
							|  |  |  |         // it's clear what the user meant to do.
 | 
					
						
							| 
									
										
										
										
											2020-01-12 09:12:13 +01:00
										 |  |  |         const selNodes = this.treeWidget.getSelectedNodes(); | 
					
						
							| 
									
										
										
										
											2019-06-17 22:25:22 +02:00
										 |  |  |         const noSelectedNotes = selNodes.length === 0 | 
					
						
							|  |  |  |                 || (selNodes.length === 1 && selNodes[0] === this.node); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:21 +01:00
										 |  |  |         const notSearch = note.type !== 'search'; | 
					
						
							| 
									
										
										
										
											2024-08-17 02:01:12 +03:00
										 |  |  |         const notOptions = !note.noteId.startsWith("_options"); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:42:47 +01:00
										 |  |  |         const parentNotSearch = !parentNote || parentNote.type !== 'search'; | 
					
						
							| 
									
										
										
										
											2019-11-04 19:37:03 +01:00
										 |  |  |         const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch; | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.open-in-a-new-tab")} <kbd>Ctrl+Click</kbd>`, command: "openInTab", uiIcon: "bx bx-empty", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |             { title: t("tree-context-menu.open-in-a-new-split"), command: "openNoteInSplit", uiIcon: "bx bx-dock-right", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |             { title: `${t("tree-context-menu.insert-note-after")} <kbd data-command="createNoteAfter"></kbd>`, command: "insertNoteAfter", uiIcon: "bx bx-plus", | 
					
						
							| 
									
										
										
										
											2024-05-30 06:24:50 +02:00
										 |  |  |                 items: insertNoteAfterEnabled ? await noteTypesService.getNoteTypeItems("insertNoteAfter") : null, | 
					
						
							| 
									
										
										
										
											2024-08-17 02:01:12 +03:00
										 |  |  |                 enabled: insertNoteAfterEnabled && noSelectedNotes && notOptions }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.insert-child-note")} <kbd data-command="createNoteInto"></kbd>`, command: "insertChildNote", uiIcon: "bx bx-plus", | 
					
						
							| 
									
										
										
										
											2024-05-30 06:24:50 +02:00
										 |  |  |                 items: notSearch ? await noteTypesService.getNoteTypeItems("insertChildNote") : null, | 
					
						
							| 
									
										
										
										
											2024-08-17 02:01:12 +03:00
										 |  |  |                 enabled: notSearch && noSelectedNotes && notOptions }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.delete")} <kbd data-command="deleteNotes"></kbd>`, command: "deleteNotes", uiIcon: "bx bx-trash", | 
					
						
							| 
									
										
										
										
											2024-08-17 02:01:12 +03:00
										 |  |  |                 enabled: isNotRoot && !isHoisted && parentNotSearch && notOptions }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.search-in-subtree")} <kbd data-command="searchInSubtree"></kbd>`, command: "searchInSubtree", uiIcon: "bx bx-search", | 
					
						
							| 
									
										
										
										
											2019-11-19 21:11:20 +01:00
										 |  |  |                 enabled: notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2024-10-15 15:24:01 +08:00
										 |  |  |             isHoisted ? null : { title: `${t("tree-context-menu.hoist-note")} <kbd data-command="toggleNoteHoisting"></kbd>`, command: "toggleNoteHoisting", uiIcon: "bx bx-empty", enabled: noSelectedNotes && notSearch }, | 
					
						
							|  |  |  |             !isHoisted || !isNotRoot ? null : { title: `${t("tree-context-menu.unhoist-note")} <kbd data-command="toggleNoteHoisting"></kbd>`, command: "toggleNoteHoisting", uiIcon: "bx bx-door-open" }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.edit-branch-prefix")} <kbd data-command="editBranchPrefix"></kbd>`, command: "editBranchPrefix", uiIcon: "bx bx-empty", | 
					
						
							| 
									
										
										
										
											2024-08-17 02:01:12 +03:00
										 |  |  |                 enabled: isNotRoot && parentNotSearch && noSelectedNotes && notOptions }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: t("tree-context-menu.advanced"), uiIcon: "bx bx-empty", enabled: true, items: [ | 
					
						
							|  |  |  |                     { title: `${t("tree-context-menu.expand-subtree")} <kbd data-command="expandSubtree"></kbd>`, command: "expandSubtree", uiIcon: "bx bx-expand", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |                     { title: `${t("tree-context-menu.collapse-subtree")} <kbd data-command="collapseSubtree"></kbd>`, command: "collapseSubtree", uiIcon: "bx bx-collapse", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |                     { title: `${t("tree-context-menu.sort-by")} <kbd data-command="sortChildNotes"></kbd>`, command: "sortChildNotes", uiIcon: "bx bx-empty", enabled: noSelectedNotes && notSearch }, | 
					
						
							|  |  |  |                     { title: t("tree-context-menu.recent-changes-in-subtree"), command: "recentChangesInSubtree", uiIcon: "bx bx-history", enabled: noSelectedNotes && notOptions }, | 
					
						
							|  |  |  |                     { title: t("tree-context-menu.convert-to-attachment"), command: "convertNoteToAttachment", uiIcon: "bx bx-empty", enabled: isNotRoot && !isHoisted && notOptions }, | 
					
						
							|  |  |  |                     { title: t("tree-context-menu.copy-note-path-to-clipboard"), command: "copyNotePathToClipboard", uiIcon: "bx bx-empty", enabled: true } | 
					
						
							| 
									
										
										
										
											2020-02-09 10:15:35 +01:00
										 |  |  |                 ] }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: t("tree-context-menu.protect-subtree"), command: "protectSubtree", uiIcon: "bx bx-check-shield", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |             { title: t("tree-context-menu.unprotect-subtree"), command: "unprotectSubtree", uiIcon: "bx bx-shield", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.copy-clone")} <kbd data-command="copyNotesToClipboard"></kbd>`, command: "copyNotesToClipboard", uiIcon: "bx bx-copy", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.clone-to")} <kbd data-command="cloneNotesTo"></kbd>`, command: "cloneNotesTo", uiIcon: "bx bx-empty", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.cut")} <kbd data-command="cutNotesToClipboard"></kbd>`, command: "cutNotesToClipboard", uiIcon: "bx bx-cut", | 
					
						
							| 
									
										
										
										
											2019-11-04 22:41:06 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted && parentNotSearch }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.move-to")} <kbd data-command="moveNotesTo"></kbd>`, command: "moveNotesTo", uiIcon: "bx bx-empty", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted && parentNotSearch }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.paste-into")} <kbd data-command="pasteNotesFromClipboard"></kbd>`, command: "pasteNotesFromClipboard", uiIcon: "bx bx-paste", | 
					
						
							| 
									
										
										
										
											2019-11-20 19:24:23 +01:00
										 |  |  |                 enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: t("tree-context-menu.paste-after"), command: "pasteNotesAfterFromClipboard", uiIcon: "bx bx-paste", | 
					
						
							| 
									
										
										
										
											2019-11-20 19:24:23 +01:00
										 |  |  |                 enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: `${t("tree-context-menu.duplicate-subtree")} <kbd data-command="duplicateSubtree">`, command: "duplicateSubtree", uiIcon: "bx bx-empty", | 
					
						
							| 
									
										
										
										
											2024-08-17 02:01:12 +03:00
										 |  |  |                 enabled: parentNotSearch && isNotRoot && !isHoisted && notOptions }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: t("tree-context-menu.export"), command: "exportNote", uiIcon: "bx bx-empty", | 
					
						
							| 
									
										
										
										
											2024-08-17 02:01:12 +03:00
										 |  |  |                 enabled: notSearch && noSelectedNotes && notOptions }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: t("tree-context-menu.import-into-note"), command: "importIntoNote", uiIcon: "bx bx-empty", | 
					
						
							| 
									
										
										
										
											2024-08-17 02:01:12 +03:00
										 |  |  |                 enabled: notSearch && noSelectedNotes && notOptions }, | 
					
						
							| 
									
										
										
										
											2024-09-08 12:32:22 +03:00
										 |  |  |             { title: t("tree-context-menu.apply-bulk-actions"), command: "openBulkActionsDialog", uiIcon: "bx bx-list-plus", | 
					
						
							| 
									
										
										
										
											2022-05-30 22:43:20 +02:00
										 |  |  |                 enabled: true } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         ].filter(row => row !== null); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-31 23:27:45 +02:00
										 |  |  |     async selectMenuItemHandler({command, type, templateNoteId}) { | 
					
						
							| 
									
										
										
										
											2020-02-10 20:57:56 +01:00
										 |  |  |         const notePath = treeService.getNotePath(this.node); | 
					
						
							| 
									
										
										
										
											2019-05-11 21:27:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |         if (command === 'openInTab') { | 
					
						
							| 
									
										
										
										
											2020-11-24 23:24:05 +01:00
										 |  |  |             appContext.tabManager.openTabWithNoteWithHoisting(notePath); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |         else if (command === "insertNoteAfter") { | 
					
						
							| 
									
										
										
										
											2021-03-03 22:48:06 +01:00
										 |  |  |             const parentNotePath = treeService.getNotePath(this.node.getParent()); | 
					
						
							| 
									
										
										
										
											2023-05-05 23:17:23 +02:00
										 |  |  |             const isProtected = treeService.getParentProtectedStatus(this.node); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-03 22:48:06 +01:00
										 |  |  |             noteCreateService.createNote(parentNotePath, { | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |                 target: 'after', | 
					
						
							|  |  |  |                 targetBranchId: this.node.data.branchId, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |                 type: type, | 
					
						
							| 
									
										
										
										
											2022-05-31 23:27:45 +02:00
										 |  |  |                 isProtected: isProtected, | 
					
						
							|  |  |  |                 templateNoteId: templateNoteId | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |         else if (command === "insertChildNote") { | 
					
						
							| 
									
										
										
										
											2021-03-03 22:48:06 +01:00
										 |  |  |             const parentNotePath = treeService.getNotePath(this.node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             noteCreateService.createNote(parentNotePath, { | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |                 type: type, | 
					
						
							| 
									
										
										
										
											2022-05-31 23:27:45 +02:00
										 |  |  |                 isProtected: this.node.data.isProtected, | 
					
						
							|  |  |  |                 templateNoteId: templateNoteId | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-03 22:23:11 +02:00
										 |  |  |         else if (command === 'openNoteInSplit') { | 
					
						
							| 
									
										
										
										
											2021-06-03 12:25:33 +02:00
										 |  |  |             const subContexts = appContext.tabManager.getActiveContext().getSubContexts(); | 
					
						
							|  |  |  |             const {ntxId} = subContexts[subContexts.length - 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 22:23:11 +02:00
										 |  |  |             this.treeWidget.triggerCommand("openNewNoteSplit", {ntxId, notePath}); | 
					
						
							| 
									
										
										
										
											2021-06-03 12:25:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-02 23:04:41 +02:00
										 |  |  |         else if (command === 'convertNoteToAttachment') { | 
					
						
							|  |  |  |             if (!await dialogService.confirm(`Are you sure you want to convert note selected notes into attachments of their parent notes?`)) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             let converted = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const noteId of this.treeWidget.getSelectedOrActiveNoteIds(this.node)) { | 
					
						
							|  |  |  |                 const note = await froca.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (note.isEligibleForConversionToAttachment()) { | 
					
						
							|  |  |  |                     const {attachment} = await server.post(`notes/${note.noteId}/convert-to-attachment`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (attachment) { | 
					
						
							|  |  |  |                         converted++; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             toastService.showMessage(`${converted} notes have been converted to attachments.`); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-09-08 23:00:43 +02:00
										 |  |  |         else if (command === 'copyNotePathToClipboard') { | 
					
						
							|  |  |  |             navigator.clipboard.writeText('#' + notePath); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         else { | 
					
						
							| 
									
										
										
										
											2022-06-12 00:05:46 +02:00
										 |  |  |             this.treeWidget.triggerCommand(command, { | 
					
						
							|  |  |  |                 node: this.node, | 
					
						
							|  |  |  |                 notePath: notePath, | 
					
						
							| 
									
										
										
										
											2022-11-08 22:19:16 +01:00
										 |  |  |                 noteId: this.node.data.noteId, | 
					
						
							| 
									
										
										
										
											2022-06-12 13:57:22 +02:00
										 |  |  |                 selectedOrActiveBranchIds: this.treeWidget.getSelectedOrActiveBranchIds(this.node), | 
					
						
							|  |  |  |                 selectedOrActiveNoteIds: this.treeWidget.getSelectedOrActiveNoteIds(this.node) | 
					
						
							| 
									
										
										
										
											2022-06-12 00:05:46 +02:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } |