-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDesignerOutlineProvider.ts
More file actions
73 lines (60 loc) · 2.63 KB
/
DesignerOutlineProvider.ts
File metadata and controls
73 lines (60 loc) · 2.63 KB
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
import * as vscode from 'vscode';
export interface SerializedOutlineItem {
id: string;
label: string;
detail?: string;
icon?: string;
contextValue?: string;
children?: SerializedOutlineItem[];
}
function convertItems(items: SerializedOutlineItem[]): vscode.CustomEditorOutlineItem[] {
return items.map(item => ({
id: item.id,
label: item.label,
detail: item.detail,
icon: item.icon ? new vscode.ThemeIcon(item.icon) : undefined,
contextValue: item.contextValue,
children: item.children ? convertItems(item.children) : undefined,
}));
}
interface ResourceState {
items: vscode.CustomEditorOutlineItem[];
webview: vscode.Webview;
}
export class DesignerOutlineProvider implements vscode.CustomEditorOutlineProvider {
private readonly _onDidChangeOutline = new vscode.EventEmitter<vscode.Uri>();
readonly onDidChangeOutline = this._onDidChangeOutline.event;
private readonly _onDidChangeActiveItem = new vscode.EventEmitter<{ uri: vscode.Uri; itemId: string | undefined }>();
readonly onDidChangeActiveItem = this._onDidChangeActiveItem.event;
private readonly _resources = new Map<string, ResourceState>();
setWebview(resource: vscode.Uri, webview: vscode.Webview): void {
const state = this._resources.get(resource.toString());
if (state) {
state.webview = webview;
} else {
this._resources.set(resource.toString(), { items: [], webview });
}
}
removeResource(resource: vscode.Uri): void {
this._resources.delete(resource.toString());
}
updateFromWebview(resource: vscode.Uri, serializedItems: SerializedOutlineItem[]): void {
const state = this._resources.get(resource.toString());
if (state) {
state.items = convertItems(serializedItems);
}
this._onDidChangeOutline.fire(resource);
}
setActive(resource: vscode.Uri, itemId: string | undefined): void {
this._onDidChangeActiveItem.fire({ uri: resource, itemId });
}
provideOutline(resource: vscode.Uri, _token: vscode.CancellationToken): vscode.CustomEditorOutlineItem[] {
return this._resources.get(resource.toString())?.items ?? [];
}
revealItem(resource: vscode.Uri, itemId: string): void {
this._resources.get(resource.toString())?.webview?.postMessage({ type: 'reveal', id: itemId });
}
sendCommand(resource: vscode.Uri, command: string, itemId?: string): void {
this._resources.get(resource.toString())?.webview?.postMessage({ type: 'outlineCommand', command, id: itemId });
}
}