angular:quill align的坑

本文描述了在使用Quill.js时,尝试左对齐文本遇到的问题,因为其align属性只支持right,center,和justify。作者介绍了如何在插入图片后,先设置新的行格式为undefined,再重新调整对齐方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一行设置了align为center,换行后下一个会继承上一行的格式,我想使用Quill.formatLine(newLineIndex, 0, ‘align’, 'left')来左对齐,发现始终不能生效。

参看quill.js源码,发现align没有left的配置

var config = {
  scope: _parchment2.default.Scope.BLOCK,
  whitelist: ['right', 'center', 'justify']
};

var AlignAttribute = new _parchment2.default.Attributor.Attribute('align', 'align', config);
var AlignClass = new _parchment2.default.Attributor.Class('align', 'ql-align', config);
var AlignStyle = new _parchment2.default.Attributor.Style('align', 'text-align', config);

因此只能获取换行后的格式,将align设置为undefined,再调用Quill.formatLine

  function insertImage(editor: Quill | undefined, url: string) {
    if (!editor) return;

    const {index} = editor.getSelection(true);
    editor.insertEmbed(index, 'image', url);
    editor.formatLine(index, 0, 'align', 'center')

    editor.insertText(index+1, '\n');
    const newLineIndex = index + 2;
    const newLineFormat = editor.getFormat(newLineIndex, length);
    newLineFormat['align'] = undefined;
    editor.formatLine(newLineIndex, 0, newLineFormat);
    editor.setSelection(newLineIndex, 0);
  }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值