From b7d7227a7c5cad23eeae392112a10fc9ffaf4e62 Mon Sep 17 00:00:00 2001 From: 4Source <38220764+4Source@users.noreply.github.com> Date: Mon, 2 Mar 2026 22:21:37 +0100 Subject: [PATCH] [FEAT] Enhance favorite list management with filtering and event listeners (#12) * Enhance favorite list management by filtering out existing favorites * Add event listeners for favorite list changes --- src/main.ts | 60 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/src/main.ts b/src/main.ts index 0c6b694..309f1a5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -35,6 +35,18 @@ export default class FavoritesPlugin extends Plugin { console.debug(`Plugins key: ${this.pluginsKey} Themes key: ${this.themesKey}`); this.loadFavorites(); + // Register + this.registerDomEvent(window, 'storage', (event) => { + if (event.key === this.pluginsKey) { + console.debug('Plugins key content changed', event); + this.onFavoritePluginsChanged(event.newValue); + } + if (event.key === this.themesKey) { + console.debug('Themes key content changed', event); + this.onFavoriteThemesChanged(event.newValue); + } + }); + // eslint-disable-next-line @typescript-eslint/no-this-alias -- Is required because the this context wil change inside the 'monkey-around' functions but the plugin is required to be accessible const plugin = this; @@ -374,14 +386,22 @@ export default class FavoritesPlugin extends Plugin { id: 'search-and-add-plugin-to-favorite-list', name: 'Add plugin to favorite list', callback: async () => { - const items = await fetchCommunityPluginList(); + let items = await fetchCommunityPluginList(); if (!items) { new Notice('Failed to fetch community plugins. See console for more information.'); return; } + // Filter out favorites + this.loadFavoritePlugins(); + items = items.filter(value => !this.favoritePlugins.contains(value.id)); + + if (items.length <= 0) { + new Notice('No plugin left which is could be added to favorite list'); + return; + } + new CommunitySuggestModal(this.app, 'Select plugin which should be added to favorites list...', items, (result) => { - this.loadFavoritePlugins(); this.favoritePlugins.push(result.id); this.saveFavoritesPlugins(); new Notice(`Added ${result.name} to favorite list`); @@ -393,14 +413,22 @@ export default class FavoritesPlugin extends Plugin { id: 'search-and-add-theme-to-favorite-list', name: 'Add theme to favorite list', callback: async () => { - const items = await fetchCommunityThemeList(); + let items = await fetchCommunityThemeList(); if (!items) { new Notice('Failed to fetch community themes. See console for more information.'); return; } + // Filter out favorites + this.loadFavoriteThemes(); + items = items.filter(value => !this.favoriteThemes.contains(value.name)); + + if (items.length <= 0) { + new Notice('No theme left which is could be added to favorite list'); + return; + } + new CommunitySuggestModal(this.app, 'Select theme which should be added to favorites list...', items, (result) => { - this.loadFavoriteThemes(); this.favoriteThemes.push(result.name); this.saveFavoritesThemes(); new Notice(`Added ${result.name} to favorite list`); @@ -409,7 +437,7 @@ export default class FavoritesPlugin extends Plugin { }); this.addCommand({ - id: 'search-and-remove-plugin-to-favorite-list', + id: 'search-and-remove-plugin-from-favorite-list', name: 'Remove plugin from favorite list', callback: async () => { this.loadFavoritePlugins(); @@ -432,7 +460,7 @@ export default class FavoritesPlugin extends Plugin { }); this.addCommand({ - id: 'search-and-remove-theme-to-favorite-list', + id: 'search-and-remove-theme-from-favorite-list', name: 'Remove theme from favorite list', callback: async () => { this.loadFavoriteThemes(); @@ -511,6 +539,26 @@ export default class FavoritesPlugin extends Plugin { } } + onFavoritePluginsChanged(newValue: string | null) { + console.debug(`onFavoritePluginsChanged: ${newValue}`); + if (newValue) { + this.favoritePlugins = JSON.parse(newValue); + } + else { + this.favoritePlugins = []; + } + } + + onFavoriteThemesChanged(newValue: string | null) { + console.debug(`onFavoriteThemesChanged: ${newValue}`); + if (newValue) { + this.favoriteThemes = JSON.parse(newValue); + } + else { + this.favoriteThemes = []; + } + } + loadFavoritePlugins() { // Load the favorite plugins this.favoritePlugins = JSON.parse(localStorage.getItem(this.pluginsKey) || '[]');