From 107c32e46103f4085209a88fc0f366d562eb1c60 Mon Sep 17 00:00:00 2001 From: SebastianMC <23032356+SebastianMC@users.noreply.github.com> Date: Mon, 13 May 2024 14:36:13 +0200 Subject: [PATCH] The global `app` object becomes deprecated in Obsidian 1.6.0 - switched code to use the DI approach - the handle to `app` is handed over to the Plugin at initializaion. Keep in and supply down the execution chain, as needed --- src/custom-sort/custom-sort.ts | 15 +++++- src/main.ts | 53 ++++++++++--------- src/utils/BookmarksCorePluginSignature.ts | 7 ++- .../ObsidianIconFolderPluginSignature.ts | 4 +- src/utils/StarredPluginSignature.ts | 4 +- 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/custom-sort/custom-sort.ts b/src/custom-sort/custom-sort.ts index 18880d7..8e26480 100644 --- a/src/custom-sort/custom-sort.ts +++ b/src/custom-sort/custom-sort.ts @@ -703,6 +703,9 @@ export const determineBookmarksOrderIfNeeded = (folderItems: Array fileExplorerView.fileItems[item.path]) - if (requireApiVersion && requireApiVersion("0.15.0")) { + console.log(`4 of length ${items.length}`) + + if (requireApiVersion && requireApiVersion("0.16.0")) { + console.log('4.3') + const scrollTop = fileExplorerView.navFileContainerEl.scrollTop + fileExplorerView.tree.infinityScroll.rootEl.vChildren.setChildren([items]) + fileExplorerView.navFileContainerEl.scrollTop = scrollTop + fileExplorerView.tree.infinityScroll.compute() + } else if (requireApiVersion && requireApiVersion("0.15.0")) { + console.log('4.1') this.vChildren.setChildren(items); } else { + console.log('4.2') this.children = items; } }; diff --git a/src/main.ts b/src/main.ts index fcdbd88..3d9eca3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -116,7 +116,7 @@ export default class CustomSortPlugin extends Plugin { } readAndParseSortingSpec() { - const mCache: MetadataCache = app.metadataCache + const mCache: MetadataCache = this.app.metadataCache let failed: boolean = false let anySortingSpecFound: boolean = false let errorMessage: string | null = null @@ -134,7 +134,7 @@ export default class CustomSortPlugin extends Plugin { ) } - Vault.recurseChildren(app.vault.getRoot(), (file: TAbstractFile) => { + Vault.recurseChildren(this.app.vault.getRoot(), (file: TAbstractFile) => { if (failed) return if (file instanceof TFile) { const aFile: TFile = file as TFile @@ -249,7 +249,7 @@ export default class CustomSortPlugin extends Plugin { // Syntax sugar const ForceFlushCache = true if (!this.settings.suspended) { - getBookmarksPlugin(this.settings.bookmarksGroupToConsumeAsOrderingReference, ForceFlushCache) + getBookmarksPlugin(this.app, this.settings.bookmarksGroupToConsumeAsOrderingReference, ForceFlushCache) } if (fileExplorerView) { @@ -306,7 +306,7 @@ export default class CustomSortPlugin extends Plugin { this.ribbonIconStateInaccurate = true } - this.addSettingTab(new CustomSortSettingTab(app, this)); + this.addSettingTab(new CustomSortSettingTab(this.app, this)); this.registerEventHandlers() @@ -321,7 +321,7 @@ export default class CustomSortPlugin extends Plugin { this.registerEvent( // Keep in mind: this event is triggered once after app starts and then after each modification of _any_ metadata - app.metadataCache.on("resolved", () => { + plugin.app.metadataCache.on("resolved", () => { if (!this.settings.suspended) { if (!this.initialAutoOrManualSortingTriggered) { this.readAndParseSortingSpec() @@ -365,7 +365,7 @@ export default class CustomSortPlugin extends Plugin { (item: MenuItem) => { item.setTitle(m ? 'Bookmark it for custom sorting' : 'Bookmark it for sorting'); item.onClick(() => { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { bookmarksPlugin.bookmarkFolderItem(file) bookmarksPlugin.saveDataAndUpdateBookmarkViews(true) @@ -377,7 +377,7 @@ export default class CustomSortPlugin extends Plugin { (item: MenuItem) => { item.setTitle(m ? 'UNbookmark it from custom sorting' : 'UNbookmark it from sorting'); item.onClick(() => { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { bookmarksPlugin.unbookmarkFolderItem(file) bookmarksPlugin.saveDataAndUpdateBookmarkViews(true) @@ -389,7 +389,7 @@ export default class CustomSortPlugin extends Plugin { (item: MenuItem) => { item.setTitle(m ? 'Bookmark it+siblings for custom sorting' : 'Bookmark it+siblings for sorting'); item.onClick(() => { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { const orderedChildren: Array = plugin.orderedFolderItemsForBookmarking(file.parent, bookmarksPlugin) bookmarksPlugin.bookmarkSiblings(orderedChildren) @@ -402,7 +402,7 @@ export default class CustomSortPlugin extends Plugin { (item: MenuItem) => { item.setTitle(m ? 'UNbookmark it+siblings from custom sorting' : 'UNbookmark it+siblings from sorting'); item.onClick(() => { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { const orderedChildren: Array = file.parent.children.map((entry: TFile | TFolder) => entry) bookmarksPlugin.unbookmarkSiblings(orderedChildren) @@ -415,7 +415,7 @@ export default class CustomSortPlugin extends Plugin { (item: MenuItem) => { item.setTitle(m ? 'Bookmark selected for custom sorting' : 'Custom sort: bookmark selected for sorting'); item.onClick(() => { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { files.forEach((file) => { bookmarksPlugin.bookmarkFolderItem(file) @@ -429,7 +429,7 @@ export default class CustomSortPlugin extends Plugin { (item: MenuItem) => { item.setTitle(m ? 'UNbookmark selected from custom sorting' : 'Custom sort: UNbookmark selected from sorting'); item.onClick(() => { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { files.forEach((file) => { bookmarksPlugin.unbookmarkFolderItem(file) @@ -440,7 +440,7 @@ export default class CustomSortPlugin extends Plugin { }; this.registerEvent( - app.workspace.on("file-menu", (menu: Menu, file: TAbstractFile, source: string, leaf?: WorkspaceLeaf) => { + this.app.workspace.on("file-menu", (menu: Menu, file: TAbstractFile, source: string, leaf?: WorkspaceLeaf) => { if (!this.settings.customSortContextSubmenu) return; // Don't show the context menus at all const customSortMenuItem = (item?: MenuItem) => { @@ -457,7 +457,7 @@ export default class CustomSortPlugin extends Plugin { if (submenu) submenu.addSeparator(); if (this.settings.bookmarksContextMenus) { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { const itemAlreadyBookmarkedForSorting: boolean = bookmarksPlugin.isBookmarkedForSorting(file) if (!itemAlreadyBookmarkedForSorting) { @@ -502,7 +502,7 @@ export default class CustomSortPlugin extends Plugin { if (submenu) submenu.addSeparator(); if (this.settings.bookmarksContextMenus) { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { (submenu ?? menu).addItem(getBookmarkSelectedMenuItemForFiles(files)); (submenu ?? menu).addItem(getUnbookmarkSelectedMenuItemForFiles(files)); @@ -521,16 +521,17 @@ export default class CustomSortPlugin extends Plugin { } this.registerEvent( - app.vault.on("rename", (file: TAbstractFile, oldPath: string) => { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + this.app.vault.on("rename", (file: TAbstractFile, oldPath: string) => { + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { bookmarksPlugin.updateSortingBookmarksAfterItemRenamed(file, oldPath) bookmarksPlugin.saveDataAndUpdateBookmarkViews(true) } }) ) - app.vault.on("delete", (file: TAbstractFile) => { - const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference) + + this.app.vault.on("delete", (file: TAbstractFile) => { + const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference) if (bookmarksPlugin) { bookmarksPlugin.updateSortingBookmarksAfterItemDeleted(file) bookmarksPlugin.saveDataAndUpdateBookmarkViews(true) @@ -557,7 +558,7 @@ export default class CustomSortPlugin extends Plugin { } initialize() { - app.workspace.onLayoutReady(() => { + this.app.workspace.onLayoutReady(() => { this.fileExplorerFolderPatched = this.patchFileExplorerFolder(); }) } @@ -576,10 +577,10 @@ export default class CustomSortPlugin extends Plugin { createProcessingContextForSorting(has: HasSortingOrGrouping): ProcessingContext { const ctx: ProcessingContext = { - _mCache: app.metadataCache, - starredPluginInstance: has.grouping.byStarred ? getStarredPlugin() : undefined, - bookmarksPluginInstance: has.grouping.byBookmarks || has.sorting.byBookmarks ? getBookmarksPlugin(this.settings.bookmarksGroupToConsumeAsOrderingReference, false, true) : undefined, - iconFolderPluginInstance: has.grouping.byIcon ? getIconFolderPlugin() : undefined, + _mCache: this.app.metadataCache, + starredPluginInstance: has.grouping.byStarred ? getStarredPlugin(this.app) : undefined, + bookmarksPluginInstance: has.grouping.byBookmarks || has.sorting.byBookmarks ? getBookmarksPlugin(this.app, this.settings.bookmarksGroupToConsumeAsOrderingReference, false, true) : undefined, + iconFolderPluginInstance: has.grouping.byIcon ? getIconFolderPlugin(this.app) : undefined, plugin: this } return ctx @@ -598,6 +599,7 @@ export default class CustomSortPlugin extends Plugin { const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(Folder.prototype, { sort(old: any) { return function (...args: any[]) { + console.log('1') // quick check for plugin status if (plugin.settings.suspended) { return old.call(this, ...args); @@ -615,13 +617,14 @@ export default class CustomSortPlugin extends Plugin { // Primary intention: when the implicit bookmarks integration is enabled, remain on std Obsidian, if no need to involve bookmarks let has: HasSortingOrGrouping = collectSortingAndGroupingTypes(sortSpec) if (hasOnlyByBookmarkOrStandardObsidian(has)) { - const bookmarksPlugin: BookmarksPluginInterface|undefined = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference, false, true) + const bookmarksPlugin: BookmarksPluginInterface|undefined = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference, false, true) if ( !bookmarksPlugin?.bookmarksIncludeItemsInFolder(folder.path)) { sortSpec = null } } if (sortSpec) { + console.log('2') return folderSort.call(this, sortSpec, plugin.createProcessingContextForSorting(has)); } else { return old.call(this, ...args); @@ -656,7 +659,7 @@ export default class CustomSortPlugin extends Plugin { // Credits go to https://github.com/nothingislost/obsidian-bartender getFileExplorer(): FileExplorerView | undefined { - let fileExplorer: FileExplorerView | undefined = app.workspace.getLeavesOfType("file-explorer")?.first() + let fileExplorer: FileExplorerView | undefined = this.app.workspace.getLeavesOfType("file-explorer")?.first() ?.view as unknown as FileExplorerView; return fileExplorer; } diff --git a/src/utils/BookmarksCorePluginSignature.ts b/src/utils/BookmarksCorePluginSignature.ts index c33cb69..54a7b43 100644 --- a/src/utils/BookmarksCorePluginSignature.ts +++ b/src/utils/BookmarksCorePluginSignature.ts @@ -1,4 +1,5 @@ import { + App, InstalledPlugin, PluginInstance, TAbstractFile, @@ -110,6 +111,7 @@ const bookmarkedGroupEmptyOrOnlyTransparentForSortingDescendants = (group: Bookm class BookmarksPluginWrapper implements BookmarksPluginInterface { + app: App plugin: Bookmarks_PluginInstance|undefined groupNameForSorting: string|undefined @@ -148,7 +150,7 @@ class BookmarksPluginWrapper implements BookmarksPluginInterface { saveDataAndUpdateBookmarkViews = (updateBookmarkViews: boolean = true) => { this.plugin!.onItemsChanged(true) if (updateBookmarkViews) { - const bookmarksLeafs = app.workspace.getLeavesOfType('bookmarks') + const bookmarksLeafs = this.app!.workspace.getLeavesOfType('bookmarks') bookmarksLeafs?.forEach((leaf) => { (leaf.view as any)?.update?.() }) @@ -259,7 +261,7 @@ class BookmarksPluginWrapper implements BookmarksPluginInterface { export const BookmarksCorePluginId: string = 'bookmarks' -export const getBookmarksPlugin = (bookmarksGroupName?: string, forceFlushCache?: boolean, ensureCachePopulated?: boolean): BookmarksPluginInterface | undefined => { +export const getBookmarksPlugin = (app: App, bookmarksGroupName?: string, forceFlushCache?: boolean, ensureCachePopulated?: boolean): BookmarksPluginInterface | undefined => { invalidateExpiredBookmarksCache(forceFlushCache) const installedBookmarksPlugin: InstalledPlugin | undefined = app?.internalPlugins?.getPluginById(BookmarksCorePluginId) if (installedBookmarksPlugin && installedBookmarksPlugin.enabled && installedBookmarksPlugin.instance) { @@ -267,6 +269,7 @@ export const getBookmarksPlugin = (bookmarksGroupName?: string, forceFlushCache? // defensive programming, in case Obsidian changes its internal APIs if (typeof bookmarksPluginInstance?.[BookmarksPlugin_getBookmarks_methodName] === 'function' && Array.isArray(bookmarksPluginInstance?.[BookmarksPlugin_items_collectionName])) { + bookmarksPlugin.app = app bookmarksPlugin.plugin = bookmarksPluginInstance bookmarksPlugin.groupNameForSorting = bookmarksGroupName if (ensureCachePopulated && !bookmarksCache) { diff --git a/src/utils/ObsidianIconFolderPluginSignature.ts b/src/utils/ObsidianIconFolderPluginSignature.ts index 91de5cb..e5fa1d9 100644 --- a/src/utils/ObsidianIconFolderPluginSignature.ts +++ b/src/utils/ObsidianIconFolderPluginSignature.ts @@ -1,4 +1,4 @@ -import {CommunityPlugin, TAbstractFile} from "obsidian"; +import {App, CommunityPlugin, TAbstractFile} from "obsidian"; // For https://github.com/FlorianWoelki/obsidian-icon-folder @@ -18,7 +18,7 @@ export interface ObsidianIconFolder_PluginInstance extends CommunityPlugin { // https://github.com/FlorianWoelki/obsidian-icon-folder/blob/fd9c7df1486744450cec3d7ee9cee2b34d008e56/manifest.json#L2 export const ObsidianIconFolderPluginId: string = 'obsidian-icon-folder' -export const getIconFolderPlugin = (): ObsidianIconFolder_PluginInstance | undefined => { +export const getIconFolderPlugin = (app: App): ObsidianIconFolder_PluginInstance | undefined => { const iconFolderPlugin: CommunityPlugin | undefined = app?.plugins?.plugins?.[ObsidianIconFolderPluginId] if (iconFolderPlugin && iconFolderPlugin._loaded && app?.plugins?.enabledPlugins?.has(ObsidianIconFolderPluginId)) { const iconFolderPluginInstance: ObsidianIconFolder_PluginInstance = iconFolderPlugin as ObsidianIconFolder_PluginInstance diff --git a/src/utils/StarredPluginSignature.ts b/src/utils/StarredPluginSignature.ts index f51c483..10aa46c 100644 --- a/src/utils/StarredPluginSignature.ts +++ b/src/utils/StarredPluginSignature.ts @@ -1,4 +1,4 @@ -import {InstalledPlugin, PluginInstance, TAbstractFile, TFile, TFolder} from "obsidian"; +import {App, InstalledPlugin, PluginInstance, TAbstractFile, TFile, TFolder} from "obsidian"; export const StarredPlugin_findStarredFile_methodName = 'findStarredFile' @@ -12,7 +12,7 @@ export interface Starred_PluginInstance extends PluginInstance { export const StarredCorePluginId: string = 'starred' -export const getStarredPlugin = (): Starred_PluginInstance | undefined => { +export const getStarredPlugin = (app: App): Starred_PluginInstance | undefined => { const starredPlugin: InstalledPlugin | undefined = app?.internalPlugins?.getPluginById(StarredCorePluginId) if (starredPlugin && starredPlugin.enabled && starredPlugin.instance) { const starredPluginInstance: Starred_PluginInstance = starredPlugin.instance as Starred_PluginInstance