From 6d912c4897cdfae61f4ada7c7389d57b13412a48 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 7 Feb 2020 20:56:49 +0100 Subject: [PATCH] removed dependency on note tree widget from app context --- src/public/javascripts/desktop.js | 1 + src/public/javascripts/dialogs/move_to.js | 1 + src/public/javascripts/mobile.js | 1 + .../javascripts/services/app_context.js | 67 ++++++------------- .../javascripts/services/dialog_events.js | 4 +- .../javascripts/services/entrypoints.js | 8 +-- .../javascripts/services/search_notes.js | 1 + src/public/javascripts/widgets/note_tree.js | 2 - src/public/javascripts/widgets/search_box.js | 1 + 9 files changed, 32 insertions(+), 54 deletions(-) diff --git a/src/public/javascripts/desktop.js b/src/public/javascripts/desktop.js index f9bc52396..23640906f 100644 --- a/src/public/javascripts/desktop.js +++ b/src/public/javascripts/desktop.js @@ -36,6 +36,7 @@ window.glob.isDesktop = utils.isDesktop; window.glob.isMobile = utils.isMobile; // required for CKEditor image upload plugin +// FIXME window.glob.getActiveNode = () => appContext.getMainNoteTree().getActiveNode(); window.glob.getHeaders = server.getHeaders; window.glob.showAddLinkDialog = () => import('./dialogs/add_link.js').then(d => d.showDialog()); diff --git a/src/public/javascripts/dialogs/move_to.js b/src/public/javascripts/dialogs/move_to.js index 4fde49802..345fb184a 100644 --- a/src/public/javascripts/dialogs/move_to.js +++ b/src/public/javascripts/dialogs/move_to.js @@ -36,6 +36,7 @@ export async function showDialog(nodes) { } async function moveNotesTo(notePath) { + // FIXME const targetNode = await appContext.getMainNoteTree().getNodeFromPath(notePath); await treeChangesService.moveToParentNote(movedNodes, targetNode); diff --git a/src/public/javascripts/mobile.js b/src/public/javascripts/mobile.js index 273713ab1..9d3cf4d74 100644 --- a/src/public/javascripts/mobile.js +++ b/src/public/javascripts/mobile.js @@ -90,6 +90,7 @@ async function showTree() { } $detail.on("click", ".note-menu-button", async e => { + // FIXME const node = appContext.getMainNoteTree().getActiveNode(); const branch = treeCache.getBranch(node.data.branchId); const note = await treeCache.getNote(node.data.noteId); diff --git a/src/public/javascripts/services/app_context.js b/src/public/javascripts/services/app_context.js index 39b84c384..0bbdbd695 100644 --- a/src/public/javascripts/services/app_context.js +++ b/src/public/javascripts/services/app_context.js @@ -1,4 +1,3 @@ -import NoteTreeWidget from "../widgets/note_tree.js"; import TabContext from "./tab_context.js"; import server from "./server.js"; import treeCache from "./tree_cache.js"; @@ -10,6 +9,7 @@ import utils from "./utils.js"; import treeService from "./tree.js"; import ZoomService from "./zoom.js"; import Layout from "../widgets/layout.js"; +import SpacedUpdate from "./spaced_update.js"; class AppContext { constructor(layout) { @@ -17,8 +17,17 @@ class AppContext { this.components = []; /** @type {TabContext[]} */ this.tabContexts = []; - this.tabsChangedTaskId = null; this.activeTabId = null; + + this.tabsUpdate = new SpacedUpdate(async () => { + const openTabs = this.tabContexts + .map(tc => tc.getTabState()) + .filter(t => !!t); + + await server.put('options', { + openTabs: JSON.stringify(openTabs) + }); + }); } async start() { @@ -88,18 +97,16 @@ class AppContext { filteredTabs[0].active = true; } - for (const tab of filteredTabs) { - const tabContext = this.openEmptyTab(); - tabContext.setNote(tab.notePath); + this.tabsUpdate.allowUpdateWithoutChange(() => { + for (const tab of filteredTabs) { + const tabContext = this.openEmptyTab(); + tabContext.setNote(tab.notePath); - if (tab.active) { - this.activateTab(tabContext.tabId); + if (tab.active) { + this.activateTab(tabContext.tabId); + } } - } - - // previous opening triggered task to save tab changes but these are bogus changes (this is init) - // so we'll cancel it - this.clearOpenTabsTask(); + }); } showWidgets() { @@ -201,13 +208,6 @@ class AppContext { await tabContext.setNote(notePath); } - /** - * @return {NoteTreeWidget} - */ - getMainNoteTree() { - return this.noteTreeWidget; - } - getTab(newTab, state) { if (!this.getActiveTabContext() || newTab) { // if it's a new tab explicitly by user then it's in background @@ -266,35 +266,8 @@ class AppContext { this.saveOpenTabs(); } - async saveOpenTabs() { - const openTabs = []; - - for (const tabContext of this.tabContexts) { - const tabState = tabContext.getTabState(); - - if (tabState) { - openTabs.push(tabState); - } - } - - await server.put('options', { - openTabs: JSON.stringify(openTabs) - }); - } - - clearOpenTabsTask() { - if (this.tabsChangedTaskId) { - clearTimeout(this.tabsChangedTaskId); - } - } - openTabsChangedListener() { - // we don't want to send too many requests with tab changes so we always schedule task to do this in 1 seconds, - // but if there's any change in between, we cancel the old one and schedule new one - // so effectively we kind of wait until user stopped e.g. quickly switching tabs - this.clearOpenTabsTask(); - - this.tabsChangedTaskId = setTimeout(() => this.saveOpenTabs(), 1000); + this.tabsUpdate.scheduleUpdate(); } activateTab(tabId) { diff --git a/src/public/javascripts/services/dialog_events.js b/src/public/javascripts/services/dialog_events.js index 51f484bf7..2aebb3a87 100644 --- a/src/public/javascripts/services/dialog_events.js +++ b/src/public/javascripts/services/dialog_events.js @@ -34,7 +34,7 @@ export default class DialogEventComponent extends Component { } async cloneNotesToListener() { - // probably should not happen here + // FIXME const selectedOrActiveNodes = this.appContext.getMainNoteTree().getSelectedOrActiveNodes(); const noteIds = selectedOrActiveNodes.map(node => node.data.noteId); @@ -44,6 +44,7 @@ export default class DialogEventComponent extends Component { } async moveNotesToListener() { + // FIXME const selectedOrActiveNodes = this.appContext.getMainNoteTree().getSelectedOrActiveNodes(); const d = await import("../dialogs/move_to.js"); @@ -51,6 +52,7 @@ export default class DialogEventComponent extends Component { } async editBranchPrefixListener() { + // FIXME const node = this.appContext.getMainNoteTree().getActiveNode(); const editBranchPrefixDialog = await import("../dialogs/branch_prefix.js"); diff --git a/src/public/javascripts/services/entrypoints.js b/src/public/javascripts/services/entrypoints.js index e82edc3c7..c1f33f21f 100644 --- a/src/public/javascripts/services/entrypoints.js +++ b/src/public/javascripts/services/entrypoints.js @@ -78,17 +78,17 @@ export default class Entrypoints extends Component { } toggleNoteHoistingListener() { - const node = appContext.getMainNoteTree().getActiveNode(); + const note = appContext.getActiveTabNote(); hoistedNoteService.getHoistedNoteId().then(async hoistedNoteId => { - if (node.data.noteId === hoistedNoteId) { + if (note.noteId === hoistedNoteId) { hoistedNoteService.unhoist(); } else { - const note = await treeCache.getNote(node.data.noteId); + const note = await treeCache.getNote(note.noteId); if (note.type !== 'search') { - hoistedNoteService.setHoistedNoteId(node.data.noteId); + hoistedNoteService.setHoistedNoteId(note.noteId); } } }); diff --git a/src/public/javascripts/services/search_notes.js b/src/public/javascripts/services/search_notes.js index de67ececa..2c001e623 100644 --- a/src/public/javascripts/services/search_notes.js +++ b/src/public/javascripts/services/search_notes.js @@ -17,6 +17,7 @@ const helpText = `

`; async function refreshSearch() { + // FIXME const activeNode = appContext.getMainNoteTree().getActiveNode(); activeNode.load(true); diff --git a/src/public/javascripts/widgets/note_tree.js b/src/public/javascripts/widgets/note_tree.js index d5f2b9349..567eff5f6 100644 --- a/src/public/javascripts/widgets/note_tree.js +++ b/src/public/javascripts/widgets/note_tree.js @@ -35,8 +35,6 @@ export default class NoteTreeWidget extends TabAwareWidget { window.glob.cutIntoNote = () => this.cutIntoNoteListener(); - this.appContext.noteTreeWidget = this; - this.tree = null; } diff --git a/src/public/javascripts/widgets/search_box.js b/src/public/javascripts/widgets/search_box.js index 432601411..eb35120e8 100644 --- a/src/public/javascripts/widgets/search_box.js +++ b/src/public/javascripts/widgets/search_box.js @@ -107,6 +107,7 @@ export default class SearchBoxWidget extends BasicWidget { return; } + // FIXME let activeNode = appContext.getMainNoteTree().getActiveNode(); const parentNote = await treeCache.getNote(activeNode.data.noteId);