Search code examples
javascriptsummernote

summernote doesn't allow to format pasted text after writing onpaste event


my issue is that, when i paste data in summernote is clears the formatting but if i want to format the pasted text as i want it doesn't allow me to make changes in pasted text nor in the text i m writing in editor. i have used this code for binding onpaste event to summernote.

I am Using summernote.js version 0.5.8

    $(".summernote").summernote({
            onpaste: function (e) {
            debugger;
            var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
            e.preventDefault();
            setTimeout(function () {
                document.execCommand('insertHTML', false, bufferText);
            }, 10);
        }
    })

Solution

  • Got the solution to my problem.I had to destroy the object of Summernote and reinitialize it. I am not sure whether it is a right way of coding or not but it worked for me. I did this on layout page as I am using Summernote various different places in my project

        $(".summernote").summernote({ height: 250 });
    
            $(".summernote").summernote({
                onpaste: function (e) {
                    var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
                    e.preventDefault();
                    setTimeout(function () {
                        document.execCommand('insertText', false, bufferText);
                        $(this).parent().siblings('.summernote').destroy();                        
                    }, 10);
                }
            });