Skip to content

Commit

Permalink
FileImagePreview class
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jul 25, 2024
1 parent d07960e commit 1693b9a
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 5 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\FileImagePreview;

/**
* 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 [
FileImagePreview::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 @@ -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();
Expand Down
48 changes: 48 additions & 0 deletions src/Panel/Ui/FilePreviews/FileImagePreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Kirby\Panel\Ui\FilePreviews;

use Kirby\Cms\File;
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 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()
];
}
}
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
2 changes: 1 addition & 1 deletion tests/Panel/Ui/FilePreviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testProps()
],
[
'title' => 'Url',
'text' => 'test/test.jpg',
'text' => '/test/test.jpg',
'link' => '/test/test.jpg',
],
[
Expand Down
57 changes: 57 additions & 0 deletions tests/Panel/Ui/FilePreviews/FileImagePreviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Kirby\Panel\Ui\FilePreviews;

use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\FilePreviews\FileImagePreview
* @covers ::__construct
*/
class FileImagePreviewTest extends TestCase
{
/**
* @covers ::accepts
*/
public function testAccepts()
{
$page = new Page(['slug' => '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']);
}
}

0 comments on commit 1693b9a

Please sign in to comment.