diff --git a/src/lib/components/furniture/SettingsPanel.svelte b/src/lib/components/furniture/SettingsPanel.svelte index 2654444..f4466fa 100644 --- a/src/lib/components/furniture/SettingsPanel.svelte +++ b/src/lib/components/furniture/SettingsPanel.svelte @@ -9,6 +9,7 @@ import { customCss } from '$lib/stores/customCss'; import { siteCustomization } from '$lib/stores/siteCustomization'; import { primaryColor } from '$lib/stores/primaryColor'; + import { storage } from '$lib/utils/localStorage'; interface Props { standalone?: boolean; @@ -17,27 +18,36 @@ let { standalone = false, onClose }: Props = $props(); + // UI State let showMoreA11y = $state(standalone); let showMoreThemes = $state(standalone); + let showCustomColorInput = $state(false); + let selectedLanguage = $state('en'); + + // Store subscriptions let accessibilitySettings = $state(accessibility); let currentTheme = $state(theme); let currentNavbarDisplay = $state(navbarDisplay); let currentHomepageLayout = $state(homepageLayout); let currentCustomCss = $state(customCss); + + // Form inputs let cssInput = $state(''); - let validationErrors = $state([]); - let validationWarnings = $state([]); - let lastStoreValue = $state(''); let siteTitleInput = $state(''); let siteDescriptionInput = $state(''); let siteIconUrlInput = $state(''); let primaryColorInput = $state(''); - let lastColorStoreValue = $state(''); - let siteCustomizationErrors = $state([]); - let showCustomColorInput = $state(false); - // Preset color palette - bright & vivid - const colorPalette = [ + // Validation & sync state + let validationErrors = $state([]); + let validationWarnings = $state([]); + let siteCustomizationErrors = $state([]); + let lastStoreValue = $state(''); + let lastColorStoreValue = $state(''); + + // Constants + const PRIMARY_A11Y_OPTIONS = ['reduce-motion']; + const COLOR_PALETTE = [ '#1a75ff', '#7711ff', '#dd11ff', @@ -55,8 +65,22 @@ '#44aaff', '#7777ff', ]; + const LANGUAGES = [ + { code: 'en', name: 'English', available: true, flag: '🇺🇸' }, + { code: 'es', name: 'Español', available: false, flag: '🇪🇸' }, + { code: 'fr', name: 'Français', available: false, flag: '🇫🇷' }, + { code: 'de', name: 'Deutsch', available: false, flag: '🇩🇪' }, + ]; - // Sync cssInput with store when store changes (not when textarea changes) + // Derived state + const primaryOptions = $derived( + $accessibilitySettings.options.filter((opt) => PRIMARY_A11Y_OPTIONS.includes(opt.id)), + ); + const additionalOptions = $derived( + $accessibilitySettings.options.filter((opt) => !PRIMARY_A11Y_OPTIONS.includes(opt.id)), + ); + + // Sync effects $effect(() => { if ($currentCustomCss !== lastStoreValue) { cssInput = $currentCustomCss; @@ -64,14 +88,12 @@ } }); - // Sync site customization inputs with store $effect(() => { siteTitleInput = $siteCustomization.title; siteDescriptionInput = $siteCustomization.description; siteIconUrlInput = $siteCustomization.iconUrl; }); - // Sync primary color input with store (only when store changes) $effect(() => { if ($primaryColor !== lastColorStoreValue) { primaryColorInput = $primaryColor; @@ -79,92 +101,8 @@ } }); - // Language selection - let selectedLanguage = $state('en'); - const languages = [ - { code: 'en', name: 'English', available: true, flag: '🇺🇸' }, - { code: 'es', name: 'Español', available: false, flag: '🇪🇸' }, - { code: 'fr', name: 'Français', available: false, flag: '🇫🇷' }, - { code: 'de', name: 'Deutsch', available: false, flag: '🇩🇪' }, - ]; - - // Handle theme change - function handleThemeChange(themeId: ThemeOption) { - theme.setTheme(themeId); - } - - // Handle accessibility setting toggle - function handleAccessibilityToggle(optionId: string) { - accessibility.toggle(optionId); - } - - // Handle navbar display mode change - function handleNavbarDisplayChange(event: Event) { - const target = event.currentTarget as HTMLSelectElement; - navbarDisplay.setMode(target.value as NavbarDisplayMode); - } - - // Handle homepage layout mode change - function handleHomepageLayoutChange(event: Event) { - const target = event.currentTarget as HTMLSelectElement; - homepageLayout.setMode(target.value as HomepageLayoutMode); - } - - // Primary accessibility options (always visible) - const primaryA11yOptions = ['reduce-motion']; - - // Get filtered accessibility options - const primaryOptions = $derived($accessibilitySettings.options.filter((opt) => primaryA11yOptions.includes(opt.id))); - - const additionalOptions = $derived( - $accessibilitySettings.options.filter((opt) => !primaryA11yOptions.includes(opt.id)), - ); - - // Handle link clicks to close menu - function handleLinkClick() { - onClose?.(); - } - - // Handle custom CSS changes - function handleApplyCustomCss() { - const validation = customCss.validate(cssInput); - validationErrors = validation.errors; - validationWarnings = validation.warnings; - - if (validation.isValid) { - customCss.set(cssInput); - } - } - - function handleClearCustomCss() { - cssInput = ''; - customCss.clear(); - validationErrors = []; - validationWarnings = []; - } - - // Handle site customization - function handleApplySiteCustomization() { - const data = { - title: siteTitleInput.trim(), - description: siteDescriptionInput.trim(), - iconUrl: siteIconUrlInput.trim(), - }; - - const validation = siteCustomization.validate(data); - siteCustomizationErrors = validation.errors; - - if (validation.isValid) { - siteCustomization.set(data); - // Reload to apply title/description/icon changes - window.location.reload(); - } - } - - // Auto-save color when changed (only when user changes it, store validates) $effect(() => { const trimmed = primaryColorInput.trim(); - // Only save if different from store (prevents loop) - store handles validation if (trimmed && trimmed !== $primaryColor) { primaryColor.set(trimmed); lastColorStoreValue = trimmed; @@ -174,25 +112,109 @@ } }); - // Reset color to default - function handleResetColor() { - primaryColorInput = ''; - } + // Event handlers + const handlers = { + themeChange: (themeId: ThemeOption) => theme.setTheme(themeId), + a11yToggle: (optionId: string) => accessibility.toggle(optionId), + navbarChange: (e: Event) => + navbarDisplay.setMode((e.currentTarget as HTMLSelectElement).value as NavbarDisplayMode), + homepageChange: (e: Event) => + homepageLayout.setMode((e.currentTarget as HTMLSelectElement).value as HomepageLayoutMode), + linkClick: () => onClose?.(), - function handleClearSiteCustomization() { - siteCustomization.clear(); - siteCustomizationErrors = []; - // Reload to apply changes - window.location.reload(); - } + applyCustomCss: () => { + const validation = customCss.validate(cssInput); + validationErrors = validation.errors; + validationWarnings = validation.warnings; + if (validation.isValid) customCss.set(cssInput); + }, + + clearCustomCss: () => { + cssInput = ''; + customCss.clear(); + validationErrors = []; + validationWarnings = []; + }, + + applySiteCustomization: () => { + const data = { + title: siteTitleInput.trim(), + description: siteDescriptionInput.trim(), + iconUrl: siteIconUrlInput.trim(), + }; + const validation = siteCustomization.validate(data); + siteCustomizationErrors = validation.errors; + if (validation.isValid) { + siteCustomization.set(data); + window.location.reload(); + } + }, + + clearSiteCustomization: () => { + siteCustomization.clear(); + siteCustomizationErrors = []; + window.location.reload(); + }, + + resetColor: () => { + primaryColorInput = ''; + }, + + clearAllData: () => { + if ( + window.confirm( + 'Your theme, language, home and nav layout, site branding, accessibility preferences, custom CSS and all other settings will be discarded. This will also clear your bookmarks, recent tools and other history. Are you sure you want to proceed?', + ) + ) { + storage.clear(); + window.location.reload(); + } + }, + }; +{#snippet validationMessages(errors: string[], warnings: string[])} + {#if errors.length > 0} +
+ +
+ {#each errors as error (error)} +

{error}

+ {/each} +
+
+ {/if} + {#if warnings.length > 0} +
+ +
+ {#each warnings as warning (warning)} +

{warning}

+ {/each} +
+
+ {/if} +{/snippet} + +{#snippet actionButtons(applyHandler: () => void, clearHandler: () => void, applyLabel = 'Apply', clearLabel = 'Clear')} +
+ + +
+{/snippet} + {#snippet accessibilityOption(option: AccessibilityOption)}