Skip to content

Commit

Permalink
#185 - drop backward compatibility
Browse files Browse the repository at this point in the history
- removed legacy code to handle backward compatibility with <0.15.0, <1.2.0, <1.4.11 and <1.6.0
  • Loading branch information
SebastianMC committed Jan 3, 2025
1 parent c300894 commit 51ea92f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 131 deletions.
26 changes: 2 additions & 24 deletions src/custom-sort/custom-sort.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
FrontMatterCache,
MetadataCache,
Plugin,
requireApiVersion,
TAbstractFile,
TFile,
TFolder,
Expand Down Expand Up @@ -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)
}
Expand Down
164 changes: 58 additions & 106 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import {
apiVersion,
App,
FileExplorerView,
Menu,
MenuItem,
MetadataCache,
normalizePath,
Notice,
Platform,
Plugin,
PluginSettingTab,
requireApiVersion,
sanitizeHTMLToDom,
setIcon,
Setting,
TAbstractFile,
TFile,
TFolder,
Vault, WorkspaceLeaf
} from 'obsidian';
import {around} from 'monkey-around';
import {
folderSort_vUpTo_1_6_0,
getSortedFolderItems_vFrom_1_6_0,
getSortedFolderItems,
ObsidianStandardDefaultSortingName,
ProcessingContext,
sortFolderItemsForBookmarking
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

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);
};
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

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) => {
Expand Down Expand Up @@ -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);
}

plugin.resetIconInaccurateStateToEnabled()

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);
}
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: TFolder = this.file
const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder)
const folder = args[0]
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
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ declare module 'obsidian' {
}

export interface FileExplorerView extends View {
createFolderDom(folder: TFolder): FileExplorerFolder;
getSortedFolderItems(sortedFolder: TFolder): any[];

requestSort(): void;
Expand Down

0 comments on commit 51ea92f

Please sign in to comment.