Snapshot commit before extraction of some refactorings and minor fixed into a separate branch

This commit is contained in:
SebastianMC 2023-08-23 18:08:02 +02:00
parent 140f7a69b1
commit c3090229a9
2 changed files with 70 additions and 6 deletions

View File

@ -1,4 +1,5 @@
import { import {
apiVersion,
App, App,
FileExplorerView, Menu, MenuItem, FileExplorerView, Menu, MenuItem,
MetadataCache, MetadataCache,
@ -6,7 +7,7 @@ import {
Notice, Notice,
Platform, Platform,
Plugin, Plugin,
PluginSettingTab, PluginSettingTab, requireApiVersion,
sanitizeHTMLToDom, sanitizeHTMLToDom,
setIcon, setIcon,
Setting, Setting,
@ -48,6 +49,7 @@ interface CustomSortPluginSettings {
notificationsEnabled: boolean notificationsEnabled: boolean
mobileNotificationsEnabled: boolean mobileNotificationsEnabled: boolean
automaticBookmarksIntegration: boolean automaticBookmarksIntegration: boolean
bookmarksContextMenus: boolean
bookmarksGroupToConsumeAsOrderingReference: string bookmarksGroupToConsumeAsOrderingReference: string
} }
@ -58,9 +60,15 @@ const DEFAULT_SETTINGS: CustomSortPluginSettings = {
notificationsEnabled: true, notificationsEnabled: true,
mobileNotificationsEnabled: false, mobileNotificationsEnabled: false,
automaticBookmarksIntegration: false, automaticBookmarksIntegration: false,
bookmarksContextMenus: false,
bookmarksGroupToConsumeAsOrderingReference: 'sortspec' bookmarksGroupToConsumeAsOrderingReference: 'sortspec'
} }
const DEFAULT_SETTING_FOR_FRESH_INSTALL_1_2_0: Partial<CustomSortPluginSettings> = {
automaticBookmarksIntegration: true,
bookmarksContextMenus: true
}
const SORTSPEC_FILE_NAME: string = 'sortspec.md' const SORTSPEC_FILE_NAME: string = 'sortspec.md'
const SORTINGSPEC_YAML_KEY: string = 'sorting-spec' const SORTINGSPEC_YAML_KEY: string = 'sorting-spec'
@ -320,8 +328,12 @@ export default class CustomSortPlugin extends Plugin {
this.registerEvent( this.registerEvent(
app.workspace.on("file-menu", (menu: Menu, file: TAbstractFile, source: string, leaf?: WorkspaceLeaf) => { app.workspace.on("file-menu", (menu: Menu, file: TAbstractFile, source: string, leaf?: WorkspaceLeaf) => {
if (!this.settings.bookmarksContextMenus) return; // Don't show the context menus at all
const bookmarksPlugin = getBookmarksPlugin(plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
if (!bookmarksPlugin) return; // Don't show context menu if bookmarks plugin not available and not enabled
const bookmarkThisMenuItem = (item: MenuItem) => { const bookmarkThisMenuItem = (item: MenuItem) => {
// TODO: if already bookmarked in the 'custom sort' group (or its descendants) don't show
item.setTitle('Custom sort: bookmark for sorting.'); item.setTitle('Custom sort: bookmark for sorting.');
item.setIcon('hashtag'); item.setIcon('hashtag');
item.onClick(() => { item.onClick(() => {
@ -347,7 +359,10 @@ export default class CustomSortPlugin extends Plugin {
}); });
}; };
menu.addItem(bookmarkThisMenuItem) const itemAlreadyBookmarkedForSorting: boolean = bookmarksPlugin.isBookmarkedForSorting(file)
if (!itemAlreadyBookmarkedForSorting) {
menu.addItem(bookmarkThisMenuItem)
}
menu.addItem(bookmarkAllMenuItem) menu.addItem(bookmarkAllMenuItem)
}) })
) )
@ -485,7 +500,14 @@ export default class CustomSortPlugin extends Plugin {
} }
async loadSettings() { async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); const data: any = await this.loadData() || {}
const isFreshInstall: boolean = Object.keys(data).length === 0
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
if (isFreshInstall) {
if (requireApiVersion('1.2.0')) {
this.settings = Object.assign(this.settings, DEFAULT_SETTING_FOR_FRESH_INSTALL_1_2_0)
}
}
} }
async saveSettings() { async saveSettings() {
@ -585,9 +607,12 @@ class CustomSortSettingTab extends PluginSettingTab {
await this.plugin.saveSettings(); await this.plugin.saveSettings();
})); }));
containerEl.createEl('h2', {text: 'Bookmarks integration'});
const bookmarksIntegrationDescription: DocumentFragment = sanitizeHTMLToDom( const bookmarksIntegrationDescription: DocumentFragment = sanitizeHTMLToDom(
'If enabled, order of files and folders in File Explorer will reflect the order ' 'If enabled, order of files and folders in File Explorer will reflect the order '
+ 'of bookmarked items in the bookmarks (core plugin) view.' + 'of bookmarked items in the bookmarks (core plugin) view. Automatically, without any '
+ 'need for sorting configuration. At the same time, it integrates seamlessly with'
+ '<pre style="display: inline;">sorting-spec:</pre> configurations and they can nicely cooperate.'
+ '<br>' + '<br>'
+ '<p>To separate regular bookmarks from the bookmarks created for sorting, you can put ' + '<p>To separate regular bookmarks from the bookmarks created for sorting, you can put '
+ 'the latter in a separate dedicated bookmarks group. The default name of the group is ' + 'the latter in a separate dedicated bookmarks group. The default name of the group is '
@ -622,6 +647,28 @@ class CustomSortSettingTab extends PluginSettingTab {
this.plugin.settings.bookmarksGroupToConsumeAsOrderingReference = value.trim() ? pathToFlatString(normalizePath(value)) : ''; this.plugin.settings.bookmarksGroupToConsumeAsOrderingReference = value.trim() ? pathToFlatString(normalizePath(value)) : '';
await this.plugin.saveSettings(); await this.plugin.saveSettings();
})); }));
const bookmarksIntegrationContextMenusDescription: DocumentFragment = sanitizeHTMLToDom(
'Enable <i>Custom-sort: bookmark for sorting</i> and <i>Custom-sort: bookmark+siblings for sorting.</i> entries '
+ 'in context menu in File Explorer'
)
new Setting(containerEl)
.setName('Context menus for Bookmarks integration')
.setDesc(bookmarksIntegrationContextMenusDescription)
.addToggle(toggle => toggle
.setValue(this.plugin.settings.bookmarksContextMenus)
.onChange(async (value) => {
this.plugin.settings.bookmarksContextMenus = value;
await this.plugin.saveSettings();
}))
.addButton(cb => cb
.setButtonText('Bt1')
)
.addExtraButton(cb => cb
.setIcon('clock')
)
} }
} }

View File

@ -67,6 +67,7 @@ export interface BookmarksPluginInterface {
bookmarkSiblings(siblings: Array<TAbstractFile>, inTheTop?: boolean): void bookmarkSiblings(siblings: Array<TAbstractFile>, inTheTop?: boolean): void
updateSortingBookmarksAfterItemRenamed(renamedItem: TAbstractFile, oldPath: string): void updateSortingBookmarksAfterItemRenamed(renamedItem: TAbstractFile, oldPath: string): void
updateSortingBookmarksAfterItemDeleted(deletedItem: TAbstractFile): void updateSortingBookmarksAfterItemDeleted(deletedItem: TAbstractFile): void
isBookmarkedForSorting(item: TAbstractFile): boolean
} }
class BookmarksPluginWrapper implements BookmarksPluginInterface { class BookmarksPluginWrapper implements BookmarksPluginInterface {
@ -141,6 +142,22 @@ class BookmarksPluginWrapper implements BookmarksPluginInterface {
updateSortingBookmarksAfterItemDeleted = (deletedItem: TAbstractFile): void => { updateSortingBookmarksAfterItemDeleted = (deletedItem: TAbstractFile): void => {
updateSortingBookmarksAfterItemDeleted(this.plugin!, deletedItem, this.groupNameForSorting) updateSortingBookmarksAfterItemDeleted(this.plugin!, deletedItem, this.groupNameForSorting)
} }
isBookmarkedForSorting = (item: TAbstractFile): boolean => {
const itemsCollection: BookmarkedParentFolder|undefined = findGroupForItemPathInBookmarks(item.path, DontCreateIfMissing, this.plugin!, this.groupNameForSorting)
if (itemsCollection) {
if (item instanceof TFile) {
return itemsCollection.items?.some((bkmrk) => bkmrk.type === 'file' && bkmrk.path === item.path)
} else {
const folderName: string = lastPathComponent(item.path)
return itemsCollection.items?.some((bkmrk) =>
(bkmrk.type === 'group' && bkmrk.title === folderName) ||
(bkmrk.type === 'folder' && bkmrk.path === item.path)
)
}
}
return false
}
} }
export const BookmarksCorePluginId: string = 'bookmarks' export const BookmarksCorePluginId: string = 'bookmarks'
@ -198,7 +215,7 @@ const traverseBookmarksCollection = (items: Array<BookmarkedItem>, callback: Tra
recursiveTraversal(items, ''); recursiveTraversal(items, '');
} }
const ARTIFICIAL_ANCHOR_SORTING_BOOKMARK_INDICATOR = '#^.' const ARTIFICIAL_ANCHOR_SORTING_BOOKMARK_INDICATOR = '#^-'
const getOrderedBookmarks = (plugin: Bookmarks_PluginInstance, bookmarksGroupName?: string): OrderedBookmarks | undefined => { const getOrderedBookmarks = (plugin: Bookmarks_PluginInstance, bookmarksGroupName?: string): OrderedBookmarks | undefined => {
console.log(`Populating bookmarks cache with group scope ${bookmarksGroupName}`) console.log(`Populating bookmarks cache with group scope ${bookmarksGroupName}`)