From 142923be9777b847e157e74fe281c9fbb80a9de5 Mon Sep 17 00:00:00 2001 From: lukeleppan Date: Tue, 10 Nov 2020 16:19:55 +0200 Subject: [PATCH 1/3] flicker fix --- src/main.ts | 58 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main.ts b/src/main.ts index a42d783..15118c3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -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 { @@ -19,6 +19,39 @@ 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; + let files: TFile[] = this.app.vault.getMarkdownFiles(); + let currentFile: TFile; + + files.forEach((file) => { + if ((file.basename = activeLeaf.getDisplayText())) { + currentFile = file; + } + }); + 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 ( + currentFile && + currentFile.extension === "md" && + !this.recentlyTyped + ) { + const contents = await this.app.vault.read(currentFile); + this.updateWordCount(contents); + } else if (!this.recentlyTyped) { + this.updateWordCount(""); + } + }, 500) + ); + let activeLeaf = this.app.workspace.activeLeaf; let files: TFile[] = this.app.vault.getMarkdownFiles(); @@ -32,32 +65,11 @@ export default class BetterWordCount extends Plugin { async onFileOpen(file: TFile) { 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) { From ec914ea36b2551e663615520f390079d69730a8c Mon Sep 17 00:00:00 2001 From: lukeleppan Date: Tue, 10 Nov 2020 16:23:28 +0200 Subject: [PATCH 2/3] fix ghost count bug --- src/main.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main.ts b/src/main.ts index 15118c3..2516804 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ 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(); @@ -22,14 +23,7 @@ export default class BetterWordCount extends Plugin { this.registerInterval( window.setInterval(async () => { let activeLeaf = this.app.workspace.activeLeaf; - let files: TFile[] = this.app.vault.getMarkdownFiles(); - let currentFile: TFile; - files.forEach((file) => { - if ((file.basename = activeLeaf.getDisplayText())) { - currentFile = file; - } - }); if (!activeLeaf || !(activeLeaf.view instanceof MarkdownView)) { return; } @@ -40,11 +34,11 @@ export default class BetterWordCount extends Plugin { this.updateWordCount(content); this.recentlyTyped = false; } else if ( - currentFile && - currentFile.extension === "md" && + this.currentFile && + this.currentFile.extension === "md" && !this.recentlyTyped ) { - const contents = await this.app.vault.read(currentFile); + const contents = await this.app.vault.read(this.currentFile); this.updateWordCount(contents); } else if (!this.recentlyTyped) { this.updateWordCount(""); @@ -63,6 +57,7 @@ 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; @@ -73,6 +68,7 @@ export default class BetterWordCount extends Plugin { } onQuickPreview(file: TFile, contents: string) { + this.currentFile = file; const leaf = this.app.workspace.activeLeaf; if (leaf && leaf.view.getViewType() === "markdown") { this.recentlyTyped = true; From ae5ba2207981ba54708c009328846fd3745e3fd3 Mon Sep 17 00:00:00 2001 From: lukeleppan Date: Tue, 10 Nov 2020 16:25:45 +0200 Subject: [PATCH 3/3] bump --- manifest.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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": {