Vue3富文本自定义行高

文章展示了如何在Vue3项目中安装和使用@vueup/vue-quill插件,创建富文本编辑器。内容包括安装插件、导入组件、自定义行高(通过修改Parchment配置)以及在toolbar中添加行高选项。此外,还介绍了如何集成imageDrop和imageResize模块,以及设置字体和大小的选项。
摘要由CSDN通过智能技术生成

1. Vue3使用插件@vueup/vue-quill

npm install @vueup/vue-quill

2. 组件中引入

import { QuillEditor, Quill } from '@vueup/vue-quill';

3. 自定义行高

const Parchment = Quill.import('parchment');
let config = {
  scope: Parchment.Scope.BLOCK,
  whitelist: ['1', '1.5', '2', '2.5']
};
const lineHeightClass = new Parchment.Attributor.Class(
  'lineHeight',
  'ql-line-height',
  config
);
const lineHeightStyle = new Parchment.Attributor.Style(
  'lineHeight',
  'line-height',
  config
);
Parchment.register(lineHeightClass);
Parchment.register(lineHeightStyle);

4. toolbar中新增

[{ lineHeight: config.whitelist }]

完整代码

<template>
    <QuillEditor
      v-model:content="content"
      contentType="html"
      :options="options"
      @keydown.stop
      ref="editorRef"
    />
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { QuillEditor, Quill } from '@vueup/vue-quill';
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import { ImageDrop } from 'quill-image-drop-module';
import imageResize from 'quill-image-resize-module';

Quill.register('modules/ImageDrop', ImageDrop);
Quill.register('modules/imageResize', imageResize);

// 自定义行高
const Parchment = Quill.import('parchment');
let config = {
  scope: Parchment.Scope.BLOCK,
  whitelist: ['1', '1.5', '2', '2.5']
};
const lineHeightClass = new Parchment.Attributor.Class(
  'lineHeight',
  'ql-line-height',
  config
);
const lineHeightStyle = new Parchment.Attributor.Style(
  'lineHeight',
  'line-height',
  config
);
Parchment.register(lineHeightClass);
Parchment.register(lineHeightStyle);

// 设置字体和大小
const fonts = [
  'SimSun',
  'SimHei',
  'Microsoft-YaHei',
  'KaiTi',
  'FangSong',
  'Arial',
  'Times-New-Roman',
  'sans-serif'
];
const Font = Quill.import('formats/font');
Font.whitelist = fonts; //将字体加入到白名单
Quill.register(Font, true);
const fontSize = Quill.import('attributors/style/size');
fontSize.whitelist = ['16px', '32px', '64px', '96px'];
Quill.register(fontSize, true);

const editorRef = ref(null);
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'],
  [{ size: ['small', 'normal', 'large', 'huge'] }],
  [{ color: [] }, { background: [] }],
  [
    {
      font: fonts
    }
  ],
  [{ align: [] }],
  [{ list: 'ordered' }, { list: 'bullet' }],
  ['image'],
  [{ lineHeight: config.whitelist }]
];
const options = {
  debug: 'info',
  modules: {
    toolbar: {
      container: toolbarOptions
    },
    ImageDrop: true,
    imageResize: {
      displayStyles: {
        backgroundColor: 'black',
        border: 'none',
        color: 'white'
      },
      modules: ['Resize', 'DisplaySize']
    }
  },
  readOnly: false,
  theme: 'snow'
};

const content = ref('');
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值