From ef651298e378669933f0e5a59c7e356b26852a30 Mon Sep 17 00:00:00 2001 From: Johnny Almonte Date: Thu, 13 Aug 2020 12:47:14 -0400 Subject: [PATCH] wip: server history support --- .../directives/views/historyMenu.ts | 52 +++++++++++++------ .../templates/directives/history-menu.pug | 28 +++++----- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/app/assets/javascripts/directives/views/historyMenu.ts b/app/assets/javascripts/directives/views/historyMenu.ts index fcbc50a6741..88936c7c039 100644 --- a/app/assets/javascripts/directives/views/historyMenu.ts +++ b/app/assets/javascripts/directives/views/historyMenu.ts @@ -1,8 +1,10 @@ import { WebDirective } from '../../types'; import { WebApplication } from '@/ui_models/application'; import template from '%/directives/history-menu.pug'; -import { SNItem, ItemHistoryEntry, ItemHistory } from '@node_modules/snjs/dist/@types'; +import { SNItem, ItemHistoryEntry } from '@node_modules/snjs/dist/@types'; import { PureViewCtrl } from '@/views'; +import { ItemSessionHistory } from 'snjs/dist/@types/services/history/session/item_session_history'; +import { RemoteHistoryList, RemoteHistoryListEntry } from 'snjs/dist/@types/services/history/history_manager'; interface HistoryScope { application: WebApplication @@ -15,8 +17,8 @@ class HistoryMenuCtrl extends PureViewCtrl implements HistoryScope { autoOptimize = false application!: WebApplication item!: SNItem - sessionHistory?: ItemHistory - serverHistory?: ItemHistory + sessionHistory?: ItemSessionHistory + remoteHistory?: RemoteHistoryList /* @ngInject */ constructor( @@ -24,7 +26,7 @@ class HistoryMenuCtrl extends PureViewCtrl implements HistoryScope { ) { super($timeout); this.state = { - fetchingServerHistory: false + fetchingRemoteHistory: false }; } @@ -40,23 +42,36 @@ class HistoryMenuCtrl extends PureViewCtrl implements HistoryScope { this.sessionHistory = this.application.historyManager!.sessionHistoryForItem(this.item); } - get isFetchingServerHistory() { - return this.state.fetchingServerHistory; + get isfetchingRemoteHistory() { + return this.state.fetchingRemoteHistory; } - async fetchServerHistory() { + set fetchingRemoteHistory(value: boolean) { this.setState({ - fetchingServerHistory: true + fetchingRemoteHistory: value }); - this.serverHistory = await this.application.historyManager!.serverHistoryForItem(this.item) + } + + async fetchServerHistory() { + this.fetchingRemoteHistory = true; + this.remoteHistory = await this.application.historyManager!.remoteHistoryForItem(this.item) .finally(() => { - this.setState({ - fetchingServerHistory: false - }); + this.fetchingRemoteHistory = false; }); } - openRevision(revision: ItemHistoryEntry) { + async openSessionRevision(revision: ItemHistoryEntry) { + this.application.presentRevisionPreviewModal( + revision.payload.uuid, + revision.payload.content + ); + } + + async openRemoteRevision(revision: RemoteHistoryListEntry) { + const itemUuid = this.item.uuid; + this.fetchingRemoteHistory = true; + revision = await this.application.historyManager!.fetchRemoteRevision(itemUuid, revision); + this.fetchingRemoteHistory = false; this.application.presentRevisionPreviewModal( revision.payload.uuid, revision.payload.content @@ -104,12 +119,12 @@ class HistoryMenuCtrl extends PureViewCtrl implements HistoryScope { }); } - get historySessionEntries() { + get sessionHistoryEntries() { return this.sessionHistory?.entries; } - get historyServerEntries() { - return this.serverHistory?.entries + get remoteHistoryEntries() { + return this.remoteHistory; } toggleDiskSaving() { @@ -142,6 +157,11 @@ class HistoryMenuCtrl extends PureViewCtrl implements HistoryScope { }); }); } + + previewTitle(revision: RemoteHistoryListEntry) { + const createdAt = revision.created_at!; + return new Date(createdAt).toLocaleString(); + } } export class HistoryMenu extends WebDirective { diff --git a/app/assets/templates/directives/history-menu.pug b/app/assets/templates/directives/history-menu.pug index e6be80da403..dbc9334b06f 100644 --- a/app/assets/templates/directives/history-menu.pug +++ b/app/assets/templates/directives/history-menu.pug @@ -2,7 +2,7 @@ .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 + .sk-menu-panel-header-subtitle {{ctrl.sessionHistoryEntries.length || 'No'}} revisions a.sk-a.info.sk-h5( ng-click='ctrl.showSessionOptions = !ctrl.showSessionOptions; $event.stopPropagation();' ) Options @@ -28,30 +28,28 @@ | 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);' + ng-repeat='revision in ctrl.sessionHistoryEntries track by $index' + action='ctrl.openSessionRevision(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 + .sk-menu-panel-header-title Remote + .sk-menu-panel-header-subtitle {{ctrl.remoteHistoryEntries.length || 'No'}} revisions a.sk-a.info.sk-h5( - ng-click='ctrl.showServerOptions = !ctrl.showServerOptions; $event.stopPropagation();' + ng-click='ctrl.showRemoteOptions = !ctrl.showRemoteOptions; $event.stopPropagation();' ) Options - div(ng-if='ctrl.showServerOptions') + div(ng-if='ctrl.showRemoteOptions') menu-row( - action='ctrl.fetchServerHistory()' + action='ctrl.fetchRemoteHistory()' label="'Refresh'" - disabled="ctrl.isFetchingServerHistory" - spinner-class="ctrl.isFetchingServerHistory ? 'info' : null") + disabled="ctrl.isfetchingRemoteHistory" + spinner-class="ctrl.isfetchingRemoteHistory ? 'info' : null") .sk-sublabel | Fetch history from server. menu-row( - ng-repeat='revision in ctrl.historyServerEntries track by $index' - action='ctrl.openRevision(revision);' - label='revision.previewTitle()' + ng-repeat='revision in ctrl.remoteHistoryEntries track by $index' + action='ctrl.openRemoteRevision(revision);' + label='ctrl.previewTitle(revision);' ) - .sk-sublabel.opaque(ng-class='ctrl.classForRevision(revision)') - | {{revision.previewSubTitle()}}