185 - drop backward compatibility

- removed legacy code to handle backward compatibility with <0.15.0, <1.2.0, <1.4.11 and <1.6.0
This commit is contained in:
SebastianMC 2025-01-03 13:22:02 +01:00
parent 779f8738a2
commit d07c61290a
3 changed files with 60 additions and 131 deletions

View File

@ -1,8 +1,6 @@
import { import {
FrontMatterCache, FrontMatterCache,
MetadataCache, MetadataCache,
Plugin,
requireApiVersion,
TAbstractFile, TAbstractFile,
TFile, TFile,
TFolder, TFolder,
@ -719,28 +717,8 @@ export const determineBookmarksOrderIfNeeded = (folderItems: Array<FolderItemFor
}) })
} }
// This function is a replacement for the Obsidian File Explorer function sort(...) up to Obsidian 1.6.0 export const getSortedFolderItems = function (sortedFolder: TFolder, sortingSpec: CustomSortSpec, ctx: ProcessingContext) {
// when a major refactoring of sorting mechanics happened const sortOrder = this.sortOrder // this is bound to FileExplorer Obsidian component
export const folderSort_vUpTo_1_6_0 = function (sortingSpec: CustomSortSpec, ctx: ProcessingContext) {
const fileExplorerView = this.fileExplorer ?? this.view // this.view replaces the former since 1.5.4 insider build
const folderUnderSort = this.file as TFolder
const sortOrder = this.sortOrder
const allFileItemsCollection = fileExplorerView.fileItems
const items = folderSortCore(folderUnderSort, sortOrder, sortingSpec, allFileItemsCollection, ctx)
if (requireApiVersion && requireApiVersion("0.15.0")) {
this.vChildren.setChildren(items);
} else {
this.children = items;
}
}
// This function is a replacement for the Obsidian File Explorer function getSortedFolderItems(...)
// which first appeared in Obsidian 1.6.0 and simplified a bit the plugin integration point
export const getSortedFolderItems_vFrom_1_6_0 = function (sortedFolder: TFolder, sortingSpec: CustomSortSpec, ctx: ProcessingContext) {
const sortOrder = this.sortOrder
const allFileItemsCollection = this.fileItems const allFileItemsCollection = this.fileItems
return folderSortCore(sortedFolder, sortOrder, sortingSpec, allFileItemsCollection, ctx) return folderSortCore(sortedFolder, sortOrder, sortingSpec, allFileItemsCollection, ctx)
} }

View File

@ -1,19 +1,12 @@
import { import {
apiVersion,
App,
FileExplorerView, FileExplorerView,
Menu, Menu,
MenuItem, MenuItem,
MetadataCache, MetadataCache,
normalizePath,
Notice, Notice,
Platform, Platform,
Plugin, Plugin,
PluginSettingTab,
requireApiVersion,
sanitizeHTMLToDom,
setIcon, setIcon,
Setting,
TAbstractFile, TAbstractFile,
TFile, TFile,
TFolder, TFolder,
@ -21,8 +14,7 @@ import {
} from 'obsidian'; } from 'obsidian';
import {around} from 'monkey-around'; import {around} from 'monkey-around';
import { import {
folderSort_vUpTo_1_6_0, getSortedFolderItems,
getSortedFolderItems_vFrom_1_6_0,
ObsidianStandardDefaultSortingName, ObsidianStandardDefaultSortingName,
ProcessingContext, ProcessingContext,
sortFolderItemsForBookmarking sortFolderItemsForBookmarking
@ -178,16 +170,9 @@ export default class CustomSortPlugin
checkFileExplorerIsAvailableAndPatchable(logWarning: boolean = true): FileExplorerView | undefined { checkFileExplorerIsAvailableAndPatchable(logWarning: boolean = true): FileExplorerView | undefined {
let fileExplorerView: FileExplorerView | undefined = this.getFileExplorer() let fileExplorerView: FileExplorerView | undefined = this.getFileExplorer()
if (fileExplorerView && typeof fileExplorerView.requestSort === 'function') { if (fileExplorerView && typeof fileExplorerView.requestSort === 'function') {
// The plugin integration points changed with Obsidian 1.6.0 hence the patchability-check should also be Obsidian version aware
if (requireApiVersion && requireApiVersion("1.6.0")) {
if (typeof fileExplorerView.getSortedFolderItems === 'function') { if (typeof fileExplorerView.getSortedFolderItems === 'function') {
return fileExplorerView return fileExplorerView
} }
} else { // Obsidian versions prior to 1.6.0
if (typeof fileExplorerView.createFolderDom === 'function') {
return fileExplorerView
}
}
} }
// Various scenarios when File Explorer was turned off (e.g. by some other plugin) // Various scenarios when File Explorer was turned off (e.g. by some other plugin)
if (logWarning) { if (logWarning) {
@ -485,7 +470,6 @@ export default class CustomSortPlugin
}) })
) )
if (requireApiVersion('1.4.11')) {
this.registerEvent( this.registerEvent(
// "files-menu" event was exposed in 1.4.11 // "files-menu" event was exposed in 1.4.11
// @ts-ignore // @ts-ignore
@ -522,7 +506,6 @@ export default class CustomSortPlugin
} }
}) })
) )
}
this.registerEvent( this.registerEvent(
this.app.vault.on("rename", (file: TAbstractFile, oldPath: string) => { this.app.vault.on("rename", (file: TAbstractFile, oldPath: string) => {
@ -633,8 +616,6 @@ export default class CustomSortPlugin
// That's why not showing and not logging error message here // That's why not showing and not logging error message here
patchableFileExplorer = patchableFileExplorer ?? this.checkFileExplorerIsAvailableAndPatchable(false) patchableFileExplorer = patchableFileExplorer ?? this.checkFileExplorerIsAvailableAndPatchable(false)
if (patchableFileExplorer) { if (patchableFileExplorer) {
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, { const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.constructor.prototype, {
getSortedFolderItems(old: any) { getSortedFolderItems(old: any) {
return function (...args: any[]) { return function (...args: any[]) {
@ -649,7 +630,7 @@ export default class CustomSortPlugin
const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder) const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder)
if (sortingData.sortSpec) { if (sortingData.sortSpec) {
return getSortedFolderItems_vFrom_1_6_0.call(this, folder, sortingData.sortSpec, plugin.createProcessingContextForSorting(sortingData.sortingAndGroupingStats)) return getSortedFolderItems.call(this, folder, sortingData.sortSpec, plugin.createProcessingContextForSorting(sortingData.sortingAndGroupingStats))
} else { } else {
return old.call(this, ...args); return old.call(this, ...args);
} }
@ -658,35 +639,6 @@ export default class CustomSortPlugin
}) })
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper)) this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
return true 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);
}
plugin.resetIconInaccurateStateToEnabled()
const folder: TFolder = this.file
const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder)
if (sortingData.sortSpec) {
return folderSort_vUpTo_1_6_0.call(this, sortingData.sortSpec, plugin.createProcessingContextForSorting(sortingData.sortingAndGroupingStats));
} else {
return old.call(this, ...args);
}
};
}
})
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
return true
}
} else { } else {
return false return false
} }
@ -730,7 +682,7 @@ export default class CustomSortPlugin
const data: any = await this.loadData() || {} const data: any = await this.loadData() || {}
const isFreshInstall: boolean = Object.keys(data).length === 0 const isFreshInstall: boolean = Object.keys(data).length === 0
this.settings = Object.assign({}, DEFAULT_SETTINGS, data); this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
if (requireApiVersion('1.2.0') && isFreshInstall) { if (isFreshInstall) {
this.settings = Object.assign(this.settings, DEFAULT_SETTING_FOR_1_2_0_UP) this.settings = Object.assign(this.settings, DEFAULT_SETTING_FOR_1_2_0_UP)
} }
} }

View File

@ -50,7 +50,6 @@ declare module 'obsidian' {
} }
export interface FileExplorerView extends View { export interface FileExplorerView extends View {
createFolderDom(folder: TFolder): FileExplorerFolder;
getSortedFolderItems(sortedFolder: TFolder): any[]; getSortedFolderItems(sortedFolder: TFolder): any[];
requestSort(): void; requestSort(): void;