From 50f45547a4fc3fc48abec65f4c9c2a4950855f4f Mon Sep 17 00:00:00 2001 From: Johnny Almonte Date: Mon, 13 Jul 2020 03:18:49 -0400 Subject: [PATCH] wip: server history support --- app/assets/javascripts/app.ts | 4 +- .../{sessionHistoryMenu.ts => historyMenu.ts} | 41 +++++++++----- .../javascripts/directives/views/index.ts | 2 +- .../javascripts/views/editor/editor-view.pug | 12 ++-- .../templates/directives/history-menu.pug | 55 +++++++++++++++++++ .../directives/session-history-menu.pug | 35 ------------ 6 files changed, 92 insertions(+), 57 deletions(-) rename app/assets/javascripts/directives/views/{sessionHistoryMenu.ts => historyMenu.ts} (77%) create mode 100644 app/assets/templates/directives/history-menu.pug delete mode 100644 app/assets/templates/directives/session-history-menu.pug diff --git a/app/assets/javascripts/app.ts b/app/assets/javascripts/app.ts index 64fb23e63..49ef4852a 100644 --- a/app/assets/javascripts/app.ts +++ b/app/assets/javascripts/app.ts @@ -44,7 +44,7 @@ import { PrivilegesAuthModal, PrivilegesManagementModal, RevisionPreviewModal, - SessionHistoryMenu, + HistoryMenu, SyncResolutionMenu } from './directives/views'; @@ -100,7 +100,7 @@ angular .directive('privilegesAuthModal', () => new PrivilegesAuthModal()) .directive('privilegesManagementModal', () => new PrivilegesManagementModal()) .directive('revisionPreviewModal', () => new RevisionPreviewModal()) - .directive('sessionHistoryMenu', () => new SessionHistoryMenu()) + .directive('historyMenu', () => new HistoryMenu()) .directive('syncResolutionMenu', () => new SyncResolutionMenu()); // Filters diff --git a/app/assets/javascripts/directives/views/sessionHistoryMenu.ts b/app/assets/javascripts/directives/views/historyMenu.ts similarity index 77% rename from app/assets/javascripts/directives/views/sessionHistoryMenu.ts rename to app/assets/javascripts/directives/views/historyMenu.ts index a4baabd35..d9eba473d 100644 --- a/app/assets/javascripts/directives/views/sessionHistoryMenu.ts +++ b/app/assets/javascripts/directives/views/historyMenu.ts @@ -1,21 +1,22 @@ -import { WebDirective } from './../../types'; +import { WebDirective } from '../../types'; import { WebApplication } from '@/ui_models/application'; -import template from '%/directives/session-history-menu.pug'; +import template from '%/directives/history-menu.pug'; import { SNItem, ItemHistoryEntry, ItemHistory } from '@node_modules/snjs/dist/@types'; +import { PayloadSource } from 'snjs'; -interface SessionHistoryScope { +interface HistoryScope { application: WebApplication item: SNItem } -class SessionHistoryMenuCtrl implements SessionHistoryScope { +class HistoryMenuCtrl implements HistoryScope { $timeout: ng.ITimeoutService diskEnabled = false autoOptimize = false + fetchingServerHistory = false application!: WebApplication item!: SNItem - entries!: ItemHistoryEntry[] history!: ItemHistory /* @ngInject */ @@ -31,12 +32,8 @@ class SessionHistoryMenuCtrl implements SessionHistoryScope { this.autoOptimize = this.application.historyManager!.isAutoOptimizeEnabled(); } - reloadHistory() { - const history = this.application.historyManager!.historyForItem(this.item); - this.entries = history.entries.slice(0).sort((a, b) => { - return a.payload.updated_at! < b.payload.updated_at! ? 1 : -1; - }); - this.history = history; + async reloadHistory() { + this.history = await this.application.historyManager!.historyForItem(this.item); } openRevision(revision: ItemHistoryEntry) { @@ -93,6 +90,18 @@ class SessionHistoryMenuCtrl implements SessionHistoryScope { ); } + get historySessionEntries() { + return this.history.entries.filter((entry) => { + return entry.payload.source === PayloadSource.SessionHistory; + }); + } + + get historyServerEntries() { + return this.history.entries.filter((entry) => { + return entry.payload.source === PayloadSource.ServerHistory; + }); + } + toggleDiskSaving() { const run = () => { this.application.historyManager!.toggleDiskSaving().then(() => { @@ -125,14 +134,20 @@ class SessionHistoryMenuCtrl implements SessionHistoryScope { }); }); } + + fetchServerHistory() { + this.fetchingServerHistory = true; + this.reloadHistory(); + this.fetchingServerHistory = false; + } } -export class SessionHistoryMenu extends WebDirective { +export class HistoryMenu extends WebDirective { constructor() { super(); this.restrict = 'E'; this.template = template; - this.controller = SessionHistoryMenuCtrl; + this.controller = HistoryMenuCtrl; this.controllerAs = 'ctrl'; this.bindToController = true; this.scope = { diff --git a/app/assets/javascripts/directives/views/index.ts b/app/assets/javascripts/directives/views/index.ts index 1f51d0023..adee8d90c 100644 --- a/app/assets/javascripts/directives/views/index.ts +++ b/app/assets/javascripts/directives/views/index.ts @@ -11,5 +11,5 @@ export { PermissionsModal } from './permissionsModal'; export { PrivilegesAuthModal } from './privilegesAuthModal'; export { PrivilegesManagementModal } from './privilegesManagementModal'; export { RevisionPreviewModal } from './revisionPreviewModal'; -export { SessionHistoryMenu } from './sessionHistoryMenu'; +export { HistoryMenu } from './historyMenu'; export { SyncResolutionMenu } from './syncResolutionMenu'; diff --git a/app/assets/javascripts/views/editor/editor-view.pug b/app/assets/javascripts/views/editor/editor-view.pug index e5ab4338b..20f06047c 100644 --- a/app/assets/javascripts/views/editor/editor-view.pug +++ b/app/assets/javascripts/views/editor/editor-view.pug @@ -183,14 +183,14 @@ application='self.application' ) .sk-app-bar-item( - click-outside=`self.setMenuState('showSessionHistory', false)`, - is-open='self.state.showSessionHistory', - ng-click="self.toggleMenu('showSessionHistory')" + click-outside=`self.setMenuState('showHistory', false)`, + is-open='self.state.showHistory', + ng-click="self.toggleMenu('showHistory')" ) - .sk-label Session History - session-history-menu( + .sk-label History + history-menu( item='self.note', - ng-if='self.state.showSessionHistory', + ng-if='self.state.showHistory', application='self.application' ) #editor-content.editor-content(ng-if='!self.note.errorDecrypting') diff --git a/app/assets/templates/directives/history-menu.pug b/app/assets/templates/directives/history-menu.pug new file mode 100644 index 000000000..80df589b8 --- /dev/null +++ b/app/assets/templates/directives/history-menu.pug @@ -0,0 +1,55 @@ +#history-menu.sn-component + .sk-menu-panel.dropdown-menu + .sk-menu-panel-header + .sk-menu-panel-header-title Session + .sk-menu-panel-header-subtitle {{ctrl.historySessionEntries.length || 'No'}} revisions + a.sk-a.info.sk-h5( + ng-click='ctrl.showSessionOptions = !ctrl.showSessionOptions; $event.stopPropagation();' + ) Options + div(ng-if='ctrl.showSessionOptions') + menu-row( + action='ctrl.clearItemHistory()' + label="'Clear note local history'" + ) + menu-row( + action='ctrl.clearAllHistory()' + label="'Clear all local history'" + ) + menu-row( + action='ctrl.toggleAutoOptimize()' + label="(ctrl.autoOptimize ? 'Disable' : 'Enable') + ' auto cleanup'") + .sk-sublabel + | Automatically cleans up small revisions to conserve space. + menu-row( + action='ctrl.toggleDiskSaving()' + label="(ctrl.diskEnabled ? 'Disable' : 'Enable') + ' saving history to disk'" + ) + .sk-sublabel + | Saving to disk is not recommended. Decreases performance and increases app + | loading time and memory footprint. + menu-row( + ng-repeat='revision in ctrl.historySessionEntries track by $index' + action='ctrl.openRevision(revision);' + label='revision.previewTitle()' + ) + .sk-sublabel.opaque(ng-class='ctrl.classForRevision(revision)') + | {{revision.previewSubTitle()}} + .sk-menu-panel-header + .sk-menu-panel-header-title Server + .sk-menu-panel-header-subtitle {{ctrl.historyServerEntries.length || 'No'}} revisions + a.sk-a.info.sk-h5( + ng-click='ctrl.showServerOptions = !ctrl.showServerOptions; $event.stopPropagation();' + ) Options + div(ng-if='ctrl.showServerOptions') + menu-row( + action='ctrl.fetchServerHistory()' + label="(ctrl.fetchingServerHistory ? 'Refreshing...' : 'Refresh')") + .sk-sublabel + | Fetch history from server. + menu-row( + ng-repeat='revision in ctrl.historyServerEntries track by $index' + action='ctrl.openRevision(revision);' + label='revision.previewTitle()' + ) + .sk-sublabel.opaque(ng-class='ctrl.classForRevision(revision)') + | {{revision.previewSubTitle()}} diff --git a/app/assets/templates/directives/session-history-menu.pug b/app/assets/templates/directives/session-history-menu.pug deleted file mode 100644 index 36788db7a..000000000 --- a/app/assets/templates/directives/session-history-menu.pug +++ /dev/null @@ -1,35 +0,0 @@ -#session-history-menu.sn-component - .sk-menu-panel.dropdown-menu - .sk-menu-panel-header - .sk-menu-panel-header-title {{ctrl.history.entries.length || 'No'}} revisions - a.sk-a.info.sk-h5( - ng-click='ctrl.showOptions = !ctrl.showOptions; $event.stopPropagation();' - ) Options - div(ng-if='ctrl.showOptions') - menu-row( - action='ctrl.clearItemHistory()' - label="'Clear note local history'" - ) - menu-row( - action='ctrl.clearAllHistory()' - label="'Clear all local history'" - ) - menu-row( - action='ctrl.toggleAutoOptimize()' - label="(ctrl.autoOptimize ? 'Disable' : 'Enable') + ' auto cleanup'") - .sk-sublabel - | Automatically cleans up small revisions to conserve space. - menu-row( - action='ctrl.toggleDiskSaving()' - label="(ctrl.diskEnabled ? 'Disable' : 'Enable') + ' saving history to disk'" - ) - .sk-sublabel - | Saving to disk is not recommended. Decreases performance and increases app - | loading time and memory footprint. - menu-row( - ng-repeat='revision in ctrl.entries' - action='ctrl.openRevision(revision);' - label='revision.previewTitle()' - ) - .sk-sublabel.opaque(ng-class='ctrl.classForRevision(revision)') - | {{revision.previewSubTitle()}}