summernote-ext-hello.js (2630B)
1 (function (factory) { 2 /* global define */ 3 if (typeof define === 'function' && define.amd) { 4 // AMD. Register as an anonymous module. 5 define(['jquery'], factory); 6 } else if (typeof module === 'object' && module.exports) { 7 // Node/CommonJS 8 module.exports = factory(require('jquery')); 9 } else { 10 // Browser globals 11 factory(window.jQuery); 12 } 13 }(function ($) { 14 15 // Extends plugins for adding hello. 16 // - plugin is external module for customizing. 17 $.extend($.summernote.plugins, { 18 /** 19 * @param {Object} context - context object has status of editor. 20 */ 21 'hello': function (context) { 22 var self = this; 23 24 // ui has renders to build ui elements. 25 // - you can create a button with `ui.button` 26 var ui = $.summernote.ui; 27 28 // add hello button 29 context.memo('button.hello', function () { 30 // create button 31 var button = ui.button({ 32 contents: '<i class="fa fa-child"/> Hello', 33 tooltip: 'hello', 34 click: function () { 35 self.$panel.show(); 36 self.$panel.hide(500); 37 // invoke insertText method with 'hello' on editor module. 38 context.invoke('editor.insertText', 'hello'); 39 } 40 }); 41 42 // create jQuery object from button instance. 43 var $hello = button.render(); 44 return $hello; 45 }); 46 47 // This events will be attached when editor is initialized. 48 this.events = { 49 // This will be called after modules are initialized. 50 'summernote.init': function (we, e) { 51 console.log('summernote initialized', we, e); 52 }, 53 // This will be called when user releases a key on editable. 54 'summernote.keyup': function (we, e) { 55 console.log('summernote keyup', we, e); 56 } 57 }; 58 59 // This method will be called when editor is initialized by $('..').summernote(); 60 // You can create elements for plugin 61 this.initialize = function () { 62 this.$panel = $('<div class="hello-panel"/>').css({ 63 position: 'absolute', 64 width: 100, 65 height: 100, 66 left: '50%', 67 top: '50%', 68 background: 'red' 69 }).hide(); 70 71 this.$panel.appendTo('body'); 72 }; 73 74 // This methods will be called when editor is destroyed by $('..').summernote('destroy'); 75 // You should remove elements on `initialize`. 76 this.destroy = function () { 77 this.$panel.remove(); 78 this.$panel = null; 79 }; 80 } 81 }); 82 }));