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
This commit is contained in:
SebastianMC 2025-03-28 15:24:47 +01:00
parent 2342b08e5f
commit b8708f81e9
1 changed files with 47 additions and 6 deletions

View File

@ -201,7 +201,7 @@ export default class CustomSortPlugin
let fileExplorerOrError = this.getFileExplorer() let fileExplorerOrError = this.getFileExplorer()
if (fileExplorerOrError.e === FileExplorerStateError.DeferredView) { if (fileExplorerOrError.e === FileExplorerStateError.DeferredView) {
if (logWarning) { if (logWarning) {
this.logWarningFileExplorerDeferred() this.logDeferredFileExplorerInfo()
} }
return fileExplorerOrError return fileExplorerOrError
} }
@ -269,12 +269,17 @@ export default class CustomSortPlugin
} }
} }
logWarningFileExplorerDeferred() { logDeferredFileExplorerInfo() {
const msg = `${PLUGIN_ID} v${this.manifest.version}: failed to get active File Explorer view.\n` const msg = `${PLUGIN_ID} v${this.manifest.version}: File Explorer is not displayed yet (Obsidian deferred view detected).\n`
+ `Until the File Explorer is visible, the custom-sort plugin cannot apply the custom order.\n` + `Until the File Explorer is visible, the custom-sort plugin cannot apply the custom order.\n`
console.warn(msg) console.warn(msg)
} }
logDeferredFileExplorerWatcherSetupInfo() {
const msg = `${PLUGIN_ID} v${this.manifest.version}: Set up a watcher to apply custom sort automatically when the File Explorer is displayed.\n`
console.warn(msg)
}
logWarningFileExplorerNotAvailable() { logWarningFileExplorerNotAvailable() {
const msg = `${PLUGIN_ID} v${this.manifest.version}: failed to locate File Explorer. The 'Files' core plugin can be disabled.\n` const msg = `${PLUGIN_ID} v${this.manifest.version}: failed to locate File Explorer. The 'Files' core plugin can be disabled.\n`
+ `Some community plugins can also disable it.\n` + `Some community plugins can also disable it.\n`
@ -583,7 +588,7 @@ export default class CustomSortPlugin
const plugin = this const plugin = this
this.app.workspace.onLayoutReady(() => { this.app.workspace.onLayoutReady(() => {
setTimeout(() => { setTimeout(() => {
plugin.initialDelayedApplicationOfCustomSorting.apply(this) plugin.delayedApplicationOfCustomSorting.apply(this)
}, },
plugin.settings.delayForInitialApplication) plugin.settings.delayForInitialApplication)
}) })
@ -682,7 +687,30 @@ export default class CustomSortPlugin
return false return false
} }
initialDelayedApplicationOfCustomSorting() { setWatcherForDelayedFileExplorerView() {
const self = this
const fullyFledgedFileExplorerElementSelector = () => document.querySelector('[data-type="file-explorer"] .nav-files-container');
const mutationObserver = new MutationObserver((_, observerInstance) => {
const fullyFledgedFileExplorerElement = fullyFledgedFileExplorerElementSelector();
if (fullyFledgedFileExplorerElement) {
observerInstance.disconnect();
self.delayedApplicationOfCustomSorting(true)
}
});
const workspaceElement = document.querySelector(".workspace");
if (workspaceElement) {
mutationObserver.observe(workspaceElement, {
childList: true,
subtree: true
});
}
}
// Entering this method for the first time after initial delay after plugin loaded (via setTimeout()),
// and if first attempt is unsuccessful, then entering this method again from DOM watcher, when
// the File Explorer view gets transformed from delayed view into fully-fledged active view
delayedApplicationOfCustomSorting(fromDOMwatcher?: boolean) {
if (!this?.isThePluginStillInstalledAndEnabled()) { if (!this?.isThePluginStillInstalledAndEnabled()) {
console.log(`${PLUGIN_ID} v${this.manifest.version} - delayed handler skipped, plugin no longer active.`) console.log(`${PLUGIN_ID} v${this.manifest.version} - delayed handler skipped, plugin no longer active.`)
return return
@ -695,8 +723,21 @@ export default class CustomSortPlugin
return return
} }
if (!fromDOMwatcher) {
// Only for the first delayed invocation:
// If file explorer is delayed, configure the watcher
// NOTE: Do not configure the watcher if the file explorer is not available
let fileExplorerOrError: FileExplorerLeafOrError = this.checkFileExplorerIsAvailableAndPatchable()
if (fileExplorerOrError.e === FileExplorerStateError.DeferredView) {
this.logDeferredFileExplorerWatcherSetupInfo()
this.setWatcherForDelayedFileExplorerView()
} else { // file explorer is available or does not exist
this.switchPluginStateTo(true) this.switchPluginStateTo(true)
} }
} else {
this.switchPluginStateTo(true)
}
}
setRibbonIconToEnabled() { setRibbonIconToEnabled() {
setIcon(this.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE) setIcon(this.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE)