import Parchment from 'parchment';
import Emitter from 'quill/emitter';
import Module from 'quill/module';
import CodeBlock from 'quill/formats/code';


class HighlightCodeBlock extends CodeBlock {
  highlight() {
    if (this.cachedHTML !== this.domNode.innerHTML) {
      this.domNode.textContent = this.domNode.textContent;
      hljs.highlightBlock(this.domNode);
      this.attach();
      this.cachedHTML = this.domNode.innerHTML;
    }
  }
}


let CodeToken = new Parchment.Attributor.Class('token', 'hljs', {
  scope: Parchment.Scope.INLINE
});


class CodeHighlighter extends Module {
  constructor(quill, options) {
    super(quill, options);
    let timer = null;
    this.quill.on(Emitter.events.SCROLL_OPTIMIZE, () => {
      if (timer != null) return;
      timer = setTimeout(() => {
        this.highlight();
        timer = null;
      }, 100);
    });
    this.highlight();
  }

  highlight() {
    let range = this.quill.getSelection();
    this.quill.scroll.descendants(HighlightCodeBlock).forEach(function(code) {
      code.highlight();
    });
    if (range != null) {
      this.quill.setSelection(range, Emitter.sources.SILENT);
    }
  }
}


export { HighlightCodeBlock as CodeBlock, CodeToken, CodeHighlighter as default};

@@ -257,24 +257,105 @@
 nnerHTML) %7B%0A
+      let text = this.domNode.textContent;%0A      if (text.trim().length %3E 0) %7B%0A  
       this.d
@@ -407,16 +407,18 @@
 ;%0A      
+ 
+ 
 hljs.hig
@@ -446,24 +446,26 @@
 ode);%0A      
+  
 this.attach(
@@ -463,24 +463,32 @@
 s.attach();%0A
+      %7D%0A
       this.c

fix when deleting last character in block