diff --git a/manifest.json b/manifest.json index bbe025e..dc6c957 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "better-word-count", "name": "Better Word Count", - "version": "0.2.1", + "version": "0.2.2", "description": "Counts the words of selected text in the editor.", "author": "Luke Leppan", "authorUrl": "https://lukeleppan.com", diff --git a/package.json b/package.json index ed5fe66..5a01ac1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "better-word-count", - "version": "0.2.1", + "version": "0.2.2", "description": "Counts the words of selected text in the editor.", "main": "main.js", "scripts": { diff --git a/src/main.ts b/src/main.ts index a42d783..2516804 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,10 @@ -import { App, MarkdownView, Modal, Plugin, TFile } from "obsidian"; +import { MarkdownView, Plugin, TFile } from "obsidian"; import { StatusBar } from "./status-bar"; export default class BetterWordCount extends Plugin { public recentlyTyped: boolean; public statusBar: StatusBar; + public currentFile: TFile; onload() { let statusBarEl = this.addStatusBarItem(); @@ -19,6 +20,32 @@ export default class BetterWordCount extends Plugin { this.app.workspace.on("quick-preview", this.onQuickPreview, this) ); + this.registerInterval( + window.setInterval(async () => { + let activeLeaf = this.app.workspace.activeLeaf; + + 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 ( + this.currentFile && + this.currentFile.extension === "md" && + !this.recentlyTyped + ) { + const contents = await this.app.vault.read(this.currentFile); + this.updateWordCount(contents); + } else if (!this.recentlyTyped) { + this.updateWordCount(""); + } + }, 500) + ); + let activeLeaf = this.app.workspace.activeLeaf; let files: TFile[] = this.app.vault.getMarkdownFiles(); @@ -30,37 +57,18 @@ export default class BetterWordCount extends Plugin { } async onFileOpen(file: TFile) { + this.currentFile = file; if (file && file.extension === "md") { const contents = await this.app.vault.cachedRead(file); + this.recentlyTyped = true; this.updateWordCount(contents); } else { this.updateWordCount(""); } - - let activeLeaf = this.app.workspace.activeLeaf; - if (!activeLeaf || !(activeLeaf.view instanceof MarkdownView)) { - return; - } - - let editor = activeLeaf.view.sourceMode.cmEditor; - - activeLeaf.view.registerInterval( - window.setInterval(async () => { - if (editor.somethingSelected()) { - let content: string = editor.getSelection(); - this.updateWordCount(content); - this.recentlyTyped = false; - } else if (file && file.extension === "md" && !this.recentlyTyped) { - const contents = await this.app.vault.cachedRead(file); - this.updateWordCount(contents); - } else if (!this.recentlyTyped) { - this.updateWordCount(""); - } - }, 500) - ); } onQuickPreview(file: TFile, contents: string) { + this.currentFile = file; const leaf = this.app.workspace.activeLeaf; if (leaf && leaf.view.getViewType() === "markdown") { this.recentlyTyped = true;