-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54e2ed4
commit 3ebd535
Showing
3 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui; | ||
|
||
use Kirby\Cms\App; | ||
use Kirby\Cms\File; | ||
use Kirby\Panel\Ui\FilePreviews\FileDefaultPreview; | ||
use Kirby\Toolkit\I18n; | ||
|
||
/** | ||
* Defines a component that implements a file preview | ||
* | ||
* @package Kirby Panel | ||
* @author Nico Hoffmann <nico@getkirby.com> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
* @since 5.0.0 | ||
* @internal | ||
*/ | ||
abstract class FilePreview extends Component | ||
{ | ||
public string $component = 'k-file-default-preview'; | ||
|
||
public function __construct( | ||
public File $file | ||
) { | ||
} | ||
|
||
/** | ||
* Returns true if this class should | ||
* handle the preview of this file | ||
*/ | ||
abstract public static function accepts(File $file): bool; | ||
|
||
/** | ||
* Returns detail information about the file | ||
*/ | ||
public function details(): array | ||
{ | ||
return [ | ||
[ | ||
'title' => I18n::translate('template'), | ||
'text' => $this->file->template() ?? '—' | ||
], | ||
[ | ||
'title' => I18n::translate('mime'), | ||
'text' => $this->file->mime() | ||
], | ||
[ | ||
'title' => I18n::translate('url'), | ||
'text' => $this->file->id(), | ||
'link' => $this->file->previewUrl() | ||
], | ||
[ | ||
'title' => I18n::translate('size'), | ||
'text' => $this->file->niceSize() | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Returns a file preview instance by going through all | ||
* available handler classes and finding the first that | ||
* accepts the file | ||
*/ | ||
final public static function factory(File $file): static | ||
{ | ||
// get file preview classes providers from plugins | ||
$handlers = App::instance()->extensions('filePreviews'); | ||
|
||
foreach ($handlers as $handler) { | ||
if ($handler::accepts($file) === true) { | ||
return new $handler($file); | ||
} | ||
} | ||
|
||
return new FileDefaultPreview($file); | ||
} | ||
|
||
public function props(): array | ||
{ | ||
return [ | ||
'details' => $this->details(), | ||
'url' => $this->file->previewUrl() | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui; | ||
|
||
use Kirby\Cms\App; | ||
use Kirby\Cms\File; | ||
use Kirby\Cms\Page; | ||
use Kirby\Panel\Ui\FilePreviews\FileDefaultPreview; | ||
use Kirby\TestCase; | ||
|
||
class FileDummyPreview extends FilePreview | ||
{ | ||
public static function accepts(File $file): bool | ||
{ | ||
return $file->type() === 'document'; | ||
} | ||
} | ||
|
||
/** | ||
* @coversDefaultClass \Kirby\Panel\Ui\FilePreview | ||
* @covers ::__construct | ||
*/ | ||
class FilePreviewTest extends TestCase | ||
{ | ||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactory() | ||
{ | ||
$page = new Page(['slug' => 'test']); | ||
$file = new File(['filename' => 'test.jpg', 'parent' => $page]); | ||
|
||
$component = FilePreview::factory($file); | ||
$this->assertInstanceOf(FileDefaultPreview::class, $component); | ||
} | ||
|
||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactoryWithCustomHandler() | ||
{ | ||
new App([ | ||
'roots' => [ | ||
'index' => '/dev/null' | ||
], | ||
'filePreviews' => [ | ||
FileDummyPreview::class | ||
] | ||
]); | ||
|
||
$page = new Page(['slug' => 'test']); | ||
|
||
$file = new File(['filename' => 'test.jpg', 'parent' => $page]); | ||
$component = FilePreview::factory($file); | ||
$this->assertInstanceOf(FileDefaultPreview::class, $component); | ||
|
||
$file = new File(['filename' => 'test.xls', 'parent' => $page]); | ||
$component = FilePreview::factory($file); | ||
$this->assertInstanceOf(FileDummyPreview::class, $component); | ||
} | ||
|
||
/** | ||
* @covers ::details | ||
* @covers ::props | ||
*/ | ||
public function testProps() | ||
{ | ||
$page = new Page(['slug' => 'test']); | ||
$file = new File(['filename' => 'test.jpg', 'parent' => $page]); | ||
$component = new FileDummyPreview($file); | ||
|
||
$this->assertSame([ | ||
'details' => [ | ||
[ | ||
'title' => 'Template', | ||
'text' => '—', | ||
], | ||
[ | ||
'title' => 'Media Type', | ||
'text' => 'image/jpeg', | ||
], | ||
[ | ||
'title' => 'Url', | ||
'text' => 'test/test.jpg', | ||
'link' => '/test/test.jpg', | ||
], | ||
[ | ||
'title' => 'Size', | ||
'text' => '0 KB', | ||
] | ||
], | ||
'url' => '/test/test.jpg' | ||
], $component->props()); | ||
} | ||
|
||
/** | ||
* @covers ::render | ||
*/ | ||
public function testRender() | ||
{ | ||
$page = new Page(['slug' => 'test']); | ||
$file = new File(['filename' => 'test.jpg', 'parent' => $page]); | ||
$component = new FileDummyPreview($file); | ||
|
||
$this->assertSame('k-file-default-preview', $component->render()['component']); | ||
} | ||
} |