@@ -211,6 +211,8 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
211211 ) : Promise < void > {
212212 this . _mpqDataMap [ document . uri . toString ( ) ] = new MPQData ( ) ;
213213 await this . initWebview ( document , webviewPanel ) ;
214+ this . initWebviewPanel ( document , webviewPanel ) ;
215+ this . updateWebview ( document , webviewPanel . webview ) ;
214216 }
215217
216218 /**
@@ -269,6 +271,72 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
269271 //TODO process messages
270272 }
271273
274+ private initWebviewPanel (
275+ document : vscode . TextDocument ,
276+ webviewPanel : vscode . WebviewPanel
277+ ) : void {
278+ vscode . commands . executeCommand (
279+ "setContext" ,
280+ MPQEditorProvider . viewType ,
281+ true
282+ ) ;
283+
284+ const changeDocumentSubscription = vscode . workspace . onDidChangeTextDocument (
285+ ( e ) => {
286+ if (
287+ e . contentChanges . length > 0 &&
288+ e . document . uri . toString ( ) === document . uri . toString ( )
289+ ) {
290+ this . updateWebview ( document , webviewPanel . webview ) ;
291+ }
292+ }
293+ ) ;
294+
295+ webviewPanel . onDidChangeViewState (
296+ ( ) => {
297+ vscode . commands . executeCommand (
298+ "setContext" ,
299+ MPQEditorProvider . viewType ,
300+ webviewPanel . visible
301+ ) ;
302+ } ,
303+ null ,
304+ this . _disposables
305+ ) ;
306+
307+ webviewPanel . onDidDispose ( ( ) => {
308+ changeDocumentSubscription . dispose ( ) ;
309+ while ( this . _disposables . length ) {
310+ const x = this . _disposables . pop ( ) ;
311+ if ( x ) {
312+ x . dispose ( ) ;
313+ }
314+ }
315+ vscode . commands . executeCommand (
316+ "setContext" ,
317+ MPQEditorProvider . viewType ,
318+ false
319+ ) ;
320+ } ) ;
321+ }
322+
323+ /**
324+ * @brief Update webview with document
325+ */
326+ private updateWebview (
327+ document : vscode . TextDocument ,
328+ webview : vscode . Webview
329+ ) : void {
330+ this . _mpqDataMap [ document . uri . toString ( ) ] . setWithString ( document . getText ( ) ) ;
331+ const content = JSON . parse ( document . getText ( ) ) ;
332+ if ( content !== undefined ) {
333+ webview . postMessage ( {
334+ type : "displayMPQ" ,
335+ content : content ,
336+ } ) ;
337+ }
338+ }
339+
272340 /**
273341 * @brief Get path of the parent .circle file
274342 */
0 commit comments