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

Previews 3 - Image preview class #6576

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
* @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
39 changes: 26 additions & 13 deletions tests/Panel/Ui/FilePreviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class InvalidFilePreview
*/
class FilePreviewTest extends TestCase
{
public function setUp(): void
{
$this->app = new App([
'roots' => [
'index' => '/dev/null'
],
]);

// authenticate for preview URL
$this->app->impersonate('kirby');
}

/**
* @covers ::details
*/
Expand Down Expand Up @@ -70,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 @@ -92,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 @@ -151,6 +163,7 @@ public function testProps()
$props = $preview->props();

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

Expand All @@ -159,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']);
}
}
Loading