|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backstage\Fields\Fields; |
| 4 | + |
| 5 | +use Backstage\Fields\Contracts\FieldContract; |
| 6 | +use Backstage\Fields\Enums\ToolbarButton; |
| 7 | +use Backstage\Fields\Models\Field; |
| 8 | +use Filament\Forms; |
| 9 | +use Filament\Forms\Components\RichEditor as Input; |
| 10 | + |
| 11 | +class MarkdownEditor extends Base implements FieldContract |
| 12 | +{ |
| 13 | + public static function getDefaultConfig(): array |
| 14 | + { |
| 15 | + return [ |
| 16 | + ...parent::getDefaultConfig(), |
| 17 | + 'toolbarButtons' => ['attachFiles', 'blockquote', 'bold', 'bulletList', 'codeBlock', 'heading', 'italic', 'link', 'orderedList', 'redo', 'strike', 'table', 'undo'], |
| 18 | + 'disableToolbarButtons' => [], |
| 19 | + 'fileAttachmentsDisk' => 'public', |
| 20 | + 'fileAttachmentsDirectory' => 'attachments', |
| 21 | + 'fileAttachmentsVisibility' => 'public', |
| 22 | + ]; |
| 23 | + } |
| 24 | + |
| 25 | + public static function make(string $name, ?Field $field = null): Input |
| 26 | + { |
| 27 | + $input = self::applyDefaultSettings(Input::make($name), $field); |
| 28 | + |
| 29 | + $input = $input->label($field->name ?? null) |
| 30 | + ->toolbarButtons($field->config['toolbarButtons'] ?? self::getDefaultConfig()['toolbarButtons']) |
| 31 | + ->disableToolbarButtons($field->config['disableToolbarButtons'] ?? self::getDefaultConfig()['disableToolbarButtons']) |
| 32 | + ->fileAttachmentsDisk($field->config['fileAttachmentsDisk'] ?? self::getDefaultConfig()['fileAttachmentsDisk']) |
| 33 | + ->fileAttachmentsDirectory($field->config['fileAttachmentsDirectory'] ?? self::getDefaultConfig()['fileAttachmentsDirectory']) |
| 34 | + ->fileAttachmentsVisibility($field->config['fileAttachmentsVisibility'] ?? self::getDefaultConfig()['fileAttachmentsVisibility']); |
| 35 | + |
| 36 | + return $input; |
| 37 | + } |
| 38 | + |
| 39 | + public function getForm(): array |
| 40 | + { |
| 41 | + return [ |
| 42 | + Forms\Components\Tabs::make() |
| 43 | + ->schema([ |
| 44 | + Forms\Components\Tabs\Tab::make('General') |
| 45 | + ->label(__('General')) |
| 46 | + ->schema([ |
| 47 | + ...parent::getForm(), |
| 48 | + ]), |
| 49 | + Forms\Components\Tabs\Tab::make('Field specific') |
| 50 | + ->label(__('Field specific')) |
| 51 | + ->schema([ |
| 52 | + Forms\Components\Grid::make(2) |
| 53 | + ->schema([ |
| 54 | + Forms\Components\Select::make('config.toolbarButtons') |
| 55 | + ->label(__('Toolbar buttons')) |
| 56 | + ->default(['attachFiles', 'blockquote', 'bold', 'bulletList', 'codeBlock', 'heading', 'italic', 'link', 'orderedList', 'redo', 'strike', 'table', 'undo']) |
| 57 | + ->default(ToolbarButton::array()) // Not working in Filament yet. |
| 58 | + ->multiple() |
| 59 | + ->options(ToolbarButton::array()) |
| 60 | + ->columnSpanFull(), |
| 61 | + Forms\Components\Grid::make(3) |
| 62 | + ->schema([ |
| 63 | + Forms\Components\TextInput::make('config.fileAttachmentsDisk') |
| 64 | + ->label(__('File attachments disk')) |
| 65 | + ->default('public'), |
| 66 | + Forms\Components\TextInput::make('config.fileAttachmentsDirectory') |
| 67 | + ->label(__('File attachments directory')) |
| 68 | + ->default('attachments'), |
| 69 | + Forms\Components\TextInput::make('config.fileAttachmentsVisibility') |
| 70 | + ->label(__('File attachments visibility')) |
| 71 | + ->default('public'), |
| 72 | + ]), |
| 73 | + ]), |
| 74 | + ]), |
| 75 | + ])->columnSpanFull(), |
| 76 | + ]; |
| 77 | + } |
| 78 | +} |
0 commit comments