Skip to content

Commit 7713f79

Browse files
committed
feat: add recursive bookmark sync for custom sorting
1 parent d47f81b commit 7713f79

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/main.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,19 @@ export default class CustomSortPlugin
444444
});
445445
};
446446

447+
const getSyncBookmarksRecursiveMenuItemForFile = (file: TAbstractFile): ContextMenuProvider =>
448+
(item: MenuItem) => {
449+
item.setTitle(m ? 'Sync bookmarks for custom sorting (recursive)' : 'Sync bookmarks for sorting (recursive)');
450+
item.onClick(() => {
451+
const bookmarksPlugin = getBookmarksPlugin(plugin.app, plugin.settings.bookmarksGroupToConsumeAsOrderingReference)
452+
if (bookmarksPlugin) {
453+
const targetFolder: TFolder = (file instanceof TFolder) ? file : file.parent!
454+
plugin.syncBookmarksRecursive(targetFolder, bookmarksPlugin)
455+
bookmarksPlugin.saveDataAndUpdateBookmarkViews(true)
456+
}
457+
});
458+
};
459+
447460
const getBookmarkSelectedMenuItemForFiles = (files: TAbstractFile[]): ContextMenuProvider =>
448461
(item: MenuItem) => {
449462
item.setTitle(m ? 'Bookmark selected for custom sorting' : 'Custom sort: bookmark selected for sorting');
@@ -500,6 +513,7 @@ export default class CustomSortPlugin
500513
}
501514
(submenu ?? menu).addItem(getBookmarkAllMenuItemForFile(file));
502515
(submenu ?? menu).addItem(getUnbookmarkAllMenuItemForFile(file));
516+
(submenu ?? menu).addItem(getSyncBookmarksRecursiveMenuItemForFile(file));
503517
}
504518
}
505519

@@ -682,6 +696,19 @@ export default class CustomSortPlugin
682696
)
683697
}
684698

699+
syncBookmarksRecursive(folder: TFolder, bookmarksPlugin: BookmarksPluginInterface): void {
700+
// Sync the current folder: bookmark all children that are not yet bookmarked (appended at end)
701+
const orderedChildren: Array<TAbstractFile> = this.orderedFolderItemsForBookmarking(folder, bookmarksPlugin)
702+
bookmarksPlugin.bookmarkSiblings(orderedChildren)
703+
704+
// Recurse into subfolders
705+
for (const child of folder.children) {
706+
if (child instanceof TFolder) {
707+
this.syncBookmarksRecursive(child, bookmarksPlugin)
708+
}
709+
}
710+
}
711+
685712
onunload() {
686713
}
687714

0 commit comments

Comments
 (0)