From 6152451e2e9b9e1c47733f748fde0ce29c2abf74 Mon Sep 17 00:00:00 2001 From: Lars Date: Tue, 17 Nov 2026 21:10:22 +0100 Subject: [PATCH] =?UTF-8?q?F=C3=BCge=20duplizierte=20Abschnitte=20direkt?= =?UTF-8?q?=20hinter=20dem=20Original=20ein.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Filament hängt eine Kopie sonst ans Seitenende; auf langen Seiten war sie dort schwer zu finden. Gefunden in der Browserprüfung. Co-Authored-By: Claude Opus 5.5 --- app/Filament/Support/SectionEditor.php | 33 +++++++++++++++++++++++++- tests/Feature/CmsEditorTest.php | 13 ++++++++++ tests/Unit/PageSectionsTest.php | 9 +++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/app/Filament/Support/SectionEditor.php b/app/Filament/Support/SectionEditor.php index 340f91f..6a1d686 100644 --- a/app/Filament/Support/SectionEditor.php +++ b/app/Filament/Support/SectionEditor.php @@ -49,7 +49,14 @@ public static function repeater(string $name = 'sections'): Repeater ->collapsible() ->deletable() ->maxItems(PageSections::MAX_SECTIONS) - ->cloneAction(fn (Action $action): Action => $action->tooltip('Abschnitt duplizieren')) + ->cloneAction(fn (Action $action): Action => $action + ->tooltip('Abschnitt duplizieren') + ->action(function (array $arguments, Repeater $component): void { + // Die Kopie steht direkt hinter dem Original (Filament hängt sie sonst ans Seitenende). + $component->rawState(self::insertCopyAfter($component->getRawState() ?? [], (string) $arguments['item'], (string) $component->generateUuid())); + $component->callAfterStateUpdated(); + $component->shouldPartiallyRenderAfterActionsCalled() ? $component->partiallyRender() : null; + })) ->deleteAction(fn (Action $action): Action => $action ->tooltip('Abschnitt löschen') ->requiresConfirmation() @@ -413,6 +420,30 @@ public static function arrangeSlots(mixed $slots, string $layout): array return $arranged; } + /** + * Fügt die Kopie eines Abschnitts direkt hinter dem Original ein. Kennungen bleiben gleich; beim Speichern + * behält das Original seine Kennungen und die Kopie erhält neue. + * + * @param array $items + * @return array + */ + public static function insertCopyAfter(array $items, string $key, string $newKey): array + { + if (! array_key_exists($key, $items)) { + return $items; + } + + $result = []; + foreach ($items as $itemKey => $item) { + $result[$itemKey] = $item; + if ((string) $itemKey === $key) { + $result[$newKey] = $item; + } + } + + return $result; + } + public static function filledCount(mixed $slots): int { return count(array_filter(is_array($slots) ? $slots : [], fn ($slot): bool => self::slotHasWidget($slot))); diff --git a/tests/Feature/CmsEditorTest.php b/tests/Feature/CmsEditorTest.php index 83840dd..d224552 100644 --- a/tests/Feature/CmsEditorTest.php +++ b/tests/Feature/CmsEditorTest.php @@ -133,6 +133,19 @@ public function test_editor_reorders_and_duplicates_sections(): void $this->assertCount(5, array_unique(array_column($revision->sections, 'id')), 'Ein duplizierter Abschnitt erhält eine eigene Kennung.'); } + public function test_duplicate_action_places_the_copy_right_after_the_section(): void + { + $page = Page::query()->where('path', '/')->firstOrFail(); + $component = Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]); + $keys = array_keys($component->get('data.sections')); + + $component->callAction(TestAction::make('clone')->schemaComponent('sections')->arguments(['item' => $keys[1]])); + + $this->assertSame(['heading', 'prose', 'prose', 'cards', 'call_to_action'], $this->widgetTypes($component->get('data.sections'))); + $component->call('save')->assertHasNoFormErrors(); + $this->assertSame(['heading', 'prose', 'prose', 'cards', 'call_to_action'], array_column($page->fresh()->latestRevision->blocks, 'type')); + } + public function test_layout_change_keeps_widgets_and_rejects_layouts_with_too_few_slots(): void { $page = Page::query()->where('path', '/')->firstOrFail(); diff --git a/tests/Unit/PageSectionsTest.php b/tests/Unit/PageSectionsTest.php index 61cdb87..cf3bc7e 100644 --- a/tests/Unit/PageSectionsTest.php +++ b/tests/Unit/PageSectionsTest.php @@ -239,6 +239,15 @@ public function test_style_summary_names_background_and_divider(): void $this->assertSame('Dunkle Markenfläche · Breit · Trenner: Dezente Linie', SectionStyle::summary(['background' => 'dark', 'width' => 'wide', 'divider' => 'line'])); } + public function test_duplicated_section_is_inserted_right_after_its_original(): void + { + $items = ['a' => ['n' => 1], 'b' => ['n' => 2], 'c' => ['n' => 3]]; + + $this->assertSame(['a', 'b', 'kopie', 'c'], array_keys(SectionEditor::insertCopyAfter($items, 'b', 'kopie'))); + $this->assertSame(['n' => 2], SectionEditor::insertCopyAfter($items, 'b', 'kopie')['kopie']); + $this->assertSame($items, SectionEditor::insertCopyAfter($items, 'fehlt', 'kopie')); + } + public function test_layout_change_in_the_editor_never_drops_a_widget(): void { $slots = [