@@ -5,18 +5,123 @@ import * as vscode from 'vscode';
55// This method is called when your extension is activated
66// Your extension is activated the very first time the command is executed
77export function activate ( context : vscode . ExtensionContext ) {
8+ console . log ( 'Superdesign extension is now active!' ) ;
89
910 // The command has been defined in the package.json file
1011 // Now provide the implementation of the command with registerCommand
1112 // The commandId parameter must match the command field in package.json
12- const disposable = vscode . commands . registerCommand ( 'superdesign.helloWorld' , ( ) => {
13+ const helloWorldDisposable = vscode . commands . registerCommand ( 'superdesign.helloWorld' , ( ) => {
1314 // The code you place here will be executed every time your command is executed
1415 // Display a message box to the user
1516 vscode . window . showInformationMessage ( 'Hello World from superdesign!' ) ;
1617 } ) ;
1718
18- context . subscriptions . push ( disposable ) ;
19+ // Register new design panel command
20+ const openPanelDisposable = vscode . commands . registerCommand ( 'superdesign.openDesignPanel' , ( ) => {
21+ SuperdesignPanel . createOrShow ( context . extensionUri ) ;
22+ } ) ;
23+
24+ context . subscriptions . push ( helloWorldDisposable , openPanelDisposable ) ;
1925}
2026
27+ class SuperdesignPanel {
28+ public static currentPanel : SuperdesignPanel | undefined ;
29+ public static readonly viewType = 'superdesignPanel' ;
30+
31+ private readonly _panel : vscode . WebviewPanel ;
32+ private readonly _extensionUri : vscode . Uri ;
33+ private _disposables : vscode . Disposable [ ] = [ ] ;
34+
35+ public static createOrShow ( extensionUri : vscode . Uri ) {
36+ const column = vscode . window . activeTextEditor ?. viewColumn ;
37+
38+ if ( SuperdesignPanel . currentPanel ) {
39+ SuperdesignPanel . currentPanel . _panel . reveal ( column ) ;
40+ return ;
41+ }
42+
43+ const panel = vscode . window . createWebviewPanel (
44+ SuperdesignPanel . viewType ,
45+ 'Superdesign' ,
46+ column || vscode . ViewColumn . One ,
47+ {
48+ enableScripts : true ,
49+ localResourceRoots : [ vscode . Uri . joinPath ( extensionUri , 'dist' ) ]
50+ }
51+ ) ;
52+
53+ SuperdesignPanel . currentPanel = new SuperdesignPanel ( panel , extensionUri ) ;
54+ }
55+
56+ private constructor ( panel : vscode . WebviewPanel , extensionUri : vscode . Uri ) {
57+ this . _panel = panel ;
58+ this . _extensionUri = extensionUri ;
59+
60+ this . _update ( ) ;
61+ this . _panel . onDidDispose ( ( ) => this . dispose ( ) , null , this . _disposables ) ;
62+
63+ // Handle messages from the webview
64+ this . _panel . webview . onDidReceiveMessage (
65+ message => {
66+ switch ( message . command ) {
67+ case 'exportDesign' :
68+ vscode . window . showInformationMessage ( 'Design system exported!' ) ;
69+ break ;
70+ }
71+ } ,
72+ null ,
73+ this . _disposables
74+ ) ;
75+ }
76+
77+ public dispose ( ) {
78+ SuperdesignPanel . currentPanel = undefined ;
79+ this . _panel . dispose ( ) ;
80+ while ( this . _disposables . length ) {
81+ const x = this . _disposables . pop ( ) ;
82+ if ( x ) {
83+ x . dispose ( ) ;
84+ }
85+ }
86+ }
87+
88+ private _update ( ) {
89+ const webview = this . _panel . webview ;
90+ this . _panel . webview . html = this . _getHtmlForWebview ( webview ) ;
91+ }
92+
93+ private _getHtmlForWebview ( webview : vscode . Webview ) {
94+ const scriptUri = webview . asWebviewUri (
95+ vscode . Uri . joinPath ( this . _extensionUri , 'dist' , 'webview.js' )
96+ ) ;
97+
98+ const nonce = getNonce ( ) ;
99+
100+ return `<!DOCTYPE html>
101+ <html lang="en">
102+ <head>
103+ <meta charset="UTF-8">
104+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${ webview . cspSource } 'unsafe-inline'; script-src 'nonce-${ nonce } ';">
105+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
106+ <title>Superdesign</title>
107+ </head>
108+ <body>
109+ <div id="root"></div>
110+ <script nonce="${ nonce } " src="${ scriptUri } "></script>
111+ </body>
112+ </html>` ;
113+ }
114+ }
115+
116+ function getNonce ( ) {
117+ let text = '' ;
118+ const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
119+ for ( let i = 0 ; i < 32 ; i ++ ) {
120+ text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) ) ;
121+ }
122+ return text ;
123+ }
124+
125+
21126// This method is called when your extension is deactivated
22127export function deactivate ( ) { }
0 commit comments