| 
									
										
										
										
											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"; | 
					
						
							| 
									
										
										
										
											2018-12-11 21:53:56 +01:00
										 |  |  | import hoistedNoteService from './hoisted_note.js'; | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | import clipboard from './clipboard.js'; | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  | import protectedSessionHolder from "./protected_session_holder.js"; | 
					
						
							| 
									
										
										
										
											2020-01-21 22:54:16 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | import noteCreateService from "./note_create.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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     getNoteTypeItems(baseCmd) { | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |             { title: "Text", cmd: baseCmd + "_text", uiIcon: "note" }, | 
					
						
							|  |  |  |             { title: "Code", cmd: baseCmd + "_code", uiIcon: "code" }, | 
					
						
							|  |  |  |             { title: "Saved search", cmd: baseCmd + "_search", uiIcon: "file-find" }, | 
					
						
							|  |  |  |             { title: "Relation Map", cmd: baseCmd + "_relation-map", uiIcon: "map-alt" }, | 
					
						
							|  |  |  |             { title: "Render HTML note", cmd: baseCmd + "_render", uiIcon: "extension" }, | 
					
						
							|  |  |  |             { title: "Book", cmd: baseCmd + "_book", uiIcon: "book" } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async getContextMenuItems() { | 
					
						
							|  |  |  |         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 parentNote = await treeCache.getNote(branch.parentNoteId); | 
					
						
							|  |  |  |         const isNotRoot = note.noteId !== 'root'; | 
					
						
							| 
									
										
										
										
											2020-02-10 20:57:56 +01:00
										 |  |  |         const isHoisted = note.noteId === hoistedNoteService.getHoistedNoteId(); | 
					
						
							| 
									
										
										
										
											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 [ | 
					
						
							| 
									
										
										
										
											2019-11-22 22:35:59 +01:00
										 |  |  |             { title: 'Open in new tab', cmd: "openInTab", uiIcon: "empty", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |             { title: 'Insert note after <kbd data-kb-command="createNoteAfter"></kbd>', cmd: "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-16 19:54:11 +01:00
										 |  |  |             { title: 'Insert child note <kbd data-kb-command="createNoteInto"></kbd>', cmd: "insertChildNote", uiIcon: "plus", | 
					
						
							| 
									
										
										
										
											2019-11-04 19:37:03 +01:00
										 |  |  |                 items: notSearch ? this.getNoteTypeItems("insertChildNote") : null, | 
					
						
							|  |  |  |                 enabled: notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             { title: 'Delete <kbd data-kb-command="deleteNotes"></kbd>', cmd: "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-16 19:54:11 +01:00
										 |  |  |             { title: 'Search in subtree <kbd data-kb-command="searchInSubtree"></kbd>', cmd: "searchInSubtree", uiIcon: "search", | 
					
						
							| 
									
										
										
										
											2019-11-19 21:11:20 +01:00
										 |  |  |                 enabled: notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             isHoisted ? null : { title: 'Hoist note <kbd data-kb-command="toggleNoteHoisting"></kbd>', cmd: "toggleNoteHoisting", uiIcon: "empty", enabled: noSelectedNotes && notSearch }, | 
					
						
							|  |  |  |             !isHoisted || !isNotRoot ? null : { title: 'Unhoist note <kbd data-kb-command="ToggleNoteHoisting"></kbd>', cmd: "toggleNoteHoisting", uiIcon: "arrow-up" }, | 
					
						
							| 
									
										
										
										
											2020-02-16 19:54:11 +01:00
										 |  |  |             { title: 'Edit branch prefix <kbd data-kb-command="editBranchPrefix"></kbd>', cmd: "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-02-16 19:54:11 +01:00
										 |  |  |                     { title: 'Collapse subtree <kbd data-kb-command="collapseSubtree"></kbd>', cmd: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-15 09:16:23 +01:00
										 |  |  |                     { title: "Force note sync", cmd: "forceNoteSync", uiIcon: "refresh", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |                     { title: 'Sort alphabetically <kbd data-kb-command="sortChildNotes"></kbd>', cmd: "sortChildNotes", uiIcon: "empty", enabled: noSelectedNotes && notSearch } | 
					
						
							| 
									
										
										
										
											2020-02-09 10:15:35 +01:00
										 |  |  |                 ] }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2019-11-09 13:32:06 +01:00
										 |  |  |             { title: "Protect subtree", cmd: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |             { title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             { title: 'Copy / clone <kbd data-kb-command="copyNotesToClipboard"></kbd>', cmd: "copyNotesToClipboard", uiIcon: "copy", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             { title: 'Clone to ... <kbd data-kb-command="cloneNotesTo"></kbd>', cmd: "cloneNotesTo", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             { title: 'Cut <kbd data-kb-command="cutNotesToClipboard"></kbd>', cmd: "cutNotesToClipboard", uiIcon: "cut", | 
					
						
							| 
									
										
										
										
											2019-11-04 22:41:06 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted && parentNotSearch }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             { title: 'Move to ... <kbd data-kb-command="moveNotesTo"></kbd>', cmd: "moveNotesTo", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-11-11 22:57:51 +01:00
										 |  |  |                 enabled: isNotRoot && !isHoisted && parentNotSearch }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             { title: 'Paste into <kbd data-kb-command="pasteNotesFromClipboard"></kbd>', cmd: "pasteNotesFromClipboard", uiIcon: "paste", | 
					
						
							| 
									
										
										
										
											2019-11-20 19:24:23 +01:00
										 |  |  |                 enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             { title: 'Paste after', cmd: "pasteNotesAfterFromClipboard", uiIcon: "paste", | 
					
						
							| 
									
										
										
										
											2019-11-20 19:24:23 +01:00
										 |  |  |                 enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  |             { title: "Duplicate note here", cmd: "duplicateNote", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-11-26 19:42:47 +01:00
										 |  |  |                 enabled: noSelectedNotes && parentNotSearch && isNotRoot && !isHoisted && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2020-02-16 22:56:40 +01:00
										 |  |  |             { title: "Export", cmd: "exportNote", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-11-04 19:37:03 +01:00
										 |  |  |                 enabled: notSearch && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "Import into note", cmd: "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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async selectContextMenuItem(event, cmd) { | 
					
						
							| 
									
										
										
										
											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-01-21 21:43:23 +01:00
										 |  |  |         if (cmd === 'openInTab') { | 
					
						
							| 
									
										
										
										
											2020-02-09 21:13:05 +01:00
										 |  |  |             const tabContext = appContext.tabManager.openEmptyTab(); | 
					
						
							| 
									
										
										
										
											2020-02-07 21:08:55 +01:00
										 |  |  |             appContext.tabManager.activateTab(tabContext.tabId); | 
					
						
							| 
									
										
										
										
											2020-01-24 17:54:47 +01:00
										 |  |  |             tabContext.setNote(notePath); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         else if (cmd.startsWith("insertNoteAfter")) { | 
					
						
							|  |  |  |             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
										 |  |  |             const type = cmd.split("_")[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         else if (cmd.startsWith("insertChildNote")) { | 
					
						
							|  |  |  |             const type = cmd.split("_")[1]; | 
					
						
							| 
									
										
										
										
											2019-03-18 22:33:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-02-16 22:56:40 +01:00
										 |  |  |             this.treeWidget.triggerCommand(cmd, {node: this.node}); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | export default TreeContextMenu; |