-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
git-navigable-list-widget.tsx
175 lines (148 loc) · 6.33 KB
/
git-navigable-list-widget.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { SELECTED_CLASS, Key } from '@theia/core/lib/browser';
import { GitFileStatus, Repository, GitFileChange } from '../common';
import URI from '@theia/core/lib/common/uri';
import { GitRepositoryProvider } from './git-repository-provider';
import { LabelProvider } from '@theia/core/lib/browser/label-provider';
import { Message } from '@phosphor/messaging';
import { ElementExt } from '@phosphor/domutils';
import { inject, injectable } from 'inversify';
import { ReactWidget } from '@theia/core/lib/browser/widgets/react-widget';
import * as React from 'react';
@injectable()
export abstract class GitNavigableListWidget<T extends { selected?: boolean }> extends ReactWidget {
protected gitNodes: T[];
private _scrollContainer: string;
@inject(GitRepositoryProvider) protected readonly repositoryProvider: GitRepositoryProvider;
@inject(LabelProvider) protected readonly labelProvider: LabelProvider;
constructor() {
super();
this.node.tabIndex = 0;
}
protected onActivateRequest(msg: Message): void {
super.onActivateRequest(msg);
this.update();
this.node.focus();
}
protected set scrollContainer(id: string) {
this._scrollContainer = id + Date.now();
}
protected get scrollContainer(): string {
return this._scrollContainer;
}
protected onUpdateRequest(msg: Message): void {
if (!this.isAttached || !this.isVisible) {
return;
}
super.onUpdateRequest(msg);
(async () => {
const selected = this.node.getElementsByClassName(SELECTED_CLASS)[0];
if (selected) {
ElementExt.scrollIntoViewIfNeeded(this.node, selected);
}
})();
}
protected getStatusCaption(status: GitFileStatus, staged?: boolean): string {
return GitFileStatus.toString(status, staged);
}
protected getAbbreviatedStatusCaption(status: GitFileStatus, staged?: boolean): string {
return GitFileStatus.toAbbreviation(status, staged);
}
protected relativePath(uri: URI | string): string {
const parsedUri = typeof uri === 'string' ? new URI(uri) : uri;
const repo = this.repositoryProvider.findRepository(parsedUri);
const relativePath = repo && Repository.relativePath(repo, parsedUri);
if (relativePath) {
return relativePath.toString();
}
return this.labelProvider.getLongName(parsedUri);
}
protected getRepositoryLabel(uri: string): string | undefined {
const repository = this.repositoryProvider.findRepository(new URI(uri));
const isSelectedRepo = this.repositoryProvider.selectedRepository && repository && this.repositoryProvider.selectedRepository.localUri === repository.localUri;
return repository && !isSelectedRepo ? this.labelProvider.getLongName(new URI(repository.localUri)) : undefined;
}
protected computeCaption(fileChange: GitFileChange): string {
let result = `${this.relativePath(fileChange.uri)} - ${this.getStatusCaption(fileChange.status, true)}`;
if (fileChange.oldUri) {
result = `${this.relativePath(fileChange.oldUri)} -> ${result}`;
}
return result;
}
protected renderHeaderRow({ name, value, classNames, title }: { name: string, value: React.ReactNode, classNames?: string[], title?: string }): React.ReactNode {
if (!value) {
return;
}
const className = ['header-row', ...(classNames || [])].join(' ');
return <div key={name} className={className} title={title}>
<div className='theia-header'>{name}</div>
<div className='header-value'>{value}</div>
</div>;
}
protected addGitListNavigationKeyListeners(container: HTMLElement): void {
this.addKeyListener(container, Key.ARROW_LEFT, () => this.navigateLeft());
this.addKeyListener(container, Key.ARROW_RIGHT, () => this.navigateRight());
this.addKeyListener(container, Key.ARROW_UP, () => this.navigateUp());
this.addKeyListener(container, Key.ARROW_DOWN, () => this.navigateDown());
this.addKeyListener(container, Key.ENTER, () => this.handleListEnter());
}
protected navigateLeft(): void {
this.selectPreviousNode();
}
protected navigateRight(): void {
this.selectNextNode();
}
protected navigateUp(): void {
this.selectPreviousNode();
}
protected navigateDown(): void {
this.selectNextNode();
}
protected handleListEnter(): void {
}
protected getSelected(): T | undefined {
return this.gitNodes ? this.gitNodes.find(c => c.selected || false) : undefined;
}
protected selectNode(node: T): void {
const n = this.getSelected();
if (n) {
n.selected = false;
}
node.selected = true;
this.update();
}
protected selectNextNode(): void {
const idx = this.indexOfSelected;
if (idx >= 0 && idx < this.gitNodes.length - 1) {
this.selectNode(this.gitNodes[idx + 1]);
} else if (this.gitNodes.length > 0 && idx === -1) {
this.selectNode(this.gitNodes[0]);
}
}
protected selectPreviousNode(): void {
const idx = this.indexOfSelected;
if (idx > 0) {
this.selectNode(this.gitNodes[idx - 1]);
}
}
protected get indexOfSelected(): number {
if (this.gitNodes && this.gitNodes.length > 0) {
return this.gitNodes.findIndex(c => c.selected || false);
}
return -1;
}
}