Filepond fieldtype is now just File and other misc. refactoring.

This commit is contained in:
Ryan Scherler
2020-12-27 22:07:39 -08:00
parent e13c9594ab
commit 8d49091c89
10 changed files with 104 additions and 83 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,4 +1,4 @@
{
"/js/filament.js": "/js/filament.js?id=575bb8a26dd76080eafa",
"/css/filament.css": "/css/filament.css?id=5b1080be992429761d2b"
"/css/filament.css": "/css/filament.css?id=bed45e60a1a278f96ad8"
}
+70
View File
@@ -0,0 +1,70 @@
@extends('filament::layouts.field-group')
@pushonce('head')
<link rel="stylesheet" href="https://unpkg.com/filepond/dist/filepond.css">
@endpushonce
@pushonce('js')
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>
@endpushonce
@section('field')
<div class="p-4 rounded focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 border @error($field->error ?? $field->model) border-red-600 motion-safe:animate-shake @else border-gray-100 @enderror space-y-6">
<div
x-data
x-init="
FilePond.setOptions({
allowMultiple: {{ isset($field->extraAttributes['multiple']) ? 'true' : 'false' }},
server: {
process:(fieldName, file, metadata, load, error, progress, abort, transfer, options) => {
@this.upload('{{ $field->model }}', file, load, error, progress)
},
revert: (filename, load, error) => {
@this.removeUpload('{{ $field->model }}', filename, load)
},
},
});
FilePond.create($refs.input);
"
wire:ignore
>
<input
type="file"
x-ref="input"
/>
</div>
@if ($field->value)
<ol class="space-y-4">
@foreach ($field->value as $file)
<li class="p-2 shadow-sm rounded border border-gray-300 p-2 flex items-center space-x-4">
@if (Filament::assetIsImage($file))
<a href="{{ Filament::assetDisk()->url($file) }}" target="_blank" rel="noopener noreferrer" class="flex-shrink-0">
<x-filament-image :src="$file" alt="{{ $file }}" :manipulations="[ 'w' => 48, 'h' => 48, 'fit' => 'crop' ]" width="48px" height="48px" loading="lazy" class="w-12 h-12 rounded" />
</a>
@endif
<div class="flex-grow overflow-scroll flex flex-col leading-tight">
<a href="{{ Filament::assetDisk()->url($file) }}" target="_blank" rel="noopener noreferrer" class="text-sm link">
{{ $file }}
</a>
<dl class="font-mono text-xs text-gray-500">
<dt class="sr-only">{{ __('MIME Type') }}</dt>
<dd>{{ Filament::assetDisk()->getMimeType($file) }}</dd>
<dt class="sr-only">{{ __('File Size') }}</dt>
<dd>{{ Filament::formatBytes(Filament::assetDisk()->size($file)) }}</dd>
</dl>
</div>
@if ($field->deleteMethod)
<button
type="button"
wire:click="{{ $field->deleteMethod }}('{{ $file }}')"
class="flex-shrink-0 text-gray-500 hover:text-red-600 transition-colors duration-200"
>
<x-heroicon-o-x class="w-4 h-4" />
</button>
@endif
</li>
@endforeach
</ol>
@endif
</div>
@overwrite
-66
View File
@@ -1,66 +0,0 @@
@extends('filament::layouts.field-group')
@pushonce('head')
<link rel="stylesheet" href="https://unpkg.com/filepond/dist/filepond.css">
@endpushonce
@pushonce('js')
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>
@endpushonce
@section('field')
<div class="p-4 rounded focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 border @error($field->error ?? $field->model) border-red-600 motion-safe:animate-shake @else border-gray-100 @enderror space-y-6">
<div
x-data
x-init="
FilePond.setOptions({
allowMultiple: {{ isset($field->extraAttributes['multiple']) ? 'true' : 'false' }},
server: {
process:(fieldName, file, metadata, load, error, progress, abort, transfer, options) => {
@this.upload('{{ $field->model }}', file, load, error, progress)
},
revert: (filename, load, error) => {
@this.removeUpload('{{ $field->model }}', filename, load)
},
},
});
FilePond.create($refs.input);
"
wire:ignore
>
<input
type="file"
x-ref="input"
/>
</div>
@if ($field->value)
<div class="overflow-hidden">
<ol class="flex flex-wrap -m-2">
@foreach ($field->value as $file)
<li class="w-full md:w-auto p-2">
<div class="shadow-sm rounded border border-gray-300 p-2 flex items-center space-x-2">
@if (Filament::isImage($file))
<a href="{{ Filament::url($file) }}" target="_blank" rel="noopener noreferrer" class="flex-shrink-0">
<x-filament-image :src="$file" alt="{{ $file }}" :manipulations="[ 'w' => 32, 'h' => 32, 'fit' => 'crop' ]" width="32px" height="32px" loading="lazy" class="w-8 h-8 rounded" />
</a>
@endif
<a href="{{ Filament::url($file) }}" target="_blank" rel="noopener noreferrer" class="font-mono text-xs leading-tight text-gray-500">
{{ $file }}
</a>
@if ($field->deleteMethod)
<button
type="button"
wire:click="{{ $field->deleteMethod }}('{{ $file }}')"
class="flex-shrink-0 text-gray-500 hover:text-red-600"
>
<x-heroicon-o-x class="w-4 h-4" />
</button>
@endif
</div>
</li>
@endforeach
</ol>
</div>
@endif
</div>
@overwrite
+1 -1
View File
@@ -28,7 +28,7 @@
</div>
{{ $actions ?? null }}
</header>
<main id="content" class="flex-grow max-w-full px-4 md:px-6">
<main id="content" class="flex-grow max-w-full pb-6 px-4 md:px-6">
{{ $slot }}
</main>
</div>
@@ -2,7 +2,7 @@
namespace Filament\Fields;
class Filepond extends Field {
class File extends Field {
public $deleteMethod;
/**
+27 -10
View File
@@ -188,6 +188,29 @@ class Filament
: RouteServiceProvider::HOME;
}
/**
* Format bytes
*
* @param integer $size
* @param integer $precision
* @return string|integer
*/
public function formatBytes($size, $precision = 0)
{
if ($size > 0) {
$base = log($size) / log(1024);
$suffixes = ['bytes', 'KB', 'MB', 'GB', 'TB'];
return round(pow(1024, $base - floor($base)), $precision).$suffixes[intval($base)];
} else {
return $size;
}
}
public function assetDisk(): \Illuminate\Filesystem\FilesystemAdapter
{
return Storage::disk(config('filament.storage_disk'));
}
/**
* Determines if a given asset is an image.
*
@@ -196,15 +219,13 @@ class Filament
* @param string $file
* @return bool
*/
public function isImage(string $file): bool
public function assetIsImage(string $file): bool
{
$disk = Storage::disk(config('filament.storage_disk'));
if (! $disk->exists($file)) {
if (! $this->assetDisk()->exists($file)) {
return false;
}
if (! exif_imagetype($disk->path($file))) {
if (! exif_imagetype($this->assetDisk()->path($file))) {
return false;
}
@@ -223,12 +244,8 @@ class Filament
*
* @return mixed
*/
public function url($path, $manipulations = [])
public function assetUrl($path, $manipulations = [])
{
if (empty($manipulations)) {
return Storage::disk(config('filament.storage_disk'))->url($path);
}
$urlBuilder = UrlBuilderFactory::create('', config('app.key'));
return route('filament.image', ['path' => ltrim($urlBuilder->getUrl($path, $manipulations), '/')]);
}
+1 -1
View File
@@ -43,7 +43,7 @@ class Avatar extends Component
return Gravatar::src($this->user->email, $this->size * $dpr);
}
return Filament::url($this->user->avatar, [
return Filament::assetUrl($this->user->avatar, [
'w' => $this->size,
'h' => $this->size,
'fit' => 'crop',
+1 -1
View File
@@ -29,7 +29,7 @@ class Image extends Component
public function src($dpr = 1): string
{
return Filament::url($this->src, array_merge($this->manipulations, ['dpr' => $dpr]));
return Filament::assetUrl($this->src, array_merge($this->manipulations, ['dpr' => $dpr]));
}
public function srcSet(): string