Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds action to store media using spatie/laravel-medialibrary #424

Open
wants to merge 7 commits into
base: 3.x
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"pest_2.34.7","defects":[],"times":{"P\\Tests\\src\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.21,"P\\Tests\\src\\FormsTest::__pest_evaluable_it_has_editor_field":0.073,"P\\Tests\\src\\FormsTest::__pest_evaluable_it_creates_record":0.464,"P\\Tests\\src\\FormsTest::__pest_evaluable_it_updates_record":0.403,"P\\Tests\\src\\FormsTest::__pest_evaluable_it_can_create_null_record":0.31}}
{"version":"pest_2.34.8","defects":[],"times":{"P\\Tests\\src\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.144,"P\\Tests\\src\\FormsTest::__pest_evaluable_it_has_editor_field":0.039,"P\\Tests\\src\\FormsTest::__pest_evaluable_it_creates_record":0.557,"P\\Tests\\src\\FormsTest::__pest_evaluable_it_updates_record":0.427,"P\\Tests\\src\\FormsTest::__pest_evaluable_it_can_create_null_record":0.299}}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ Tools can also be added on a per-instance basis by using the `->tools()` modifie
'image_resize_target_height' => null,
]
```
### Media Storage

By default, the media files are stored in the `public` disk in the `media` directory.

If you prefer to store your media using `spatie/laravel-medialibrary` package you can do so by changing the `media_action` key in the config file `filament-tiptap-editor.php` to `SpatieMediaLibraryAction`:

```php
/*
|--------------------------------------------------------------------------
| Actions
|--------------------------------------------------------------------------
|
*/
'media_action' => FilamentTiptapEditor\Actions\SpatieMediaLibraryAction::class,
```


### Output format

Expand Down
148 changes: 148 additions & 0 deletions src/Actions/SpatieMediaLibraryAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace FilamentTiptapEditor\Actions;

use Filament\Forms\ComponentContainer;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\BaseFileUpload;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\TextInput;
use FilamentTiptapEditor\TiptapEditor;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

class SpatieMediaLibraryAction extends Action
{
public static function getDefaultName(): ?string
{
return 'filament_tiptap_media';
}

protected function setUp(): void
{
parent::setUp();

$this
->arguments([
'src' => '',
'alt' => '',
'title' => '',
'width' => '',
'height' => '',
'lazy' => null,
])
->modalWidth('md')
->mountUsing(function (TiptapEditor $component, ComponentContainer $form, array $arguments) {
$source = $arguments['src'] !== ''
? $component->getDirectory() . Str::of($arguments['src'])
->after($component->getDirectory())
: null;

$form->fill([
'src' => $source,
'alt' => $arguments['alt'] ?? '',
'title' => $arguments['title'] ?? '',
'width' => $arguments['width'] ?? '',
'height' => $arguments['height'] ?? '',
'lazy' => $arguments['lazy'] ?? false,
]);
})->modalHeading(function (TiptapEditor $component, array $arguments) {
$context = blank($arguments['src'] ?? null) ? 'insert' : 'update';

return trans('filament-tiptap-editor::media-modal.heading.' . $context);
})->form(function (TiptapEditor $component) {
return [
FileUpload::make('src')
->label(trans('filament-tiptap-editor::media-modal.labels.file'))
->disk($component->getDisk())
->directory($component->getDirectory())
->visibility($component->getVisibility())
->preserveFilenames($component->shouldPreserveFileNames())
->acceptedFileTypes($component->getAcceptedFileTypes())
->maxFiles(1)
->maxSize($component->getMaxSize())
->minSize($component->getMinSize())
->imageResizeMode($component->getImageResizeMode())
->imageCropAspectRatio($component->getImageCropAspectRatio())
->imageResizeTargetWidth($component->getImageResizeTargetWidth())
->imageResizeTargetHeight($component->getImageResizeTargetHeight())
->required()
->live()
->afterStateUpdated(function (TemporaryUploadedFile $state, callable $set) {
if (Str::contains($state->getMimeType(), 'image')) {
$set('type', 'image');
} else {
$set('type', 'document');
}

if ($dimensions = $state->dimensions()) {
$set('width', $dimensions[0]);
$set('height', $dimensions[1]);
}
})
->saveUploadedFileUsing(function (BaseFileUpload $component, TemporaryUploadedFile $file, callable $set) {
$model = $component->getContainer()->model;
$media = $component->shouldPreserveFilenames()
? $model->addMedia($file)->toMediaCollection('tiptap-media')
: $model->addMedia($file)->usingFileName(Str::uuid())->toMediaCollection('ck-media');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is 'ck-media'?


return $media->getUrl('large');
}),
TextInput::make('link_text')
->label(trans('filament-tiptap-editor::media-modal.labels.link_text'))
->required()
->visible(fn (callable $get) => $get('type') == 'document'),
TextInput::make('alt')
->label(trans('filament-tiptap-editor::media-modal.labels.alt'))
->hidden(fn (callable $get) => $get('type') == 'document')
->hintAction(
Action::make('alt_hint_action')
->label('?')
->color('primary')
->url('https://www.w3.org/WAI/tutorials/images/decision-tree', true)
),
TextInput::make('title')
->label(trans('filament-tiptap-editor::media-modal.labels.title')),
Checkbox::make('lazy')
->label(trans('filament-tiptap-editor::media-modal.labels.lazy'))
->default(false),
Group::make([
TextInput::make('width'),
TextInput::make('height'),
])->columns(),
Hidden::make('type')
->default('document'),
];
})->action(function (TiptapEditor $component, $data) {
if (config('filament-tiptap-editor.use_relative_paths')) {
$source = Str::of($data['src'])
->replace(config('app.url'), '')
->ltrim('/')
->prepend('/');
} else {
$source = str_starts_with($data['src'], 'http')
? $data['src']
: Storage::disk(config('filament-tiptap-editor.disk'))->url($data['src']);
}

$component->getLivewire()->dispatch(
event: 'insertFromAction',
type: 'media',
statePath: $component->getStatePath(),
media: [
'src' => $source,
'alt' => $data['alt'] ?? null,
'title' => $data['title'],
'width' => $data['width'],
'height' => $data['height'],
'lazy' => $data['lazy'] ?? false,
'link_text' => $data['link_text'] ?? null,
],
);
});
}
}