Merge branch 'hotfix/0.2.2' into develop
This commit is contained in:
commit
b6032d58e3
3 changed files with 33 additions and 25 deletions
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"id": "better-word-count",
|
"id": "better-word-count",
|
||||||
"name": "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.",
|
"description": "Counts the words of selected text in the editor.",
|
||||||
"author": "Luke Leppan",
|
"author": "Luke Leppan",
|
||||||
"authorUrl": "https://lukeleppan.com",
|
"authorUrl": "https://lukeleppan.com",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "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.",
|
"description": "Counts the words of selected text in the editor.",
|
||||||
"main": "main.js",
|
"main": "main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
54
src/main.ts
54
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";
|
import { StatusBar } from "./status-bar";
|
||||||
|
|
||||||
export default class BetterWordCount extends Plugin {
|
export default class BetterWordCount extends Plugin {
|
||||||
public recentlyTyped: boolean;
|
public recentlyTyped: boolean;
|
||||||
public statusBar: StatusBar;
|
public statusBar: StatusBar;
|
||||||
|
public currentFile: TFile;
|
||||||
|
|
||||||
onload() {
|
onload() {
|
||||||
let statusBarEl = this.addStatusBarItem();
|
let statusBarEl = this.addStatusBarItem();
|
||||||
|
@ -19,6 +20,32 @@ export default class BetterWordCount extends Plugin {
|
||||||
this.app.workspace.on("quick-preview", this.onQuickPreview, this)
|
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 activeLeaf = this.app.workspace.activeLeaf;
|
||||||
let files: TFile[] = this.app.vault.getMarkdownFiles();
|
let files: TFile[] = this.app.vault.getMarkdownFiles();
|
||||||
|
|
||||||
|
@ -30,37 +57,18 @@ export default class BetterWordCount extends Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
async onFileOpen(file: TFile) {
|
async onFileOpen(file: TFile) {
|
||||||
|
this.currentFile = file;
|
||||||
if (file && file.extension === "md") {
|
if (file && file.extension === "md") {
|
||||||
const contents = await this.app.vault.cachedRead(file);
|
const contents = await this.app.vault.cachedRead(file);
|
||||||
|
this.recentlyTyped = true;
|
||||||
this.updateWordCount(contents);
|
this.updateWordCount(contents);
|
||||||
} else {
|
} else {
|
||||||
this.updateWordCount("");
|
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) {
|
onQuickPreview(file: TFile, contents: string) {
|
||||||
|
this.currentFile = file;
|
||||||
const leaf = this.app.workspace.activeLeaf;
|
const leaf = this.app.workspace.activeLeaf;
|
||||||
if (leaf && leaf.view.getViewType() === "markdown") {
|
if (leaf && leaf.view.getViewType() === "markdown") {
|
||||||
this.recentlyTyped = true;
|
this.recentlyTyped = true;
|
||||||
|
|
Loading…
Reference in a new issue