Create CKEditor Plugin (Tool Bar Button And Dialog)

CKEditor is a famous HTML editor in JavaScript world. It has thousands of free and paid plugins. These plugins extend the feature of CKEditor. If you know the basic structure and configuration of CKEditor, you are also able to make plugin (toolbar button and dialog).

Need of plugins

CKEditor gives basic options only. Thus, you need extra features (like handle file uploads) must create custom plugins.

Here, we will learn to make simple plugin, add button link with font-awesome icons.

Step 1

Create your Plugin folder
 

Create your custom plugin folder inside Plugins folder. Now, add an icon image for tool bar button, which must be 16*16 pixels for perfect fit. Finally, create plugin.js file to write some logics.

Step 2

Write script for AddButton plugin

Here is the full code of addButton plugin. Just copy and paste the code inside plugin.js file. The code commands are explained, which explains how this code workes.

  1.   CKEDITOR.plugins.add('addButton',  
  2. {  
  3. init: function (editor) {  
  4.   
  5. /* Add Button in CKeditor tool bar */  
  6. editor.ui.addButton('AddnewButton',  
  7. {  
  8. label: 'Add New Button',  
  9. command: 'cmdAddButtonDialog'/*this command invoke function when you click button */  
  10. icon: this.path + 'add-Button-icon.png' /* path of your icon image*/  
  11. });  
  12.   
  13. /* addCommand - Bind our button event with Dialog */  
  14. editor.addCommand('cmdAddButtonDialog'new CKEDITOR.dialogCommand('addButtonDialog'));  
  15.   
  16. /*create a DIALOG (addButtonDialog)*/  
  17. CKEDITOR.dialog.add('addButtonDialog'function (editor) {  
  18. return {  
  19. title: 'Add New Button',  
  20. minWidth: 300,  
  21. minHeight: 200,  
  22. contents: /* tab of dialog*/  
  23. [{  
  24. id: 'tab1',  
  25. label: 'Settings',  
  26. elements:[{  
  27. type: 'text',  
  28. id: 'btnName',  
  29. label: 'Button Name: ',  
  30. validate: CKEDITOR.dialog.validate.notEmpty("Button Field is Required"/*Add validation for input field*/  
  31. },  
  32. {id: "btnIcon",  
  33. type: "select",  
  34. widths: ["35%""65%"],  
  35. style: "width:90px",  
  36. label: 'Button Icon',  
  37. "default""",  
  38. items: [['twitter'"fa fa-twitter btn btn-info"], ['facebook'"fa fa-facebook btn btn-primary"],['google+'"fa-fa-google-plus btn btn-danger"]], /* select items with name and value*/  
  39. validate: CKEDITOR.dialog.validate.notEmpty("Button Icon Field is Required"),  
  40. },{  
  41. type: 'text',  
  42. id: 'btnLink',  
  43. label: 'Link: ',  
  44. validate: CKEDITOR.dialog.validate.notEmpty("Link Field is Required")  
  45. },]  
  46. }],  
  47. onOk: function () { /*event trigger when click OK Button*/  
  48. var dialog = this;  
  49. /*Get all input Values*/  
  50. var btnName = dialog.getValueOf('tab1''btnName');  
  51. var btnIconClass = dialog.getValueOf('tab1''btnIcon');  
  52. var btnLink =dialog.getValueOf('tab1''btnLink');  
  53. //create Dom Element  
  54. var container = new CKEDITOR.dom.element('a');  
  55. //insert your dialog fields to 'container'(a)  
  56. container.addClass(btnIconClass);  
  57. container.setAttribute('href',btnLink)  
  58. container.appendText(btnName);  
  59.   
  60. //insert Element to Ckeditor content  
  61. editor.insertElement(container);  
  62. }};  
  63. }); //dialog end  
  64. //init end  
  65. }); //plugin.add end  

 

Setp 3

Add plugin to CKEditor

Finally, your plugin is ready to use. Just implement your plugin to CKEditor page and use config.extraPlugins.

Here, we will add bootstrap.css and font-awesome.css for button style and icons.

  1.  <textarea name="myckeditor "></textarea>

  2. <script>
  3. $(document).ready(function () {
  4. CKEDITOR.replace('myckeditor');
  5. CKEDITOR.config.contentsCss = ['/Assets/bootstrap-4.0.0/css/bootstrap.css',
  6. 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'];

  7. CKEDITOR.config.extraPlugins = 'addButton';
  8. });

  9. </script> 

We can get our plugin icon in CKEditor toolbar.

 

Note

This button style and icon depends on Bootstrap and font-awesome. Thus, you must include thess two files in your target page.

 
Ebook Download
View all
Learn
View all