Working State
This commit is contained in:
parent
c33160dff5
commit
8b7630a357
5 changed files with 78 additions and 74 deletions
|
@ -1,8 +1,8 @@
|
||||||
# Better Word Count
|
# Better Word Count
|
||||||
|
|
||||||
  
|
  
|
||||||
|
|
||||||
**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.
|
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.
|
||||||
|
|
||||||
|
|
|
@ -22,13 +22,35 @@ class EditorPlugin implements PluginValue {
|
||||||
}
|
}
|
||||||
|
|
||||||
const tr = update.transactions[0];
|
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 = "";
|
let text = "";
|
||||||
const selection = tr.newSelection.main;
|
const selection = tr.newSelection.main;
|
||||||
const textIter = tr.newDoc.iterRange(selection.from, selection.to);
|
const textIter = tr.newDoc.iterRange(selection.from, selection.to);
|
||||||
while (!textIter.done) {
|
while (!textIter.done) {
|
||||||
text = text + textIter.next().value;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
47
src/main.ts
47
src/main.ts
|
@ -33,51 +33,20 @@ export default class BetterWordCount extends Plugin {
|
||||||
// Handle Status Bar
|
// Handle Status Bar
|
||||||
let statusBarEl = this.addStatusBarItem();
|
let statusBarEl = this.addStatusBarItem();
|
||||||
this.statusBar = new StatusBar(statusBarEl, this);
|
this.statusBar = new StatusBar(statusBarEl, this);
|
||||||
this.statusBar.displayText("Hiya!");
|
|
||||||
|
|
||||||
// Handle the Editor Plugin
|
// Handle the Editor Plugin
|
||||||
this.registerEditorExtension(editorPlugin);
|
this.registerEditorExtension(editorPlugin);
|
||||||
//@ts-expect-error, not typed
|
|
||||||
const editorView = this.app.workspace.getMostRecentLeaf().view.editor
|
this.app.workspace.onLayoutReady(() => {
|
||||||
.cm as EditorView;
|
//@ts-expect-error, not typed
|
||||||
const editorPlug = editorView.plugin(editorPlugin);
|
const editorView = this.app.workspace.getMostRecentLeaf().view.editor
|
||||||
editorPlug.addPlugin(this);
|
.cm as EditorView;
|
||||||
|
const editorPlug = editorView.plugin(editorPlugin);
|
||||||
|
editorPlug.addPlugin(this);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveSettings(): Promise<void> {
|
async saveSettings(): Promise<void> {
|
||||||
await this.saveData(this.settings);
|
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);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
import type { Vault, TFile, Workspace } from "obsidian";
|
import type { Vault, TFile, Workspace } from "obsidian";
|
||||||
import { STATS_FILE } from "../constants";
|
import { STATS_FILE } from "../constants";
|
||||||
import type {
|
import type {
|
||||||
CountDiff,
|
|
||||||
Day,
|
Day,
|
||||||
VaultStatistics,
|
VaultStatistics,
|
||||||
History,
|
|
||||||
FileStat,
|
|
||||||
} from "./Stats";
|
} from "./Stats";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import {
|
import {
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
|
import { Counter } from "src/settings/Settings";
|
||||||
import type BetterWordCount from "../main";
|
import type BetterWordCount from "../main";
|
||||||
|
import {
|
||||||
|
getWordCount,
|
||||||
|
getCharacterCount,
|
||||||
|
getSentenceCount,
|
||||||
|
} from "src/utils/StatUtils";
|
||||||
|
|
||||||
export default class StatusBar {
|
export default class StatusBar {
|
||||||
private statusBarEl: HTMLElement;
|
private statusBarEl: HTMLElement;
|
||||||
|
@ -24,34 +30,44 @@ export default class StatusBar {
|
||||||
this.statusBarEl.setText(text);
|
this.statusBarEl.setText(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateStatusBar() {
|
updateStatusBar(text: string) {
|
||||||
// switch (sbItem.count) {
|
const sb = this.plugin.settings.statusBar;
|
||||||
// case Counter.fileWords:
|
let display = "";
|
||||||
// display = display + getWordCount(text);
|
|
||||||
// break;
|
for (let i = 0; i < sb.length; i++) {
|
||||||
// case Counter.fileChars:
|
const sbItem = sb[i];
|
||||||
// display = display + getCharacterCount(text);
|
|
||||||
// break;
|
display = display + sbItem.prefix;
|
||||||
// case Counter.fileSentences:
|
switch (sbItem.count) {
|
||||||
// display = display + getSentenceCount(text);
|
case Counter.fileWords:
|
||||||
// break;
|
display = display + getWordCount(text);
|
||||||
// case Counter.totalWords:
|
break;
|
||||||
// display = display + BetterWordCount.statsManager.getTotalWords();
|
case Counter.fileChars:
|
||||||
// break;
|
display = display + getCharacterCount(text);
|
||||||
// case Counter.totalChars:
|
break;
|
||||||
// display = display + BetterWordCount.statsManager.getTotalCharacters();
|
case Counter.fileSentences:
|
||||||
// break;
|
display = display + getSentenceCount(text);
|
||||||
// case Counter.totalSentences:
|
break;
|
||||||
// display = display + BetterWordCount.statsManager.getTotalSentences();
|
case Counter.totalWords:
|
||||||
// break;
|
display = display + this.plugin.statsManager.getTotalWords();
|
||||||
// case Counter.totalNotes:
|
break;
|
||||||
// display = display + BetterWordCount.statsManager.getTotalFiles();
|
case Counter.totalChars:
|
||||||
// break;
|
display = display + this.plugin.statsManager.getTotalCharacters();
|
||||||
//
|
break;
|
||||||
// default:
|
case Counter.totalSentences:
|
||||||
// break;
|
display = display + this.plugin.statsManager.getTotalSentences();
|
||||||
// }
|
break;
|
||||||
//
|
case Counter.totalNotes:
|
||||||
// display = display + sbItem.suffix;
|
display = display + this.plugin.statsManager.getTotalFiles();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
display = display + sbItem.suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.displayText(display);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue