55use Backstage \Fields \Contracts \FieldContract ;
66use Backstage \Fields \Enums \ToolbarButton ;
77use Backstage \Fields \Models \Field ;
8- use Backstage \Fields \Services \ContentCleaningService ;
9- use Filament \Forms ;
108use Filament \Forms \Components \RichEditor as Input ;
119use Filament \Forms \Components \Select ;
1210use Filament \Schemas \Components \Grid ;
@@ -21,9 +19,6 @@ public static function getDefaultConfig(): array
2119 ...parent ::getDefaultConfig (),
2220 'toolbarButtons ' => ['attachFiles ' , 'blockquote ' , 'bold ' , 'bulletList ' , 'codeBlock ' , 'h2 ' , 'h3 ' , 'italic ' , 'link ' , 'orderedList ' , 'redo ' , 'strike ' , 'underline ' , 'undo ' ],
2321 'disableToolbarButtons ' => [],
24- 'autoCleanContent ' => true ,
25- 'preserveCustomCaptions ' => false ,
26- 'hideCaptions ' => true ,
2722 ];
2823 }
2924
@@ -32,7 +27,6 @@ public static function make(string $name, ?Field $field = null): Input
3227 $ input = self ::createBaseInput ($ name , $ field );
3328 $ input = self ::configureToolbarButtons ($ input , $ field );
3429 $ input = self ::configureStateHandling ($ input , $ name );
35- $ input = self ::configureCaptions ($ input , $ field );
3630
3731 return $ input ;
3832 }
@@ -66,17 +60,6 @@ private static function configureStateHandling(Input $input, string $name): Inpu
6660 });
6761 }
6862
69- private static function configureCaptions (Input $ input , ?Field $ field ): Input
70- {
71- $ hideCaptions = $ field ->config ['hideCaptions ' ] ?? self ::getDefaultConfig ()['hideCaptions ' ];
72-
73- if ($ hideCaptions ) {
74- $ input ->extraAttributes (['data-hide-captions ' => 'true ' ]);
75- }
76-
77- return $ input ;
78- }
79-
8063 private static function formatRichEditorState ($ state )
8164 {
8265 if (empty ($ state )) {
@@ -130,55 +113,13 @@ private static function cleanContentArray(array $state): array
130113 return $ state ;
131114 }
132115
133- public static function cleanRichEditorState ($ state , array $ options = [])
134- {
135- if (empty ($ state )) {
136- return '' ;
137- }
138-
139- $ cleanedState = ContentCleaningService::cleanContent ($ state , $ options );
140-
141- return $ cleanedState ;
142- }
143-
144116 public static function mutateBeforeSaveCallback ($ record , $ field , array $ data ): array
145117 {
146118 $ data = self ::ensureRichEditorDataFormat ($ record , $ field , $ data );
147119
148- if (self ::shouldAutoCleanContent ($ field )) {
149- $ data = self ::applyContentCleaning ($ record , $ field , $ data );
150- }
151-
152120 return $ data ;
153121 }
154122
155- private static function shouldAutoCleanContent ($ field ): bool
156- {
157- return $ field ->config ['autoCleanContent ' ] ?? self ::getDefaultConfig ()['autoCleanContent ' ];
158- }
159-
160- private static function applyContentCleaning ($ record , $ field , array $ data ): array
161- {
162- $ options = self ::getCleaningOptions ($ field );
163-
164- if (isset ($ data ['values ' ][$ field ->ulid ])) {
165- // Called from ContentResource
166- $ data ['values ' ][$ field ->ulid ] = self ::cleanRichEditorState ($ data ['values ' ][$ field ->ulid ], $ options );
167- } elseif (isset ($ data [$ record ->valueColumn ][$ field ->ulid ])) {
168- // Called from CanMapDynamicFields trait
169- $ data [$ record ->valueColumn ][$ field ->ulid ] = self ::cleanRichEditorState ($ data [$ record ->valueColumn ][$ field ->ulid ], $ options );
170- }
171-
172- return $ data ;
173- }
174-
175- private static function getCleaningOptions ($ field ): array
176- {
177- return [
178- 'preserveCustomCaptions ' => $ field ->config ['preserveCustomCaptions ' ] ?? self ::getDefaultConfig ()['preserveCustomCaptions ' ],
179- ];
180- }
181-
182123 private static function ensureRichEditorDataFormat ($ record , $ field , array $ data ): array
183124 {
184125 $ data = self ::normalizeContentResourceValue ($ data , $ field );
@@ -239,21 +180,6 @@ public function getForm(): array
239180 ->multiple ()
240181 ->options (ToolbarButton::array ())
241182 ->columnSpanFull (),
242- Forms \Components \Toggle::make ('config.autoCleanContent ' )
243- ->label (__ ('Auto-clean content ' ))
244- ->helperText (__ ('Automatically remove figcaption and unwrap images from links ' ))
245- ->inline (false )
246- ->columnSpanFull (),
247- Forms \Components \Toggle::make ('config.preserveCustomCaptions ' )
248- ->label (__ ('Preserve custom captions ' ))
249- ->helperText (__ ('Only remove default captions, keep custom ones ' ))
250- ->inline (false )
251- ->columnSpanFull (),
252- Forms \Components \Toggle::make ('config.hideCaptions ' )
253- ->label (__ ('Hide caption fields ' ))
254- ->helperText (__ ('Hide the caption input field that appears when uploading images ' ))
255- ->inline (false )
256- ->columnSpanFull (),
257183 ]),
258184 ]),
259185 ])->columnSpanFull (),
0 commit comments