@@ -101,6 +101,8 @@ export default class CustomSortPlugin
101101 sortSpecCache ?: SortSpecsCollection | null
102102 customSortAppliedAtLeastOnce : boolean = false
103103
104+ uninstallerOfFileExplorerPatch : MonkeyAroundUninstaller | undefined = undefined
105+
104106 showNotice ( message : string , timeout ?: number ) {
105107 if ( this . settings . notificationsEnabled || ( Platform . isMobile && this . settings . mobileNotificationsEnabled ) ) {
106108 new Notice ( message , timeout )
@@ -231,21 +233,12 @@ export default class CustomSortPlugin
231233 // For the idea of monkey-patching credits go to https://github.com/nothingislost/obsidian-bartender
232234 patchFileExplorer ( patchableFileExplorer : FileExplorerLeaf ) : FileExplorerLeaf | undefined {
233235 let plugin = this ;
234- const requestStandardObsidianSortAfter = ( patchUninstaller : MonkeyAroundUninstaller | undefined ) => {
235- return ( ) => {
236- if ( patchUninstaller ) patchUninstaller ( )
237-
238- const fileExplorerOrError = this . checkFileExplorerIsAvailableAndPatchable ( )
239- if ( fileExplorerOrError . v && fileExplorerOrError . v . view ) {
240- fileExplorerOrError . v . view . requestSort ?.( )
241- }
242- }
243- }
244236
245237 // patching file explorer might fail here because of various non-error reasons.
246238 // That's why not showing and not logging error message here
247239 if ( patchableFileExplorer ) {
248- const uninstallerOfFolderSortFunctionWrapper : MonkeyAroundUninstaller = around ( patchableFileExplorer . view . constructor . prototype , {
240+ this . uninstallFileExplorerPatchIfInstalled ( )
241+ this . uninstallerOfFileExplorerPatch = around ( patchableFileExplorer . view . constructor . prototype , {
249242 getSortedFolderItems ( old : any ) {
250243 return function ( ...args : any [ ] ) {
251244 // quick check for plugin status
@@ -272,7 +265,6 @@ export default class CustomSortPlugin
272265 } ;
273266 }
274267 } )
275- this . register ( requestStandardObsidianSortAfter ( uninstallerOfFolderSortFunctionWrapper ) )
276268 return patchableFileExplorer
277269 } else {
278270 return undefined
@@ -379,6 +371,8 @@ export default class CustomSortPlugin
379371
380372 this . registerCommands ( )
381373
374+ this . registerPluginUnloadHandler ( )
375+
382376 this . initialize ( ) ;
383377 }
384378
@@ -576,6 +570,31 @@ export default class CustomSortPlugin
576570 } )
577571 }
578572
573+ uninstallFileExplorerPatchIfInstalled ( ) {
574+ if ( this . uninstallerOfFileExplorerPatch ) {
575+ try {
576+ this . uninstallerOfFileExplorerPatch ( )
577+ } catch {
578+
579+ }
580+ this . uninstallerOfFileExplorerPatch = undefined
581+ }
582+ }
583+
584+ registerPluginUnloadHandler ( ) {
585+ let plugin = this ;
586+
587+ this . register ( ( ) => {
588+ plugin . uninstallFileExplorerPatchIfInstalled ( )
589+
590+ // Request standard File Explorer sorting to remove any custom sorting cached by File Explorer
591+ const fileExplorerOrError = plugin . checkFileExplorerIsAvailableAndPatchable ( )
592+ if ( fileExplorerOrError . v && fileExplorerOrError . v . view ) {
593+ fileExplorerOrError . v . view . requestSort ?.( )
594+ }
595+ } )
596+ }
597+
579598 registerCommands ( ) {
580599 const plugin : CustomSortPlugin = this
581600 this . addCommand ( {
0 commit comments