From 94860f5d85f9378845282742909edb9b188b3411 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 31 Jan 2024 23:04:07 +0000 Subject: [PATCH] Insert files after upload --- .../components/modals/curator-panel.blade.php | 3 +- src/Components/Modals/CuratorPanel.php | 70 +++++++++++++------ 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/resources/views/components/modals/curator-panel.blade.php b/resources/views/components/modals/curator-panel.blade.php index 2dfb52f8..be29a941 100644 --- a/resources/views/components/modals/curator-panel.blade.php +++ b/resources/views/components/modals/curator-panel.blade.php @@ -187,7 +187,8 @@ class="w-5 h-5"
@if (count($selected) !== 1)
- {{ $this->addFilesAction }} + {{ $this->addFilesAction }} + {{ $this->addInsertFilesAction }}
@endif @if (count($selected) === 1) diff --git a/src/Components/Modals/CuratorPanel.php b/src/Components/Modals/CuratorPanel.php index 582ba557..2388c74d 100644 --- a/src/Components/Modals/CuratorPanel.php +++ b/src/Components/Modals/CuratorPanel.php @@ -309,7 +309,7 @@ public function setMediaForm(): void } } - public function addFilesAction(): Action + public function addFilesAction(bool $insertAfter = false): Action { return Action::make('addFiles') ->button() @@ -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(); @@ -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') @@ -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; + } }