From adc52d3ecf53ab977e9bfab0be372fc0d16bfd8b Mon Sep 17 00:00:00 2001 From: Yu Wang Date: Thu, 3 Mar 2022 20:29:15 -0800 Subject: [PATCH 1/4] fix: only reveal when file explorer is visible --- main.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/main.ts b/main.ts index 859af81..9a55780 100644 --- a/main.ts +++ b/main.ts @@ -1,9 +1,20 @@ import { Plugin, Command } from 'obsidian'; - +declare module "obsidian" { + interface WorkspaceLeaf { + width: Number; + } +} export default class MyPlugin extends Plugin { onload() { this.app.workspace.on('file-open', () => { - (this.app as any).commands.executeCommandById('file-explorer:reveal-active-file'); - }) + const workspace = this.app.workspace; + workspace.iterateAllLeaves((leaf) => { + if(leaf.getViewState().type == "file-explorer" && leaf.width > 0) + { + (this.app as any).commands.executeCommandById('file-explorer:reveal-active-file'); + } + }); + + }) } } From cf22ba00fdeaa8c0857805b1d46dbb4fc53815b4 Mon Sep 17 00:00:00 2001 From: Yu Wang Date: Sun, 13 Mar 2022 06:53:40 -0700 Subject: [PATCH 2/4] fix: only reveal when file explorer is visible The current experience is: - When file explorer is visible, auto reveal - When file explorer is visible, disable auto reveal, so the sidebar will not auto popup. The reasons are two folds: 1) the auto popup creates a distraction while writing. 2) when using in a reduced window size auto popup ceates a hassle to hide sidebar everytime I create or navigate a new page - When file explorer is changed from hidden to visible, auto reveal. So the experience is still very similar to original plugin. --- main.ts | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/main.ts b/main.ts index 9a55780..98a2861 100644 --- a/main.ts +++ b/main.ts @@ -1,20 +1,47 @@ -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', () => { - const workspace = this.app.workspace; - workspace.iterateAllLeaves((leaf) => { - if(leaf.getViewState().type == "file-explorer" && leaf.width > 0) - { - (this.app as any).commands.executeCommandById('file-explorer:reveal-active-file'); - } - }); + if(this.is_file_explorer_open()) + { + this.reveal(); + } + }) + this.app.workspace.on('click', () => { + const is_file_explorer_open_now = this.is_file_explorer_open(); + 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; }) } } From 669689a562dc639c5fd37af86ea9db00db44740f Mon Sep 17 00:00:00 2001 From: Yu Wang Date: Sun, 13 Mar 2022 08:06:16 -0700 Subject: [PATCH 3/4] fix: add delay to wait for file explorer open --- main.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index 98a2861..be8f915 100644 --- a/main.ts +++ b/main.ts @@ -35,8 +35,10 @@ export default class MyPlugin extends Plugin { } }) - this.app.workspace.on('click', () => { + this.app.workspace.on('click', async () => { + await new Promise(resolve => setTimeout(resolve, 500)); 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(); From 707cf61e8d3877abfbfa8b630cc133ea71166b04 Mon Sep 17 00:00:00 2001 From: Yu Wang Date: Sun, 13 Mar 2022 08:18:54 -0700 Subject: [PATCH 4/4] fix: shorten delay time When shows the filer explorer on mobile, if the file_explorer is not reveal current file, the file explorer will reopen. Long delay time like 500ms is not quite smooth --- main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.ts b/main.ts index be8f915..bb7e65a 100644 --- a/main.ts +++ b/main.ts @@ -36,7 +36,7 @@ export default class MyPlugin extends Plugin { }) this.app.workspace.on('click', async () => { - await new Promise(resolve => setTimeout(resolve, 500)); + 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)