Custom embed Blot cannot reinstantiate already existing object

Viewed 369

QuillJS custom inline blot

I am trying to insert a custom blot (using blots/embed) into the editor and that part works as show by the playground. However if I try to reinstantiate the content submitted it returns undefined at the params givens to the class.

I have created a codepen that shows this behavoir. It's contents are shown below.

HTML

<div id="editor-container"><br/><span class="proc-link" style="text-decoration: underline;background-color : lightgreen;" data-proc="value"><span contenteditable="false">Test</span></span></div>

CSS

#editor-container {
  height: 375px;
}

JS

var Embed = Quill.import('blots/embed');
class ProcLink extends Embed {
    static create(value) {
        let node = super.create(value);
        // give it some margin
        node.setAttribute('style', "text-decoration: underline;background-color : lightgreen;");
        node.setAttribute('data-proc', value.value);
        node.innerHTML = value.text;
        return node;
    }
}

ProcLink.blotName = 'proc-link'; 
ProcLink.className = 'proc-link';
ProcLink.tagName = 'span';

Quill.register({
    'formats/proc-link': ProcLink
});

var quill = new Quill('#editor-container', {
  modules: {
    toolbar: [
      [{ header: [1, 2, false] }],
      ['bold', 'italic', 'underline']
    ]
  },
  placeholder: 'Compose an epic...',
  theme: 'snow' // or 'bubble'
});

var index = quill.getSelection(true).index;//quill is the reference to my instantiated quill obj
 var cObj = {text : 'Test', value : 'value'};
 quill.insertEmbed(0,"proc-link",cObj)
1 Answers

I think you should use value(node) method to achieve this. It returns DOM information about related blot. It provides reinstantiation.

    class ProcLink extends Embed {
      static create(value) {
        let node = super.create(value);
        // give it some margin
        node.setAttribute('style', "text-decoration: underline;background-color : lightgreen;");
        node.setAttribute('data-proc', value.value);
        node.innerHTML = value.text;
        return node;
      }
      static value(node) {
        return {
          value: node.attributes.dataProc,
          text: node.innerHTML,
          style: node.attributes.style,
        }
      }
    }

Related