Skip to content

Commit

Permalink
Merge pull request #6576 from getkirby/v5/file-previews/3-image-preview
Browse files Browse the repository at this point in the history
Previews 3 - Image preview class
  • Loading branch information
bastianallgeier authored Aug 15, 2024
2 parents 1cedd6d + 161d2ec commit 295ca51
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/Cms/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Kirby\Cms\Auth\TotpChallenge;
use Kirby\Form\Field\BlocksField;
use Kirby\Form\Field\LayoutField;
use Kirby\Panel\Ui\FilePreviews\ImageFilePreview;

/**
* The Core class lists all parts of Kirby
Expand Down Expand Up @@ -280,7 +281,9 @@ public function fields(): array
*/
public function filePreviews(): array
{
return [];
return [
ImageFilePreview::class
];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Panel/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,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();
Expand Down
1 change: 1 addition & 0 deletions src/Panel/Ui/FilePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function props(): array
{
return [
'details' => $this->details(),
'image' => $this->image(),
'url' => $this->file->previewUrl()
];
}
Expand Down
53 changes: 53 additions & 0 deletions src/Panel/Ui/FilePreviews/ImageFilePreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Kirby\Panel\Ui\FilePreviews;

use Kirby\Cms\File;
use Kirby\Panel\Ui\FilePreview;
use Kirby\Toolkit\I18n;

/**
* @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
*/
class ImageFilePreview extends FilePreview
{
public function __construct(
public File $file,
public string $component = 'k-image-file-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()
];
}
}
2 changes: 1 addition & 1 deletion tests/Cms/App/AppPluginsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function testFilePreviews()
]
]);

$this->assertCount(1, $app->extensions('filePreviews'));
$this->assertCount(2, $app->extensions('filePreviews'));
}

public function testKirbyTag()
Expand Down
2 changes: 1 addition & 1 deletion tests/Cms/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function testFields()
public function testFilePreviews()
{
$previews = $this->core->filePreviews();
$this->assertCount(0, $previews);
$this->assertCount(1, $previews);
}

/**
Expand Down
27 changes: 14 additions & 13 deletions tests/Panel/Ui/FilePreviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public function testDetails()
public function testFactory()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$file = new File(['filename' => 'test.pdf', 'parent' => $page]);

$component = FilePreview::factory($file);
$this->assertInstanceOf(DefaultFilePreview::class, $component);
$preview = FilePreview::factory($file);
$this->assertInstanceOf(DefaultFilePreview::class, $preview);
}

/**
Expand All @@ -104,13 +104,13 @@ public function testFactoryWithCustomHandler()

$page = new Page(['slug' => 'test']);

$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$component = FilePreview::factory($file);
$this->assertInstanceOf(DefaultFilePreview::class, $component);
$file = new File(['filename' => 'test.foo', 'parent' => $page]);
$preview = FilePreview::factory($file);
$this->assertInstanceOf(DefaultFilePreview::class, $preview);

$file = new File(['filename' => 'test.xls', 'parent' => $page]);
$component = FilePreview::factory($file);
$this->assertInstanceOf(DummyFilePreview::class, $component);
$file = new File(['filename' => 'test.xls', 'parent' => $page]);
$preview = FilePreview::factory($file);
$this->assertInstanceOf(DummyFilePreview::class, $preview);
}

/**
Expand Down Expand Up @@ -163,6 +163,7 @@ public function testProps()
$props = $preview->props();

$this->assertIsArray($props['details']);
$this->assertIsArray($props['image']);
$this->assertIsString($props['url']);
}

Expand All @@ -171,10 +172,10 @@ public function testProps()
*/
public function testRender()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$component = new DummyFilePreview($file);
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$preview = new DummyFilePreview($file);

$this->assertSame('k-dummy-file-preview', $component->render()['component']);
$this->assertSame('k-dummy-file-preview', $preview->render()['component']);
}
}
69 changes: 69 additions & 0 deletions tests/Panel/Ui/FilePreviews/ImageFilePreviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Kirby\Panel\Ui\FilePreviews;

use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\Panel\Ui\FilePreview;
use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\FilePreviews\ImageFilePreview
* @covers ::__construct
*/
class ImageFilePreviewTest extends TestCase
{
/**
* @covers ::accepts
*/
public function testAccepts()
{
$page = new Page(['slug' => 'test']);

$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$this->assertTrue(ImageFilePreview::accepts($file));

$file = new File(['filename' => 'test.xls', 'parent' => $page]);
$this->assertFalse(ImageFilePreview::accepts($file));
}

/**
* @covers ::details
*/
public function testDetails()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$preview = new ImageFilePreview($file);
$details = $preview->details();

$detail = array_pop($details);
$this->assertSame('Orientation', $detail['title']);

$detail = array_pop($details);
$this->assertSame('Dimensions', $detail['title']);
}

/**
* @coversNothing
*/
public function testFactory()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$preview = FilePreview::factory($file);
$this->assertInstanceOf(ImageFilePreview::class, $preview);
}

/**
* @covers ::props
*/
public function testProps()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.xls', 'parent' => $page]);
$preview = new ImageFilePreview($file);
$props = $preview->props();
$this->assertFalse($props['focusable']);
}
}

0 comments on commit 295ca51

Please sign in to comment.