import * as path from "path" ; import * as vscode from "vscode" ; import * as fs from "fs" ; export function activate ( context ) { context . subscriptions . push ( vscode . commands . registerCommand ( "asciiflow.open" , ( ) => { Asciiflow2Panel . createOrShow ( context . extensionPath ) ; } ) ) ; if ( vscode . window . registerWebviewPanelSerializer ) { vscode . window . registerWebviewPanelSerializer ( Asciiflow2Panel . viewType , { async deserializeWebviewPanel ( webviewPanel , state ) { console . log ( `Got state: ${ state }` ) ; Asciiflow2Panel . revive ( webviewPanel , context . extensionPath ) ; } , } ) ; } } class Asciiflow2Panel { public static currentPanel : Asciiflow2Panel | undefined ; public static readonly viewType = "asciiflow2" ; private readonly _panel : vscode . WebviewPanel ; private readonly _resourcesPath : string ; private _disposables : vscode . Disposable [ ] = [ ] ; public static createOrShow ( extensionPath ) { const column = vscode . window . activeTextEditor ? vscode . window . activeTextEditor . viewColumn : undefined ; if ( Asciiflow2Panel . currentPanel ) { Asciiflow2Panel . currentPanel . _panel . reveal ( column ) ; return ; } const panel = vscode . window . createWebviewPanel ( Asciiflow2Panel . viewType , "ASCIIFlow Infinity" , column || vscode . ViewColumn . Two , { enableScripts : true , retainContextWhenHidden : true , } ) ; Asciiflow2Panel . currentPanel = new Asciiflow2Panel ( panel , extensionPath ) ; } public static revive ( panel , extensionPath ) { Asciiflow2Panel . currentPanel = new Asciiflow2Panel ( panel , extensionPath ) ; } private constructor ( panel , extensionPath ) { this . _panel = panel ; this . _resourcesPath = path . join ( extensionPath , "asciiflow2" ) ; this . _panel . webview . html = this . _getHtmlForWebview ( ) ; this . _panel . onDidDispose ( ( ) => this . dispose ( ) , null , this . _disposables ) ; } private _getHtmlForWebview ( ) { let html = fs . readFileSync ( path . join ( this . _resourcesPath , "index.html" ) , "utf-8" ) ; html = html . replace ( /(<link.+?href="|<script.+?src="|<img.+?src="|url\(")(.+?)"/g , ( m , $1 , $2 ) => { return ( $1 + vscode . Uri . file ( path . resolve ( this . _resourcesPath , $2 ) ) . with ( { scheme : "vscode-resource" } ) . toString ( ) + '"' ) ; } ) ; return html ; } public dispose ( ) { Asciiflow2Panel . currentPanel = undefined ; this . _panel . dispose ( ) ; while ( this . _disposables . length ) { const x = this . _disposables . pop ( ) ; if ( x ) { x . dispose ( ) ; } } } }