generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from anselmwang/reveal-only-when-file-explorer-…
…visible fix: only reveal when file explorer is visible
- Loading branch information
Showing
1 changed file
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}) | ||
} | ||
} |