Merge branch 'hotfix/0.7.3' into develop

This commit is contained in:
Luke Leppan 2021-07-11 23:37:51 +02:00
commit 6c7e97e21e
5 changed files with 39 additions and 27 deletions

View file

@ -1,7 +1,7 @@
{ {
"id": "better-word-count", "id": "better-word-count",
"name": "Better Word Count", "name": "Better Word Count",
"version": "0.7.2", "version": "0.7.3",
"description": "Counts the words of selected text in the editor.", "description": "Counts the words of selected text in the editor.",
"author": "Luke Leppan", "author": "Luke Leppan",
"authorUrl": "https://lukeleppan.com", "authorUrl": "https://lukeleppan.com",

View file

@ -1,6 +1,6 @@
{ {
"name": "better-word-count", "name": "better-word-count",
"version": "0.7.2", "version": "0.7.3",
"description": "Counts the words of selected text in the editor.", "description": "Counts the words of selected text in the editor.",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {

View file

@ -1,5 +1,5 @@
import moment from "moment"; import moment from "moment";
import type { MetadataCache, TFile, Vault } from "obsidian"; import { debounce, Debouncer, MetadataCache, TFile, Vault } from "obsidian";
import { STATS_FILE } from "src/constants"; import { STATS_FILE } from "src/constants";
import { DataCollector } from "./collector"; import { DataCollector } from "./collector";
import { getCharacterCount, getSentenceCount, getWordCount } from "./stats"; import { getCharacterCount, getSentenceCount, getWordCount } from "./stats";
@ -39,11 +39,17 @@ export class DataManager {
private today: string; private today: string;
private collector: DataCollector; private collector: DataCollector;
private todayCounts: TodayCounts; private todayCounts: TodayCounts;
public debounceChange: Debouncer<[file: TFile, data: string]>;
constructor(vault: Vault, metadataCache: MetadataCache) { constructor(vault: Vault, metadataCache: MetadataCache) {
this.vault = vault; this.vault = vault;
this.metadataCache = metadataCache; this.metadataCache = metadataCache;
this.collector = new DataCollector(vault, metadataCache); this.collector = new DataCollector(vault, metadataCache);
this.debounceChange = debounce(
(file: TFile, data: string) => this.change(file, data),
1000,
false
);
this.vault.adapter.exists(".vault-stats").then(async (exists) => { this.vault.adapter.exists(".vault-stats").then(async (exists) => {
if (!exists) { if (!exists) {

View file

@ -56,7 +56,7 @@ export default class BetterWordCount extends Plugin {
this.registerEvent( this.registerEvent(
this.app.workspace.on( this.app.workspace.on(
"quick-preview", "quick-preview",
this.dataManager.change, this.dataManager.debounceChange,
this.dataManager this.dataManager
) )
); );

View file

@ -1,4 +1,4 @@
import type { MetadataCache, Vault } from "obsidian"; import { MetadataCache, Vault, Debouncer, debounce } from "obsidian";
import { DataCollector } from "src/data/collector"; import { DataCollector } from "src/data/collector";
import { DataManager } from "src/data/manager"; import { DataManager } from "src/data/manager";
import type { TodayCounts } from "src/data/manager"; import type { TodayCounts } from "src/data/manager";
@ -18,6 +18,8 @@ export class BarManager {
private vault: Vault; private vault: Vault;
private dataCollector: DataCollector; private dataCollector: DataCollector;
private dataManager: DataManager; private dataManager: DataManager;
private deboucer: Debouncer<[text: string]>;
private expression: Expression;
constructor( constructor(
statusBar: StatusBar, statusBar: StatusBar,
@ -30,13 +32,18 @@ export class BarManager {
this.vault = vault; this.vault = vault;
this.dataCollector = new DataCollector(vault, metadataCache); this.dataCollector = new DataCollector(vault, metadataCache);
this.dataManager = new DataManager(vault, metadataCache); this.dataManager = new DataManager(vault, metadataCache);
this.deboucer = debounce(
(text: string) => this.updateStatusBar(text),
20,
false
);
this.expression = parse(this.settings.statusBarQuery);
} }
async updateStatusBar(text: string): Promise<void> { updateStatusBar(text: string): void {
let newText = ""; let newText = "";
const expression: Expression = parse(this.settings.statusBarQuery);
if (this.settings.collectStats) { if (this.settings.collectStats) {
this.dataManager.updateToday();
this.dataManager.updateTodayCounts(); this.dataManager.updateTodayCounts();
} }
const todayCounts: TodayCounts = this.settings.collectStats const todayCounts: TodayCounts = this.settings.collectStats
@ -44,10 +51,10 @@ export class BarManager {
: { words: 0, characters: 0, sentences: 0 }; : { words: 0, characters: 0, sentences: 0 };
let varsIndex = 0; let varsIndex = 0;
for (const i in expression.parsed) { for (const i in this.expression.parsed) {
const e = expression.parsed[i]; const e = this.expression.parsed[i];
newText = newText + e; newText = newText + e;
switch (expression.vars[varsIndex]) { switch (this.expression.vars[varsIndex]) {
case 0: case 0:
newText = newText + getWordCount(text); newText = newText + getWordCount(text);
break; break;
@ -58,15 +65,13 @@ export class BarManager {
newText = newText + getSentenceCount(text); newText = newText + getSentenceCount(text);
break; break;
case 3: case 3:
newText = newText + (await this.dataCollector.getTotalWordCount()); newText = newText + this.dataManager.getTodayCounts().words;
break; break;
case 4: case 4:
newText = newText = newText + this.dataManager.getTodayCounts().characters;
newText + (await this.dataCollector.getTotalCharacterCount());
break; break;
case 5: case 5:
newText = newText = newText + this.dataManager.getTodayCounts().sentences;
newText + (await this.dataCollector.getTotalCharacterCount());
break; break;
case 6: case 6:
newText = newText + this.dataCollector.getTotalFileCount(); newText = newText + this.dataCollector.getTotalFileCount();
@ -87,10 +92,13 @@ export class BarManager {
this.statusBar.displayText(newText); this.statusBar.displayText(newText);
} }
async updateAltStatusBar(): Promise<void> { updateAltStatusBar(): void {
let newText = ""; let newText = "";
const expression: Expression = parse(this.settings.statusBarAltQuery); const expression: Expression = parse(this.settings.statusBarAltQuery);
if (this.settings.collectStats) this.dataManager.updateTodayCounts(); if (this.settings.collectStats) {
this.dataManager.updateTodayCounts();
}
const todayCounts: TodayCounts = this.settings.collectStats const todayCounts: TodayCounts = this.settings.collectStats
? this.dataManager.getTodayCounts() ? this.dataManager.getTodayCounts()
: { words: 0, characters: 0, sentences: 0 }; : { words: 0, characters: 0, sentences: 0 };
@ -110,15 +118,13 @@ export class BarManager {
newText = newText + getSentenceCount(""); newText = newText + getSentenceCount("");
break; break;
case 3: case 3:
newText = newText + (await this.dataCollector.getTotalWordCount()); newText = newText + this.dataManager.getTodayCounts().words;
break; break;
case 4: case 4:
newText = newText = newText + this.dataManager.getTodayCounts().characters;
newText + (await this.dataCollector.getTotalCharacterCount());
break; break;
case 5: case 5:
newText = newText = newText + this.dataManager.getTodayCounts().sentences;
newText + (await this.dataCollector.getTotalCharacterCount());
break; break;
case 6: case 6:
newText = newText + this.dataCollector.getTotalFileCount(); newText = newText + this.dataCollector.getTotalFileCount();
@ -142,18 +148,18 @@ export class BarManager {
cursorActivity(cm: CodeMirror.Editor) { cursorActivity(cm: CodeMirror.Editor) {
if (cm.somethingSelected()) { if (cm.somethingSelected()) {
if (this.settings.countComments) { if (this.settings.countComments) {
this.updateStatusBar(cleanComments(cm.getSelection())); this.deboucer(cleanComments(cm.getSelection()));
} else { } else {
this.updateStatusBar(cm.getSelection()); this.deboucer(cm.getSelection());
} }
} else { } else {
if (this.settings.collectStats) { if (this.settings.collectStats) {
this.dataManager.updateFromFile(); this.dataManager.updateFromFile();
} }
if (this.settings.countComments) { if (this.settings.countComments) {
this.updateStatusBar(cleanComments(cm.getValue())); this.deboucer(cleanComments(cm.getValue()));
} else { } else {
this.updateStatusBar(cm.getValue()); this.deboucer(cm.getValue());
} }
} }
} }