@@ -47,7 +47,9 @@ async function writeFile(targetUri: Uri, text: string) {
4747}
4848
4949class MarkmapEditor implements CustomTextEditorProvider {
50- constructor ( private context : ExtensionContext ) { }
50+ private webviewPanelMap = new Map < TextDocument , WebviewPanel > ( ) ;
51+
52+ constructor ( private context : ExtensionContext ) { }
5153
5254 private resolveAssetPath ( relPath : string ) {
5355 return Utils . joinPath ( this . context . extensionUri , relPath ) ;
@@ -61,6 +63,7 @@ class MarkmapEditor implements CustomTextEditorProvider {
6163 }
6264
6365 resolveCustomTextEditor ( document : TextDocument , webviewPanel : WebviewPanel ) {
66+ this . webviewPanelMap . set ( document , webviewPanel ) ;
6467 webviewPanel . webview . options = {
6568 enableScripts : true ,
6669 } ;
@@ -111,11 +114,16 @@ class MarkmapEditor implements CustomTextEditorProvider {
111114 if ( editor ?. document === document ) {
112115 webviewPanel . webview . postMessage ( {
113116 type : 'setCursor' ,
114- data : editor . selection . active . line ,
117+ data : {
118+ line : editor . selection . active . line ,
119+ autoExpand : globalOptions ?. autoExpand ,
120+ } ,
115121 } ) ;
116122 }
117123 } ;
118- let globalOptions : IMarkmapJSONOptions ;
124+ let globalOptions : IMarkmapJSONOptions & {
125+ autoExpand ?: boolean ;
126+ } ;
119127 let customCSS : string ;
120128 const updateOptions = ( ) => {
121129 const raw = workspace
@@ -185,11 +193,11 @@ class MarkmapEditor implements CustomTextEditorProvider {
185193 styles : [
186194 ...( customCSS
187195 ? [
188- {
189- type : 'style' ,
190- data : customCSS ,
191- } as CSSItem ,
192- ]
196+ {
197+ type : 'style' ,
198+ data : customCSS ,
199+ } as CSSItem ,
200+ ]
193201 : [ ] ) ,
194202 ] ,
195203 scripts : [
@@ -311,6 +319,16 @@ class MarkmapEditor implements CustomTextEditorProvider {
311319 ] ;
312320 webviewPanel . onDidDispose ( ( ) => {
313321 disposables . forEach ( ( disposable ) => disposable . dispose ( ) ) ;
322+ this . webviewPanelMap . delete ( document ) ;
323+ } ) ;
324+ }
325+
326+ toggleActiveNode ( document : TextDocument , recursive = false ) {
327+ const webviewPanel = this . webviewPanelMap . get ( document ) ;
328+ if ( ! webviewPanel ) return ;
329+ webviewPanel . webview . postMessage ( {
330+ type : 'toggleNode' ,
331+ data : recursive ,
314332 } ) ;
315333 }
316334}
@@ -327,6 +345,14 @@ export function activate(context: ExtensionContext) {
327345 ViewColumn . Beside ,
328346 ) ;
329347 } ) ,
348+ commands . registerCommand ( `${ PREFIX } .toggle` , ( ) => {
349+ const document = vscodeWindow . activeTextEditor ?. document ;
350+ if ( document ) markmapEditor . toggleActiveNode ( document ) ;
351+ } ) ,
352+ commands . registerCommand ( `${ PREFIX } .toggle-recursively` , ( ) => {
353+ const document = vscodeWindow . activeTextEditor ?. document ;
354+ if ( document ) markmapEditor . toggleActiveNode ( document , true ) ;
355+ } ) ,
330356 ) ;
331357 context . subscriptions . push (
332358 vscodeWindow . registerCustomEditorProvider ( VIEW_TYPE , markmapEditor , {
0 commit comments