Add settings to toggle counts and more #4

This commit is contained in:
Luke Leppan 2021-01-19 22:39:10 +02:00
parent 7dfa75e1bc
commit 11b793baa8
3 changed files with 147 additions and 8 deletions

View file

@ -1,17 +1,23 @@
import { MarkdownView, Plugin, TFile } from "obsidian";
import { MarkdownView, Plugin, TFile, WorkspaceSidedock } from "obsidian";
import { BetterWordCountSettingsTab } from "./settings/settings-tab";
import { BetterWordCountSettings } from "./settings/settings";
import { StatusBar } from "./status-bar";
export default class BetterWordCount extends Plugin {
public recentlyTyped: boolean;
public statusBar: StatusBar;
public currentFile: TFile;
public settings: BetterWordCountSettings;
onload() {
async onload() {
let statusBarEl = this.addStatusBarItem();
this.statusBar = new StatusBar(statusBarEl);
this.recentlyTyped = false;
this.settings = (await this.loadData()) || new BetterWordCountSettings();
this.addSettingTab(new BetterWordCountSettingsTab(this.app, this));
this.registerEvent(
this.app.workspace.on("file-open", this.onFileOpen, this)
);
@ -78,7 +84,7 @@ export default class BetterWordCount extends Plugin {
}
updateWordCount(text: string) {
let words = 0;
let words: number = 0;
const matches = text.match(
/[a-zA-Z0-9_\u0392-\u03c9\u00c0-\u00ff\u0600-\u06ff]+|[\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/gm
@ -102,10 +108,29 @@ export default class BetterWordCount extends Plugin {
) || []
).length;
this.statusBar.displayText(
`${words} words ` +
`${text.length} characters ` +
`${sentences} sentences`
);
let displayText: string = "";
if (this.settings.showWords) {
displayText =
displayText +
this.settings.wordsPrefix +
words +
this.settings.wordsSuffix;
}
if (this.settings.showCharacters) {
displayText =
displayText +
this.settings.charactersPrefix +
text.length +
this.settings.charactersSuffix;
}
if (this.settings.showSentences) {
displayText =
displayText +
this.settings.sentencesPrefix +
sentences +
this.settings.sentencesSuffix;
}
this.statusBar.displayText(displayText);
}
}

View file

@ -0,0 +1,103 @@
import { settings } from "cluster";
import { PluginSettingTab, Setting } from "obsidian";
import BetterWordCount from "../main";
export class BetterWordCountSettingsTab extends PluginSettingTab {
display(): void {
let { containerEl } = this;
const plugin: BetterWordCount = (this as any).plugin;
containerEl.empty();
containerEl.createEl("h2", { text: "Better Word Count Settings" });
// Word Count Settings
containerEl.createEl("h3", { text: "Word Count Settings" });
new Setting(containerEl)
.setName("Show Word Count")
.setDesc("Enable this to show the word count.")
.addToggle((boolean) =>
boolean.setValue(plugin.settings.showWords).onChange((value) => {
plugin.settings.showWords = value;
plugin.saveData(plugin.settings);
})
);
new Setting(containerEl)
.setName("Word Count Prefix")
.setDesc("This changes the text in front of the word count number.")
.addText((text) =>
text.setValue(plugin.settings.wordsPrefix).onChange((value) => {
plugin.settings.wordsPrefix = value;
plugin.saveData(plugin.settings);
})
);
new Setting(containerEl)
.setName("Word Count Suffix")
.setDesc("This changes the text after of the word count number.")
.addText((text) =>
text.setValue(plugin.settings.wordsSuffix).onChange((value) => {
plugin.settings.wordsSuffix = value;
plugin.saveData(plugin.settings);
})
);
// Character Count Settings
containerEl.createEl("h3", { text: "Character Count Settings" });
new Setting(containerEl)
.setName("Show Character Count")
.setDesc("Enable this to show the character count.")
.addToggle((boolean) =>
boolean.setValue(plugin.settings.showCharacters).onChange((value) => {
plugin.settings.showCharacters = value;
plugin.saveData(plugin.settings);
})
);
new Setting(containerEl)
.setName("Character Count Prefix")
.setDesc("This changes the text in front of the character count number.")
.addText((text) =>
text.setValue(plugin.settings.charactersPrefix).onChange((value) => {
plugin.settings.charactersPrefix = value;
plugin.saveData(plugin.settings);
})
);
new Setting(containerEl)
.setName("Character Count Suffix")
.setDesc("This changes the text after of the character count number.")
.addText((text) =>
text.setValue(plugin.settings.charactersSuffix).onChange((value) => {
plugin.settings.charactersSuffix = value;
plugin.saveData(plugin.settings);
})
);
// Sentence Count Settings
containerEl.createEl("h3", { text: "Sentence Count Settings" });
new Setting(containerEl)
.setName("Show Sentence Count")
.setDesc("Enable this to show the sentence count.")
.addToggle((boolean) =>
boolean.setValue(plugin.settings.showSentences).onChange((value) => {
plugin.settings.showSentences = value;
plugin.saveData(plugin.settings);
})
);
new Setting(containerEl)
.setName("Sentence Count Prefix")
.setDesc("This changes the text in front of the sentence count number.")
.addText((text) =>
text.setValue(plugin.settings.sentencesPrefix).onChange((value) => {
plugin.settings.sentencesPrefix = value;
plugin.saveData(plugin.settings);
})
);
new Setting(containerEl)
.setName("Sentence Count Suffix")
.setDesc("This changes the text after of the sentence count number.")
.addText((text) =>
text.setValue(plugin.settings.sentencesSuffix).onChange((value) => {
plugin.settings.sentencesSuffix = value;
plugin.saveData(plugin.settings);
})
);
}
}

11
src/settings/settings.ts Normal file
View file

@ -0,0 +1,11 @@
export class BetterWordCountSettings {
showWords: boolean = true;
wordsPrefix: string = "";
wordsSuffix: string = " words ";
showCharacters: boolean = true;
charactersPrefix: string = "";
charactersSuffix: string = " characters ";
showSentences: boolean = true;
sentencesPrefix: string = "";
sentencesSuffix: string = " sentences";
}