@@ -44,7 +44,7 @@ public function index($themeCode)
4444 'config ' => [
4545 'baseUrl ' => parse_url (route ('visual.admin.editor ' , ['theme ' => $ themeCode ]), PHP_URL_PATH ),
4646 'imagesBaseUrl ' => Storage::disk (config ('bagisto_visual.images_storage ' ))->url ('' ),
47- 'storefrontUrl ' => url ('/ ' ). '? ' . http_build_query (['_designMode ' => $ themeCode ]),
47+ 'storefrontUrl ' => url ('/ ' ) . '? ' . http_build_query (['_designMode ' => $ themeCode ]),
4848 'channels ' => $ this ->getChannels (),
4949 'defaultChannel ' => core ()->getDefaultChannelCode (),
5050 'blockSchemas ' => $ this ->loadBlocks (),
@@ -87,11 +87,17 @@ public function persistUpdates(Request $request)
8787 'updates.regions ' => ['present ' , 'array ' ],
8888 ]);
8989
90- $ this ->persistEditorUpdates ->handle ($ validated );
90+ $ result = $ this ->persistEditorUpdates ->handle ($ validated );
91+ $ loadedBlocks = $ result ['loadedBlocks ' ] ?? [];
92+
93+ $ changedBlockIds = $ this ->extractChangedBlockIds (
94+ $ validated ['updates ' ],
95+ $ loadedBlocks
96+ );
9197
9298 $ url = $ request ->input ('template.url ' );
9399
94- return $ this ->renderPreview ->execute ($ url );
100+ return $ this ->renderPreview ->execute ($ url, $ changedBlockIds );
95101 }
96102
97103 public function persistThemeSettings (Request $ request )
@@ -134,7 +140,7 @@ public function uploadImages(Request $request)
134140 return $ images ->map (function ($ image ) {
135141 $ originalName = pathinfo ($ image ->getClientOriginalName (), PATHINFO_FILENAME );
136142 $ extension = $ image ->guessExtension ();
137- $ storedName = bin2hex ($ originalName ). '_ ' . uniqid (). '. ' . $ extension ;
143+ $ storedName = bin2hex ($ originalName ) . '_ ' . uniqid () . '. ' . $ extension ;
138144
139145 $ path = $ image ->storeAs (
140146 config ('bagisto_visual.images_directory ' ),
@@ -209,15 +215,15 @@ public function icons(Request $request, Factory $factory, IconsManifest $iconsMa
209215
210216 $ icons ->push ([
211217 'name ' => $ name ,
212- 'id ' => $ set ['prefix ' ]. '- ' . $ name ,
218+ 'id ' => $ set ['prefix ' ] . '- ' . $ name ,
213219 'svg ' => File::get ($ file ->getRealPath ()),
214220 ]);
215221 }
216222 }
217223
218224 return [
219225 'currentSet ' => $ selectedSet ,
220- 'sets ' => collect ($ sets )->map (fn ($ set , $ key ) => ['id ' => $ key , 'prefix ' => $ set ['prefix ' ], 'name ' => Str::headline ($ key )])->values (),
226+ 'sets ' => collect ($ sets )->map (fn ($ set , $ key ) => ['id ' => $ key , 'prefix ' => $ set ['prefix ' ], 'name ' => Str::headline ($ key )])->values (),
221227 'icons ' => $ icons ->values (),
222228 ];
223229 }
@@ -260,7 +266,7 @@ protected function loadBlocks()
260266 'category ' => $ blockSchema ->category ,
261267 'description ' => $ blockSchema ->description ,
262268 'previewImageUrl ' => asset ($ blockSchema ->previewImageUrl ),
263- 'isSection ' => collect ([SimpleSection::class, BladeSection::class, LivewireSection::class])->some (fn ($ class ) => is_subclass_of ($ blockSchema ->class , $ class )),
269+ 'isSection ' => collect ([SimpleSection::class, BladeSection::class, LivewireSection::class])->some (fn ($ class ) => is_subclass_of ($ blockSchema ->class , $ class )),
264270 'enabledOn ' => $ blockSchema ->enabledOn ?? [],
265271 'disabledOn ' => $ blockSchema ->disabledOn ?? [],
266272 ],
@@ -292,7 +298,7 @@ protected function loadTheme($themeCode)
292298 protected function loadTemplates ()
293299 {
294300 return collect (app (\BagistoPlus \Visual \ThemeEditor::class)->getTemplates ())
295- ->map (fn ($ template ) => [
301+ ->map (fn ($ template ) => [
296302 'template ' => $ template ->template ,
297303 'label ' => $ template ->label ,
298304 'icon ' => $ template ->icon ,
@@ -329,7 +335,7 @@ protected function translateSettingsSchema(array $settingsSchema): array
329335
330336 protected function getChannels ()
331337 {
332- return core ()->getAllChannels ()->map (fn ($ channel ) => [
338+ return core ()->getAllChannels ()->map (fn ($ channel ) => [
333339 'code ' => $ channel ->code ,
334340 'name ' => $ channel ->name ,
335341 'locales ' => $ channel ->locales ,
@@ -340,7 +346,7 @@ protected function getChannels()
340346 protected function getChannelCodes (): array
341347 {
342348 return $ this ->getChannels ()
343- ->map (fn ($ channel ) => $ channel ['code ' ])
349+ ->map (fn ($ channel ) => $ channel ['code ' ])
344350 ->toArray ();
345351 }
346352
@@ -352,14 +358,48 @@ protected function getLocaleCodes(string $channel): array
352358 return [];
353359 }
354360
355- return $ channel ['locales ' ]->map (fn ($ locale ) => $ locale ['code ' ])->toArray ();
361+ return $ channel ['locales ' ]->map (fn ($ locale ) => $ locale ['code ' ])->toArray ();
356362 }
357363
358364 protected function getVisualThemes (): array
359365 {
360366 return collect (config ('themes.shop ' , []))
361- ->filter (fn ($ config ) => $ config ['visual_theme ' ] ?? false )
362- ->map (fn ($ config ) => $ config ['code ' ])
367+ ->filter (fn ($ config ) => $ config ['visual_theme ' ] ?? false )
368+ ->map (fn ($ config ) => $ config ['code ' ])
363369 ->toArray ();
364370 }
371+
372+ /**
373+ * Extract changed block IDs from updates and include all parent blocks.
374+ */
375+ protected function extractChangedBlockIds (array $ updates , array $ loadedBlocks ): array
376+ {
377+ $ changes = $ updates ['changes ' ] ?? [];
378+
379+ $ changedIds = array_merge ($ changes ['added ' ] ?? [], $ changes ['updated ' ] ?? []);
380+
381+ return $ this ->includeParentBlocks ($ changedIds , $ loadedBlocks );
382+ }
383+
384+ /**
385+ * Include all parent blocks for the given block IDs.
386+ */
387+ protected function includeParentBlocks (array $ blockIds , array $ loadedBlocks ): array
388+ {
389+ $ result = [];
390+
391+ foreach ($ blockIds as $ id ) {
392+ $ result [] = $ id ;
393+
394+ // Walk up the parent chain using parentId
395+ $ currentId = $ id ;
396+ while (isset ($ loadedBlocks [$ currentId ]['parentId ' ]) && $ loadedBlocks [$ currentId ]['parentId ' ] !== null ) {
397+ $ parentId = $ loadedBlocks [$ currentId ]['parentId ' ];
398+ $ result [] = $ parentId ;
399+ $ currentId = $ parentId ;
400+ }
401+ }
402+
403+ return array_unique ($ result );
404+ }
365405}
0 commit comments