Fixes
This commit is contained in:
parent
8b7630a357
commit
88eea5ad67
4 changed files with 75 additions and 23 deletions
|
@ -33,7 +33,7 @@ class EditorPlugin implements PluginValue {
|
||||||
while (!textIter.done) {
|
while (!textIter.done) {
|
||||||
text = text + textIter.next().value;
|
text = text + textIter.next().value;
|
||||||
}
|
}
|
||||||
this.plugin.statusBar.updateStatusBar(text);
|
this.plugin.statusBar.debounceStatusBarUpdate(text);
|
||||||
} else if (
|
} else if (
|
||||||
tr.isUserEvent("input") ||
|
tr.isUserEvent("input") ||
|
||||||
tr.isUserEvent("delete") ||
|
tr.isUserEvent("delete") ||
|
||||||
|
@ -48,9 +48,9 @@ class EditorPlugin implements PluginValue {
|
||||||
text = text + textIter.next().value;
|
text = text + textIter.next().value;
|
||||||
}
|
}
|
||||||
if (tr.docChanged && this.plugin.statsManager) {
|
if (tr.docChanged && this.plugin.statsManager) {
|
||||||
this.plugin.statsManager.change(text);
|
this.plugin.statsManager.debounceChange(text);
|
||||||
}
|
}
|
||||||
this.plugin.statusBar.updateStatusBar(text);
|
this.plugin.statusBar.debounceStatusBarUpdate(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
44
src/main.ts
44
src/main.ts
|
@ -1,4 +1,4 @@
|
||||||
import { Plugin } from "obsidian";
|
import { Plugin, TFile, WorkspaceLeaf } from "obsidian";
|
||||||
import BetterWordCountSettingsTab from "./settings/SettingsTab";
|
import BetterWordCountSettingsTab from "./settings/SettingsTab";
|
||||||
import StatsManager from "./stats/StatsManager";
|
import StatsManager from "./stats/StatsManager";
|
||||||
import StatusBar from "./status/StatusBar";
|
import StatusBar from "./status/StatusBar";
|
||||||
|
@ -14,11 +14,10 @@ export default class BetterWordCount extends Plugin {
|
||||||
public statusBar: StatusBar;
|
public statusBar: StatusBar;
|
||||||
public statsManager: StatsManager;
|
public statsManager: StatsManager;
|
||||||
|
|
||||||
// onunload(): void {
|
async onunload(): Promise<void> {
|
||||||
// this.app.workspace
|
this.statsManager = null;
|
||||||
// .getLeavesOfType(VIEW_TYPE_STATS)
|
this.statusBar = null;
|
||||||
// .forEach((leaf) => leaf.detach());
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
async onload() {
|
async onload() {
|
||||||
// Handle Settings
|
// Handle Settings
|
||||||
|
@ -38,12 +37,37 @@ export default class BetterWordCount extends Plugin {
|
||||||
this.registerEditorExtension(editorPlugin);
|
this.registerEditorExtension(editorPlugin);
|
||||||
|
|
||||||
this.app.workspace.onLayoutReady(() => {
|
this.app.workspace.onLayoutReady(() => {
|
||||||
//@ts-expect-error, not typed
|
this.giveEditorPlugin(this.app.workspace.getMostRecentLeaf());
|
||||||
const editorView = this.app.workspace.getMostRecentLeaf().view.editor
|
});
|
||||||
.cm as EditorView;
|
|
||||||
|
this.registerEvent(
|
||||||
|
this.app.workspace.on(
|
||||||
|
"active-leaf-change",
|
||||||
|
async (leaf: WorkspaceLeaf) => {
|
||||||
|
await this.statsManager.recalcTotals();
|
||||||
|
this.giveEditorPlugin(leaf);
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.registerEvent(
|
||||||
|
this.app.vault.on("delete", async () => {
|
||||||
|
await this.statsManager.recalcTotals();
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
giveEditorPlugin(leaf: WorkspaceLeaf): void {
|
||||||
|
//@ts-expect-error, not typed
|
||||||
|
const editor = leaf.view.editor;
|
||||||
|
if (editor) {
|
||||||
|
const editorView = editor.cm as EditorView;
|
||||||
const editorPlug = editorView.plugin(editorPlugin);
|
const editorPlug = editorView.plugin(editorPlugin);
|
||||||
editorPlug.addPlugin(this);
|
editorPlug.addPlugin(this);
|
||||||
});
|
//@ts-expect-error, not typed
|
||||||
|
const data: string = leaf.view.data;
|
||||||
|
this.statusBar.updateStatusBar(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveSettings(): Promise<void> {
|
async saveSettings(): Promise<void> {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import type { Vault, TFile, Workspace } from "obsidian";
|
import { debounce, Debouncer, TFile, Vault, Workspace } from "obsidian";
|
||||||
import { STATS_FILE } from "../constants";
|
import { STATS_FILE } from "../constants";
|
||||||
import type {
|
import type {
|
||||||
Day,
|
Day,
|
||||||
|
@ -16,11 +16,12 @@ export default class StatsManager {
|
||||||
private workspace: Workspace;
|
private workspace: Workspace;
|
||||||
private vaultStats: VaultStatistics;
|
private vaultStats: VaultStatistics;
|
||||||
private today: string;
|
private today: string;
|
||||||
// public debounceChange: Debouncer<[file: TFile, data: string]>;
|
public debounceChange;
|
||||||
|
|
||||||
constructor(vault: Vault, workspace: Workspace) {
|
constructor(vault: Vault, workspace: Workspace) {
|
||||||
this.vault = vault;
|
this.vault = vault;
|
||||||
this.workspace = workspace;
|
this.workspace = workspace;
|
||||||
|
this.debounceChange = debounce((text:string) => this.change(text), 50, false)
|
||||||
|
|
||||||
this.vault.adapter.exists(STATS_FILE).then(async (exists) => {
|
this.vault.adapter.exists(STATS_FILE).then(async (exists) => {
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
|
@ -52,7 +53,6 @@ export default class StatsManager {
|
||||||
const totalWords = await this.calcTotalWords();
|
const totalWords = await this.calcTotalWords();
|
||||||
const totalCharacters = await this.calcTotalCharacters();
|
const totalCharacters = await this.calcTotalCharacters();
|
||||||
const totalSentences = await this.calcTotalSentences();
|
const totalSentences = await this.calcTotalSentences();
|
||||||
const totalFiles = this.getTotalFiles();
|
|
||||||
|
|
||||||
const newDay: Day = {
|
const newDay: Day = {
|
||||||
words: 0,
|
words: 0,
|
||||||
|
@ -133,6 +133,22 @@ export default class StatsManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async recalcTotals() {
|
||||||
|
if (!this.vaultStats) return;
|
||||||
|
if (
|
||||||
|
this.vaultStats.history.hasOwnProperty(this.today) &&
|
||||||
|
this.today === moment().format("YYYY-MM-DD")
|
||||||
|
) {
|
||||||
|
const todayHist: Day = this.vaultStats.history[this.today];
|
||||||
|
todayHist.totalWords = await this.calcTotalWords();
|
||||||
|
todayHist.totalCharacters = await this.calcTotalCharacters()
|
||||||
|
todayHist.totalSentences = await this.calcTotalSentences()
|
||||||
|
this.update();
|
||||||
|
} else {
|
||||||
|
this.updateToday();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async calcTotalWords(): Promise<number> {
|
private async calcTotalWords(): Promise<number> {
|
||||||
let words = 0;
|
let words = 0;
|
||||||
|
|
||||||
|
@ -176,15 +192,18 @@ export default class StatsManager {
|
||||||
return this.vault.getMarkdownFiles().length;
|
return this.vault.getMarkdownFiles().length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTotalWords(): number {
|
public async getTotalWords(): Promise<number> {
|
||||||
|
if (!this.vaultStats) return await this.calcTotalWords();
|
||||||
return this.vaultStats.history[this.today].totalWords;
|
return this.vaultStats.history[this.today].totalWords;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTotalCharacters(): number {
|
public async getTotalCharacters(): Promise<number> {
|
||||||
|
if (!this.vaultStats) return await this.calcTotalCharacters();
|
||||||
return this.vaultStats.history[this.today].totalCharacters;
|
return this.vaultStats.history[this.today].totalCharacters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTotalSentences(): number {
|
public async getTotalSentences(): Promise<number> {
|
||||||
|
if (!this.vaultStats) return await this.calcTotalSentences();
|
||||||
return this.vaultStats.history[this.today].totalSentences;
|
return this.vaultStats.history[this.today].totalSentences;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,14 +5,21 @@ import {
|
||||||
getCharacterCount,
|
getCharacterCount,
|
||||||
getSentenceCount,
|
getSentenceCount,
|
||||||
} from "src/utils/StatUtils";
|
} from "src/utils/StatUtils";
|
||||||
|
import { debounce } from "obsidian";
|
||||||
|
|
||||||
export default class StatusBar {
|
export default class StatusBar {
|
||||||
private statusBarEl: HTMLElement;
|
private statusBarEl: HTMLElement;
|
||||||
private plugin: BetterWordCount;
|
private plugin: BetterWordCount;
|
||||||
|
public debounceStatusBarUpdate;
|
||||||
|
|
||||||
constructor(statusBarEl: HTMLElement, plugin: BetterWordCount) {
|
constructor(statusBarEl: HTMLElement, plugin: BetterWordCount) {
|
||||||
this.statusBarEl = statusBarEl;
|
this.statusBarEl = statusBarEl;
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
|
this.debounceStatusBarUpdate = debounce(
|
||||||
|
(text: string) => this.updateStatusBar(text),
|
||||||
|
20,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
this.statusBarEl.classList.add("mod-clickable");
|
this.statusBarEl.classList.add("mod-clickable");
|
||||||
this.statusBarEl.setAttribute("aria-label", "Open Stats View");
|
this.statusBarEl.setAttribute("aria-label", "Open Stats View");
|
||||||
|
@ -30,7 +37,7 @@ export default class StatusBar {
|
||||||
this.statusBarEl.setText(text);
|
this.statusBarEl.setText(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateStatusBar(text: string) {
|
async updateStatusBar(text: string) {
|
||||||
const sb = this.plugin.settings.statusBar;
|
const sb = this.plugin.settings.statusBar;
|
||||||
let display = "";
|
let display = "";
|
||||||
|
|
||||||
|
@ -49,13 +56,15 @@ export default class StatusBar {
|
||||||
display = display + getSentenceCount(text);
|
display = display + getSentenceCount(text);
|
||||||
break;
|
break;
|
||||||
case Counter.totalWords:
|
case Counter.totalWords:
|
||||||
display = display + this.plugin.statsManager.getTotalWords();
|
display = display + (await this.plugin.statsManager.getTotalWords());
|
||||||
break;
|
break;
|
||||||
case Counter.totalChars:
|
case Counter.totalChars:
|
||||||
display = display + this.plugin.statsManager.getTotalCharacters();
|
display =
|
||||||
|
display + (await this.plugin.statsManager.getTotalCharacters());
|
||||||
break;
|
break;
|
||||||
case Counter.totalSentences:
|
case Counter.totalSentences:
|
||||||
display = display + this.plugin.statsManager.getTotalSentences();
|
display =
|
||||||
|
display + (await this.plugin.statsManager.getTotalSentences());
|
||||||
break;
|
break;
|
||||||
case Counter.totalNotes:
|
case Counter.totalNotes:
|
||||||
display = display + this.plugin.statsManager.getTotalFiles();
|
display = display + this.plugin.statsManager.getTotalFiles();
|
||||||
|
|
Loading…
Reference in a new issue