| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import treeService from './tree.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 22:37:02 -04:00
										 |  |  | import treeCache from "./tree_cache.js"; | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | import clipboard from './clipboard.js'; | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | import noteCreateService from "./note_create.js"; | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  | import contextMenu from "./context_menu.js"; | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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(), | 
					
						
							|  |  |  |             selectMenuItemHandler: (item, e) => this.selectMenuItemHandler(item, e) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |     getNoteTypeItems(command) { | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: "Text", command: command, type: "text", uiIcon: "note" }, | 
					
						
							|  |  |  |             { title: "Code", command: command, type: "code", uiIcon: "code" }, | 
					
						
							|  |  |  |             { title: "Saved search", command: command, type: "search", uiIcon: "file-find" }, | 
					
						
							|  |  |  |             { title: "Relation Map", command: command, type: "relation-map", uiIcon: "map-alt" }, | 
					
						
							|  |  |  |             { title: "Render HTML note", command: command, type: "render", uiIcon: "extension" }, | 
					
						
							|  |  |  |             { title: "Book", command: command, type: "book", uiIcon: "book" } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |     async getMenuItems() { | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         const note = await treeCache.getNote(this.node.data.noteId); | 
					
						
							| 
									
										
										
										
											2019-11-04 20:20:21 +01:00
										 |  |  |         const branch = treeCache.getBranch(this.node.data.branchId); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         const isNotRoot = note.noteId !== 'root'; | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |         const isHoisted = note.noteId === appContext.tabManager.getActiveTabContext().hoistedNoteId; | 
					
						
							| 
									
										
										
										
											2020-05-03 13:52:12 +02:00
										 |  |  |         const parentNote = isNotRoot ? await treeCache.getNote(branch.parentNoteId) : null; | 
					
						
							| 
									
										
										
										
											2019-06-17 22:25:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // some actions don't support multi-note so they are disabled when notes are selected
 | 
					
						
							|  |  |  |         // 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'; | 
					
						
							| 
									
										
										
										
											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 [ | 
					
						
							| 
									
										
										
										
											2020-04-26 23:11:52 +02:00
										 |  |  |             { title: 'Open in a new tab <kbd>Ctrl+Click</kbd>', command: "openInTab", uiIcon: "empty", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-05-05 19:30:03 +02:00
										 |  |  |             { title: 'Open in a new window', command: "openInWindow", uiIcon: "window-open", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Insert note after <kbd data-command="createNoteAfter"></kbd>', command: "insertNoteAfter", uiIcon: "plus", | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |                 items: insertNoteAfterEnabled ? this.getNoteTypeItems("insertNoteAfter") : null, | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |                 enabled: insertNoteAfterEnabled && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Insert child note <kbd data-command="createNoteInto"></kbd>', command: "insertChildNote", uiIcon: "plus", | 
					
						
							| 
									
										
										
										
											2019-11-04 19:37:03 +01:00
										 |  |  |                 items: notSearch ? this.getNoteTypeItems("insertChildNote") : null, | 
					
						
							|  |  |  |                 enabled: notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Delete <kbd data-command="deleteNotes"></kbd>', command: "deleteNotes", uiIcon: "trash", | 
					
						
							| 
									
										
										
										
											2019-11-04 19:37:03 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted && parentNotSearch }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Search in subtree <kbd data-command="searchInSubtree"></kbd>', command: "searchInSubtree", uiIcon: "search", | 
					
						
							| 
									
										
										
										
											2019-11-19 21:11:20 +01:00
										 |  |  |                 enabled: notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             isHoisted ? null : { title: 'Hoist note <kbd data-command="toggleNoteHoisting"></kbd>', command: "toggleNoteHoisting", uiIcon: "empty", enabled: noSelectedNotes && notSearch }, | 
					
						
							| 
									
										
										
										
											2021-02-06 19:58:12 +01:00
										 |  |  |             !isHoisted || !isNotRoot ? null : { title: 'Unhoist note <kbd data-command="toggleNoteHoisting"></kbd>', command: "toggleNoteHoisting", uiIcon: "door-open" }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Edit branch prefix <kbd data-command="editBranchPrefix"></kbd>', command: "editBranchPrefix", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-11-04 19:37:03 +01:00
										 |  |  |                 enabled: isNotRoot && parentNotSearch && noSelectedNotes}, | 
					
						
							| 
									
										
										
										
											2020-02-09 10:15:35 +01:00
										 |  |  |             { title: "Advanced", uiIcon: "empty", enabled: true, items: [ | 
					
						
							| 
									
										
										
										
											2020-04-29 23:13:05 +02:00
										 |  |  |                     { title: 'Expand subtree <kbd data-command="expandSubtree"></kbd>', command: "expandSubtree", uiIcon: "expand", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |                     { title: 'Collapse subtree <kbd data-command="collapseSubtree"></kbd>', command: "collapseSubtree", uiIcon: "collapse", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |                     { title: "Force note sync", command: "forceNoteSync", uiIcon: "refresh", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2021-02-25 22:26:46 +01:00
										 |  |  |                     { title: 'Sort by ... <kbd data-command="sortChildNotes"></kbd>', command: "sortChildNotes", uiIcon: "empty", enabled: noSelectedNotes && notSearch }, | 
					
						
							| 
									
										
										
										
											2020-03-29 19:43:04 +02:00
										 |  |  |                     { title: 'Recent changes in subtree', command: "recentChangesInSubtree", uiIcon: "history", enabled: noSelectedNotes } | 
					
						
							| 
									
										
										
										
											2020-02-09 10:15:35 +01:00
										 |  |  |                 ] }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: "Protect subtree", command: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |             { title: "Unprotect subtree", command: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Copy / clone <kbd data-command="copyNotesToClipboard"></kbd>', command: "copyNotesToClipboard", uiIcon: "copy", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Clone to ... <kbd data-command="cloneNotesTo"></kbd>', command: "cloneNotesTo", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Cut <kbd data-command="cutNotesToClipboard"></kbd>', command: "cutNotesToClipboard", uiIcon: "cut", | 
					
						
							| 
									
										
										
										
											2019-11-04 22:41:06 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted && parentNotSearch }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Move to ... <kbd data-command="moveNotesTo"></kbd>', command: "moveNotesTo", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted && parentNotSearch }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Paste into <kbd data-command="pasteNotesFromClipboard"></kbd>', command: "pasteNotesFromClipboard", uiIcon: "paste", | 
					
						
							| 
									
										
										
										
											2019-11-20 19:24:23 +01:00
										 |  |  |                 enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: 'Paste after', command: "pasteNotesAfterFromClipboard", uiIcon: "paste", | 
					
						
							| 
									
										
										
										
											2019-11-20 19:24:23 +01:00
										 |  |  |                 enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-11-23 20:17:53 +01:00
										 |  |  |             { title: `Duplicate subtree <kbd data-command="duplicateSubtree">`, command: "duplicateSubtree", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2020-09-23 22:45:51 +02:00
										 |  |  |                 enabled: parentNotSearch && isNotRoot && !isHoisted }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: "Export", command: "exportNote", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-11-04 19:37:03 +01:00
										 |  |  |                 enabled: notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |             { title: "Import into note", command: "importIntoNote", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2020-02-09 10:15:35 +01:00
										 |  |  |                 enabled: notSearch && noSelectedNotes } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         ].filter(row => row !== null); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |     async selectMenuItemHandler({command, type}) { | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  |         const noteId = this.node.data.noteId; | 
					
						
							| 
									
										
										
										
											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") { | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             const parentNoteId = this.node.data.parentNoteId; | 
					
						
							| 
									
										
										
										
											2020-01-25 09:56:08 +01:00
										 |  |  |             const isProtected = await treeService.getParentProtectedStatus(this.node); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |             noteCreateService.createNote(parentNoteId, { | 
					
						
							|  |  |  |                 target: 'after', | 
					
						
							|  |  |  |                 targetBranchId: this.node.data.branchId, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |                 type: type, | 
					
						
							|  |  |  |                 isProtected: isProtected | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-29 13:03:05 +01:00
										 |  |  |         else if (command === "insertChildNote") { | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |             noteCreateService.createNote(noteId, { | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |                 type: type, | 
					
						
							|  |  |  |                 isProtected: this.node.data.isProtected | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2020-12-05 23:00:28 +01:00
										 |  |  |             console.log("Triggering", command, notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-05 19:30:03 +02:00
										 |  |  |             this.treeWidget.triggerCommand(command, {node: this.node, notePath: notePath}); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-17 20:58:34 +02:00
										 |  |  | export default TreeContextMenu; |