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

Support multiple Panel assets and absolute URLs #5602

Merged
Merged
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
98 changes: 59 additions & 39 deletions src/Panel/Assets.php
Original file line number Diff line number Diff line change
@@ -3,11 +3,13 @@
namespace Kirby\Panel;

use Kirby\Cms\App;
use Kirby\Cms\Url;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Filesystem\Asset;
use Kirby\Filesystem\Dir;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;

/**
* The Assets class collects all js, css, icons and other
@@ -54,11 +56,13 @@ public function __construct()
*/
public function css(): array
{
$css = [
'index' => $this->url . '/css/style.min.css',
'plugins' => $this->plugins->url('css'),
'custom' => $this->custom('panel.css'),
];
$css = A::merge(
[
'index' => $this->url . '/css/style.min.css',
'plugins' => $this->plugins->url('css')
],
$this->custom('panel.css')
);

// during dev mode we do not need to load
// the general stylesheet (as styling will be inlined)
@@ -73,17 +77,28 @@ public function css(): array
* Check for a custom asset file from the
* config (e.g. panel.css or panel.js)
*/
public function custom(string $option): string|null
public function custom(string $option): array
{
if ($path = $this->kirby->option($option)) {
$asset = new Asset($path);
$customs = [];

if ($assets = $this->kirby->option($option)) {
$assets = A::wrap($assets);

if ($asset->exists() === true) {
return $asset->url() . '?' . $asset->modified();
foreach ($assets as $index => $path) {
if (Url::isAbsolute($path) === true) {
$customs['custom-' . $index] = $path;
continue;
}

$asset = new Asset($path);

if ($asset->exists() === true) {
$customs['custom-' . $index] = $asset->url() . '?' . $asset->modified();
}
}
}

return null;
return $customs;
}

/**
@@ -161,37 +176,42 @@ public function icons(): string
*/
public function js(): array
{
$js = [
'vue' => [
'nonce' => $this->nonce,
'src' => $this->url . '/js/vue.min.js'
],
'vendor' => [
'nonce' => $this->nonce,
'src' => $this->url . '/js/vendor.min.js',
'type' => 'module'
],
'pluginloader' => [
'nonce' => $this->nonce,
'src' => $this->url . '/js/plugins.js',
'type' => 'module'
],
'plugins' => [
'nonce' => $this->nonce,
'src' => $this->plugins->url('js'),
'defer' => true
],
'custom' => [
'nonce' => $this->nonce,
'src' => $this->custom('panel.js'),
'type' => 'module'
$js = A::merge(
[
'vue' => [
'nonce' => $this->nonce,
'src' => $this->url . '/js/vue.min.js'
],
'vendor' => [
'nonce' => $this->nonce,
'src' => $this->url . '/js/vendor.min.js',
'type' => 'module'
],
'pluginloader' => [
'nonce' => $this->nonce,
'src' => $this->url . '/js/plugins.js',
'type' => 'module'
],
'plugins' => [
'nonce' => $this->nonce,
'src' => $this->plugins->url('js'),
'defer' => true
]
],
'index' => [
A::map($this->custom('panel.js'), fn ($src) => [
'nonce' => $this->nonce,
'src' => $this->url . '/js/index.min.js',
'src' => $src,
'type' => 'module'
],
];
]),
[
'index' => [
'nonce' => $this->nonce,
'src' => $this->url . '/js/index.min.js',
'type' => 'module'
],
]
);


// during dev mode, add vite client and adapt
// path to `index.js` - vendor does not need
57 changes: 43 additions & 14 deletions tests/Panel/AssetsTest.php
Original file line number Diff line number Diff line change
@@ -106,12 +106,14 @@ public function testCssInDevMode(): void

/**
* @covers ::css
* @covers ::custom
*/
public function testCssWithCustomFile(): void
{
// valid
F::write($this->tmp . '/panel.css', '');
F::write($this->tmp . '/foo.css', '');

// single
$this->app->clone([
'options' => [
'panel' => [
@@ -121,11 +123,23 @@ public function testCssWithCustomFile(): void
]);

$assets = new Assets();
$assets = $assets->custom('panel.css');
$this->assertStringContainsString('/panel.css', $assets['custom-0']);

$this->assertStringContainsString(
'/panel.css',
$assets->custom('panel.css')
);
// multiple
$this->app->clone([
'options' => [
'panel' => [
'css' => ['panel.css', 'foo.css', $url = 'https://getkirby.com/bar.css']
]
]
]);

$assets = new Assets();
$assets = $assets->custom('panel.css');
$this->assertStringContainsString('/panel.css', $assets['custom-0']);
$this->assertStringContainsString('/foo.css', $assets['custom-1']);
$this->assertSame($url, $assets['custom-2']);
}

/**
@@ -142,7 +156,7 @@ public function testCssWithCustomFileMissing(): void
]);

$assets = new Assets();
$this->assertNull($assets->custom('panel.css'));
$this->assertEmpty($assets->custom('panel.css'));
}

/**
@@ -163,6 +177,7 @@ public function testCssWithCustomUrl(): void

/**
* @covers ::external
* @covers ::custom
*/
public function testExternalWithCustomCssJs(): void
{
@@ -183,8 +198,8 @@ public function testExternalWithCustomCssJs(): void
$assets = new Assets();
$external = $assets->external();

$this->assertTrue(Str::contains($external['css']['custom'], 'assets/panel.css'));
$this->assertTrue(Str::contains($external['js']['custom']['src'], 'assets/panel.js'));
$this->assertTrue(Str::contains($external['css']['custom-0'], 'assets/panel.css'));
$this->assertTrue(Str::contains($external['js']['custom-0']['src'], 'assets/panel.js'));
}

/**
@@ -377,9 +392,10 @@ public function testJsInDevMode(): void
*/
public function testJsWithCustomFile(): void
{
// valid
F::write($this->tmp . '/panel.js', '');
F::write($this->tmp . '/foo.js', '');

// single
$this->app->clone([
'options' => [
'panel' => [
@@ -389,11 +405,24 @@ public function testJsWithCustomFile(): void
]);

$assets = new Assets();
$assets = $assets->custom('panel.js');
$this->assertStringContainsString('/panel.js', $assets['custom-0']);


$this->assertStringContainsString(
'/panel.js',
$assets->custom('panel.js')
);
// multiple
$this->app->clone([
'options' => [
'panel' => [
'js' => ['panel.js', 'foo.js', $url = 'https://getkirby.com/bar.js']
]
]
]);

$assets = new Assets();
$assets = $assets->custom('panel.js');
$this->assertStringContainsString('/panel.js', $assets['custom-0']);
$this->assertStringContainsString('/foo.js', $assets['custom-1']);
$this->assertSame($url, $assets['custom-2']);
}

/**
@@ -410,7 +439,7 @@ public function testJsWithCustomFileMissing(): void
]);

$assets = new Assets();
$this->assertNull($assets->custom('panel.js'));
$this->assertEmpty($assets->custom('panel.js'));
}

/**