vue-editor富文本编辑器,自定义toolbar修改工具栏

10 篇文章 0 订阅

1.概述
在项目中,会需要使用富文本编辑器去编辑文字、上传图片、视频等信息去描述某一个物品的详细信息与介绍。可使用vue的图文编辑vue2-editor去处理这些事情。

首先进行vue2-editor安装

npm install vue2-editor --save 安装至项目中

富文本的使用
  在使用vue2-editor的vue页面文件中,需要引入import { VueEditor } from ‘vue2-editor’ ,然后在components中进行注册图文编辑,然后对图文编辑器组件进行配置处理。

  <template>
  <div>
   <vue-editor v-model="form.content" id='editorsay' style="height:400px;" :editorToolbar="customToolbar" useCustomImageHandler @image-added="handleImageAdded"></vue-editor>
  </div>
</template>
<script>
  import {VueEditor} from 'vue2-editor'
  export default {
    name: 'Vue2Editor',
    data() {
      return {
     form: {
        content: '',
      },
         customToolbar: [
        ["bold", "italic", "size", " font", "underline", "color", "italic", "strike", "clean",],
        [{ align: ['', 'center', 'right', 'justify'] }],
        [{
          header: [false, 1, 2, 3, 4, 5, 6]
        }],
        [{
          list: "ordered"
        }, {
          list: "bullet"
        }],
        [{
          indent: "-1"
        }, {
          indent: "+1"
        }],
        ["image"],
      ],
      }
    },
    methods: {
    handleImageAdded: function (file, Editor, cursorLocation, reseter) {
      var formData = new FormData();
      formData.append("image", file);
      $http.post(this.$common.baseUrl + "upload/image", formData).then(res => {
        let data = res.body;
        if (data.code == 200) {
          let url = data.data.url;
          Editor.insertEmbed(cursorLocation, "image", url);
          resetUploader();
        } else { }
      })
    },
    },
  }
</script>
<style>
</style>

效果图
我这里没有添加全部工具栏

如果不需要那么多的工具栏功能要怎么办呢;可以通过customToolbar里边的配置删除多余不要得参数
显示的工具就会变少,想要增加功能可以按下面的对照表配置

关于toolbar工具栏对应功能的模块名

背景颜色 - background
加粗- bold
颜色 - color
字体 - font
内联代码 - code
斜体 - italic
链接 - link
大小 - size
删除线 - strike
上标/下标 - script
下划线 - underline
引用- blockquote
标题 - header
缩进 - indent
列表 - list
文本对齐 - align
文本方向 - direction
代码块 - code-block
公式 - formula
图片 - image
视频 - video
清除字体样式- clean

但是要注意直接引入发现有部分的图标不会显示;
他有些如list这种列表应该是有默认值,文档里有这个默认格式规范

大致上分为这几类:
1.只需要填写功能名的
加粗 - bold;
斜体 - italic
下划线 - underline
删除线 - strike
引用- blockquote
代码块 - code-block
公式 - formula
图片 - image
视频 - video
清除字体样式- clean
这一类的引用 直接[‘bold’,‘clean’]这种格式放一起就好了

2.需要有默认值的
标题 - header
[{ ‘header’: 1 }, { ‘header’: 2 }] 值字体大小

列表 - list
[{ ‘list’: ‘ordered’}, { ‘list’: ‘bullet’ }], 值ordered,bullet

上标/下标 - script
[{ ‘script’: ‘sub’}, { ‘script’: ‘super’ }], 值sub,super

缩进 - indent
[{ ‘indent’: ‘-1’}, { ‘indent’: ‘+1’ }], 值-1,+1等

文本方向 - direction
[{ ‘direction’: ‘rtl’ }], 值rtl

这种下拉的就需要默认值配置


 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在vue-quill-editor副文本编辑器的自定义工具栏内添加多个自定义按钮,你可以按照以下步骤进行操作: 1. 首先,将vue-quill-editor引入到项目中。可以使用以下代码: ```javascript import { quillEditor } from 'vue-quill-editor' ``` 2. 在组件中设置编辑器的相关数据和选项。可以使用以下代码: ```javascript export default { data() { return { content: null, editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 默认按钮 ['blockquote', 'code-block'], // 默认按钮 ['custom_button_1', 'custom_button_2'] // 自定义按钮 ] } } } }, methods: { onEditorBlur() { // 失去焦点事件 }, onEditorFocus() { // 获得焦点事件 }, onEditorChange() { // 内容改变事件 } } } ``` 3. 在toolbar中添加自定义按钮。在`toolbar`数组中添加一个新的数组,该数组包含你想要添加的自定义按钮的标识符。例如,你可以添加`custom_button_1`和`custom_button_2`两个自定义按钮。 4. 在编辑器的模板中使用`quill-editor`组件,并将之前设置的`content`、`editorOption`和相关事件绑定到相应的属性上。可以使用以下代码: ```html <template> <quill-editor v-model="content" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"></quill-editor> </template> ``` 通过以上步骤,你就可以在vue-quill-editor副文本编辑器的自定义工具栏中添加多个自定义按钮了。请注意,在`toolbar`数组中的每个子数组都代表一行按钮,你可以根据需要添加更多的按钮,并按照自己的需求进行自定义

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值