Skip to content
Loading
how to add custom button in summernote editor
+1
  • Sachin Singh
    please share how are you instantiating the popup for example for bootstrap model
    it simple be
    1. $("#myModel").model('show');  
    +2
  • I want to one popupon click of custom button.
    I am not able to call a function inside click event of custom button.
    i have tried with " this " keyword but I am getting error of can not able to read property of undefined.
    +1
  • i already test without if statement but show same error.
    ERROR TypeError: Cannot read property 'top' of undefined
    at t.value
    1. HelloButton = (context) => {  
    2. const ui = $.summernote.ui;  
    3. // create button  
    4. const button = ui.button({  
    5. contents: ' Hello',  
    6. tooltip: 'hello',  
    7. click: () => {  
    8. context.invoke('editor.insertText', 'hello');  
    9. }  
    10. });  
    11. return button.render();  
    12. }
     
    +1
  • Sachin Singh
    test without using if statement and keeping toolbar simple
    1. HelloButton = (context) => {  
    2. const ui = $.summernote.ui;  
    3. // create button  
    4. const button = ui.button({  
    5. contents: ' Hello',  
    6. tooltip: 'hello',  
    7. click: () => {  
    8. context.invoke('editor.insertText', 'hello');  
    9. }  
    10. });  
    11. return button.render();  
    12. }  
    13. }  
    14.    
    15. // Summer Note  
    16. $(document).ready(() => {  
    17.   
    18. $('.summernote').summernote({  
    19.   toolbar: [  
    20.     ['mybutton', ['hello']]  
    21.   ],  
    22.   
    23.   buttons: {  
    24.     hello: HelloButton   
    25.   }  
    26. });  
    27.   
    28. callbacks: {  
    29. onChange: () => {  
    30. this.toasterService.success($('.summernote').summernote('code'));  
    31. }  
    32. }  
    33. });  
    34. }); 
     
    +3
  • I already use ready function..
    1. HelloButton = (context) => {  
    2. const ui = $.summernote.ui;  
    3. if (ui) {  
    4. // create button  
    5. const button = ui.button({  
    6. contents: ' Hello',  
    7. tooltip: 'hello',  
    8. click: () => {  
    9. context.invoke('editor.insertText', 'hello');  
    10. }  
    11. });  
    12. return button.render();  
    13. }  
    14. }  
    15. // Summer Note  
    16. $(document).ready(() => {  
    17. $(".summernote").summernote({  
    18. height: 200,  
    19. toolbar: [  
    20. ['style', ['bold', 'italic', 'underline', 'clear']],  
    21. ['mybutton', ['hello']]  
    22. ],  
    23. buttons: {  
    24. hello: this.HelloButton  
    25. },  
    26. callbacks: {  
    27. onChange: () => {  
    28. this.toasterService.success($('.summernote').summernote('code'));  
    29. }  
    30. }  
    31. });  
    32. });  
    +2
  • Sachin Singh
    The error may be causing due to the precedence , that is why i have used ready function so that the summernote could initialize before loading the dom.
    check how are you invoking the function or find out how can you use ready in angular.
    +3
  • I tried this function but I'm getting following error on hover of custom button
    ERROR TypeError: Cannot read property 'top' of undefined
    at t.value
     
    +2
  • Sachin Singh
    This is how we add buttons to summernote with click event handler
    1. var HightlightButton = function(context) {  
    2.   var ui = $.summernote.ui;  
    3.   var button = ui.button({  
    4.     contents: ' Highlight3',  
    5.     tooltip: 'Highlight text with red color',  
    6.     click: function() {  
    7.       context.invoke('editor.foreColor', 'red');  
    8. //  do whatever you want here like open popup  
    9.     }  
    10.   });  
    11.   
    12.   return button.render();  
    13. }  
    14.   
    15. $(document).ready(function() {  
    16.   $('#summernote').summernote({  
    17.     toolbar: [  
    18.       ['style', ['highlight', 'bold', 'italic', 'underline', 'clear']],  
    19.       ['font', ['strikethrough', 'superscript', 'subscript']],  
    20.       ['para', ['ul', 'ol', 'paragraph']],  
    21.       ['view', ['codeview']]  
    22.     ],  
    23.     buttons: {  
    24.       highlight: HightlightButton,  
    25.     }  
    26.   });  
    27. }); 
     
    +4