diff --git a/src/main.ts b/src/main.ts index d0fe838..8b5eca0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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); } } diff --git a/src/settings/settings-tab.ts b/src/settings/settings-tab.ts new file mode 100644 index 0000000..2e53a99 --- /dev/null +++ b/src/settings/settings-tab.ts @@ -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); + }) + ); + } +} diff --git a/src/settings/settings.ts b/src/settings/settings.ts new file mode 100644 index 0000000..2402f0a --- /dev/null +++ b/src/settings/settings.ts @@ -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"; +}