From 0583363f547f9ae5828dd4897e019fb9f300ef8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20R=C3=BCmmler?= Date: Tue, 25 Jun 2024 22:43:35 +0200 Subject: [PATCH 1/3] Add missing configuration methods to TranslatedText component --- .../Forms/Components/TranslatedText.php | 130 +++++++++++++++++- 1 file changed, 128 insertions(+), 2 deletions(-) diff --git a/packages/admin/src/Support/Forms/Components/TranslatedText.php b/packages/admin/src/Support/Forms/Components/TranslatedText.php index 8526e43888..da43baffbd 100644 --- a/packages/admin/src/Support/Forms/Components/TranslatedText.php +++ b/packages/admin/src/Support/Forms/Components/TranslatedText.php @@ -2,6 +2,7 @@ namespace Lunar\Admin\Support\Forms\Components; +use Closure; use Filament\Forms\ComponentContainer; use Filament\Forms\Components\Component; use Filament\Forms\Components\TextInput; @@ -16,6 +17,24 @@ class TranslatedText extends TextInput public bool $optionRichtext = false; + protected ?array $richtextToolbarButtons = null; + + protected array $richtextDisableToolbarButtons = []; + + protected bool $richtextDisableAllToolbarButtons = false; + + protected Closure|null|string $richtextFileAttachmentsDisk = null; + + protected Closure|null|string $richtextFileAttachmentsDirectory = null; + + protected Closure|string $richtextFileAttachmentsVisibility = 'public'; + + protected ?Closure $richtextGetUploadedAttachmentUrlUsing = null; + + protected ?Closure $richtextSaveUploadedFileAttachmentsUsing = null; + + protected bool $mergeExtraInputAttributes = false; + public Language $defaultLanguage; public Collection $components; @@ -37,12 +56,91 @@ public function prepareChildComponents() { $this->components = collect( $this->getLanguages()->map(fn ($lang) => $this->getOptionRichtext() ? - TranslatedRichEditor::make($lang->code)->statePath($lang->code) : - TranslatedTextInput::make($lang->code)->statePath($lang->code) + $this->getTranslatedRichEditorComponent($lang->code) : + $this->getTranslatedTextComponent($lang->code) ) ); } + protected function getTranslatedRichEditorComponent(string $langCode): TranslatedRichEditor + { + $component = TranslatedRichEditor::make($langCode) + ->statePath($langCode) + ->disableAllToolbarButtons($this->richtextDisableAllToolbarButtons) + ->fileAttachmentsVisibility($this->richtextFileAttachmentsVisibility) + ->fileAttachmentsDirectory($this->richtextFileAttachmentsDirectory) + ->fileAttachmentsDisk($this->richtextFileAttachmentsDisk) + ->getUploadedAttachmentUrlUsing($this->richtextGetUploadedAttachmentUrlUsing) + ->saveUploadedFileAttachmentsUsing($this->richtextSaveUploadedFileAttachmentsUsing); + + if (! empty($this->richtextToolbarButtons)) { + $component->disableToolbarButtons($this->richtextToolbarButtons); + } + + if ($this->richtextToolbarButtons !== null) { + $component->toolbarButtons($this->richtextToolbarButtons); + } + + return $this->prepareTranslatedTextComponent($component); + } + + public function extraInputAttributes(array | Closure $attributes, bool $merge = false): static + { + $this->mergeExtraInputAttributes = $merge; + + if ($merge) { + $this->extraInputAttributes[] = $attributes; + } else { + $this->extraInputAttributes = [$attributes]; + } + + return $this; + } + + protected function getTranslatedTextComponent(string $langCode): TranslatedTextInput + { + $component = TranslatedTextInput::make($langCode) + ->statePath($langCode) + ->telRegex($this->telRegex) + ->step($this->step); + + if ($this->isEmail) { + $component->email(); + } + + if ($this->isTel) { + $component->tel(); + } + + if ($this->isUrl) { + $component->url(); + } + + if ($this->isNumeric) { + $component->numeric(); + } + + if ($this->step === 1) { + $component->integer(); + } + + return $this->prepareTranslatedTextComponent($component); + } + + protected function prepareTranslatedTextComponent(TranslatedTextInput|TranslatedRichEditor $component): TranslatedTextInput|TranslatedRichEditor + { + $component + ->regex($this->regexPattern) + ->minLength($this->minLength) + ->maxLength($this->maxLength); + + if (!empty($this->extraInputAttributes)) { + $component->extraInputAttributes($this->extraInputAttributes, $this->mergeExtraInputAttributes); + } + + return $component; + } + public function prepareTranslateLocaleComponent(Component $component, string $locale) { $localeComponent = clone $component; @@ -83,6 +181,34 @@ public function getOptionRichtext(): bool return $this->optionRichtext; } + public function richtextToolbarButtons(array $buttons): static + { + $this->richtextToolbarButtons = $buttons; + + return $this; + } + + public function richtextDisableToolbarButtons(array $buttons): static + { + $this->richtextDisableToolbarButtons = $buttons; + + return $this; + } + + public function richtextDisableAllToolbarButtons(bool $condition = true): static + { + $this->richtextDisableAllToolbarButtons = $condition; + + return $this; + } + + public function richtextFileAttachmentsDirectory(string | Closure | null $name): static + { + $this->richtextFileAttachmentsDirectory = $name; + + return $this; + } + public function getExpanded() { return $this->expanded; From 2f2b63d116b466210d04cea850f389d86250ad54 Mon Sep 17 00:00:00 2001 From: Glenn Jacobs Date: Thu, 22 Aug 2024 14:34:35 +0100 Subject: [PATCH 2/3] Update TranslatedText.php --- .../src/Support/Forms/Components/TranslatedText.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/admin/src/Support/Forms/Components/TranslatedText.php b/packages/admin/src/Support/Forms/Components/TranslatedText.php index da43baffbd..afac822f2b 100644 --- a/packages/admin/src/Support/Forms/Components/TranslatedText.php +++ b/packages/admin/src/Support/Forms/Components/TranslatedText.php @@ -9,7 +9,7 @@ use Illuminate\Support\Collection; use Lunar\Models\Language; -class TranslatedText extends TextInput +class TranslatedText extends TextInput { protected string $view = 'lunarpanel::forms.components.translated-text'; @@ -209,17 +209,17 @@ public function richtextFileAttachmentsDirectory(string | Closure | null $name): return $this; } - public function getExpanded() + public function getExpanded(): bool { return $this->expanded; } - public function getDefaultLanguage() + public function getDefaultLanguage(): Language { return $this->languages->first(fn ($lang) => $lang->default); } - public function getMoreLanguages() + public function getMoreLanguages(): Collection { return $this->languages->filter(fn ($lang) => ! $lang->default); } @@ -229,7 +229,7 @@ public function getLanguageDefaults(): array return $this->getLanguages()->mapWithKeys(fn ($language) => [$language->code => ''])->toArray(); } - public function getLanguages() + public function getLanguages(): Collection { return $this->languages; } From 5fb4dc5417277584f9282ac3e26f5291c0df3f7c Mon Sep 17 00:00:00 2001 From: Glenn Jacobs Date: Thu, 22 Aug 2024 14:43:45 +0100 Subject: [PATCH 3/3] Update TranslatedText.php --- packages/admin/src/Support/Forms/Components/TranslatedText.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/admin/src/Support/Forms/Components/TranslatedText.php b/packages/admin/src/Support/Forms/Components/TranslatedText.php index afac822f2b..624ec8247a 100644 --- a/packages/admin/src/Support/Forms/Components/TranslatedText.php +++ b/packages/admin/src/Support/Forms/Components/TranslatedText.php @@ -9,7 +9,7 @@ use Illuminate\Support\Collection; use Lunar\Models\Language; -class TranslatedText extends TextInput +class TranslatedText extends TextInput { protected string $view = 'lunarpanel::forms.components.translated-text';