vue-quill-editor自定义工具栏,确定光标位置、插入内容,获取选中的内容。

一、安装使用

  1. 下载
yarn add vue-quill-editor 

yarn add quill
  1. 导入
import { quillEditor } from "vue-quill-editor"; 
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
  1. 注册
export default {
	components: {
    	quillEditor
  },
}
  1. 使用
<quill-editor v-model="content" :options="editorOption" ref="QuillEditor"></quill-editor>

二、自定义工具栏

  1. 定义工具栏
const 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] }],

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

  ['clean']                                         // remove formatting button
];
  1. 配置

export default {
  placeholder: '', // 默认展示文字
  theme: 'snow',  // 主题
  modules: {
    toolbar: {
      container: toolbarOptions ,  // 自定义工具栏选项
      handlers: { // 事件添加
      	// handlers object will be merged with default handlers object
    	'link': function(value) { // 事件名和工具名一致
      		if (value) {
       			var href = prompt('Enter the URL');
        		this.quill.format('link', href);
      		} else {
        		this.quill.format('link', false);
      		}
    	}
      }
    }
  }
}

三、自定义按钮

  1. 在工具栏添加自定义的按钮,并添加对应的处理函数
const toolbarOptions = [
  ['newFunction']     // 新添加的按钮
];

const handlers = {
  newFunction: function () {     // 添加处理方法
    alert('我是一个新的功能!');
  }
};
  1. 配置
import { quillEditor } from "vue-quill-editor";
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';

export default {
  components: {
    quillEditor
  },
  mounted() { // 初始化按钮样式
    const newFunctionButton = document.querySelector('.ql-newFunction');
    newFunctionButton.style.cssText = "width:60px; border:1px solid #ccc; border-radius:5px;";
    newFunctionButton.innerText = "新功能";
  },
  data() {
    return {
      quillOption: {
        placeholder: '', // 默认显示文字
        theme: 'snow',  // 主题
        modules: {
          toolbar: {
            container: toolbarOptions,  // 自定义工具栏
            handlers: handlers  // 处理事件
          }
        },
      },
      content: ''
    }
  }
};
  1. 使用
<template>
  <div class="box">
    <quill-editor ref="myEditor" v-model="content" :options="quillOption"></quill-editor>
  </div>
</template>
  1. 效果图
    在这里插入图片描述
    在这里插入图片描述
  2. 完整代码
<template>
  <div class="box">
    <quill-editor ref="myEditor" v-model="content" :options="quillOption"></quill-editor>
  </div>
</template>

<script>

import { quillEditor } from "vue-quill-editor";
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';

const toolbarOptions = [
  ['newFunction']     // 新添加的按钮
];

const handlers = {
  newFunction: function () {     // 添加处理方法
    alert('我是一个新的功能!');
  }
};

export default {
  components: {
    quillEditor
  },
  mounted() {
    const newFunctionButton = document.querySelector('.ql-newFunction');
    newFunctionButton.style.cssText = "width:60px; border:1px solid #ccc; border-radius:5px;";
    newFunctionButton.innerText = "新功能";
  },
  data() {
    return {
      quillOption: {
        placeholder: '', // 默认显示文字
        theme: 'snow',  // 主题
        modules: {
          toolbar: {
            container: toolbarOptions,  // 自定义工具栏
            handlers: handlers  // 处理事件
          }
        },
      },
      content: ''
    }
  }
};
</script>

<style>
.box {
  width: 520px;
  margin: 50px;
}
</style>

四、获取富文本编辑器的光标位置,并插入内容或图片

  1. 富文本编辑器的定义
<quill-editor v-model="content" :options="editorOption" ref="QuillEditor"></quill-editor>
  1. 获取富文本编辑器
let quill = this.$refs.QuillEditor.quill // 获取富文本编辑器
  1. 获取光标位置
let index = quill.selection.savedRange.index // 获取光标位置
  1. 插入文本或图片
quill.insertEmbed(index, 'text', '这里是要插入的文字')
quill.insertEmbed(index, 'image', '这里是图片的url')
  1. 光标移到插入内容后面
quill.setSelection(index + 1) // 光标移到插入内容后面
  1. 获取编辑区的内容
let editorContent = quill.getContents(); // 编辑区内容

五、获取选中的内容

let range = this.quill.getSelection(true)
let delta = this.quill.getContents(range.index, range.length) // 获取选中的内容

六、官方文档

Quill官方文档

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值