[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
This commit is contained in:
4Source 2026-03-02 22:21:37 +01:00 committed by GitHub
parent a4182bf48a
commit b7d7227a7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<CommunityPlugin>(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<CommunityTheme>(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) || '[]');