timo-de-winter / filament-monaco-editor
A package to implement the monaco editor into a filament project
Fund package maintenance!
timo-de-winter
Requires
- php: ^8.3
- filament/filament: ^3.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- scssphp/scssphp: ^2.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
README
A package to implement the monaco editor into a filament project. Including a morphable model to relate code to any model. Obviously, you can use only the editor without publishing and running the migrations for the morphable model.
Installation
You can install the package via composer:
composer require timo-de-winter/filament-monaco-editor
You can publish and run the migrations with:
php artisan vendor:publish --tag="filament-monaco-editor-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="filament-monaco-editor-config"
This is the contents of the published config file:
return [ 'table' => 'editor_codes', ];
Optionally, you can publish the views using
php artisan vendor:publish --tag="filament-monaco-editor-views"
Usage
You can use the monaco editor directly in a form.
public static function form(Form $form): Form { return $form->schema([ \TimoDeWinter\FilamentMonacoEditor\Filament\Forms\Components\MonacoEditor::make('code') ->language('php') ->height('500px'), ]); }
Code compilation
This package also comes with features to compile code. At this moment we support compilation of the following:
- SCSS -> CSS
\TimoDeWinter\FilamentMonacoEditor\Facades\FilamentMonacoEditor::compileScssToCss('your-css');
Use as action
The package also comes with an action that you can add to your resources or pages.
In order to do this you should first add the following interface and trait to the model you want to use this on.
class YourModel extends Model implements \TimoDeWinter\FilamentMonacoEditor\Contracts\HasMonacoEditor { use \TimoDeWinter\FilamentMonacoEditor\Concerns\InteractsWithMonacoEditor; }
After doing that you can use both a table action and a default action.
protected function getHeaderActions(): array { return [ \TimoDeWinter\FilamentMonacoEditor\Filament\Actions\MonacoAction::make() ->language('php'), ]; } // Table action ->actions([ MonacoAction::make() ->language('php'), ])
By default, the code will be stored in the database under a specific collection. When not explicitly setting a collection we fall back to the language you use for the editor. However, you can explicitly set the collection as well (for example when you want to add the same language twice on the same model):
protected function getHeaderActions(): array { return [ \TimoDeWinter\FilamentMonacoEditor\Filament\Actions\MonacoAction::make() ->collection('client-side-code') ->label('Client side code') ->language('javascript'), \TimoDeWinter\FilamentMonacoEditor\Filament\Actions\MonacoAction::make() ->collection('server-side-code') ->label('Server side code') ->language('php'), ]; }
Using a grid within the actions
It is possible to use a (codepen like) grid in your actions by defining the collection as an array where key=collection and value=language.
protected function getHeaderActions(): array { return [ \TimoDeWinter\FilamentMonacoEditor\Filament\Actions\MonacoAction::make() ->collection([ 'client-side-code' => 'javascript', 'server-side-code' => 'php', ]), ]; }
If you want to set a default state for the different collections in the grid-style action you can do so like this:
protected function getHeaderActions(): array { return [ \TimoDeWinter\FilamentMonacoEditor\Filament\Actions\MonacoAction::make() ->collection([ 'client-side-code' => 'javascript', 'server-side-code' => 'php', ]) ->default([ 'client-side-code' => 'Very cool code', 'server-side-code' => 'Cool PHP code', ]), ]; }
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.