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 { useFontOptionsWithCustom } from '../../context/CustomFontsContext.jsx'; const SECTIONS = [ { slug: 'hero', label: 'Hero (ganz oben)' }, { slug: 'youtube', label: 'YouTube-Sektion' }, { slug: 'craft', label: 'Handwerk' }, { slug: 'advice', label: 'Beratung' }, { slug: 'insights', label: 'Einblicke (Bildergalerie-Intro)' }, { slug: 'more', label: 'Mehr als Car-Hifi' }, ]; // Hero und YouTube haben einen echten Hintergrund (Bild bzw. Video) und der Textblock liegt // darueber - dort ist eine frei ziehbare 2D-Position sinnvoll, genau wie bei den // Bildergalerie-Kacheln (siehe WebsiteSettings.jsx). Die anderen vier Abschnitte sind // normaler, neben einem separaten Bild fliessender bzw. umbrechender Text ohne eigenen // Hintergrund - dort bleibt links/mittig/rechts (text-align auf ganzen Absaetzen) das // richtige Werkzeug, keine frei ziehbare Position wie bei einem einzelnen ueberlagerten Label. const POSITIONABLE_SLUGS = ['hero', 'youtube']; const ALIGN_OPTIONS = [ { value: 'left', label: 'Links' }, { value: 'center', label: 'Mittig' }, { value: 'right', label: 'Rechts' }, ]; // Feste Klassen (nicht dynamisch zusammengesetzt) fuer Tailwinds Build-Zeit-Scanner - siehe // Kommentar bei dragAnchorPosition.js/Home.jsx. const ALIGN_OPTIONS_TEXT = { left: 'text-left', center: 'text-center', right: 'text-right' }; function stripHtml(html) { const div = document.createElement('div'); div.innerHTML = html || ''; return div.textContent || ''; } export default function HomeSections() { const fontOptions = useFontOptionsWithCustom(); const [sections, setSections] = useState(null); const [activeSlug, setActiveSlug] = useState('hero'); const [activeLang, setActiveLang] = useState('de'); const [saveBusy, setSaveBusy] = useState(false); const [saveError, setSaveError] = useState(''); const [saveSuccess, setSaveSuccess] = useState(false); // Fuer eine realistischere Hero-Vorschau (echtes Hero-Bild, falls schon eines hochgeladen ist). const [heroImagePath, setHeroImagePath] = useState(''); const previewRef = useRef(null); const [dragging, setDragging] = useState(false); useEffect(() => { api.get('/home-sections').then(setSections); api.get('/site-settings').then((res) => setHeroImagePath(res.hero_image_path || '')); }, []); if (!sections) { return

Lädt…

; } const active = sections[activeSlug]; const isPositionable = POSITIONABLE_SLUGS.includes(activeSlug); const update = (field, value) => { setSections((s) => ({ ...s, [activeSlug]: { ...s[activeSlug], [field]: value } })); setSaveSuccess(false); }; const updatePosition = (x, y) => { setSections((s) => ({ ...s, [activeSlug]: { ...s[activeSlug], text_align_x: x, text_align_y: y } })); setSaveSuccess(false); }; const positionFromPointer = (clientX, clientY) => { const rect = previewRef.current.getBoundingClientRect(); return { x: Math.max(0, Math.min(100, Math.round(((clientX - rect.left) / rect.width) * 100))), y: Math.max(0, Math.min(100, Math.round(((clientY - rect.top) / rect.height) * 100))), }; }; const handleDragStart = (e) => { e.preventDefault(); e.currentTarget.setPointerCapture(e.pointerId); setDragging(true); const { x, y } = positionFromPointer(e.clientX, e.clientY); updatePosition(x, y); }; const handleDragMove = (e) => { if (!dragging) return; const { x, y } = positionFromPointer(e.clientX, e.clientY); updatePosition(x, y); }; const handleDragEnd = (e) => { setDragging(false); e.currentTarget.releasePointerCapture(e.pointerId); }; const handleSave = async () => { setSaveBusy(true); setSaveError(''); setSaveSuccess(false); try { await api.put(`/home-sections/${activeSlug}`, active); setSaveSuccess(true); } catch (err) { setSaveError(err.message); } finally { setSaveBusy(false); } }; const previewEyebrow = active[`eyebrow_${activeLang}`] || ''; const previewTitle = active[`title_${activeLang}`] || SECTIONS.find((s) => s.slug === activeSlug)?.label || ''; const previewBody = stripHtml(active[`body_html_${activeLang}`]); return (

Startseite

Text, Position/Ausrichtung und (falls vorhanden) Button-Ziel der wichtigsten Textabschnitte auf der Startseite. Bilder, Statistik-Kacheln, Marken-Streifen, Bewertungen und FAQ werden an anderer Stelle verwaltet.

{SECTIONS.map((s) => ( ))}
{[ ['de', 'Deutsch'], ['en', 'English'], ].map(([lang, label]) => ( ))}
{isPositionable ? (
{activeSlug === 'hero' && heroImagePath ? ( ) : (
{activeSlug === 'youtube' ? 'Video-Hintergrund' : 'Hero-Bild'}
)}
{previewEyebrow && (

{previewEyebrow}

)}

{previewTitle}

Zum Verschieben ziehen

Position:{' '} {active.text_align_x}% / {active.text_align_y}%

Schnellauswahl waagerecht

Schnellauswahl senkrecht

) : (
{previewEyebrow && (

{previewEyebrow}

)}

{previewTitle}

{previewBody || 'Text erscheint hier…'}

{ALIGN_OPTIONS.map((opt) => ( ))}
)}
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" />
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(`body_html_${activeLang}`, html)} />
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" />
update('button_url', e.target.value)} placeholder="/leistungen oder https://…" 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" />

Ohne Beschriftung UND Ziel wird kein Button angezeigt. Mit "/" beginnende Ziele bleiben innerhalb der Seite, alles andere öffnet extern in einem neuen Tab.

{active.updated_at && (

Zuletzt geändert: {new Date(active.updated_at.replace(' ', 'T')).toLocaleString('de-DE')}

)} {saveError &&

{saveError}

} {saveSuccess &&

Gespeichert.

}
); }