Skip to content

Commit 3cd4b66

Browse files
committed
Merge remote-tracking branch 'origin/master' into 195-mdata-matchers
2 parents 41cb981 + 743e1b7 commit 3cd4b66

4 files changed

Lines changed: 35 additions & 15 deletions

File tree

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "custom-sort",
33
"name": "Custom File Explorer sorting",
4-
"version": "3.1.3",
4+
"version": "3.1.4",
55
"minAppVersion": "1.7.2",
66
"description": "Allows for manual and automatic, config-driven reordering and sorting of files and folders in File Explorer",
77
"author": "SebastianMC",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-custom-sort",
3-
"version": "3.1.3",
3+
"version": "3.1.4",
44
"description": "Custom Sort plugin for Obsidian (https://obsidian.md)",
55
"main": "main.js",
66
"scripts": {

src/main.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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({

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@
5454
"3.1.0": "1.7.2",
5555
"3.1.1": "1.7.2",
5656
"3.1.2": "1.7.2",
57-
"3.1.3": "1.7.2"
57+
"3.1.3": "1.7.2",
58+
"3.1.4": "1.7.2"
5859
}

0 commit comments

Comments
 (0)