Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change enter behaviour in summernote

Tags:

summernote

I have get this old code

// myenter.js, enter key is binded to insertParagraph command.
    $.summernote.addPlugin({
      name : 'myenter',
      events : {
        // redefine insertParagraph 
        'insertParagraph' : function(event, editor, layoutInfo) {
      //you can use summernote enter 
      //layoutInfo.holder().summernote('insertParagraph');   

      // also you can use your enter key 
      layoutInfo.holder().summernote('insertNode', document.createTextNode("\r\n")); 

     // to stop enter key 
     //e.preventDefault();
    }
  }
});

$('.summernote').summernote({ height : 300 });

but now method of add plugin has changed and i want this functionality with new version by this code

$.extend($.summernote.plugins, {
    'myenter': function (context) {
        console.log('myenter');
    }
});

but it is not called at all

I had tried to get same functionality by summernote.onEnter and summernote.keyPress but it gives error..

like image 878
Zohaib Avatar asked Nov 02 '25 17:11

Zohaib


2 Answers

$.extend($.summernote.plugins, {
    'brenter': function (context) {
        this.events = {
            'summernote.enter': function (we, e) {
                // insert 2 br tags (if only one br tag is inserted the cursor won't go to the next line)
                document.execCommand('insertHTML', false, '<br><br>');
                e.preventDefault();
            }
        };
    }
}
like image 60
user1634824 Avatar answered Nov 04 '25 09:11

user1634824


I managed to fix it like this:

$('#summernote').summernote('destroy');

$.extend($.summernote.plugins, {
    'brenter': function (context) {
         this.events = {
            'summernote.enter': function (we, e) {
                //get hold of the enter event and trigger a shift+enter keypress

                e.trigger($.Event("keydown", {
                    keyCode: 13, // ENTER
                    shiftKey: true
                }));

                //stop the normal event from happening
                e.preventDefault();
             }
         };
     }
});

// then do summernote as normal...
$('#summernote').summernote({
like image 28
Colin Avatar answered Nov 04 '25 09:11

Colin



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!