2021-07-03 19:22:50 +00:00
|
|
|
import { MarkdownView, Plugin, TFile, addIcon, WorkspaceLeaf } from "obsidian";
|
2021-01-19 20:39:10 +00:00
|
|
|
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";
|
2021-07-03 19:22:50 +00:00
|
|
|
import { StatusBar } from "./status/bar";
|
|
|
|
import { STATS_ICON, STATS_ICON_NAME, VIEW_TYPE_STATS } from "./constants";
|
|
|
|
import StatsView from "./view/view";
|
|
|
|
import { DataManager } from "./data/manager";
|
2020-11-09 13:33:44 +00:00
|
|
|
|
|
|
|
export default class BetterWordCount extends Plugin {
|
|
|
|
public recentlyTyped: boolean;
|
|
|
|
public statusBar: StatusBar;
|
2020-11-10 14:23:28 +00:00
|
|
|
public currentFile: TFile;
|
2021-01-19 20:39:10 +00:00
|
|
|
public settings: BetterWordCountSettings;
|
2021-07-03 19:22:50 +00:00
|
|
|
public view: StatsView;
|
|
|
|
public dataManager: DataManager;
|
|
|
|
|
|
|
|
onunload(): void {
|
|
|
|
this.app.workspace
|
|
|
|
.getLeavesOfType(VIEW_TYPE_STATS)
|
|
|
|
.forEach((leaf) => leaf.detach());
|
|
|
|
}
|
2020-11-09 13:33:44 +00:00
|
|
|
|
2021-01-19 20:39:10 +00:00
|
|
|
async onload() {
|
2021-07-03 19:22:50 +00:00
|
|
|
this.dataManager = new DataManager(this.app.vault);
|
|
|
|
|
2020-11-09 13:33:44 +00:00
|
|
|
let statusBarEl = this.addStatusBarItem();
|
|
|
|
this.statusBar = new StatusBar(statusBarEl);
|
|
|
|
|
|
|
|
this.recentlyTyped = false;
|
|
|
|
|
2021-01-19 20:39:10 +00:00
|
|
|
this.settings = (await this.loadData()) || new BetterWordCountSettings();
|
|
|
|
this.addSettingTab(new BetterWordCountSettingsTab(this.app, this));
|
|
|
|
|
2021-07-03 19:22:50 +00:00
|
|
|
addIcon(STATS_ICON_NAME, STATS_ICON);
|
|
|
|
|
2021-04-02 21:24:17 +00:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
|
2021-07-03 19:22:50 +00:00
|
|
|
this.registerEvent(
|
|
|
|
this.app.vault.on(
|
|
|
|
"modify",
|
|
|
|
this.dataManager.onVaultModify,
|
|
|
|
this.dataManager
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.addCommand({
|
|
|
|
id: "show-vault-stats-view",
|
|
|
|
name: "Open view",
|
|
|
|
checkCallback: (checking: boolean) => {
|
|
|
|
if (checking) {
|
|
|
|
return this.app.workspace.getLeavesOfType("vault-stats").length === 0;
|
|
|
|
}
|
|
|
|
this.initLeaf();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.registerView(
|
|
|
|
VIEW_TYPE_STATS,
|
|
|
|
(leaf: WorkspaceLeaf) => (this.view = new StatsView(leaf))
|
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2021-07-03 19:22:50 +00:00
|
|
|
|
|
|
|
if (this.app.workspace.layoutReady) {
|
|
|
|
this.initLeaf();
|
|
|
|
}
|
2020-11-09 13:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-01-19 22:41:11 +00:00
|
|
|
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;
|
2021-01-19 16:36:58 +00:00
|
|
|
|
2020-11-09 13:33:44 +00:00
|
|
|
if (leaf && leaf.view.getViewType() === "markdown") {
|
|
|
|
this.recentlyTyped = true;
|
|
|
|
this.updateWordCount(contents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 22:41:11 +00:00
|
|
|
async updateAltCount() {
|
2021-02-17 04:29:24 +00:00
|
|
|
const files = getFilesCount(this.app.vault.getFiles());
|
2021-01-19 22:41:11 +00:00
|
|
|
|
2021-04-02 21:24:17 +00:00
|
|
|
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()) {
|
2021-04-02 21:24:17 +00:00
|
|
|
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);
|
2021-01-19 22:41:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 13:33:44 +00:00
|
|
|
updateWordCount(text: string) {
|
2021-01-19 20:39:10 +00:00
|
|
|
let displayText: string = "";
|
|
|
|
if (this.settings.showWords) {
|
|
|
|
displayText =
|
|
|
|
displayText +
|
|
|
|
this.settings.wordsPrefix +
|
2021-02-17 04:29:24 +00:00
|
|
|
getWordCount(text) +
|
2021-01-19 20:39:10 +00:00
|
|
|
this.settings.wordsSuffix;
|
|
|
|
}
|
|
|
|
if (this.settings.showCharacters) {
|
|
|
|
displayText =
|
|
|
|
displayText +
|
|
|
|
this.settings.charactersPrefix +
|
2021-02-17 04:29:24 +00:00
|
|
|
getCharacterCount(text) +
|
2021-01-19 20:39:10 +00:00
|
|
|
this.settings.charactersSuffix;
|
|
|
|
}
|
|
|
|
if (this.settings.showSentences) {
|
|
|
|
displayText =
|
|
|
|
displayText +
|
|
|
|
this.settings.sentencesPrefix +
|
2021-02-17 04:29:24 +00:00
|
|
|
getSentenceCount(text) +
|
2021-01-19 20:39:10 +00:00
|
|
|
this.settings.sentencesSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.statusBar.displayText(displayText);
|
2020-11-09 13:33:44 +00:00
|
|
|
}
|
2021-07-03 19:22:50 +00:00
|
|
|
|
|
|
|
initLeaf(): void {
|
|
|
|
if (this.app.workspace.getLeavesOfType(VIEW_TYPE_STATS).length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.app.workspace.getRightLeaf(false).setViewState({
|
|
|
|
type: VIEW_TYPE_STATS,
|
|
|
|
});
|
|
|
|
}
|
2020-11-09 13:33:44 +00:00
|
|
|
}
|