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
|
||||
|
||||
  
|
||||
  
|
||||
|
||||
**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.
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
47
src/main.ts
47
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<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);
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue