forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistryViewLoader.ts
More file actions
131 lines (124 loc) · 5.58 KB
/
registryViewLoader.ts
File metadata and controls
131 lines (124 loc) · 5.58 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
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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { ExtenisonID } from '../../util/constants';
import { stringify } from 'yaml';
import { ComponentTypesView } from '../../registriesView'
import { vsCommand } from '../../vscommand';
import { ExtCommandTelemetryEvent } from '../../telemetry';
let panel: vscode.WebviewPanel;
async function devfileRegistryViewerMessageListener(event: any): Promise<any> {
let starterProject = event.selectedProject;
switch (event?.action) {
case 'getAllComponents':
getAllComponents(event.action);
break;
case 'getYAML':
const yaml = stringify(event.data, { indent: 4 });
panel.webview.postMessage(
{
action: event.action,
devYAML: yaml
}
);
break;
case 'createComponent':
const registryName = event.registryName;
vscode.commands.executeCommand('openshift.componentType.newComponent', starterProject, registryName);
break;
case 'cloneToWorkSpace':
vscode.commands.executeCommand('openshift.componentType.cloneStarterProjectRepository', starterProject);
break;
case 'openInBrowser':
vscode.commands.executeCommand('openshift.componentType.openStarterProjectRepository', starterProject);
break;
case 'telemeteryCopyEvent':
const devFileName = event.devFileName;
const telemetryEventCopyDevFile = new ExtCommandTelemetryEvent('openshift.registryView.starterProjects.copyDevFile');
telemetryEventCopyDevFile.send({
component_type: devFileName
})
break;
default:
panel.webview.postMessage(
{
error: 'Invalid command'
}
);
break;
}
}
export default class RegistryViewLoader {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
static get extensionPath() {
return vscode.extensions.getExtension(ExtenisonID).extensionPath
}
// eslint-disable-next-line @typescript-eslint/require-await
static async loadView(title: string): Promise<vscode.WebviewPanel> {
const localResourceRoot = vscode.Uri.file(path.join(RegistryViewLoader.extensionPath, 'out', 'devFileRegistryViewer'));
if (panel) {
// If we already have a panel, show it in the target column
panel.reveal(vscode.ViewColumn.One);
getAllComponents('getAllComponents');
} else {
panel = vscode.window.createWebviewPanel('devFileRegistryView', title, vscode.ViewColumn.One, {
enableScripts: true,
localResourceRoots: [localResourceRoot],
retainContextWhenHidden: true
});
panel.iconPath = vscode.Uri.file(path.join(RegistryViewLoader.extensionPath, 'images/context/devfile.png'));
panel.webview.html = RegistryViewLoader.getWebviewContent(RegistryViewLoader.extensionPath, panel);
panel.onDidDispose(() => {
panel = undefined;
});
panel.webview.onDidReceiveMessage(devfileRegistryViewerMessageListener);
}
return panel;
}
private static getWebviewContent(extensionPath: string, p: vscode.WebviewPanel): string {
// Local path to main script run in the webview
const reactAppRootOnDisk = path.join(extensionPath, 'out', 'devFileRegistryViewer');
const reactAppPathOnDisk = vscode.Uri.file(
path.join(reactAppRootOnDisk, 'devFileRegistryViewer.js'),
);
const reactAppUri = p.webview.asWebviewUri(reactAppPathOnDisk);
const htmlString: Buffer = fs.readFileSync(path.join(reactAppRootOnDisk, 'index.html'));
const meta = `<meta http-equiv="Content-Security-Policy"
content="connect-src *;
default-src 'none';
img-src ${p.webview.cspSource} https: 'self' data:;
script-src 'unsafe-eval' 'unsafe-inline' vscode-resource:;
style-src 'self' vscode-resource: 'unsafe-inline';">`;
return `${htmlString}`
.replace('%COMMAND%', '')
.replace('%PLATFORM%', process.platform)
.replace('devFileRegistryViewer.js', `${reactAppUri}`)
.replace('%BASE_URL%', `${reactAppUri}`)
.replace('<!-- meta http-equiv="Content-Security-Policy" -->', meta);
}
@vsCommand('openshift.componentTypesView.registry.closeView')
static async closeRegistryInWebview(): Promise<void> {
panel?.dispose();
}
static refresh(): void {
if (panel) {
panel.webview.postMessage({ action: 'loadingComponents' });
getAllComponents('getAllComponents');
}
}
}
function getAllComponents(eventActionName: string) {
const registries = ComponentTypesView.instance.getListOfRegistries();
const componentDescriptions = ComponentTypesView.instance.getCompDescriptions();
panel.webview.postMessage(
{
action: eventActionName,
compDescriptions: Array.from(componentDescriptions),
registries: registries
}
);
}