Skip to content

Instantly share code, notes, and snippets.

@Neeraj1005
Last active January 14, 2024 19:39
Show Gist options
  • Save Neeraj1005/7c652f144e6b1f6358a6490015406a81 to your computer and use it in GitHub Desktop.
Save Neeraj1005/7c652f144e6b1f6358a6490015406a81 to your computer and use it in GitHub Desktop.
Add CKEDITOR in Laravel-Livewire

textarea tag field

<div wire:ignore class="form-group row">
    <x-label class="col-md-3 col-form-label" for="message" :value="__('Compose message')" />
    <div class="col-md-9">
        <textarea wire:model="message" class="form-control required" name="message"
            id="message"></textarea>
        <x-error-message :value="__('message')" />
    </div>
</div>

CKEDITOR-4

<script src="https://cdn.ckeditor.com/4.16.1/full/ckeditor.js"></script>
<script>
    const editor = CKEDITOR.replace('message');
    
    # OR 1
    editor.on('change', function (event) {
        // console.log(event.editor.getData())
        @this.set('message', event.editor.getData());
    })
    
    # OR 2
    
     CKEDITOR.instances.message.on('change', function() {
          @this.set('message', this.getData());
      });
      
    # OR 3 now I don't want to render data in each request
     document.querySelector("#submit").addEventListener("click", () => {
        // console.log(editor.getData())
        @this.set('message', editor.getData());
    });
</script>

CKEDITOR-5

<script src="https://cdn.ckeditor.com/ckeditor5/27.1.0/classic/ckeditor.js"></script>
 <script>
    ClassicEditor
        .create(document.querySelector('#message'))
        .then(editor => {
            editor.model.document.on('change:data', () => {
                @this.set('message', editor.getData());
            })
        })
        .catch(error => {
            console.error(error);
        });
</script>

# optional if you want fire the event when submit button is clicked
<script>
 ClassicEditor
        .create(document.querySelector('#message'))
        .then(editor => {
            document.querySelector("#submit").addEventListener("click", () => {
                const textareaValue = $("#message").data("message");
                eval(textareaValue).set("message", editor.getData());
                // @this.set('message', editor.getData());
            });
        })
        .catch(error => {
            console.error(error);
        });

</script>

@salimkamboh
Copy link

salimkamboh commented May 27, 2023

I am trying to use same ckeditor 5 code. for a new form(create mode) its fine. But in edit form ckeditor is always empty. No text is shown there. Although if I display plain textarea it is always initialized.
Any hint?

@the-paulus
Copy link

@salimkamboh I believe that this example is simply a WYSIWYG text area component. So the @this.set('message', editor.getData()) would work. Ensure that the wire:mode the @this.set() match. Another thing that you can do is place {!! $message !!} in the textarea tag:

<div wire:ignore class="form-group row">
    <x-label class="col-md-3 col-form-label" for="message" :value="__('Compose message')" />
    <div class="col-md-9">
        <textarea wire:model="message" class="form-control required" name="message"
            id="message">{!! $message !!}</textarea>
        <x-error-message :value="__('message')" />
    </div>
</div>

I have a Livewire form component with three WYSIWYG textareas and placed all the JS code in a single JS file rather than the blade template.

app.js or whatever

import ClassicEditor from "@ckeditor/ckeditor5-build-classic";

ClassicEditor.create(document.querySelector('#message').then( (editor) => {
            editor.model.document.on('change:data', (e) => {
                let componentId = Livewire.components.getComponentsByName('my-form')[0].id
                Livewire.find(componentId).set('message', editor.getData())
            })
        })
        .catch(error => {
            console.error(error);
        })

my-form.blade.php

<div>
    <form wire:submit.prevent="submit">
        <div wire:ignore>
            <textarea wire:model.lazy="message" class="min-h-fit h-48" name="message" id="message" wire:key="wysiwyg-message">{!! $message !!}</textarea>
        </div>
<!-- snip -->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment