2021-07-03 19:22:50 +00:00
|
|
|
import { MarkdownView, Plugin, TFile, addIcon, WorkspaceLeaf } from "obsidian";
|
2021-01-19 20:39:10 +00:00
|
|
|
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";
|
2021-07-03 19:22:50 +00:00
|
|
|
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;
|
2021-01-19 20:39:10 +00:00
|
|
|
public settings: BetterWordCountSettings;
|
2021-07-03 19:22:50 +00:00
|
|
|
public view: StatsView;
|
|
|
|
public dataManager: DataManager;
|
2021-07-07 12:34:39 +00:00
|
|
|
public barManager: BarManager;
|
2021-07-03 19:22:50 +00:00
|
|
|
|
|
|
|
onunload(): void {
|
|
|
|
this.app.workspace
|
|
|
|
.getLeavesOfType(VIEW_TYPE_STATS)
|
|
|
|
.forEach((leaf) => leaf.detach());
|
|
|
|
}
|
2020-11-09 13:33:44 +00:00
|
|
|
|
2021-01-19 20:39:10 +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));
|
2021-07-03 19:22:50 +00:00
|
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-07-03 19:22:50 +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);
|
|
|
|
|
2021-07-03 19:22:50 +00:00
|
|
|
this.addCommand({
|
|
|
|
id: "show-vault-stats-view",
|
2021-07-07 12:34:39 +00:00
|
|
|
name: "Open Statistics",
|
2021-07-03 19:22:50 +00:00
|
|
|
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-04-02 21:24:17 +00:00
|
|
|
}
|
2021-01-19 22:41:11 +00:00
|
|
|
}
|
|
|
|
|
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-03 19:22:50 +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
|
|
|
}
|