summernote doesn't allow to format pasted text

2019-07-17 13:49发布

问题:

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);
        }
    })

回答1:

In html file use on-paste attribute

<div summernote="rules" ng-model="league.rules" validate-summernote="checkLeagueRules" config="summernote_options" on-paste="removeFormatting(evt)" class="summernote"></div>

In controller use removeFormatting function

$scope.removeFormatting = function(e)
{ 
    var bufferText = ((e.originalEvent || e).clipboardData || window.clipboardData).getData('Text');
    e.preventDefault();
    // Firefox fix
    setTimeout(function () {            
        document.execCommand('insertText', false, bufferText);
    }, 10);  
}


回答2:

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);
            }
        });