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

爷,独闯天下 提交于 2019-12-13 16:15:57

问题


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


来源:https://stackoverflow.com/questions/33799909/summernote-doesnt-allow-to-format-pasted-text-after-writing-onpaste-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!