@@ -2,27 +2,10 @@ import * as vscode from 'vscode'
22import * as CR from 'typings'
33import * as path from 'path'
44
5- function getNonce ( ) : string {
6- let text = ''
7- const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
8- for ( let i = 0 ; i < 32 ; i ++ ) {
9- text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) )
10- }
11- return text
12- }
13-
14- // TODO: move column into createOrShow
15-
16-
175/**
186 * Manages React webview panels
197 */
208class ReactWebView {
21- /**
22- * Track the currently panel. Only allow a single panel to exist at a time.
23- */
24- public static currentPanel : ReactWebView | undefined = undefined
25-
269 // @ts -ignore
2710 private panel : vscode . WebviewPanel
2811 private extensionPath : string
@@ -32,53 +15,71 @@ class ReactWebView {
3215 public constructor ( extensionPath : string , onReceive : any ) {
3316 this . extensionPath = extensionPath
3417 this . onReceive = onReceive
18+
19+ // Create and show a new webview panel
20+ this . panel = this . createWebviewPanel ( vscode . ViewColumn . One )
21+
22+ // Set the webview's initial html content
23+ this . panel . webview . html = this . getHtmlForWebview ( )
24+
25+ // Listen for when the panel is disposed
26+ // This happens when the user closes the panel or when the panel is closed programatically
27+ this . panel . onDidDispose ( ( ) => this . dispose ( ) , null , this . disposables )
28+
29+ // Handle messages from the webview
30+ this . panel . webview . onDidReceiveMessage ( this . onReceive , null , this . disposables )
31+ console . log ( 'webview loaded' )
3532 }
3633
37- public async createOrShow ( column : number = vscode . ViewColumn . One ) : Promise < void > {
34+ public async createOrShow ( column : number ) : Promise < void > {
3835 // If we already have a panel, show it.
3936 // Otherwise, create a new panel.
40- if ( ReactWebView . currentPanel && ReactWebView . currentPanel . panel ) {
41- ReactWebView . currentPanel . panel . reveal ( column )
37+ if ( this . panel && this . panel . webview ) {
38+ console . log ( 'reveal' )
39+ this . panel . reveal ( column )
4240 } else {
43- const viewType = 'CodeRoad'
44- const title = 'CodeRoad'
45- const config = {
46- // Enable javascript in the webview
47- enableScripts : true ,
48-
49- // And restric the webview to only loading content from our extension's `media` directory.
50- localResourceRoots : [ vscode . Uri . file ( path . join ( this . extensionPath , 'build' ) ) ] ,
41+ console . log ( 'make new panel' )
42+ this . panel = this . createWebviewPanel ( column )
5143
52- // prevents destroying the window when it is in the background
53- retainContextWhenHidden : true ,
54- }
55- // Create and show a new webview panel
56- this . panel = vscode . window . createWebviewPanel ( viewType , title , column , config )
57-
58- // Set the webview's initial html content
59- this . panel . webview . html = this . getHtmlForWebview ( )
44+ }
45+ }
6046
61- // Listen for when the panel is disposed
62- // This happens when the user closes the panel or when the panel is closed programatically
63- this . panel . onDidDispose ( ( ) => this . dispose ( ) , null , this . disposables )
47+ private createWebviewPanel ( column : number ) : vscode . WebviewPanel {
48+ const viewType = 'CodeRoad'
49+ const title = 'CodeRoad'
50+ const config = {
51+ // Enable javascript in the webview
52+ enableScripts : true ,
53+ // And restric the webview to only loading content from our extension's `media` directory.
54+ localResourceRoots : [ vscode . Uri . file ( path . join ( this . extensionPath , 'build' ) ) ] ,
55+ // prevents destroying the window when it is in the background
56+ retainContextWhenHidden : true ,
57+ }
58+ return vscode . window . createWebviewPanel ( viewType , title , column , config )
59+ }
6460
65- // Handle messages from the webview
66- this . panel . webview . onDidReceiveMessage ( this . onReceive , null , this . disposables )
61+ private getNonce ( ) : string {
62+ let text = ''
63+ const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
64+ for ( let i = 0 ; i < 32 ; i ++ ) {
65+ text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) )
6766 }
67+ return text
6868 }
6969
7070 public async postMessage ( action : CR . Action ) : Promise < void > {
71+ console . log ( 'webview postMessage' )
72+ console . log ( action )
7173 // Send a message to the webview webview.
7274 // You can send any JSON serializable data.
7375 const success = await this . panel . webview . postMessage ( action )
7476 if ( ! success ) {
7577 throw new Error ( `Message post failure: ${ JSON . stringify ( action ) } ` )
7678 }
79+ console . log ( 'postMessage sent' )
7780 }
7881
7982 public dispose ( ) : void {
80- ReactWebView . currentPanel = undefined
81-
8283 // Clean up our resources
8384 this . panel . dispose ( )
8485
@@ -108,9 +109,9 @@ class ReactWebView {
108109 const styleUri = stylePathOnDisk . with ( { scheme : 'vscode-resource' } )
109110
110111 // Use a nonce to whitelist which scripts can be run
111- const nonce = getNonce ( )
112- const nonce2 = getNonce ( )
113- const nonce3 = getNonce ( )
112+ const nonce = this . getNonce ( )
113+ const nonce2 = this . getNonce ( )
114+ const nonce3 = this . getNonce ( )
114115
115116 return `<!DOCTYPE html>
116117 <html lang="en">
0 commit comments