gtgalone / react-quilljs

React Hook Wrapper for Quill, powerful rich text editor.
https://www.npmjs.com/package/react-quilljs
MIT License
248 stars 28 forks source link

Create a new blot #58

Closed rebryk closed 1 year ago

rebryk commented 2 years ago

I am trying to clone medium with parchment using this library. But I don't understand how to define a new blot.

Could you please help me to replicate the following code?

let Inline = Quill.import('blots/inline');

class BoldBlot extends Inline { }
BoldBlot.blotName = 'bold';
BoldBlot.tagName = 'strong';
meep-morp commented 1 year ago

I managed to override ImageBlot to allow the alt attribute with the following:

--TS--

  if (Quill && !quill) {
    const BlockEmbed = Quill.import('blots/block/embed');

    /// OVERRIDE IMAGE BLOT ///
    class ImageBlot extends BlockEmbed {
      static create(value) {
        const node = super.create();
        node.setAttribute('alt', value.alt);
        node.setAttribute('src', value.src);
        return node;
      }

      static value(node) {
        return {
          alt: node.getAttribute('alt') as string,
          url: node.getAttribute('src') as string,
        };
      }
    }
    ImageBlot.blotName = 'image';
    ImageBlot.tagName = 'img';

    Quill.register({ 'formats/imageBlot': ImageBlot });
  }

Hopefully, that helps you out!

gtgalone commented 1 year ago

Thank you so much! @meep-morp