Merge branch 'hotfix/0.2.2' into develop

This commit is contained in:
lukeleppan 2020-11-10 16:25:56 +02:00
commit b6032d58e3
3 changed files with 33 additions and 25 deletions

View file

@ -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",

View file

@ -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": {

View file

@ -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;