Unified the way of accessing the FileExplorerView and added a bit more defensive programming in this area. Also added support for deferred monkey-patching of FileExplorerView in case it doesn't succeeds in the initialization sequence
This commit is contained in:
parent
a2126fa888
commit
13f76cc623
27
src/main.ts
27
src/main.ts
|
@ -55,6 +55,8 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
sortSpecCache?: SortSpecsCollection | null
|
sortSpecCache?: SortSpecsCollection | null
|
||||||
initialAutoOrManualSortingTriggered: boolean
|
initialAutoOrManualSortingTriggered: boolean
|
||||||
|
|
||||||
|
fileExplorerFolderPatched: boolean
|
||||||
|
|
||||||
showNotice(message: string, timeout?: number) {
|
showNotice(message: string, timeout?: number) {
|
||||||
if (this.settings.notificationsEnabled) {
|
if (this.settings.notificationsEnabled) {
|
||||||
new Notice(message, timeout)
|
new Notice(message, timeout)
|
||||||
|
@ -133,9 +135,14 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
this.saveSettings()
|
this.saveSettings()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const fileExplorerView: FileExplorerView = this.getFileExplorer()
|
const fileExplorerView: FileExplorerView | undefined = this.getFileExplorer()
|
||||||
if (fileExplorerView) {
|
if (fileExplorerView) {
|
||||||
|
if (!this.fileExplorerFolderPatched) {
|
||||||
|
this.fileExplorerFolderPatched = this.patchFileExplorerFolder(fileExplorerView);
|
||||||
|
}
|
||||||
|
if (this.fileExplorerFolderPatched) {
|
||||||
fileExplorerView.requestSort();
|
fileExplorerView.requestSort();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (iconToSet === ICON_SORT_ENABLED_ACTIVE) {
|
if (iconToSet === ICON_SORT_ENABLED_ACTIVE) {
|
||||||
iconToSet = ICON_SORT_ENABLED_NOT_APPLIED
|
iconToSet = ICON_SORT_ENABLED_NOT_APPLIED
|
||||||
|
@ -189,7 +196,7 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
this.initialAutoOrManualSortingTriggered = true
|
this.initialAutoOrManualSortingTriggered = true
|
||||||
if (this.sortSpecCache) { // successful read of sorting specifications?
|
if (this.sortSpecCache) { // successful read of sorting specifications?
|
||||||
this.showNotice('Custom sort ON')
|
this.showNotice('Custom sort ON')
|
||||||
const fileExplorerView: FileExplorerView = this.getFileExplorer()
|
const fileExplorerView: FileExplorerView | undefined = this.getFileExplorer()
|
||||||
if (fileExplorerView) {
|
if (fileExplorerView) {
|
||||||
setIcon(this.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE)
|
setIcon(this.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE)
|
||||||
fileExplorerView.requestSort()
|
fileExplorerView.requestSort()
|
||||||
|
@ -228,20 +235,19 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
|
|
||||||
initialize() {
|
initialize() {
|
||||||
this.app.workspace.onLayoutReady(() => {
|
this.app.workspace.onLayoutReady(() => {
|
||||||
this.patchFileExplorerFolder();
|
this.fileExplorerFolderPatched = this.patchFileExplorerFolder();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// For the idea of monkey-patching credits go to https://github.com/nothingislost/obsidian-bartender
|
// For the idea of monkey-patching credits go to https://github.com/nothingislost/obsidian-bartender
|
||||||
patchFileExplorerFolder() {
|
patchFileExplorerFolder(fileExplorer?: FileExplorerView): boolean {
|
||||||
let plugin = this;
|
let plugin = this;
|
||||||
let leaf = this.app.workspace.getLeaf(true);
|
fileExplorer = fileExplorer ?? this.getFileExplorer()
|
||||||
const fileExplorer = this.app.viewRegistry.viewByType["file-explorer"](leaf) as FileExplorerView;
|
if (fileExplorer) {
|
||||||
// @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;
|
||||||
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(Folder.prototype, {
|
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) {
|
sort(old: any) {
|
||||||
return function (...args: any[]) {
|
return function (...args: any[]) {
|
||||||
// quick check for plugin status
|
// quick check for plugin status
|
||||||
|
@ -272,11 +278,14 @@ export default class CustomSortPlugin extends Plugin {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.register(uninstallerOfFolderSortFunctionWrapper)
|
this.register(uninstallerOfFolderSortFunctionWrapper)
|
||||||
leaf.detach()
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Credits go to https://github.com/nothingislost/obsidian-bartender
|
// Credits go to https://github.com/nothingislost/obsidian-bartender
|
||||||
getFileExplorer() {
|
getFileExplorer(): FileExplorerView | undefined {
|
||||||
let fileExplorer: FileExplorerView | undefined = this.app.workspace.getLeavesOfType("file-explorer")?.first()
|
let fileExplorer: FileExplorerView | undefined = this.app.workspace.getLeavesOfType("file-explorer")?.first()
|
||||||
?.view as unknown as FileExplorerView;
|
?.view as unknown as FileExplorerView;
|
||||||
return fileExplorer;
|
return fileExplorer;
|
||||||
|
|
Loading…
Reference in New Issue