better-word-count/src/main.ts

107 lines
3 KiB
TypeScript
Raw Normal View History

import { MarkdownView, Plugin, TFile, addIcon, WorkspaceLeaf } from "obsidian";
import { BetterWordCountSettingsTab } from "./settings/settings-tab";
2021-07-07 12:34:39 +00:00
import { BetterWordCountSettings, DEFAULT_SETTINGS } from "./settings/settings";
2021-02-17 04:29:24 +00:00
import {
getWordCount,
getCharacterCount,
getSentenceCount,
getFilesCount,
} from "./stats";
import { StatusBar } from "./status/bar";
import { STATS_ICON, STATS_ICON_NAME, VIEW_TYPE_STATS } from "./constants";
import StatsView from "./view/view";
import { DataManager } from "./data/manager";
2021-07-07 12:34:39 +00:00
import { BarManager } from "./status/manager";
import type CodeMirror from "codemirror";
import { parse } from "./status/parse";
2020-11-09 13:33:44 +00:00
export default class BetterWordCount extends Plugin {
public statusBar: StatusBar;
2020-11-10 14:23:28 +00:00
public currentFile: TFile;
public settings: BetterWordCountSettings;
public view: StatsView;
public dataManager: DataManager;
2021-07-07 12:34:39 +00:00
public barManager: BarManager;
onunload(): void {
this.app.workspace
.getLeavesOfType(VIEW_TYPE_STATS)
.forEach((leaf) => leaf.detach());
}
2020-11-09 13:33:44 +00:00
async onload() {
2021-07-07 12:34:39 +00:00
this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData());
this.saveData(this.settings);
this.addSettingTab(new BetterWordCountSettingsTab(this.app, this));
2020-11-09 13:33:44 +00:00
let statusBarEl = this.addStatusBarItem();
this.statusBar = new StatusBar(statusBarEl);
2021-07-07 12:34:39 +00:00
this.dataManager = new DataManager(this.app.vault);
this.barManager = new BarManager(
this.statusBar,
this.settings,
this.app.vault
2020-11-09 13:33:44 +00:00
);
this.registerEvent(
2021-07-07 12:34:39 +00:00
this.app.workspace.on("active-leaf-change", this.activeLeafChange, this)
2020-11-09 13:33:44 +00:00
);
this.registerEvent(
this.app.vault.on(
"modify",
this.dataManager.onVaultModify,
this.dataManager
)
);
2021-07-07 12:34:39 +00:00
this.registerCodeMirror((cm: CodeMirror.Editor) => {
cm.on("cursorActivity", (cm: CodeMirror.Editor) =>
this.barManager.cursorActivity(cm)
);
});
addIcon(STATS_ICON_NAME, STATS_ICON);
this.addCommand({
id: "show-vault-stats-view",
2021-07-07 12:34:39 +00:00
name: "Open Statistics",
checkCallback: (checking: boolean) => {
if (checking) {
return this.app.workspace.getLeavesOfType("vault-stats").length === 0;
}
this.initLeaf();
},
});
this.registerView(
VIEW_TYPE_STATS,
(leaf: WorkspaceLeaf) => (this.view = new StatsView(leaf))
);
if (this.app.workspace.layoutReady) {
this.initLeaf();
}
2020-11-09 13:33:44 +00:00
}
2021-07-07 12:34:39 +00:00
activeLeafChange(leaf: WorkspaceLeaf) {
if (!(leaf.view.getViewType() === "markdown")) {
this.barManager.updateAltStatusBar();
}
}
2021-07-07 12:34:39 +00:00
async saveSettings(): Promise<void> {
await this.saveData(this.settings);
2020-11-09 13:33:44 +00:00
}
initLeaf(): void {
if (this.app.workspace.getLeavesOfType(VIEW_TYPE_STATS).length) {
return;
}
this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE_STATS,
});
}
2020-11-09 13:33:44 +00:00
}