better-word-count/src/main.ts

115 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-07-07 17:00:11 +00:00
import { 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";
import { StatusBar } from "./status/bar";
import { STATS_ICON, STATS_ICON_NAME, VIEW_TYPE_STATS } from "./constants";
2021-07-08 20:20:38 +00:00
// 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";
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;
2021-07-08 20:20:38 +00:00
// 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.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.barManager = new BarManager(
this.statusBar,
this.settings,
2021-07-07 17:00:11 +00:00
this.app.vault,
this.app.metadataCache
2020-11-09 13:33:44 +00:00
);
2021-07-08 20:20:38 +00:00
if (this.settings.collectStats) {
this.dataManager = new DataManager(
this.app.vault,
this.app.metadataCache
);
}
2020-11-09 13:33:44 +00:00
this.registerEvent(
2021-07-08 20:20:38 +00:00
this.app.workspace.on("active-leaf-change", this.activeLeafChange, this)
);
2021-07-07 12:34:39 +00:00
this.registerCodeMirror((cm: CodeMirror.Editor) => {
cm.on("cursorActivity", (cm: CodeMirror.Editor) =>
this.barManager.cursorActivity(cm)
);
});
2021-07-08 20:20:38 +00:00
if (this.settings.collectStats) {
this.registerEvent(
this.app.workspace.on(
"quick-preview",
this.dataManager.change,
this.dataManager
)
);
2021-07-08 20:20:38 +00:00
this.registerInterval(
window.setInterval(() => {
this.dataManager.setTotalStats();
}, 1000 * 60)
);
}
2021-07-08 20:20:38 +00:00
// addIcon(STATS_ICON_NAME, STATS_ICON);
// this.addCommand({
// id: "show-vault-stats-view",
// 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();
// } else {
// this.app.workspace.onLayoutReady(() => 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
}
2021-07-08 20:20:38 +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
}