better-word-count/src/main.ts

167 lines
4.6 KiB
TypeScript
Raw Normal View History

import {
MarkdownView,
Plugin,
TFile,
MetadataCache,
getAllTags,
} from "obsidian";
import { BetterWordCountSettingsTab } from "./settings/settings-tab";
import { BetterWordCountSettings } from "./settings/settings";
2021-02-17 04:29:24 +00:00
import {
getWordCount,
getCharacterCount,
getSentenceCount,
getFilesCount,
} from "./stats";
2020-11-09 13:33:44 +00:00
import { StatusBar } from "./status-bar";
export default class BetterWordCount extends Plugin {
public recentlyTyped: boolean;
public statusBar: StatusBar;
2020-11-10 14:23:28 +00:00
public currentFile: TFile;
public settings: BetterWordCountSettings;
2020-11-09 13:33:44 +00:00
async onload() {
2020-11-09 13:33:44 +00:00
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.updateAltCount();
2020-11-09 13:33:44 +00:00
this.registerEvent(
this.app.workspace.on("file-open", this.onFileOpen, this)
);
this.registerEvent(
this.app.workspace.on("quick-preview", this.onQuickPreview, this)
);
2020-11-10 14:19:55 +00:00
this.registerInterval(
window.setInterval(async () => {
let activeLeaf = this.app.workspace.activeLeaf;
2020-11-10 14:23:28 +00:00
2020-11-10 14:19:55 +00:00
if (!activeLeaf || !(activeLeaf.view instanceof MarkdownView)) {
return;
}
let editor = activeLeaf.view.sourceMode.cmEditor;
if (editor.somethingSelected()) {
let content: string = editor.getSelection();
this.updateWordCount(content);
this.recentlyTyped = false;
} else if (
2020-11-10 14:23:28 +00:00
this.currentFile &&
this.currentFile.extension === "md" &&
2020-11-10 14:19:55 +00:00
!this.recentlyTyped
) {
2020-11-11 09:32:20 +00:00
const contents = await this.app.vault.cachedRead(this.currentFile);
2020-11-10 14:19:55 +00:00
this.updateWordCount(contents);
} else if (!this.recentlyTyped) {
this.updateWordCount("");
}
}, 500)
);
2020-11-09 13:33:44 +00:00
let activeLeaf = this.app.workspace.activeLeaf;
let files: TFile[] = this.app.vault.getMarkdownFiles();
files.forEach((file) => {
2020-11-11 09:47:44 +00:00
if (file.basename === activeLeaf.getDisplayText()) {
2020-11-09 13:33:44 +00:00
this.onFileOpen(file);
}
});
}
async onFileOpen(file: TFile) {
2020-11-10 14:23:28 +00:00
this.currentFile = file;
2020-11-09 13:33:44 +00:00
if (file && file.extension === "md") {
const contents = await this.app.vault.cachedRead(file);
2020-11-10 14:19:55 +00:00
this.recentlyTyped = true;
2020-11-09 13:33:44 +00:00
this.updateWordCount(contents);
} else {
this.updateAltCount();
2020-11-09 13:33:44 +00:00
}
}
onQuickPreview(file: TFile, contents: string) {
2020-11-10 14:23:28 +00:00
this.currentFile = file;
2020-11-09 13:33:44 +00:00
const leaf = this.app.workspace.activeLeaf;
2020-11-09 13:33:44 +00:00
if (leaf && leaf.view.getViewType() === "markdown") {
this.recentlyTyped = true;
this.updateWordCount(contents);
}
}
async updateAltCount() {
2021-02-17 04:29:24 +00:00
const files = getFilesCount(this.app.vault.getFiles());
let displayText: string = `${files} files `;
let allWords = 0;
let allCharacters = 0;
let allSentences = 0;
2021-04-03 09:56:14 +00:00
for (const f of this.app.vault.getMarkdownFiles()) {
let fileContents = await this.app.vault.cachedRead(f);
allWords += getWordCount(fileContents);
allCharacters += getCharacterCount(fileContents);
allSentences += getSentenceCount(fileContents);
}
if (this.settings.showWords) {
displayText =
displayText +
this.settings.wordsPrefix +
allWords +
this.settings.wordsSuffix;
}
if (this.settings.showCharacters) {
displayText =
displayText +
this.settings.charactersPrefix +
allCharacters +
this.settings.charactersSuffix;
}
if (this.settings.showSentences) {
displayText =
displayText +
this.settings.sentencesPrefix +
allSentences +
this.settings.sentencesSuffix;
}
this.statusBar.displayText(displayText);
}
2020-11-09 13:33:44 +00:00
updateWordCount(text: string) {
let displayText: string = "";
if (this.settings.showWords) {
displayText =
displayText +
this.settings.wordsPrefix +
2021-02-17 04:29:24 +00:00
getWordCount(text) +
this.settings.wordsSuffix;
}
if (this.settings.showCharacters) {
displayText =
displayText +
this.settings.charactersPrefix +
2021-02-17 04:29:24 +00:00
getCharacterCount(text) +
this.settings.charactersSuffix;
}
if (this.settings.showSentences) {
displayText =
displayText +
this.settings.sentencesPrefix +
2021-02-17 04:29:24 +00:00
getSentenceCount(text) +
this.settings.sentencesSuffix;
}
this.statusBar.displayText(displayText);
2020-11-09 13:33:44 +00:00
}
}