javascript 如何在 Quill JS 中添加图片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29008914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:57:24  来源:igfitidea点击:

How to add image in Quill JS?

javascriptrichtextrtequill

提问by kidcapital

I can't figure out how to get images to work like in the example on http://quilljs.com/.

我无法弄清楚如何让图像像http://quilljs.com/上的示例一样工作。

I tried adding <span title="Image" class="ql-format-button ql-image"></span>to the toolbar, which adds the button, but clicking on the button does nothing and I can't find anything in the documentation. Any suggestion?

我尝试添加<span title="Image" class="ql-format-button ql-image"></span>到工具栏,它添加了按钮,但单击按钮什么也不做,我在文档中找不到任何内容。有什么建议吗?

回答by Chris Hawkes

Updated Answer

更新答案

As of version 1.0 and beyond you no longer need to add the tool-tip module it's included by default. An example of how to enable it would be this.

从 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>

回答by jhchen

Edit: This is no longer accurate as of 1.0. Chris Hawkes's answeris correct.

编辑:从 1.0 开始,这不再准确。克里斯霍克斯的回答是正确的。

This unfortunately doesn't seem documented anywhere but you need to include the image-tooltip module. For example, this is what the editor on the quilljs.com homepage uses:

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

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

回答by Andres Felipe

well the above answer is the correct in the js, but you have to add html to the editor, example:

好吧,上面的答案在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>

so after that put in the js

所以在那之后放入js

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

回答by Anton

As of Quill version 1.3, none of the answers above work anymore. Unfortunately, the official documentation hasn't progressed much either.

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

You have two ways to solve your problem, both work for official themes Snowand Bubble. Either way, you do not have toadd the following code:

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

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

Way 1: Initialize quill as following:

方式1:初始化quill如下:

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

Way 2: Initialize quill as following:

方式2:初始化quill如下:

<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,
        ...
    },
    ...
});

As of version 1.3, Quill does not support resizing images. One can do it with a custom module, though.

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