1212
1313class 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