mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	context menu converted to module
This commit is contained in:
		| @@ -1,86 +1,95 @@ | |||||||
| const treeEl = $("#tree"); | const contextMenu = (function() { | ||||||
|  |     const treeEl = $("#tree"); | ||||||
|  |  | ||||||
| function pasteAfter(node) { |     function pasteAfter(node) { | ||||||
|     const subjectNode = getNodeByKey(glob.clipboardNoteId); |         const subjectNode = getNodeByKey(glob.clipboardNoteId); | ||||||
|  |  | ||||||
|     moveAfterNode(subjectNode, node); |         moveAfterNode(subjectNode, node); | ||||||
|  |  | ||||||
|     glob.clipboardNoteId = null; |         glob.clipboardNoteId = null; | ||||||
| } |  | ||||||
|  |  | ||||||
| function pasteInto(node) { |  | ||||||
|     const subjectNode = getNodeByKey(glob.clipboardNoteId); |  | ||||||
|  |  | ||||||
|     moveToNode(subjectNode, node); |  | ||||||
|  |  | ||||||
|     glob.clipboardNoteId = null; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function cut(node) { |  | ||||||
|     glob.clipboardNoteId = node.key; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| const contextMenuSetup = { |  | ||||||
|     delegate: "span.fancytree-title", |  | ||||||
|     autoFocus: true, |  | ||||||
|     menu: [ |  | ||||||
|         {title: "Insert note here", cmd: "insertNoteHere", uiIcon: "ui-icon-pencil"}, |  | ||||||
|         {title: "Insert child note", cmd: "insertChildNote", uiIcon: "ui-icon-pencil"}, |  | ||||||
|         {title: "Delete", cmd: "delete", uiIcon: "ui-icon-trash"}, |  | ||||||
|         {title: "----"}, |  | ||||||
|         {title: "Encrypt sub-tree", cmd: "encryptSubTree", uiIcon: "ui-icon-locked"}, |  | ||||||
|         {title: "Decrypt sub-tree", cmd: "decryptSubTree", uiIcon: "ui-icon-unlocked"}, |  | ||||||
|         {title: "----"}, |  | ||||||
|         {title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors"}, |  | ||||||
|         {title: "Copy / clone", cmd: "copy", uiIcon: "ui-icon-copy"}, |  | ||||||
|         {title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"}, |  | ||||||
|         {title: "Paste into", cmd: "pasteInto", uiIcon: "ui-icon-clipboard"} |  | ||||||
|     ], |  | ||||||
|     beforeOpen: (event, ui) => { |  | ||||||
|         const node = $.ui.fancytree.getNode(ui.target); |  | ||||||
|         // Modify menu entries depending on node status |  | ||||||
|         treeEl.contextmenu("enableEntry", "pasteAfter", glob.clipboardNoteId !== null); |  | ||||||
|         treeEl.contextmenu("enableEntry", "pasteInto", glob.clipboardNoteId !== null); |  | ||||||
|  |  | ||||||
|         // Activate node on right-click |  | ||||||
|         node.setActive(); |  | ||||||
|         // Disable tree keyboard handling |  | ||||||
|         ui.menu.prevKeyboard = node.tree.options.keyboard; |  | ||||||
|         node.tree.options.keyboard = false; |  | ||||||
|     }, |  | ||||||
|     close: (event, ui) => {}, |  | ||||||
|     select: (event, ui) => { |  | ||||||
|         const node = $.ui.fancytree.getNode(ui.target); |  | ||||||
|  |  | ||||||
|         if (ui.cmd === "insertNoteHere") { |  | ||||||
|             const parentKey = getParentKey(node); |  | ||||||
|             const encryption = getParentEncryption(node); |  | ||||||
|  |  | ||||||
|             noteEditor.createNote(node, parentKey, 'after', encryption); |  | ||||||
|         } |  | ||||||
|         else if (ui.cmd === "insertChildNote") { |  | ||||||
|             noteEditor.createNote(node, node.key, 'into'); |  | ||||||
|         } |  | ||||||
|         else if (ui.cmd === "encryptSubTree") { |  | ||||||
|             encryption.encryptSubTree(node.key); |  | ||||||
|         } |  | ||||||
|         else if (ui.cmd === "decryptSubTree") { |  | ||||||
|             encryption.decryptSubTree(node.key); |  | ||||||
|         } |  | ||||||
|         else if (ui.cmd === "cut") { |  | ||||||
|             cut(node); |  | ||||||
|         } |  | ||||||
|         else if (ui.cmd === "pasteAfter") { |  | ||||||
|             pasteAfter(node); |  | ||||||
|         } |  | ||||||
|         else if (ui.cmd === "pasteInto") { |  | ||||||
|             pasteInto(node); |  | ||||||
|         } |  | ||||||
|         else if (ui.cmd === "delete") { |  | ||||||
|             deleteNode(node); |  | ||||||
|         } |  | ||||||
|         else { |  | ||||||
|             console.log("Unknown command: " + ui.cmd); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| }; |  | ||||||
|  |     function pasteInto(node) { | ||||||
|  |         const subjectNode = getNodeByKey(glob.clipboardNoteId); | ||||||
|  |  | ||||||
|  |         moveToNode(subjectNode, node); | ||||||
|  |  | ||||||
|  |         glob.clipboardNoteId = null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     function cut(node) { | ||||||
|  |         glob.clipboardNoteId = node.key; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     const contextMenuSettings = { | ||||||
|  |         delegate: "span.fancytree-title", | ||||||
|  |         autoFocus: true, | ||||||
|  |         menu: [ | ||||||
|  |             {title: "Insert note here", cmd: "insertNoteHere", uiIcon: "ui-icon-pencil"}, | ||||||
|  |             {title: "Insert child note", cmd: "insertChildNote", uiIcon: "ui-icon-pencil"}, | ||||||
|  |             {title: "Delete", cmd: "delete", uiIcon: "ui-icon-trash"}, | ||||||
|  |             {title: "----"}, | ||||||
|  |             {title: "Encrypt sub-tree", cmd: "encryptSubTree", uiIcon: "ui-icon-locked"}, | ||||||
|  |             {title: "Decrypt sub-tree", cmd: "decryptSubTree", uiIcon: "ui-icon-unlocked"}, | ||||||
|  |             {title: "----"}, | ||||||
|  |             {title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors"}, | ||||||
|  |             {title: "Copy / clone", cmd: "copy", uiIcon: "ui-icon-copy"}, | ||||||
|  |             {title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"}, | ||||||
|  |             {title: "Paste into", cmd: "pasteInto", uiIcon: "ui-icon-clipboard"} | ||||||
|  |         ], | ||||||
|  |         beforeOpen: (event, ui) => { | ||||||
|  |             const node = $.ui.fancytree.getNode(ui.target); | ||||||
|  |             // Modify menu entries depending on node status | ||||||
|  |             treeEl.contextmenu("enableEntry", "pasteAfter", glob.clipboardNoteId !== null); | ||||||
|  |             treeEl.contextmenu("enableEntry", "pasteInto", glob.clipboardNoteId !== null); | ||||||
|  |  | ||||||
|  |             // Activate node on right-click | ||||||
|  |             node.setActive(); | ||||||
|  |             // Disable tree keyboard handling | ||||||
|  |             ui.menu.prevKeyboard = node.tree.options.keyboard; | ||||||
|  |             node.tree.options.keyboard = false; | ||||||
|  |         }, | ||||||
|  |         close: (event, ui) => {}, | ||||||
|  |         select: (event, ui) => { | ||||||
|  |             const node = $.ui.fancytree.getNode(ui.target); | ||||||
|  |  | ||||||
|  |             if (ui.cmd === "insertNoteHere") { | ||||||
|  |                 const parentKey = getParentKey(node); | ||||||
|  |                 const encryption = getParentEncryption(node); | ||||||
|  |  | ||||||
|  |                 noteEditor.createNote(node, parentKey, 'after', encryption); | ||||||
|  |             } | ||||||
|  |             else if (ui.cmd === "insertChildNote") { | ||||||
|  |                 noteEditor.createNote(node, node.key, 'into'); | ||||||
|  |             } | ||||||
|  |             else if (ui.cmd === "encryptSubTree") { | ||||||
|  |                 encryption.encryptSubTree(node.key); | ||||||
|  |             } | ||||||
|  |             else if (ui.cmd === "decryptSubTree") { | ||||||
|  |                 encryption.decryptSubTree(node.key); | ||||||
|  |             } | ||||||
|  |             else if (ui.cmd === "cut") { | ||||||
|  |                 cut(node); | ||||||
|  |             } | ||||||
|  |             else if (ui.cmd === "pasteAfter") { | ||||||
|  |                 pasteAfter(node); | ||||||
|  |             } | ||||||
|  |             else if (ui.cmd === "pasteInto") { | ||||||
|  |                 pasteInto(node); | ||||||
|  |             } | ||||||
|  |             else if (ui.cmd === "delete") { | ||||||
|  |                 deleteNode(node); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 console.log("Unknown command: " + ui.cmd); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     return { | ||||||
|  |         pasteAfter, | ||||||
|  |         pasteInto, | ||||||
|  |         cut, | ||||||
|  |         contextMenuSettings | ||||||
|  |     } | ||||||
|  | })(); | ||||||
| @@ -161,7 +161,7 @@ const noteTree = (function() { | |||||||
|                         console.log("CTRL-V"); |                         console.log("CTRL-V"); | ||||||
|  |  | ||||||
|                         if (event.ctrlKey) { // Ctrl-V |                         if (event.ctrlKey) { // Ctrl-V | ||||||
|                             pasteAfter(node); |                             contextMenu.pasteAfter(node); | ||||||
|                             return false; |                             return false; | ||||||
|                         } |                         } | ||||||
|                         break; |                         break; | ||||||
| @@ -169,7 +169,7 @@ const noteTree = (function() { | |||||||
|                         console.log("CTRL-X"); |                         console.log("CTRL-X"); | ||||||
|  |  | ||||||
|                         if (event.ctrlKey) { // Ctrl-X |                         if (event.ctrlKey) { // Ctrl-X | ||||||
|                             cut(node); |                             contextMenu.cut(node); | ||||||
|                             return false; |                             return false; | ||||||
|                         } |                         } | ||||||
|                         break; |                         break; | ||||||
| @@ -177,7 +177,7 @@ const noteTree = (function() { | |||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         treeEl.contextmenu(contextMenuSetup); |         treeEl.contextmenu(contextMenu.contextMenuSettings); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     function loadTree() { |     function loadTree() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user