Summernote自定义对话框和按钮

时间:2016-02-06 17:38:57

标签: javascript jquery summernote

我尝试实现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 所以,在这个例子中,我想简单地放一个&#34; Hello&#34;字符串单击按钮但它给我一个错误&#34; TypeError:context is undefined&#34;。有人能帮助我吗?

由于

3 个答案:

答案 0 :(得分:3)

而不是

  context.invoke('editor.insertText', 'hello');

使用

  $('#st-editor').summernote('editor.insertText', 'hello'); 

只有当你有一位编辑时才有效。我仍然在寻找如何让这个上下文通过。也许有onInit的东西,但我还不能让它工作。

答案 1 :(得分:0)

@wessel代码有效,对于多个id,我每个都使用jQuery进行迭代: 确保oyu将id属性附加到所有编辑器:

        if ($('.summernote').length) {

        var blockQuoteButton = function(itemId) {
            var ui = $.summernote.ui;
            var button = ui.button({
                className: 'note-btn-blockquote',
                contents: '<i class="fa fa-quote-right">Quo</i>',
                tooltip: 'Blockquote',
                click: function() {
                    $('#' + itemId).summernote('editor.formatBlock', 'blockquote');
                }
            });

            return button.render();
        }
        $('.summernote').each(function(k, item) {
            let itemId = $(item).attr('id');
            $('#' + itemId).summernote({
                height: 100,
                toolbar: [
                    ['style', ['style']],
                    ['font', ['bold', 'italic', 'underline']],
                    ['para', ['ul', 'ol']],
                    ['mybutton', ['blq']]
                ],
                buttons: {
                    blq: blockQuoteButton(itemId)
                }
            });
        });
    }

答案 2 :(得分:0)

此问题出现在版本0.8.12中。恢复到0.8.10即可解决

在package.json内部指定

  "dependencies": {
    ...
    "ngx-summernote": "0.7.0",
    "summernote": "0.8.10",
    ...

  },

,然后运行npm install。之后就可以了