Quill 自定义行高

在使用Quill的过程中,发现官方并没有自定义行高的功能,恰好产品有这样的需求,因此需要自己添加。

废话少说,直接上核心代码,以下是以Vue为示例

<template>
     <quill-editor v-model="data" :options="editorOption" ref="editor" />
</template>

<script>
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import { quillEditor } from 'vue-quill-editor';
import Quill from 'quill';


// 行高,这里是从1.0开始到2.5,步增0.1
const lineHeight = [...Array(16).keys()].map((x) => `${(100 + x * 10) / 100}`);

const parchment = Quill.import('parchment');
const lineHeightConfig = {
  scope: parchment.Scope.INLINE,
  whitelist: lineHeight,
};
const lineHeightStyle = new parchment.Attributor.Style(
  'lineHeight',
  'line-height',
  lineHeightConfig
);
// 最核心的两行代码
Quill.register({ 'formats/lineHeight': lineHeightStyle }, true);
Quill.register({ lineHeightStyle: true });


export default {
    data() {
        return {
            data:'',
            editorOption:{
                modules: {
                  toolbar: {
                    container: [
                      ...其他工具
                      [{ lineheight: lineHeight }],
                    ],
                    handlers: {
                      lineheight: (value: any) => {
                        const editor = this.$refs.editor.quill as any;
                        if (value) {
                          editor.format('lineHeight', value);
                        }
                      },
                    },
                  },
                }
              }
        }
    }
}
</script>

到这里,基本上已经完成了自定义行高的主要实现。

最后需要添加上 css 样式

<style>
.ql-snow .ql-picker.ql-lineheight .ql-picker-label::before {
  content: '行高';
}

.ql-snow .ql-picker.ql-lineheight .ql-picker-item {
  &::before {
    content: attr(data-value) !important;
  }
}

.ql-snow .ql-picker.ql-lineheight {
  width: 70px;
}

.ql-snow .ql-picker.ql-lineheight .ql-picker-options {
  height: 200px;
  overflow: auto;
}
</style>

  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 React-Quill 编辑器中自定义行高,可以使用 Quill 自带的 line-height 模块。首先需要在 React-Quill 的配置中添加 line-height 模块,然后在需要自定义行高的地方调用该模块的 API。 以下是一个示例代码: ```javascript import ReactQuill, { Quill } from 'react-quill'; import 'react-quill/dist/quill.snow.css'; // 添加 line-height 模块 var lineHeight = Quill.import('attributors/style/line-height'); lineHeight.whitelist = ['1', '1.5', '2', '2.5', '3']; Quill.register(lineHeight, true); class Editor extends React.Component { constructor(props) { super(props); this.state = { text: '' }; this.handleChange = this.handleChange.bind(this); } handleChange(value) { this.setState({ text: value }); } render() { return ( <ReactQuill value={this.state.text} onChange={this.handleChange} modules={{ toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 文字样式 [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 标题 [{ 'list': 'ordered'}, { 'list': 'bullet' }], // 列表 [{ 'align': [] }], // 对齐方式 [{ 'color': [] }, { 'background': [] }], // 文字颜色和背景色 ['clean'] // 清除格式 ], 'line-height': {} // 添加 line-height 模块 }} /> ); } } ``` 要注意的是,使用 line-height 模块设置行高时,需要在模块的 whitelist 中指定可用的行高选项,这里示例中设置了五个选项,分别为 1、1.5、2、2.5 和 3。在调用模块的 API 时,需要使用字符串形式的行高值,例如 `'1.5'`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值