From 858173e9803f20144ad86f224be59a4bc844f5f1 Mon Sep 17 00:00:00 2001 From: Mara Date: Wed, 8 Jun 2022 11:34:03 +0200 Subject: [PATCH] fix: obsidian return null after opening Adding a little sleep (50ms) to wait for the dom after opening obsidian --- src/main.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main.ts b/src/main.ts index 5f3ee9c..3419f35 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,7 @@ import { changePathVisibility } from './utils'; interface FileHiderSettings { hidden: boolean; hiddenList: string[]; -}; +} export default class FileHider extends Plugin { @@ -20,7 +20,6 @@ export default class FileHider extends Plugin { async onload() { await this.loadSettings(); - this.registerEvent( this.app.workspace.on(`file-menu`, (menu, file) => { if (file instanceof TFolder) { @@ -39,34 +38,36 @@ export default class FileHider extends Plugin { this.settings.hiddenList.push(file.path); this.saveSettings(); }); - }; + } }); } else { menu.addItem((i) => { if (this.settings.hiddenList.includes(file.path)) { i.setTitle(`Unhide File`) .setIcon(`eye`) - .onClick((e) => { + .onClick(() => { this.unhidePath(file.path); }); } else { i.setTitle(`Hide File`) .setIcon(`eye-off`) - .onClick((e) => { + .onClick(() => { changePathVisibility(file.path, this.settings.hidden); this.settings.hiddenList.push(file.path); this.saveSettings(); }); - }; + } }); - }; + } }) ); - this.app.workspace.onLayoutReady(() => { + + this.app.workspace.onLayoutReady( async () => { + await sleep(50) for (const path of this.settings.hiddenList) { - changePathVisibility(path, this.settings.hidden); - }; + await changePathVisibility(path, this.settings.hidden); + } }); new VisibilityToggleCommand(this); @@ -88,19 +89,19 @@ export default class FileHider extends Plugin { /* Enables/Disables the file visibility based. (gets the stylesheet if needed) */ - toggleVisibility() { + async toggleVisibility() { this.settings.hidden = !this.settings.hidden; for (const path of this.settings.hiddenList) { changePathVisibility(path, this.settings.hidden); - }; - this.saveSettings(); + } + await this.saveSettings(); }; - unhidePath(path: string) { + async unhidePath(path: string) { let i = this.settings.hiddenList.indexOf(path); this.settings.hiddenList.splice(i, 1); changePathVisibility(path, false); - this.saveSettings(); + await this.saveSettings(); }; }; @@ -123,4 +124,4 @@ class FileHiderSettingsTab extends PluginSettingTab { VisibilityToggleSetting.create(this.plugin, container); ManageHiddenPaths.create(this.plugin, container); }; -}; \ No newline at end of file +}