简体   繁体   中英

Not able to attach event handler to ClassicEditor

I am using CKEditor to allow the user to style post message and I am using the following javascript code to configure my ClassicEditor:

ClassicEditor
.create($("#post-message")[0], {
    language: $("html").attr("lang"),
    alignment: {
        options: ['left', 'right']
    },
    toolbar: ["undo", "redo", "bold", "italic", "blockQuote", "link", "numberedList", "bulletedList"],
    autoParagraph : false
})
.then(editor => {
    myEditor = editor;
    myEditor.model.document.on('change', (event) => {
        $("#post-message").val(myEditor.getData());
        if (validatePostMessage() == false) {
            $(".ck-content").css("border-color", "red");
        }
        else {
            $(".ck-content").css("border-color", "rgb(196, 196, 196)")
        }

    });
     //this one not working
    myEditor.model.document.on('blur', (event) => {
        alert("hi");

    });
})
.catch(error => {
    console.error(error);
});

The above code working when change event occurs but blur event not working.

<textarea type="text"
  class="form-control"
  maxlength="2000"
  id="post-message"
  name="post-message">
</textarea>

My question why blur event handler not working when my editor loses focus?

PS:

I am using the following version:

https://cdn.ckeditor.com/ckeditor5/12.3.1/classic/ckeditor.js

You can use focus tracking in the editor to determine if it is a focus or blur.

Example from documentation:

editor.editing.view.document.on('change:isFocused', ( evt, data, isFocused ) => {
    console.log( `View document is focused: ${ isFocused }.` );
});

So, for your use-case, it would be:

editor.editing.view.document.on('change:isFocused', ( evt, data, isFocused ) => {
    if ( isFocused == false ) // blur
    {
        alert('blur');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM