Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CKEditor5 Custom Modal Plugin

I followed the initial plugin tutorial and got the Image Insert to work, but I would like to display a custom modal with two input fields instead of the prompt to set some more attributes. How would I best implement this?

I know how to implement a normal modal in plain JS/CSS but I am a bit confused as to where to put the HTML for the modal to be displayed on the button click.

class Test extends Plugin {
init() {
    editor = this.editor
    editor.ui.componentFactory.add('SampleView', (locale) => {

        const view = new ButtonView(locale)

        view.set({
            label: 'test',
            icon: imageIcon,
            tooltip: true
        })
        view.on('execute', () => {
     //here I would like to open the modal instead of the prompt
        })

    })
  }
}
like image 327
user10109650 Avatar asked Nov 24 '25 11:11

user10109650


1 Answers

For example, you can try to use SweetAlert2 which is zero-dependency pure javascript replacement for default popups.

import swal from 'sweetalert2';
...
view.on( 'execute', () => {
    swal( {
        input: 'text',
        inputPlaceholder: 'Your image URL'
    } )
    .then ( result => {
        editor.model.change( writer => {
            const imageElement = writer.createElement( 'image', {
                src: result.value
            } );

            editor.model.insertContent( imageElement, editor.model.document.selection );
        } );
    } )
} );
like image 80
k0n0pka Avatar answered Nov 27 '25 00:11

k0n0pka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!