我尝试实现 Summertnote 编辑器。 JS代码如下:
$(document).ready(function() {
//Summernote
//var te_markdown = document.getElementById("code-markdown");.
var textarea = document.getElementById("code");
var HelloButton = function (context) {
var ui = $.summernote.ui;
// create button
var button = ui.button({
contents: '<i class="fa fa-child"/> Hello',
tooltip: 'Ciao!',
click: function () {
// invoke insertText method with 'hello' on editor module.
context.invoke('editor.insertText', 'hello');
}
});
return button.render(); // return button as jquery object
}
function autoFormat() {
var totalLines = editor.lineCount();
editor.autoFormatRange({line:0, ch:0}, {line:totalLines});
}
$('#st-editor').summernote({
lang: 'it-IT', // set italian language
height: 350, // set editor height
width: 350, // set editor width
minHeight: null, // set minimum height of editor
maxHeight: null, // set maximum height of editor
dialogsFade: true, // set fade on dialog
prettifyHtml: false,
toolbar: [
['mybutton', ['hello']]
],
buttons: {
hello: HelloButton
},
codemirror: { // codemirror options
mode: "text/html",
lineNumbers: true,
lineWrapping: true,
extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
foldGutter: true,
theme: 'monokai',
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
}
},
focus: true set focus to editable area after initializing summernote
});
我在这里得到代码:http://summernote.org/deep-dive/#custom-button 因此,在这个例子中,我想简单地放置一个“Hello”字符串,单击按钮,但它给了我一个错误“TypeError:上下文未定义”。有人可以帮助我吗?
谢谢
最佳答案
而不是
context.invoke('editor.insertText', 'hello');
使用
$('#st-editor').summernote('editor.insertText', 'hello');
当然,仅当您有一名编辑器时才有效。我仍在寻找如何让这个上下文通过。也许与 onInit 有关,但我还无法让它工作。
关于javascript - Summernote 自定义对话框和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244207/