Skip to content

Commit

Permalink
fixup! Apply Timeline plugin API
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Jul 10, 2020
1 parent 1bc6617 commit 5a0eec1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 99 deletions.
22 changes: 21 additions & 1 deletion packages/timeline/src/browser/timeline-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import { FileNavigatorContribution } from '@theia/navigator/lib/browser/navigato
import { EXPLORER_VIEW_CONTAINER_ID } from '@theia/navigator/lib/browser';
import { TimelineWidget } from './timeline-widget';
import { TimelineService } from './timeline-service';
import { CommandRegistry } from '@theia/core/lib/common';
import { Command, CommandRegistry } from '@theia/core/lib/common';
import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';

@injectable()
export class TimelineContribution implements FrontendApplicationContribution {
Expand All @@ -41,6 +42,14 @@ export class TimelineContribution implements FrontendApplicationContribution {
protected readonly commandRegistry: CommandRegistry;
@inject(TabBarToolbarRegistry)
protected readonly tabBarToolbar: TabBarToolbarRegistry;
@inject(EditorManager)
protected readonly editorManager: EditorManager;

public static readonly LOAD_MORE_COMMAND: Command = {
id: 'timeline-load-more',
label: 'Refresh',
iconClass: 'fa fa-refresh'
};

async onDidInitializeLayout?(app: FrontendApplication): Promise<void> {
const explorer = await this.widgetManager.getWidget(EXPLORER_VIEW_CONTAINER_ID);
Expand Down Expand Up @@ -70,6 +79,17 @@ export class TimelineContribution implements FrontendApplicationContribution {
isEnabled: widget => this.checkWidget(widget, () => true),
isVisible: widget => this.checkWidget(widget, () => true)
});
this.commandRegistry.registerCommand(TimelineContribution.LOAD_MORE_COMMAND, {
execute: () => {
const current = this.editorManager.currentEditor;
if (current instanceof EditorWidget) {
const uri = current.getResourceUri();
if (uri) {
timeline.loadTimeline(uri, false);
}
}
}
});
this.tabBarToolbar.registerItem(toolbarItem);
}

Expand Down
36 changes: 29 additions & 7 deletions packages/timeline/src/browser/timeline-tree-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
TreeModelImpl,
} from '@theia/core/lib/browser/tree';
import { Command } from '@theia/core/lib/common';
import { TimelineItem } from '../common/timeline-protocol';
import { Timeline, TimelineItem } from '../common/timeline-protocol';
import { TimelineContribution } from './timeline-contribution';

export interface TimelineNode extends SelectableTreeNode {
source: string;
Expand All @@ -34,13 +35,34 @@ export interface TimelineNode extends SelectableTreeNode {
contextValue: string | undefined;
}

export class TimelineAggregate {
readonly items: TimelineItem[];
readonly source: string;
readonly uri: string;

private _cursor?: string;
get cursor(): string | undefined {
return this._cursor;
}

set cursor(cursor: string | undefined) {
this._cursor = cursor;
}

constructor(timeline: Timeline) {
this.source = timeline.source;
this.items = timeline.items;
this._cursor = timeline.paging?.cursor;
}

add(items: TimelineItem[]): void {
this.items.push(...items);
this.items.sort((a, b) => b.timestamp - a.timestamp);
}
}

@injectable()
export class TimelineTreeModel extends TreeModelImpl {
public static readonly LOAD_MORE_COMMAND: Command = {
id: 'timeline-load-more',
label: 'Refresh',
iconClass: 'fa fa-refresh'
};

updateTree(source: string, uri: string, items: TimelineItem[], loadMore: boolean): void {
const root = {
Expand Down Expand Up @@ -76,7 +98,7 @@ export class TimelineTreeModel extends TreeModelImpl {
name: 'Load-more',
description: '',
detail: undefined,
command: TimelineTreeModel.LOAD_MORE_COMMAND,
command: TimelineContribution.LOAD_MORE_COMMAND,
commandArgs: [],
contextValue: undefined,
selected: true
Expand Down
89 changes: 2 additions & 87 deletions packages/timeline/src/browser/timeline-tree-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ import { injectable, inject } from 'inversify';
import { Command, CommandRegistry, MenuModelRegistry, MenuPath } from '@theia/core/lib/common';
import { TreeWidget, TreeProps, NodeProps, TREE_NODE_SEGMENT_GROW_CLASS } from '@theia/core/lib/browser/tree';
import { ContextMenuRenderer } from '@theia/core/lib/browser';
import { EditorManager, EditorWidget } from '@theia/editor/lib/browser';
import { TimelineNode, TimelineTreeModel } from './timeline-tree-model';
import { EditorManager } from '@theia/editor/lib/browser';
import { TimelineAggregate, TimelineNode, TimelineTreeModel } from './timeline-tree-model';
import { TimelineService } from './timeline-service';
import { TimelineContextKeyService } from './timeline-context-key-service';
import { Timeline, TimelineItem, TimelineProvidersChangeEvent } from '../common/timeline-protocol';
import URI from '@theia/core/lib/common/uri';
import * as React from 'react';

export const TIMELINE_ITEM_CONTEXT_MENU: MenuPath = ['timeline-item-context-menu'];
Expand All @@ -50,35 +48,6 @@ export class TimelineTreeWidget extends TreeWidget {
super(props, model, contextMenuRenderer);
this.id = TimelineTreeWidget.ID;
this.addClass('timeline-outer-container');
this.commandRegistry.registerCommand(TimelineTreeModel.LOAD_MORE_COMMAND, {
execute: () => {
const current = this.editorManager.currentEditor;
if (current instanceof EditorWidget) {
const uri = current.getResourceUri();
if (uri) {
this.loadTimeline(uri, false);
}
}
}
});
this.timelineService.onDidChangeProviders(e => this.onProvidersChanged(e));
}

private onProvidersChanged(event: TimelineProvidersChangeEvent): void {
const currentUri = this.editorManager.currentEditor?.getResourceUri();
if (event.removed) {
for (const source of event.removed) {
this.timelinesBySource.delete(source);
}

if (currentUri) {
this.loadTimeline(currentUri, true);
}
} else if (event.added) {
if (currentUri) {
event.added.forEach( source => this.loadTimelineForSource(source, currentUri, true));
}
}
}

protected renderNode(node: TimelineNode, props: NodeProps): React.ReactNode {
Expand All @@ -98,60 +67,6 @@ export class TimelineTreeWidget extends TreeWidget {
contextMenuRenderer={this.contextMenuRenderer}/>;
return React.createElement('div', attributes, content);
}
async loadTimeline(uri: URI, reset: boolean): Promise<void> {
for (const source of this.timelineService.getSources().map(s => s.id)) {
this.loadTimelineForSource(source, uri, reset);
}
}

async loadTimelineForSource(source: string, uri: URI, reset: boolean): Promise<void> {
if (reset) {
this.timelinesBySource.delete(source);
}
let timeline = this.timelinesBySource.get(source);
const cursor = timeline?.cursor;
const options = { cursor: reset ? undefined : cursor, limit: TimelineTreeWidget.PAGE_SIZE };
const timelineResult = await this.timelineService.getTimeline(source, uri, options);
if (timelineResult) {
const items = timelineResult.items;
if (items) {
if (timeline) {
timeline.add(items);
timeline.cursor = timelineResult.paging?.cursor;
} else {
timeline = new TimelineAggregate(timelineResult);
}
this.timelinesBySource.set(source, timeline);
this.model.updateTree(source, uri.toString(), timeline.items, !!timeline.cursor);
}
}
}
}

class TimelineAggregate {
readonly items: TimelineItem[];
readonly source: string;
readonly uri: string;

private _cursor?: string;
get cursor(): string | undefined {
return this._cursor;
}

set cursor(cursor: string | undefined) {
this._cursor = cursor;
}

constructor(timeline: Timeline) {
this.source = timeline.source;
this.items = timeline.items;
this._cursor = timeline.paging?.cursor;
}

add(items: TimelineItem[]): void {
this.items.push(...items);
this.items.sort((a, b) => b.timestamp - a.timestamp);
}
}

export namespace TimelineItemNode {
Expand Down
59 changes: 55 additions & 4 deletions packages/timeline/src/browser/timeline-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ import { TimelineEmptyWidget } from './timeline-empty-widget';
import { toArray } from '@phosphor/algorithm';
import URI from '@theia/core/lib/common/uri';
import { EditorPreviewWidget } from '@theia/editor-preview/lib/browser';
import { TimelineProvidersChangeEvent } from '../common/timeline-protocol';
import { TimelineAggregate } from './timeline-tree-model';

@injectable()
export class TimelineWidget extends BaseWidget {

protected panel: Panel;
static ID = 'timeline-view';

private readonly timelinesBySource = new Map<string, TimelineAggregate>();

@inject(TimelineTreeWidget) protected readonly resourceWidget: TimelineTreeWidget;
@inject(TimelineService) protected readonly timelineService: TimelineService;
@inject(CommandRegistry) protected readonly commandRegistry: CommandRegistry;
Expand Down Expand Up @@ -73,11 +77,11 @@ export class TimelineWidget extends BaseWidget {
this.toDispose.push(this.timelineService.onDidChangeTimeline(event => {
const current = this.editorManager.currentEditor;
if (NavigatableWidget.is(current) && event.uri && event.uri === current.getResourceUri()?.toString()) {
this.resourceWidget.loadTimeline(new URI(event.uri), event.reset);
this.loadTimeline(new URI(event.uri), event.reset);
} else {
const uri = this.editorManager.currentEditor?.getResourceUri();
if (uri) {
this.resourceWidget.loadTimeline(uri, event.reset);
this.loadTimeline(uri, event.reset);
}
}
})
Expand All @@ -88,7 +92,7 @@ export class TimelineWidget extends BaseWidget {
if (uri) {
this.timelineEmptyWidget.hide();
this.resourceWidget.show();
this.resourceWidget.loadTimeline(uri, true);
this.loadTimeline(uri, true);
}
return;
}
Expand All @@ -104,6 +108,53 @@ export class TimelineWidget extends BaseWidget {
this.timelineEmptyWidget.show();
}
}));
this.toDispose.push(this.timelineService.onDidChangeProviders(e => this.onProvidersChanged(e)));
}

private onProvidersChanged(event: TimelineProvidersChangeEvent): void {
const currentUri = this.editorManager.currentEditor?.getResourceUri();
if (event.removed) {
for (const source of event.removed) {
this.timelinesBySource.delete(source);
}

if (currentUri) {
this.loadTimeline(currentUri, true);
}
} else if (event.added) {
if (currentUri) {
event.added.forEach( source => this.loadTimelineForSource(source, currentUri, true));
}
}
}

async loadTimelineForSource(source: string, uri: URI, reset: boolean): Promise<void> {
if (reset) {
this.timelinesBySource.delete(source);
}
let timeline = this.timelinesBySource.get(source);
const cursor = timeline?.cursor;
const options = { cursor: reset ? undefined : cursor, limit: TimelineTreeWidget.PAGE_SIZE };
const timelineResult = await this.timelineService.getTimeline(source, uri, options);
if (timelineResult) {
const items = timelineResult.items;
if (items) {
if (timeline) {
timeline.add(items);
timeline.cursor = timelineResult.paging?.cursor;
} else {
timeline = new TimelineAggregate(timelineResult);
}
this.timelinesBySource.set(source, timeline);
this.resourceWidget.model.updateTree(source, uri.toString(), timeline.items, !!timeline.cursor);
}
}
}

async loadTimeline(uri: URI, reset: boolean): Promise<void> {
for (const source of this.timelineService.getSources().map(s => s.id)) {
this.loadTimelineForSource(source, uri, reset);
}
}

refreshList(): void {
Expand All @@ -112,7 +163,7 @@ export class TimelineWidget extends BaseWidget {
if (uri) {
this.timelineEmptyWidget.hide();
this.resourceWidget.show();
this.resourceWidget.loadTimeline(uri, true);
this.loadTimeline(uri, true);
} else {
this.timelineEmptyWidget.show();
this.resourceWidget.hide();
Expand Down

0 comments on commit 5a0eec1

Please sign in to comment.