I have a Livewire component that uses CKEditor 5 (also tested with 4).
I have based my adaptation on this post on Laracast forum and some others: https://laracasts.com/discuss/channels/laravel/how-to-bind-ckeditor-value-to-laravel-livewire-component
This component is reused throughout the admin panel it’s part of. This is part of a complex model that has many relationships that need to be saved individually so the controller methods don’t get jammed. The reason for Livewire.
I do have a button to save some of the properties that belong only to the parent model, but I am hoping to have all other relationships saved via Livewire and not adding any other save button. I don’t what the user to get confused.
Now this works but I am unable to get it to work with lazy or debounce. It fires a request on about every key strokes.
I have also tried a timeout, but this does only delay the requests. All requests get sent after the timeout anyway. I have tried to change the event name “change:data” to what I could find from CKEditor, that didn’t work.
Is there a way to either fix or get around it so it fires one request after x amount of time with the actual content at that time?
I am presently going through the CKEditor events documentation to try to find a working solution…
Also getting into Alpinejs... Probably the best choice. Not yet familiar with it.
Thanks,
Call the component
@livewire( "rich-text-input", [ "plant" => $plant, "relation" =>"description", "modelName" => "description" ] )
RichTextInput.php
class RichTextInput extends Component
{
public string $modelName;
public string $content;
public Plant $plant;
public string $relation;
public string $timeout;
public bool $disabled;
protected array $rules = [
"content" => "required|string"
];
public function mount( Plant $plant, $relation, $modelName )
{
$this->content = $plant->{$relation}()->first()->content ?? "";
$this->relation = $relation;
$this->modelName = $modelName;
$this->timeout = config( "website.settings.timeout" );
$this->disabled = !$plant->id;
}
public function updatedContent()
{
$this->validate();
$plant = $this->plant->{$this->relation}()->updateOrCreate(
["plant_id" => $this->plant->id],
[ "content" => $this->content, "lang" => "fr" ],
);
}
public function render()
{
return view('livewire.rich-text-input');
}
}
rich-text-input.blade.php
<div wire:ignore wire:model.lazy="{{ $modelName }}" wire:key="{{ $modelName }}" class="{{ $disabled ? "disabled" : "" }}" >
@csrf
<textarea id="{{ $modelName }}" name="{{ $modelName }}" >{{ $content }}</textarea>
<script>
ClassicEditor
.create( document.querySelector( '#{{ $modelName }}' ) )
.then( editor => {
@if ( $disabled )
editor.readOnly = true;
@else
editor.model.document.on( "change:data", async () => {
@this.set( 'content', editor.getData() );
})
@endif
})
.catch( error => {
console.error( error );
});
</script>
</div>