Skip to content

Commit

Permalink
Small tweals
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryMoustache committed Sep 26, 2023
1 parent 38fd731 commit 2f1a088
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
13 changes: 8 additions & 5 deletions src/Collections/Formats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}
}
5 changes: 5 additions & 0 deletions src/Filament/AttachmentInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 4 additions & 9 deletions src/Livewire/FormatterModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Mixins/UploadedFileMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2f1a088

Please sign in to comment.