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:
parent
779f8738a2
commit
d07c61290a
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
164
src/main.ts
164
src/main.ts
|
@ -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,15 +170,8 @@ 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 (typeof fileExplorerView.getSortedFolderItems === 'function') {
|
||||||
if (requireApiVersion && requireApiVersion("1.6.0")) {
|
return fileExplorerView
|
||||||
if (typeof fileExplorerView.getSortedFolderItems === 'function') {
|
|
||||||
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)
|
||||||
|
@ -485,44 +470,42 @@ 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
|
this.app.workspace.on("files-menu", (menu: Menu, files: TAbstractFile[], source: string, leaf?: WorkspaceLeaf) => {
|
||||||
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 (!this.settings.customSortContextSubmenu) return; // Don't show the context menus at all
|
|
||||||
|
|
||||||
const customSortMenuItem = (item?: MenuItem) => {
|
const customSortMenuItem = (item?: MenuItem) => {
|
||||||
// if parameter is empty it means mobile invocation, where submenus are not supported.
|
// if parameter is empty it means mobile invocation, where submenus are not supported.
|
||||||
// In that case flatten the menu.
|
// In that case flatten the menu.
|
||||||
let submenu: Menu|undefined
|
let submenu: Menu|undefined
|
||||||
if (item) {
|
if (item) {
|
||||||
item.setTitle('Custom sort:');
|
item.setTitle('Custom sort:');
|
||||||
item.setIcon('hashtag');
|
item.setIcon('hashtag');
|
||||||
submenu = item.setSubmenu()
|
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)
|
|
||||||
}
|
}
|
||||||
})
|
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.registerEvent(
|
||||||
this.app.vault.on("rename", (file: TAbstractFile, oldPath: string) => {
|
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
|
// 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")) {
|
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.constructor.prototype, {
|
||||||
// Starting from Obsidian 1.6.0 the sorting mechanics has been significantly refactored internally in Obsidian
|
getSortedFolderItems(old: any) {
|
||||||
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.constructor.prototype, {
|
return function (...args: any[]) {
|
||||||
getSortedFolderItems(old: any) {
|
// quick check for plugin status
|
||||||
return function (...args: any[]) {
|
if (plugin.settings.suspended) {
|
||||||
// quick check for plugin status
|
return old.call(this, ...args);
|
||||||
if (plugin.settings.suspended) {
|
}
|
||||||
return old.call(this, ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
plugin.resetIconInaccurateStateToEnabled()
|
plugin.resetIconInaccurateStateToEnabled()
|
||||||
|
|
||||||
const folder = args[0]
|
const folder = args[0]
|
||||||
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue