mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	refactoring/cleanup
This commit is contained in:
		| @@ -257,12 +257,6 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null, tabConte | |||||||
|      */ |      */ | ||||||
|     this.createNoteLink = linkService.createNoteLink; |     this.createNoteLink = linkService.createNoteLink; | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * @method |  | ||||||
|      * @returns {string} content of active note (loaded into right pane) |  | ||||||
|      */ |  | ||||||
|     this.getActiveNoteContent = noteDetailService.getActiveNoteContent; |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @method |      * @method | ||||||
|      * @returns {NoteFull} active note (loaded into right pane) |      * @returns {NoteFull} active note (loaded into right pane) | ||||||
|   | |||||||
| @@ -17,24 +17,6 @@ const $savedIndicator = $(".saved-indicator"); | |||||||
|  |  | ||||||
| let detailLoadedListeners = []; | let detailLoadedListeners = []; | ||||||
|  |  | ||||||
| /** @return {NoteFull} */ |  | ||||||
| function getActiveNote() { |  | ||||||
|     const activeContext = getActiveTabContext(); |  | ||||||
|     return activeContext ? activeContext.note : null; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function getActiveNoteId() { |  | ||||||
|     const activeNote = getActiveNote(); |  | ||||||
|  |  | ||||||
|     return activeNote ? activeNote.noteId : null; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function getActiveNoteType() { |  | ||||||
|     const activeNote = getActiveNote(); |  | ||||||
|  |  | ||||||
|     return activeNote ? activeNote.type : null; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function reload() { | async function reload() { | ||||||
|     // no saving here |     // no saving here | ||||||
|  |  | ||||||
| @@ -67,10 +49,6 @@ async function switchToNote(notePath) { | |||||||
|     openTabsChanged(); |     openTabsChanged(); | ||||||
| } | } | ||||||
|  |  | ||||||
| function getActiveNoteContent() { |  | ||||||
|     return getActiveTabContext().getComponent().getContent(); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function onNoteChange(func) { | function onNoteChange(func) { | ||||||
|     return getActiveTabContext().getComponent().onNoteChange(func); |     return getActiveTabContext().getComponent().onNoteChange(func); | ||||||
| } | } | ||||||
| @@ -115,17 +93,40 @@ function getActiveTabContext() { | |||||||
|     return tabContexts.find(tc => tc.tabId === tabId); |     return tabContexts.find(tc => tc.tabId === tabId); | ||||||
| } | } | ||||||
|  |  | ||||||
| function isActive(tabContext) { | /** @return {NoteFull} */ | ||||||
|     return tabContext.$tab[0] === tabRow.activateTab; | function getActiveNote() { | ||||||
|  |     const activeContext = getActiveTabContext(); | ||||||
|  |     return activeContext ? activeContext.note : null; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function activateTabContext(tabContext) { | function getActiveNoteId() { | ||||||
|     await tabRow.activateTab(tabContext.$tab[0]); |     const activeNote = getActiveNote(); | ||||||
|  |  | ||||||
|  |     return activeNote ? activeNote.noteId : null; | ||||||
| } | } | ||||||
|  |  | ||||||
| /** @returns {TabContext} */ | function getActiveNoteType() { | ||||||
| function getTabContext(tabId) { |     const activeNote = getActiveNote(); | ||||||
|     return tabContexts.find(tc => tc.tabId === tabId); |  | ||||||
|  |     return activeNote ? activeNote.type : null; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function switchToTab(tabId, notePath) { | ||||||
|  |     const tabContext = tabContexts.find(tc => tc.tabId === tabId); | ||||||
|  |  | ||||||
|  |     if (!tabContext) { | ||||||
|  |         await loadNoteDetail(notePath, { | ||||||
|  |             newTab: true, | ||||||
|  |             tabId: tabId, | ||||||
|  |             activate: true | ||||||
|  |         }); | ||||||
|  |     } else { | ||||||
|  |         await tabContext.activate(); | ||||||
|  |  | ||||||
|  |         if (notePath && tabContext.notePath !== notePath) { | ||||||
|  |             await loadNoteDetail(notePath); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| async function showTab(tabId) { | async function showTab(tabId) { | ||||||
| @@ -348,7 +349,13 @@ $tabContentsContainer.on("dragover", e => e.preventDefault()); | |||||||
| $tabContentsContainer.on("dragleave", e => e.preventDefault()); | $tabContentsContainer.on("dragleave", e => e.preventDefault()); | ||||||
|  |  | ||||||
| $tabContentsContainer.on("drop", e => { | $tabContentsContainer.on("drop", e => { | ||||||
|     importDialog.uploadFiles(getActiveNoteId(), e.originalEvent.dataTransfer.files, { |     const activeNote = getActiveNote(); | ||||||
|  |  | ||||||
|  |     if (!activeNote) { | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     importDialog.uploadFiles(activeNote.noteId, e.originalEvent.dataTransfer.files, { | ||||||
|         safeImport: true, |         safeImport: true, | ||||||
|         shrinkImages: true, |         shrinkImages: true, | ||||||
|         textImportedAsText: true, |         textImportedAsText: true, | ||||||
| @@ -502,7 +509,6 @@ export default { | |||||||
|     loadNote, |     loadNote, | ||||||
|     loadNoteDetail, |     loadNoteDetail, | ||||||
|     getActiveNote, |     getActiveNote, | ||||||
|     getActiveNoteContent, |  | ||||||
|     getActiveNoteType, |     getActiveNoteType, | ||||||
|     getActiveNoteId, |     getActiveNoteId, | ||||||
|     focusOnTitle, |     focusOnTitle, | ||||||
| @@ -510,12 +516,10 @@ export default { | |||||||
|     saveNotesIfChanged, |     saveNotesIfChanged, | ||||||
|     onNoteChange, |     onNoteChange, | ||||||
|     addDetailLoadedListener, |     addDetailLoadedListener, | ||||||
|     getTabContext, |     switchToTab, | ||||||
|     getTabContexts, |     getTabContexts, | ||||||
|     getActiveTabContext, |     getActiveTabContext, | ||||||
|     getActiveEditor, |     getActiveEditor, | ||||||
|     isActive, |  | ||||||
|     activateTabContext, |  | ||||||
|     clearOpenTabsTask, |     clearOpenTabsTask, | ||||||
|     filterTabs, |     filterTabs, | ||||||
|     openEmptyTab, |     openEmptyTab, | ||||||
|   | |||||||
| @@ -65,7 +65,7 @@ class NoteDetailImage { | |||||||
|  |  | ||||||
|     getFileUrl() { |     getFileUrl() { | ||||||
|         // electron needs absolute URL so we extract current host, port, protocol |         // electron needs absolute URL so we extract current host, port, protocol | ||||||
|         return utils.getHost() + "/api/notes/" + noteDetailService.getActiveNoteId() + "/download"; |         return utils.getHost() + `/api/notes/${this.ctx.note.noteId}/download`; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     getContent() {} |     getContent() {} | ||||||
|   | |||||||
| @@ -114,7 +114,7 @@ function NoteTypeContext(ctx) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     function confirmChangeIfContent() { |     function confirmChangeIfContent() { | ||||||
|         if (!noteDetailService.getActiveNoteContent()) { |         if (!self.ctx.getComponent().getContent()) { | ||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -70,7 +70,7 @@ async function enterProtectedSessionOnServer(password) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function protectNoteAndSendToServer() { | async function protectNoteAndSendToServer() { | ||||||
|     if (noteDetailService.getActiveNote().isProtected) { |     if (!noteDetailService.getActiveNote() || noteDetailService.getActiveNote().isProtected) { | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -192,13 +192,17 @@ class TabContext { | |||||||
|         return this.components[type]; |         return this.components[type]; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     async activate() { | ||||||
|  |         await this.tabRow.activateTab(this.$tab[0]); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     async saveNote() { |     async saveNote() { | ||||||
|         if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) { |         if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         this.note.title = this.$noteTitle.val(); |         this.note.title = this.$noteTitle.val(); | ||||||
|         this.note.content = noteDetailService.getActiveNoteContent(); |         this.note.content = this.getComponent().getContent(); | ||||||
|  |  | ||||||
|         // it's important to set the flag back to false immediatelly after retrieving title and content |         // it's important to set the flag back to false immediatelly after retrieving title and content | ||||||
|         // otherwise we might overwrite another change (especially async code) |         // otherwise we might overwrite another change (especially async code) | ||||||
|   | |||||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -791,24 +791,7 @@ $(window).bind('hashchange', async function() { | |||||||
|  |  | ||||||
|         console.debug(`Switching to ${notePath} on tab ${tabId} because of hash change`); |         console.debug(`Switching to ${notePath} on tab ${tabId} because of hash change`); | ||||||
|  |  | ||||||
|         let tabContext = noteDetailService.getTabContext(tabId); |         noteDetailService.switchToTab(tabId, notePath); | ||||||
|  |  | ||||||
|         if (!tabContext) { |  | ||||||
|             noteDetailService.loadNoteDetail(notePath, { |  | ||||||
|                 newTab: true, |  | ||||||
|                 tabId: tabId, |  | ||||||
|                 activate: true |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|         else { |  | ||||||
|             if (!noteDetailService.isActive(tabContext)) { |  | ||||||
|                 noteDetailService.activateTabContext(tabContext); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             if (notePath && tabContext.notePath !== notePath) { |  | ||||||
|                 noteDetailService.loadNoteDetail(notePath); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| }); | }); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user