Skip to content

Commit

Permalink
Insert files after upload
Browse files Browse the repository at this point in the history
  • Loading branch information
howdu committed Jan 31, 2024
1 parent 5b6cf2c commit 94860f5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
3 changes: 2 additions & 1 deletion resources/views/components/modals/curator-panel.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ class="w-5 h-5"
<div class="flex items-center justify-start mt-auto gap-3 py-3 px-4 border-t border-gray-300 bg-gray-200 dark:border-gray-800 dark:bg-black/10">
@if (count($selected) !== 1)
<div>
{{ $this->addFilesAction }}
{{ $this->addFilesAction }}
{{ $this->addInsertFilesAction }}
</div>
@endif
@if (count($selected) === 1)
Expand Down
70 changes: 48 additions & 22 deletions src/Components/Modals/CuratorPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function setMediaForm(): void
}
}

public function addFilesAction(): Action
public function addFilesAction(bool $insertAfter = false): Action
{
return Action::make('addFiles')
->button()
Expand All @@ -319,27 +319,8 @@ public function addFilesAction(): Action
->disabled(function (): bool {
return count($this->form->getRawState()['files_to_add'] ?? []) === 0;
})
->action(function (): void {
$media = [];
$formData = $this->form->getState();

foreach ($formData['files_to_add'] as $item) {
// Fix malformed utf-8 characters
if (!empty($item['exif'])) {
array_walk_recursive($item['exif'], function (&$entry) {
if (!mb_detect_encoding($entry, 'utf-8', true)) {
$entry = mb_convert_encoding($entry, 'utf-8');
}
});
}

$item['title'] = pathinfo($formData['originalFilenames'][$item['path']] ?? null, PATHINFO_FILENAME);

$media[] = tap(
App::make(Media::class)->create($item),
fn(Media $media) => $media->getPrettyName(),
)->toArray();
}
->action(function () use($insertAfter): void {
$media = self::createMediaFiles($this->form->getState());

$this->form->fill();

Expand All @@ -348,12 +329,33 @@ public function addFilesAction(): Action
...$this->files,
];

if ($insertAfter) {
$this->dispatch(
'insert-content',
type: 'media',
statePath: $this->statePath,
media: $media
);

$this->dispatch('close-modal', id: $this->modalId ?? 'curator-panel');

return;
}

foreach ($media as $item) {
$this->addToSelection($item['id']);
}
});
}

public function addInsertFilesAction(): Action
{
return $this->addFilesAction(true)
->name('addInsertFiles')
->color('success')
->label(__('curator::views.panel.use_selected_image'));
}

public function cancelEditAction(): Action
{
return Action::make('cancelEdit')
Expand Down Expand Up @@ -506,4 +508,28 @@ public function render(): View
{
return view('curator::components.modals.curator-panel');
}

protected function createMediaFiles(array $formData) : array {
$media = [];

foreach ($formData['files_to_add'] as $item) {
// Fix malformed utf-8 characters
if (!empty($item['exif'])) {
array_walk_recursive($item['exif'], function (&$entry) {
if (!mb_detect_encoding($entry, 'utf-8', true)) {
$entry = mb_convert_encoding($entry, 'utf-8');
}
});
}

$item['title'] = pathinfo($formData['originalFilenames'][$item['path']] ?? null, PATHINFO_FILENAME);

$media[] = tap(
App::make(Media::class)->create($item),
fn(Media $media) => $media->getPrettyName(),
)->toArray();
}

return $media;
}
}

0 comments on commit 94860f5

Please sign in to comment.