Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export default class CustomSortPlugin
sortSpecCache?: SortSpecsCollection | null
customSortAppliedAtLeastOnce: boolean = false

uninstallerOfFileExplorerPatch: MonkeyAroundUninstaller|undefined = undefined

showNotice(message: string, timeout?: number) {
if (this.settings.notificationsEnabled || (Platform.isMobile && this.settings.mobileNotificationsEnabled)) {
new Notice(message, timeout)
Expand Down Expand Up @@ -231,21 +233,12 @@ export default class CustomSortPlugin
// For the idea of monkey-patching credits go to https://github.com/nothingislost/obsidian-bartender
patchFileExplorer(patchableFileExplorer: FileExplorerLeaf): FileExplorerLeaf|undefined {
let plugin = this;
const requestStandardObsidianSortAfter = (patchUninstaller: MonkeyAroundUninstaller|undefined) => {
return () => {
if (patchUninstaller) patchUninstaller()

const fileExplorerOrError= this.checkFileExplorerIsAvailableAndPatchable()
if (fileExplorerOrError.v && fileExplorerOrError.v.view) {
fileExplorerOrError.v.view.requestSort?.()
}
}
}

// patching file explorer might fail here because of various non-error reasons.
// That's why not showing and not logging error message here
if (patchableFileExplorer) {
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.view.constructor.prototype, {
this.uninstallFileExplorerPatchIfInstalled()
this.uninstallerOfFileExplorerPatch = around(patchableFileExplorer.view.constructor.prototype, {
getSortedFolderItems(old: any) {
return function (...args: any[]) {
// quick check for plugin status
Expand All @@ -272,7 +265,6 @@ export default class CustomSortPlugin
};
}
})
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
return patchableFileExplorer
} else {
return undefined
Expand Down Expand Up @@ -379,6 +371,8 @@ export default class CustomSortPlugin

this.registerCommands()

this.registerPluginUnloadHandler()

this.initialize();
}

Expand Down Expand Up @@ -576,6 +570,31 @@ export default class CustomSortPlugin
})
}

uninstallFileExplorerPatchIfInstalled() {
if (this.uninstallerOfFileExplorerPatch) {
try {
this.uninstallerOfFileExplorerPatch()
} catch {

}
this.uninstallerOfFileExplorerPatch = undefined
}
}

registerPluginUnloadHandler() {
let plugin = this;

this.register(() => {
plugin.uninstallFileExplorerPatchIfInstalled()

// Request standard File Explorer sorting to remove any custom sorting cached by File Explorer
const fileExplorerOrError= plugin.checkFileExplorerIsAvailableAndPatchable()
if (fileExplorerOrError.v && fileExplorerOrError.v.view) {
fileExplorerOrError.v.view.requestSort?.()
}
})
}

registerCommands() {
const plugin: CustomSortPlugin = this
this.addCommand({
Expand Down
Loading