Skip to content

Commit

Permalink
🧑‍💻 Change default editorStyles implementation from URL to local th…
Browse files Browse the repository at this point in the history
…eme path (#367)

---------

Co-authored-by: QWp6t <hi@qwp6t.me>
  • Loading branch information
Log1x and QWp6t committed Mar 29, 2024
1 parent 308e5a0 commit cacfc23
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/Roots/Acorn/Assets/Concerns/Enqueuable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Roots\Acorn\Assets\Concerns;

use Illuminate\Support\Str;
use Roots\Acorn\Filesystem\Filesystem;

trait Enqueuable
{
/**
Expand Down Expand Up @@ -85,7 +88,24 @@ public function enqueue()
*/
public function editorStyles()
{
$this->css(fn ($handle, $src) => add_editor_style($src));
$relativePath = (new Filesystem)->getRelativePath(
Str::finish(get_theme_file_path(), '/'),
$this->path
);

$this->css(function ($handle, $src) use ($relativePath) {
if (! Str::startsWith($src, $this->uri)) {
return add_editor_style($src);
}

$style = Str::of($src)
->after($this->uri)
->ltrim('/')
->start("{$relativePath}/")
->toString();

add_editor_style($style);
});

return $this;
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Assets/BundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
$manifest = json_decode(file_get_contents($this->fixture('bud_single_runtime/public/entrypoints.json')), JSON_OBJECT_AS_ARRAY);
$app = new Bundle('app', $manifest['app'], $this->fixture('bud_single_runtime'), 'https://k.jo');

$app->js(fn ($handle, $src, $dependencies) => assertMatchesSnapshot(compact('handle', 'src', 'dependencies'))
);
$app->js(fn ($handle, $src, $dependencies) => assertMatchesSnapshot(compact('handle', 'src', 'dependencies')));

$app->css(fn ($handle, $src) => assertMatchesSnapshot(compact('handle', 'src'))
);
$app->css(fn ($handle, $src) => assertMatchesSnapshot(compact('handle', 'src')));
});

it('can enqueue css', function () {
Expand All @@ -46,6 +44,7 @@
$manifest = json_decode(file_get_contents($this->fixture('bud_single_runtime/public/entrypoints.json')), JSON_OBJECT_AS_ARRAY);
$app = new Bundle('app', $manifest['app'], $this->fixture('bud_single_runtime'), 'https://k.jo');

$this->stub('get_theme_file_path', fn ($path = '') => $this->fixture('bud_v6_single_runtime/public/'.$path));
$this->stub('add_editor_style', fn (...$args) => assertMatchesSnapshot($args));

$app->editorStyles();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- 'https://k.jo/public/app.2.4476c5f869ee7584ba3c.css'
- ../../bud_single_runtime/public/app.2.4476c5f869ee7584ba3c.css

0 comments on commit cacfc23

Please sign in to comment.