From 8b7630a35704d208eee465bd108893789ea8dbce Mon Sep 17 00:00:00 2001 From: Luke Leppan Date: Tue, 15 Nov 2022 19:21:08 +0200 Subject: [PATCH] Working State --- README.md | 4 +-- src/editor/EditorPlugin.ts | 24 ++++++++++++- src/main.ts | 47 +++++------------------- src/stats/StatsManager.ts | 3 -- src/status/StatusBar.ts | 74 +++++++++++++++++++++++--------------- 5 files changed, 78 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index f04f39a..0e14833 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Better Word Count -![GitHub Workflow Status](https://img.shields.io/github/workflow/status/lukeleppan/better-word-count/Build%20Release?logo=github&style=for-the-badge) ![GitHub release (latest by date)](https://img.shields.io/github/v/release/lukeleppan/better-word-count?style=for-the-badge) ![GitHub All Releases](https://img.shields.io/github/downloads/lukeleppan/better-word-count/total?style=for-the-badge) +![GitHub Workflow Status](https://img.shields.io/github/workflow/status/lukeleppan/better-word-count/Build%20Release?logo=github&style=for-the-badge) ![GitHub release (latest by date)](https://img.shields.io/github/v/release/lukeleppan/better-word-count?style=for-the-badge) ![Obsidian Downloads](https://img.shields.io/badge/dynamic/json?logo=obsidian&color=%23483699&label=downloads&query=%24%5B%22better-word-count%22%5D.downloads&url=https%3A%2F%2Fraw.githubusercontent.com%2Fobsidianmd%2Fobsidian-releases%2Fmaster%2Fcommunity-plugin-stats.json&style=for-the-badge) -**IMPORTANT NOTICE:** Due to the introduction of the new Live Preview feature, this plugin needed to be almost entirely rewritten. Currently the plugin only displays the only the word and character count with selecting text features. ALL other features have been scrapped and will be ported in future updates. **DO NOT UPDATE** If you want to use the legacy editor since their is no backwards compatibility yet (I know I'm lazy but I wanted to release the fix, its been too long). +**IMPORTANT NOTICE:** Due to the introduction of the new Live Preview feature, this plugin needed to be almost entirely rewritten. Currently the plugin only displays the only the word and character count with selecting text features. ALL other features have been scrapped and will be ported in future updates. **DO NOT UPDATE** If you want to use the legacy editor since their is no backwards compatibility yet (I know I'm lazy but I wanted to release the fix, its been too long). This plugin is the same as the built-in **Word Count** plugin, except when you select text, it will count the selected word instead of the whole document. I recommend turning off the built-in **Word Count** because this plugin is designed to replace that. This plugin also has the ability to store statistics about your vault. diff --git a/src/editor/EditorPlugin.ts b/src/editor/EditorPlugin.ts index b2cf357..29336ef 100644 --- a/src/editor/EditorPlugin.ts +++ b/src/editor/EditorPlugin.ts @@ -22,13 +22,35 @@ class EditorPlugin implements PluginValue { } const tr = update.transactions[0]; - if (tr.isUserEvent("select")) { + if (!tr) return; + if ( + tr.isUserEvent("select") && + tr.newSelection.ranges[0].from !== tr.newSelection.ranges[0].to + ) { let text = ""; const selection = tr.newSelection.main; const textIter = tr.newDoc.iterRange(selection.from, selection.to); while (!textIter.done) { text = text + textIter.next().value; } + this.plugin.statusBar.updateStatusBar(text); + } else if ( + tr.isUserEvent("input") || + tr.isUserEvent("delete") || + tr.isUserEvent("move") || + tr.isUserEvent("undo") || + tr.isUserEvent("redo") || + tr.isUserEvent("select") + ) { + const textIter = tr.newDoc.iter(); + let text = ""; + while (!textIter.done) { + text = text + textIter.next().value; + } + if (tr.docChanged && this.plugin.statsManager) { + this.plugin.statsManager.change(text); + } + this.plugin.statusBar.updateStatusBar(text); } } diff --git a/src/main.ts b/src/main.ts index d580a18..01c8288 100644 --- a/src/main.ts +++ b/src/main.ts @@ -33,51 +33,20 @@ export default class BetterWordCount extends Plugin { // 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); + + this.app.workspace.onLayoutReady(() => { + //@ts-expect-error, not typed + const editorView = this.app.workspace.getMostRecentLeaf().view.editor + .cm as EditorView; + const editorPlug = editorView.plugin(editorPlugin); + editorPlug.addPlugin(this); + }); } async saveSettings(): Promise { await this.saveData(this.settings); } - - // createCMExtension() { - // const cmStateField = StateField.define({ - // 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), - // }); - // - // this.registerEditorExtension(cmStateField); - // } } diff --git a/src/stats/StatsManager.ts b/src/stats/StatsManager.ts index 0b4b451..3c7197b 100644 --- a/src/stats/StatsManager.ts +++ b/src/stats/StatsManager.ts @@ -1,11 +1,8 @@ import type { Vault, TFile, Workspace } from "obsidian"; import { STATS_FILE } from "../constants"; import type { - CountDiff, Day, VaultStatistics, - History, - FileStat, } from "./Stats"; import moment from "moment"; import { diff --git a/src/status/StatusBar.ts b/src/status/StatusBar.ts index d44ee3f..624d36a 100644 --- a/src/status/StatusBar.ts +++ b/src/status/StatusBar.ts @@ -1,4 +1,10 @@ +import { Counter } from "src/settings/Settings"; import type BetterWordCount from "../main"; +import { + getWordCount, + getCharacterCount, + getSentenceCount, +} from "src/utils/StatUtils"; export default class StatusBar { private statusBarEl: HTMLElement; @@ -24,34 +30,44 @@ export default class StatusBar { this.statusBarEl.setText(text); } - updateStatusBar() { - // switch (sbItem.count) { - // case Counter.fileWords: - // display = display + getWordCount(text); - // break; - // case Counter.fileChars: - // display = display + getCharacterCount(text); - // break; - // case Counter.fileSentences: - // display = display + getSentenceCount(text); - // break; - // case Counter.totalWords: - // display = display + BetterWordCount.statsManager.getTotalWords(); - // break; - // case Counter.totalChars: - // display = display + BetterWordCount.statsManager.getTotalCharacters(); - // break; - // case Counter.totalSentences: - // display = display + BetterWordCount.statsManager.getTotalSentences(); - // break; - // case Counter.totalNotes: - // display = display + BetterWordCount.statsManager.getTotalFiles(); - // break; - // - // default: - // break; - // } - // - // display = display + sbItem.suffix; + updateStatusBar(text: string) { + const sb = this.plugin.settings.statusBar; + let display = ""; + + for (let i = 0; i < sb.length; i++) { + const sbItem = sb[i]; + + display = display + sbItem.prefix; + switch (sbItem.count) { + case Counter.fileWords: + display = display + getWordCount(text); + break; + case Counter.fileChars: + display = display + getCharacterCount(text); + break; + case Counter.fileSentences: + display = display + getSentenceCount(text); + break; + case Counter.totalWords: + display = display + this.plugin.statsManager.getTotalWords(); + break; + case Counter.totalChars: + display = display + this.plugin.statsManager.getTotalCharacters(); + break; + case Counter.totalSentences: + display = display + this.plugin.statsManager.getTotalSentences(); + break; + case Counter.totalNotes: + display = display + this.plugin.statsManager.getTotalFiles(); + break; + + default: + break; + } + + display = display + sbItem.suffix; + } + + this.displayText(display); } }