Skip to content

Commit

Permalink
wip cleanup recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
tanthammar committed Jan 30, 2025
1 parent 0db8c3f commit e6bf14f
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/TiptapEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use FilamentTiptapEditor\Concerns\HasCustomActions;
use FilamentTiptapEditor\Concerns\InteractsWithMedia;
use FilamentTiptapEditor\Concerns\InteractsWithMenus;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Js;
use Illuminate\Support\Str;
use JsonException;
Expand Down Expand Up @@ -92,8 +93,30 @@ protected function setUp(): void
$component->state($state);
});

$this->afterStateUpdated(function (TiptapEditor $component, Component $livewire): void {
$this->afterStateUpdated(function (TiptapEditor $component, Component $livewire, mixed $state, mixed $old): void {

$livewire->validateOnly($component->getStatePath());

$findImageSrcs = static function ($content) use (&$findImageSrcs) {
return collect($content)->reduce(function ($carry, $node) use ($findImageSrcs) {
if ($node['type'] === 'image') {
$carry[] = $node['attrs']['src'];
}

if (isset($node['content'])) {
$carry = array_merge($carry, $findImageSrcs($node['content']));
}

return $carry;
}, []);
};

$oldImages = collect($findImageSrcs($old['content'] ?? []));
$newImages = collect($findImageSrcs($state['content'] ?? []));

$imagesToDelete = $oldImages->diff($newImages);

$imagesToDelete->each(fn ($imageUrl) => Storage::disk($component->getDisk())->delete($imageUrl));
});

$this->dehydrateStateUsing(function (TiptapEditor $component, string | array | null $state): string | array | null {
Expand Down

0 comments on commit e6bf14f

Please sign in to comment.