better-word-count/src/main.ts

90 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-11-15 20:39:34 +00:00
import { Plugin, TFile, WorkspaceLeaf } from "obsidian";
2022-11-10 15:23:23 +00:00
import BetterWordCountSettingsTab from "./settings/SettingsTab";
import StatsManager from "./stats/StatsManager";
import StatusBar from "./status/StatusBar";
import type { EditorView } from "@codemirror/view";
import { editorPlugin } from "./editor/EditorPlugin";
2022-11-08 04:45:18 +00:00
import {
BetterWordCountSettings,
DEFAULT_SETTINGS,
} from "src/settings/Settings";
2022-11-16 09:48:56 +00:00
import { settingsStore } from "./utils/SvelteStores";
2020-11-09 13:33:44 +00:00
export default class BetterWordCount extends Plugin {
2022-11-08 04:45:18 +00:00
public settings: BetterWordCountSettings;
2022-11-10 15:23:23 +00:00
public statusBar: StatusBar;
public statsManager: StatsManager;
2022-11-15 20:39:34 +00:00
async onunload(): Promise<void> {
this.statsManager = null;
this.statusBar = null;
}
2020-11-09 13:33:44 +00:00
2022-11-08 04:45:18 +00:00
async onload() {
2022-11-16 09:48:56 +00:00
// Settings Store
2022-12-06 15:12:59 +00:00
// this.register(
// settingsStore.subscribe((value) => {
// this.settings = value;
// })
// );
2022-11-10 15:23:23 +00:00
// Handle Settings
2022-11-08 04:45:18 +00:00
this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData());
this.addSettingTab(new BetterWordCountSettingsTab(this.app, this));
2022-02-24 13:02:26 +00:00
2022-11-10 15:23:23 +00:00
// Handle Statistics
if (this.settings.collectStats) {
this.statsManager = new StatsManager(this.app.vault, this.app.workspace);
}
2022-11-10 15:23:23 +00:00
// Handle Status Bar
let statusBarEl = this.addStatusBarItem();
this.statusBar = new StatusBar(statusBarEl, this);
// Handle the Editor Plugin
this.registerEditorExtension(editorPlugin);
2022-11-15 17:21:08 +00:00
this.app.workspace.onLayoutReady(() => {
2022-11-15 20:39:34 +00:00
this.giveEditorPlugin(this.app.workspace.getMostRecentLeaf());
});
this.registerEvent(
this.app.workspace.on(
"active-leaf-change",
async (leaf: WorkspaceLeaf) => {
this.giveEditorPlugin(leaf);
2022-12-06 15:12:59 +00:00
if (leaf.view.getViewType() !== "markdown") {
this.statusBar.updateAltBar();
}
if (!this.settings.collectStats) return;
await this.statsManager.recalcTotals();
2022-11-15 20:39:34 +00:00
}
)
);
this.registerEvent(
this.app.vault.on("delete", async () => {
2022-12-06 15:12:59 +00:00
if (!this.settings.collectStats) return;
2022-11-15 20:39:34 +00:00
await this.statsManager.recalcTotals();
})
);
}
giveEditorPlugin(leaf: WorkspaceLeaf): void {
//@ts-expect-error, not typed
const editor = leaf.view.editor;
if (editor) {
const editorView = editor.cm as EditorView;
2022-11-15 17:21:08 +00:00
const editorPlug = editorView.plugin(editorPlugin);
editorPlug.addPlugin(this);
2022-11-15 20:39:34 +00:00
//@ts-expect-error, not typed
const data: string = leaf.view.data;
this.statusBar.updateStatusBar(data);
}
2020-11-09 13:33:44 +00:00
}
2022-11-08 04:45:18 +00:00
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
}
2020-11-09 13:33:44 +00:00
}