最近一個項目用到可編輯p,公司使用的是bootstrap作為主要框架,故選用了summernote插件
現(xiàn)需要實現(xiàn)自定義按鈕功能,類似點擊插入表情(我這是要點擊插入一條預設規(guī)則、運算符等)
summernote給出了plugin,鏈接在此summernote。
也給出了實例,鏈接在此自定義plugin:hello
但是當我將代碼原封不動搬下來的時候,得到的結(jié)果是報錯:如圖
那個renderer到底是什么?
我在這行前面加了一個console,這個summernote對象的具體情況如圖所示
我的代碼結(jié)構(gòu)是這樣的:
//先引入
$(document).ready(function() {
setSummernote();
});
//初始化
function setSummernote(){
$("#summernote").summernote({
lang: 'zh-CN',
height:300,
focus:true,
toolbar:[
['group', ['hello']]
]
});
//此行報錯,我不知道這個renderer是什么。
var tmpl = $.summernote.renderer.getTemplate();
var editor = $.summernote.eventHandler.getEditor();
// add hello plugin
$.summernote.addPlugin({
// plugin's name
name: 'hello',
init : function(layoutInfo) { // run init method when summernote was initialized
// layoutInfo.holder() is current summernote's jquery instance.
var $note = layoutInfo.holder();
// you can use jquery custom event.
$note.on('summernote.update', function(customEvent, nativeEvent) {
var $boldButton = $(this).summernote('toolbar.get', 'bold');
$boldButton.toggleClass('active').css({
color : 'red'
});
});
$note.on('summernote.blur', function(customEvent, nativeEvent) {
var $boldButton = $(this).summernote('toolbar.get', 'bold');
$boldButton.removeClass('active').css({
color : 'inherit'
});
});
$note.summernote('insertText', 'plugin start.');
},
buttons: { // define button that be added in the toolbar
// "hello" button (key is a button's name)
hello: function () {
// create button template
return tmpl.iconButton('fa fa-header', {
// set event's name to used as callback when this button is clicked
// add data-event='hello' in button element
event : 'hello',
title: 'hello',
hide: true
});
}
},
events: { // events
// run callback when hello button is clicked
hello: function (event, editor, layoutInfo, value) {
// Get current editable node
var $editable = layoutInfo.editable();
// Call insertText with 'hello'
editor.insertText($editable, 'hello ');
// or
layoutInfo.holder().summernote("insertText", "Hello");
}
}
});
}
ringa_lee