"use strict";var O=Object.defineProperty;var C=(o,t,s)=>t in o?O(o,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):o[t]=s;var m=(o,t,s)=>(C(o,typeof t!="symbol"?t+"":t,s),s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const f=require("../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.js"),d=require("../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.js"),b=require("../core/emitter.js"),u=require("./block.js"),N=require("./break.js"),S=require("./container.js");function A(o){return o instanceof u.default||o instanceof u.BlockEmbed}function B(o){return typeof o.updateContent=="function"}class p extends f.ScrollBlot{constructor(t,s,e){let{emitter:i}=e;super(t,s),this.emitter=i,this.batch=!1,this.optimize(),this.enable(),this.domNode.addEventListener("dragstart",n=>this.handleDragStart(n))}batchStart(){Array.isArray(this.batch)||(this.batch=[])}batchEnd(){if(!this.batch)return;const t=this.batch;this.batch=!1,this.update(t)}emitMount(t){this.emitter.emit(b.default.events.SCROLL_BLOT_MOUNT,t)}emitUnmount(t){this.emitter.emit(b.default.events.SCROLL_BLOT_UNMOUNT,t)}emitEmbedUpdate(t,s){this.emitter.emit(b.default.events.SCROLL_EMBED_UPDATE,t,s)}deleteAt(t,s){const[e,i]=this.line(t),[n]=this.line(t+s);if(super.deleteAt(t,s),n!=null&&e!==n&&i>0){if(e instanceof u.BlockEmbed||n instanceof u.BlockEmbed){this.optimize();return}const r=n.children.head instanceof N.default?null:n.children.head;e.moveChildren(n,r),e.remove()}this.optimize()}enable(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:!0;this.domNode.setAttribute("contenteditable",t?"true":"false")}formatAt(t,s,e,i){super.formatAt(t,s,e,i),this.optimize()}insertAt(t,s,e){if(t>=this.length())if(e==null||this.scroll.query(s,f.Scope.BLOCK)==null){const i=this.scroll.create(this.statics.defaultChild.blotName);this.appendChild(i),e==null&&s.endsWith(`
`)?i.insertAt(0,s.slice(0,-1),e):i.insertAt(0,s,e)}else{const i=this.scroll.create(s,e);this.appendChild(i)}else super.insertAt(t,s,e);this.optimize()}insertBefore(t,s){if(t.statics.scope===f.Scope.INLINE_BLOT){const e=this.scroll.create(this.statics.defaultChild.blotName);e.appendChild(t),super.insertBefore(e,s)}else super.insertBefore(t,s)}insertContents(t,s){const e=this.deltaToRenderBlocks(s.concat(new d.default().insert(`
`))),i=e.pop();if(i==null)return;this.batchStart();const n=e.shift();if(n){const a=n.type==="block"&&(n.delta.length()===0||!this.descendant(u.BlockEmbed,t)[0]&&t<this.length()),c=n.type==="block"?n.delta:new d.default().insert({[n.key]:n.value});E(this,t,c);const h=n.type==="block"?1:0,g=t+c.length()+h;a&&this.insertAt(g-1,`
`);const L=u.bubbleFormats(this.line(t)[0]),y=d.DeltaExports.AttributeMap.diff(L,n.attributes)||{};Object.keys(y).forEach(k=>{this.formatAt(g-1,1,k,y[k])}),t=g}let[r,l]=this.children.find(t);if(e.length&&(r&&(r=r.split(l),l=0),e.forEach(a=>{if(a.type==="block"){const c=this.createBlock(a.attributes,r||void 0);E(c,0,a.delta)}else{const c=this.create(a.key,a.value);this.insertBefore(c,r||void 0),Object.keys(a.attributes).forEach(h=>{c.format(h,a.attributes[h])})}})),i.type==="block"&&i.delta.length()){const a=r?r.offset(r.scroll)+l:this.length();E(this,a,i.delta)}this.batchEnd(),this.optimize()}isEnabled(){return this.domNode.getAttribute("contenteditable")==="true"}leaf(t){const s=this.path(t).pop();if(!s)return[null,-1];const[e,i]=s;return e instanceof f.LeafBlot?[e,i]:[null,-1]}line(t){return t===this.length()?this.line(t-1):this.descendant(A,t)}lines(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:0,s=arguments.length>1&&arguments[1]!==void 0?arguments[1]:Number.MAX_VALUE;const e=(i,n,r)=>{let l=[],a=r;return i.children.forEachAt(n,r,(c,h,g)=>{A(c)?l.push(c):c instanceof f.ContainerBlot&&(l=l.concat(e(c,h,a))),a-=g}),l};return e(this,t,s)}optimize(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:[],s=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};this.batch||(super.optimize(t,s),t.length>0&&this.emitter.emit(b.default.events.SCROLL_OPTIMIZE,t,s))}path(t){return super.path(t).slice(1)}remove(){}update(t){if(this.batch){Array.isArray(t)&&(this.batch=this.batch.concat(t));return}let s=b.default.sources.USER;typeof t=="string"&&(s=t),Array.isArray(t)||(t=this.observer.takeRecords()),t=t.filter(e=>{let{target:i}=e;const n=this.find(i,!0);return n&&!B(n)}),t.length>0&&this.emitter.emit(b.default.events.SCROLL_BEFORE_UPDATE,s,t),super.update(t.concat([])),t.length>0&&this.emitter.emit(b.default.events.SCROLL_UPDATE,s,t)}updateEmbedAt(t,s,e){const[i]=this.descendant(n=>n instanceof u.BlockEmbed,t);i&&i.statics.blotName===s&&B(i)&&i.updateContent(e)}handleDragStart(t){t.preventDefault()}deltaToRenderBlocks(t){const s=[];let e=new d.default;return t.forEach(i=>{const n=i==null?void 0:i.insert;if(n)if(typeof n=="string"){const r=n.split(`
`);r.slice(0,-1).forEach(a=>{e.insert(a,i.attributes),s.push({type:"block",delta:e,attributes:i.attributes??{}}),e=new d.default});const l=r[r.length-1];l&&e.insert(l,i.attributes)}else{const r=Object.keys(n)[0];if(!r)return;this.query(r,f.Scope.INLINE)?e.push(i):(e.length()&&s.push({type:"block",delta:e,attributes:{}}),e=new d.default,s.push({type:"blockEmbed",key:r,value:n[r],attributes:i.attributes??{}}))}}),e.length()&&s.push({type:"block",delta:e,attributes:{}}),s}createBlock(t,s){let e;const i={};Object.entries(t).forEach(l=>{let[a,c]=l;this.query(a,f.Scope.BLOCK&f.Scope.BLOT)!=null?e=a:i[a]=c});const n=this.create(e||this.statics.defaultChild.blotName,e?t[e]:void 0);this.insertBefore(n,s||void 0);const r=n.length();return Object.entries(i).forEach(l=>{let[a,c]=l;n.formatAt(0,r,a,c)}),n}}m(p,"blotName","scroll"),m(p,"className","ql-editor"),m(p,"tagName","DIV"),m(p,"defaultChild",u.default),m(p,"allowedChildren",[u.default,u.BlockEmbed,S.default]);function E(o,t,s){s.reduce((e,i)=>{const n=d.DeltaExports.Op.length(i);let r=i.attributes||{};if(i.insert!=null){if(typeof i.insert=="string"){const l=i.insert;o.insertAt(e,l);const[a]=o.descendant(f.LeafBlot,e),c=u.bubbleFormats(a);r=d.DeltaExports.AttributeMap.diff(c,r)||{}}else if(typeof i.insert=="object"){const l=Object.keys(i.insert)[0];if(l==null)return e;if(o.insertAt(e,l,i.insert[l]),o.scroll.query(l,f.Scope.INLINE)!=null){const[c]=o.descendant(f.LeafBlot,e),h=u.bubbleFormats(c);r=d.DeltaExports.AttributeMap.diff(h,r)||{}}}}return Object.keys(r).forEach(l=>{o.formatAt(e,n,l,r[l])}),e+n},t)}exports.default=p;
