-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.ts
71 lines (59 loc) · 1.79 KB
/
main.ts
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
import { Plugin, WorkspaceLeaf } from "obsidian";
import {
type CardsViewSettings,
CardsViewSettingsTab,
DEFAULT_SETTINGS,
} from "./settings";
import { CardsViewPluginView, VIEW_TYPE } from "./view";
import store from "./components/store";
export default class CardsViewPlugin extends Plugin {
settings: CardsViewSettings = Object.assign({}, DEFAULT_SETTINGS);
async onload() {
this.settings = Object.assign(this.settings, await this.loadData());
store.settings.subscribe(async () => await this.saveSettings());
store.app.set(this.app);
store.settings.set(this.settings);
store.appCache.set(this.app.metadataCache);
this.registerEvent(
this.app.metadataCache.on("resolved", async () =>
store.appCache.update(() => this.app.metadataCache),
),
);
this.addSettingTab(new CardsViewSettingsTab(this.app, this));
this.addRibbonIcon("align-start-horizontal", "Card view", () => {
this.activateView();
});
this.addCommand({
id: "cards-view-plugin",
name: "Open card view",
callback: () => {
this.activateView();
},
});
this.registerView(
VIEW_TYPE,
(leaf) => new CardsViewPluginView(this.settings, leaf),
);
this.app.workspace.onLayoutReady(() => {
if (this.settings.launchOnStart) {
this.activateView();
}
});
}
onunload() {}
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE);
if (leaves.length) {
leaf = leaves[0];
} else {
leaf = workspace.getLeaf("tab");
}
await leaf.setViewState({ type: VIEW_TYPE, active: true });
store.viewIsVisible.set(true);
}
async saveSettings() {
await this.saveData(this.settings);
}
}