Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
created concept of "detail loaded listeners" which allow scripts to execute some action after the note detail has been loaded
azivner3ab657fe4663
diff --git a/src/public/javascripts/services/frontend_script_api.js b/src/public/javascripts/services/frontend_script_api.jsindex 47159d6d93..95e3e1a6f0 100644--- a/src/public/javascripts/services/frontend_script_api.js+++ b/src/public/javascripts/services/frontend_script_api.js@@ -43,7 +43,7 @@ function FrontendScriptApi(startNote, currentNote, originEntity = null) {this.activateNewNote = async notePath => {await treeService.reload();- await treeService.activateNote(notePath, true);+ await treeService.activateNote(notePath, noteDetailService.focusOnTitle);};/**diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.jsindex ec275e0f32..1556dec7c1 100644--- a/src/public/javascripts/services/note_detail.js+++ b/src/public/javascripts/services/note_detail.js@@ -37,6 +37,8 @@ let noteChangeDisabled = false;let isNoteChanged = false;+let detailLoadedListeners = [];+const components = {'code': noteDetailCode,'text': noteDetailText,@@ -147,12 +149,6 @@ function setNoteBackgroundIfProtected(note) {$unprotectButton.prop("disabled", !protectedSessionHolder.isProtectedSessionAvailable());}-let isNewNoteCreated = false;--function newNoteCreated() {- isNewNoteCreated = true;-}-async function handleProtectedSession() {const newSessionCreated = await protectedSessionService.ensureProtectedSession(currentNote.isProtected, false);@@ -191,12 +187,6 @@ async function loadNoteDetail(noteId) {attributeService.invalidateAttributes();}- if (isNewNoteCreated) {- isNewNoteCreated = false;-- $noteTitle.focus().select();- }-$noteIdDisplay.html(noteId);setNoteBackgroundIfProtected(currentNote);@@ -240,11 +230,13 @@ async function loadNoteDetail(noteId) {// after loading new note make sure editor is scrolled to the topgetComponent(currentNote.type).scrollToTop();- if (utils.isDesktop()) {- $scriptArea.empty();+ fireDetailLoaded();++ $scriptArea.empty();- await bundleService.executeRelationBundles(getCurrentNote(), 'runOnNoteView');+ await bundleService.executeRelationBundles(getCurrentNote(), 'runOnNoteView');+ if (utils.isDesktop()) {await attributeService.showAttributes();await showChildrenOverview();@@ -291,6 +283,30 @@ function focusOnTitle() {$noteTitle.focus();}+/**+ * Since detail loading may take some time and user might just browse through the notes using UP-DOWN keys,+ * we intentionally decouple activation of the note in the tree and full load of the note so just avaiting on+ * fancytree's activate() won't wait for the full load.+ *+ * This causes an issue where in some cases you want to do some action after detail is loaded. For this reason+ * we provide the listeners here which will be triggered after the detail is loaded and if the loaded note+ * is the one registered in the listener.+ */+function addDetailLoadedListener(noteId, callback) {+ detailLoadedListeners.push({ noteId, callback });+}++function fireDetailLoaded() {+ for (const {noteId, callback} of detailLoadedListeners) {+ if (noteId === currentNote.noteId) {+ callback();+ }+ }++ // all the listeners are one time only+ detailLoadedListeners = [];+}+messagingService.subscribeToSyncMessages(syncData => {if (syncData.some(sync => sync.entityName === 'notes' && sync.entityId === getCurrentNoteId())) {infoService.showMessage('Reloading note because of background changes');@@ -325,11 +341,11 @@ export default {getCurrentNote,getCurrentNoteType,getCurrentNoteId,- newNoteCreated,focusOnTitle,saveNote,saveNoteIfChanged,noteChanged,getCurrentNoteContent,- onNoteChange+ onNoteChange,+ addDetailLoadedListener};\ No newline at end of filediff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.jsindex fe83021d2b..e52dd822ab 100644--- a/src/public/javascripts/services/tree.js+++ b/src/public/javascripts/services/tree.js@@ -126,7 +126,7 @@ async function expandToNote(notePath, expandOpts) {}}-async function activateNote(notePath, newNote) {+async function activateNote(notePath, noteLoadedListener) {utils.assertArguments(notePath);const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();@@ -146,8 +146,8 @@ async function activateNote(notePath, newNote) {const node = await expandToNote(notePath);- if (newNote) {- noteDetailService.newNoteCreated();+ if (noteLoadedListener) {+ noteDetailService.addDetailLoadedListener(node.data.noteId, noteLoadedListener);}// we use noFocus because when we reload the tree because of background changes@@ -562,7 +562,7 @@ async function createNote(node, parentNoteId, target, isProtected, saveSelectionawait noteDetailService.saveNoteIfChanged();- noteDetailService.newNoteCreated();+ noteDetailService.addDetailLoadedListener(note.noteId, noteDetailService.focusOnTitle);const noteEntity = new NoteShort(treeCache, note);const branchEntity = new Branch(treeCache, branch);