-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathtree-view-widget.tsx
241 lines (210 loc) · 7.34 KB
/
tree-view-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
import { LabelIcon } from '@theia/core/lib/browser/label-parser';
import { OpenerService, open } from '@theia/core/lib/browser/opener-service';
import { codicon } from '@theia/core/lib/browser/widgets/widget';
import { DisposableCollection } from '@theia/core/lib/common/disposable';
import { URI } from '@theia/core/lib/common/uri';
import { inject, injectable } from '@theia/core/shared/inversify';
import React from '@theia/core/shared/react';
import { URI as CodeUri } from '@theia/core/shared/vscode-uri';
import { TreeViewWidget as TheiaTreeViewWidget } from '@theia/plugin-ext/lib/main/browser/view/tree-view-widget';
// Copied back from https://github.com/eclipse-theia/theia/pull/14391
// Remove the patching when Arduino uses Eclipse Theia >1.55.0
// https://github.com/eclipse-theia/theia/blob/8d3c5a11af65448b6700bedd096f8d68f0675541/packages/core/src/browser/tree/tree-view-welcome-widget.tsx#L37-L54
// https://github.com/eclipse-theia/theia/blob/8d3c5a11af65448b6700bedd096f8d68f0675541/packages/core/src/browser/tree/tree-view-welcome-widget.tsx#L146-L298
interface ViewWelcome {
readonly view: string;
readonly content: string;
readonly when?: string;
readonly enablement?: string;
readonly order: number;
}
export interface IItem {
readonly welcomeInfo: ViewWelcome;
visible: boolean;
}
export interface ILink {
readonly label: string;
readonly href: string;
readonly title?: string;
}
type LinkedTextItem = string | ILink;
@injectable()
export class TreeViewWidget extends TheiaTreeViewWidget {
@inject(OpenerService)
private readonly openerService: OpenerService;
private readonly toDisposeBeforeUpdateViewWelcomeNodes =
new DisposableCollection();
protected override updateViewWelcomeNodes(): void {
this.viewWelcomeNodes = [];
this.toDisposeBeforeUpdateViewWelcomeNodes.dispose();
const items = this.visibleItems.sort((a, b) => a.order - b.order);
const enablementKeys: Set<string>[] = [];
// the plugin-view-registry will push the changes when there is a change in the `when` prop which controls the visibility
// this listener is to update the enablement of the components in the view welcome
this.toDisposeBeforeUpdateViewWelcomeNodes.push(
this.contextService.onDidChange((event) => {
if (enablementKeys.some((keys) => event.affects(keys))) {
this.updateViewWelcomeNodes();
this.update();
}
})
);
// Note: VS Code does not support the `renderSecondaryButtons` prop in welcome content either.
for (const item of items) {
const { content } = item;
const enablement = isEnablementAware(item) ? item.enablement : undefined;
const itemEnablementKeys = enablement
? this.contextService.parseKeys(enablement)
: undefined;
if (itemEnablementKeys) {
enablementKeys.push(itemEnablementKeys);
}
const lines = content.split('\n');
for (let line of lines) {
line = line.trim();
if (!line) {
continue;
}
const linkedTextItems = this.parseLinkedText_patch14309(line);
if (
linkedTextItems.length === 1 &&
typeof linkedTextItems[0] !== 'string'
) {
const node = linkedTextItems[0];
this.viewWelcomeNodes.push(
this.renderButtonNode_patch14309(
node,
this.viewWelcomeNodes.length,
enablement
)
);
} else {
const renderNode = (item: LinkedTextItem, index: number) =>
typeof item == 'string'
? this.renderTextNode_patch14309(item, index)
: this.renderLinkNode_patch14309(item, index, enablement);
this.viewWelcomeNodes.push(
<p key={`p-${this.viewWelcomeNodes.length}`}>
{...linkedTextItems.flatMap(renderNode)}
</p>
);
}
}
}
}
private renderButtonNode_patch14309(
node: ILink,
lineKey: string | number,
enablement: string | undefined
): React.ReactNode {
return (
<div key={`line-${lineKey}`} className="theia-WelcomeViewButtonWrapper">
<button
title={node.title}
className="theia-button theia-WelcomeViewButton"
disabled={!this.isEnabledClick_patch14309(enablement)}
onClick={(e) => this.openLinkOrCommand_patch14309(e, node.href)}
>
{node.label}
</button>
</div>
);
}
private renderTextNode_patch14309(
node: string,
textKey: string | number
): React.ReactNode {
return (
<span key={`text-${textKey}`}>
{this.labelParser
.parse(node)
.map((segment, index) =>
LabelIcon.is(segment) ? (
<span key={index} className={codicon(segment.name)} />
) : (
<span key={index}>{segment}</span>
)
)}
</span>
);
}
private renderLinkNode_patch14309(
node: ILink,
linkKey: string | number,
enablement: string | undefined
): React.ReactNode {
return (
<a
key={`link-${linkKey}`}
className={this.getLinkClassName_patch14309(node.href, enablement)}
title={node.title || ''}
onClick={(e) => this.openLinkOrCommand_patch14309(e, node.href)}
>
{node.label}
</a>
);
}
private getLinkClassName_patch14309(
href: string,
enablement: string | undefined
): string {
const classNames = ['theia-WelcomeViewCommandLink'];
// Only command-backed links can be disabled. All other, https:, file: remain enabled
if (
href.startsWith('command:') &&
!this.isEnabledClick_patch14309(enablement)
) {
classNames.push('disabled');
}
return classNames.join(' ');
}
private isEnabledClick_patch14309(enablement: string | undefined): boolean {
return typeof enablement === 'string'
? this.contextService.match(enablement)
: true;
}
private openLinkOrCommand_patch14309 = (
event: React.MouseEvent,
value: string
): void => {
event.stopPropagation();
if (value.startsWith('command:')) {
const command = value.replace('command:', '');
this.commands.executeCommand(command);
} else if (value.startsWith('file:')) {
const uri = value.replace('file:', '');
open(this.openerService, new URI(CodeUri.file(uri).toString()));
} else {
this.windowService.openNewWindow(value, { external: true });
}
};
private parseLinkedText_patch14309(text: string): LinkedTextItem[] {
const result: LinkedTextItem[] = [];
const linkRegex =
/\[([^\]]+)\]\(((?:https?:\/\/|command:|file:)[^\)\s]+)(?: (["'])(.+?)(\3))?\)/gi;
let index = 0;
let match: RegExpExecArray | null;
while ((match = linkRegex.exec(text))) {
if (match.index - index > 0) {
result.push(text.substring(index, match.index));
}
const [, label, href, , title] = match;
if (title) {
result.push({ label, href, title });
} else {
result.push({ label, href });
}
index = match.index + match[0].length;
}
if (index < text.length) {
result.push(text.substring(index));
}
return result;
}
}
interface EnablementAware {
readonly enablement: string | undefined;
}
function isEnablementAware(arg: unknown): arg is EnablementAware {
return !!arg && typeof arg === 'object' && 'enablement' in arg;
}