|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SolutionForest\InspireCms\Filament\Forms\Components; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Filament\Forms\Components\Actions\Action; |
| 7 | +use Filament\Forms\Components\RichEditor as BaseRichEditor; |
| 8 | +use Illuminate\Database\Eloquent\Model; |
| 9 | +use SolutionForest\InspireCms\InspireCmsConfig; |
| 10 | +use SolutionForest\InspireCms\Models\Contracts\Content; |
| 11 | +use SolutionForest\InspireCms\Support\MediaLibrary\Forms\Components\Concerns\InteractsWithMediaLibraryModal; |
| 12 | +use SolutionForest\InspireCms\Support\Models\Contracts\MediaAsset; |
| 13 | +use Spatie\MediaLibrary\MediaCollections\Models\Media; |
| 14 | + |
| 15 | +class RichEditor extends BaseRichEditor |
| 16 | +{ |
| 17 | + use InteractsWithMediaLibraryModal; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var view-string |
| 21 | + */ |
| 22 | + protected string $view = 'inspirecms::filament.forms.components.rich-editor'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array<string> |
| 26 | + */ |
| 27 | + protected array | Closure $toolbarButtons = [ |
| 28 | + 'attachFiles', |
| 29 | + 'blockquote', |
| 30 | + 'bold', |
| 31 | + 'bulletList', |
| 32 | + 'codeBlock', |
| 33 | + 'h2', |
| 34 | + 'h3', |
| 35 | + 'italic', |
| 36 | + 'link', |
| 37 | + 'orderedList', |
| 38 | + 'redo', |
| 39 | + 'strike', |
| 40 | + 'underline', |
| 41 | + 'undo', |
| 42 | + 'contentPicker', |
| 43 | + 'mediaPicker', |
| 44 | + ]; |
| 45 | + |
| 46 | + protected function setUp(): void |
| 47 | + { |
| 48 | + parent::setUp(); |
| 49 | + |
| 50 | + $this->extraAlpineAttributes([ |
| 51 | + 'x-init' => $this->getExtraTrixActionsRegEvent(), |
| 52 | + ]); |
| 53 | + |
| 54 | + $this->registerActions([ |
| 55 | + Action::make('selectContent') |
| 56 | + ->slideOver() |
| 57 | + ->fillForm(['selection' => []]) |
| 58 | + ->form([ |
| 59 | + ContentTree::make('selection')->hiddenLabel(), |
| 60 | + ]) |
| 61 | + ->action(function (array $data, self $component) { |
| 62 | + $component->getLivewire()->dispatch( |
| 63 | + 'content-picker-trix-appead', |
| 64 | + statePath: $component->getStatePath(), |
| 65 | + data: $component->formatContentPickerState($data['selection']), |
| 66 | + ); |
| 67 | + }), |
| 68 | + ]); |
| 69 | + |
| 70 | + $this->registerListeners([ |
| 71 | + 'mediaPicker::select' => [ |
| 72 | + function (self $component, string $statePath, $ids = null, $callback = null) { |
| 73 | + if ($statePath === $component->getStatePath() && ! empty($ids)) { |
| 74 | + $component->getLivewire()->dispatch( |
| 75 | + 'media-picker-trix-appead', |
| 76 | + statePath: $statePath, |
| 77 | + data: $component->formatMediaPickerState($ids), |
| 78 | + ); |
| 79 | + } |
| 80 | + }, |
| 81 | + ], |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + protected function getExtraTrixActionsRegEvent() |
| 86 | + { |
| 87 | + return <<<'JS' |
| 88 | + document.addEventListener("trix-action-invoke", function(event) { |
| 89 | + const { target, invokingElement, actionName } = event |
| 90 | + switch (actionName) { |
| 91 | + case 'x-content-picker': |
| 92 | + $dispatch('content-picker-trix-click'); |
| 93 | + break; |
| 94 | + case 'x-media-picker': |
| 95 | + $dispatch('media-picker-trix-click'); |
| 96 | + break; |
| 97 | + default: |
| 98 | + break; |
| 99 | + } |
| 100 | + }); |
| 101 | + JS; |
| 102 | + } |
| 103 | + |
| 104 | + public function formatMediaPickerState($state) |
| 105 | + { |
| 106 | + if (! is_array($state)) { |
| 107 | + $state = is_null($state) || empty($state) ? [] : [$state]; |
| 108 | + } |
| 109 | + if (empty($state)) { |
| 110 | + return []; |
| 111 | + } |
| 112 | + |
| 113 | + return InspireCmsConfig::getMediaAssetModelClass()::query() |
| 114 | + ->with(['media']) |
| 115 | + ->findMany($state) |
| 116 | + ->filter(fn ($record) => $record instanceof MediaAsset) |
| 117 | + ->sortBy(fn (Model $record) => array_search($record->getKey(), $state)) |
| 118 | + ->map(fn (Model | MediaAsset $record) => $this->mutateMediaPickerState($record)) |
| 119 | + ->all(); |
| 120 | + } |
| 121 | + |
| 122 | + public function formatContentPickerState($state) |
| 123 | + { |
| 124 | + if (! is_array($state)) { |
| 125 | + $state = is_null($state) || empty($state) ? [] : [$state]; |
| 126 | + } |
| 127 | + if (empty($state)) { |
| 128 | + return []; |
| 129 | + } |
| 130 | + |
| 131 | + return InspireCmsConfig::getContentModelClass()::query() |
| 132 | + ->findMany($state) |
| 133 | + ->filter(fn ($record) => $record instanceof Content) |
| 134 | + ->sortBy(fn (Model $record) => array_search($record->getKey(), $state)) |
| 135 | + ->map(fn (Model | Content $record) => $this->mutateContentPickerState($record))->all(); |
| 136 | + } |
| 137 | + |
| 138 | + protected function mutateMediaPickerState(Model | MediaAsset $mediaAsset) |
| 139 | + { |
| 140 | + /** @var null | Media */ |
| 141 | + $media = $mediaAsset->getFirstMedia(); |
| 142 | + $mediaUrl = $mediaAsset->getUrl(isAbsolute: false); |
| 143 | + $title = $media?->title ?? $mediaAsset->title; |
| 144 | + |
| 145 | + $data = [ |
| 146 | + 'contentType' => $media?->mime_type, |
| 147 | + 'filename' => $media?->file_name, |
| 148 | + 'href' => $mediaUrl, |
| 149 | + 'url' => $mediaUrl, |
| 150 | + 'id' => $mediaAsset->getKey(), |
| 151 | + 'title' => $title, |
| 152 | + 'caption' => $mediaAsset->caption, |
| 153 | + 'description' => $mediaAsset->description, |
| 154 | + 'name' => $mediaAsset->caption, |
| 155 | + ]; |
| 156 | + |
| 157 | + if ($mediaAsset->isImage()) { |
| 158 | + // Define conversion sizes with their widths |
| 159 | + $conversions = [ |
| 160 | + 'thumbnail' => 150, |
| 161 | + 'small' => 300, |
| 162 | + 'medium' => 600, |
| 163 | + 'large' => 1200, |
| 164 | + ]; |
| 165 | + |
| 166 | + // Build sizes attribute for responsive images |
| 167 | + $sizesAttr = collect([ |
| 168 | + '(max-width: 300px) 300px', |
| 169 | + '(max-width: 600px) 600px', |
| 170 | + '(max-width: 1200px) 1200px', |
| 171 | + '100vw', |
| 172 | + ])->implode(', '); |
| 173 | + |
| 174 | + // Generate URLs for each conversion |
| 175 | + $conversionUrls = collect($conversions) |
| 176 | + ->mapWithKeys(fn ($width, $conversion) => [ |
| 177 | + $conversion => $mediaAsset->getUrl($conversion, false), |
| 178 | + ]) |
| 179 | + ->all(); |
| 180 | + |
| 181 | + // Build srcset string |
| 182 | + $srcset = collect($conversions) |
| 183 | + ->map(function ($width, $conversion) use ($mediaAsset) { |
| 184 | + $url = $mediaAsset->getUrl($conversion, false); |
| 185 | + |
| 186 | + return "{$url} {$width}w"; |
| 187 | + }) |
| 188 | + ->implode(', '); |
| 189 | + |
| 190 | + // Create responsive image HTML with srcset |
| 191 | + $content = sprintf( |
| 192 | + '<img src="%s" alt="%s" srcset="%s" sizes="%s" loading="lazy" class="trix-attachment-image trix-attachment-mediapicker">', |
| 193 | + htmlspecialchars($mediaUrl), |
| 194 | + htmlspecialchars($title), |
| 195 | + htmlspecialchars($srcset), |
| 196 | + $sizesAttr |
| 197 | + ); |
| 198 | + |
| 199 | + $data['sizes'] = $conversionUrls; |
| 200 | + $data['content'] = $content; |
| 201 | + $data['srcset'] = $srcset; |
| 202 | + } |
| 203 | + |
| 204 | + return $data; |
| 205 | + } |
| 206 | + |
| 207 | + protected function mutateContentPickerState(Model | Content $content) |
| 208 | + { |
| 209 | + $livewire = $this->getLivewire(); |
| 210 | + $translatedLocale = null; |
| 211 | + foreach ([ |
| 212 | + 'getActiveLocale', |
| 213 | + 'getActiveFormsLocale', |
| 214 | + 'getLocale', |
| 215 | + ] as $method) { |
| 216 | + try { |
| 217 | + if (method_exists($livewire, $method)) { |
| 218 | + $translatedLocale = $livewire->{$method}(); |
| 219 | + |
| 220 | + break; |
| 221 | + } |
| 222 | + } catch (\Throwable $th) { |
| 223 | + $translatedLocale = null; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + if (! is_string($translatedLocale) || empty($translatedLocale)) { |
| 228 | + $translatedLocale = app()->getLocale(); |
| 229 | + } |
| 230 | + |
| 231 | + $url = $content->getUrl($translatedLocale); |
| 232 | + |
| 233 | + $relativeUrl = str($url) |
| 234 | + ->replaceFirst(url(''), '') |
| 235 | + ->trim()->trim('/') |
| 236 | + ->prepend('/') |
| 237 | + ->toString(); |
| 238 | + |
| 239 | + $template = ' <a href="%s" class="trix-attachment-contentpicker" data-id="%s" data-slug="%s">%s</a> '; |
| 240 | + |
| 241 | + return sprintf( |
| 242 | + $template, |
| 243 | + htmlspecialchars($relativeUrl), |
| 244 | + htmlspecialchars($content->getKey()), |
| 245 | + htmlspecialchars($content->slug), |
| 246 | + htmlspecialchars($content->title) |
| 247 | + ); |
| 248 | + } |
| 249 | +} |
0 commit comments