Add settings to toggle counts and more #4
This commit is contained in:
parent
7dfa75e1bc
commit
11b793baa8
3 changed files with 147 additions and 8 deletions
41
src/main.ts
41
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";
|
import { StatusBar } from "./status-bar";
|
||||||
|
|
||||||
export default class BetterWordCount extends Plugin {
|
export default class BetterWordCount extends Plugin {
|
||||||
public recentlyTyped: boolean;
|
public recentlyTyped: boolean;
|
||||||
public statusBar: StatusBar;
|
public statusBar: StatusBar;
|
||||||
public currentFile: TFile;
|
public currentFile: TFile;
|
||||||
|
public settings: BetterWordCountSettings;
|
||||||
|
|
||||||
onload() {
|
async onload() {
|
||||||
let statusBarEl = this.addStatusBarItem();
|
let statusBarEl = this.addStatusBarItem();
|
||||||
this.statusBar = new StatusBar(statusBarEl);
|
this.statusBar = new StatusBar(statusBarEl);
|
||||||
|
|
||||||
this.recentlyTyped = false;
|
this.recentlyTyped = false;
|
||||||
|
|
||||||
|
this.settings = (await this.loadData()) || new BetterWordCountSettings();
|
||||||
|
this.addSettingTab(new BetterWordCountSettingsTab(this.app, this));
|
||||||
|
|
||||||
this.registerEvent(
|
this.registerEvent(
|
||||||
this.app.workspace.on("file-open", this.onFileOpen, this)
|
this.app.workspace.on("file-open", this.onFileOpen, this)
|
||||||
);
|
);
|
||||||
|
@ -78,7 +84,7 @@ export default class BetterWordCount extends Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
updateWordCount(text: string) {
|
updateWordCount(text: string) {
|
||||||
let words = 0;
|
let words: number = 0;
|
||||||
|
|
||||||
const matches = text.match(
|
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
|
/[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;
|
).length;
|
||||||
|
|
||||||
this.statusBar.displayText(
|
let displayText: string = "";
|
||||||
`${words} words ` +
|
if (this.settings.showWords) {
|
||||||
`${text.length} characters ` +
|
displayText =
|
||||||
`${sentences} sentences`
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
103
src/settings/settings-tab.ts
Normal file
103
src/settings/settings-tab.ts
Normal 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
11
src/settings/settings.ts
Normal 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";
|
||||||
|
}
|
Loading…
Reference in a new issue