|
| 1 | +import*asvscodefrom'vscode' |
| 2 | +import*asCRfrom'typings' |
| 3 | +import*aspathfrom'path' |
| 4 | + |
| 5 | +functiongetNonce():string{ |
| 6 | +lettext='' |
| 7 | +constpossible='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' |
| 8 | +for(leti=0;i<32;i++){ |
| 9 | +text+=possible.charAt(Math.floor(Math.random()*possible.length)) |
| 10 | +} |
| 11 | +returntext |
| 12 | +} |
| 13 | + |
| 14 | +// TODO: move column into createOrShow |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Manages React webview panels |
| 19 | + */ |
| 20 | +classReactWebView{ |
| 21 | +/** |
| 22 | + * Track the currently panel. Only allow a single panel to exist at a time. |
| 23 | + */ |
| 24 | +publicstaticcurrentPanel:ReactWebView|undefined=undefined |
| 25 | + |
| 26 | +//@ts-ignore |
| 27 | +privatepanel:vscode.WebviewPanel |
| 28 | +privateextensionPath:string |
| 29 | +privatedisposables:vscode.Disposable[]=[] |
| 30 | +privateonReceive:any// TODO: properly type |
| 31 | + |
| 32 | +publicconstructor(extensionPath:string,onReceive:any){ |
| 33 | +this.extensionPath=extensionPath |
| 34 | +this.onReceive=onReceive |
| 35 | +} |
| 36 | + |
| 37 | +publicasynccreateOrShow(extensionPath:string,column:number=vscode.ViewColumn.One):Promise<void>{ |
| 38 | +consthasActiveEditor=vscode.window.activeTextEditor |
| 39 | + |
| 40 | +if(!hasActiveEditor){ |
| 41 | +thrownewError('Should have an open file on launch') |
| 42 | +} |
| 43 | + |
| 44 | +// If we already have a panel, show it. |
| 45 | +// Otherwise, create a new panel. |
| 46 | +if(ReactWebView.currentPanel&&ReactWebView.currentPanel.panel){ |
| 47 | +ReactWebView.currentPanel.panel.reveal(column) |
| 48 | +}else{ |
| 49 | +constviewType='CodeRoad' |
| 50 | +consttitle='CodeRoad' |
| 51 | +constconfig={ |
| 52 | +// Enable javascript in the webview |
| 53 | +enableScripts:true, |
| 54 | + |
| 55 | +// And restric the webview to only loading content from our extension's `media` directory. |
| 56 | +localResourceRoots:[vscode.Uri.file(path.join(this.extensionPath,'build'))], |
| 57 | + |
| 58 | +// prevents destroying the window when it is in the background |
| 59 | +retainContextWhenHidden:true, |
| 60 | +} |
| 61 | +// Create and show a new webview panel |
| 62 | +this.panel=vscode.window.createWebviewPanel(viewType,title,column,config) |
| 63 | + |
| 64 | +// Set the webview's initial html content |
| 65 | +this.panel.webview.html=this.getHtmlForWebview() |
| 66 | + |
| 67 | +// Listen for when the panel is disposed |
| 68 | +// This happens when the user closes the panel or when the panel is closed programatically |
| 69 | +this.panel.onDidDispose(()=>this.dispose(),null,this.disposables) |
| 70 | + |
| 71 | +// Handle messages from the webview |
| 72 | +this.panel.webview.onDidReceiveMessage(this.onReceive,null,this.disposables) |
| 73 | +} |
| 74 | +} |
| 75 | + |
| 76 | +publicasyncpostMessage(action:CR.Action):Promise<void>{ |
| 77 | +// Send a message to the webview webview. |
| 78 | +// You can send any JSON serializable data. |
| 79 | +constsuccess=awaitthis.panel.webview.postMessage(action) |
| 80 | +if(!success){ |
| 81 | +thrownewError(`Message post failure:${JSON.stringify(action)}`) |
| 82 | +} |
| 83 | +} |
| 84 | + |
| 85 | +publicdispose():void{ |
| 86 | +ReactWebView.currentPanel=undefined |
| 87 | + |
| 88 | +// Clean up our resources |
| 89 | +this.panel.dispose() |
| 90 | + |
| 91 | +while(this.disposables.length){ |
| 92 | +constx=this.disposables.pop() |
| 93 | +if(x){ |
| 94 | +x.dispose() |
| 95 | +} |
| 96 | +} |
| 97 | +} |
| 98 | + |
| 99 | +privategetHtmlForWebview():string{ |
| 100 | + |
| 101 | +// eslint-disable-next-line |
| 102 | +constmanifest=require(path.join(this.extensionPath,'build','asset-manifest.json')) |
| 103 | +constmainScript=manifest.files['main.js'] |
| 104 | +// grab first chunk |
| 105 | +constchunk=Object.keys(manifest.files).filter(f=>f.match(/^static\/js\/.+\.js$/))[0] |
| 106 | +constchunkScript=manifest.files[chunk] |
| 107 | +constmainStyle=manifest.files['main.css'] |
| 108 | + |
| 109 | +constscriptPathOnDisk=vscode.Uri.file(path.join(this.extensionPath,'build',mainScript)) |
| 110 | +constscriptUri=scriptPathOnDisk.with({scheme:'vscode-resource'}) |
| 111 | +constchunkPathOnDisk=vscode.Uri.file(path.join(this.extensionPath,'build',chunkScript)) |
| 112 | +constchunkUri=chunkPathOnDisk.with({scheme:'vscode-resource'}) |
| 113 | +conststylePathOnDisk=vscode.Uri.file(path.join(this.extensionPath,'build',mainStyle)) |
| 114 | +conststyleUri=stylePathOnDisk.with({scheme:'vscode-resource'}) |
| 115 | + |
| 116 | +// Use a nonce to whitelist which scripts can be run |
| 117 | +constnonce=getNonce() |
| 118 | +constnonce2=getNonce() |
| 119 | +constnonce3=getNonce() |
| 120 | + |
| 121 | +return`<!DOCTYPE html> |
| 122 | +<html lang="en"> |
| 123 | +<head> |
| 124 | +<meta charset="utf-8"> |
| 125 | +<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"> |
| 126 | + <meta name="theme-color" content="#000000"> |
| 127 | + <title>React App</title> |
| 128 | + <link rel="manifest" href="./manifest.json" /> |
| 129 | +<link rel="stylesheet" type="text/css" href="${styleUri}"> |
| 130 | +<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src vscode-resource: https:; script-src 'nonce-${nonce}' 'nonce-${nonce2}' 'nonce-${nonce3}'; style-src vscode-resource: 'unsafe-inline' http: https: data:;"> |
| 131 | + <base href="${vscode.Uri.file(path.join(this.extensionPath,'build')).with({scheme:'vscode-resource'})}/"> |
| 132 | + <style></style> |
| 133 | +</head> |
| 134 | +
|
| 135 | +<body> |
| 136 | +<noscript>You need to enable JavaScript to run this app.</noscript> |
| 137 | + <div id="root">Loading...</div> |
| 138 | + <script nonce=${nonce} src="./webpackBuild.js"></script> |
| 139 | + <script nonce=${nonce2} src="${chunkUri}"></script> |
| 140 | + <script nonce="${nonce3}" src="${scriptUri}"></script> |
| 141 | +</body> |
| 142 | + </html>` |
| 143 | +} |
| 144 | +} |
| 145 | + |
| 146 | +exportdefaultReactWebView |