Skip to content

Commit 3782fbd

Browse files
authored
Merge pull request #45 from tonysm/tm/fix-install-script
Fix install command to import Trix JS module and stop publishing the CSS file in favor of the styles component
2 parents 557c89e + fc72f24 commit 3782fbd

File tree

3 files changed

+50
-484
lines changed

3 files changed

+50
-484
lines changed

resources/views/components/styles.blade.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@props(['theme' => 'default'])
22

3-
@if ($theme === 'richtextlaravel')
43
<style {{ $attributes }}>
4+
@if ($theme === 'richtextlaravel')
55
trix-editor {
66
border: 1px solid #bbb;
77
border-radius: 3px;
@@ -585,9 +585,7 @@
585585
flex-basis: 50%;
586586
max-width: 50%;
587587
}
588-
</style>
589588
@else
590-
<style {{ $attributes }}>
591589
trix-editor {
592590
border: 1px solid #bbb;
593591
border-radius: 3px;
@@ -998,5 +996,29 @@
998996
.trix-content .attachment-gallery.attachment-gallery--2 .attachment, .trix-content .attachment-gallery.attachment-gallery--4 .attachment {
999997
flex-basis: 50%;
1000998
max-width: 50%; }
1001-
</style>
1002999
@endif
1000+
1001+
/*
1002+
* We need to override trix.css’s image gallery styles to accommodate the
1003+
* <rich-text-attachment> element we wrap around attachments. Otherwise,
1004+
* images in galleries will be squished by the max-width: 33%; rule.
1005+
*/
1006+
.trix-content .attachment-gallery > rich-text-attachment,
1007+
.trix-content .attachment-gallery > .attachment {
1008+
flex: 1 0 33%;
1009+
padding: 0 0.5em;
1010+
max-width: 33%;
1011+
}
1012+
1013+
.trix-content .attachment-gallery.attachment-gallery--2 > rich-text-attachment,
1014+
.trix-content .attachment-gallery.attachment-gallery--2 > .attachment, .trix-content .attachment-gallery.attachment-gallery--4 > rich-text-attachment,
1015+
.trix-content .attachment-gallery.attachment-gallery--4 > .attachment {
1016+
flex-basis: 50%;
1017+
max-width: 50%;
1018+
}
1019+
1020+
.trix-content rich-text-attachment .attachment {
1021+
padding: 0 !important;
1022+
max-width: 100% !important;
1023+
}
1024+
</style>

src/Commands/InstallCommand.php

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
class InstallCommand extends Command
1414
{
15-
const JS_BOOTSTRAP_IMPORT_PATTERN = '/(.*[\'\"](?:\.\/)?bootstrap[\'\"].*)/';
16-
1715
const JS_TRIX_LIBS_IMPORT_PATTERN = '/import [\'\"](?:\.\/)?libs\/trix[\'\"];?/';
1816

1917
public $signature = 'richtext:install
@@ -31,7 +29,6 @@ public function handle()
3129
}
3230

3331
$this->ensureTrixLibIsImported();
34-
$this->ensureTrixOverridesStylesIsPublished();
3532
$this->ensureTrixFieldComponentIsCopied();
3633
$this->updateAppLayoutFiles();
3734
$this->updateJsDependencies();
@@ -136,45 +133,24 @@ private function ensureTrixLibIsImported(): void
136133
resource_path('js/app.js'),
137134
], fn ($file) => file_exists($file));
138135

139-
if (! File::exists($entrypoint)) {
136+
if (! $entrypoint) {
140137
$this->components->warn(sprintf('Add `%s` your main JS file.', sprintf("\nimport '%slibs/trix';\n", $this->usingImportmaps() ? '' : './')));
141138

142139
return;
143140
}
144141

145-
$this->components->info(sprintf('Importing the `libs/trix.js` module in `%s`', str($entrypoint)->after(resource_path())));
146-
147-
// If the import line doesn't exist on the js/app.js file, add it after the import
148-
// of the bootstrap.js file that ships with Laravel's default scaffolding.
149-
150-
if (! preg_match(self::JS_TRIX_LIBS_IMPORT_PATTERN, File::get($entrypoint))) {
151-
File::put($entrypoint, preg_replace(
152-
self::JS_BOOTSTRAP_IMPORT_PATTERN,
153-
str_replace(
154-
'%path%',
155-
$this->usingImportmaps() ? '' : './',
156-
<<<'JS'
157-
\1
158-
import '%path%libs/trix';
159-
JS,
160-
),
161-
File::get($entrypoint),
162-
));
142+
if (preg_match(self::JS_TRIX_LIBS_IMPORT_PATTERN, File::get($entrypoint))) {
143+
$this->components->info('Trix module was already imported.');
144+
145+
return;
163146
}
164-
}
165147

166-
private function ensureTrixOverridesStylesIsPublished(): void
167-
{
168-
$this->components->info('Publishing styles.');
169-
File::copy(__DIR__.'/../../stubs/resources/css/trix.css', resource_path('css/_trix.css'));
148+
$this->components->info(sprintf('Importing the Trix module in %s', str_replace(resource_path('/'), '', $entrypoint)));
170149

171-
if (File::exists($mainCssFile = resource_path('css/app.css')) && ! str_contains(File::get($mainCssFile), '_trix.css')) {
172-
$this->components->info('Importing the `resources/css/_trix.css` styles file.');
150+
File::prepend($entrypoint, str_replace('%path%', $this->usingImportmaps() ? '' : './', <<<'JS'
151+
import "%path%libs/trix";
173152

174-
File::prepend($mainCssFile, "@import './_trix.css';\n");
175-
} else {
176-
$this->components->warn('Import the `resources/css/_trix.css` in your main CSS file.');
177-
}
153+
JS));
178154
}
179155

180156
private function ensureTrixFieldComponentIsCopied(): void
@@ -191,18 +167,25 @@ private function ensureTrixFieldComponentIsCopied(): void
191167

192168
private function updateAppLayoutFiles(): void
193169
{
170+
$layouts = $this->existingLayoutFiles();
171+
172+
if ($layouts->isEmpty()) {
173+
$this->components->warn('Add the `<x-rich-text::styles />` component to your layouts.');
174+
175+
return;
176+
}
177+
194178
$this->components->info('Updating layouts.');
195179

196-
$this->existingLayoutFiles()
197-
->each(function ($file) {
198-
$contents = File::get($file);
180+
$layouts->each(function ($file) {
181+
$contents = File::get($file);
199182

200-
if (str_contains($contents, '<x-rich-text::styles />')) {
201-
return;
202-
}
183+
if (str_contains($contents, '<x-rich-text::styles />')) {
184+
return;
185+
}
203186

204-
File::put($file, preg_replace('/(\s*)(<\/head>)/', '\\1 <x-rich-text::styles theme="richtextlaravel" data-turbo-track="false" />\\1\\2', $contents));
205-
});
187+
File::put($file, preg_replace('/(\s*)(<\/head>)/', '\\1 <x-rich-text::styles theme="richtextlaravel" data-turbo-track="false" />\\1\\2', $contents));
188+
});
206189
}
207190

208191
private function existingLayoutFiles()

0 commit comments

Comments
 (0)