Skip to content

Commit 6bb1d9d

Browse files
committed
fix(laravel): remove entire block subtree when deleting a block
1 parent 743ccdd commit 6bb1d9d

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

packages/laravel/src/Support/HandleUpdates.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,18 @@ private function applyUpdates(array $data, UpdateRequest $updateRequest, ?array
4545

4646
foreach ($blocksToRemove as $blockId) {
4747
if (isset($data['blocks'][$blockId])) {
48+
// Get all descendants to remove entire subtree
49+
$descendants = $this->getAllDescendants($blockId, $data['blocks']);
50+
51+
// Remove the block itself
4852
unset($data['blocks'][$blockId]);
53+
54+
// Remove all descendants
55+
foreach ($descendants as $descendantId) {
56+
if (isset($data['blocks'][$descendantId])) {
57+
unset($data['blocks'][$descendantId]);
58+
}
59+
}
4960
}
5061
}
5162

@@ -185,4 +196,26 @@ private function filterChangesForRegions(UpdateRequest $updateRequest, array $re
185196
'moved' => array_filter($updateRequest->getMovedBlocks(), $inRegion, ARRAY_FILTER_USE_KEY),
186197
];
187198
}
199+
200+
/**
201+
* Get all descendant block IDs for a given block (recursive).
202+
*/
203+
private function getAllDescendants(string $blockId, array $blocks): array
204+
{
205+
$descendants = [];
206+
207+
if (! isset($blocks[$blockId]) || empty($blocks[$blockId]['children'])) {
208+
return $descendants;
209+
}
210+
211+
foreach ($blocks[$blockId]['children'] as $childId) {
212+
$descendants[] = $childId;
213+
$descendants = array_merge(
214+
$descendants,
215+
$this->getAllDescendants($childId, $blocks)
216+
);
217+
}
218+
219+
return $descendants;
220+
}
188221
}

0 commit comments

Comments
 (0)