Skip to content

Commit 51ea92f

Browse files
committed
#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
1 parent c300894 commit 51ea92f

File tree

3 files changed

+60
-131
lines changed

3 files changed

+60
-131
lines changed

src/custom-sort/custom-sort.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
22
FrontMatterCache,
33
MetadataCache,
4-
Plugin,
5-
requireApiVersion,
64
TAbstractFile,
75
TFile,
86
TFolder,
@@ -719,28 +717,8 @@ export const determineBookmarksOrderIfNeeded = (folderItems: Array<FolderItemFor
719717
})
720718
}
721719

722-
// This function is a replacement for the Obsidian File Explorer function sort(...) up to Obsidian 1.6.0
723-
// when a major refactoring of sorting mechanics happened
724-
export const folderSort_vUpTo_1_6_0 = function (sortingSpec: CustomSortSpec, ctx: ProcessingContext) {
725-
726-
const fileExplorerView = this.fileExplorer ?? this.view // this.view replaces the former since 1.5.4 insider build
727-
const folderUnderSort = this.file as TFolder
728-
const sortOrder = this.sortOrder
729-
const allFileItemsCollection = fileExplorerView.fileItems
730-
731-
const items = folderSortCore(folderUnderSort, sortOrder, sortingSpec, allFileItemsCollection, ctx)
732-
733-
if (requireApiVersion && requireApiVersion("0.15.0")) {
734-
this.vChildren.setChildren(items);
735-
} else {
736-
this.children = items;
737-
}
738-
}
739-
740-
// This function is a replacement for the Obsidian File Explorer function getSortedFolderItems(...)
741-
// which first appeared in Obsidian 1.6.0 and simplified a bit the plugin integration point
742-
export const getSortedFolderItems_vFrom_1_6_0 = function (sortedFolder: TFolder, sortingSpec: CustomSortSpec, ctx: ProcessingContext) {
743-
const sortOrder = this.sortOrder
720+
export const getSortedFolderItems = function (sortedFolder: TFolder, sortingSpec: CustomSortSpec, ctx: ProcessingContext) {
721+
const sortOrder = this.sortOrder // this is bound to FileExplorer Obsidian component
744722
const allFileItemsCollection = this.fileItems
745723
return folderSortCore(sortedFolder, sortOrder, sortingSpec, allFileItemsCollection, ctx)
746724
}

src/main.ts

Lines changed: 58 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
import {
2-
apiVersion,
3-
App,
42
FileExplorerView,
53
Menu,
64
MenuItem,
75
MetadataCache,
8-
normalizePath,
96
Notice,
107
Platform,
118
Plugin,
12-
PluginSettingTab,
13-
requireApiVersion,
14-
sanitizeHTMLToDom,
159
setIcon,
16-
Setting,
1710
TAbstractFile,
1811
TFile,
1912
TFolder,
2013
Vault, WorkspaceLeaf
2114
} from 'obsidian';
2215
import {around} from 'monkey-around';
2316
import {
24-
folderSort_vUpTo_1_6_0,
25-
getSortedFolderItems_vFrom_1_6_0,
17+
getSortedFolderItems,
2618
ObsidianStandardDefaultSortingName,
2719
ProcessingContext,
2820
sortFolderItemsForBookmarking
@@ -178,15 +170,8 @@ export default class CustomSortPlugin
178170
checkFileExplorerIsAvailableAndPatchable(logWarning: boolean = true): FileExplorerView | undefined {
179171
let fileExplorerView: FileExplorerView | undefined = this.getFileExplorer()
180172
if (fileExplorerView && typeof fileExplorerView.requestSort === 'function') {
181-
// The plugin integration points changed with Obsidian 1.6.0 hence the patchability-check should also be Obsidian version aware
182-
if (requireApiVersion && requireApiVersion("1.6.0")) {
183-
if (typeof fileExplorerView.getSortedFolderItems === 'function') {
184-
return fileExplorerView
185-
}
186-
} else { // Obsidian versions prior to 1.6.0
187-
if (typeof fileExplorerView.createFolderDom === 'function') {
188-
return fileExplorerView
189-
}
173+
if (typeof fileExplorerView.getSortedFolderItems === 'function') {
174+
return fileExplorerView
190175
}
191176
}
192177
// Various scenarios when File Explorer was turned off (e.g. by some other plugin)
@@ -485,44 +470,42 @@ export default class CustomSortPlugin
485470
})
486471
)
487472

488-
if (requireApiVersion('1.4.11')) {
489-
this.registerEvent(
490-
// "files-menu" event was exposed in 1.4.11
491-
// @ts-ignore
492-
this.app.workspace.on("files-menu", (menu: Menu, files: TAbstractFile[], source: string, leaf?: WorkspaceLeaf) => {
493-
if (!this.settings.customSortContextSubmenu) return; // Don't show the context menus at all
494-
495-
const customSortMenuItem = (item?: MenuItem) => {
496-
// if parameter is empty it means mobile invocation, where submenus are not supported.
497-
// In that case flatten the menu.
498-
let submenu: Menu|undefined
499-
if (item) {
500-
item.setTitle('Custom sort:');
501-
item.setIcon('hashtag');
502-
submenu = item.setSubmenu()
503-
}
504-
if (!submenu) menu.addSeparator();
505-
(submenu ?? menu).addItem(applyCustomSortMenuItem)
506-
if (submenu) submenu.addSeparator();
507-
508-
if (this.settings.bookmarksContextMenus) {
509-
const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
510-
if (bookmarksPlugin) {
511-
(submenu ?? menu).addItem(getBookmarkSelectedMenuItemForFiles(files));
512-
(submenu ?? menu).addItem(getUnbookmarkSelectedMenuItemForFiles(files));
513-
}
514-
}
515-
(submenu ?? menu).addItem(suspendCustomSortMenuItem);
516-
};
473+
this.registerEvent(
474+
// "files-menu" event was exposed in 1.4.11
475+
// @ts-ignore
476+
this.app.workspace.on("files-menu", (menu: Menu, files: TAbstractFile[], source: string, leaf?: WorkspaceLeaf) => {
477+
if (!this.settings.customSortContextSubmenu) return; // Don't show the context menus at all
517478

518-
if (m) {
519-
customSortMenuItem(undefined)
520-
} else {
521-
menu.addItem(customSortMenuItem)
479+
const customSortMenuItem = (item?: MenuItem) => {
480+
// if parameter is empty it means mobile invocation, where submenus are not supported.
481+
// In that case flatten the menu.
482+
let submenu: Menu|undefined
483+
if (item) {
484+
item.setTitle('Custom sort:');
485+
item.setIcon('hashtag');
486+
submenu = item.setSubmenu()
522487
}
523-
})
524-
)
525-
}
488+
if (!submenu) menu.addSeparator();
489+
(submenu ?? menu).addItem(applyCustomSortMenuItem)
490+
if (submenu) submenu.addSeparator();
491+
492+
if (this.settings.bookmarksContextMenus) {
493+
const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
494+
if (bookmarksPlugin) {
495+
(submenu ?? menu).addItem(getBookmarkSelectedMenuItemForFiles(files));
496+
(submenu ?? menu).addItem(getUnbookmarkSelectedMenuItemForFiles(files));
497+
}
498+
}
499+
(submenu ?? menu).addItem(suspendCustomSortMenuItem);
500+
};
501+
502+
if (m) {
503+
customSortMenuItem(undefined)
504+
} else {
505+
menu.addItem(customSortMenuItem)
506+
}
507+
})
508+
)
526509

527510
this.registerEvent(
528511
this.app.vault.on("rename", (file: TAbstractFile, oldPath: string) => {
@@ -633,60 +616,29 @@ export default class CustomSortPlugin
633616
// That's why not showing and not logging error message here
634617
patchableFileExplorer = patchableFileExplorer ?? this.checkFileExplorerIsAvailableAndPatchable(false)
635618
if (patchableFileExplorer) {
636-
if (requireApiVersion && requireApiVersion("1.6.0")) {
637-
// Starting from Obsidian 1.6.0 the sorting mechanics has been significantly refactored internally in Obsidian
638-
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.constructor.prototype, {
639-
getSortedFolderItems(old: any) {
640-
return function (...args: any[]) {
641-
// quick check for plugin status
642-
if (plugin.settings.suspended) {
643-
return old.call(this, ...args);
644-
}
645-
646-
plugin.resetIconInaccurateStateToEnabled()
647-
648-
const folder = args[0]
649-
const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder)
650-
651-
if (sortingData.sortSpec) {
652-
return getSortedFolderItems_vFrom_1_6_0.call(this, folder, sortingData.sortSpec, plugin.createProcessingContextForSorting(sortingData.sortingAndGroupingStats))
653-
} else {
654-
return old.call(this, ...args);
655-
}
656-
};
657-
}
658-
})
659-
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
660-
return true
661-
} else {
662-
// Up to Obsidian 1.6.0
663-
// @ts-ignore
664-
let tmpFolder = new TFolder(Vault, "");
665-
let Folder = patchableFileExplorer.createFolderDom(tmpFolder).constructor;
666-
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(Folder.prototype, {
667-
sort(old: any) {
668-
return function (...args: any[]) {
669-
// quick check for plugin status
670-
if (plugin.settings.suspended) {
671-
return old.call(this, ...args);
672-
}
619+
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.constructor.prototype, {
620+
getSortedFolderItems(old: any) {
621+
return function (...args: any[]) {
622+
// quick check for plugin status
623+
if (plugin.settings.suspended) {
624+
return old.call(this, ...args);
625+
}
673626

674-
plugin.resetIconInaccurateStateToEnabled()
627+
plugin.resetIconInaccurateStateToEnabled()
675628

676-
const folder: TFolder = this.file
677-
const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder)
629+
const folder = args[0]
630+
const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder)
678631

679-
if (sortingData.sortSpec) {
680-
return folderSort_vUpTo_1_6_0.call(this, sortingData.sortSpec, plugin.createProcessingContextForSorting(sortingData.sortingAndGroupingStats));
681-
} else {
682-
return old.call(this, ...args);
683-
}
684-
};
685-
}
686-
})
687-
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
688-
return true
689-
}
632+
if (sortingData.sortSpec) {
633+
return getSortedFolderItems.call(this, folder, sortingData.sortSpec, plugin.createProcessingContextForSorting(sortingData.sortingAndGroupingStats))
634+
} else {
635+
return old.call(this, ...args);
636+
}
637+
};
638+
}
639+
})
640+
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
641+
return true
690642
} else {
691643
return false
692644
}
@@ -730,7 +682,7 @@ export default class CustomSortPlugin
730682
const data: any = await this.loadData() || {}
731683
const isFreshInstall: boolean = Object.keys(data).length === 0
732684
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
733-
if (requireApiVersion('1.2.0') && isFreshInstall) {
685+
if (isFreshInstall) {
734686
this.settings = Object.assign(this.settings, DEFAULT_SETTING_FOR_1_2_0_UP)
735687
}
736688
}

src/types/types.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ declare module 'obsidian' {
5050
}
5151

5252
export interface FileExplorerView extends View {
53-
createFolderDom(folder: TFolder): FileExplorerFolder;
5453
getSortedFolderItems(sortedFolder: TFolder): any[];
5554

5655
requestSort(): void;

0 commit comments

Comments
 (0)