Working State

This commit is contained in:
Luke Leppan 2022-11-15 19:21:08 +02:00
parent c33160dff5
commit 8b7630a357
No known key found for this signature in database
GPG key ID: AE403C75AFBBC102
5 changed files with 78 additions and 74 deletions

View file

@ -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.

View file

@ -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);
}
}

View file

@ -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<void> {
await this.saveData(this.settings);
}
// 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),
// });
//
// this.registerEditorExtension(cmStateField);
// }
}

View file

@ -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 {

View file

@ -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);
}
}