angular:quill align的坑

上一行设置了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);
  }

你可能感兴趣的:(Angular,前端,typescript,angular)