diff --git a/hifi-src/src/pages/admin/HomeSections.jsx b/hifi-src/src/pages/admin/HomeSections.jsx index 90382f8..72cc25d 100644 --- a/hifi-src/src/pages/admin/HomeSections.jsx +++ b/hifi-src/src/pages/admin/HomeSections.jsx @@ -1,6 +1,7 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { api } from '../../api/client.js'; import RichTextEditor from '../../components/RichTextEditor.jsx'; +import { dragAnchorStyle } from '../../utils/dragAnchorPosition.js'; const SECTIONS = [ { slug: 'hero', label: 'Hero (ganz oben)' }, @@ -11,12 +12,30 @@ const SECTIONS = [ { 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 [sections, setSections] = useState(null); const [activeSlug, setActiveSlug] = useState('hero'); @@ -24,9 +43,15 @@ export default function HomeSections() { 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) { @@ -34,12 +59,42 @@ export default function HomeSections() { } 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(''); @@ -54,12 +109,16 @@ export default function HomeSections() { } }; + 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, Ausrichtung und (falls vorhanden) Button-Ziel der wichtigsten Textabschnitte auf der + 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.

@@ -87,42 +146,139 @@ export default function HomeSections() { ))}
-
-
- {[ - ['de', 'Deutsch'], - ['en', 'English'], - ].map(([lang, label]) => ( - - ))} -
-
- - -
+ {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) => ( + + ))} +
+
+
+ )} +