2022-11-10 15:23:23 +00:00
|
|
|
import { Plugin } from "obsidian";
|
|
|
|
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";
|
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;
|
2021-07-03 19:22:50 +00:00
|
|
|
|
2022-02-24 13:02:26 +00:00
|
|
|
// onunload(): void {
|
|
|
|
// this.app.workspace
|
|
|
|
// .getLeavesOfType(VIEW_TYPE_STATS)
|
|
|
|
// .forEach((leaf) => leaf.detach());
|
|
|
|
// }
|
2020-11-09 13:33:44 +00:00
|
|
|
|
2022-11-08 04:45:18 +00:00
|
|
|
async onload() {
|
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);
|
|
|
|
}
|
2021-07-03 19:22:50 +00:00
|
|
|
|
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(() => {
|
|
|
|
//@ts-expect-error, not typed
|
|
|
|
const editorView = this.app.workspace.getMostRecentLeaf().view.editor
|
|
|
|
.cm as EditorView;
|
|
|
|
const editorPlug = editorView.plugin(editorPlugin);
|
|
|
|
editorPlug.addPlugin(this);
|
|
|
|
});
|
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
|
|
|
}
|