| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import treeService from './tree.js'; | 
					
						
							| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  | import ws from './ws.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import protectedSessionService from './protected_session.js'; | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  | import treeChangesService from './branches.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | import treeUtils from './tree_utils.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 22:37:02 -04:00
										 |  |  | import treeCache from "./tree_cache.js"; | 
					
						
							| 
									
										
										
										
											2018-04-06 18:46:29 -04:00
										 |  |  | import syncService from "./sync.js"; | 
					
						
							| 
									
										
										
										
											2018-12-11 21:53:56 +01:00
										 |  |  | import hoistedNoteService from './hoisted_note.js'; | 
					
						
							| 
									
										
										
										
											2019-05-02 22:24:43 +02:00
										 |  |  | import noteDetailService from './note_detail.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"; | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TreeContextMenu { | 
					
						
							|  |  |  |     constructor(node) { | 
					
						
							|  |  |  |         this.node = node; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     getNoteTypeItems(baseCmd) { | 
					
						
							|  |  |  |         return [ | 
					
						
							|  |  |  |             { title: "Text", cmd: baseCmd + "_text", uiIcon: "file" }, | 
					
						
							|  |  |  |             { title: "Code", cmd: baseCmd + "_code", uiIcon: "terminal" }, | 
					
						
							|  |  |  |             { title: "Saved search", cmd: baseCmd + "_search", uiIcon: "search-folder" }, | 
					
						
							|  |  |  |             { title: "Relation Map", cmd: baseCmd + "_relation-map", uiIcon: "map" }, | 
					
						
							|  |  |  |             { title: "Render HTML note", cmd: baseCmd + "_render", uiIcon: "play" } | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async getContextMenuItems() { | 
					
						
							|  |  |  |         const branch = await treeCache.getBranch(this.node.data.branchId); | 
					
						
							|  |  |  |         const note = await treeCache.getNote(this.node.data.noteId); | 
					
						
							|  |  |  |         const parentNote = await treeCache.getNote(branch.parentNoteId); | 
					
						
							|  |  |  |         const isNotRoot = note.noteId !== 'root'; | 
					
						
							|  |  |  |         const isHoisted = note.noteId === await 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.
 | 
					
						
							|  |  |  |         const selNodes = treeService.getSelectedNodes(); | 
					
						
							|  |  |  |         const noSelectedNotes = selNodes.length === 0 | 
					
						
							|  |  |  |                 || (selNodes.length === 1 && selNodes[0] === this.node); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNote.type !== 'search'; | 
					
						
							|  |  |  |         const insertChildNoteEnabled = note.type !== 'search'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [ | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |             { title: "Open in new tab", cmd: "openInTab", uiIcon: "empty", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "Insert note after <kbd>Ctrl+O</kbd>", cmd: "insertNoteAfter", uiIcon: "plus", | 
					
						
							|  |  |  |                 items: insertNoteAfterEnabled ? this.getNoteTypeItems("insertNoteAfter") : null, | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |                 enabled: insertNoteAfterEnabled && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "Insert child note <kbd>Ctrl+P</kbd>", cmd: "insertChildNote", uiIcon: "plus", | 
					
						
							|  |  |  |                 items: insertChildNoteEnabled ? this.getNoteTypeItems("insertChildNote") : null, | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |                 enabled: insertChildNoteEnabled && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "Delete <kbd>Delete</kbd>", cmd: "delete", uiIcon: "trash", | 
					
						
							|  |  |  |                 enabled: isNotRoot && !isHoisted && parentNote.type !== 'search' }, | 
					
						
							|  |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |             isHoisted ? null : { title: "Hoist note <kbd>Ctrl-H</kbd>", cmd: "hoist", uiIcon: "empty", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             !isHoisted || !isNotRoot ? null : { title: "Unhoist note <kbd>Ctrl-H</kbd>", cmd: "unhoist", uiIcon: "arrow-up" }, | 
					
						
							|  |  |  |             { title: "Edit branch prefix <kbd>F2</kbd>", cmd: "editBranchPrefix", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |                 enabled: isNotRoot && parentNote.type !== 'search' && noSelectedNotes}, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |             { title: "Protect subtree", cmd: "protectSubtree", uiIcon: "shield-check", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |             { title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield-close", enabled: noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							|  |  |  |             { title: "Copy / clone <kbd>Ctrl+C</kbd>", cmd: "copy", uiIcon: "files", | 
					
						
							|  |  |  |                 enabled: isNotRoot }, | 
					
						
							|  |  |  |             { title: "Cut <kbd>Ctrl+X</kbd>", cmd: "cut", uiIcon: "scissors", | 
					
						
							|  |  |  |                 enabled: isNotRoot }, | 
					
						
							|  |  |  |             { title: "Paste into <kbd>Ctrl+V</kbd>", cmd: "pasteInto", uiIcon: "clipboard", | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |                 enabled: !clipboard.isEmpty() && note.type !== 'search' && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "Paste after", cmd: "pasteAfter", uiIcon: "clipboard", | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |                 enabled: !clipboard.isEmpty() && isNotRoot && parentNote.type !== 'search' && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  |             { title: "Duplicate note here", cmd: "duplicateNote", uiIcon: "empty", | 
					
						
							|  |  |  |                 enabled: noSelectedNotes && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							|  |  |  |             { title: "Export", cmd: "export", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |                 enabled: note.type !== 'search' && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "Import into note", cmd: "importIntoNote", uiIcon: "empty", | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |                 enabled: note.type !== 'search' && noSelectedNotes }, | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             { title: "----" }, | 
					
						
							| 
									
										
										
										
											2019-06-16 12:06:06 +02:00
										 |  |  |             { title: "Collapse subtree <kbd>Alt+-</kbd>", cmd: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |             { title: "Force note sync", cmd: "forceNoteSync", uiIcon: "refresh", enabled: noSelectedNotes }, | 
					
						
							|  |  |  |             { title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sortAlphabetically", uiIcon: "empty", enabled: noSelectedNotes } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         ].filter(row => row !== null); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async selectContextMenuItem(event, cmd) { | 
					
						
							|  |  |  |         if (cmd === 'openInTab') { | 
					
						
							| 
									
										
										
										
											2019-05-12 12:58:55 +02:00
										 |  |  |             const notePath = await treeUtils.getNotePath(this.node); | 
					
						
							| 
									
										
										
										
											2019-05-11 21:27:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             noteDetailService.openInTab(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; | 
					
						
							|  |  |  |             const isProtected = await treeUtils.getParentProtectedStatus(this.node); | 
					
						
							|  |  |  |             const type = cmd.split("_")[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             treeService.createNote(this.node, parentNoteId, 'after', { | 
					
						
							|  |  |  |                 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             treeService.createNote(this.node, this.node.data.noteId, 'into', { | 
					
						
							|  |  |  |                 type: type, | 
					
						
							|  |  |  |                 isProtected: this.node.data.isProtected | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "editBranchPrefix") { | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  |             const branchPrefixDialog = await import('../dialogs/branch_prefix.js'); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |             branchPrefixDialog.showDialog(this.node); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "protectSubtree") { | 
					
						
							|  |  |  |             protectedSessionService.protectSubtree(this.node.data.noteId, true); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "unprotectSubtree") { | 
					
						
							|  |  |  |             protectedSessionService.protectSubtree(this.node.data.noteId, false); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "copy") { | 
					
						
							| 
									
										
										
										
											2019-06-16 18:07:22 +02:00
										 |  |  |             clipboard.copy(treeService.getSelectedOrActiveNodes(this.node)); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "cut") { | 
					
						
							| 
									
										
										
										
											2019-06-16 18:07:22 +02:00
										 |  |  |             clipboard.cut(treeService.getSelectedOrActiveNodes(this.node)); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "pasteAfter") { | 
					
						
							|  |  |  |             clipboard.pasteAfter(this.node); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "pasteInto") { | 
					
						
							|  |  |  |             clipboard.pasteInto(this.node); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "delete") { | 
					
						
							| 
									
										
										
										
											2019-06-16 18:07:22 +02:00
										 |  |  |             treeChangesService.deleteNodes(treeService.getSelectedOrActiveNodes(this.node)); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "export") { | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  |             const exportDialog = await import('../dialogs/export.js'); | 
					
						
							| 
									
										
										
										
											2019-06-20 09:37:18 +02:00
										 |  |  |             exportDialog.showDialog(this.node,"subtree"); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "importIntoNote") { | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  |             const importDialog = await import('../dialogs/import.js'); | 
					
						
							| 
									
										
										
										
											2019-06-20 09:37:18 +02:00
										 |  |  |             importDialog.showDialog(this.node); | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "collapseSubtree") { | 
					
						
							|  |  |  |             treeService.collapseTree(this.node); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "forceNoteSync") { | 
					
						
							|  |  |  |             syncService.forceNoteSync(this.node.data.noteId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "sortAlphabetically") { | 
					
						
							|  |  |  |             treeService.sortAlphabetically(this.node.data.noteId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "hoist") { | 
					
						
							|  |  |  |             hoistedNoteService.setHoistedNoteId(this.node.data.noteId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (cmd === "unhoist") { | 
					
						
							|  |  |  |             hoistedNoteService.unhoist(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  |         else if (cmd === "duplicateNote") { | 
					
						
							|  |  |  |             const branch = await treeCache.getBranch(this.node.data.branchId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             treeService.duplicateNote(this.node.data.noteId, branch.parentNoteId); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-03 20:27:38 +02:00
										 |  |  |         else { | 
					
						
							| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  |             ws.logError("Unknown command: " + cmd); | 
					
						
							| 
									
										
										
										
											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; |