-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.ts
93 lines (81 loc) · 2.95 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { MarkdownView, Plugin, WorkspaceLeaf } from "obsidian";
import BetterWordCountSettingsTab from "./settings/SettingsTab";
import StatsManager from "./stats/StatsManager";
import StatusBar from "./status/StatusBar";
import type { EditorView } from "@codemirror/view";
import {
settingsChanged,
pluginField,
sectionWordCountEditorPlugin,
statusBarEditorPlugin,
} from "./editor/EditorPlugin";
import { BetterWordCountSettings, DEFAULT_SETTINGS } from "src/settings/Settings";
import { settingsStore } from "./utils/SvelteStores";
import BetterWordCountApi from "src/api/api";
import { handleFileMenu } from "./utils/FileMenu";
export default class BetterWordCount extends Plugin {
public settings: BetterWordCountSettings;
public statusBar: StatusBar;
public statsManager: StatsManager;
public api: BetterWordCountApi = new BetterWordCountApi(this);
async onunload(): Promise<void> {
this.statsManager = null;
this.statusBar = null;
}
async onload() {
// Settings Store
// this.register(
// settingsStore.subscribe((value) => {
// this.settings = value;
// })
// );
// Handle Settings
this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData());
this.addSettingTab(new BetterWordCountSettingsTab(this.app, this));
// Handle Statistics
if (this.settings.collectStats) {
this.statsManager = new StatsManager(this.app.vault, this.app.workspace, this);
}
// Handle Status Bar
let statusBarEl = this.addStatusBarItem();
this.statusBar = new StatusBar(statusBarEl, this);
// Handle the Editor Plugins
this.registerEditorExtension([pluginField.init(() => this), statusBarEditorPlugin, sectionWordCountEditorPlugin]);
this.registerEvent(
this.app.workspace.on("active-leaf-change", async (leaf: WorkspaceLeaf) => {
if (leaf.view.getViewType() !== "markdown") {
this.statusBar.updateAltBar();
}
if (!this.settings.collectStats) return;
await this.statsManager.recalcTotals();
})
);
this.registerEvent(
this.app.vault.on("delete", async () => {
if (!this.settings.collectStats) return;
await this.statsManager.recalcTotals();
})
);
// Register a new action for right clicking on folders
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file, source) => {
handleFileMenu(menu, file, source, this);
})
);
}
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
}
onDisplaySectionCountsChange() {
this.app.workspace.getLeavesOfType("markdown").forEach((leaf) => {
if (leaf?.view instanceof MarkdownView) {
const cm = (leaf.view.editor as any).cm as EditorView;
if (cm.dispatch) {
cm.dispatch({
effects: [settingsChanged.of()],
});
}
}
});
}
}