Froala添加自定义预编码按钮

时间:2014-10-15 21:18:31

标签: button tags customization wysiwyg froala

我尝试使用Froala编辑器创建一个代码按钮,通过按CNTRL+K可以基本上执行与此处相同的操作。现在我觉得我有两个选择。

第一个是编辑froala-editor.js文件,因为Froala已经有一个&#39;代码&#39;只添加<pre>标签的按钮。如果我能以某种方式让它也添加<code>标签,问题就解决了。不幸的是,我没有让这个工作。

第二个选项是创建一个自定义按钮,到目前为止我有这段代码:

$('textarea[name="description"]').editable({
    //Settings here
    customButtons: {
        insertCode: {
            title: 'Insert code',
            icon: {
                type: 'font',
                value: 'fa fa-code'
            },
            callback: function() {
                this.saveSelection();

                if (!this.selectionInEditor()) {
                    this.$element.focus(); // Focus on editor if it's not.
                }

                var html = '<pre><code>' + this.text() + ' </code></pre>';

                this.restoreSelection();
                this.insertHTML(html);
                this.saveUndoStep();
            }
        }
    }
});

它以某种方式起作用,但它的错误并产生如此奇怪的HTML:

<p><code></code>
  <pre><code>asdasdasdasd
</code></pre>
</p>

非常感谢任何一两个选项的帮助。

1 个答案:

答案 0 :(得分:2)

如果您升级到Github上提供的1.2.3版,则您的代码应该可以正常运行https://github.com/froala/wysiwyg-editor。没有必要保存/恢复选择。


稍后编辑: 这是jsFiddle for http://jsfiddle.net/9pmmg1jk/

customButtons: {
  insertCode: {
    title: 'Insert code',
      icon: {
        type: 'font',
          value: 'fa fa-code'
      },
        callback: function() {
          if (!this.selectionInEditor()) {
            this.$element.focus(); // Focus on editor if it's not.
          }

          var html = '<code>' + (this.text() || '&#8203;') + '<span class="f-marker" data-type="false" data-id="0" data-fr-verified="true"></span><span class="f-marker" data-type="true" data-id="0" data-fr-verified="true"></span></code>';

          this.insertHTML(html);
          this.restoreSelectionByMarkers();
          this.saveUndoStep();
        }
  }
}
相关问题