mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 03:29:02 +01:00 
			
		
		
		
	tree utils as a module
This commit is contained in:
		
							parent
							
								
									900f169dab
								
							
						
					
					
						commit
						30df3cba1c
					
				| @ -4,7 +4,7 @@ const contextMenu = (function() { | |||||||
|     const treeEl = $("#tree"); |     const treeEl = $("#tree"); | ||||||
| 
 | 
 | ||||||
|     function pasteAfter(node) { |     function pasteAfter(node) { | ||||||
|         const subjectNode = getNodeByKey(noteTree.getClipboardNoteId()); |         const subjectNode = treeUtils.getNodeByKey(noteTree.getClipboardNoteId()); | ||||||
| 
 | 
 | ||||||
|         treeChanges.moveAfterNode(subjectNode, node); |         treeChanges.moveAfterNode(subjectNode, node); | ||||||
| 
 | 
 | ||||||
| @ -12,7 +12,7 @@ const contextMenu = (function() { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     function pasteInto(node) { |     function pasteInto(node) { | ||||||
|         const subjectNode = getNodeByKey(noteTree.getClipboardNoteId()); |         const subjectNode = treeUtils.getNodeByKey(noteTree.getClipboardNoteId()); | ||||||
| 
 | 
 | ||||||
|         treeChanges.moveToNode(subjectNode, node); |         treeChanges.moveToNode(subjectNode, node); | ||||||
| 
 | 
 | ||||||
| @ -56,8 +56,8 @@ const contextMenu = (function() { | |||||||
|             const node = $.ui.fancytree.getNode(ui.target); |             const node = $.ui.fancytree.getNode(ui.target); | ||||||
| 
 | 
 | ||||||
|             if (ui.cmd === "insertNoteHere") { |             if (ui.cmd === "insertNoteHere") { | ||||||
|                 const parentKey = getParentKey(node); |                 const parentKey = treeUtils.getParentKey(node); | ||||||
|                 const encryption = getParentEncryption(node); |                 const encryption = treeUtils.getParentEncryption(node); | ||||||
| 
 | 
 | ||||||
|                 noteEditor.createNote(node, parentKey, 'after', encryption); |                 noteEditor.createNote(node, parentKey, 'after', encryption); | ||||||
|             } |             } | ||||||
|  | |||||||
| @ -21,7 +21,7 @@ const addLink = (function() { | |||||||
|         linkTitleEl.val(''); |         linkTitleEl.val(''); | ||||||
| 
 | 
 | ||||||
|         function setDefaultLinkTitle(noteId) { |         function setDefaultLinkTitle(noteId) { | ||||||
|             const noteTitle = getNoteTitle(noteId); |             const noteTitle = treeUtils.getNoteTitle(noteId); | ||||||
| 
 | 
 | ||||||
|             linkTitleEl.val(noteTitle); |             linkTitleEl.val(noteTitle); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -28,7 +28,7 @@ const jumpToNote = (function() { | |||||||
|         const noteId = link.getNodeIdFromLabel(val); |         const noteId = link.getNodeIdFromLabel(val); | ||||||
| 
 | 
 | ||||||
|         if (noteId) { |         if (noteId) { | ||||||
|             getNodeByKey(noteId).setActive(); |             treeUtils.activateNode(noteId); | ||||||
| 
 | 
 | ||||||
|             dialogEl.dialog('close'); |             dialogEl.dialog('close'); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -40,7 +40,7 @@ const recentNotes = (function() { | |||||||
|         const recNotes = list.filter(note => note !== noteEditor.getCurrentNoteId()); |         const recNotes = list.filter(note => note !== noteEditor.getCurrentNoteId()); | ||||||
| 
 | 
 | ||||||
|         $.each(recNotes, (key, valueNoteId) => { |         $.each(recNotes, (key, valueNoteId) => { | ||||||
|             const noteTitle = getFullName(valueNoteId); |             const noteTitle = treeUtils.getFullName(valueNoteId); | ||||||
| 
 | 
 | ||||||
|             if (!noteTitle) { |             if (!noteTitle) { | ||||||
|                 return; |                 return; | ||||||
| @ -66,7 +66,7 @@ const recentNotes = (function() { | |||||||
|     function setActiveNoteBasedOnRecentNotes() { |     function setActiveNoteBasedOnRecentNotes() { | ||||||
|         const noteId = getSelectedNoteIdFromRecentNotes(); |         const noteId = getSelectedNoteIdFromRecentNotes(); | ||||||
| 
 | 
 | ||||||
|         getNodeByKey(noteId).setActive(); |         treeUtils.activateNode(noteId); | ||||||
| 
 | 
 | ||||||
|         dialogEl.dialog('close'); |         dialogEl.dialog('close'); | ||||||
|     } |     } | ||||||
| @ -74,7 +74,7 @@ const recentNotes = (function() { | |||||||
|     function addLinkBasedOnRecentNotes() { |     function addLinkBasedOnRecentNotes() { | ||||||
|         const noteId = getSelectedNoteIdFromRecentNotes(); |         const noteId = getSelectedNoteIdFromRecentNotes(); | ||||||
| 
 | 
 | ||||||
|         const linkTitle = getNoteTitle(noteId); |         const linkTitle = treeUtils.getNoteTitle(noteId); | ||||||
| 
 | 
 | ||||||
|         dialogEl.dialog("close"); |         dialogEl.dialog("close"); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -42,7 +42,7 @@ const encryption = (function() { | |||||||
|                 open: () => { |                 open: () => { | ||||||
|                     if (!modal) { |                     if (!modal) { | ||||||
|                         // dialog steals focus for itself, which is not what we want for non-modal (viewing)
 |                         // dialog steals focus for itself, which is not what we want for non-modal (viewing)
 | ||||||
|                         getNodeByKey(noteEditor.getCurrentNoteId()).setFocus(); |                         treeUtils.getNodeByKey(noteEditor.getCurrentNoteId()).setFocus(); | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
| @ -105,7 +105,7 @@ const encryption = (function() { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         for (const noteId of glob.allNoteIds) { |         for (const noteId of glob.allNoteIds) { | ||||||
|             const note = getNodeByKey(noteId); |             const note = treeUtils.getNodeByKey(noteId); | ||||||
| 
 | 
 | ||||||
|             if (note.data.encryption > 0) { |             if (note.data.encryption > 0) { | ||||||
|                 const title = decryptString(note.data.note_title); |                 const title = decryptString(note.data.note_title); | ||||||
| @ -375,7 +375,7 @@ const encryption = (function() { | |||||||
|     function updateSubTreeRecursively(noteId, updateCallback, successCallback) { |     function updateSubTreeRecursively(noteId, updateCallback, successCallback) { | ||||||
|         updateNoteSynchronously(noteId, updateCallback, successCallback); |         updateNoteSynchronously(noteId, updateCallback, successCallback); | ||||||
| 
 | 
 | ||||||
|         const node = getNodeByKey(noteId); |         const node = treeUtils.getNodeByKey(noteId); | ||||||
|         if (!node || !node.getChildren()) { |         if (!node || !node.getChildren()) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -25,7 +25,7 @@ const link = (function() { | |||||||
|     function createNoteLink(noteId) { |     function createNoteLink(noteId) { | ||||||
|         const noteLink = $("<a>", { |         const noteLink = $("<a>", { | ||||||
|             href: 'javascript:', |             href: 'javascript:', | ||||||
|             text: getFullName(noteId) |             text: treeUtils.getFullName(noteId) | ||||||
|         }).attr('action', 'note') |         }).attr('action', 'note') | ||||||
|             .attr('note-id', noteId); |             .attr('note-id', noteId); | ||||||
| 
 | 
 | ||||||
| @ -41,7 +41,7 @@ const link = (function() { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (noteId) { |         if (noteId) { | ||||||
|             getNodeByKey(noteId).setActive(); |             treeUtils.activateNode(noteId); | ||||||
| 
 | 
 | ||||||
|             // this is quite ugly hack, but it seems like we can't close the tooltip otherwise
 |             // this is quite ugly hack, but it seems like we can't close the tooltip otherwise
 | ||||||
|             $("[role='tooltip']").remove(); |             $("[role='tooltip']").remove(); | ||||||
|  | |||||||
| @ -78,7 +78,7 @@ const noteEditor = (function() { | |||||||
| 
 | 
 | ||||||
|         const title = noteTitleEl.val(); |         const title = noteTitleEl.val(); | ||||||
| 
 | 
 | ||||||
|         getNodeByKey(note.detail.note_id).setTitle(title); |         treeUtils.getNodeByKey(note.detail.note_id).setTitle(title); | ||||||
| 
 | 
 | ||||||
|         note.detail.note_title = title; |         note.detail.note_title = title; | ||||||
|     } |     } | ||||||
| @ -155,7 +155,7 @@ const noteEditor = (function() { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     function setTreeBasedOnEncryption(note) { |     function setTreeBasedOnEncryption(note) { | ||||||
|         const node = getNodeByKey(note.detail.note_id); |         const node = treeUtils.getNodeByKey(note.detail.note_id); | ||||||
|         node.toggleClass("encrypted", note.detail.encryption > 0); |         node.toggleClass("encrypted", note.detail.encryption > 0); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -58,8 +58,8 @@ const noteTree = (function() { | |||||||
|     function initFancyTree(notes, startNoteId) { |     function initFancyTree(notes, startNoteId) { | ||||||
|         const keybindings = { |         const keybindings = { | ||||||
|             "insert": node => { |             "insert": node => { | ||||||
|                 const parentKey = getParentKey(node); |                 const parentKey = treeUtils.getParentKey(node); | ||||||
|                 const encryption = getParentEncryption(node); |                 const encryption = treeUtils.getParentEncryption(node); | ||||||
| 
 | 
 | ||||||
|                 noteEditor.createNote(node, parentKey, 'after', encryption); |                 noteEditor.createNote(node, parentKey, 'after', encryption); | ||||||
|             }, |             }, | ||||||
| @ -227,7 +227,7 @@ const noteTree = (function() { | |||||||
|     $(document).bind('keydown', 'alt+c', collapseTree); |     $(document).bind('keydown', 'alt+c', collapseTree); | ||||||
| 
 | 
 | ||||||
|     function scrollToCurrentNote() { |     function scrollToCurrentNote() { | ||||||
|         const node = getNodeByKey(noteEditor.getCurrentNoteId()); |         const node = treeUtils.getNodeByKey(noteEditor.getCurrentNoteId()); | ||||||
| 
 | 
 | ||||||
|         if (node) { |         if (node) { | ||||||
|             node.makeVisible({scrollIntoView: true}); |             node.makeVisible({scrollIntoView: true}); | ||||||
|  | |||||||
| @ -1,21 +1,28 @@ | |||||||
| "use strict"; | "use strict"; | ||||||
| 
 | 
 | ||||||
| const treeEl = $("#tree"); | const treeUtils = (function() { | ||||||
|  |     const treeEl = $("#tree"); | ||||||
| 
 | 
 | ||||||
| function getParentKey(node) { |     function getParentKey(node) { | ||||||
|         return (node.getParent() === null || node.getParent().key === "root_1") ? "root" : node.getParent().key; |         return (node.getParent() === null || node.getParent().key === "root_1") ? "root" : node.getParent().key; | ||||||
| } |     } | ||||||
| 
 | 
 | ||||||
| function getParentEncryption(node) { |     function getParentEncryption(node) { | ||||||
|         return node.getParent() === null ? 0 : node.getParent().data.encryption; |         return node.getParent() === null ? 0 : node.getParent().data.encryption; | ||||||
| } |     } | ||||||
| 
 | 
 | ||||||
| function getNodeByKey(noteId) { |     function getNodeByKey(noteId) { | ||||||
|         return treeEl.fancytree('getNodeByKey', noteId); |         return treeEl.fancytree('getNodeByKey', noteId); | ||||||
| } |     } | ||||||
| 
 | 
 | ||||||
| function getNoteTitle(noteId) { |     function activateNode(noteId) { | ||||||
|     const note = getNodeByKey(noteId); |         const node = treeUtils.getNodeByKey(noteId); | ||||||
|  | 
 | ||||||
|  |         node.setActive(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     function getNoteTitle(noteId) { | ||||||
|  |         const note = treeUtils.getNodeByKey(noteId); | ||||||
|         if (!note) { |         if (!note) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| @ -27,10 +34,10 @@ function getNoteTitle(noteId) { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return noteTitle; |         return noteTitle; | ||||||
| } |     } | ||||||
| 
 | 
 | ||||||
| function getFullName(noteId) { |     function getFullName(noteId) { | ||||||
|     let note = getNodeByKey(noteId); |         let note = treeUtils.getNodeByKey(noteId); | ||||||
| 
 | 
 | ||||||
|         if (note === null) { |         if (note === null) { | ||||||
|             return "[unknown]"; |             return "[unknown]"; | ||||||
| @ -58,4 +65,14 @@ function getFullName(noteId) { | |||||||
|         path.pop(); |         path.pop(); | ||||||
| 
 | 
 | ||||||
|         return path.reverse().join(" > "); |         return path.reverse().join(" > "); | ||||||
| } |     } | ||||||
|  | 
 | ||||||
|  |     return { | ||||||
|  |         getParentKey, | ||||||
|  |         getParentEncryption, | ||||||
|  |         getNodeByKey, | ||||||
|  |         activateNode, | ||||||
|  |         getNoteTitle, | ||||||
|  |         getFullName | ||||||
|  |     }; | ||||||
|  | })(); | ||||||
| @ -20,7 +20,7 @@ function getAutocompleteItems(noteIds) { | |||||||
|     const autocompleteItems = []; |     const autocompleteItems = []; | ||||||
| 
 | 
 | ||||||
|     for (const noteId of noteIds) { |     for (const noteId of noteIds) { | ||||||
|         const fullName = getFullName(noteId); |         const fullName = treeUtils.getFullName(noteId); | ||||||
| 
 | 
 | ||||||
|         if (fullName !== null) { |         if (fullName !== null) { | ||||||
|             autocompleteItems.push({ |             autocompleteItems.push({ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner