From 1693b9abe7769a6d83cf47a712f24dbf3029fe5e Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Thu, 25 Jul 2024 19:08:30 +0200 Subject: [PATCH] `FileImagePreview` class --- src/Cms/Core.php | 5 +- src/Panel/File.php | 2 +- .../Ui/FilePreviews/FileImagePreview.php | 48 ++++++++++++++++ tests/Cms/App/AppPluginsTest.php | 2 +- tests/Cms/CoreTest.php | 2 +- tests/Panel/Ui/FilePreviewTest.php | 2 +- .../Ui/FilePreviews/FileImagePreviewTest.php | 57 +++++++++++++++++++ 7 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 src/Panel/Ui/FilePreviews/FileImagePreview.php create mode 100644 tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php diff --git a/src/Cms/Core.php b/src/Cms/Core.php index 441292d0c0..0292a724f4 100644 --- a/src/Cms/Core.php +++ b/src/Cms/Core.php @@ -10,6 +10,7 @@ use Kirby\Cms\Auth\TotpChallenge; use Kirby\Form\Field\BlocksField; use Kirby\Form\Field\LayoutField; +use Kirby\Panel\Ui\FilePreviews\FileImagePreview; /** * The Core class lists all parts of Kirby @@ -280,7 +281,9 @@ public function fields(): array */ public function filePreviews(): array { - return []; + return [ + FileImagePreview::class + ]; } /** diff --git a/src/Panel/File.php b/src/Panel/File.php index a1cd12b304..f886a5be9d 100644 --- a/src/Panel/File.php +++ b/src/Panel/File.php @@ -300,7 +300,7 @@ protected function imageSource( public function isFocusable(): bool { // blueprint option - $option = $this->model->blueprint()->focus(); + $option = $this->model->blueprint()->focus(); // fallback to whether the file is viewable // (images should be focusable by default, others not) $option ??= $this->model->isViewable(); diff --git a/src/Panel/Ui/FilePreviews/FileImagePreview.php b/src/Panel/Ui/FilePreviews/FileImagePreview.php new file mode 100644 index 0000000000..6b970bc51d --- /dev/null +++ b/src/Panel/Ui/FilePreviews/FileImagePreview.php @@ -0,0 +1,48 @@ + + * @link https://getkirby.com + * @copyright Bastian Allgeier + * @license https://getkirby.com/license + * @since 5.0.0 + * @internal + */ +class FileImagePreview extends FileDefaultPreview +{ + public string $component = 'k-file-image-preview'; + + public static function accepts(File $file): bool + { + return $file->type() === 'image'; + } + + public function details(): array + { + return [ + ...parent::details(), + [ + 'title' => I18n::translate('dimensions'), + 'text' => $this->file->dimensions() . ' ' . I18n::translate('pixel') + ], + [ + 'title' => I18n::translate('orientation'), + 'text' => I18n::translate('orientation.' . $this->file->dimensions()->orientation()) + ] + ]; + } + + public function props(): array + { + return [ + ...parent::props(), + 'focusable' => $this->file->panel()->isFocusable() + ]; + } +} diff --git a/tests/Cms/App/AppPluginsTest.php b/tests/Cms/App/AppPluginsTest.php index 9ca831b64e..a9e81e422c 100644 --- a/tests/Cms/App/AppPluginsTest.php +++ b/tests/Cms/App/AppPluginsTest.php @@ -416,7 +416,7 @@ public function testFilePreviews() ] ]); - $this->assertCount(1, $app->extensions('filePreviews')); + $this->assertCount(2, $app->extensions('filePreviews')); } public function testKirbyTag() diff --git a/tests/Cms/CoreTest.php b/tests/Cms/CoreTest.php index 2eef2147a4..bd6bad3bbb 100644 --- a/tests/Cms/CoreTest.php +++ b/tests/Cms/CoreTest.php @@ -265,7 +265,7 @@ public function testFields() public function testFilePreviews() { $previews = $this->core->filePreviews(); - $this->assertCount(0, $previews); + $this->assertCount(1, $previews); } /** diff --git a/tests/Panel/Ui/FilePreviewTest.php b/tests/Panel/Ui/FilePreviewTest.php index c771cd4e7d..4c379d7779 100644 --- a/tests/Panel/Ui/FilePreviewTest.php +++ b/tests/Panel/Ui/FilePreviewTest.php @@ -81,7 +81,7 @@ public function testProps() ], [ 'title' => 'Url', - 'text' => 'test/test.jpg', + 'text' => '/test/test.jpg', 'link' => '/test/test.jpg', ], [ diff --git a/tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php b/tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php new file mode 100644 index 0000000000..ac12f8235a --- /dev/null +++ b/tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php @@ -0,0 +1,57 @@ + 'test']); + + $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + $this->assertTrue(FileImagePreview::accepts($file)); + + $file = new File(['filename' => 'test.xls', 'parent' => $page]); + $this->assertFalse(FileImagePreview::accepts($file)); + } + + /** + * @covers ::details + */ + public function testDetails() + { + $page = new Page(['slug' => 'test']); + $file = new File(['filename' => 'test.jpg', 'parent' => $page]); + $preview = new FileImagePreview($file); + $details = $preview->details(); + + $detail = array_pop($details); + $this->assertSame('Orientation', $detail['title']); + + $detail = array_pop($details); + $this->assertSame('Dimensions', $detail['title']); + } + + /** + * @covers ::props + */ + public function testProps() + { + $page = new Page(['slug' => 'test']); + $file = new File(['filename' => 'test.xls', 'parent' => $page]); + $preview = new FileImagePreview($file); + $props = $preview->props(); + $this->assertFalse($props['focusable']); + } +}