|
| 1 | +--- |
| 2 | +name: ace-editor-field-development |
| 3 | +description: Build and work with the Filament Ace Editor Field plugin, including code editor forms, syntax highlighting modes, themes, dark mode support, and editor configuration. |
| 4 | +--- |
| 5 | + |
| 6 | +# Ace Editor Field Development |
| 7 | + |
| 8 | +## When to use this skill |
| 9 | + |
| 10 | +Use this skill when: |
| 11 | +- Adding a code editor field to a Filament form |
| 12 | +- Configuring syntax highlighting modes (HTML, PHP, JavaScript, CSS, etc.) |
| 13 | +- Customizing Ace editor themes and dark mode behavior |
| 14 | +- Setting editor dimensions, options, and extensions |
| 15 | +- Troubleshooting Ace editor rendering or asset loading issues |
| 16 | + |
| 17 | +## Architecture |
| 18 | + |
| 19 | +### Namespace |
| 20 | +``` |
| 21 | +JeffersonGoncalves\Filament\AceEditorField |
| 22 | +``` |
| 23 | + |
| 24 | +### Key Classes |
| 25 | + |
| 26 | +| Class | Path | Description | |
| 27 | +|-------|------|-------------| |
| 28 | +| `AceEditorInput` | `src/Forms/Components/AceEditorInput.php` | Main form field component extending `Filament\Forms\Components\Field` | |
| 29 | +| `AceEditorFieldServiceProvider` | `src/AceEditorFieldServiceProvider.php` | Service provider that registers views, config, and assets | |
| 30 | + |
| 31 | +### Dependencies |
| 32 | +- `filament/filament: ^5.0` |
| 33 | +- `spatie/laravel-package-tools: ^1.14.0` |
| 34 | + |
| 35 | +## Configuration |
| 36 | + |
| 37 | +### Publish Config |
| 38 | + |
| 39 | +```bash |
| 40 | +php artisan vendor:publish --tag=filament-ace-editor-field-config |
| 41 | +``` |
| 42 | + |
| 43 | +The config file `filament-ace-editor-field.php` contains: |
| 44 | +- `base_url` - Base URL for Ace editor CDN assets |
| 45 | +- `file` - Ace editor main JS file path |
| 46 | +- `extensions` - Available Ace extensions map |
| 47 | +- `enabled_extensions` - Extensions enabled by default |
| 48 | +- `editor_config` - Default editor configuration |
| 49 | +- `editor_options` - Default Ace editor options |
| 50 | +- `dark_mode.enable` - Whether dark mode theme switching is enabled |
| 51 | +- `dark_mode.theme` - Theme to use in dark mode |
| 52 | + |
| 53 | +## Features |
| 54 | + |
| 55 | +### Basic Code Editor |
| 56 | + |
| 57 | +```php |
| 58 | +use JeffersonGoncalves\Filament\AceEditorField\Forms\Components\AceEditorInput; |
| 59 | + |
| 60 | +AceEditorInput::make('code') |
| 61 | + ->mode('php') |
| 62 | + ->theme('monokai') |
| 63 | + ->height('20rem') |
| 64 | + ->required(); |
| 65 | +``` |
| 66 | + |
| 67 | +### Setting Language Mode |
| 68 | + |
| 69 | +The `mode()` method sets the Ace editor language mode. It automatically prepends `ace/mode/`: |
| 70 | + |
| 71 | +```php |
| 72 | +AceEditorInput::make('content') |
| 73 | + ->mode('html'); // Sets ace/mode/html |
| 74 | + |
| 75 | +AceEditorInput::make('script') |
| 76 | + ->mode('javascript'); // Sets ace/mode/javascript |
| 77 | + |
| 78 | +AceEditorInput::make('styles') |
| 79 | + ->mode('css'); // Sets ace/mode/css |
| 80 | +``` |
| 81 | + |
| 82 | +### Setting Themes |
| 83 | + |
| 84 | +The `theme()` method sets the light mode theme. It automatically prepends `ace/theme/`: |
| 85 | + |
| 86 | +```php |
| 87 | +AceEditorInput::make('code') |
| 88 | + ->theme('github'); // Sets ace/theme/github |
| 89 | +``` |
| 90 | + |
| 91 | +### Dark Mode Support |
| 92 | + |
| 93 | +Configure a separate theme for dark mode, or disable automatic dark mode switching: |
| 94 | + |
| 95 | +```php |
| 96 | +// Set a dark mode theme |
| 97 | +AceEditorInput::make('code') |
| 98 | + ->theme('chrome') |
| 99 | + ->darkTheme('twilight'); |
| 100 | + |
| 101 | +// Disable dark mode theme switching entirely |
| 102 | +AceEditorInput::make('code') |
| 103 | + ->theme('monokai') |
| 104 | + ->disableDarkTheme(); |
| 105 | +``` |
| 106 | + |
| 107 | +### Custom Editor Options |
| 108 | + |
| 109 | +Merge additional Ace editor options (these are passed directly to the Ace API): |
| 110 | + |
| 111 | +```php |
| 112 | +AceEditorInput::make('code') |
| 113 | + ->mode('php') |
| 114 | + ->editorOptions([ |
| 115 | + 'fontSize' => 14, |
| 116 | + 'showGutter' => true, |
| 117 | + 'showPrintMargin' => false, |
| 118 | + 'tabSize' => 4, |
| 119 | + 'useSoftTabs' => true, |
| 120 | + 'wrap' => true, |
| 121 | + ]); |
| 122 | +``` |
| 123 | + |
| 124 | +### Editor Configuration |
| 125 | + |
| 126 | +```php |
| 127 | +AceEditorInput::make('code') |
| 128 | + ->editorConfig([ |
| 129 | + 'basePath' => '/custom/ace/path', |
| 130 | + ]); |
| 131 | +``` |
| 132 | + |
| 133 | +### Adding Extensions |
| 134 | + |
| 135 | +```php |
| 136 | +AceEditorInput::make('code') |
| 137 | + ->addExtensions(['emmet', 'language_tools']); |
| 138 | + |
| 139 | +// With a custom callback |
| 140 | +AceEditorInput::make('code') |
| 141 | + ->addExtensions(['emmet'], function ($existing, $new) { |
| 142 | + return $existing->merge($new)->unique(); |
| 143 | + }); |
| 144 | +``` |
| 145 | + |
| 146 | +### Sizing |
| 147 | + |
| 148 | +```php |
| 149 | +AceEditorInput::make('code') |
| 150 | + ->height('30rem') // Set height (default: '16rem') |
| 151 | + ->cols(80) // Set columns |
| 152 | + ->rows(20) // Set rows |
| 153 | + ->autosize(); // Enable auto-sizing |
| 154 | +``` |
| 155 | + |
| 156 | +### Read-Only Mode |
| 157 | + |
| 158 | +The editor automatically enters read-only mode when the field is disabled: |
| 159 | + |
| 160 | +```php |
| 161 | +AceEditorInput::make('code') |
| 162 | + ->mode('json') |
| 163 | + ->disabled(); |
| 164 | +``` |
| 165 | + |
| 166 | +## Internal Behavior |
| 167 | + |
| 168 | +### State Management |
| 169 | + |
| 170 | +- `afterStateHydrated`: Sets the component state from the model value |
| 171 | +- `afterStateUpdated`: Validates the field path on the Livewire component |
| 172 | +- `initializeConfigurations()`: Reads all defaults from the published config file on `setUp()` |
| 173 | + |
| 174 | +### Editor Options Resolution |
| 175 | + |
| 176 | +`getEditorOptions()` merges custom options with a `readOnly` flag derived from `$this->isDisabled()`. |
| 177 | + |
| 178 | +## Troubleshooting |
| 179 | + |
| 180 | +### Editor Not Rendering |
| 181 | +**Cause**: Assets not loaded or Alpine component not registered. |
| 182 | +**Solution**: Ensure the service provider is auto-discovered. Clear caches with `php artisan filament:assets` and `php artisan view:clear`. |
| 183 | + |
| 184 | +### Syntax Highlighting Not Working |
| 185 | +**Cause**: Incorrect mode name. |
| 186 | +**Solution**: Use the Ace mode name without the `ace/mode/` prefix (e.g., `'php'` not `'ace/mode/php'`). The `mode()` method adds the prefix automatically. |
| 187 | + |
| 188 | +### Dark Theme Not Switching |
| 189 | +**Cause**: Dark mode disabled in config or `disableDarkTheme()` called. |
| 190 | +**Solution**: Check `filament-ace-editor-field.dark_mode.enable` in config, or ensure `disableDarkTheme()` is not called on the component. |
| 191 | + |
| 192 | +### Extensions Not Loading |
| 193 | +**Cause**: Extension not defined in config file. |
| 194 | +**Solution**: Publish the config and ensure the extension is listed in the `extensions` array with its CDN URL. |
0 commit comments