Skip to content

Commit 890aa42

Browse files
committed
Finetuning initial application of custom sort order:
- console messages more accurate - DOM-based watcher for deferred File Explorer view to automatically apply custom sort when File Explorer view is actually displayed
1 parent b62fa95 commit 890aa42

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/main.ts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default class CustomSortPlugin
201201
let fileExplorerOrError = this.getFileExplorer()
202202
if (fileExplorerOrError.e === FileExplorerStateError.DeferredView) {
203203
if (logWarning) {
204-
this.logWarningFileExplorerDeferred()
204+
this.logDeferredFileExplorerInfo()
205205
}
206206
return fileExplorerOrError
207207
}
@@ -269,12 +269,17 @@ export default class CustomSortPlugin
269269
}
270270
}
271271

272-
logWarningFileExplorerDeferred() {
273-
const msg = `${PLUGIN_ID} v${this.manifest.version}: failed to get active File Explorer view.\n`
272+
logDeferredFileExplorerInfo() {
273+
const msg = `${PLUGIN_ID} v${this.manifest.version}: File Explorer is not displayed yet (Obsidian deferred view detected).\n`
274274
+ `Until the File Explorer is visible, the custom-sort plugin cannot apply the custom order.\n`
275275
console.warn(msg)
276276
}
277277

278+
logDeferredFileExplorerWatcherSetupInfo() {
279+
const msg = `${PLUGIN_ID} v${this.manifest.version}: Set up a watcher to apply custom sort automatically when the File Explorer is displayed.\n`
280+
console.warn(msg)
281+
}
282+
278283
logWarningFileExplorerNotAvailable() {
279284
const msg = `${PLUGIN_ID} v${this.manifest.version}: failed to locate File Explorer. The 'Files' core plugin can be disabled.\n`
280285
+ `Some community plugins can also disable it.\n`
@@ -583,7 +588,7 @@ export default class CustomSortPlugin
583588
const plugin = this
584589
this.app.workspace.onLayoutReady(() => {
585590
setTimeout(() => {
586-
plugin.initialDelayedApplicationOfCustomSorting.apply(this)
591+
plugin.delayedApplicationOfCustomSorting.apply(this)
587592
},
588593
plugin.settings.delayForInitialApplication)
589594
})
@@ -682,7 +687,30 @@ export default class CustomSortPlugin
682687
return false
683688
}
684689

685-
initialDelayedApplicationOfCustomSorting() {
690+
setWatcherForDelayedFileExplorerView() {
691+
const self = this
692+
const fullyFledgedFileExplorerElementSelector = () => document.querySelector('[data-type="file-explorer"] .nav-files-container');
693+
694+
const mutationObserver = new MutationObserver((_, observerInstance) => {
695+
const fullyFledgedFileExplorerElement = fullyFledgedFileExplorerElementSelector();
696+
if (fullyFledgedFileExplorerElement) {
697+
observerInstance.disconnect();
698+
self.delayedApplicationOfCustomSorting(true)
699+
}
700+
});
701+
const workspaceElement = document.querySelector(".workspace");
702+
if (workspaceElement) {
703+
mutationObserver.observe(workspaceElement, {
704+
childList: true,
705+
subtree: true
706+
});
707+
}
708+
}
709+
710+
// Entering this method for the first time after initial delay after plugin loaded (via setTimeout()),
711+
// and if first attempt is unsuccessful, then entering this method again from DOM watcher, when
712+
// the File Explorer view gets transformed from delayed view into fully-fledged active view
713+
delayedApplicationOfCustomSorting(fromDOMwatcher?: boolean) {
686714
if (!this?.isThePluginStillInstalledAndEnabled()) {
687715
console.log(`${PLUGIN_ID} v${this.manifest.version} - delayed handler skipped, plugin no longer active.`)
688716
return
@@ -695,7 +723,20 @@ export default class CustomSortPlugin
695723
return
696724
}
697725

698-
this.switchPluginStateTo(true)
726+
if (!fromDOMwatcher) {
727+
// Only for the first delayed invocation:
728+
// If file explorer is delayed, configure the watcher
729+
// NOTE: Do not configure the watcher if the file explorer is not available
730+
let fileExplorerOrError: FileExplorerLeafOrError = this.checkFileExplorerIsAvailableAndPatchable()
731+
if (fileExplorerOrError.e === FileExplorerStateError.DeferredView) {
732+
this.logDeferredFileExplorerWatcherSetupInfo()
733+
this.setWatcherForDelayedFileExplorerView()
734+
} else { // file explorer is available or does not exist
735+
this.switchPluginStateTo(true)
736+
}
737+
} else {
738+
this.switchPluginStateTo(true)
739+
}
699740
}
700741

701742
setRibbonIconToEnabled() {

0 commit comments

Comments
 (0)