What I’m trying to do
I am developing an Obsidian plugin called “FolderPeek” using TypeScript. This plugin allows users to preview files located under the active folder they click on.
The issue I am encountering is that I cannot disable the default behavior in Obsidian, where it automatically opens the previously viewed file after deleting a file, which seems to be a kind of “hook.”
By default, Obsidian automatically opens the file you viewed before the currently deleted one. For example, if you open files in the order A.md -> B.md -> C.md
, and you delete C.md
, B.md
will automatically open. I would like to disable this behavior. Instead, I want my plugin to open the file adjacent to the deleted one as the next active file. Here, “adjacent file” refers to the one next to the deleted file in the list, sorted by criteria like “created date” or “file name.” The default hook behavior does not align with my plugin’s purpose and is unnecessary.
Things I have tried
I tried the following Obsidian APIs for deleting files:
this.app.fileManager.trashFile(file);
await this.app.vault.delete(file);
await this.app.vault.adapter.remove(file.path);
However, all of these methods trigger the default hook to open a file automatically after deletion.
As a workaround, I tried closing the editor tab using the activeLeaf
to cancel the auto-opening behavior and then opening the adjacent file:
this.app.vault.on("delete", (file) => {
const activeLeaf = this.plugin.app.workspace.activeLeaf;
if (activeLeaf && activeLeaf?.view?.getViewType() === "markdown") {
activeLeaf.detach();
}
});
This approach works to some extent, but it has the following issues:
- Console Errors: The following errors appear in the console:
RangeError: Field is not present in this state
TypeError: Cannot read properties of null (reading 'children')
- Poor User Experience: Closing the tab and opening the adjacent file causes the UI to flicker, making it visually unpleasant.
What I want to achieve
What I want to achieve is that when a file is deleted from the active folder, the file adjacent to it (based on the sort order) is opened as the next active file. I would like to know if there is a better way to accomplish this without relying on closing the editor tab manually. Any advice or suggestions would be greatly appreciated!