【问题标题】:Summernote custom dialog and buttonSummernote 自定义对话框和按钮
【发布时间】:2016-05-16 14:44:24
【问题描述】:

我尝试实现 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 is undefined”。有人可以帮我吗?

谢谢

【问题讨论】:

    标签: javascript jquery summernote


    【解决方案1】:

    代替

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

    使用

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

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

    【讨论】:

    • 从 Summernote ~0.8 开始不推荐将上下文传递给事件处理程序,因此唯一有效的答案是您提供的内容
    【解决方案2】:

    @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)
                    }
                });
            });
        }
    

    【讨论】:

      【解决方案3】:

      此问题出现在 0.8.12 版本中。恢复到 0.8.10 修复它

      在 package.json 中指定

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

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

      【讨论】:

        猜你喜欢
        • 2021-02-06
        • 1970-01-01
        • 1970-01-01
        • 2015-06-20
        • 2016-04-27
        • 1970-01-01
        • 2015-03-16
        • 1970-01-01
        • 2012-06-15
        相关资源
        最近更新 更多