将自定义 html 插入鹅毛笔编辑器

Fra*_*ino 7 editor contenteditable quill angular

我使用 Quill 作为发送邮件表单,在我的工具栏中有一个带有签名的下拉菜单。当我选择签名时,我必须在编辑器中插入自定义和复杂的 html 代码(格式化并带有 img),当我更改签名时,我必须替换插入的 html 代码。

我在指定 id 的 ap 标签中插入了代码,但 Quill 清理了代码并删除了 id,所以我找不到 p 标签。

我需要保留 html 代码和 id。

有人可以帮助我吗?请。

Ale*_*lec 6

您必须编写一个自定义Blot,如下所示:

class Signature extends BlockEmbed {
  static create(value) {
    const node = super.create(value);
    node.contentEditable = 'false';
    this._addSignature(node, value);
    return node;
  }

  static value(node) {
    return node.getAttribute(Signature.blotName)
  }

  static _addSignature(node, value) {
    node.setAttribute(Signature.blotName, value);

    // This is a simple switch, but you can use
    // whatever method of building HTML you need.
    // Could even be async.
    switch (value) {
      case 1:
        return this._addSignature1(node);
      default:
        throw new Error(`Unknown signature type ${ value }`);
    }
  }

  static _addSignature1(node) {
    const div = document.createElement('DIV');
    div.textContent = 'Signature with image';
    const img = document.createElement('IMG');
    img.src = 'https://example.com/image.jpg';

    node.appendChild(div);
    node.appendChild(img);
  }
}
Signature.blotName = 'signature';
Signature.tagName = 'DIV';
Signature.className = 'ql-signature';
Run Code Online (Sandbox Code Playgroud)

确保您注册了它:

Quill.register(Signature);
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

quill.updateContents([
  { insert: { signature: 1 } },
]);
Run Code Online (Sandbox Code Playgroud)