Skip to content

Commit

Permalink
feat(ui) alimenta o meta
Browse files Browse the repository at this point in the history
  • Loading branch information
irineujunior committed Dec 10, 2024
1 parent 21f340a commit 2850a4d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/Casts/AsSingleMediaLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function get(Model $model, string $key, mixed $value, array $attributes):
'uuid' => $media->uuid,
'url' => $media->getUrl(),
'path' => $media->file_name,
'meta' => $media->custom_properties,
];
})
: collect();
Expand Down
20 changes: 12 additions & 8 deletions src/Traits/WithMediaSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public function removeMedia(string $uuid, string $filesModelName, string $collec
->toString();

// Updates library
$this->form->{$collection} = $this->form->{$collection}->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)
->after('preview-file/')
->before('?expires')
->__toString();
$this->form->{$filesModelName} = collect($this->form->{$filesModelName})
->filter(static fn($file) => $file->getFilename() != $name)
->filter(static fn($file) => $file->getFilename() !== $name)
->all();
}

Expand Down Expand Up @@ -74,9 +74,10 @@ public function refreshMediaSources(string $filesModelName, string $collection):
public function syncMedia(
Model $model,
string $collection = 'collection',
string $files = '',
): void {
$files = $files ?: $collection . '_files';
$files = $collection . '_files';
$meta = $collection . '_meta';
$arrayMeta = $this->{$meta};
foreach ($this->{$files} as $index => $file) {
$name = str($model->name . '-' . date('YmdHisv'))
->slug()
Expand All @@ -87,12 +88,13 @@ public function syncMedia(
->__toString() ?: 'jpg';
$fileName = "{$name}.{$extension}";

$customProperties = [];
$customProperties = $arrayMeta[$index] ?? [];
$media = $model->addMedia($file)
->usingName($name)
->usingFileName($fileName)
// esse uuid não é necessário
->withCustomProperties(array_merge(['uuid' => Str::uuid()], $customProperties))
->withCustomProperties(array_merge([
//'uuid' => Str::uuid()
], $customProperties))
->withResponsiveImages()
->toMediaCollection($collection);

Expand All @@ -101,6 +103,7 @@ public function syncMedia(
'uuid' => $media->uuid,
'url' => $media->getUrl(),
'path' => $media->file_name,
'meta' => $media->custom_properties,
],
]);
}
Expand All @@ -112,11 +115,12 @@ public function syncMedia(
$model->clearMediaCollectionExcept($collection, $presentMedias);

$startAt = 1;
foreach ($this->{$collection} as $media) {
foreach ($this->{$collection} as $key => $media) {
$media = Media::query()
->where('uuid', $media['uuid'])
->first();
$media->order_column = $startAt++;
$media->custom_properties = $arrayMeta[$key];
$media->save();
}

Expand Down
45 changes: 25 additions & 20 deletions src/View/Components/Forms/Inputs/ImageLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function modelName(): ?string
return $this->name . '_files';
}

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

public function validationMessage(string $message): string
{
return str($message)->after('field');
Expand Down Expand Up @@ -170,15 +175,23 @@ public function render(): View|Closure|string
>
<!-- STANDARD LABEL -->
@if($label)
<x-form.label for="{{ $modelName() . $uuid }}" @class(['required' => $attributes->has('required')])>
<x-form.label
for="{{ $modelName() . $uuid }}"
@class([
'required' => $attributes->has('required')
])
>
{{ str($label)->lower()->ucfirst() }}
</x-form.label>
@endif
<!-- PREVIEW AREA -->
<div
:class="(processing || indeterminate) && 'opacity-50 pe-none'"
@class(["card mb-2", "d-none" => $this->form->{$collection}->count() == 0])
@class([
'card mb-2',
'd-none' => $this->form->{$collection}->count() === 0
])
>
<div
@if(!$isSingle)
Expand All @@ -199,7 +212,7 @@ public function render(): View|Closure|string
<a class="btn btn-icon link-muted" @click="removeMedia('{{ $image['uuid'] }}', '{{ $image['url'] }}')" title="{{ __('Remove') }}"><svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-x" width="24" height="24" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M18 6l-12 12" /><path d="M6 6l12 12" /></svg></a>
</div>
<div class="d-block mb-2">
<a class="btn btn-icon link-muted" @click="crop('image-{{ $modelName().'.'.$key }}-{{ $uuid }}')" title="{{ __('Crop') }}"><svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-scissors" width="24" height="24" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 7m-3 0a3 3 0 1 0 6 0a3 3 0 1 0 -6 0" /><path d="M6 17m-3 0a3 3 0 1 0 6 0a3 3 0 1 0 -6 0" /><path d="M8.6 8.6l10.4 10.4" /><path d="M8.6 15.4l10.4 -10.4" /></svg></a>
<a class="btn btn-icon link-muted" @click="crop('image-{{ $modelName() . '.' . $key }}-{{ $uuid }}')" title="{{ __('Crop') }}"><svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-scissors" width="24" height="24" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M6 7m-3 0a3 3 0 1 0 6 0a3 3 0 1 0 -6 0" /><path d="M6 17m-3 0a3 3 0 1 0 6 0a3 3 0 1 0 -6 0" /><path d="M8.6 8.6l10.4 10.4" /><path d="M8.6 15.4l10.4 -10.4" /></svg></a>
</div>
</div>
<div class="col-auto">
Expand All @@ -208,41 +221,33 @@ class="rounded ratio-1x1"
alt="{{ $image['url'] }}"
width="160"
@click="document.getElementById('file-{{ $uuid}}-{{ $key }}').click()"
id="image-{{ $modelName().'.'.$key }}-{{ $uuid }}">
id="image-{{ $modelName() . '.' . $key }}-{{ $uuid }}">
<!-- HIDDEN FILE INPUT -->
<input
type="file"
id="file-{{ $uuid}}-{{ $key }}"
wire:model="{{ $modelName().'.'.$key }}"
wire:model="{{ $modelName() . '.' . $key }}"
accept="{{ $attributes->get('accept') ?? $accept }}"
class="d-none"
@change="progress = 1"
/>
</div>
@if($slot->isEmpty())
<div class="col">
{{--
<div class="mb-3">
<x-form.input label="Nome"/>
<x-form.input wire:model="{{ $metaName() . '.' . $key . '.title' }}" id="title-{{ $image['uuid'] }}" placeholder="Nome"/>
</div>
<div class="-mb-3">
<x-form.input label="Descrição"/>
<x-form.input wire:model="{{ $metaName() . '.' . $key . '.alt' }}" id="alt-{{ $image['uuid'] }}" placeholder="Descrição"/>
</div>
--}}
</div>
<!-- TODO colocaremos aqui os inputs de meta -->
<!--div class="col">
Górą ty
<div class="text-secondary">
GOLEC UORKIESTRA,
Gromee,
Bedoes
</div>
</div-->
@else
{!! str($slot)->replace('{key}', $key) !!}
@endif
</div>
<!-- VALIDATION -->
@error($modelName().'.'.$key)
@error($modelName() . '.' . $key)
<div class="invalid-feedback d-block">{{ $validationMessage($message) }}</div>
@enderror
</div>
Expand Down

0 comments on commit 2850a4d

Please sign in to comment.