/
navigator-open-editors-widget.tsx
268 lines (239 loc) · 11 KB
/
navigator-open-editors-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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// *****************************************************************************
// Copyright (C) 2021 Ericsson 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 * as React from '@theia/core/shared/react';
import { injectable, interfaces, Container, postConstruct, inject } from '@theia/core/shared/inversify';
import {
ApplicationShell,
codicon,
ContextMenuRenderer,
defaultTreeProps,
NavigatableWidget,
NodeProps,
Saveable,
TabBar,
TreeDecoration,
TreeDecoratorService,
TreeModel,
TreeNode,
TreeProps,
TreeWidget,
TREE_NODE_CONTENT_CLASS,
Widget,
} from '@theia/core/lib/browser';
import { OpenEditorNode, OpenEditorsModel } from './navigator-open-editors-tree-model';
import { createFileTreeContainer, FileTreeModel, FileTreeWidget } from '@theia/filesystem/lib/browser';
import { OpenEditorsTreeDecoratorService } from './navigator-open-editors-decorator-service';
import { OPEN_EDITORS_CONTEXT_MENU } from './navigator-open-editors-menus';
import { CommandService } from '@theia/core/lib/common';
import { OpenEditorsCommands } from './navigator-open-editors-commands';
import { nls } from '@theia/core/lib/common/nls';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { AbstractNavigatorTreeWidget } from '../abstract-navigator-tree-widget';
export const OPEN_EDITORS_PROPS: TreeProps = {
...defaultTreeProps,
virtualized: false,
contextMenuPath: OPEN_EDITORS_CONTEXT_MENU,
leftPadding: 22
};
export interface OpenEditorsNodeRow extends TreeWidget.NodeRow {
node: OpenEditorNode;
}
@injectable()
export class OpenEditorsWidget extends AbstractNavigatorTreeWidget {
static ID = 'theia-open-editors-widget';
static LABEL = nls.localizeByDefault('Open Editors');
@inject(ApplicationShell) protected readonly applicationShell: ApplicationShell;
@inject(CommandService) protected readonly commandService: CommandService;
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
static createContainer(parent: interfaces.Container): Container {
const child = createFileTreeContainer(parent);
child.unbind(FileTreeModel);
child.bind(OpenEditorsModel).toSelf();
child.rebind(TreeModel).toService(OpenEditorsModel);
child.unbind(FileTreeWidget);
child.bind(OpenEditorsWidget).toSelf();
child.rebind(TreeProps).toConstantValue(OPEN_EDITORS_PROPS);
child.bind(OpenEditorsTreeDecoratorService).toSelf().inSingletonScope();
child.rebind(TreeDecoratorService).toService(OpenEditorsTreeDecoratorService);
return child;
}
static createWidget(parent: interfaces.Container): OpenEditorsWidget {
return OpenEditorsWidget.createContainer(parent).get(OpenEditorsWidget);
}
constructor(
@inject(TreeProps) props: TreeProps,
@inject(OpenEditorsModel) override readonly model: OpenEditorsModel,
@inject(ContextMenuRenderer) contextMenuRenderer: ContextMenuRenderer
) {
super(props, model, contextMenuRenderer);
}
@postConstruct()
override init(): void {
super.init();
this.id = OpenEditorsWidget.ID;
this.title.label = OpenEditorsWidget.LABEL;
this.addClass(OpenEditorsWidget.ID);
this.update();
}
get editorWidgets(): NavigatableWidget[] {
return this.model.editorWidgets;
}
// eslint-disable-next-line no-null/no-null
protected activeTreeNodePrefixElement: string | undefined | null;
protected override renderNode(node: OpenEditorNode, props: NodeProps): React.ReactNode {
if (!TreeNode.isVisible(node)) {
return undefined;
}
const attributes = this.createNodeAttributes(node, props);
const isEditorNode = !(node.id.startsWith(OpenEditorsModel.GROUP_NODE_ID_PREFIX) || node.id.startsWith(OpenEditorsModel.AREA_NODE_ID_PREFIX));
const content = <div className={`${TREE_NODE_CONTENT_CLASS}`}>
{this.renderExpansionToggle(node, props)}
{isEditorNode && this.renderPrefixIcon(node)}
{this.decorateIcon(node, this.renderIcon(node, props))}
{this.renderCaptionAffixes(node, props, 'captionPrefixes')}
{this.renderCaption(node, props)}
{this.renderCaptionAffixes(node, props, 'captionSuffixes')}
{this.renderTailDecorations(node, props)}
{(this.isGroupNode(node) || this.isAreaNode(node)) && this.renderInteractables(node, props)}
</div>;
return React.createElement('div', attributes, content);
}
protected override getDecorationData<K extends keyof TreeDecoration.Data>(node: TreeNode, key: K): Required<Pick<TreeDecoration.Data, K>>[K][] {
const contributed = super.getDecorationData(node, key);
if (key === 'captionSuffixes' && OpenEditorNode.is(node)) {
(contributed as Array<Array<TreeDecoration.CaptionAffix>>).push(this.getWorkspaceDecoration(node));
}
return contributed;
}
protected getWorkspaceDecoration(node: OpenEditorNode): TreeDecoration.CaptionAffix[] {
const color = this.getDecorationData(node, 'fontData').find(data => data.color)?.color;
return [{
fontData: { color },
data: this.labelProvider.getDetails(node.fileStat),
}];
}
protected isGroupNode(node: OpenEditorNode): boolean {
return node.id.startsWith(OpenEditorsModel.GROUP_NODE_ID_PREFIX);
}
protected isAreaNode(node: OpenEditorNode): boolean {
return node.id.startsWith(OpenEditorsModel.AREA_NODE_ID_PREFIX);
}
protected override doRenderNodeRow({ node, depth }: OpenEditorsNodeRow): React.ReactNode {
let groupClass = '';
if (this.isGroupNode(node)) {
groupClass = 'group-node';
} else if (this.isAreaNode(node)) {
groupClass = 'area-node';
}
return <div className={`open-editors-node-row ${this.getPrefixIconClass(node)}${groupClass}`}>
{this.renderNode(node, { depth })}
</div>;
}
protected renderInteractables(node: OpenEditorNode, props: NodeProps): React.ReactNode {
return (<div className='open-editors-inline-actions-container'>
<div className='open-editors-inline-action'>
<a className='codicon codicon-save-all'
title={OpenEditorsCommands.SAVE_ALL_IN_GROUP_FROM_ICON.label}
onClick={this.handleGroupActionIconClicked}
data-id={node.id}
id={OpenEditorsCommands.SAVE_ALL_IN_GROUP_FROM_ICON.id}
/>
</div>
<div className='open-editors-inline-action' >
<a className='codicon codicon-close-all'
title={OpenEditorsCommands.CLOSE_ALL_EDITORS_IN_GROUP_FROM_ICON.label}
onClick={this.handleGroupActionIconClicked}
data-id={node.id}
id={OpenEditorsCommands.CLOSE_ALL_EDITORS_IN_GROUP_FROM_ICON.id}
/>
</div>
</div>
);
}
protected handleGroupActionIconClicked = async (e: React.MouseEvent<HTMLAnchorElement>) => this.doHandleGroupActionIconClicked(e);
protected async doHandleGroupActionIconClicked(e: React.MouseEvent<HTMLAnchorElement>): Promise<void> {
e.stopPropagation();
const groupName = e.currentTarget.getAttribute('data-id');
const command = e.currentTarget.id;
if (groupName && command) {
const groupFromTarget: string | number | undefined = groupName.split(':').pop();
const areaOrTabBar = this.sanitizeInputFromClickHandler(groupFromTarget);
if (areaOrTabBar) {
return this.commandService.executeCommand(command, areaOrTabBar);
}
}
}
protected sanitizeInputFromClickHandler(groupFromTarget?: string): ApplicationShell.Area | TabBar<Widget> | undefined {
let areaOrTabBar: ApplicationShell.Area | TabBar<Widget> | undefined;
if (groupFromTarget) {
if (ApplicationShell.isValidArea(groupFromTarget)) {
areaOrTabBar = groupFromTarget;
} else {
const groupAsNum = parseInt(groupFromTarget);
if (!isNaN(groupAsNum)) {
areaOrTabBar = this.model.getTabBarForGroup(groupAsNum);
}
}
}
return areaOrTabBar;
}
protected renderPrefixIcon(node: OpenEditorNode): React.ReactNode {
return (
<div className='open-editors-prefix-icon-container'>
<div data-id={node.id}
className={`open-editors-prefix-icon dirty ${codicon('circle-filled', true)}`}
/>
<div data-id={node.id}
onClick={this.closeEditor}
className={`open-editors-prefix-icon close ${codicon('close', true)}`}
/>
</div>);
}
protected getPrefixIconClass(node: OpenEditorNode): string {
const saveable = Saveable.get(node.widget);
if (saveable) {
return saveable.dirty ? 'dirty' : '';
}
return '';
}
protected closeEditor = async (e: React.MouseEvent<HTMLDivElement>) => this.doCloseEditor(e);
protected async doCloseEditor(e: React.MouseEvent<HTMLDivElement>): Promise<void> {
const widgetId = e.currentTarget.getAttribute('data-id');
if (widgetId) {
await this.applicationShell.closeWidget(widgetId);
}
}
protected override tapNode(node?: TreeNode): void {
if (OpenEditorNode.is(node)) {
this.applicationShell.activateWidget(node.widget.id);
}
super.tapNode(node);
}
protected override handleContextMenuEvent(node: OpenEditorNode | undefined, event: React.MouseEvent<HTMLElement>): void {
super.handleContextMenuEvent(node, event);
if (node) {
// Since the CommonCommands used in the context menu act on the shell's activeWidget, this is necessary to ensure
// that the EditorWidget is activated, not the Navigator itself
this.applicationShell.activateWidget(node.widget.id);
}
}
protected override getPaddingLeft(node: TreeNode): number {
if (node.id.startsWith(OpenEditorsModel.AREA_NODE_ID_PREFIX)) {
return 0;
}
return this.props.leftPadding;
}
}