-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.ts
More file actions
82 lines (73 loc) · 2 KB
/
Copy pathmain.ts
File metadata and controls
82 lines (73 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Plugin, debounce } from "obsidian";
import { Graph3DPluginSettings, DEFAULT_SETTINGS } from "./src/types";
import { Graph3DView, VIEW_TYPE_3D_GRAPH } from "./src/view";
import { Graph3DSettingsTab } from "./src/settings";
export default class Graph3DPlugin extends Plugin {
settings: Graph3DPluginSettings;
async onload() {
await this.loadSettings();
this.registerView(
VIEW_TYPE_3D_GRAPH,
(leaf) => new Graph3DView(leaf, this),
);
this.addSettingTab(new Graph3DSettingsTab(this.app, this));
this.addRibbonIcon("network", "Open 3d graph", () =>
this.activateView(),
);
this.addCommand({
id: "open-3d-graph-view",
name: "Open 3d graph",
callback: () => {
this.activateView();
},
});
const debouncedUpdate = debounce(
() => this.triggerLiveUpdate(),
300,
false,
);
this.registerEvent(this.app.vault.on("create", debouncedUpdate));
this.registerEvent(this.app.vault.on("delete", debouncedUpdate));
this.registerEvent(this.app.vault.on("modify", debouncedUpdate));
this.registerEvent(
this.app.metadataCache.on("resolve", debouncedUpdate),
);
}
triggerLiveUpdate() {
this.app.workspace
.getLeavesOfType(VIEW_TYPE_3D_GRAPH)
.forEach((leaf) => {
if (leaf.view instanceof Graph3DView) {
leaf.view.updateData();
}
});
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData(),
);
}
async saveSettings() {
await this.saveData(this.settings);
this.app.workspace
.getLeavesOfType(VIEW_TYPE_3D_GRAPH)
.forEach((leaf) => {
if (leaf.view instanceof Graph3DView) {
leaf.view.setGroupsDirty();
}
});
}
async activateView() {
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_3D_GRAPH);
if (leaves.length > 0) {
this.app.workspace.revealLeaf(leaves[0]);
return;
}
const leaf = this.app.workspace.getLeaf("tab");
await leaf.setViewState({ type: VIEW_TYPE_3D_GRAPH, active: true });
this.app.workspace.revealLeaf(leaf);
}
}