1616use RedberryProducts \PageBuilderPlugin \Components \Forms \Actions \CreatePageBuilderBlockAction ;
1717use RedberryProducts \PageBuilderPlugin \Components \Forms \Actions \DeletePageBuilderBlockAction ;
1818use RedberryProducts \PageBuilderPlugin \Components \Forms \Actions \EditPageBuilderBlockAction ;
19+ use RedberryProducts \PageBuilderPlugin \Components \Forms \Actions \ReoraderPageBuilderBlockAction ;
1920use RedberryProducts \PageBuilderPlugin \Components \Forms \Actions \SelectBlockAction ;
2021
2122// TODO: make this reorder-able
2223class PageBuilder extends Field
2324{
2425 public ?string $ relationship = null ;
2526
27+ public bool |Closure $ reorderable = false ;
28+
2629 protected ?Closure $ renderDeleteActionButtonUsing = null ;
2730
2831 protected ?Closure $ modifyDeleteActionUsing = null ;
@@ -35,6 +38,10 @@ class PageBuilder extends Field
3538
3639 protected ?Closure $ renderEditActionButtonUsing = null ;
3740
41+ protected ?Closure $ modifyReorderActionUsing = null ;
42+
43+ protected ?Closure $ renderReorderActionButtonUsing = null ;
44+
3845 public array | Closure $ blocks = [];
3946
4047 public string $ view = 'page-builder-plugin::forms.page-builder ' ;
@@ -52,9 +59,25 @@ protected function setUp(): void
5259 fn (self $ component ): Action => $ component ->getEditAction (),
5360 fn (self $ component ): Action => $ component ->getSelectBlockAction (),
5461 fn (self $ component ): Action => $ component ->getCreateAction (),
62+ fn (self $ component ): Action => $ component ->getReorderAction (),
5563 ]);
5664 }
5765
66+ public function getReorderAction (): Action
67+ {
68+ $ action = ReoraderPageBuilderBlockAction::make ($ this ->getReorderActionName ())
69+ ->hidden (!$ this ->getReorderable ())
70+ ->disabled ($ this ->isDisabled ());
71+
72+ if ($ this ->modifyReorderActionUsing ) {
73+ $ action = $ this ->evaluate ($ this ->modifyReorderActionUsing , [
74+ 'action ' => $ action ,
75+ ]);
76+ }
77+
78+ return $ action ;
79+ }
80+
5881 public function getCreateAction (): Action
5982 {
6083 $ action = CreatePageBuilderBlockAction::make ($ this ->getCreateActionName ())
@@ -167,6 +190,31 @@ public function renderEditActionButton(string $item, $index)
167190 return view ('filament::components.button.index ' , $ attributes );
168191 }
169192
193+ public function renderReorderActionButton (string $ item , $ index )
194+ {
195+ $ reorderAction = $ this ->getReorderAction ();
196+
197+ $ attributes = [
198+ 'icon ' => 'heroicon-o-arrows-up-down ' ,
199+ 'disabled ' => $ reorderAction ->isDisabled (),
200+ 'color ' => 'gray ' ,
201+ 'attributes ' => collect ([
202+ 'x-sortable-handle ' => 'x-sortable-handle ' ,
203+ 'x-on:click.stop ' => 'x-on:click.stop ' ,
204+ ])
205+ ];
206+
207+ if ($ this ->renderReorderActionButtonUsing ) {
208+ return $ this ->evaluate ($ this ->renderReorderActionButtonUsing , [
209+ 'action ' => $ reorderAction ,
210+ 'item ' => $ item ,
211+ 'index ' => $ index ,
212+ ]);
213+ }
214+
215+ return view ('filament::components.icon-button ' , $ attributes );
216+ }
217+
170218 public function deleteActionButton (Closure $ renderDeleteActionButtonUsing )
171219 {
172220 $ this ->renderDeleteActionButtonUsing = $ renderDeleteActionButtonUsing ;
@@ -181,6 +229,13 @@ public function editActionButton(Closure $renderEditActionButtonUsing)
181229 return $ this ;
182230 }
183231
232+ public function reorderActionButton (Closure $ renderReorderActionButtonUsing )
233+ {
234+ $ this ->renderReorderActionButtonUsing = $ renderReorderActionButtonUsing ;
235+
236+ return $ this ;
237+ }
238+
184239 public function getDeleteActionName (): string
185240 {
186241 return 'delete ' ;
@@ -196,6 +251,11 @@ public function getCreateActionName(): string
196251 return 'create ' ;
197252 }
198253
254+ public function getReorderActionName (): string
255+ {
256+ return 'reorder ' ;
257+ }
258+
199259 public function getSelectBlockActionName (): string
200260 {
201261 return 'select-block ' ;
@@ -225,6 +285,14 @@ public function createAction(
225285 return $ this ;
226286 }
227287
288+ public function reorderAction (
289+ Closure $ modifyReorderActionUsing ,
290+ ) {
291+ $ this ->modifyReorderActionUsing = $ modifyReorderActionUsing ;
292+
293+ return $ this ;
294+ }
295+
228296 public function blocks (
229297 array | Closure $ blocks ,
230298 ) {
@@ -233,6 +301,19 @@ public function blocks(
233301 return $ this ;
234302 }
235303
304+ public function reorderable (
305+ bool | Closure $ reorderable = true ,
306+ ) {
307+ $ this ->reorderable = $ reorderable ;
308+
309+ return $ this ;
310+ }
311+
312+ public function getReorderable (): bool
313+ {
314+ return (bool ) $ this ->evaluate ($ this ->reorderable );
315+ }
316+
236317 #[Computed(true )]
237318 public function getBlocks (): array
238319 {
@@ -263,6 +344,7 @@ public function relationship(
263344 $ this ->loadStateFromRelationshipsUsing (function ($ record , PageBuilder $ component ) {
264345 /** @var Collection */
265346 $ blocks = $ this ->getConstrainAppliedQuery ($ record )
347+ ->orderBy ('order ' )
266348 ->get ();
267349
268350 $ component ->state ($ blocks ->toArray ());
@@ -281,11 +363,10 @@ public function relationship(
281363
282364 $ record ->{$ this ->relationship }()->upsert (array_map (function ($ item ) {
283365 return [
284- 'id ' => $ item ['id ' ] ?? null ,
285- 'block_type ' => $ item ['block_type ' ],
366+ ...$ item ,
286367 'data ' => json_encode ($ item ['data ' ] ?? []),
287368 ];
288- }, $ state ), uniqueBy: ['id ' ], update: ['data ' ]);
369+ }, $ state ), uniqueBy: ['id ' ], update: ['data ' , ' order ' ]);
289370
290371 DB ::commit ();
291372
@@ -297,7 +378,7 @@ public function relationship(
297378 ->danger ()
298379 ->send ();
299380
300- throw new Halt ;
381+ throw new Halt () ;
301382 }
302383
303384 });
0 commit comments