Quill Editor H5 工具栏自定义图标和方法实现

编程教程 > WEB > Web Editor (342) 2025-04-17 11:34:43

首先确定你用的是下面这个编辑器,Quill官网

Quill Editor H5 工具栏自定义图标和方法实现_图示-cb5f98a1bad848509ebe11ba5dd857b3.png
Quill

 

实现效果参考:

Quill Editor H5 工具栏自定义图标和方法实现_图示-4b3dde4dbe464a509c6688f9b487e91f.png
效果示例

 

然后开始了

配置toolbarOptions

默认的全量配置

const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
  ['blockquote', 'code-block'],
  ['link', 'image', 'video', 'formula'],

  [{ 'header': 1 }, { 'header': 2 }],               // custom button values
  [{ 'list': 'ordered'}, { 'list': 'bullet' }, { 'list': 'check' }],
  [{ '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] }],

  [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
  [{ 'font': [] }],
  [{ 'align': [] }],

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

参考:Toolbar Module - Quill Rich Text Editor (quilljs.com)

在toolbar配置中添加自己的功能(如:media)

function initEditor(){
    const toolbarOptions = [
        ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
        ['blockquote', 'code-block'],
        ['link', 'image', 'video', 'formula'],

        // [{ 'header': 1 }, { 'header': 2 }],               // custom button values
        [{ 'list': 'ordered'}, { 'list': 'bullet' }, { 'list': 'check' }],
        [{ '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] }],

        [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
        [{ 'font': [] }],
        [{ 'align': [] }],

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

如上所示,最后一个地方添加了一个media节点。

 

初始化Quill编辑器

 

 const quill = new Quill('#editor', {
     theme: 'snow',
     modules:{
         toolbar:{
             container:toolbarOptions,
             handlers:{
                 media:function (){
                     alert('打开媒体库,插入了一个图片链接');
                     let index = quill.selection.savedRange.index; // 获取光标位置
                     quill.insertEmbed(index, 'image', 'https://cdn.pixabay.com/photo/2023/11/14/20/08/woman-8388428_1280.jpg');
// 重新计算index
//注意如果是文本内容则需要计算文本长度  index = text.length;
index+=1
 
// 移动光标至文本后面
quill.setSelection(index);
                 }
             }
         },

     }
 });
 

初始化自定义toobar按钮样式

/**
 * 初始化自定义按钮
 */
function initCustomToolbarBtn(){
    //css  '.ql-' + 'toolbar里面配置的btn名称'
    let mediaBtn = document.querySelector('.ql-media');
    let elementI=document.createElement('i');
    // elementI.classList.add('fa-solid fa-image'); //error
    elementI.classList='fa-solid fa-image';
    // elementI.style.cssText="width:80px; border:1px solid #ccc; border-radius:5px;"
    elementI.title='打开媒体库';
    mediaBtn.appendChild(elementI)
}

 

 

注意:toolbar需要在初始化editor完成后进行。

至此就配置完毕了,打开即可查看效果。

Quill Editor H5 工具栏自定义图标和方法实现_图示-924cb6ca89a541cfafd86920dee3020b.png

 

image

 

完整示例代码:

VIP资源,登录后查阅

此处内容需要开通会员后查阅(刷新)

登录/注册

 


评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
首先确定你用的是下面这个编辑器,Quill官网Quill 实现效果参考:实现效果 然后开始了配置toolbarOptions默认的全量配置const toolb
tui.editor编辑器部分引入必要文件<link rel="stylesheet" href="your/path/to/tui@editor/toastu
接上一篇:mybatis Interceptor拦截器实现自定义扩展查询兼容mybatis plus-xqlee (blog.xqlee.com)这里进行自定义分页查询扩展,基于mybatis ...
引入插件必要文件基于上一篇《TUI.Editor Markdown 编辑器基于 JavaScript 使用-XQLEE'Blog》改编<link rel="st
Java编程之通过eclipse创建maven自定义项目原型模板(Archetype),Java编程,maven自定义项目模板
ckeditor 5 自定义插件实现,自定义一个插件并在toolbar上。点击自定义插件图标响应对应事件。环境ckeditor 5.42.1(github稳定版
CKeditor无论那个版本,在我主要以文章编辑的时候,需要去掉默认的H1标签,所以需要自定义配置heading标签。同样CKeditor5 与之前的4版本有所不同。CKeditor5自定义配置...
需求fail2ban 根据某个程序的日志文件进行监视,发现对应问题后进行监禁IP。具体需求示例:监控nginx示例,1分钟发现10个404页面访问则封禁该IP1
CKeditor5 默认情况下的fontsize组件是小号默认大号特大号这种,没有具体的字体大小px单位选择,有些时候需要配置。故而对默认配置进行了修改。配置方式如下: C...
mybatis Interceptor拦截器实现自定义扩展查询兼容mybatis plus @Intercepts({ @Signature(type = Executor.c...
问题说明我有一个自己的图像上传逻辑。 但是当我上传图像时,我想为图像添加一个自定义属性和一个自定义class。所以我的图片上传后应该是这样的:<img clas
1.概述本教程将重点介绍如何使用Apache HttpClient 4发送自定义Cookie
Spring data-mongodb ID自增长注解实现,mongodb ID自增长
如何支持直接传递自定义对象那么我们希望能有一种方式保持跟controller完全一致只需要传递自定义的对象,既让服务提供端开发人员爽,也让服务消费端开发人员爽,