Skip to content

Commit

Permalink
Merge pull request #9 from anselmwang/reveal-only-when-file-explorer-…
Browse files Browse the repository at this point in the history
…visible

fix: only reveal when file explorer is visible
  • Loading branch information
shichongrui authored Mar 16, 2022
2 parents 8d1372b + 707cf61 commit 5755eb7
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
import { Plugin, Command } from 'obsidian';

import { Plugin, Command, Workspace } from 'obsidian';
declare module "obsidian" {
interface WorkspaceLeaf {
width: Number;
}
}
export default class MyPlugin extends Plugin {
private is_file_explorer_open_previously = false;

private is_file_explorer_open()
{
const workspace = this.app.workspace;
let is_open = false;
workspace.iterateAllLeaves((leaf) => {
if(leaf.getViewState().type == "file-explorer" && leaf.width > 0)
{
is_open = true;
}
});
return is_open;

}
private reveal()
{
(this.app as any).commands.executeCommandById('file-explorer:reveal-active-file');
}

onload() {
this.is_file_explorer_open_previously = this.is_file_explorer_open();

this.app.workspace.on('file-open', () => {
(this.app as any).commands.executeCommandById('file-explorer:reveal-active-file');
})
if(this.is_file_explorer_open())
{
this.reveal();
}
})

this.app.workspace.on('click', async () => {
await new Promise(resolve => setTimeout(resolve, 200));
const is_file_explorer_open_now = this.is_file_explorer_open();
console.log(`is_file_explorer_open_previously: ${this.is_file_explorer_open_previously}, is_file_explorer_open_now: ${is_file_explorer_open_now}`);
if(is_file_explorer_open_now && ! this.is_file_explorer_open_previously)
{
this.reveal();
}
this.is_file_explorer_open_previously = is_file_explorer_open_now;
})
}
}

0 comments on commit 5755eb7

Please sign in to comment.