@@ -4,16 +4,16 @@ import * as path from 'path';
4
4
import { ListProvider } from './tree' ;
5
5
6
6
export function activate ( context : vscode . ExtensionContext ) {
7
- context . subscriptions . push (
8
- vscode . commands . registerCommand ( 'catCoding.show' , ( ) => {
9
- CatCodingPanel . createOrShow ( context ) ;
10
- } )
7
+ context . subscriptions . push (
8
+ vscode . commands . registerCommand ( 'catCoding.show' , ( ) => {
9
+ CatCodingPanel . createOrShow ( context ) ;
10
+ } )
11
11
) ;
12
12
13
13
context . subscriptions . push (
14
14
vscode . commands . registerCommand ( 'catCoding.openConvertPage' , k => {
15
15
CatCodingPanel . createOrShow ( context ) ;
16
- // CatCodingPanel.currentPanel?.webview.postMessage()
16
+ CatCodingPanel . currentPanel ?. broadcast ( `UPDATE_CURRENCY` , k ) ;
17
17
} )
18
18
) ;
19
19
@@ -22,178 +22,178 @@ export function activate(context: vscode.ExtensionContext) {
22
22
new ListProvider ( `` )
23
23
) ;
24
24
25
- if ( vscode . window . registerWebviewPanelSerializer ) {
26
- // Make sure we register a serializer in activation event
27
- vscode . window . registerWebviewPanelSerializer ( CatCodingPanel . viewType , {
28
- async deserializeWebviewPanel ( webviewPanel : vscode . WebviewPanel , state : any ) {
29
- CatCodingPanel . revive ( webviewPanel , context . extensionUri , context . extensionPath ) ;
30
- }
31
- } ) ;
32
- }
25
+ if ( vscode . window . registerWebviewPanelSerializer ) {
26
+ // Make sure we register a serializer in activation event
27
+ vscode . window . registerWebviewPanelSerializer ( CatCodingPanel . viewType , {
28
+ async deserializeWebviewPanel ( webviewPanel : vscode . WebviewPanel , state : any ) {
29
+ CatCodingPanel . revive ( webviewPanel , context . extensionUri , context . extensionPath ) ;
30
+ }
31
+ } ) ;
32
+ }
33
33
}
34
34
35
35
/**
36
36
* Manages cat coding webview panels
37
37
*/
38
38
class CatCodingPanel {
39
- /**
40
- * Track the currently panel. Only allow a single panel to exist at a time.
41
- */
42
- public static currentPanel : CatCodingPanel | undefined ;
43
-
44
- public static readonly viewType = 'catCoding' ;
45
-
46
- private readonly _panel : vscode . WebviewPanel ;
47
- private readonly _extensionUri : vscode . Uri ;
48
- private readonly _extensionPath : string ;
49
- private _disposables : vscode . Disposable [ ] = [ ] ;
50
-
51
- public static createOrShow ( context : vscode . ExtensionContext ) {
52
- const { extensionUri, extensionPath } = context ;
53
- const column = vscode . window . activeTextEditor
54
- ? vscode . window . activeTextEditor . viewColumn
55
- : undefined ;
56
-
57
- // If we already have a panel, show it.
58
- if ( CatCodingPanel . currentPanel ) {
59
- CatCodingPanel . currentPanel . _panel . reveal ( column ) ;
60
- return ;
61
- }
62
-
63
- // Otherwise, create a new panel.
64
- const panel = vscode . window . createWebviewPanel (
65
- CatCodingPanel . viewType ,
66
- 'Cat Coding' ,
67
- column || vscode . ViewColumn . One ,
68
- {
69
- // Enable javascript in the webview
70
- enableScripts : true ,
71
-
72
- localResourceRoots : [ vscode . Uri . joinPath ( extensionUri , 'out' ) ]
73
- }
74
- ) ;
75
-
76
- CatCodingPanel . currentPanel = new CatCodingPanel ( panel , extensionUri , extensionPath ) ;
77
- }
78
-
79
- public static revive ( panel : vscode . WebviewPanel , extensionUri : vscode . Uri , extensionPath : string ) {
80
- CatCodingPanel . currentPanel = new CatCodingPanel ( panel , extensionUri , extensionPath ) ;
81
- }
82
-
83
- private constructor ( panel : vscode . WebviewPanel , extensionUri : vscode . Uri , extensionPath : string ) {
84
- this . _panel = panel ;
85
- this . _extensionUri = extensionUri ;
86
- this . _extensionPath = extensionPath ;
87
-
88
- // Set the webview's initial html content
89
- this . _update ( ) ;
90
-
91
- // Listen for when the panel is disposed
92
- // This happens when the user closes the panel or when the panel is closed programatically
93
- this . _panel . onDidDispose ( ( ) => this . dispose ( ) , null , this . _disposables ) ;
94
-
95
- // Update the content based on view changes
96
- this . _panel . onDidChangeViewState (
97
- e => {
98
- if ( this . _panel . visible ) {
99
- this . _update ( ) ;
100
- }
101
- } ,
102
- null ,
103
- this . _disposables
104
- ) ;
105
-
106
- // Handle messages from the webview
107
- this . _panel . webview . onDidReceiveMessage (
108
- message => {
109
- switch ( message . command ) {
110
- case 'alert' :
111
- vscode . window . showErrorMessage ( message . text ) ;
112
- return ;
113
- }
114
- } ,
115
- null ,
116
- this . _disposables
117
- ) ;
118
- }
119
-
120
- public doRefactor ( ) {
121
- // Send a message to the webview webview.
122
- // You can send any JSON serializable data.
123
- this . _panel . webview . postMessage ( { command : 'refactor' } ) ;
124
- }
125
-
126
- public dispose ( ) {
127
- CatCodingPanel . currentPanel = undefined ;
128
-
129
- // Clean up our resources
130
- this . _panel . dispose ( ) ;
131
-
132
- while ( this . _disposables . length ) {
133
- const x = this . _disposables . pop ( ) ;
134
- if ( x ) {
135
- x . dispose ( ) ;
136
- }
137
- }
138
- }
139
-
140
- private _update ( ) {
141
- const webview = this . _panel . webview ;
142
-
143
- // Vary the webview's content based on where it is located in the editor.
144
- switch ( this . _panel . viewColumn ) {
145
- case vscode . ViewColumn . Two :
146
- this . _updateForCat ( webview ) ;
147
- return ;
148
-
149
- case vscode . ViewColumn . Three :
150
- this . _updateForCat ( webview ) ;
151
- return ;
152
-
153
- case vscode . ViewColumn . One :
154
- default :
155
- this . _updateForCat ( webview ) ;
156
- return ;
157
- }
158
- }
159
-
160
- private _updateForCat ( webview : vscode . Webview ) {
161
- this . _panel . title = `Coding cat ${ Date . now ( ) } ` ;
162
- this . _panel . webview . html = this . _getHtmlForWebview ( webview ) ;
163
- }
164
-
165
- private _getHtmlForWebview ( webview : vscode . Webview ) {
166
- const reactAppPathOnDisk = vscode . Uri . file (
167
- path . join ( this . _extensionPath , "out/webviews/main.js" )
168
- ) ;
169
- const reactAppUri = reactAppPathOnDisk . with ( { scheme : "vscode-resource" } ) ;
170
- console . log ( reactAppUri ) ;
171
-
172
- return `<!DOCTYPE html>
39
+ /**
40
+ * Track the currently panel. Only allow a single panel to exist at a time.
41
+ */
42
+ public static currentPanel : CatCodingPanel | undefined ;
43
+
44
+ public static readonly viewType = 'catCoding' ;
45
+
46
+ private readonly _panel : vscode . WebviewPanel ;
47
+ private readonly _extensionUri : vscode . Uri ;
48
+ private readonly _extensionPath : string ;
49
+ private _disposables : vscode . Disposable [ ] = [ ] ;
50
+
51
+ public static createOrShow ( context : vscode . ExtensionContext ) {
52
+ const { extensionUri, extensionPath } = context ;
53
+ const column = vscode . window . activeTextEditor
54
+ ? vscode . window . activeTextEditor . viewColumn
55
+ : undefined ;
56
+
57
+ // If we already have a panel, show it.
58
+ if ( CatCodingPanel . currentPanel ) {
59
+ CatCodingPanel . currentPanel . _panel . reveal ( column ) ;
60
+ return ;
61
+ }
62
+
63
+ // Otherwise, create a new panel.
64
+ const panel = vscode . window . createWebviewPanel (
65
+ CatCodingPanel . viewType ,
66
+ 'Cat Coding' ,
67
+ column || vscode . ViewColumn . One ,
68
+ {
69
+ // Enable javascript in the webview
70
+ enableScripts : true ,
71
+
72
+ localResourceRoots : [ vscode . Uri . joinPath ( extensionUri , 'out' ) ]
73
+ }
74
+ ) ;
75
+
76
+ CatCodingPanel . currentPanel = new CatCodingPanel ( panel , extensionUri , extensionPath ) ;
77
+ }
78
+
79
+ public static revive ( panel : vscode . WebviewPanel , extensionUri : vscode . Uri , extensionPath : string ) {
80
+ CatCodingPanel . currentPanel = new CatCodingPanel ( panel , extensionUri , extensionPath ) ;
81
+ }
82
+
83
+ private constructor ( panel : vscode . WebviewPanel , extensionUri : vscode . Uri , extensionPath : string ) {
84
+ this . _panel = panel ;
85
+ this . _extensionUri = extensionUri ;
86
+ this . _extensionPath = extensionPath ;
87
+
88
+ // Set the webview's initial html content
89
+ this . _update ( ) ;
90
+
91
+ // Listen for when the panel is disposed
92
+ // This happens when the user closes the panel or when the panel is closed programatically
93
+ this . _panel . onDidDispose ( ( ) => this . dispose ( ) , null , this . _disposables ) ;
94
+
95
+ // Update the content based on view changes
96
+ this . _panel . onDidChangeViewState (
97
+ e => {
98
+ if ( this . _panel . visible ) {
99
+ this . _update ( ) ;
100
+ }
101
+ } ,
102
+ null ,
103
+ this . _disposables
104
+ ) ;
105
+
106
+ // Handle messages from the webview
107
+ this . _panel . webview . onDidReceiveMessage (
108
+ message => {
109
+ switch ( message . command ) {
110
+ case 'alert' :
111
+ vscode . window . showErrorMessage ( message . text ) ;
112
+ return ;
113
+ }
114
+ } ,
115
+ null ,
116
+ this . _disposables
117
+ ) ;
118
+ }
119
+
120
+ public doRefactor ( ) {
121
+ // Send a message to the webview webview.
122
+ // You can send any JSON serializable data.
123
+ this . _panel . webview . postMessage ( { command : 'refactor' } ) ;
124
+ }
125
+
126
+ public broadcast ( type : string , value : any ) {
127
+ this . _panel . webview . postMessage ( {
128
+ type,
129
+ value,
130
+ } ) ;
131
+ }
132
+
133
+ public dispose ( ) {
134
+ CatCodingPanel . currentPanel = undefined ;
135
+
136
+ // Clean up our resources
137
+ this . _panel . dispose ( ) ;
138
+
139
+ while ( this . _disposables . length ) {
140
+ const x = this . _disposables . pop ( ) ;
141
+ if ( x ) {
142
+ x . dispose ( ) ;
143
+ }
144
+ }
145
+ }
146
+
147
+ private _update ( ) {
148
+ const webview = this . _panel . webview ;
149
+
150
+ // Vary the webview's content based on where it is located in the editor.
151
+ switch ( this . _panel . viewColumn ) {
152
+ case vscode . ViewColumn . Two :
153
+ this . _updateForCat ( webview ) ;
154
+ return ;
155
+
156
+ case vscode . ViewColumn . Three :
157
+ this . _updateForCat ( webview ) ;
158
+ return ;
159
+
160
+ case vscode . ViewColumn . One :
161
+ default :
162
+ this . _updateForCat ( webview ) ;
163
+ return ;
164
+ }
165
+ }
166
+
167
+ private _updateForCat ( webview : vscode . Webview ) {
168
+ this . _panel . title = `Coding cat ${ Date . now ( ) } ` ;
169
+ this . _panel . webview . html = this . _getHtmlForWebview ( webview ) ;
170
+ }
171
+
172
+ private _getHtmlForWebview ( webview : vscode . Webview ) {
173
+ const appPathOnDisk = vscode . Uri . file (
174
+ path . join ( this . _extensionPath , "out/webviews/main.js" )
175
+ ) ;
176
+ const appUri = appPathOnDisk . with ( { scheme : "vscode-resource" } ) ;
177
+
178
+ return `<!DOCTYPE html>
173
179
<html lang="en">
174
180
<head>
175
181
<meta charset="UTF-8">
176
182
<meta name="viewport" content="width=device-width, initial-scale=1.0">
177
183
<title>Coding Cat</title>
178
184
179
185
<meta http-equiv="Content-Security-Policy"
180
- content="default-src 'none ';
186
+ content="default-src 'unsafe-inline ';
181
187
img-src https:;
182
- script-src 'unsafe-eval' 'unsafe-inline' vscode-resource:;
188
+ script-src 'unsafe-eval' 'unsafe-inline' vscode-resource:;
189
+ connect-src 'self' https:;
183
190
style-src vscode-resource: 'unsafe-inline';">
184
-
185
- <script>
186
- window.addEventListener('message', event => {
187
- const message = event.data; // The JSON data our extension sent
188
- console.log(message);
189
- });
190
- </script>
191
191
</head>
192
192
<body>
193
193
<div id="root"></div>
194
194
195
- <script src="${ reactAppUri } "></script>
195
+ <script src="${ appUri } "></script>
196
196
</body>
197
197
</html>` ;
198
- }
198
+ }
199
199
}
0 commit comments