From 2f1a088c7e39a26100cb8c161098fbca1d64b234 Mon Sep 17 00:00:00 2001 From: Sander Van Damme Date: Tue, 26 Sep 2023 08:23:21 +0000 Subject: [PATCH] Small tweals --- src/Collections/Formats.php | 13 ++++++++----- src/Filament/AttachmentInput.php | 5 +++++ src/Livewire/FormatterModal.php | 13 ++++--------- src/Mixins/UploadedFileMixin.php | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Collections/Formats.php b/src/Collections/Formats.php index ca66327..369deb4 100644 --- a/src/Collections/Formats.php +++ b/src/Collections/Formats.php @@ -49,6 +49,14 @@ public function mapToKebab(): Collection ]); } + public function mapToClasses(): Collection + { + return $this->flatten() + ->map(fn (Format $format) => get_class($format)) + ->unique() + ->values(); + } + public function dispatchGeneration(Attachment $attachment, bool $force = false): void { $this->flatten()->each(fn (Format $format) => dispatch(new GenerateAttachmentFormat( @@ -57,9 +65,4 @@ public function dispatchGeneration(Attachment $attachment, bool $force = false): force: $force, ))); } - - public function findByKey(string $key): ?Format - { - return $this->flatten(1)->firstWhere(fn ($format) => get_class($format) === $key); - } } diff --git a/src/Filament/AttachmentInput.php b/src/Filament/AttachmentInput.php index 47cd2c0..a14c01c 100644 --- a/src/Filament/AttachmentInput.php +++ b/src/Filament/AttachmentInput.php @@ -171,6 +171,11 @@ public function sortField(string|Closure $sortField): static return $this; } + public function disableFormatter(): static + { + return $this->allowedFormats([]); + } + public function getSortField(): ?string { return $this->evaluate($this->sortField); diff --git a/src/Livewire/FormatterModal.php b/src/Livewire/FormatterModal.php index 342312b..8a5bb0f 100644 --- a/src/Livewire/FormatterModal.php +++ b/src/Livewire/FormatterModal.php @@ -6,6 +6,7 @@ use Codedor\MediaLibrary\Formats\Format; use Codedor\MediaLibrary\Models\Attachment; use Filament\Notifications\Notification; +use Illuminate\Support\Collection; use Livewire\Attributes\On; use Livewire\Component; @@ -22,14 +23,8 @@ public function setAttachment(string $uuid, array $formats = null) { $this->attachment = Attachment::find($uuid); - $formats = Formats::mapToKebab() - ->when( - ! is_null($formats), - fn ($allFormats) => $allFormats->filter(fn (Format $format) => in_array( - $format->key(), - $formats, - )) - ) + $formats = Collection::wrap($formats ?? Formats::mapToClasses()) + ->map(fn ($format) => $format::make()) ->filter(fn (Format $format) => $format->shownInFormatter()) ->map->toArray(); @@ -61,7 +56,7 @@ public function render() public function saveCrop($event) { - $format = Formats::findByKey($event['format']['key']); + $format = $event['format']['key']::make(); $filename = $format->filename($this->attachment); // Save the crop in the storage diff --git a/src/Mixins/UploadedFileMixin.php b/src/Mixins/UploadedFileMixin.php index 1d2cbc1..e6fc3db 100644 --- a/src/Mixins/UploadedFileMixin.php +++ b/src/Mixins/UploadedFileMixin.php @@ -61,7 +61,7 @@ public function save() // Create the thumbnail now, so we don't have an empty preview in the next response GenerateAttachmentFormat::dispatchAfterResponse( $attachment, - Formats::findByKey(Thumbnail::class), + Thumbnail::make(), ); return $attachment;