diff --git a/src/custom-sort/custom-sort.ts b/src/custom-sort/custom-sort.ts index da95f4aa1..126126174 100644 --- a/src/custom-sort/custom-sort.ts +++ b/src/custom-sort/custom-sort.ts @@ -702,21 +702,45 @@ export const determineBookmarksOrderIfNeeded = (folderItems: Array Object.assign({} as CustomSortGroup, group)) - const parentFolderName: string|undefined = this.file.name + const parentFolderName: string|undefined = sortedFolder.name expandMacros(sortingSpec, parentFolderName) const folderItems: Array = (sortingSpec.itemsToHide ? - this.file.children.filter((entry: TFile | TFolder) => { + sortedFolder.children.filter((entry: TFile | TFolder) => { return !sortingSpec.itemsToHide!.has(entry.name) }) : - this.file.children) + sortedFolder.children) .map((entry: TFile | TFolder) => { const itemForSorting: FolderItemForSorting = determineSortingGroup(entry, sortingSpec, ctx) return itemForSorting @@ -729,23 +753,14 @@ export const folderSort = function (sortingSpec: CustomSortSpec, ctx: Processing determineBookmarksOrderIfNeeded(folderItems, sortingSpec, ctx.bookmarksPluginInstance) } - const comparator: SorterFn = getComparator(sortingSpec, fileExplorerView.sortOrder) + const comparator: SorterFn = getComparator(sortingSpec, sortOrder) folderItems.sort(comparator) const items = folderItems - .map((item: FolderItemForSorting) => fileExplorerView.fileItems[item.path]) - - if (requireApiVersion && requireApiVersion("0.16.0")) { - const scrollTop = fileExplorerView.navFileContainerEl.scrollTop - fileExplorerView.tree.infinityScroll.rootEl.vChildren.setChildren([items]) - fileExplorerView.navFileContainerEl.scrollTop = scrollTop - fileExplorerView.tree.infinityScroll.compute() - } else if (requireApiVersion && requireApiVersion("0.15.0")) { - this.vChildren.setChildren(items); - } else { - this.children = items; - } + .map((item: FolderItemForSorting) => allFileItemsCollection[item.path]) + + return items }; // Returns a sorted copy of the input array, intentionally to keep it intact diff --git a/src/main.ts b/src/main.ts index 450c29c44..4239471d6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -21,7 +21,8 @@ import { } from 'obsidian'; import {around} from 'monkey-around'; import { - folderSort, + folderSort_vUpTo_1_6_0, + getSortedFolderItems_vFrom_1_6_0, ObsidianStandardDefaultSortingName, ProcessingContext, sortFolderItemsForBookmarking @@ -593,51 +594,86 @@ export default class CustomSortPlugin extends Plugin { // That's why not showing and not logging error message here patchableFileExplorer = patchableFileExplorer ?? this.checkFileExplorerIsAvailableAndPatchable(false) if (patchableFileExplorer) { - // @ts-ignore - let tmpFolder = new TFolder(Vault, ""); - let Folder = patchableFileExplorer.createFolderDom(tmpFolder).constructor; - const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(Folder.prototype, { - sort(old: any) { - return function (...args: any[]) { - // quick check for plugin status - if (plugin.settings.suspended) { - return old.call(this, ...args); - } - - if (plugin.ribbonIconStateInaccurate && plugin.ribbonIconEl) { - plugin.ribbonIconStateInaccurate = false - setIcon(plugin.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE) - } + if (requireApiVersion && requireApiVersion("1.6.0")) { + // Starting from Obsidian 1.6.0 the sorting mechanics has been significantly refactored internally in Obsidian + const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.constructor.prototype, { + getSortedFolderItems(old: any) { + return function (...args: any[]) { + // quick check for plugin status + if (plugin.settings.suspended) { + return old.call(this, ...args); + } + + if (plugin.ribbonIconStateInaccurate && plugin.ribbonIconEl) { + plugin.ribbonIconStateInaccurate = false + setIcon(plugin.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE) + } + + const folder = args[0] + let sortSpec: CustomSortSpec | null | undefined = plugin.determineSortSpecForFolder(folder.path, folder.name) + + // Performance optimization + // Primary intention: when the implicit bookmarks integration is enabled, remain on std Obsidian, if no need to involve bookmarks + let has: HasSortingOrGrouping = collectSortingAndGroupingTypes(sortSpec) + if (hasOnlyByBookmarkOrStandardObsidian(has)) { + const bookmarksPlugin: BookmarksPluginInterface|undefined = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference, false, true) + if ( !bookmarksPlugin?.bookmarksIncludeItemsInFolder(folder.path)) { + sortSpec = null + } + } + + if (sortSpec) { + return getSortedFolderItems_vFrom_1_6_0.call(this, folder, sortSpec, plugin.createProcessingContextForSorting(has)) + } else { + return old.call(this, ...args); + } + }; + } + }) + this.register(uninstallerOfFolderSortFunctionWrapper) + return true + } else { + // Up to Obsidian 1.6.0 + // @ts-ignore + let tmpFolder = new TFolder(Vault, ""); + let Folder = patchableFileExplorer.createFolderDom(tmpFolder).constructor; + const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = 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 folder: TFolder = this.file - let sortSpec: CustomSortSpec | null | undefined = plugin.determineSortSpecForFolder(folder.path, folder.name) + if (plugin.ribbonIconStateInaccurate && plugin.ribbonIconEl) { + plugin.ribbonIconStateInaccurate = false + setIcon(plugin.ribbonIconEl, ICON_SORT_ENABLED_ACTIVE) + } - // Performance optimization - // Primary intention: when the implicit bookmarks integration is enabled, remain on std Obsidian, if no need to involve bookmarks - let has: HasSortingOrGrouping = collectSortingAndGroupingTypes(sortSpec) - if (hasOnlyByBookmarkOrStandardObsidian(has)) { - const bookmarksPlugin: BookmarksPluginInterface|undefined = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference, false, true) - if ( !bookmarksPlugin?.bookmarksIncludeItemsInFolder(folder.path)) { - sortSpec = null + const folder: TFolder = this.file + let sortSpec: CustomSortSpec | null | undefined = plugin.determineSortSpecForFolder(folder.path, folder.name) + + // Performance optimization + // Primary intention: when the implicit bookmarks integration is enabled, remain on std Obsidian, if no need to involve bookmarks + let has: HasSortingOrGrouping = collectSortingAndGroupingTypes(sortSpec) + if (hasOnlyByBookmarkOrStandardObsidian(has)) { + const bookmarksPlugin: BookmarksPluginInterface | undefined = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference, false, true) + if (!bookmarksPlugin?.bookmarksIncludeItemsInFolder(folder.path)) { + sortSpec = null + } } - } - if (sortSpec) { - return folderSort.call(this, sortSpec, plugin.createProcessingContextForSorting(has)); - } else { - return old.call(this, ...args); - } - }; - },*/ - getSortedFolderItems(old: any) { - return function (...args: any[]) { - console.log(`Cuda cuda ${args?.length}`) - return old.call(this, ...args); - }; - } - }) - this.register(uninstallerOfFolderSortFunctionWrapper) - return true + if (sortSpec) { + return folderSort_vUpTo_1_6_0.call(this, sortSpec, plugin.createProcessingContextForSorting(has)); + } else { + return old.call(this, ...args); + } + }; + } + }) + this.register(uninstallerOfFolderSortFunctionWrapper) + return true + } } else { return false }