@@ -78,11 +78,16 @@ type MonkeyAroundUninstaller = () => void
7878
7979type ContextMenuProvider = ( item : MenuItem ) => void
8080
81- enum FileExplorerStateError {
82- DoesNotExist ,
81+ enum FileExplorerState {
82+ DoesNotExist = 1 ,
8383 DeferredView
8484}
8585
86+ interface FileExplorerStateError {
87+ state : FileExplorerState
88+ fileExplorerInDeferredState ?: FileExplorerLeaf
89+ }
90+
8691type FileExplorerLeafOrError = ValueOrError < FileExplorerLeaf , FileExplorerStateError >
8792
8893export default class CustomSortPlugin
@@ -188,18 +193,23 @@ export default class CustomSortPlugin
188193
189194 if ( fileExplorer ) {
190195 if ( fileExplorer . isDeferred ) {
191- return fileExplorerOrError . setError ( FileExplorerStateError . DeferredView )
196+ return fileExplorerOrError . setError ( {
197+ state : FileExplorerState . DeferredView ,
198+ fileExplorerInDeferredState : fileExplorer
199+ } )
192200 } else {
193201 return fileExplorerOrError . setValue ( fileExplorer )
194202 }
195203 } else {
196- return fileExplorerOrError . setError ( FileExplorerStateError . DoesNotExist )
204+ return fileExplorerOrError . setError ( {
205+ state : FileExplorerState . DoesNotExist
206+ } )
197207 }
198208 }
199209
200210 checkFileExplorerIsAvailableAndPatchable ( logWarning : boolean = true ) : FileExplorerLeafOrError {
201211 let fileExplorerOrError = this . getFileExplorer ( )
202- if ( fileExplorerOrError . e === FileExplorerStateError . DeferredView ) {
212+ if ( fileExplorerOrError . e && fileExplorerOrError . e . state === FileExplorerState . DeferredView ) {
203213 if ( logWarning ) {
204214 this . logDeferredFileExplorerInfo ( )
205215 }
@@ -687,29 +697,38 @@ export default class CustomSortPlugin
687697 return false
688698 }
689699
690- setWatcherForDelayedFileExplorerView ( ) {
700+ setWatcherForDelayedFileExplorerView ( fileExplorerInDeferredState ?: FileExplorerLeaf ) {
691701 const self = this
692- const fullyFledgedFileExplorerElementSelector = ( ) => document . querySelector ( '[data-type="file-explorer"] .nav-files-container' ) ;
693702
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 , {
703+ let workspaceLeafContentElementParentToObserve : HTMLElement | Element | null | undefined = fileExplorerInDeferredState ?. view ?. containerEl ?. parentElement
704+ if ( ! workspaceLeafContentElementParentToObserve ) {
705+ // Fallback for the case when DOM is not available for deferred file explorer
706+ // (did not happen in practice, but just in case)
707+ workspaceLeafContentElementParentToObserve = document . querySelector ( ".workspace" ) ;
708+ }
709+ if ( workspaceLeafContentElementParentToObserve ) {
710+ const fullyFledgedFileExplorerElementSelector =
711+ ( ) => workspaceLeafContentElementParentToObserve . querySelector ( '[data-type="file-explorer"] .nav-files-container' ) ;
712+
713+ const mutationObserver = new MutationObserver ( ( _ , observerInstance ) => {
714+ const fullyFledgedFileExplorerElement = fullyFledgedFileExplorerElementSelector ( ) ;
715+ if ( fullyFledgedFileExplorerElement ) {
716+ observerInstance . disconnect ( ) ;
717+ self . delayedApplicationOfCustomSorting ( self . FROM_DOM_WATCHER )
718+ }
719+ } ) ;
720+
721+ mutationObserver . observe ( workspaceLeafContentElementParentToObserve , {
704722 childList : true ,
705- subtree : true
723+ subtree : false
706724 } ) ;
707725 }
708726 }
709727
710728 // Entering this method for the first time after initial delay after plugin loaded (via setTimeout()),
711729 // and if first attempt is unsuccessful, then entering this method again from DOM watcher, when
712730 // the File Explorer view gets transformed from delayed view into fully-fledged active view
731+ FROM_DOM_WATCHER : boolean = true
713732 delayedApplicationOfCustomSorting ( fromDOMwatcher ?: boolean ) {
714733 if ( ! this ?. isThePluginStillInstalledAndEnabled ( ) ) {
715734 console . log ( `${ PLUGIN_ID } v${ this . manifest . version } - delayed handler skipped, plugin no longer active.` )
@@ -728,10 +747,15 @@ export default class CustomSortPlugin
728747 // If file explorer is delayed, configure the watcher
729748 // NOTE: Do not configure the watcher if the file explorer is not available
730749 let fileExplorerOrError : FileExplorerLeafOrError = this . checkFileExplorerIsAvailableAndPatchable ( )
731- if ( fileExplorerOrError . e === FileExplorerStateError . DeferredView ) {
750+ if ( fileExplorerOrError . e && fileExplorerOrError . e . state === FileExplorerState . DeferredView ) {
732751 this . logDeferredFileExplorerWatcherSetupInfo ( )
733- this . setWatcherForDelayedFileExplorerView ( )
734- } else { // file explorer is available or does not exist
752+ this . setWatcherForDelayedFileExplorerView ( fileExplorerOrError . e . fileExplorerInDeferredState )
753+ } else if ( fileExplorerOrError . e ) {
754+ // file explorer other error - does not exist
755+ // force the plugin switch state to report error and show the error icon
756+ this . switchPluginStateTo ( true )
757+ }
758+ else { // file explorer is available
735759 this . switchPluginStateTo ( true )
736760 }
737761 } else {
0 commit comments