$key->fullPath); $this->$key->delete(); $this->$key = null; $key = Str::camel(str_replace('existing', '', $key)); } else { $fileService = new FileService($this->getFilePath($key)); } $fileService->delete(); $this->$key = null; if ($key == TypographyType::BODY_FONT->value) { foreach (TypographyType::defaultFonts() as $font) { if (! $this->{$font['key']}) { $this->dispatch('change-font', $font); } } } elseif ($key != TypographyType::BODY_FONT->value && $this->{TypographyType::BODY_FONT->value}) { $fileService = new FileService($this->getFilePath(TypographyType::BODY_FONT->value)); $this->dispatch('change-font', Arr::only(TypographyType::tryFrom($key)->defaultFont(), ['key', 'style', 'weight']) + ['src' => $fileService->getAssetUrl()]); } elseif ($key != TypographyType::BODY_FONT->value && $this->existingPrimaryFontRegular) { $fileService = new FileService($this->existingPrimaryFontRegular->fullPath); $this->dispatch('change-font', Arr::only(TypographyType::tryFrom($key)->defaultFont(), ['key', 'style', 'weight']) + ['src' => $fileService->getAssetUrl()]); } else { $this->dispatch('change-font', TypographyType::tryFrom($key)->defaultFont()); } } public function updated($key, $value) { try { $validated = $this->validateOnly($key); } catch (ValidationException $e) { $this->$key = null; throw $e; } $file = $validated[$key]->storeAs($this->getFilePath($key), options: ['visibility' => 'public', 'directory_visibility' => 'public']); $fileService = new FileService($file); if ($key == TypographyType::BODY_FONT->value) { foreach (TypographyType::defaultFonts() as $font) { if (! $this->{$font['key']} || $font['key'] == TypographyType::BODY_FONT->value) { $this->dispatch('change-font', Arr::only($font, ['key', 'style', 'weight']) + ['src' => $fileService->getAssetUrl()]); } } } else { $fontDefault = TypographyType::tryFrom($key)->defaultFont(); $this->dispatch('change-font', Arr::only($fontDefault, ['key', 'style', 'weight']) + ['src' => $fileService->getAssetUrl()]); } } public function save() { $this->validate(); $typographies = $this->site->typographies; if (! $typographies->count()) { $typographies = $this->site->typographies()->createMany([ ['type' => TypographyType::BODY_FONT], ['type' => TypographyType::BODY_BOLD_FONT], ['type' => TypographyType::BODY_ITALIC_FONT], ['type' => TypographyType::HEADING_FONT], ['type' => TypographyType::BUTTON_FONT], ]); } foreach ($typographies as $typography) { $key = Str::camel($typography->type->value); if ($attibute = $this->{$key}) { $originalName = $attibute->getClientOriginalName(); $file = $attibute->storeAs($this->getFilePath($key), options: ['visibility' => 'public', 'directory_visibility' => 'public']); $pathinfo = pathinfo($file); $extension = $pathinfo['extension']; if (in_array($extension, ['ttf', 'otf', 'woff'])) { $fileService = new FileService($file); $converted = $this->convert($key, $fileService->getStoragePath()); $pathinfo = pathinfo($converted); $originalName = str_replace($extension, 'woff2', $originalName); $fileService->delete(); } $typography->file()->updateOrCreate([ 'fileable_type' => $typography::class, 'fileable_id' => $typography->id, ], [ 'original_name' => $originalName, 'path' => $pathinfo['dirname'], 'filename' => $pathinfo['filename'], 'extension' => 'woff2', ]); } } $this->site->load('typographies'); $this->dispatch('save'); } public function rules(): array; public function messages(): array; public function validationAttributes(): array; protected function getFilePath($key, $extension = 'woff2'): string; protected function convert($key, $path): string; }