From fe90df845ac4ff704f0cf6de4513d2c20c5e8ae5 Mon Sep 17 00:00:00 2001 From: SebastianMC Date: Wed, 14 Sep 2022 11:10:47 +0200 Subject: [PATCH] Added more clarity and readability to the code around monkey-patching of TFolder.sort() method --- src/main.ts | 62 +++++++++++++++++++++++--------------------- src/types/types.d.ts | 2 ++ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/main.ts b/src/main.ts index fd5ec9a..14d573b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -42,6 +42,9 @@ const SORTINGSPEC_YAML_KEY: string = 'sorting-spec' const ERROR_NOTICE_TIMEOUT: number = 10000 +// the monkey-around package doesn't export the below type +type MonkeyAroundUninstaller = () => void + export default class CustomSortPlugin extends Plugin { settings: CustomSortPluginSettings statusBarItemEl: HTMLElement @@ -229,39 +232,38 @@ export default class CustomSortPlugin extends Plugin { // @ts-ignore let tmpFolder = new TFolder(Vault, ""); let Folder = fileExplorer.createFolderDom(tmpFolder).constructor; - this.register( - // TODO: Unit tests please!!! The logic below becomes more and more complex, bugs are captured at run-time... - around(Folder.prototype, { - sort(old: any) { - return function (...args: any[]) { - // quick check for plugin status - if (plugin.settings.suspended) { - return old.call(this, ...args); - } + const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(Folder.prototype, { + // TODO: Unit tests, the logic below becomes more and more complex, bugs are captured at run-time... + sort(old: any) { + return function (...args: any[]) { + // quick check for plugin status + if (plugin.settings.suspended) { + return old.call(this, ...args); + } - // if custom sort is not specified, use the UI-selected - const folder: TFolder = this.file - let sortSpec: CustomSortSpec | null | undefined = plugin.sortSpecCache?.sortSpecByPath[folder.path] - if (sortSpec) { - if (sortSpec.defaultOrder === CustomSortOrder.standardObsidian) { - sortSpec = null // A folder is explicitly excluded from custom sorting plugin - } - } else if (plugin.sortSpecCache?.sortSpecByWildcard) { - // when no sorting spec found directly by folder path, check for wildcard-based match - sortSpec = plugin.sortSpecCache?.sortSpecByWildcard.folderMatch(folder.path) - if (sortSpec?.defaultOrder === CustomSortOrder.standardObsidian) { - sortSpec = null // A folder subtree can be also explicitly excluded from custom sorting plugin - } + // if custom sort is not specified, use the UI-selected + const folder: TFolder = this.file + let sortSpec: CustomSortSpec | null | undefined = plugin.sortSpecCache?.sortSpecByPath[folder.path] + if (sortSpec) { + if (sortSpec.defaultOrder === CustomSortOrder.standardObsidian) { + sortSpec = null // A folder is explicitly excluded from custom sorting plugin } - if (sortSpec) { - return folderSort.call(this, sortSpec, ...args); - } else { - return old.call(this, ...args); + } else if (plugin.sortSpecCache?.sortSpecByWildcard) { + // when no sorting spec found directly by folder path, check for wildcard-based match + sortSpec = plugin.sortSpecCache?.sortSpecByWildcard.folderMatch(folder.path) + if (sortSpec?.defaultOrder === CustomSortOrder.standardObsidian) { + sortSpec = null // A folder subtree can be also explicitly excluded from custom sorting plugin } - }; - }, - }) - ); + } + if (sortSpec) { + return folderSort.call(this, sortSpec, ...args); + } else { + return old.call(this, ...args); + } + }; + } + }) + this.register(uninstallerOfFolderSortFunctionWrapper) leaf.detach() } diff --git a/src/types/types.d.ts b/src/types/types.d.ts index 3fe852c..58e02fa 100644 --- a/src/types/types.d.ts +++ b/src/types/types.d.ts @@ -1,5 +1,7 @@ import {TFolder, WorkspaceLeaf} from "obsidian"; +// Needed to support monkey-patching of the folder sort() function + declare module 'obsidian' { export interface ViewRegistry { viewByType: Record unknown>;