From 6081c3540ee0cc7a69e096fad4da3e7556326ad5 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 29 Jan 2021 22:44:59 +0100 Subject: [PATCH] refactoring --- .../app/services/dialog_command_executor.js | 2 +- src/public/app/services/note_create.js | 4 +- src/public/app/services/tab_context.js | 80 +++++++++++-------- .../attribute_widgets/attribute_editor.js | 2 +- src/public/app/widgets/note_detail.js | 1 - 5 files changed, 50 insertions(+), 39 deletions(-) diff --git a/src/public/app/services/dialog_command_executor.js b/src/public/app/services/dialog_command_executor.js index 2b149f5db..349126532 100644 --- a/src/public/app/services/dialog_command_executor.js +++ b/src/public/app/services/dialog_command_executor.js @@ -64,7 +64,7 @@ export default class DialogCommandExecutor extends Component { const tabContext = await appContext.tabManager.openTabWithNote(sqlConsoleNote.noteId, true); - appContext.triggerCommand('focusOnDetail', {tabId: tabContext.tabId}); + appContext.triggerEvent('focusOnDetail', {tabId: tabContext.tabId}); } async searchNotesCommand({searchString, ancestorNoteId}) { diff --git a/src/public/app/services/note_create.js b/src/public/app/services/note_create.js index 314f27f2c..8a03e8b05 100644 --- a/src/public/app/services/note_create.js +++ b/src/public/app/services/note_create.js @@ -56,10 +56,10 @@ async function createNote(parentNoteId, options = {}) { await activeTabContext.setNote(note.noteId); if (options.focus === 'title') { - appContext.triggerCommand('focusAndSelectTitle'); + appContext.triggerEvent('focusAndSelectTitle'); } else if (options.focus === 'content') { - appContext.triggerCommand('focusOnDetail', {tabId: activeTabContext.tabId}); + appContext.triggerEvent('focusOnDetail', {tabId: activeTabContext.tabId}); } } diff --git a/src/public/app/services/tab_context.js b/src/public/app/services/tab_context.js index b4aee5cc1..fd3b81bdc 100644 --- a/src/public/app/services/tab_context.js +++ b/src/public/app/services/tab_context.js @@ -26,31 +26,10 @@ class TabContext extends Component { } async setNote(inputNotePath, triggerSwitchEvent = true) { - const noteId = treeService.getNoteIdFromNotePath(inputNotePath); - let resolvedNotePath; + const resolvedNotePath = await this.getResolvedNotePath(inputNotePath); - if ((await treeCache.getNote(noteId)).isDeleted) { - // no point in trying to resolve canonical notePath - resolvedNotePath = inputNotePath; - } - else { - resolvedNotePath = await treeService.resolveNotePath(inputNotePath); - - if (!resolvedNotePath) { - logError(`Cannot resolve note path ${inputNotePath}`); - return; - } - - if (resolvedNotePath === this.notePath) { - return; - } - - if (await hoistedNoteService.checkNoteAccess(resolvedNotePath, this) === false) { - return; // note is outside of hoisted subtree and user chose not to unhoist - } - - // if user choise to unhoist, cache was reloaded, but might not contain this note (since it's on unexpanded path) - await treeCache.getNote(noteId); + if (!resolvedNotePath) { + return; } await this.triggerEvent('beforeNoteSwitch', {tabContext: this}); @@ -58,20 +37,12 @@ class TabContext extends Component { utils.closeActiveDialog(); this.notePath = resolvedNotePath; - this.noteId = noteId; + this.noteId = treeService.getNoteIdFromNotePath(resolvedNotePath); this.textPreviewDisabled = false; this.codePreviewDisabled = false; - setTimeout(async () => { - // we include the note into recent list only if the user stayed on the note at least 5 seconds - if (resolvedNotePath && resolvedNotePath === this.notePath) { - await server.post('recent-notes', { - noteId: this.note.noteId, - notePath: this.notePath - }); - } - }, 5000); + this.saveToRecentNotes(resolvedNotePath); protectedSessionHolder.touchProtectedSessionIfNecessary(this.note); @@ -88,6 +59,47 @@ class TabContext extends Component { } } + saveToRecentNotes(resolvedNotePath) { + setTimeout(async () => { + // we include the note into recent list only if the user stayed on the note at least 5 seconds + if (resolvedNotePath && resolvedNotePath === this.notePath) { + await server.post('recent-notes', { + noteId: this.note.noteId, + notePath: this.notePath + }); + } + }, 5000); + } + + async getResolvedNotePath(inputNotePath) { + const noteId = treeService.getNoteIdFromNotePath(inputNotePath); + + if ((await treeCache.getNote(noteId)).isDeleted) { + // no point in trying to resolve canonical notePath + return inputNotePath; + } + + const resolvedNotePath = await treeService.resolveNotePath(inputNotePath); + + if (!resolvedNotePath) { + logError(`Cannot resolve note path ${inputNotePath}`); + return; + } + + if (resolvedNotePath === this.notePath) { + return; + } + + if (await hoistedNoteService.checkNoteAccess(resolvedNotePath, this) === false) { + return; // note is outside of hoisted subtree and user chose not to unhoist + } + + // if user choise to unhoist, cache was reloaded, but might not contain this note (since it's on unexpanded path) + await treeCache.getNote(noteId); + + return resolvedNotePath; + } + /** @property {NoteShort} */ get note() { if (this.noteId && !(this.noteId in treeCache.notes)) { diff --git a/src/public/app/widgets/attribute_widgets/attribute_editor.js b/src/public/app/widgets/attribute_widgets/attribute_editor.js index 94f36d0ab..fc88accff 100644 --- a/src/public/app/widgets/attribute_widgets/attribute_editor.js +++ b/src/public/app/widgets/attribute_widgets/attribute_editor.js @@ -485,7 +485,7 @@ export default class AttributeEditorWidget extends TabAwareWidget { }); } else { - this.triggerCommand('focusOnDetail', {tabId: this.tabContext.tabId}); + this.triggerEvent('focusOnDetail', {tabId: this.tabContext.tabId}); } } } diff --git a/src/public/app/widgets/note_detail.js b/src/public/app/widgets/note_detail.js index 1b373274b..b0a869e2b 100644 --- a/src/public/app/widgets/note_detail.js +++ b/src/public/app/widgets/note_detail.js @@ -249,7 +249,6 @@ export default class NoteDetailWidget extends TabAwareWidget { loadCSS: [ "libraries/codemirror/codemirror.css", "libraries/ckeditor/ckeditor-content.css", - "libraries/ckeditor/ckeditor-content.css", "libraries/bootstrap/css/bootstrap.min.css", "libraries/katex/katex.min.css", "stylesheets/print.css",