From 260cd0700e882539b744f83a4ef149234c6f8d1e Mon Sep 17 00:00:00 2001 From: Maaxxs <61059039+MaaxxsDev@users.noreply.github.com> Date: Thu, 10 Sep 2026 02:59:06 +0200 Subject: [PATCH] Add generic per-field text styling (font/size/color), starting with home sections Adds a new text_styles table keyed by field_key ("{table}.{slugOrId}.{column}") so any individual text field anywhere in the dashboard can get its own font-family/font-size/color override, without a dedicated column per field. App\Support\TextStyleFields is a server-side registry of which table/column/ permission combinations are valid, so TextStyleController::update() can't be used to write styling for a field outside the caller's own permission domain, and typos can't create permanently orphaned rows. Resetting all three values deletes the row entirely rather than leaving it null, so the table only ever holds fields an admin actually customized. Font-size reuses the same "curated list, mirrored PHP+JS constant" approach already used for font-family (App\Support\Fonts / fontOptions.js) - a free px input on dozens of independently-edited fields with no design review step is the most likely way to end up with an accidentally 4px or 400px heading. New reusable pieces: TextStyleWidget.jsx (a self-contained font/size/color toolbar with a live preview, used next to any field by passing its field_key) and TextStylesContext.jsx + useTextStyle() hook (bulk-fetches all styles once per page load in PublicLayout.jsx, same pattern as SiteSettingsContext). This first phase wires both into the home-section eyebrow/title/button-label fields (the ones from the screenshot that started this) as a proven pilot; the registry is designed so extending it to the remaining ~15 admin pages is just adding entries and wiring the widget/hook, no new architecture. Verified locally via direct PHP invocation: migration creates the table, registry validation accepts only known table/slug-or-id/column combinations and rejects the rest, upsert/reset round-trips correctly, and a second migration run doesn't re-touch anything. Co-Authored-By: Claude Sonnet 5 --- hifi-src/src/components/PublicLayout.jsx | 5 + hifi-src/src/components/TextStyleWidget.jsx | 93 +++++ hifi-src/src/context/TextStylesContext.jsx | 21 ++ hifi-src/src/pages/admin/HomeSections.jsx | 13 + hifi-src/src/pages/public/Home.jsx | 52 ++- hifi-src/src/utils/fontSizeOptions.js | 13 + hifi/api/database/migration_text_styles.sql | 17 + hifi/api/database/schema.sql | 9 + hifi/api/public/index.php | 7 + .../src/Controllers/TextStyleController.php | 111 ++++++ hifi/api/src/Support/FontSizes.php | 13 + hifi/api/src/Support/Schema.php | 22 ++ hifi/api/src/Support/TextStyleCleanup.php | 19 + hifi/api/src/Support/TextStyleFields.php | 29 ++ hifi/assets/index-Bcw7aoHB.js | 325 ++++++++++++++++++ hifi/assets/index-CV-knTge.css | 1 - hifi/assets/index-D5DdJjJZ.js | 325 ------------------ hifi/assets/index-fn7vJl0-.css | 1 + hifi/index.html | 4 +- 19 files changed, 737 insertions(+), 343 deletions(-) create mode 100644 hifi-src/src/components/TextStyleWidget.jsx create mode 100644 hifi-src/src/context/TextStylesContext.jsx create mode 100644 hifi-src/src/utils/fontSizeOptions.js create mode 100644 hifi/api/database/migration_text_styles.sql create mode 100644 hifi/api/src/Controllers/TextStyleController.php create mode 100644 hifi/api/src/Support/FontSizes.php create mode 100644 hifi/api/src/Support/TextStyleCleanup.php create mode 100644 hifi/api/src/Support/TextStyleFields.php create mode 100644 hifi/assets/index-Bcw7aoHB.js delete mode 100644 hifi/assets/index-CV-knTge.css delete mode 100644 hifi/assets/index-D5DdJjJZ.js create mode 100644 hifi/assets/index-fn7vJl0-.css diff --git a/hifi-src/src/components/PublicLayout.jsx b/hifi-src/src/components/PublicLayout.jsx index 03e5acf..63f4496 100644 --- a/hifi-src/src/components/PublicLayout.jsx +++ b/hifi-src/src/components/PublicLayout.jsx @@ -9,6 +9,7 @@ import CookieConsentBanner from './CookieConsentBanner.jsx'; import GoogleAnalytics from './GoogleAnalytics.jsx'; import { MaintenanceProvider } from '../context/MaintenanceContext.jsx'; import { SiteSettingsProvider } from '../context/SiteSettingsContext.jsx'; +import { TextStylesProvider } from '../context/TextStylesContext.jsx'; import { LanguageProvider } from '../context/LanguageContext.jsx'; import { useAuth } from '../context/AuthContext.jsx'; import { api } from '../api/client.js'; @@ -58,12 +59,14 @@ const LEGAL_PATHS = ['/impressum', '/datenschutz', '/agb']; export default function PublicLayout() { const [status, setStatus] = useState(null); const [siteSettings, setSiteSettings] = useState(DEFAULT_SITE_SETTINGS); + const [textStyles, setTextStyles] = useState({}); const { loading: authLoading, hasPermission } = useAuth(); const location = useLocation(); useEffect(() => { api.get('/maintenance').then(setStatus).catch(() => setStatus(FALLBACK_STATUS)); api.get('/site-settings').then(setSiteSettings).catch(() => {}); + api.get('/text-styles').then(setTextStyles).catch(() => {}); }, []); // Globale Schriftart als CSS-Variable auf - tailwind.config.js definiert font-sans @@ -96,6 +99,7 @@ export default function PublicLayout() { +
{bypass && status.global.enabled && } @@ -107,6 +111,7 @@ export default function PublicLayout() {
+
diff --git a/hifi-src/src/components/TextStyleWidget.jsx b/hifi-src/src/components/TextStyleWidget.jsx new file mode 100644 index 0000000..8b5ca03 --- /dev/null +++ b/hifi-src/src/components/TextStyleWidget.jsx @@ -0,0 +1,93 @@ +import { useEffect, useState } from 'react'; +import { api } from '../api/client.js'; +import { FONT_OPTIONS } from '../utils/fontOptions.js'; +import { FONT_SIZE_OPTIONS } from '../utils/fontSizeOptions.js'; + +const EMPTY = { font_family: '', font_size: '', color: '' }; + +/** + * Kleine, in sich geschlossene Werkzeugleiste fuer Schriftart/-groesse/Farbe eines einzelnen + * Textfelds - laedt und speichert eigenstaendig gegen /text-styles, ohne dass die umgebende + * Seite den Stil-Zustand selbst verwalten muss. fieldKey identifiziert das Feld eindeutig + * (siehe App\Support\TextStyleFields fuer gueltige Kombinationen). + */ +export default function TextStyleWidget({ fieldKey, previewText = 'Beispieltext', label = 'Stil' }) { + const [style, setStyle] = useState(EMPTY); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(''); + + useEffect(() => { + setLoading(true); + api + .get(`/text-styles/one?key=${encodeURIComponent(fieldKey)}`) + .then((res) => setStyle({ + font_family: res.font_family || '', + font_size: res.font_size || '', + color: res.color || '', + })) + .catch(() => setStyle(EMPTY)) + .finally(() => setLoading(false)); + }, [fieldKey]); + + const save = async (next) => { + setStyle(next); + setError(''); + try { + await api.put('/text-styles', { field_key: fieldKey, ...next }); + } catch (err) { + setError(err.message); + } + }; + + if (loading) { + return null; + } + + const previewStyle = { + fontFamily: style.font_family || undefined, + fontSize: style.font_size || undefined, + color: style.color || undefined, + }; + + return ( +
+ {label} + + + save({ ...style, color: e.target.value })} + className="h-7 w-9 cursor-pointer rounded border border-neutral-300 bg-white p-0.5 dark:border-neutral-700 dark:bg-neutral-900" + title="Textfarbe" + /> + + + {previewText} + + {error && {error}} +
+ ); +} diff --git a/hifi-src/src/context/TextStylesContext.jsx b/hifi-src/src/context/TextStylesContext.jsx new file mode 100644 index 0000000..f7ce789 --- /dev/null +++ b/hifi-src/src/context/TextStylesContext.jsx @@ -0,0 +1,21 @@ +import { createContext, useContext } from 'react'; + +// Ein einzelner Bulk-Request laedt alle text_styles-Zeilen (siehe PublicLayout.jsx), damit nicht +// jede einzelne Textkomponente einen eigenen Request braucht. Leeres Objekt = noch keine +// Overrides gesetzt, wie beim ersten Laden oder auf frisch installierten Seiten. +const TextStylesContext = createContext({}); + +export const TextStylesProvider = TextStylesContext.Provider; + +/** Liefert {fontFamily, fontSize, color} (nur die tatsaechlich gesetzten Werte) zum + * Hineinspreaden in ein style-Objekt, oder {} wenn fuer dieses Feld nichts hinterlegt ist. */ +export function useTextStyle(fieldKey) { + const styles = useContext(TextStylesContext); + const entry = styles[fieldKey]; + if (!entry) return {}; + const result = {}; + if (entry.font_family) result.fontFamily = entry.font_family; + if (entry.font_size) result.fontSize = entry.font_size; + if (entry.color) result.color = entry.color; + return result; +} diff --git a/hifi-src/src/pages/admin/HomeSections.jsx b/hifi-src/src/pages/admin/HomeSections.jsx index da47752..0ff12c8 100644 --- a/hifi-src/src/pages/admin/HomeSections.jsx +++ b/hifi-src/src/pages/admin/HomeSections.jsx @@ -1,6 +1,7 @@ import { useEffect, useRef, useState } from 'react'; import { api } from '../../api/client.js'; import RichTextEditor from '../../components/RichTextEditor.jsx'; +import TextStyleWidget from '../../components/TextStyleWidget.jsx'; import { dragAnchorStyle } from '../../utils/dragAnchorPosition.js'; import { FONT_OPTIONS } from '../../utils/fontOptions.js'; @@ -290,6 +291,10 @@ export default function HomeSections() { onChange={(e) => update(`eyebrow_${activeLang}`, e.target.value)} className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900" /> +
@@ -298,6 +303,10 @@ export default function HomeSections() { onChange={(e) => update(`title_${activeLang}`, e.target.value)} className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900" /> +
update(`button_label_${activeLang}`, e.target.value)} className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900" /> +