Added more clarity and readability to the code around monkey-patching of TFolder.sort() method
This commit is contained in:
parent
fd8dfa1fcd
commit
fe90df845a
12
src/main.ts
12
src/main.ts
|
@ -42,6 +42,9 @@ const SORTINGSPEC_YAML_KEY: string = 'sorting-spec'
|
||||||
|
|
||||||
const ERROR_NOTICE_TIMEOUT: number = 10000
|
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 {
|
export default class CustomSortPlugin extends Plugin {
|
||||||
settings: CustomSortPluginSettings
|
settings: CustomSortPluginSettings
|
||||||
statusBarItemEl: HTMLElement
|
statusBarItemEl: HTMLElement
|
||||||
|
@ -229,9 +232,8 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
let tmpFolder = new TFolder(Vault, "");
|
let tmpFolder = new TFolder(Vault, "");
|
||||||
let Folder = fileExplorer.createFolderDom(tmpFolder).constructor;
|
let Folder = fileExplorer.createFolderDom(tmpFolder).constructor;
|
||||||
this.register(
|
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(Folder.prototype, {
|
||||||
// TODO: Unit tests please!!! The logic below becomes more and more complex, bugs are captured at run-time...
|
// TODO: Unit tests, the logic below becomes more and more complex, bugs are captured at run-time...
|
||||||
around(Folder.prototype, {
|
|
||||||
sort(old: any) {
|
sort(old: any) {
|
||||||
return function (...args: any[]) {
|
return function (...args: any[]) {
|
||||||
// quick check for plugin status
|
// quick check for plugin status
|
||||||
|
@ -259,9 +261,9 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
return old.call(this, ...args);
|
return old.call(this, ...args);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
);
|
this.register(uninstallerOfFolderSortFunctionWrapper)
|
||||||
leaf.detach()
|
leaf.detach()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import {TFolder, WorkspaceLeaf} from "obsidian";
|
import {TFolder, WorkspaceLeaf} from "obsidian";
|
||||||
|
|
||||||
|
// Needed to support monkey-patching of the folder sort() function
|
||||||
|
|
||||||
declare module 'obsidian' {
|
declare module 'obsidian' {
|
||||||
export interface ViewRegistry {
|
export interface ViewRegistry {
|
||||||
viewByType: Record<string, (leaf: WorkspaceLeaf) => unknown>;
|
viewByType: Record<string, (leaf: WorkspaceLeaf) => unknown>;
|
||||||
|
|
Loading…
Reference in New Issue