From 21f340a4085100565d596c2a8229ef11b752fad6 Mon Sep 17 00:00:00 2001 From: Irineu Junior Date: Mon, 9 Dec 2024 17:38:20 -0300 Subject: [PATCH] feat(media-library) convenciona o model name a partir do collection --- src/Traits/WithMediaSync.php | 33 ++++++++++--------- .../Components/Forms/Inputs/ImageLibrary.php | 22 +++++++++---- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/Traits/WithMediaSync.php b/src/Traits/WithMediaSync.php index 5b95495..cdb37b2 100644 --- a/src/Traits/WithMediaSync.php +++ b/src/Traits/WithMediaSync.php @@ -9,14 +9,14 @@ trait WithMediaSync { // Remove media - public function removeMedia(string $uuid, string $filesModelName, string $library, string $url): void + public function removeMedia(string $uuid, string $filesModelName, string $collection, string $url): void { $filesModelName = str($filesModelName) ->afterLast('.') ->toString(); // Updates library - $this->form->{$library} = $this->form->{$library}->filter(static fn($image) => $image['uuid'] != $uuid); + $this->form->{$collection} = $this->form->{$collection}->filter(static fn($image) => $image['uuid'] != $uuid); // Remove file $name = str($url) @@ -29,15 +29,15 @@ public function removeMedia(string $uuid, string $filesModelName, string $librar } // Set order - public function refreshMediaOrder(array $order, string $library): void + public function refreshMediaOrder(array $order, string $collection): void { - $this->form->{$library} = $this->form->{$library}->sortBy(static function ($item) use ($order) { + $this->form->{$collection} = $this->form->{$collection}->sortBy(static function ($item) use ($order) { return array_search($item['uuid'], $order); }); } // Bind temporary files with respective previews and replace existing ones, if necessary - public function refreshMediaSources(string $filesModelName, string $library): void + public function refreshMediaSources(string $filesModelName, string $collection): void { $filesModelName = str($filesModelName) ->afterLast('.') @@ -45,13 +45,13 @@ public function refreshMediaSources(string $filesModelName, string $library): vo // New files area foreach ($this->form->{$filesModelName}['*'] ?? [] as $key => $file) { - $this->form->{$library} = $this->form->{$library}->add([ + $this->form->{$collection} = $this->form->{$collection}->add([ 'uuid' => Str::uuid() ->__toString(), 'url' => $file->temporaryUrl(), ]); - $key = $this->form->{$library}->keys() + $key = $this->form->{$collection}->keys() ->last(); $this->form->{$filesModelName}[$key] = $file; } @@ -61,10 +61,10 @@ public function refreshMediaSources(string $filesModelName, string $library): vo //Replace existing files foreach ($this->form->{$filesModelName} as $key => $file) { - $media = $this->form->{$library}->get($key); + $media = $this->form->{$collection}->get($key); $media['url'] = $file->temporaryUrl(); - $this->form->{$library} = $this->form->{$library}->replace([$key => $media]); + $this->form->{$collection} = $this->form->{$collection}->replace([$key => $media]); } $this->validateOnly($filesModelName . '.*'); @@ -73,9 +73,10 @@ public function refreshMediaSources(string $filesModelName, string $library): vo // Storage files into permanent area and updates the model with fresh sources public function syncMedia( Model $model, - string $library = 'library', - string $files = 'files', + string $collection = 'collection', + string $files = '', ): void { + $files = $files ?: $collection . '_files'; foreach ($this->{$files} as $index => $file) { $name = str($model->name . '-' . date('YmdHisv')) ->slug() @@ -93,9 +94,9 @@ public function syncMedia( // esse uuid não é necessário ->withCustomProperties(array_merge(['uuid' => Str::uuid()], $customProperties)) ->withResponsiveImages() - ->toMediaCollection($library); + ->toMediaCollection($collection); - $this->{$library} = $this->{$library}->replace([ + $this->{$collection} = $this->{$collection}->replace([ $index => [ 'uuid' => $media->uuid, 'url' => $media->getUrl(), @@ -105,13 +106,13 @@ public function syncMedia( } $presentMedias = $model->media() - ->whereIn('uuid', $this->{$library}->pluck('uuid') + ->whereIn('uuid', $this->{$collection}->pluck('uuid') ->toArray()) ->get(); - $model->clearMediaCollectionExcept($library, $presentMedias); + $model->clearMediaCollectionExcept($collection, $presentMedias); $startAt = 1; - foreach ($this->{$library} as $media) { + foreach ($this->{$collection} as $media) { $media = Media::query() ->where('uuid', $media['uuid']) ->first(); diff --git a/src/View/Components/Forms/Inputs/ImageLibrary.php b/src/View/Components/Forms/Inputs/ImageLibrary.php index f7b703b..74c8f5f 100644 --- a/src/View/Components/Forms/Inputs/ImageLibrary.php +++ b/src/View/Components/Forms/Inputs/ImageLibrary.php @@ -4,7 +4,6 @@ use Closure; use Illuminate\Contracts\View\View; -use Illuminate\Support\Collection; use Illuminate\View\Component; class ImageLibrary extends Component @@ -17,7 +16,7 @@ class ImageLibrary extends Component public function __construct( public string $name = '', - public string $collection = '', + public ?string $collection = '', public ?string $label = null, public ?string $hint = null, public ?bool $hideErrors = false, @@ -25,6 +24,10 @@ public function __construct( public ?string $addFilesText = 'Add images', public ?array $cropConfig = [], ) { + $this->collection = $this->collection ?: str($this->name) + ->after('.') + ->toString(); + /* source: https://mary-ui.com/docs/components/image-library */ $this->uuid = '-mary-' . str(serialize($this)) ->pipe('md5') @@ -34,7 +37,7 @@ public function __construct( public function modelName(): ?string { - return $this->name; + return $this->name . '_files'; } public function validationMessage(string $message): string @@ -178,9 +181,15 @@ public function render(): View|Closure|string @class(["card mb-2", "d-none" => $this->form->{$collection}->count() == 0]) >
!$isSingle, + ]) > @foreach($this->form->{$collection} as $key => $image)
@@ -286,6 +295,7 @@ class="d-none" @endif + {{-- TODO: usar esse fluxo para alimentar o hint quando ele estiver vazio $this->form->rules()['avatar_files.*'][2]->__toString()) --}}
HTML;