|
1 | 1 | import { |
2 | | - apiVersion, |
3 | | - App, |
4 | 2 | FileExplorerView, |
5 | 3 | Menu, |
6 | 4 | MenuItem, |
7 | 5 | MetadataCache, |
8 | | - normalizePath, |
9 | 6 | Notice, |
10 | 7 | Platform, |
11 | 8 | Plugin, |
12 | | - PluginSettingTab, |
13 | | - requireApiVersion, |
14 | | - sanitizeHTMLToDom, |
15 | 9 | setIcon, |
16 | | - Setting, |
17 | 10 | TAbstractFile, |
18 | 11 | TFile, |
19 | 12 | TFolder, |
20 | 13 | Vault, WorkspaceLeaf |
21 | 14 | } from 'obsidian'; |
22 | 15 | import {around} from 'monkey-around'; |
23 | 16 | import { |
24 | | - folderSort_vUpTo_1_6_0, |
25 | | - getSortedFolderItems_vFrom_1_6_0, |
| 17 | + getSortedFolderItems, |
26 | 18 | ObsidianStandardDefaultSortingName, |
27 | 19 | ProcessingContext, |
28 | 20 | sortFolderItemsForBookmarking |
@@ -178,15 +170,8 @@ export default class CustomSortPlugin |
178 | 170 | checkFileExplorerIsAvailableAndPatchable(logWarning: boolean = true): FileExplorerView | undefined { |
179 | 171 | let fileExplorerView: FileExplorerView | undefined = this.getFileExplorer() |
180 | 172 | 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 |
190 | 175 | } |
191 | 176 | } |
192 | 177 | // Various scenarios when File Explorer was turned off (e.g. by some other plugin) |
@@ -485,44 +470,42 @@ export default class CustomSortPlugin |
485 | 470 | }) |
486 | 471 | ) |
487 | 472 |
|
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 |
517 | 478 |
|
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() |
522 | 487 | } |
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 | + ) |
526 | 509 |
|
527 | 510 | this.registerEvent( |
528 | 511 | this.app.vault.on("rename", (file: TAbstractFile, oldPath: string) => { |
@@ -633,60 +616,29 @@ export default class CustomSortPlugin |
633 | 616 | // That's why not showing and not logging error message here |
634 | 617 | patchableFileExplorer = patchableFileExplorer ?? this.checkFileExplorerIsAvailableAndPatchable(false) |
635 | 618 | 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 | + } |
673 | 626 |
|
674 | | - plugin.resetIconInaccurateStateToEnabled() |
| 627 | + plugin.resetIconInaccurateStateToEnabled() |
675 | 628 |
|
676 | | - const folder: TFolder = this.file |
677 | | - const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder) |
| 629 | + const folder = args[0] |
| 630 | + const sortingData = plugin.determineAndPrepareSortingDataForFolder(folder) |
678 | 631 |
|
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 |
690 | 642 | } else { |
691 | 643 | return false |
692 | 644 | } |
@@ -730,7 +682,7 @@ export default class CustomSortPlugin |
730 | 682 | const data: any = await this.loadData() || {} |
731 | 683 | const isFreshInstall: boolean = Object.keys(data).length === 0 |
732 | 684 | this.settings = Object.assign({}, DEFAULT_SETTINGS, data); |
733 | | - if (requireApiVersion('1.2.0') && isFreshInstall) { |
| 685 | + if (isFreshInstall) { |
734 | 686 | this.settings = Object.assign(this.settings, DEFAULT_SETTING_FOR_1_2_0_UP) |
735 | 687 | } |
736 | 688 | } |
|
0 commit comments