Hello Everyone!
I am not sure if this used to work earlier, or I never tested this. But say, I have my custom leaf opened and its not in active state right now → Then I closed the Obsidian → Then opened Obsidian again. Now, when my plugin tries to get the leaf using getLeavesOfType(), it receives leaves = []. I am calling it inside onLayoutReady().
Here is where I added a debugger :
console.log("Task Board : Uninstalling...");
// deleteAllLocalStorageKeys(); // TODO : Enable this while production build. This is disabled for testing purpose because the data from localStorage is required for testing.
// onUnloadSave(this.plugin);
// this.app.workspace.detachLeavesOfType(VIEW_TYPE_TASKBOARD);
}
async activateView(leafLayout: string) {
let leaf: WorkspaceLeaf | null = null;
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_TASKBOARD);
function isFromMainWindow(leaf: WorkspaceLeaf): boolean | undefined {
if (!leaf.view.containerEl.ownerDocument.defaultView) return;
return "Notice" in leaf.view.containerEl.ownerDocument.defaultView;
}
// Separate leaves into MainWindow and SeparateWindow categories
const mainWindowLeaf = leaves.find((leaf) => isFromMainWindow(leaf));
const separateWindowLeaf = leaves.find(
(leaf) => !isFromMainWindow(leaf),
);
Here is an image for more context :
Another approach I was thinking is by using the getLeafById(), but for that I will need to store the leaf ID . Ill think about this approach…
But, is there anything wrong with the current approach and why I am not able to retrieve the leaf?
You should register VIEW_TYPE_TASKBOARD directly in plugin.onload instead of in workspace.onLayoutReady.
plugin.registerView will replace all existing Views of this type with EmptyView
1 Like
I cant believe it!
I knew the Obsidian architecture, but never thought of that, I wish I could have looked my own code a little closer.
Thank you for the solution, its working👍