@@ -25,6 +25,7 @@ export default class DiscourseGraphPlugin extends Plugin {
2525 private styleElement : HTMLStyleElement | null = null ;
2626 private tagNodeHandler : TagNodeHandler | null = null ;
2727 private currentViewActions : { leaf : WorkspaceLeaf ; action : any } [ ] = [ ] ;
28+ private pendingCanvasSwitches = new Set < string > ( ) ;
2829
2930 async onload ( ) {
3031 await this . loadSettings ( ) ;
@@ -40,26 +41,38 @@ export default class DiscourseGraphPlugin extends Plugin {
4041 if ( ! leaf ) return ;
4142
4243 const view = leaf . view ;
43- if ( ! ( view instanceof MarkdownView ) ) return ;
44-
45- const file = view . file ;
46- if ( ! file ) return ;
47-
48- const cache = this . app . metadataCache . getFileCache ( file ) ;
49- if ( cache ?. frontmatter ?. [ FRONTMATTER_KEY ] ) {
50- // Add new action and track it
51- const action = view . addAction (
52- "layout" ,
53- "View as canvas" ,
54- async ( ) => {
55- await leaf . setViewState ( {
44+ const file = view instanceof MarkdownView ? view . file : null ;
45+
46+ if ( file ) {
47+ const cache = this . app . metadataCache . getFileCache ( file ) ;
48+ const isCanvasFile = ! ! cache ?. frontmatter ?. [ FRONTMATTER_KEY ] ;
49+
50+ // If this file is pending a switch, switch it now
51+ if ( this . pendingCanvasSwitches . has ( file . path ) && isCanvasFile ) {
52+ if ( view . getViewType ( ) !== VIEW_TYPE_TLDRAW_DG_PREVIEW ) {
53+ void leaf . setViewState ( {
5654 type : VIEW_TYPE_TLDRAW_DG_PREVIEW ,
5755 state : view . getState ( ) ,
5856 } ) ;
59- } ,
60- ) ;
57+ }
58+ this . pendingCanvasSwitches . delete ( file . path ) ;
59+ return ;
60+ }
61+
62+ if ( view instanceof MarkdownView && isCanvasFile ) {
63+ const action = view . addAction (
64+ "layout" ,
65+ "View as canvas" ,
66+ async ( ) => {
67+ await leaf . setViewState ( {
68+ type : VIEW_TYPE_TLDRAW_DG_PREVIEW ,
69+ state : view . getState ( ) ,
70+ } ) ;
71+ } ,
72+ ) ;
6173
62- this . currentViewActions . push ( { leaf, action } ) ;
74+ this . currentViewActions . push ( { leaf, action } ) ;
75+ }
6376 }
6477 } ,
6578 ) ,
@@ -71,14 +84,9 @@ export default class DiscourseGraphPlugin extends Plugin {
7184
7285 const cache = this . app . metadataCache . getFileCache ( file ) ;
7386 if ( cache ?. frontmatter ?. [ FRONTMATTER_KEY ] ) {
74- const leaf =
75- this . app . workspace . getActiveViewOfType ( MarkdownView ) ?. leaf ;
76- if ( leaf ) {
77- void leaf . setViewState ( {
78- type : VIEW_TYPE_TLDRAW_DG_PREVIEW ,
79- state : leaf . view . getState ( ) ,
80- } ) ;
81- }
87+ // Mark this file as needing a switch
88+ // The actual switch will happen in active-leaf-change when the view is ready
89+ this . pendingCanvasSwitches . add ( file . path ) ;
8290 }
8391 } ) ,
8492 ) ;
0 commit comments