| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import treeService from './tree.js'; | 
					
						
							|  |  |  | import cloningService from './cloning.js'; | 
					
						
							|  |  |  | import messagingService from './messaging.js'; | 
					
						
							|  |  |  | 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-04-01 20:50:58 -04:00
										 |  |  | import branchPrefixDialog from '../dialogs/branch_prefix.js'; | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | import exportDialog from '../dialogs/export.js'; | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  | import importDialog from '../dialogs/import.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  | import infoService from "./info.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'; | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | let clipboardIds = []; | 
					
						
							|  |  |  | let clipboardMode = null; | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function pasteAfter(node) { | 
					
						
							|  |  |  |     if (clipboardMode === 'cut') { | 
					
						
							|  |  |  |         const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey)); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |         await treeChangesService.moveAfterNode(nodes, node); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         clipboardIds = []; | 
					
						
							|  |  |  |         clipboardMode = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (clipboardMode === 'copy') { | 
					
						
							|  |  |  |         for (const noteId of clipboardIds) { | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             await cloningService.cloneNoteAfter(noteId, node.data.branchId); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (clipboardIds.length === 0) { | 
					
						
							|  |  |  |         // just do nothing
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |         infoService.throwError("Unrecognized clipboard mode=" + clipboardMode); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function pasteInto(node) { | 
					
						
							|  |  |  |     if (clipboardMode === 'cut') { | 
					
						
							|  |  |  |         const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey)); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |         await treeChangesService.moveToNode(nodes, node); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-18 22:33:19 +01:00
										 |  |  |         await node.setExpanded(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         clipboardIds = []; | 
					
						
							|  |  |  |         clipboardMode = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (clipboardMode === 'copy') { | 
					
						
							|  |  |  |         for (const noteId of clipboardIds) { | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             await cloningService.cloneNoteTo(noteId, node.data.noteId); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-18 22:33:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         await node.setExpanded(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         // copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (clipboardIds.length === 0) { | 
					
						
							|  |  |  |         // just do nothing
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |         infoService.throwError("Unrecognized clipboard mode=" + mode); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function copy(nodes) { | 
					
						
							|  |  |  |     clipboardIds = nodes.map(node => node.data.noteId); | 
					
						
							|  |  |  |     clipboardMode = 'copy'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |     infoService.showMessage("Note(s) have been copied into clipboard."); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function cut(nodes) { | 
					
						
							|  |  |  |     clipboardIds = nodes.map(node => node.key); | 
					
						
							|  |  |  |     clipboardMode = 'cut'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |     infoService.showMessage("Note(s) have been cut into clipboard."); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  | function 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" } | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  | async function getTopLevelItems(event) { | 
					
						
							|  |  |  |     const node = $.ui.fancytree.getNode(event); | 
					
						
							|  |  |  |     const branch = await treeCache.getBranch(node.data.branchId); | 
					
						
							|  |  |  |     const note = await treeCache.getNote(node.data.noteId); | 
					
						
							|  |  |  |     const parentNote = await treeCache.getNote(branch.parentNoteId); | 
					
						
							|  |  |  |     const isNotRoot = note.noteId !== 'root'; | 
					
						
							|  |  |  |     const isHoisted = note.noteId === await hoistedNoteService.getHoistedNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNote.type !== 'search'; | 
					
						
							|  |  |  |     const insertChildNoteEnabled = note.type !== 'search'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  |     return [ | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  |         { title: "Insert note after <kbd>Ctrl+O</kbd>", cmd: "insertNoteAfter", uiIcon: "plus", | 
					
						
							|  |  |  |             items: insertNoteAfterEnabled ? getNoteTypeItems("insertNoteAfter") : null, | 
					
						
							|  |  |  |             enabled: insertNoteAfterEnabled }, | 
					
						
							|  |  |  |         { title: "Insert child note <kbd>Ctrl+P</kbd>", cmd: "insertChildNote", uiIcon: "plus", | 
					
						
							|  |  |  |             items: insertChildNoteEnabled ? getNoteTypeItems("insertChildNote") : null, | 
					
						
							|  |  |  |             enabled: insertChildNoteEnabled }, | 
					
						
							|  |  |  |         { title: "Delete <kbd>Delete</kbd>", cmd: "delete", uiIcon: "trash", | 
					
						
							|  |  |  |             enabled: isNotRoot && parentNote.type !== 'search' }, | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  |         { title: "----" }, | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  |         isHoisted ? null : { title: "Hoist note <kbd>Ctrl-H</kbd>", cmd: "hoist", uiIcon: "arrow-up" }, | 
					
						
							|  |  |  |         !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: "pencil", | 
					
						
							|  |  |  |             enabled: isNotRoot && parentNote.type !== 'search'}, | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  |         { title: "----" }, | 
					
						
							|  |  |  |         { title: "Protect subtree", cmd: "protectSubtree", uiIcon: "shield-check" }, | 
					
						
							|  |  |  |         { title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield-close" }, | 
					
						
							|  |  |  |         { title: "----" }, | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  |         { 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", | 
					
						
							|  |  |  |             enabled: clipboardIds.length > 0 && note.type !== 'search' }, | 
					
						
							|  |  |  |         { title: "Paste after", cmd: "pasteAfter", uiIcon: "clipboard", | 
					
						
							|  |  |  |             enabled: clipboardIds.length > 0 && isNotRoot && parentNote.type !== 'search' }, | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  |         { title: "----" }, | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  |         { title: "Export", cmd: "export", uiIcon: "arrow-up-right", | 
					
						
							|  |  |  |             enabled: note.type !== 'search' }, | 
					
						
							|  |  |  |         { title: "Import into note", cmd: "importIntoNote", uiIcon: "arrow-down-left", | 
					
						
							|  |  |  |             enabled: note.type !== 'search' }, | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  |         { title: "----" }, | 
					
						
							|  |  |  |         { title: "Collapse subtree <kbd>Alt+-</kbd>", cmd: "collapseSubtree", uiIcon: "align-justify" }, | 
					
						
							|  |  |  |         { title: "Force note sync", cmd: "forceNoteSync", uiIcon: "refresh" }, | 
					
						
							|  |  |  |         { title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sortAlphabetically", uiIcon: "arrows-v" } | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  |     ].filter(row => row !== null); | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function getContextMenuItems(event) { | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  |     const items = await getTopLevelItems(event); | 
					
						
							| 
									
										
										
										
											2018-12-11 21:53:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     // Activate node on right-click
 | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  |     const node = $.ui.fancytree.getNode(event); | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     node.setActive(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // right click resets selection to just this node
 | 
					
						
							|  |  |  |     // this is important when e.g. you right click on a note while having different note active
 | 
					
						
							|  |  |  |     // and then click on delete - obviously you want to delete only that one right-clicked
 | 
					
						
							|  |  |  |     node.setSelected(true); | 
					
						
							|  |  |  |     treeService.clearSelectedNodes(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-19 22:56:37 +01:00
										 |  |  |     return items; | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function selectContextMenuItem(event, cmd) { | 
					
						
							| 
									
										
										
										
											2018-11-06 12:50:48 +01:00
										 |  |  |     // context menu is always triggered on current node
 | 
					
						
							| 
									
										
										
										
											2019-03-20 22:28:54 +01:00
										 |  |  |     const node = treeService.getActiveNode(); | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  |     if (cmd.startsWith("insertNoteAfter")) { | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |         const parentNoteId = node.data.parentNoteId; | 
					
						
							|  |  |  |         const isProtected = treeUtils.getParentProtectedStatus(node); | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  |         const type = cmd.split("_")[1]; | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |         treeService.createNote(node, parentNoteId, 'after', { | 
					
						
							|  |  |  |             type: type, | 
					
						
							|  |  |  |             isProtected: isProtected | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-17 12:19:23 +01:00
										 |  |  |     else if (cmd.startsWith("insertChildNote")) { | 
					
						
							|  |  |  |         const type = cmd.split("_")[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |         treeService.createNote(node, node.data.noteId, 'into', { type: type }); | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "editBranchPrefix") { | 
					
						
							|  |  |  |         branchPrefixDialog.showDialog(node); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "protectSubtree") { | 
					
						
							|  |  |  |         protectedSessionService.protectSubtree(node.data.noteId, true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "unprotectSubtree") { | 
					
						
							|  |  |  |         protectedSessionService.protectSubtree(node.data.noteId, false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "copy") { | 
					
						
							|  |  |  |         copy(treeService.getSelectedNodes()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "cut") { | 
					
						
							|  |  |  |         cut(treeService.getSelectedNodes()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "pasteAfter") { | 
					
						
							|  |  |  |         pasteAfter(node); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "pasteInto") { | 
					
						
							|  |  |  |         pasteInto(node); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     else if (cmd === "delete") { | 
					
						
							|  |  |  |         treeChangesService.deleteNodes(treeService.getSelectedNodes(true)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |     else if (cmd === "export") { | 
					
						
							|  |  |  |         exportDialog.showDialog("subtree"); | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "importIntoNote") { | 
					
						
							| 
									
										
										
										
											2019-02-10 14:33:13 +01:00
										 |  |  |         importDialog.showDialog(); | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "collapseSubtree") { | 
					
						
							|  |  |  |         treeService.collapseTree(node); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "forceNoteSync") { | 
					
						
							|  |  |  |         syncService.forceNoteSync(node.data.noteId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "sortAlphabetically") { | 
					
						
							|  |  |  |         treeService.sortAlphabetically(node.data.noteId); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-12-11 21:53:56 +01:00
										 |  |  |     else if (cmd === "hoist") { | 
					
						
							|  |  |  |         hoistedNoteService.setHoistedNoteId(node.data.noteId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (cmd === "unhoist") { | 
					
						
							| 
									
										
										
										
											2018-12-15 20:29:08 +01:00
										 |  |  |         hoistedNoteService.unhoist(); | 
					
						
							| 
									
										
										
										
											2018-12-11 21:53:56 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     else { | 
					
						
							|  |  |  |         messagingService.logError("Unknown command: " + cmd); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |     pasteAfter, | 
					
						
							|  |  |  |     pasteInto, | 
					
						
							|  |  |  |     cut, | 
					
						
							|  |  |  |     copy, | 
					
						
							| 
									
										
										
										
											2018-11-06 12:46:29 +01:00
										 |  |  |     getContextMenuItems, | 
					
						
							|  |  |  |     selectContextMenuItem | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }; |