Fix counts not loading or appearing on start; add manual recount command and button
This commit is contained in:
parent
0b60934f06
commit
5a51033da8
66
main.ts
66
main.ts
|
@ -32,7 +32,9 @@ interface FileItem {
|
||||||
|
|
||||||
export default class NovelWordCountPlugin extends Plugin {
|
export default class NovelWordCountPlugin extends Plugin {
|
||||||
savedData: NovelWordCountSavedData;
|
savedData: NovelWordCountSavedData;
|
||||||
settings: NovelWordCountSettings;
|
get settings(): NovelWordCountSettings {
|
||||||
|
return this.savedData.settings;
|
||||||
|
}
|
||||||
fileHelper: FileHelper;
|
fileHelper: FileHelper;
|
||||||
|
|
||||||
constructor(app: App, manifest: PluginManifest) {
|
constructor(app: App, manifest: PluginManifest) {
|
||||||
|
@ -46,10 +48,16 @@ export default class NovelWordCountPlugin extends Plugin {
|
||||||
await this.loadSettings();
|
await this.loadSettings();
|
||||||
this.addSettingTab(new NovelWordCountSettingTab(this.app, this));
|
this.addSettingTab(new NovelWordCountSettingTab(this.app, this));
|
||||||
|
|
||||||
this.handleEvents();
|
this.addCommand({
|
||||||
|
id: "recount-vault",
|
||||||
|
name: "Recount all documents in vault",
|
||||||
|
callback: async () => {
|
||||||
|
await this.initialize();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
await this.refreshAllCounts();
|
this.handleEvents();
|
||||||
await this.updateDisplayedCounts();
|
this.initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
async onunload() {
|
async onunload() {
|
||||||
|
@ -63,11 +71,11 @@ export default class NovelWordCountPlugin extends Plugin {
|
||||||
{},
|
{},
|
||||||
await this.loadData()
|
await this.loadData()
|
||||||
) as NovelWordCountSavedData;
|
) as NovelWordCountSavedData;
|
||||||
const settings: NovelWordCountSettings | null = this.savedData
|
this.savedData.settings = Object.assign(
|
||||||
? this.savedData.settings
|
{},
|
||||||
: null;
|
DEFAULT_SETTINGS,
|
||||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, settings);
|
this.savedData.settings
|
||||||
this.savedData.settings = this.settings;
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveSettings() {
|
async saveSettings() {
|
||||||
|
@ -76,7 +84,16 @@ export default class NovelWordCountPlugin extends Plugin {
|
||||||
|
|
||||||
// PUBLIC
|
// PUBLIC
|
||||||
|
|
||||||
|
public async initialize() {
|
||||||
|
await this.refreshAllCounts();
|
||||||
|
await this.updateDisplayedCounts();
|
||||||
|
}
|
||||||
|
|
||||||
public async updateDisplayedCounts() {
|
public async updateDisplayedCounts() {
|
||||||
|
if (!this.savedData.cachedCounts.hasOwnProperty("/")) {
|
||||||
|
await this.refreshAllCounts();
|
||||||
|
}
|
||||||
|
|
||||||
const fileExplorerLeaf = await this.getFileExplorerLeaf();
|
const fileExplorerLeaf = await this.getFileExplorerLeaf();
|
||||||
const fileItems: { [path: string]: FileItem } = (
|
const fileItems: { [path: string]: FileItem } = (
|
||||||
fileExplorerLeaf.view as any
|
fileExplorerLeaf.view as any
|
||||||
|
@ -134,6 +151,13 @@ export default class NovelWordCountPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleEvents(): void {
|
private handleEvents(): void {
|
||||||
|
this.registerEvent(
|
||||||
|
this.app.workspace.on("file-open", async () => {
|
||||||
|
await this.loadSettings();
|
||||||
|
await this.updateDisplayedCounts();
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
this.registerEvent(
|
this.registerEvent(
|
||||||
this.app.vault.on("modify", async (file) => {
|
this.app.vault.on("modify", async (file) => {
|
||||||
await this.fileHelper.updateFileCounts(
|
await this.fileHelper.updateFileCounts(
|
||||||
|
@ -154,6 +178,7 @@ export default class NovelWordCountPlugin extends Plugin {
|
||||||
|
|
||||||
private async refreshAllCounts() {
|
private async refreshAllCounts() {
|
||||||
this.savedData.cachedCounts = await this.fileHelper.getAllFileCounts();
|
this.savedData.cachedCounts = await this.fileHelper.getAllFileCounts();
|
||||||
|
await this.saveSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,5 +211,28 @@ class NovelWordCountSettingTab extends PluginSettingTab {
|
||||||
await this.plugin.updateDisplayedCounts();
|
await this.plugin.updateDisplayedCounts();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName("Recount all documents")
|
||||||
|
.setDesc(
|
||||||
|
"If changes have occurred outside of Obsidian, you may need to trigger a manual recount"
|
||||||
|
)
|
||||||
|
.addButton((button) =>
|
||||||
|
button
|
||||||
|
.setButtonText("Recount")
|
||||||
|
.setCta()
|
||||||
|
.onClick(async () => {
|
||||||
|
button.disabled = true;
|
||||||
|
await this.plugin.initialize();
|
||||||
|
button.setButtonText("Done");
|
||||||
|
button.removeCta();
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
button.setButtonText("Recount");
|
||||||
|
button.setCta();
|
||||||
|
button.disabled = false;
|
||||||
|
}, 1000);
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue