Skip to content

Commit

Permalink
feat(media-library) convenciona o model name a partir do collection
Browse files Browse the repository at this point in the history
  • Loading branch information
irineujunior committed Dec 9, 2024
1 parent ea3d232 commit 21f340a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
33 changes: 17 additions & 16 deletions src/Traits/WithMediaSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -29,29 +29,29 @@ 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('.')
->__toString();

// 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;
}
Expand All @@ -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 . '.*');
Expand All @@ -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()
Expand All @@ -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(),
Expand All @@ -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();
Expand Down
22 changes: 16 additions & 6 deletions src/View/Components/Forms/Inputs/ImageLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\View\Component;

class ImageLibrary extends Component
Expand All @@ -17,14 +16,18 @@ 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,
public ?bool $hideProgress = false,
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')
Expand All @@ -34,7 +37,7 @@ public function __construct(

public function modelName(): ?string
{
return $this->name;
return $this->name . '_files';
}

public function validationMessage(string $message): string
Expand Down Expand Up @@ -178,9 +181,15 @@ public function render(): View|Closure|string
@class(["card mb-2", "d-none" => $this->form->{$collection}->count() == 0])
>
<div
x-data="{ sortable: null }"
x-init="sortable = new Sortable($el, { animation: 150, ghostClass: 'bg-gray-300', onEnd: (ev) => refreshMediaOrder(sortable.toArray()) })"
class="list-group card-list-group cursor-move"
@if(!$isSingle)
x-data="{ sortable: null }"
x-init="sortable = new Sortable($el, { animation: 150, ghostClass: 'bg-gray-300', onEnd: (ev) => refreshMediaOrder(sortable.toArray()) })"
@endif
@class([
'list-group',
'card-list-group',
'cursor-move' => !$isSingle,
])
>
@foreach($this->form->{$collection} as $key => $image)
<div class="list-group-item" data-id="{{ $image['uuid'] }}">
Expand Down Expand Up @@ -286,6 +295,7 @@ class="d-none"
@endif
<!-- HINT -->
{{-- TODO: usar esse fluxo para alimentar o hint quando ele estiver vazio $this->form->rules()['avatar_files.*'][2]->__toString()) --}}
<x-form.hint message="{{ $hint }}"/>
</div>
HTML;
Expand Down

0 comments on commit 21f340a

Please sign in to comment.