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);
|
|
|
|
this.statusBar.displayText("Hiya!");
|
|
|
|
|
|
|
|
// Handle the Editor Plugin
|
|
|
|
this.registerEditorExtension(editorPlugin);
|
|
|
|
//@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);
|
|
|
|
}
|
2021-07-03 19:22:50 +00:00
|
|
|
|
2022-11-10 15:23:23 +00:00
|
|
|
// createCMExtension() {
|
|
|
|
// const cmStateField = StateField.define<DecorationSet>({
|
|
|
|
// create() {
|
|
|
|
// return Decoration.none;
|
|
|
|
// },
|
|
|
|
// update(effects: DecorationSet, tr: Transaction) {
|
|
|
|
// let text = "";
|
|
|
|
// if (tr.isUserEvent("select")) {
|
|
|
|
// const selection = tr.newSelection.main;
|
|
|
|
// const textIter = tr.newDoc.iterRange(selection.from, selection.to);
|
|
|
|
// while (!textIter.done) {
|
|
|
|
// text = text + textIter.next().value;
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// const textIter = tr.newDoc.iter();
|
|
|
|
// while (!textIter.done) {
|
|
|
|
// text = text + textIter.next().value;
|
|
|
|
// }
|
|
|
|
// if (tr.docChanged && BetterWordCount.statsManager) {
|
|
|
|
// BetterWordCount.statsManager.change(text);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// BetterWordCount.updateStatusBar(text);
|
|
|
|
//
|
|
|
|
// return effects;
|
|
|
|
// },
|
|
|
|
// provide: (f: any) => EditorView.decorations.from(f),
|
2021-07-08 20:20:38 +00:00
|
|
|
// });
|
2022-11-10 15:23:23 +00:00
|
|
|
//
|
|
|
|
// this.registerEditorExtension(cmStateField);
|
2021-07-08 20:20:38 +00:00
|
|
|
// }
|
2020-11-09 13:33:44 +00:00
|
|
|
}
|