"use strict";var O=Object.defineProperty;var C=(r,t,n)=>t in r?O(r,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):r[t]=n;var S=(r,t,n)=>(C(r,typeof t!="symbol"?t+"":t,n),n);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const h=require("../../../../parchment@3.0.0/node_modules/parchment/dist/parchment.js"),c=require("../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.js"),I=require("../blots/block.js"),B=require("../core/logger.js"),P=require("../core/module.js"),g=require("../core/quill.js"),L=require("../formats/align.js"),_=require("../formats/background.js"),y=require("../formats/code.js"),R=require("../formats/color.js"),v=require("../formats/direction.js"),U=require("../formats/font.js"),F=require("../formats/size.js"),H=require("./keyboard.js"),z=require("./normalizeExternalHTML/index.js"),W=B.default("quill:clipboard"),j=[[Node.TEXT_NODE,M],[Node.TEXT_NODE,T],["br",$],[Node.ELEMENT_NODE,T],[Node.ELEMENT_NODE,A],[Node.ELEMENT_NODE,w],[Node.ELEMENT_NODE,Q],["li",J],["ol, ul",K],["pre",V],["tr",Z],["b",N("bold")],["i",N("italic")],["strike",N("strike")],["style",Y]],X=[L.AlignAttribute,v.DirectionAttribute].reduce((r,t)=>(r[t.keyName]=t,r),{}),D=[L.AlignStyle,_.BackgroundStyle,R.ColorStyle,v.DirectionStyle,U.FontStyle,F.SizeStyle].reduce((r,t)=>(r[t.keyName]=t,r),{});class k extends P.default{constructor(t,n){super(t,n),this.quill.root.addEventListener("copy",e=>this.onCaptureCopy(e,!1)),this.quill.root.addEventListener("cut",e=>this.onCaptureCopy(e,!0)),this.quill.root.addEventListener("paste",this.onCapturePaste.bind(this)),this.matchers=[],j.concat(this.options.matchers??[]).forEach(e=>{let[i,l]=e;this.addMatcher(i,l)})}addMatcher(t,n){this.matchers.push([t,n])}convert(t){let{html:n,text:e}=t,i=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(i[y.default.blotName])return new c.default().insert(e||"",{[y.default.blotName]:i[y.default.blotName]});if(!n)return new c.default().insert(e||"",i);const l=this.convertHTML(n);return d(l,`
`)&&(l.ops[l.ops.length-1].attributes==null||i.table)?l.compose(new c.default().retain(l.length()-1).delete(1)):l}normalizeHTML(t){z.default(t)}convertHTML(t){const n=new DOMParser().parseFromString(t,"text/html");this.normalizeHTML(n);const e=n.body,i=new WeakMap,[l,u]=this.prepareMatching(e,i);return q(this.quill.scroll,e,l,u,i)}dangerouslyPasteHTML(t,n){let e=arguments.length>2&&arguments[2]!==void 0?arguments[2]:g.default.sources.API;if(typeof t=="string"){const i=this.convert({html:t,text:""});this.quill.setContents(i,n),this.quill.setSelection(0,g.default.sources.SILENT)}else{const i=this.convert({html:n,text:""});this.quill.updateContents(new c.default().retain(t).concat(i),e),this.quill.setSelection(t+i.length(),g.default.sources.SILENT)}}onCaptureCopy(t){var u,s;let n=arguments.length>1&&arguments[1]!==void 0?arguments[1]:!1;if(t.defaultPrevented)return;t.preventDefault();const[e]=this.quill.selection.getRange();if(e==null)return;const{html:i,text:l}=this.onCopy(e,n);(u=t.clipboardData)==null||u.setData("text/plain",l),(s=t.clipboardData)==null||s.setData("text/html",i),n&&H.deleteRange({range:e,quill:this.quill})}normalizeURIList(t){return t.split(/\r?\n/).filter(n=>n[0]!=="#").join(`
`)}onCapturePaste(t){var u,s,a,o,b;if(t.defaultPrevented||!this.quill.isEnabled())return;t.preventDefault();const n=this.quill.getSelection(!0);if(n==null)return;const e=(u=t.clipboardData)==null?void 0:u.getData("text/html");let i=(s=t.clipboardData)==null?void 0:s.getData("text/plain");if(!e&&!i){const p=(a=t.clipboardData)==null?void 0:a.getData("text/uri-list");p&&(i=this.normalizeURIList(p))}const l=Array.from(((o=t.clipboardData)==null?void 0:o.files)||[]);if(!e&&l.length>0){this.quill.uploader.upload(n,l);return}if(e&&l.length>0){const p=new DOMParser().parseFromString(e,"text/html");if(p.body.childElementCount===1&&((b=p.body.firstElementChild)==null?void 0:b.tagName)==="IMG"){this.quill.uploader.upload(n,l);return}}this.onPaste(n,{html:e,text:i})}onCopy(t){const n=this.quill.getText(t);return{html:this.quill.getSemanticHTML(t),text:n}}onPaste(t,n){let{text:e,html:i}=n;const l=this.quill.getFormat(t.index),u=this.convert({text:e,html:i},l);W.log("onPaste",u,{text:e,html:i});const s=new c.default().retain(t.index).delete(t.length).concat(u);this.quill.updateContents(s,g.default.sources.USER),this.quill.setSelection(s.length()-t.length,g.default.sources.SILENT),this.quill.scrollSelectionIntoView()}prepareMatching(t,n){const e=[],i=[];return this.matchers.forEach(l=>{const[u,s]=l;switch(u){case Node.TEXT_NODE:i.push(s);break;case Node.ELEMENT_NODE:e.push(s);break;default:Array.from(t.querySelectorAll(u)).forEach(a=>{if(n.has(a)){const o=n.get(a);o==null||o.push(s)}else n.set(a,[s])});break}}),[e,i]}}S(k,"DEFAULTS",{matchers:[]});function m(r,t,n,e){return e.query(t)?r.reduce((i,l)=>{if(!l.insert)return i;if(l.attributes&&l.attributes[t])return i.push(l);const u=n?{[t]:n}:{};return i.insert(l.insert,{...u,...l.attributes})},new c.default):r}function d(r,t){let n="";for(let e=r.ops.length-1;e>=0&&n.length<t.length;--e){const i=r.ops[e];if(typeof i.insert!="string")break;n=i.insert+n}return n.slice(-1*t.length)===t}function f(r,t){if(!(r instanceof Element))return!1;const n=t.query(r);return n&&n.prototype instanceof h.EmbedBlot?!1:["address","article","blockquote","canvas","dd","div","dl","dt","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","iframe","li","main","nav","ol","output","p","pre","section","table","td","tr","ul","video"].includes(r.tagName.toLowerCase())}function G(r,t){return r.previousElementSibling&&r.nextElementSibling&&!f(r.previousElementSibling,t)&&!f(r.nextElementSibling,t)}const E=new WeakMap;function x(r){return r==null?!1:(E.has(r)||(r.tagName==="PRE"?E.set(r,!0):E.set(r,x(r.parentNode))),E.get(r))}function q(r,t,n,e,i){return t.nodeType===t.TEXT_NODE?e.reduce((l,u)=>u(t,l,r),new c.default):t.nodeType===t.ELEMENT_NODE?Array.from(t.childNodes||[]).reduce((l,u)=>{let s=q(r,u,n,e,i);return u.nodeType===t.ELEMENT_NODE&&(s=n.reduce((a,o)=>o(u,a,r),s),s=(i.get(u)||[]).reduce((a,o)=>o(u,a,r),s)),l.concat(s)},new c.default):new c.default}function N(r){return(t,n,e)=>m(n,r,!0,e)}function w(r,t,n){const e=h.Attributor.keys(r),i=h.ClassAttributor.keys(r),l=h.StyleAttributor.keys(r),u={};return e.concat(i).concat(l).forEach(s=>{let a=n.query(s,h.Scope.ATTRIBUTE);a!=null&&(u[a.attrName]=a.value(r),u[a.attrName])||(a=X[s],a!=null&&(a.attrName===s||a.keyName===s)&&(u[a.attrName]=a.value(r)||void 0),a=D[s],a!=null&&(a.attrName===s||a.keyName===s)&&(a=D[s],u[a.attrName]=a.value(r)||void 0))}),Object.entries(u).reduce((s,a)=>{let[o,b]=a;return m(s,o,b,n)},t)}function A(r,t,n){const e=n.query(r);if(e==null)return t;if(e.prototype instanceof h.EmbedBlot){const i={},l=e.value(r);if(l!=null)return i[e.blotName]=l,new c.default().insert(i,e.formats(r,n))}else if(e.prototype instanceof h.BlockBlot&&!d(t,`
`)&&t.insert(`
`),"blotName"in e&&"formats"in e&&typeof e.formats=="function")return m(t,e.blotName,e.formats(r,n),n);return t}function $(r,t){return d(t,`
`)||t.insert(`
`),t}function V(r,t,n){const e=n.query("code-block"),i=e&&"formats"in e&&typeof e.formats=="function"?e.formats(r,n):!0;return m(t,"code-block",i,n)}function Y(){return new c.default}function J(r,t,n){const e=n.query(r);if(e==null||e.blotName!=="list"||!d(t,`
`))return t;let i=-1,l=r.parentNode;for(;l!=null;)["OL","UL"].includes(l.tagName)&&(i+=1),l=l.parentNode;return i<=0?t:t.reduce((u,s)=>s.insert?s.attributes&&typeof s.attributes.indent=="number"?u.push(s):u.insert(s.insert,{indent:i,...s.attributes||{}}):u,new c.default)}function K(r,t,n){const e=r;let i=e.tagName==="OL"?"ordered":"bullet";const l=e.getAttribute("data-checked");return l&&(i=l==="true"?"checked":"unchecked"),m(t,"list",i,n)}function T(r,t,n){if(!d(t,`
`)){if(f(r,n)&&(r.childNodes.length>0||r instanceof HTMLParagraphElement))return t.insert(`
`);if(t.length()>0&&r.nextSibling){let e=r.nextSibling;for(;e!=null;){if(f(e,n))return t.insert(`
`);const i=n.query(e);if(i&&i.prototype instanceof I.BlockEmbed)return t.insert(`
`);e=e.firstChild}}}return t}function Q(r,t,n){var l;const e={},i=r.style||{};return i.fontStyle==="italic"&&(e.italic=!0),i.textDecoration==="underline"&&(e.underline=!0),i.textDecoration==="line-through"&&(e.strike=!0),((l=i.fontWeight)!=null&&l.startsWith("bold")||parseInt(i.fontWeight,10)>=700)&&(e.bold=!0),t=Object.entries(e).reduce((u,s)=>{let[a,o]=s;return m(u,a,o,n)},t),parseFloat(i.textIndent||0)>0?new c.default().insert("	").concat(t):t}function Z(r,t,n){var i,l;const e=((i=r.parentElement)==null?void 0:i.tagName)==="TABLE"?r.parentElement:(l=r.parentElement)==null?void 0:l.parentElement;if(e!=null){const s=Array.from(e.querySelectorAll("tr")).indexOf(r)+1;return m(t,"table",s,n)}return t}function M(r,t,n){var i;let e=r.data;if(((i=r.parentElement)==null?void 0:i.tagName)==="O:P")return t.insert(e.trim());if(!x(r)){if(e.trim().length===0&&e.includes(`
`)&&!G(r,n))return t;const l=(u,s)=>{const a=s.replace(/[^\u00a0]/g,"");return a.length<1&&u?" ":a};e=e.replace(/\r\n/g," ").replace(/\n/g," "),e=e.replace(/\s\s+/g,l.bind(l,!0)),(r.previousSibling==null&&r.parentElement!=null&&f(r.parentElement,n)||r.previousSibling instanceof Element&&f(r.previousSibling,n))&&(e=e.replace(/^\s+/,l.bind(l,!1))),(r.nextSibling==null&&r.parentElement!=null&&f(r.parentElement,n)||r.nextSibling instanceof Element&&f(r.nextSibling,n))&&(e=e.replace(/\s+$/,l.bind(l,!1)))}return t.insert(e)}exports.default=k;exports.matchAttributor=w;exports.matchBlot=A;exports.matchNewline=T;exports.matchText=M;exports.traverse=q;
