Skip to content

Commit

Permalink
Snapshot commit before extraction of some refactorings and minor fixe…
Browse files Browse the repository at this point in the history
…d into a separate branch
  • Loading branch information
SebastianMC committed Aug 23, 2023
1 parent 140f7a6 commit c309022
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
57 changes: 52 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
apiVersion,
App,
FileExplorerView, Menu, MenuItem,
MetadataCache,
normalizePath,
Notice,
Platform,
Plugin,
PluginSettingTab,
PluginSettingTab, requireApiVersion,
sanitizeHTMLToDom,
setIcon,
Setting,
Expand Down Expand Up @@ -48,6 +49,7 @@ interface CustomSortPluginSettings {
notificationsEnabled: boolean
mobileNotificationsEnabled: boolean
automaticBookmarksIntegration: boolean
bookmarksContextMenus: boolean
bookmarksGroupToConsumeAsOrderingReference: string
}

Expand All @@ -58,9 +60,15 @@ const DEFAULT_SETTINGS: CustomSortPluginSettings = {
notificationsEnabled: true,
mobileNotificationsEnabled: false,
automaticBookmarksIntegration: false,
bookmarksContextMenus: false,
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 SORTINGSPEC_YAML_KEY: string = 'sorting-spec'

Expand Down Expand Up @@ -320,8 +328,12 @@ export default class CustomSortPlugin extends Plugin {

this.registerEvent(
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) => {
// TODO: if already bookmarked in the 'custom sort' group (or its descendants) don't show
item.setTitle('Custom sort: bookmark for sorting.');
item.setIcon('hashtag');
item.onClick(() => {
Expand All @@ -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)
})
)
Expand Down Expand Up @@ -485,7 +500,14 @@ export default class CustomSortPlugin extends Plugin {
}

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() {
Expand Down Expand Up @@ -585,9 +607,12 @@ class CustomSortSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}));

containerEl.createEl('h2', {text: 'Bookmarks integration'});
const bookmarksIntegrationDescription: DocumentFragment = sanitizeHTMLToDom(
'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>'
+ '<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 '
Expand Down Expand Up @@ -622,6 +647,28 @@ class CustomSortSettingTab extends PluginSettingTab {
this.plugin.settings.bookmarksGroupToConsumeAsOrderingReference = value.trim() ? pathToFlatString(normalizePath(value)) : '';
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')
)


}
}

Expand Down
19 changes: 18 additions & 1 deletion src/utils/BookmarksCorePluginSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface BookmarksPluginInterface {
bookmarkSiblings(siblings: Array<TAbstractFile>, inTheTop?: boolean): void
updateSortingBookmarksAfterItemRenamed(renamedItem: TAbstractFile, oldPath: string): void
updateSortingBookmarksAfterItemDeleted(deletedItem: TAbstractFile): void
isBookmarkedForSorting(item: TAbstractFile): boolean
}

class BookmarksPluginWrapper implements BookmarksPluginInterface {
Expand Down Expand Up @@ -141,6 +142,22 @@ class BookmarksPluginWrapper implements BookmarksPluginInterface {
updateSortingBookmarksAfterItemDeleted = (deletedItem: TAbstractFile): void => {
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'
Expand Down Expand Up @@ -198,7 +215,7 @@ const traverseBookmarksCollection = (items: Array<BookmarkedItem>, callback: Tra
recursiveTraversal(items, '');
}

const ARTIFICIAL_ANCHOR_SORTING_BOOKMARK_INDICATOR = '#^.'
const ARTIFICIAL_ANCHOR_SORTING_BOOKMARK_INDICATOR = '#^-'

const getOrderedBookmarks = (plugin: Bookmarks_PluginInstance, bookmarksGroupName?: string): OrderedBookmarks | undefined => {
console.log(`Populating bookmarks cache with group scope ${bookmarksGroupName}`)
Expand Down

0 comments on commit c309022

Please sign in to comment.