Skip to content

Commit

Permalink
fix(initialize): add more retrys to get FileExplorer view
Browse files Browse the repository at this point in the history
fix #21, close #12
  • Loading branch information
aidenlx committed Sep 15, 2021
1 parent f1ed456 commit 38625dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions manifest.json
Expand Up @@ -4,7 +4,7 @@
"version": "0.11.1",
"minAppVersion": "0.12.5",
"description": "Add description, summary and more info to folders with folder notes.",
"author": "",
"authorUrl": "",
"isDesktopOnly": true
"author": "AidenLx",
"authorUrl": "https://github.com/aidenlx",
"isDesktopOnly": false
}
32 changes: 24 additions & 8 deletions src/initialize.ts
@@ -1,4 +1,4 @@
import { AFItem, FileExplorer } from "obsidian";
import { AFItem, App, FileExplorer } from "obsidian";

import ALxFolderNote from "./fn-main";
import { isFolder } from "./misc";
Expand All @@ -7,12 +7,28 @@ import { noHideMark } from "./settings";

export default function initialize(this: ALxFolderNote, revert = false) {
PatchRevealInExplorer(this);
const leaves = this.app.workspace.getLeavesOfType("file-explorer");
if (leaves.length > 1) console.error("more then one file-explorer");
else if (leaves.length < 1) console.error("file-explorer not found");
else {
const fileExplorer = leaves[0].view as FileExplorer;
const feHandler = new FEHandler(this, fileExplorer);

const doWithFileExplorer = (callback: (view: FileExplorer) => void) => {
let leaves,
count = 0;
const tryGetView = () => {
leaves = this.app.workspace.getLeavesOfType("file-explorer");
if (leaves.length === 0) {
if (count++ > 5) console.error("failed to get file-explorer");
else {
console.log("file-explorer not found, retrying...");
setTimeout(tryGetView, 500);
}
} else {
if (leaves.length > 1) console.warn("more then one file-explorer");
callback(leaves[0].view as FileExplorer);
}
};
tryGetView();
};

doWithFileExplorer((view) => {
const feHandler = new FEHandler(this, view);
this.feHandler = feHandler;
/** get all AbstractFile (file+folder) and attach event */
const setupClick = (re: boolean) => {
Expand All @@ -26,5 +42,5 @@ export default function initialize(this: ALxFolderNote, revert = false) {
setupClick(revert);
feHandler.markAll(revert);
document.body.toggleClass(noHideMark, !this.settings.hideNoteInExplorer);
}
});
}

0 comments on commit 38625dc

Please sign in to comment.