Skip to content

Commit

Permalink
wip: server history support
Browse files Browse the repository at this point in the history
  • Loading branch information
johnny243 committed Aug 13, 2020
1 parent 9d8f6a3 commit ef65129
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
52 changes: 36 additions & 16 deletions app/assets/javascripts/directives/views/historyMenu.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,16 +17,16 @@ class HistoryMenuCtrl extends PureViewCtrl implements HistoryScope {
autoOptimize = false
application!: WebApplication
item!: SNItem
sessionHistory?: ItemHistory
serverHistory?: ItemHistory
sessionHistory?: ItemSessionHistory
remoteHistory?: RemoteHistoryList

/* @ngInject */
constructor(
$timeout: ng.ITimeoutService
) {
super($timeout);
this.state = {
fetchingServerHistory: false
fetchingRemoteHistory: false
};
}

Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 13 additions & 15 deletions app/assets/templates/directives/history-menu.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()}}

0 comments on commit ef65129

Please sign in to comment.