2020-01-22 11:53:40 +01:00
|
|
|
// Inspired by: https://github.com/pHAlkaline/summernote-plugins/tree/master/plugins/nugget
|
2020-01-21 18:03:15 +01:00
|
|
|
|
|
|
|
(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 ($) {
|
|
|
|
|
|
|
|
|
|
|
|
$.extend($.summernote.options, {
|
|
|
|
nugget: {
|
|
|
|
list: []
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
$.extend(true, $.summernote, {
|
|
|
|
// add localization texts
|
|
|
|
lang: {
|
|
|
|
'en-US': {
|
|
|
|
nugget: {
|
|
|
|
Nugget: 'Nugget',
|
|
|
|
Insert_nugget: 'Insert Nugget'
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'en-GB': {
|
|
|
|
nugget: {
|
|
|
|
Nugget: 'Nugget',
|
|
|
|
Insert_nugget: 'Insert Nugget'
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'pt-PT': {
|
|
|
|
nugget: {
|
|
|
|
Nugget: 'Pepita',
|
|
|
|
Insert_nugget: 'Inserir pepita'
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'it-IT': {
|
|
|
|
nugget: {
|
|
|
|
Nugget: 'Pepite',
|
|
|
|
Insert_nugget: 'Pepite Inserto'
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Extends plugins for adding nuggets.
|
|
|
|
// - plugin is external module for customizing.
|
|
|
|
$.extend($.summernote.plugins, {
|
|
|
|
/**
|
|
|
|
* @param {Object} context - context object has status of editor.
|
|
|
|
*/
|
|
|
|
'nugget': function (context) {
|
|
|
|
// ui has renders to build ui elements.
|
|
|
|
// - you can create a button with `ui.button`
|
2020-01-22 11:53:40 +01:00
|
|
|
const ui = $.summernote.ui;
|
|
|
|
const options = context.options.nugget;
|
|
|
|
const context_options = context.options;
|
|
|
|
const lang = context_options.langInfo;
|
|
|
|
const defaultOptions = {
|
2020-01-21 18:03:15 +01:00
|
|
|
label: lang.nugget.Nugget,
|
|
|
|
tooltip: lang.nugget.Insert_nugget
|
|
|
|
};
|
|
|
|
|
|
|
|
// Assign default values if not supplied
|
2020-01-22 11:53:40 +01:00
|
|
|
for (const propertyName in defaultOptions) {
|
2020-01-21 18:03:15 +01:00
|
|
|
if (options.hasOwnProperty(propertyName) === false) {
|
|
|
|
options[propertyName] = defaultOptions[propertyName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add hello button
|
|
|
|
context.memo('button.nugget', function () {
|
|
|
|
// create button
|
|
|
|
|
2020-01-22 11:53:40 +01:00
|
|
|
const button = ui.buttonGroup([
|
2020-01-21 18:03:15 +01:00
|
|
|
ui.button({
|
|
|
|
className: 'dropdown-toggle',
|
|
|
|
contents: '<span class="nugget">' + options.label + ' </span><span class="note-icon-caret"></span>',
|
|
|
|
tooltip: options.tooltip,
|
|
|
|
data: {
|
|
|
|
toggle: 'dropdown'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
ui.dropdown({
|
|
|
|
className: 'dropdown-nugget',
|
2020-01-22 11:53:40 +01:00
|
|
|
contents: options.list.map((i) => {
|
|
|
|
const li = document.createElement('li');
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.innerHTML = i.trim();
|
|
|
|
a.setAttribute('href', '#');
|
|
|
|
li.appendChild(a);
|
|
|
|
return li.outerHTML;
|
|
|
|
}),
|
2020-01-21 18:03:15 +01:00
|
|
|
click: function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2020-01-22 11:53:40 +01:00
|
|
|
const $button = $(event.target);
|
|
|
|
const value = $button[0].outerHTML;
|
|
|
|
const node = document.createElement('div');
|
|
|
|
node.innerHTML = value.trim();
|
|
|
|
context.invoke('editor.insertNode', node.firstChild);
|
2020-01-21 18:03:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
// create jQuery object from button instance.
|
|
|
|
return button.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}));
|