Skip to content

Instantly share code, notes, and snippets.

@ThomasTJdev
Forked from onesimoh/summernote-ext-checkbox.js
Last active October 30, 2018 19:10
Show Gist options
  • Save ThomasTJdev/b47f1f8c28d66d6a6409e696089538b1 to your computer and use it in GitHub Desktop.
Save ThomasTJdev/b47f1f8c28d66d6a6409e696089538b1 to your computer and use it in GitHub Desktop.
Summernote 0.8.10 plugin for checkbox using Bootstrap 4 with Font Awesome 4
(function (factory) {
/* global define */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(window.jQuery);
}
}(function ($) {
// Extends plugins for adding Checkbox.
$.extend($.summernote.plugins, {
/**
* @param {Object} context - context object has status of editor.
*/
'checkbox': function (context) {
var self = this;
var ui = $.summernote.ui;
context.memo('button.checkbox', function () {
var button = ui.button({
contents: '<i class="fa fa-check-square-o"/>',
click: function () {
context.invoke('insertNode', self.createCheckbox());
}
});
return button.render();
});
this.createCheckbox = function () {
var elem = document.createElement('input');
elem.type = "checkbox";
return elem;
}
// This events will be attached when editor is initialized.
this.events = {
'summernote.init': function (we, e) {
},
'summernote.keyup': function (we, e) {
}
};
this.initialize = function () {
var layoutInfo = context.layoutInfo;
var $editor = layoutInfo.editor;
$editor.click(function (event) {
if (event.target.type && event.target.type == 'checkbox') {
var checked = $(event.target).is(':checked');
$(event.target).attr('checked', checked);
context.invoke('insertText', '');
}
});
};
this.destroy = function () {
};
}
});
}));
@ThomasTJdev
Copy link
Author

Button to insert checkboxes in Summernote. Requires Summernote > 0.8.10, Font Awesome 4 and Bootstrap 4.

Insert with:

$('.summernote-edit').summernote({
    toolbar: [
      ['style', ['bold', 'italic', 'clear']],
      ['fontsize', ['fontsize']],
      ['para', ['ul', 'ol', 'checkbox']],
    ],
 ..truncated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment