#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 c3008946cc
commit 51ea92f0e8
3 changed files with 60 additions and 131 deletions

View File

@ -1,8 +1,6 @@
import {
FrontMatterCache,
MetadataCache,
Plugin,
requireApiVersion,
TAbstractFile,
TFile,
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
// when a major refactoring of sorting mechanics happened
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
export const getSortedFolderItems = function (sortedFolder: TFolder, sortingSpec: CustomSortSpec, ctx: ProcessingContext) {
const sortOrder = this.sortOrder // this is bound to FileExplorer Obsidian component
const allFileItemsCollection = this.fileItems
return folderSortCore(sortedFolder, sortOrder, sortingSpec, allFileItemsCollection, ctx)
}

View File

@ -1,19 +1,12 @@
import {
apiVersion,
App,
FileExplorerView,
Menu,
MenuItem,
MetadataCache,
normalizePath,
Notice,
Platform,
Plugin,
PluginSettingTab,
requireApiVersion,
sanitizeHTMLToDom,
setIcon,
Setting,
TAbstractFile,
TFile,
TFolder,
@ -21,8 +14,7 @@ import {
} from 'obsidian';
import {around} from 'monkey-around';
import {
folderSort_vUpTo_1_6_0,
getSortedFolderItems_vFrom_1_6_0,
getSortedFolderItems,
ObsidianStandardDefaultSortingName,
ProcessingContext,
sortFolderItemsForBookmarking
@ -178,15 +170,8 @@ export default class CustomSortPlugin
checkFileExplorerIsAvailableAndPatchable(logWarning: boolean = true): FileExplorerView | undefined {
let fileExplorerView: FileExplorerView | undefined = this.getFileExplorer()
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') {
return fileExplorerView
}
} else { // Obsidian versions prior to 1.6.0
if (typeof fileExplorerView.createFolderDom === 'function') {
return fileExplorerView
}
if (typeof fileExplorerView.getSortedFolderItems === 'function') {
return fileExplorerView
}
}
// Various scenarios when File Explorer was turned off (e.g. by some other plugin)
@ -485,44 +470,42 @@ export default class CustomSortPlugin
})
)
if (requireApiVersion('1.4.11')) {
this.registerEvent(
// "files-menu" event was exposed in 1.4.11
// @ts-ignore
this.app.workspace.on("files-menu", (menu: Menu, files: TAbstractFile[], source: string, leaf?: WorkspaceLeaf) => {
if (!this.settings.customSortContextSubmenu) return; // Don't show the context menus at all
this.registerEvent(
// "files-menu" event was exposed in 1.4.11
// @ts-ignore
this.app.workspace.on("files-menu", (menu: Menu, files: TAbstractFile[], source: string, leaf?: WorkspaceLeaf) => {
if (!this.settings.customSortContextSubmenu) return; // Don't show the context menus at all
const customSortMenuItem = (item?: MenuItem) => {
// if parameter is empty it means mobile invocation, where submenus are not supported.
// In that case flatten the menu.
let submenu: Menu|undefined
if (item) {
item.setTitle('Custom sort:');
item.setIcon('hashtag');
submenu = item.setSubmenu()
}
if (!submenu) menu.addSeparator();
(submenu ?? menu).addItem(applyCustomSortMenuItem)
if (submenu) submenu.addSeparator();
if (this.settings.bookmarksContextMenus) {
const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
if (bookmarksPlugin) {
(submenu ?? menu).addItem(getBookmarkSelectedMenuItemForFiles(files));
(submenu ?? menu).addItem(getUnbookmarkSelectedMenuItemForFiles(files));
}
}
(submenu ?? menu).addItem(suspendCustomSortMenuItem);
};
if (m) {
customSortMenuItem(undefined)
} else {
menu.addItem(customSortMenuItem)
const customSortMenuItem = (item?: MenuItem) => {
// if parameter is empty it means mobile invocation, where submenus are not supported.
// In that case flatten the menu.
let submenu: Menu|undefined
if (item) {
item.setTitle('Custom sort:');
item.setIcon('hashtag');
submenu = item.setSubmenu()
}
})
)
}
if (!submenu) menu.addSeparator();
(submenu ?? menu).addItem(applyCustomSortMenuItem)
if (submenu) submenu.addSeparator();
if (this.settings.bookmarksContextMenus) {
const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
if (bookmarksPlugin) {
(submenu ?? menu).addItem(getBookmarkSelectedMenuItemForFiles(files));
(submenu ?? menu).addItem(getUnbookmarkSelectedMenuItemForFiles(files));
}
}
(submenu ?? menu).addItem(suspendCustomSortMenuItem);
};
if (m) {
customSortMenuItem(undefined)
} else {
menu.addItem(customSortMenuItem)
}
})
)
this.registerEvent(
this.app.vault.on("rename", (file: TAbstractFile, oldPath: string) => {
@ -633,60 +616,29 @@ export default class CustomSortPlugin
// That's why not showing and not logging error message here
patchableFileExplorer = patchableFileExplorer ?? this.checkFileExplorerIsAvailableAndPatchable(false)
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, {
getSortedFolderItems(old: any) {
return function (...args: any[]) {
// quick check for plugin status
if (plugin.settings.suspended) {
return old.call(this, ...args);
}
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);
}
plugin.resetIconInaccurateStateToEnabled()
plugin.resetIconInaccurateStateToEnabled()
const folder = args[0]
const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder)
const folder = args[0]
const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder)
if (sortingData.sortSpec) {
return getSortedFolderItems_vFrom_1_6_0.call(this, folder, sortingData.sortSpec, plugin.createProcessingContextForSorting(sortingData.sortingAndGroupingStats))
} else {
return old.call(this, ...args);
}
};
}
})
this.register(requestStandardObsidianSortAfter(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);
}
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
}
if (sortingData.sortSpec) {
return getSortedFolderItems.call(this, folder, sortingData.sortSpec, plugin.createProcessingContextForSorting(sortingData.sortingAndGroupingStats))
} else {
return old.call(this, ...args);
}
};
}
})
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
return true
} else {
return false
}
@ -730,7 +682,7 @@ export default class CustomSortPlugin
const data: any = await this.loadData() || {}
const isFreshInstall: boolean = Object.keys(data).length === 0
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)
}
}

View File

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