Skip to content

Commit 373aeaf

Browse files
authored
[ENG-606] Open the file as canvas view right when open (#573)
* open the file as canvas view right when open * sm clean up
1 parent b29a7db commit 373aeaf

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

apps/obsidian/src/index.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default class DiscourseGraphPlugin extends Plugin {
2828
private styleElement: HTMLStyleElement | null = null;
2929
private tagNodeHandler: TagNodeHandler | null = null;
3030
private currentViewActions: { leaf: WorkspaceLeaf; action: any }[] = [];
31+
private pendingCanvasSwitches = new Set<string>();
3132

3233
async onload() {
3334
await this.loadSettings();
@@ -43,26 +44,37 @@ export default class DiscourseGraphPlugin extends Plugin {
4344
if (!leaf) return;
4445

4546
const view = leaf.view;
46-
if (!(view instanceof MarkdownView)) return;
47-
48-
const file = view.file;
49-
if (!file) return;
50-
51-
const cache = this.app.metadataCache.getFileCache(file);
52-
if (cache?.frontmatter?.[FRONTMATTER_KEY]) {
53-
// Add new action and track it
54-
const action = view.addAction(
55-
"layout",
56-
"View as canvas",
57-
async () => {
58-
await leaf.setViewState({
47+
const file = view instanceof MarkdownView ? view.file : null;
48+
49+
if (file) {
50+
const cache = this.app.metadataCache.getFileCache(file);
51+
const isCanvasFile = !!cache?.frontmatter?.[FRONTMATTER_KEY];
52+
53+
if (this.pendingCanvasSwitches.has(file.path)) {
54+
if (view.getViewType() !== VIEW_TYPE_TLDRAW_DG_PREVIEW) {
55+
void leaf.setViewState({
5956
type: VIEW_TYPE_TLDRAW_DG_PREVIEW,
6057
state: view.getState(),
6158
});
62-
},
63-
);
59+
}
60+
this.pendingCanvasSwitches.delete(file.path);
61+
return;
62+
}
63+
64+
if (view instanceof MarkdownView && isCanvasFile) {
65+
const action = view.addAction(
66+
"layout",
67+
"View as canvas",
68+
async () => {
69+
await leaf.setViewState({
70+
type: VIEW_TYPE_TLDRAW_DG_PREVIEW,
71+
state: view.getState(),
72+
});
73+
},
74+
);
6475

65-
this.currentViewActions.push({ leaf, action });
76+
this.currentViewActions.push({ leaf, action });
77+
}
6678
}
6779
},
6880
),
@@ -74,14 +86,7 @@ export default class DiscourseGraphPlugin extends Plugin {
7486

7587
const cache = this.app.metadataCache.getFileCache(file);
7688
if (cache?.frontmatter?.[FRONTMATTER_KEY]) {
77-
const leaf =
78-
this.app.workspace.getActiveViewOfType(MarkdownView)?.leaf;
79-
if (leaf) {
80-
void leaf.setViewState({
81-
type: VIEW_TYPE_TLDRAW_DG_PREVIEW,
82-
state: leaf.view.getState(),
83-
});
84-
}
89+
this.pendingCanvasSwitches.add(file.path);
8590
}
8691
}),
8792
);

0 commit comments

Comments
 (0)