如何在 Quill JS 中添加图像?

How to add image in Quill JS?

我不知道如何让图像像 http://quilljs.com/ 上的示例那样工作。

我尝试将 <span title="Image" class="ql-format-button ql-image"></span> 添加到工具栏,这会添加按钮,但单击按钮没有任何反应,而且我在文档中找不到任何内容。有什么建议吗?

编辑:从 1.0 开始,这不再准确。 Chris Hawkes 的 是正确的。

不幸的是,这似乎没有在任何地方记录,但您需要包含 image-tooltip 模块。比如quilljs.com首页的编辑是这样用的:

quill = new Quill('#editor', {
  modules: {
    'toolbar': { container: '#toolbar' },
    'image-tooltip': true,
    'link-tooltip': true
  },
  theme: 'snow'
});

上面的答案在js中是正确的,但是你必须在编辑器中添加html,例如:

<span class="ql-format-group">
  <span title="Link" class="ql-format-button ql-link"></span>
  <span class="ql-format-separator"></span>
  <span title="Image" class="ql-format-button ql-image"></span>
</span>

所以之后放入 js

quill = new Quill('#editor', {
  modules: {
    'toolbar': { container: '#toolbar' },
    'image-tooltip': true,
    'link-tooltip': true
  },
  theme: 'snow'
});

更新答案

从 1.0 版及更高版本开始,您不再需要添加默认包含的工具提示模块。如何启用它的一个例子就是这个。

    <script>
            var toolbarOptions = [
                ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                ['blockquote', 'code-block'],

                [{ 'header': 1 }, { 'header': 2 }],               // custom button values
                [{ 'list': 'ordered'}, { 'list': 'bullet' }],
                [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
                [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
                [{ 'direction': 'rtl' }],                         // text direction

                [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
                [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
                [ 'link', 'image', 'video', 'formula' ],          // add's image support
                [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
                [{ 'font': [] }],
                [{ 'align': [] }],

                ['clean']                                         // remove formatting button
            ];

        var quill = new Quill('#editor', {
            modules: {
                toolbar: toolbarOptions
            },

            theme: 'snow'
        });
    </script>

从 Quill 1.3 版开始,none 以上答案不再有效。 不幸的是,官方文档也没有太大进展。

您有两种解决问题的方法,都适用于官方主题 SnowBubble。 无论哪种方式,您 都不必 添加以下代码:

'image-tooltip': true,
'link-tooltip': true

方式一: 初始化羽毛笔如下:

var editor = new Quill('#editorDiv', 
{
    modules: {
      toolbar: [
            ...
            ['image'],
            ...
        ],
        //not needed anymore: 'image-tooltip': true,
        //not needed anymore: 'link-tooltip': true
        ...
    },
    ...
});

方式 2: 初始化羽毛笔如下:

<div id="editorToolbarDiv">
  ...
  <button class="ql-image"></button>
</div>
<div id="editorDiv"></div>
var editor = new Quill('#editorDiv', 
{
    modules: {
        toolbar: '#editorToolbarDiv',
        //not needed anymore: 'image-tooltip': true,
        //not needed anymore: 'link-tooltip': true,
        ...
    },
    ...
});

从 1.3 版本开始,Quill 不支持调整图像大小。不过,可以使用自定义模块来完成。