1
+ /*-----------------------------------------------------------------------------------------------
2
+ * Copyright (c) Red Hat, Inc. All rights reserved.
3
+ * Licensed under the MIT License. See LICENSE file in the project root for license information.
4
+ *-----------------------------------------------------------------------------------------------*/
5
+ import * as vscode from 'vscode' ;
6
+ import * as path from 'path' ;
7
+ import * as fs from 'fs' ;
8
+ import { spawn , ChildProcess } from 'child_process' ;
9
+ import { ExtenisonID } from '../../util/constants' ;
10
+ import { WindowUtil } from '../../util/windowUtils' ;
11
+ import { CliChannel } from '../../cli' ;
12
+ import { vsCommand } from '../../vscommand' ;
13
+
14
+ let panel : vscode . WebviewPanel ;
15
+
16
+ const channel : vscode . OutputChannel = vscode . window . createOutputChannel ( 'CRC Logs' ) ;
17
+
18
+ async function clusterEditorMessageListener ( event : any ) : Promise < any > {
19
+ //temp empty
20
+ }
21
+
22
+ export default class ClusterViewLoader {
23
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
24
+ static get extensionPath ( ) {
25
+ return vscode . extensions . getExtension ( ExtenisonID ) . extensionPath
26
+ }
27
+
28
+ // eslint-disable-next-line @typescript-eslint/require-await
29
+ static async loadView ( title : string ) : Promise < vscode . WebviewPanel > {
30
+ const localResourceRoot = vscode . Uri . file ( path . join ( ClusterViewLoader . extensionPath , 'out' , 'createServiceView' ) ) ;
31
+ if ( panel ) {
32
+ // If we already have a panel, show it in the target column
33
+ panel . reveal ( vscode . ViewColumn . One ) ;
34
+ } else {
35
+ panel = vscode . window . createWebviewPanel ( 'createServiceView' , title , vscode . ViewColumn . One , {
36
+ enableScripts : true ,
37
+ localResourceRoots : [ localResourceRoot ] ,
38
+ retainContextWhenHidden : true
39
+ } ) ;
40
+ }
41
+ panel . iconPath = vscode . Uri . file ( path . join ( ClusterViewLoader . extensionPath , 'images/context/cluster-node.png' ) ) ;
42
+ panel . webview . html = ClusterViewLoader . getWebviewContent ( ClusterViewLoader . extensionPath , panel ) ;
43
+ panel . webview . postMessage ( { action : 'cluster' , data : '' } ) ;
44
+ panel . onDidDispose ( ( ) => {
45
+ panel = undefined ;
46
+ } ) ;
47
+ panel . webview . onDidReceiveMessage ( clusterEditorMessageListener ) ;
48
+ return panel ;
49
+ }
50
+
51
+ private static getWebviewContent ( extensionPath : string , p : vscode . WebviewPanel ) : string {
52
+ // Local path to main script run in the webview
53
+ const reactAppRootOnDisk = path . join ( extensionPath , 'out' , 'createServiceView' ) ;
54
+ const reactAppPathOnDisk = vscode . Uri . file (
55
+ path . join ( reactAppRootOnDisk , 'createServiceView.js' ) ,
56
+ ) ;
57
+ const reactAppUri = p . webview . asWebviewUri ( reactAppPathOnDisk ) ;
58
+ const htmlString :Buffer = fs . readFileSync ( path . join ( reactAppRootOnDisk , 'index.html' ) ) ;
59
+ const meta = `<meta http-equiv="Content-Security-Policy"
60
+ content="connect-src *;
61
+ default-src 'none';
62
+ img-src ${ p . webview . cspSource } https: 'self' data:;
63
+ script-src 'unsafe-eval' 'unsafe-inline' vscode-resource:;
64
+ style-src 'self' vscode-resource: 'unsafe-inline';">` ;
65
+ return `${ htmlString } `
66
+ . replace ( '%COMMAND%' , '' )
67
+ . replace ( '%PLATFORM%' , process . platform )
68
+ . replace ( 'createServiceView.js' , `${ reactAppUri } ` )
69
+ . replace ( '%BASE_URL%' , `${ reactAppUri } ` )
70
+ . replace ( '<!-- meta http-equiv="Content-Security-Policy" -->' , meta ) ;
71
+ }
72
+ }
0 commit comments