"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const k=require("../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.js"),q=require("../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.js"),v=require("../blots/inline.js"),o=require("../core/quill.js"),L=require("../core/module.js"),T=require("../blots/block.js"),C=require("../blots/break.js"),w=require("../blots/cursor.js"),y=require("../blots/text.js"),c=require("../formats/code.js"),x=require("./clipboard.js"),d=new q.ClassAttributor("code-token","hljs",{scope:q.Scope.INLINE});class n extends v.default{static formats(e,t){for(;e!=null&&e!==t.domNode;){if(e.classList&&e.classList.contains(c.default.className))return super.formats(e,t);e=e.parentNode}}constructor(e,t,i){super(e,t,i),d.add(this.domNode,i)}format(e,t){e!==n.blotName?super.format(e,t):t?d.add(this.domNode,t):(d.remove(this.domNode),this.domNode.classList.remove(this.statics.className))}optimize(){super.optimize(...arguments),d.value(this.domNode)||this.unwrap()}}n.blotName="code-token";n.className="ql-token";class a extends c.default{static create(e){const t=super.create(e);return typeof e=="string"&&t.setAttribute("data-language",e),t}static formats(e){return e.getAttribute("data-language")||"plain"}static register(){}format(e,t){e===this.statics.blotName&&t?this.domNode.setAttribute("data-language",t):super.format(e,t)}replaceWith(e,t){return this.formatAt(0,this.length(),n.blotName,!1),super.replaceWith(e,t)}}class f extends c.CodeBlockContainer{attach(){super.attach(),this.forceNext=!1,this.scroll.emitMount(this)}format(e,t){e===a.blotName&&(this.forceNext=!0,this.children.forEach(i=>{i.format(e,t)}))}formatAt(e,t,i,s){i===a.blotName&&(this.forceNext=!0),super.formatAt(e,t,i,s)}highlight(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:!1;if(this.children.head==null)return;const s=`${Array.from(this.domNode.childNodes).filter(l=>l!==this.uiNode).map(l=>l.textContent).join(`
`)}
`,r=a.formats(this.children.head.domNode);if(t||this.forceNext||this.cachedText!==s){if(s.trim().length>0||this.cachedText==null){const l=this.children.reduce((h,m)=>h.concat(T.blockDelta(m,!1)),new k.default),g=e(s,r);l.diff(g).reduce((h,m)=>{let{retain:p,attributes:N}=m;return p?(N&&Object.keys(N).forEach(b=>{[a.blotName,n.blotName].includes(b)&&this.formatAt(h,p,b,N[b])}),h+p):h},0)}this.cachedText=s,this.forceNext=!1}}html(e,t){const[i]=this.children.find(e);return`<pre data-language="${i?a.formats(i.domNode):"plain"}">
${y.escapeText(this.code(e,t))}
</pre>`}optimize(e){if(super.optimize(e),this.parent!=null&&this.children.head!=null&&this.uiNode!=null){const t=a.formats(this.children.head.domNode);t!==this.uiNode.value&&(this.uiNode.value=t)}}}f.allowedChildren=[a];a.requiredContainer=f;a.allowedChildren=[n,w.default,y.default,C.default];const E=(u,e,t)=>{if(typeof u.versionString=="string"){const i=u.versionString.split(".")[0];if(parseInt(i,10)>=11)return u.highlight(t,{language:e}).value}return u.highlight(e,t).value};class S extends L.default{static register(){o.default.register(n,!0),o.default.register(a,!0),o.default.register(f,!0)}constructor(e,t){if(super(e,t),this.options.hljs==null)throw new Error("Syntax module requires highlight.js. Please include the library on the page before Quill.");this.languages=this.options.languages.reduce((i,s)=>{let{key:r}=s;return i[r]=!0,i},{}),this.highlightBlot=this.highlightBlot.bind(this),this.initListener(),this.initTimer()}initListener(){this.quill.on(o.default.events.SCROLL_BLOT_MOUNT,e=>{if(!(e instanceof f))return;const t=this.quill.root.ownerDocument.createElement("select");this.options.languages.forEach(i=>{let{key:s,label:r}=i;const l=t.ownerDocument.createElement("option");l.textContent=r,l.setAttribute("value",s),t.appendChild(l)}),t.addEventListener("change",()=>{e.format(a.blotName,t.value),this.quill.root.focus(),this.highlight(e,!0)}),e.uiNode==null&&(e.attachUI(t),e.children.head&&(t.value=a.formats(e.children.head.domNode)))})}initTimer(){let e=null;this.quill.on(o.default.events.SCROLL_OPTIMIZE,()=>{e&&clearTimeout(e),e=setTimeout(()=>{this.highlight(),e=null},this.options.interval)})}highlight(){let e=arguments.length>0&&arguments[0]!==void 0?arguments[0]:null,t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:!1;if(this.quill.selection.composing)return;this.quill.update(o.default.sources.USER);const i=this.quill.getSelection();(e==null?this.quill.scroll.descendants(f):[e]).forEach(r=>{r.highlight(this.highlightBlot,t)}),this.quill.update(o.default.sources.SILENT),i!=null&&this.quill.setSelection(i,o.default.sources.SILENT)}highlightBlot(e){let t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:"plain";if(t=this.languages[t]?t:"plain",t==="plain")return y.escapeText(e).split(`
`).reduce((s,r,l)=>(l!==0&&s.insert(`
`,{[c.default.blotName]:t}),s.insert(r)),new k.default);const i=this.quill.root.ownerDocument.createElement("div");return i.classList.add(c.default.className),i.innerHTML=E(this.options.hljs,t,e),x.traverse(this.quill.scroll,i,[(s,r)=>{const l=d.value(s);return l?r.compose(new k.default().retain(r.length(),{[n.blotName]:l})):r}],[(s,r)=>s.data.split(`
`).reduce((l,g,h)=>(h!==0&&l.insert(`
`,{[c.default.blotName]:t}),l.insert(g)),r)],new WeakMap)}}S.DEFAULTS={hljs:window.hljs,interval:1e3,languages:[{key:"plain",label:"Plain"},{key:"bash",label:"Bash"},{key:"cpp",label:"C++"},{key:"cs",label:"C#"},{key:"css",label:"CSS"},{key:"diff",label:"Diff"},{key:"xml",label:"HTML/XML"},{key:"java",label:"Java"},{key:"javascript",label:"JavaScript"},{key:"markdown",label:"Markdown"},{key:"php",label:"PHP"},{key:"python",label:"Python"},{key:"ruby",label:"Ruby"},{key:"sql",label:"SQL"}]};exports.CodeBlock=a;exports.CodeToken=n;exports.default=S;
