Biete Bildausschnitt und Zoom für Bilder und Videos in Widgets an (ADR-70)
Redaktionelle Karten erhalten je Karte Fokus und Zoom, Einstieg, Bild und Text sowie Premium-Karten zusätzlich einen Zoom (100 bis 250 Prozent, darunter entstünden leere Ränder). Die Darstellung ist reines CSS um den Fokuspunkt; Video und Clip folgen dem Ausschnitt. Die Regler zeigen beim Laden die Vorgabe statt 0 (FramingSlider). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7f9534d289
commit
9353e2e38a
29 files changed
+641
-40
No files matched your search
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms\Widgets\Support;
|
||||
|
||||
use App\Filament\Support\FramingSlider;
|
||||
use Filament\Forms\Components\Slider;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
|
||||
/**
|
||||
* Bildausschnitt für Bilder und Videos in Widgets (ADR-70): Fokuspunkt (waagerecht, senkrecht, 0–100 %) und Zoom
|
||||
* (100–250 %). Gespeichert wird in data (`focus_x`, `focus_y`, `zoom`), weil settings nur Aufzählungswerte trägt.
|
||||
*
|
||||
* Der Zoom beginnt bei 100 %: Dann füllt das Bild den Rahmen (object-fit: cover), mehr Verkleinern gäbe es nur mit leeren
|
||||
* Rändern. Darüber wird um den Fokuspunkt vergrößert (`scale`, `transform-origin` = Fokuspunkt). Ein Bild, das mindestens
|
||||
* so groß ist wie der Rahmen und um einen Punkt im Rahmen vergrößert wird, lässt den Rahmen nie frei; es entstehen keine
|
||||
* Ränder. Die Rahmen schneiden ab (overflow: hidden), die Darstellung ist reines CSS, es gibt keine neue Bildfassung.
|
||||
*
|
||||
* Die Karten (CardsWidget) tragen den Ausschnitt je Karte, die übrigen Widgets je Widget (PremiumFields).
|
||||
*/
|
||||
final class ImageFraming
|
||||
{
|
||||
public const ZOOM_MIN = 100;
|
||||
|
||||
public const ZOOM_MAX = 250;
|
||||
|
||||
/** Vorgaben des Fokuspunkts bei Widgets, die ihn nicht selbst vorgeben (Karten: Bildmitte). */
|
||||
public const FOCUS_DEFAULT = 50;
|
||||
|
||||
/**
|
||||
* Die drei Regler. Ohne gespeicherten Wert zeigen sie die Vorgabe der Widget-Art, die auch die Darstellung verwendet.
|
||||
*
|
||||
* @return list<Slider>
|
||||
*/
|
||||
public static function sliders(int $defaultX = self::FOCUS_DEFAULT, int $defaultY = self::FOCUS_DEFAULT, ?\Closure $visible = null): array
|
||||
{
|
||||
$slider = function (string $name, string $label, int $min, int $max, int $default, ?string $help = null) use ($visible): FramingSlider {
|
||||
$field = FramingSlider::make($name)
|
||||
->label($label)
|
||||
->range($min, $max)
|
||||
->step(1)
|
||||
->required(false)
|
||||
->tooltips()
|
||||
->default($default)
|
||||
->afterStateHydrated(function (FramingSlider $component, mixed $state) use ($default, $min): void {
|
||||
// Ein Regler ohne Wert beginnt bei null; der Zoom darunter ist keine Eingabe, sondern „nicht gesetzt“.
|
||||
if (! is_numeric($state) || $state < $min) {
|
||||
$component->state($default);
|
||||
}
|
||||
})
|
||||
->live();
|
||||
|
||||
if ($help !== null) {
|
||||
$field->helperText($help);
|
||||
}
|
||||
|
||||
return $visible !== null ? $field->visible($visible) : $field;
|
||||
};
|
||||
|
||||
return [
|
||||
$slider('focus_x', 'Bildausschnitt waagerecht (0 = links, 100 = rechts)', 0, 100, $defaultX),
|
||||
$slider('focus_y', 'Bildausschnitt senkrecht (0 = oben, 100 = unten)', 0, 100, $defaultY),
|
||||
$slider('zoom', 'Zoom (in Prozent)', self::ZOOM_MIN, self::ZOOM_MAX, self::ZOOM_MIN, 'Bei 100 % füllt das Bild den Rahmen; weiter herauszoomen geht nicht, sonst entstünden leere Ränder. Darüber wird um den gewählten Ausschnitt vergrößert. Gilt auch für das Video auf dem Bild.'),
|
||||
];
|
||||
}
|
||||
|
||||
/** Sichtbar, sobald in der Karte ein Bild gewählt ist (Feld `image` desselben Eintrags). */
|
||||
public static function whenImageChosen(): \Closure
|
||||
{
|
||||
return fn (Get $get): bool => filled($get('image'));
|
||||
}
|
||||
|
||||
public static function rules(): array
|
||||
{
|
||||
return [
|
||||
'focus_x' => ['nullable', 'numeric', 'between:0,100'],
|
||||
'focus_y' => ['nullable', 'numeric', 'between:0,100'],
|
||||
// Nach oben begrenzt; Werte darunter gelten als „kein Zoom“ (zoom() hebt sie auf 100 %).
|
||||
'zoom' => ['nullable', 'numeric', 'min:0', 'max:'.self::ZOOM_MAX],
|
||||
];
|
||||
}
|
||||
|
||||
/** Zoom in Prozent (100 bis 250); ungültig oder ohne Angabe 100. */
|
||||
public static function zoom(mixed $value): int
|
||||
{
|
||||
return is_numeric($value) ? (int) round(max(self::ZOOM_MIN, min(self::ZOOM_MAX, (float) $value))) : self::ZOOM_MIN;
|
||||
}
|
||||
|
||||
/** Fokuspunkt in Prozent, ohne Angabe der Vorgabe. */
|
||||
public static function percent(mixed $value, int $default = self::FOCUS_DEFAULT): int
|
||||
{
|
||||
return is_numeric($value) ? (int) round(max(0, min(100, (float) $value))) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Speicherform: der Zoom nur, wenn er über 100 % liegt (Widgets ohne Zoom bleiben byteweise wie zuvor).
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function cleanZoom(array $data): array
|
||||
{
|
||||
$zoom = self::zoom($data['zoom'] ?? null);
|
||||
|
||||
if ($zoom > self::ZOOM_MIN) {
|
||||
$data['zoom'] = $zoom;
|
||||
} else {
|
||||
unset($data['zoom']);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ausschnitt einer Karte in der Speicherform: Schlüssel nur, wenn sie von der Vorgabe (Mitte, 100 %) abweichen.
|
||||
*
|
||||
* @param array<string, mixed> $item
|
||||
* @return array<string, int>
|
||||
*/
|
||||
public static function cleanItem(array $item): array
|
||||
{
|
||||
$out = [];
|
||||
|
||||
foreach (['focus_x', 'focus_y'] as $key) {
|
||||
$value = self::percent($item[$key] ?? null);
|
||||
|
||||
if ($value !== self::FOCUS_DEFAULT) {
|
||||
$out[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$zoom = self::zoom($item['zoom'] ?? null);
|
||||
|
||||
if ($zoom > self::ZOOM_MIN) {
|
||||
$out['zoom'] = $zoom;
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inline-Stil am Bild einer Karte, nur wenn etwas von der Vorgabe abweicht (sonst null: Ausgabe unverändert).
|
||||
*
|
||||
* @param array<string, mixed> $item
|
||||
*/
|
||||
public static function itemStyle(array $item): ?string
|
||||
{
|
||||
$x = self::percent($item['focus_x'] ?? null);
|
||||
$y = self::percent($item['focus_y'] ?? null);
|
||||
$zoom = self::zoom($item['zoom'] ?? null);
|
||||
|
||||
if ($x === self::FOCUS_DEFAULT && $y === self::FOCUS_DEFAULT && $zoom === self::ZOOM_MIN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return 'object-position: '.$x.'% '.$y.'%'.($zoom > self::ZOOM_MIN ? '; transform-origin: '.$x.'% '.$y.'%; scale: '.round($zoom / 100, 2) : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* `sizes` für `srcset`: Bei Zoom wird ein Teil des Bilds auf die Rahmenbreite vergrößert, der Browser soll deshalb eine
|
||||
* größere Fassung holen.
|
||||
*/
|
||||
public static function sizes(string $sizes, int $zoom): string
|
||||
{
|
||||
if ($zoom <= self::ZOOM_MIN) {
|
||||
return $sizes;
|
||||
}
|
||||
|
||||
$factor = round($zoom / 100, 2);
|
||||
|
||||
return (string) preg_replace_callback('/(\d+(?:\.\d+)?)vw/', fn (array $m): string => 'calc('.$m[1].'vw * '.$factor.')', $sizes);
|
||||
}
|
||||
}
|
||||
@@ -37,9 +37,7 @@ public static function rules(): array
|
||||
'cta2_label' => ['nullable', 'string', 'max:80', 'required_with:data.cta2_url'],
|
||||
'cta2_url' => ['nullable', 'string', 'max:500', 'required_with:data.cta2_label'],
|
||||
'alt' => ['nullable', 'string', 'max:300'],
|
||||
'focus_x' => ['nullable', 'numeric', 'between:0,100'],
|
||||
'focus_y' => ['nullable', 'numeric', 'between:0,100'],
|
||||
];
|
||||
] + ImageFraming::rules();
|
||||
}
|
||||
|
||||
public static function eyebrowField(): TextInput
|
||||
@@ -67,29 +65,13 @@ public static function ctaFields(): Grid
|
||||
}
|
||||
|
||||
/**
|
||||
* Fokus waagerecht und senkrecht sowie Zoom (ImageFraming).
|
||||
*
|
||||
* @return list<Slider>
|
||||
*/
|
||||
public static function focusFields(int $defaultX = 50, int $defaultY = 50): array
|
||||
{
|
||||
// Ohne gespeicherten Wert zeigt der Regler die Vorgabe der Widget-Art, die auch die Darstellung verwendet.
|
||||
$slider = fn (string $name, string $label, int $default) => Slider::make($name)
|
||||
->label($label)
|
||||
->range(0, 100)
|
||||
->step(1)
|
||||
->required(false)
|
||||
->tooltips()
|
||||
->default($default)
|
||||
->afterStateHydrated(function (Slider $component, mixed $state) use ($default): void {
|
||||
if (! is_numeric($state)) {
|
||||
$component->state($default);
|
||||
}
|
||||
})
|
||||
->live();
|
||||
|
||||
return [
|
||||
$slider('focus_x', 'Bildausschnitt waagerecht (0 = links, 100 = rechts)', $defaultX),
|
||||
$slider('focus_y', 'Bildausschnitt senkrecht (0 = oben, 100 = unten)', $defaultY),
|
||||
];
|
||||
return ImageFraming::sliders($defaultX, $defaultY);
|
||||
}
|
||||
|
||||
public static function altField(): TextInput
|
||||
@@ -124,7 +106,7 @@ public static function clean(array $data, WidgetErrors $errors, bool $hasImage):
|
||||
$data[$key] = is_numeric($value) ? (int) round(max(0, min(100, (float) $value))) : null;
|
||||
}
|
||||
|
||||
return $data;
|
||||
return ImageFraming::cleanZoom($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,6 +141,12 @@ public static function focus(array $data, int $defaultX = 50, int $defaultY = 50
|
||||
return ['x' => $clamp($data['focus_x'] ?? null, $defaultX), 'y' => $clamp($data['focus_y'] ?? null, $defaultY)];
|
||||
}
|
||||
|
||||
/** Zoom in Prozent (100 bis 250); ohne Angabe 100. */
|
||||
public static function zoom(array $data): int
|
||||
{
|
||||
return ImageFraming::zoom($data['zoom'] ?? null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Übersetzt den früheren Fokuspunkt (Aufzählung wie beim Abschnittshintergrund) in Prozentwerte.
|
||||
*
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use App\Domain\Catalog\FormattedText;
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Support\HeadingAccent;
|
||||
use App\Domain\Cms\Widgets\Support\ImageFraming;
|
||||
use App\Domain\Cms\Widgets\Support\ImageRefFields;
|
||||
use App\Domain\Cms\Widgets\Support\LinkField;
|
||||
use App\Domain\Cms\Widgets\Support\PublicMedia;
|
||||
@@ -161,6 +162,9 @@ public function dataRules(): array
|
||||
'items.*.symbol' => ['nullable', 'string', Rule::in(array_keys(HeadingAccent::symbols()))],
|
||||
'items.*.image' => ['nullable'],
|
||||
'items.*._upload' => ['nullable'],
|
||||
'items.*.focus_x' => ImageFraming::rules()['focus_x'],
|
||||
'items.*.focus_y' => ImageFraming::rules()['focus_y'],
|
||||
'items.*.zoom' => ImageFraming::rules()['zoom'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -231,6 +235,7 @@ public function dataFields(): array
|
||||
$image,
|
||||
$upload,
|
||||
$status,
|
||||
...ImageFraming::sliders(visible: ImageFraming::whenImageChosen()),
|
||||
])
|
||||
->minItems(1)
|
||||
->maxItems(12)
|
||||
@@ -294,6 +299,8 @@ public function clean(array $data, array $refs, WidgetErrors $errors): array
|
||||
...(filled($item['url'] ?? null) && filled($item['link_label'] ?? null) ? ['link_label' => trim((string) $item['link_label'])] : []),
|
||||
...(filled($item['symbol'] ?? null) ? ['symbol' => $item['symbol']] : []),
|
||||
'image' => $itemRefs['image'],
|
||||
// Bildausschnitt (ADR-70) nur, wenn er von der Vorgabe (Mitte, 100 %) abweicht und ein Bild da ist.
|
||||
...($itemRefs['image'] !== null ? ImageFraming::cleanItem($item) : []),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -320,6 +327,8 @@ public function resolve(array $widget, RenderContext $context): array
|
||||
'symbol' => HeadingAccent::symbol(['symbol' => $item['symbol'] ?? null]),
|
||||
'imageUrl' => $images->url($item['image'] ?? null, $context->preview),
|
||||
'imageSrcset' => $images->srcset($item['image'] ?? null),
|
||||
'imageStyle' => ImageFraming::itemStyle($item),
|
||||
'imageSizes' => ImageFraming::sizes('(min-width: 900px) 40vw, 100vw', ImageFraming::zoom($item['zoom'] ?? null)),
|
||||
];
|
||||
|
||||
if ($context->preview && ($notice = $images->previewNotice($item['image'] ?? null, 'Das Bild der Karte „'.$item['title'].'“')) !== null) {
|
||||
|
||||
@@ -220,11 +220,11 @@ public function editorSchema(): array
|
||||
|
||||
public function mediaSchema(): array
|
||||
{
|
||||
[, , , , $alt, $focusX, $focusY] = $this->dataFields();
|
||||
[, , , , $alt, $focusX, $focusY, $zoom] = $this->dataFields();
|
||||
|
||||
return [
|
||||
Group::make($this->refFields())->statePath('refs'),
|
||||
Group::make([$alt, $focusX, $focusY])->statePath('data'),
|
||||
Group::make([$alt, $focusX, $focusY, $zoom])->statePath('data'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -262,6 +262,7 @@ public function resolve(array $widget, RenderContext $context): array
|
||||
'actions' => $actions,
|
||||
'action' => $actions[0] ?? null,
|
||||
'focus' => PremiumFields::focus($widget['data'], 68, 50),
|
||||
'zoom' => PremiumFields::zoom($widget['data']),
|
||||
'accent' => in_array($widget['settings']['accent'] ?? null, ['underline', 'rays', 'none'], true) ? $widget['settings']['accent'] : 'underline',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -272,11 +272,11 @@ public function editorSchema(): array
|
||||
|
||||
public function mediaSchema(): array
|
||||
{
|
||||
[, , , , $alt, $focusX, $focusY] = $this->dataFields();
|
||||
[, , , , $alt, $focusX, $focusY, $zoom] = $this->dataFields();
|
||||
|
||||
return [
|
||||
Group::make($this->refFields())->statePath('refs'),
|
||||
Group::make([$alt, $focusX, $focusY])->statePath('data'),
|
||||
Group::make([$alt, $focusX, $focusY, $zoom])->statePath('data'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -357,6 +357,7 @@ public function resolve(array $widget, RenderContext $context): array
|
||||
'body' => TextMarkers::decorate(RichText::sanitize(is_string($widget['data']['body'] ?? null) ? $widget['data']['body'] : '')),
|
||||
'actions' => PremiumFields::actions($widget['data']),
|
||||
'focus' => PremiumFields::focus($widget['data'], 64, 43),
|
||||
'zoom' => PremiumFields::zoom($widget['data']),
|
||||
// Eine gewählte Farbe aus der Palette (ADR-68) hat Vorrang vor „Hintergrund des Widgets“.
|
||||
'surface' => WidgetAppearance::slot($this, $widget) !== null ? 'palette' : (in_array($surface, ['cream', 'clear', 'navy'], true) ? $surface : 'cream'),
|
||||
];
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use App\Domain\Cms\Catalog\CardSubject;
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Support\CardClip;
|
||||
use App\Domain\Cms\Widgets\Support\ImageFraming;
|
||||
use App\Domain\Cms\Widgets\Support\ImageRefFields;
|
||||
use App\Domain\Cms\Widgets\Support\PremiumFields;
|
||||
use App\Domain\Cms\Widgets\Support\PublicMedia;
|
||||
@@ -228,9 +229,7 @@ public function dataRules(): array
|
||||
'heading' => ['nullable', 'string', 'max:200'],
|
||||
'summary' => ['nullable', 'string', 'max:500'],
|
||||
'alt' => ['nullable', 'string', 'max:300'],
|
||||
'focus_x' => ['nullable', 'numeric', 'between:0,100'],
|
||||
'focus_y' => ['nullable', 'numeric', 'between:0,100'],
|
||||
];
|
||||
] + ImageFraming::rules();
|
||||
}
|
||||
|
||||
public function refRules(): array
|
||||
@@ -294,11 +293,11 @@ public function editorSchema(): array
|
||||
|
||||
public function mediaSchema(): array
|
||||
{
|
||||
[, , $alt, $focusX, $focusY] = $this->dataFields();
|
||||
[, , $alt, $focusX, $focusY, $zoom] = $this->dataFields();
|
||||
|
||||
return [
|
||||
Group::make([...ImageRefFields::fields('Kartenbild', live: true), ...$this->imageSourceFields()])->statePath('refs'),
|
||||
Group::make([$alt, $focusX, $focusY])->statePath('data'),
|
||||
Group::make([$alt, $focusX, $focusY, $zoom])->statePath('data'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -324,7 +323,7 @@ public function clean(array $data, array $refs, WidgetErrors $errors): array
|
||||
$data[$field] = is_numeric($value) ? (int) round(max(0, min(100, (float) $value))) : null;
|
||||
}
|
||||
|
||||
return [$data, $refs];
|
||||
return [ImageFraming::cleanZoom($data), $refs];
|
||||
}
|
||||
|
||||
public function resolve(array $widget, RenderContext $context): array
|
||||
@@ -367,6 +366,7 @@ public function resolve(array $widget, RenderContext $context): array
|
||||
'alt' => app(WebsiteImage::class)->alt($imageRef, $data['alt'] ?? null),
|
||||
'imageNotice' => $context->preview ? app(WebsiteImage::class)->previewNotice($imageRef, 'Das Kartenbild') : null,
|
||||
'focus' => PremiumFields::focus($data, 66, 45),
|
||||
'zoom' => PremiumFields::zoom($data),
|
||||
'format' => $option('format', 'landscape'),
|
||||
'fit' => $option('fit', 'cover'),
|
||||
'headingTag' => $option('heading_level', 'h3'),
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support;
|
||||
|
||||
use Filament\Forms\Components\Slider;
|
||||
use Filament\Schemas\Components\StateCasts\Contracts\StateCast;
|
||||
use Filament\Schemas\Components\StateCasts\SliderStateCast;
|
||||
|
||||
/**
|
||||
* Regler für Bildfokus und Zoom (ImageFraming). Filaments Regler macht beim Laden aus „kein Wert“ eine 0; der Dialog
|
||||
* zeigte dann bei jedem Widget ohne gespeicherten Ausschnitt „0“ statt der Vorgabe, mit der die Seite tatsächlich
|
||||
* dargestellt wird (Mitte, 100 %). Ohne diese Umwandlung setzt der Regler beim Laden die Vorgabe selbst.
|
||||
*/
|
||||
class FramingSlider extends Slider
|
||||
{
|
||||
/**
|
||||
* @return array<StateCast>
|
||||
*/
|
||||
public function getDefaultStateCasts(): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
parent::getDefaultStateCasts(),
|
||||
fn (object $cast): bool => ! $cast instanceof SliderStateCast,
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user