Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert custom html into quill editor

I use Quill for my send mail form and in my toolbar I have a drop down with the signatures. When I select a signature I have to insert custom and complex html code (formatted and with img) in the editor, when I change signature I have to replace inserted html code.

I inserted the code in a p tag specifing the id but Quill cleans the code also deleting the id so i can't find the p tag.

I need to preserve the html code and the id.

Someone could help me? Please.

like image 475
Francesco Borraccino Avatar asked Jun 26 '26 02:06

Francesco Borraccino


1 Answers

You'll have to write a custom Blot, like this:

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';

Make sure you register it:

Quill.register(Signature);

Then you can use it like so:

quill.updateContents([
  { insert: { signature: 1 } },
]);
like image 95
Alec Avatar answered Jun 28 '26 16:06

Alec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!