-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathCustomViews.ts
269 lines (215 loc) · 8.01 KB
/
CustomViews.ts
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
269
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as path from "path";
import * as vscode from "vscode";
import { RequestType } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { LanguageClientConsumer } from "../languageClientConsumer";
export class CustomViewsFeature extends LanguageClientConsumer {
private commands: vscode.Disposable[] = [];
private contentProvider: PowerShellContentProvider;
constructor() {
super();
this.contentProvider = new PowerShellContentProvider();
this.commands.push(
vscode.workspace.registerTextDocumentContentProvider(
"powershell",
this.contentProvider));
}
public dispose(): void {
for (const command of this.commands) {
command.dispose();
}
}
public override setLanguageClient(languageClient: LanguageClient): void {
languageClient.onRequest(
NewCustomViewRequestType,
(args) => {
this.contentProvider.createView(
args.id,
args.title,
args.viewType);
});
languageClient.onRequest(
ShowCustomViewRequestType,
(args) => {
this.contentProvider.showView(
args.id,
args.viewColumn);
});
languageClient.onRequest(
CloseCustomViewRequestType,
(args) => {
this.contentProvider.closeView(args.id);
});
languageClient.onRequest(
SetHtmlContentViewRequestType,
(args) => {
this.contentProvider.setHtmlContentView(
args.id,
args.htmlContent);
});
languageClient.onRequest(
AppendHtmlOutputViewRequestType,
(args) => {
this.contentProvider.appendHtmlOutputView(
args.id,
args.appendedHtmlBodyContent);
});
}
}
class PowerShellContentProvider implements vscode.TextDocumentContentProvider {
private viewIndex: Record<string, CustomView> = {};
private didChangeEvent: vscode.EventEmitter<vscode.Uri> = new vscode.EventEmitter<vscode.Uri>();
public onDidChange: vscode.Event<vscode.Uri> = this.didChangeEvent.event;
public provideTextDocumentContent(uri: vscode.Uri): string {
return this.viewIndex[uri.toString()].getContent();
}
public createView(id: string, title: string, viewType: CustomViewType): void {
let view;
switch (viewType) {
case CustomViewType.HtmlContent:
view = new HtmlContentView(id, title);
}
this.viewIndex[this.getUri(view.id)] = view;
}
public showView(id: string, viewColumn: vscode.ViewColumn): void {
const uriString = this.getUri(id);
(this.viewIndex[uriString] as HtmlContentView).showContent(viewColumn);
}
public closeView(id: string): void {
const uriString = this.getUri(id);
vscode.workspace.textDocuments.some((doc) => {
if (doc.uri.toString() === uriString) {
void vscode.window.showTextDocument(doc);
void vscode.commands.executeCommand("workbench.action.closeActiveEditor");
return true;
}
return false;
});
}
public setHtmlContentView(id: string, content: IHtmlContent): void {
const uriString = this.getUri(id);
const view: CustomView = this.viewIndex[uriString];
(view as HtmlContentView).setContent(content);
this.didChangeEvent.fire(vscode.Uri.parse(uriString));
}
public appendHtmlOutputView(id: string, content: string): void {
const uriString = this.getUri(id);
const view: CustomView = this.viewIndex[uriString];
(view as HtmlContentView).appendContent(content);
this.didChangeEvent.fire(vscode.Uri.parse(uriString));
}
private getUri(id: string): string {
return `powershell://views/${id}`;
}
}
abstract class CustomView {
constructor(
public id: string,
public title: string,
public viewType: CustomViewType) {
}
public abstract getContent(): string;
}
class HtmlContentView extends CustomView {
private htmlContent: IHtmlContent = {
bodyContent: "",
javaScriptPaths: [],
styleSheetPaths: [],
};
private webviewPanel: vscode.WebviewPanel | undefined;
constructor(
id: string,
title: string) {
super(id, title, CustomViewType.HtmlContent);
}
public setContent(htmlContent: IHtmlContent): void {
this.htmlContent = htmlContent;
}
public appendContent(content: string): void {
this.htmlContent.bodyContent += content;
}
public getContent(): string {
let styleTags = "";
if (this.htmlContent.styleSheetPaths.length > 0) {
for (const styleSheetPath of this.htmlContent.styleSheetPaths) {
styleTags += `<link rel="stylesheet" href="${styleSheetPath.toString().replace("file://", "vscode-resource://")}">\n`;
}
}
let scriptTags = "";
if (this.htmlContent.javaScriptPaths.length > 0) {
for (const javaScriptPath of this.htmlContent.javaScriptPaths) {
scriptTags += `<script src="${javaScriptPath.toString().replace("file://", "vscode-resource://")}"></script>\n`;
}
}
// Return an HTML page with the specified content
return `<html><head>${styleTags}</head><body>\n${this.htmlContent.bodyContent}\n${scriptTags}</body></html>`;
}
public showContent(viewColumn: vscode.ViewColumn): void {
this.webviewPanel?.dispose();
let localResourceRoots: vscode.Uri[] = [];
localResourceRoots = localResourceRoots.concat(this.htmlContent.javaScriptPaths.map((p) => {
return vscode.Uri.parse(path.dirname(p));
}));
localResourceRoots = localResourceRoots.concat(this.htmlContent.styleSheetPaths.map((p) => {
return vscode.Uri.parse(path.dirname(p));
}));
this.webviewPanel = vscode.window.createWebviewPanel(
this.id,
this.title,
viewColumn,
{
enableScripts: true,
enableFindWidget: true,
enableCommandUris: true,
retainContextWhenHidden: true,
localResourceRoots,
});
this.webviewPanel.webview.html = this.getContent();
this.webviewPanel.reveal(viewColumn);
}
}
enum CustomViewType {
HtmlContent = 1,
}
export const NewCustomViewRequestType =
new RequestType<INewCustomViewRequestArguments, void, void>(
"powerShell/newCustomView");
interface INewCustomViewRequestArguments {
id: string;
title: string;
viewType: CustomViewType;
}
export const ShowCustomViewRequestType =
new RequestType<IShowCustomViewRequestArguments, void, void>(
"powerShell/showCustomView");
interface IShowCustomViewRequestArguments {
id: string;
viewColumn: vscode.ViewColumn;
}
export const CloseCustomViewRequestType =
new RequestType<ICloseCustomViewRequestArguments, void, void>(
"powerShell/closeCustomView");
interface ICloseCustomViewRequestArguments {
id: string;
}
export const SetHtmlContentViewRequestType =
new RequestType<ISetHtmlContentViewRequestArguments, void, void>(
"powerShell/setHtmlViewContent");
interface IHtmlContent {
bodyContent: string;
javaScriptPaths: string[];
styleSheetPaths: string[];
}
interface ISetHtmlContentViewRequestArguments {
id: string;
htmlContent: IHtmlContent;
}
export const AppendHtmlOutputViewRequestType =
new RequestType<IAppendHtmlOutputViewRequestArguments, void, void>(
"powerShell/appendHtmlViewContent");
interface IAppendHtmlOutputViewRequestArguments {
id: string;
appendedHtmlBodyContent: string;
}