如何在鹅毛笔JS中添加图像

How to add image in Quill JS?

本文关键字:添加 图像 JS 鹅毛笔      更新时间:2023-09-26

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

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

更新的答案

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

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

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

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

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

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

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

方式1:按如下方式初始化羽毛笔:

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 不支持调整图像大小。不过,可以使用自定义模块来做到这一点。

上面的答案在 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'
});