Skip to content

Commit

Permalink
FileDefaultPreview class
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jul 25, 2024
1 parent 3ebd535 commit d07960e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Panel/Ui/FilePreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function details(): array
],
[
'title' => I18n::translate('url'),
'text' => $this->file->id(),
'text' => '/' . $this->file->id(),
'link' => $this->file->previewUrl()
],
[
Expand Down
49 changes: 49 additions & 0 deletions src/Panel/Ui/FilePreviews/FileDefaultPreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Kirby\Panel\Ui\FilePreviews;

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

/**
* Fallback file preview component
*
* @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 FileDefaultPreview extends FilePreview
{
public string $component = 'k-file-default-preview';

/**
* Accepts any file as last resort
*/
public static function accepts(File $file): bool
{
return true;
}

/**
* Icon or image to display as thumbnail
*/
public function image(): array|null
{
return $this->file->panel()->image([
'back' => 'transparent',
'ratio' => '1/1'
], 'cards');
}

public function props(): array
{
return [
...parent::props(),
'image' => $this->image()
];
}
}
28 changes: 14 additions & 14 deletions tests/Panel/Ui/FilePreviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ public function testProps()
[
'title' => 'Template',
'text' => '',
],
[
'title' => 'Media Type',
'text' => 'image/jpeg',
],
[
'title' => 'Url',
'text' => 'test/test.jpg',
'link' => '/test/test.jpg',
],
[
'title' => 'Size',
'text' => '0 KB',
]
],
[
'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());
Expand Down
41 changes: 41 additions & 0 deletions tests/Panel/Ui/FilePreviews/FileDefaultPreviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Kirby\Panel\Ui\FilePreviews;

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

/**
* @coversDefaultClass \Kirby\Panel\Ui\FilePreviews\FileDefaultPreview
* @covers ::__construct
*/
class FileDefaultPreviewTest extends TestCase
{
/**
* @covers ::accepts
*/
public function testAccepts()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);

$this->assertTrue(FileDefaultPreview::accepts($file));
}

/**
* @covers ::image
* @covers ::props
*/
public function testProps()
{
$page = new Page(['slug' => 'test']);
$file = new File(['filename' => 'test.jpg', 'parent' => $page]);
$component = new FileDefaultPreview($file);
$props = $component->props();

$this->assertSame('image', $props['image']['icon']);
$this->assertFalse($props['image']['cover']);
$this->assertIsString($props['image']['src']);
}
}

0 comments on commit d07960e

Please sign in to comment.