mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 15:23:29 -05:00
12 lines
51 KiB
JavaScript
12 lines
51 KiB
JavaScript
var st=()=>!0,E=class{constructor(n,e,t){this.root=n,this.currentNode=n,this.nodeType=e,this.filter=t||st}isAcceptableNode(n){let e=n.nodeType;return!!((e===Node.ELEMENT_NODE?1:e===Node.TEXT_NODE?4:0)&this.nodeType)&&this.filter(n)}nextNode(){let n=this.root,e=this.currentNode,t;for(;;){for(t=e.firstChild;!t&&e&&e!==n;)t=e.nextSibling,t||(e=e.parentNode);if(!t)return null;if(this.isAcceptableNode(t))return this.currentNode=t,t;e=t}}previousNode(){let n=this.root,e=this.currentNode,t;for(;;){if(e===n)return null;if(t=e.previousSibling,t)for(;e=t.lastChild;)t=e;else t=e.parentNode;if(!t)return null;if(this.isAcceptableNode(t))return this.currentNode=t,t;e=t}}previousPONode(){let n=this.root,e=this.currentNode,t;for(;;){for(t=e.lastChild;!t&&e&&e!==n;)t=e.previousSibling,t||(e=e.parentNode);if(!t)return null;if(this.isAcceptableNode(t))return this.currentNode=t,t;e=t}}};var k="\u200B",ee=navigator.userAgent,de=/Mac OS X/.test(ee),fe=/Windows NT/.test(ee),_e=/iP(?:ad|hone|od)/.test(ee)||de&&!!navigator.maxTouchPoints,Fe=/Android/.test(ee),He=/Gecko\//.test(ee),se=/Edge\//.test(ee),rt=!se&&/WebKit\//.test(ee),B=de||_e?"Meta-":"Ctrl-",V=rt,Pe="onbeforeinput"in document&&"inputType"in new InputEvent("input"),I=/[^ \t\r\n]/;var ct=/^(?:#text|A(?:BBR|CRONYM)?|B(?:R|D[IO])?|C(?:ITE|ODE)|D(?:ATA|EL|FN)|EM|FONT|HR|I(?:FRAME|MG|NPUT|NS)?|KBD|Q|R(?:P|T|UBY)|S(?:AMP|MALL|PAN|TR(?:IKE|ONG)|U[BP])?|TIME|U|VAR|WBR)$/,dt=new Set(["BR","HR","IFRAME","IMG","INPUT"]),ft=0,xe=1,Ue=2,qe=3,ue=new WeakMap,We=()=>{ue=new WeakMap},M=o=>dt.has(o.nodeName),ve=o=>{switch(o.nodeType){case 3:return xe;case 1:case 11:if(ue.has(o))return ue.get(o);break;default:return ft}let n;return Array.from(o.childNodes).every(N)?ct.test(o.nodeName)?n=xe:n=Ue:n=qe,ue.set(o,n),n},N=o=>ve(o)===xe,P=o=>ve(o)===Ue,Q=o=>ve(o)===qe;var p=(o,n,e)=>{let t=document.createElement(o);if(n instanceof Array&&(e=n,n=null),n)for(let i in n){let s=n[i];s!==void 0&&t.setAttribute(i,s)}return e&&e.forEach(i=>t.appendChild(i)),t},Le=(o,n)=>M(o)||o.nodeType!==n.nodeType||o.nodeName!==n.nodeName?!1:o instanceof HTMLElement&&n instanceof HTMLElement?o.nodeName!=="A"&&o.className===n.className&&o.style.cssText===n.style.cssText:!0,he=(o,n,e)=>{if(o.nodeName!==n)return!1;for(let t in e)if(!("getAttribute"in o)||o.getAttribute(t)!==e[t])return!1;return!0},S=(o,n,e,t)=>{for(;o&&o!==n;){if(he(o,e,t))return o;o=o.parentNode}return null},me=(o,n)=>{let e=o.childNodes;for(;n&&o instanceof Element;)o=e[n-1],e=o.childNodes,n=e.length;return o},Re=(o,n)=>{let e=o;if(e instanceof Element){let t=e.childNodes;if(n<t.length)e=t[n];else{for(;e&&!e.nextSibling;)e=e.parentNode;e&&(e=e.nextSibling)}}return e},y=o=>o instanceof Element||o instanceof DocumentFragment?o.childNodes.length:o instanceof CharacterData?o.length:0,T=o=>{let n=document.createDocumentFragment(),e=o.firstChild;for(;e;)n.appendChild(e),e=o.firstChild;return n},C=o=>{let n=o.parentNode;return n&&n.removeChild(o),o},R=(o,n)=>{let e=o.parentNode;e&&e.replaceChild(n,o)};var ut=o=>o instanceof Element?o.nodeName==="BR":I.test(o.data),te=(o,n)=>{let e=o.parentNode;for(;N(e);)e=e.parentNode;let t=new E(e,5,ut);return t.currentNode=o,!!t.nextNode()||n&&!t.previousNode()},re=(o,n)=>{let e=new E(o,4),t,i;for(;t=e.nextNode();)for(;(i=t.data.indexOf(k))>-1&&(!n||t.parentNode!==n);)if(t.length===1){let s=t,r=s.parentNode;for(;r&&(r.removeChild(s),e.currentNode=r,!(!N(r)||y(r)));)s=r,r=s.parentNode;break}else t.deleteData(i,1)};var ht=0,mt=1,pt=2,Nt=3,K=(o,n,e)=>{let t=document.createRange();if(t.selectNode(n),e){let i=o.compareBoundaryPoints(Nt,t)>-1,s=o.compareBoundaryPoints(mt,t)<1;return!i&&!s}else{let i=o.compareBoundaryPoints(ht,t)<1,s=o.compareBoundaryPoints(pt,t)>-1;return i&&s}},_=o=>{let{startContainer:n,startOffset:e,endContainer:t,endOffset:i}=o;for(;!(n instanceof Text);){let s=n.childNodes[e];if(!s||M(s)){if(e&&(s=n.childNodes[e-1],s instanceof Text)){let r=s,l;for(;!r.length&&(l=r.previousSibling)&&l instanceof Text;)r.remove(),r=l;n=r,e=r.data.length}break}n=s,e=0}if(i)for(;!(t instanceof Text);){let s=t.childNodes[i-1];if(!s||M(s)){if(s&&s.nodeName==="BR"&&!te(s,!1)){i-=1;continue}break}t=s,i=y(t)}else for(;!(t instanceof Text);){let s=t.firstChild;if(!s||M(s))break;t=s}o.setStart(n,e),o.setEnd(t,i)},q=(o,n,e,t)=>{let i=o.startContainer,s=o.startOffset,r=o.endContainer,l=o.endOffset,a;for(n||(n=o.commonAncestorContainer),e||(e=n);!s&&i!==n&&i!==t;)a=i.parentNode,s=Array.from(a.childNodes).indexOf(i),i=a;for(;!(r===e||r===t||(r.nodeType!==3&&r.childNodes[l]&&r.childNodes[l].nodeName==="BR"&&!te(r.childNodes[l],!1)&&(l+=1),l!==y(r)));)a=r.parentNode,l=Array.from(a.childNodes).indexOf(r)+1,r=a;o.setStart(i,s),o.setEnd(r,l)},Oe=(o,n,e)=>{let t=S(o.endContainer,e,n);if(t&&(t=t.parentNode)){let i=o.cloneRange();q(i,t,t,e),i.endContainer===t&&(o.setStart(i.endContainer,i.endOffset),o.setEnd(i.endContainer,i.endOffset))}return o};var x=o=>{let n=null;if(o instanceof Text)return o;if(N(o)){let e=o.firstChild;if(V)for(;e&&e instanceof Text&&!e.data;)o.removeChild(e),e=o.firstChild;e||(V?n=document.createTextNode(k):n=document.createTextNode(""))}else if(o instanceof Element&&!o.querySelector("BR")){n=p("BR");let e=o,t;for(;(t=e.lastElementChild)&&!N(t);)e=t}if(n)try{o.appendChild(n)}catch{}return o},D=(o,n)=>{let e=null;return Array.from(o.childNodes).forEach(t=>{let i=t.nodeName==="BR";!i&&N(t)?(e||(e=p("DIV")),e.appendChild(t)):(i||e)&&(e||(e=p("DIV")),x(e),i?o.replaceChild(e,t):o.insertBefore(e,t),e=null),Q(t)&&D(t,n)}),e&&o.appendChild(x(e)),o},w=(o,n,e,t)=>{if(o instanceof Text&&o!==e){if(typeof n!="number")throw new Error("Offset must be a number to split text node!");if(!o.parentNode)throw new Error("Cannot split text node with no parent!");return w(o.parentNode,o.splitText(n),e,t)}let i=typeof n=="number"?n<o.childNodes.length?o.childNodes[n]:null:n,s=o.parentNode;if(!s||o===e||!(o instanceof Element))return i;let r=o.cloneNode(!1);for(;i;){let l=i.nextSibling;r.appendChild(i),i=l}return o instanceof HTMLOListElement&&S(o,t,"BLOCKQUOTE")&&(r.start=(+o.start||1)+o.childNodes.length-1),x(o),x(r),s.insertBefore(r,o.nextSibling),w(s,r,e,t)},Ke=(o,n)=>{let e=o.childNodes,t=e.length,i=[];for(;t--;){let s=e[t],r=t?e[t-1]:null;if(r&&N(s)&&Le(s,r))n.startContainer===s&&(n.startContainer=r,n.startOffset+=y(r)),n.endContainer===s&&(n.endContainer=r,n.endOffset+=y(r)),n.startContainer===o&&(n.startOffset>t?n.startOffset-=1:n.startOffset===t&&(n.startContainer=r,n.startOffset=y(r))),n.endContainer===o&&(n.endOffset>t?n.endOffset-=1:n.endOffset===t&&(n.endContainer=r,n.endOffset=y(r))),C(s),s instanceof Text?r.appendData(s.data):i.push(T(s));else if(s instanceof Element){let l;for(;l=i.pop();)s.appendChild(l);Ke(s,n)}}},ne=(o,n)=>{let e=o instanceof Text?o.parentNode:o;if(e instanceof Element){let t={startContainer:n.startContainer,startOffset:n.startOffset,endContainer:n.endContainer,endOffset:n.endOffset};Ke(e,t),n.setStart(t.startContainer,t.startOffset),n.setEnd(t.endContainer,t.endOffset)}},J=(o,n,e,t)=>{let i=n,s,r;for(;(s=i.parentNode)&&s!==t&&s instanceof Element&&s.childNodes.length===1;)i=s;C(i),r=o.childNodes.length;let l=o.lastChild;l&&l.nodeName==="BR"&&(o.removeChild(l),r-=1),o.appendChild(T(n)),e.setStart(o,r),e.collapse(!0),ne(o,e)},F=(o,n)=>{let e=o.previousSibling,t=o.firstChild,i=o.nodeName==="LI";if(!(i&&(!t||!/^[OU]L$/.test(t.nodeName)))){if(e&&Le(e,o)){if(!Q(e))if(i){let r=p("DIV");r.appendChild(T(e)),e.appendChild(r)}else return;C(o);let s=!Q(o);e.appendChild(T(o)),s&&D(e,n),t&&F(t,n)}else if(i){let s=p("DIV");o.insertBefore(s,t),x(s)}}};var ze={"font-weight":{regexp:/^bold|^700/i,replace(){return p("B")}},"font-style":{regexp:/^italic/i,replace(){return p("I")}},"font-family":{regexp:I,replace(o,n){return p("SPAN",{class:o.fontFamily,style:"font-family:"+n})}},"font-size":{regexp:I,replace(o,n){return p("SPAN",{class:o.fontSize,style:"font-size:"+n})}},"text-decoration":{regexp:/^underline/i,replace(){return p("U")}}},St=(o,n,e)=>{let t=o.style,i,s;for(let r in ze){let l=ze[r],a=t.getPropertyValue(r);if(a&&l.regexp.test(a)){let d=l.replace(e.classNames,a);if(d.nodeName===o.nodeName&&d.className===o.className)continue;s||(s=d),i&&i.appendChild(d),i=d,o.style.removeProperty(r)}}return s&&i&&(i.appendChild(T(o)),o.style.cssText?o.appendChild(s):R(o,s)),i||o},pe=o=>(n,e)=>{let t=p(o),i=n.attributes;for(let s=0,r=i.length;s<r;s+=1){let l=i[s];t.setAttribute(l.name,l.value)}return e.replaceChild(t,n),t.appendChild(T(n)),t},gt={1:"10",2:"13",3:"16",4:"18",5:"24",6:"32",7:"48"},Et={STRONG:pe("B"),EM:pe("I"),INS:pe("U"),STRIKE:pe("S"),SPAN:St,FONT:(o,n,e)=>{let t=o,i=t.face,s=t.size,r=t.color,l=e.classNames,a,d,c,f,u;return i&&(a=p("SPAN",{class:l.fontFamily,style:"font-family:"+i}),u=a,f=a),s&&(d=p("SPAN",{class:l.fontSize,style:"font-size:"+gt[s]+"px"}),u||(u=d),f&&f.appendChild(d),f=d),r&&/^#?([\dA-F]{3}){1,2}$/i.test(r)&&(r.charAt(0)!=="#"&&(r="#"+r),c=p("SPAN",{class:l.color,style:"color:"+r}),u||(u=c),f&&f.appendChild(c),f=c),(!u||!f)&&(u=f=p("SPAN")),n.replaceChild(u,t),f.appendChild(T(t)),f},TT:(o,n,e)=>{let t=p("SPAN",{class:e.classNames.fontFamily,style:'font-family:menlo,consolas,"courier new",monospace'});return n.replaceChild(t,o),t.appendChild(T(o)),t}},Tt=/^(?:A(?:DDRESS|RTICLE|SIDE|UDIO)|BLOCKQUOTE|CAPTION|D(?:[DLT]|IV)|F(?:IGURE|IGCAPTION|OOTER)|H[1-6]|HEADER|L(?:ABEL|EGEND|I)|O(?:L|UTPUT)|P(?:RE)?|SECTION|T(?:ABLE|BODY|D|FOOT|H|HEAD|R)|COL(?:GROUP)?|UL)$/,Ct=/^(?:HEAD|META|STYLE)/,Ne=(o,n,e)=>{let t=o.childNodes,i=o;for(;N(i);)i=i.parentNode;let s=new E(i,5);for(let r=0,l=t.length;r<l;r+=1){let a=t[r],d=a.nodeName,c=Et[d];if(a instanceof HTMLElement){let f=a.childNodes.length;if(c)a=c(a,o,n);else if(Ct.test(d)){o.removeChild(a),r-=1,l-=1;continue}else if(!Tt.test(d)&&!N(a)){r-=1,l+=f-1,o.replaceChild(T(a),a);continue}f&&Ne(a,n,e||d==="PRE")}else{if(a instanceof Text){let f=a.data,u=!I.test(f.charAt(0)),m=!I.test(f.charAt(f.length-1));if(e||!u&&!m)continue;if(u){s.currentNode=a;let h;for(;(h=s.previousPONode())&&!(h.nodeName==="IMG"||h instanceof Text&&I.test(h.data));)if(!N(h)){h=null;break}f=f.replace(/^[ \t\r\n]+/g,h?" ":"")}if(m){s.currentNode=a;let h;for(;(h=s.nextNode())&&!(h.nodeName==="IMG"||h instanceof Text&&I.test(h.data));)if(!N(h)){h=null;break}f=f.replace(/[ \t\r\n]+$/g,h?" ":"")}if(f){a.data=f;continue}}o.removeChild(a),r-=1,l-=1}}return o},Se=o=>{let n=o.childNodes,e=n.length;for(;e--;){let t=n[e];t instanceof Element&&!M(t)?(Se(t),N(t)&&!t.firstChild&&o.removeChild(t)):t instanceof Text&&!t.data&&o.removeChild(t)}},le=(o,n,e)=>{let t=o.querySelectorAll("BR"),i=[],s=t.length;for(let r=0;r<s;r+=1)i[r]=te(t[r],e);for(;s--;){let r=t[s],l=r.parentNode;l&&(i[s]?N(l)||D(l,n):C(r))}},ke=o=>o.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""");var ae=(o,n)=>{let e=new E(n,1,P);return e.currentNode=o,e},z=(o,n)=>{let e=ae(o,n).previousNode();return e!==n?e:null},W=(o,n)=>{let e=ae(o,n).nextNode();return e!==n?e:null},ce=o=>!o.textContent&&!o.querySelector("IMG");var L=(o,n)=>{let e=o.startContainer,t;if(N(e))t=z(e,n);else if(e!==n&&e instanceof HTMLElement&&P(e))t=e;else{let i=me(e,o.startOffset);t=W(i,n)}return t&&K(o,t,!0)?t:null},G=(o,n)=>{let e=o.endContainer,t;if(N(e))t=z(e,n);else if(e!==n&&e instanceof HTMLElement&&P(e))t=e;else{let i=Re(e,o.endOffset);if(!i||!n.contains(i)){i=n;let s;for(;s=i.lastChild;)i=s}t=z(i,n)}return t&&K(o,t,!0)?t:null},Ge=o=>o instanceof Text?I.test(o.data):o.nodeName==="IMG",X=(o,n)=>{let e=o.startContainer,t=o.startOffset,i;if(e instanceof Text){let l=e.data;for(let a=t;a>0;a-=1)if(l.charAt(a-1)!==k)return!1;i=e}else if(i=Re(e,t),i&&!n.contains(i)&&(i=null),!i&&(i=me(e,t),i instanceof Text&&i.length))return!1;let s=L(o,n);if(!s)return!1;let r=new E(s,5,Ge);return r.currentNode=i,!r.previousNode()},Z=(o,n)=>{let e=o.endContainer,t=o.endOffset,i;if(e instanceof Text){let l=e.data,a=l.length;for(let d=t;d<a;d+=1)if(l.charAt(d)!==k)return!1;i=e}else i=me(e,t);let s=G(o,n);if(!s)return!1;let r=new E(s,5,Ge);return r.currentNode=i,!r.nextNode()},ye=(o,n)=>{let e=L(o,n),t=G(o,n),i;e&&t&&(i=e.parentNode,o.setStart(i,Array.from(i.childNodes).indexOf(e)),i=t.parentNode,o.setEnd(i,Array.from(i.childNodes).indexOf(t)+1))};function $(o,n,e,t){let i=document.createRange();return i.setStart(o,n),e&&typeof t=="number"?i.setEnd(e,t):i.setEnd(o,n),i}var j=(o,n)=>{let{startContainer:e,startOffset:t,endContainer:i,endOffset:s}=o,r;if(e instanceof Text){let a=e.parentNode;if(r=a.childNodes,t===e.length)t=Array.from(r).indexOf(e)+1,o.collapsed&&(i=a,s=t);else{if(t){let d=e.splitText(t);i===e?(s-=t,i=d):i===a&&(s+=1),e=d}t=Array.from(r).indexOf(e)}e=a}else r=e.childNodes;let l=r.length;t===l?e.appendChild(n):e.insertBefore(n,r[t]),e===i&&(s+=r.length-l),o.setStart(e,t),o.setEnd(i,s)},Be=(o,n,e)=>{let t=document.createDocumentFragment();if(o.collapsed)return t;n||(n=o.commonAncestorContainer),n instanceof Text&&(n=n.parentNode);let i=o.startContainer,s=o.startOffset,r=w(o.endContainer,o.endOffset,n,e),l=0,a=w(i,s,n,e);for(;a&&a!==r;){let d=a.nextSibling;t.appendChild(a),a=d}return i instanceof Text&&r instanceof Text&&(i.appendData(r.data),C(r),r=i,l=s),o.setStart(i,s),r?o.setEnd(r,l):o.setEnd(n,n.childNodes.length),x(n),t},Ze=(o,n,e)=>{o.currentNode=e;let t;for(;t=o[n]();){if(t instanceof Text||M(t))return t;if(!N(t))return null}return null},H=(o,n)=>{let e=L(o,n),t=G(o,n),i=e!==t;e&&t&&(_(o),q(o,e,t,n));let s=Be(o,null,n);_(o),i&&(t=G(o,n),e&&t&&e!==t&&J(e,t,o,n)),e&&x(e);let r=n.firstChild;(!r||r.nodeName==="BR")&&(x(n),n.firstChild&&o.selectNodeContents(n.firstChild)),o.collapse(!0);let l=o.startContainer,a=o.startOffset,d=new E(n,5),c=l,f=a;(!(c instanceof Text)||f===c.data.length)&&(c=Ze(d,"nextNode",c),f=0);let u=l,m=a-1;(!(u instanceof Text)||m===-1)&&(u=Ze(d,"previousPONode",c||(l instanceof Text?l:l.childNodes[a]||l)),u instanceof Text&&(m=u.data.length));let h=null,g=0;return c instanceof Text&&c.data.charAt(f)===" "&&X(o,n)?(h=c,g=f):u instanceof Text&&u.data.charAt(m)===" "&&(c instanceof Text&&c.data.charAt(f)===" "||Z(o,n))&&(h=u,g=m),h&&h.replaceData(g,1,"\xA0"),o.setStart(l,a),o.collapse(!0),s},je=(o,n,e)=>{let t=n.firstChild&&N(n.firstChild),i;for(D(n,e),i=n;i=W(i,e);)x(i);o.collapsed||H(o,e),_(o),o.collapse(!1);let s=S(o.endContainer,e,"BLOCKQUOTE")||e,r=L(o,e),l=null,a=W(n,n),d=!t&&!!r&&ce(r);if(r&&a&&!d&&!S(a,n,"PRE")&&!S(a,n,"TABLE")){q(o,r,r,e),o.collapse(!0);let c=o.endContainer,f=o.endOffset;if(le(r,e,!1),N(c)){let u=w(c,f,z(c,e)||e,e);c=u.parentNode,f=Array.from(c.childNodes).indexOf(u)}if(f!==y(c))for(l=document.createDocumentFragment();i=c.childNodes[f];)l.appendChild(i);J(c,a,o,e),f=Array.from(c.parentNode.childNodes).indexOf(c)+1,c=c.parentNode,o.setEnd(c,f)}if(y(n)){d&&r&&(o.setEndBefore(r),o.collapse(!1),C(r)),q(o,s,s,e);let c=w(o.endContainer,o.endOffset,s,e),f=c?c.previousSibling:s.lastChild;s.insertBefore(n,c),c?o.setEndBefore(c):o.setEnd(s,y(s)),r=G(o,e),_(o);let u=o.endContainer,m=o.endOffset;c&&Q(c)&&F(c,e),c=f&&f.nextSibling,c&&Q(c)&&F(c,e),o.setEnd(u,m)}if(l&&r){let c=o.cloneRange();J(r,l,c,e),o.setEnd(c.endContainer,c.endOffset)}_(o)};var ge=o=>{if(o.collapsed)return"";let n=o.startContainer,e=o.endContainer,t=new E(o.commonAncestorContainer,5,a=>K(o,a,!0));t.currentNode=n;let i=n,s="",r=!1,l;for((!(i instanceof Element)&&!(i instanceof Text)||!t.filter(i))&&(i=t.nextNode());i;)i instanceof Text?(l=i.data,l&&/\S/.test(l)&&(i===e&&(l=l.slice(0,o.endOffset)),i===n&&(l=l.slice(o.startOffset)),s+=l,r=!0)):(i.nodeName==="BR"||r&&!N(i))&&(s+=`
|
||
`,r=!1),i=t.nextNode();return s=s.replace(/ /g," "),s};var De=Array.prototype.indexOf,Qe=(o,n,e,t,i,s,r)=>{let l=o.clipboardData;if(se||!l)return!1;let a=s?"":ge(n),d=L(n,e),c=G(n,e),f=e;d===c&&d?.contains(n.commonAncestorContainer)&&(f=d);let u;t?u=H(n,e):(n=n.cloneRange(),_(n),q(n,f,f,e),u=n.cloneContents());let m=n.commonAncestorContainer;for(m instanceof Text&&(m=m.parentNode);m&&m!==f;){let g=m.cloneNode(!1);g.appendChild(u),u=g,m=m.parentNode}let h;if(u.childNodes.length===1&&u.childNodes[0]instanceof Text)a=u.childNodes[0].data.replace(/ /g," "),r=!0;else{let g=p("DIV");g.appendChild(u),h=g.innerHTML,i&&(h=i(h))}return s&&h!==void 0&&(a=s(h)),fe&&(a=a.replace(/\r?\n/g,`\r
|
||
`)),!r&&h&&a!==h&&l.setData("text/html",h),l.setData("text/plain",a),o.preventDefault(),!0},Xe=function(o){let n=this.getSelection(),e=this._root;if(n.collapsed){o.preventDefault();return}this.saveUndoState(n),Qe(o,n,e,!0,this._config.willCutCopy,this._config.toPlainText,!1)||setTimeout(()=>{try{this._ensureBottomLine()}catch(i){this._config.didError(i)}},0),this.setSelection(n)},$e=function(o){Qe(o,this.getSelection(),this._root,!1,this._config.willCutCopy,this._config.toPlainText,!1)},Ae=function(o){this._isShiftDown=o.shiftKey},Ve=function(o){let n=o.clipboardData,e=n?.items,t=this._isShiftDown,i=!1,s=!1,r=null,l=null;if(e){let b=e.length;for(;b--;){let O=e[b],A=O.type;A==="text/html"?l=O:A==="text/plain"||A==="text/uri-list"?r=O:A==="text/rtf"?i=!0:/^image\/.*/.test(A)&&(s=!0)}if(s&&!(i&&l)){o.preventDefault(),this.fireEvent("pasteImage",{clipboardData:n});return}if(!se){o.preventDefault(),l&&(!t||!r)?l.getAsString(O=>{this.insertHTML(O,!0)}):r&&r.getAsString(O=>{let A=!1,Ie=this.getSelection();if(!Ie.collapsed&&I.test(Ie.toString())){let Me=this.linkRegExp.exec(O);A=!!Me&&Me[0].length===O.length}A?this.makeLink(O):this.insertPlainText(O,!0)});return}}let a=n?.types;if(!se&&a&&(De.call(a,"text/html")>-1||!He&&De.call(a,"text/plain")>-1&&De.call(a,"text/rtf")<0)){o.preventDefault();let b;!t&&(b=n.getData("text/html"))?this.insertHTML(b,!0):((b=n.getData("text/plain"))||(b=n.getData("text/uri-list")))&&this.insertPlainText(b,!0);return}let d=document.body,c=this.getSelection(),f=c.startContainer,u=c.startOffset,m=c.endContainer,h=c.endOffset,g=p("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});d.appendChild(g),c.selectNodeContents(g),this.setSelection(c),setTimeout(()=>{try{let b="",O=g,A;for(;g=O;)O=g.nextSibling,C(g),A=g.firstChild,A&&A===g.lastChild&&A instanceof HTMLDivElement&&(g=A),b+=g.innerHTML;this.setSelection($(f,u,m,h)),b&&this.insertHTML(b,!0)}catch(b){this._config.didError(b)}},0)},Ye=function(o){if(!o.dataTransfer)return;let n=o.dataTransfer.types,e=n.length,t=!1,i=!1;for(;e--;)switch(n[e]){case"text/plain":t=!0;break;case"text/html":i=!0;break;default:return}(i||t&&this.saveUndoState)&&this.saveUndoState()};var we=(o,n,e)=>{n.preventDefault(),o.splitBlock(n.shiftKey,e)};var oe=(o,n)=>{try{n||(n=o.getSelection());let e=n.startContainer;e instanceof Text&&(e=e.parentNode);let t=e;for(;N(t)&&(!t.textContent||t.textContent===k);)e=t,t=e.parentNode;e!==t&&(n.setStart(t,Array.from(t.childNodes).indexOf(e)),n.collapse(!0),t.removeChild(e),P(t)||(t=z(t,o._root)||o._root),x(t),_(n)),e===o._root&&(e=e.firstChild)&&e.nodeName==="BR"&&C(e),o._ensureBottomLine(),o.setSelection(n),o._updatePath(n,!0)}catch(e){o._config.didError(e)}},Ee=(o,n)=>{let e;for(;(e=o.parentNode)&&!(e===n||e.isContentEditable);)o=e;C(o)},Te=(o,n,e)=>{if(S(n,o._root,"A"))return;let t=n.data||"",i=Math.max(t.lastIndexOf(" ",e-1),t.lastIndexOf("\xA0",e-1))+1,s=t.slice(i,e),r=o.linkRegExp.exec(s);if(r){let l=o.getSelection();o._docWasChanged(),o._recordUndoState(l),o._getRangeAndRemoveBookmark(l);let a=i+r.index,d=a+r[0].length,c=l.startContainer===n,f=l.startOffset-d;a&&(n=n.splitText(a));let u=o._config.tagAttributes.a,m=p("A",Object.assign({href:r[1]?/^(?:ht|f)tps?:/i.test(r[1])?r[1]:"http://"+r[1]:"mailto:"+r[0]},u));m.textContent=t.slice(a,d),n.parentNode.insertBefore(m,n),n.data=t.slice(d),c&&(l.setStart(n,f),l.setEnd(n,f)),o.setSelection(l)}};var Je=(o,n,e)=>{let t=o._root;if(o._removeZWS(),o.saveUndoState(e),!e.collapsed)n.preventDefault(),H(e,t),oe(o,e);else if(X(e,t)){n.preventDefault();let i=L(e,t);if(!i)return;let s=i;D(s.parentNode,t);let r=z(s,t);if(r){if(!r.isContentEditable){Ee(r,t);return}for(J(r,s,e,t),s=r.parentNode;s!==t&&!s.nextSibling;)s=s.parentNode;s!==t&&(s=s.nextSibling)&&F(s,t),o.setSelection(e)}else if(s){if(S(s,t,"UL")||S(s,t,"OL")){o.decreaseListLevel(e);return}else if(S(s,t,"BLOCKQUOTE")){o.removeQuote(e);return}o.setSelection(e),o._updatePath(e,!0)}}else{_(e);let i=e.startContainer,s=e.startOffset,r=i.parentNode;i instanceof Text&&r instanceof HTMLAnchorElement&&s&&r.href.includes(i.data)?(i.deleteData(s-1,1),o.setSelection(e),o.removeLink(),n.preventDefault()):(o.setSelection(e),setTimeout(()=>{oe(o)},0))}};var et=(o,n,e)=>{let t=o._root,i,s,r,l,a,d;if(o._removeZWS(),o.saveUndoState(e),!e.collapsed)n.preventDefault(),H(e,t),oe(o,e);else if(Z(e,t)){if(n.preventDefault(),i=L(e,t),!i)return;if(D(i.parentNode,t),s=W(i,t),s){if(!s.isContentEditable){Ee(s,t);return}for(J(i,s,e,t),s=i.parentNode;s!==t&&!s.nextSibling;)s=s.parentNode;s!==t&&(s=s.nextSibling)&&F(s,t),o.setSelection(e),o._updatePath(e,!0)}}else{if(r=e.cloneRange(),q(e,t,t,t),l=e.endContainer,a=e.endOffset,l instanceof Element&&(d=l.childNodes[a],d&&d.nodeName==="IMG")){n.preventDefault(),C(d),_(e),oe(o,e);return}o.setSelection(r),setTimeout(()=>{oe(o)},0)}};var tt=(o,n,e)=>{let t=o._root;if(o._removeZWS(),e.collapsed&&X(e,t)){let i=L(e,t),s;for(;s=i.parentNode;){if(s.nodeName==="UL"||s.nodeName==="OL"){n.preventDefault(),o.increaseListLevel(e);break}i=s}}},nt=(o,n,e)=>{let t=o._root;if(o._removeZWS(),e.collapsed&&X(e,t)){let i=e.startContainer;(S(i,t,"UL")||S(i,t,"OL"))&&(n.preventDefault(),o.decreaseListLevel(e))}};var ot=(o,n,e)=>{let t,i=o._root;if(o._recordUndoState(e),o._getRangeAndRemoveBookmark(e),!e.collapsed)H(e,i),o._ensureBottomLine(),o.setSelection(e),o._updatePath(e,!0);else if(Z(e,i)){let s=L(e,i);if(s&&s.nodeName!=="PRE"){let r=s.textContent?.trimEnd().replace(k,"");if(r==="*"||r==="1."){n.preventDefault();let l=new E(s,4),a;for(;a=l.nextNode();)a.data=V?k:"";r==="*"?o.makeUnorderedList():o.makeOrderedList();return}}}if(t=e.endContainer,e.endOffset===y(t))do if(t.nodeName==="A"){e.setStartAfter(t);break}while(!t.nextSibling&&(t=t.parentNode)&&t!==i);if(o._config.addLinks){let s=e.cloneRange();_(s);let r=s.startContainer,l=s.startOffset;setTimeout(()=>{Te(o,r,l)},0)}o.setSelection(e)};var xt={8:"Backspace",9:"Tab",13:"Enter",27:"Escape",32:"Space",33:"PageUp",34:"PageDown",37:"ArrowLeft",38:"ArrowUp",39:"ArrowRight",40:"ArrowDown",46:"Delete",191:"/",219:"[",220:"\\",221:"]"},it=function(o){let n=o.keyCode,e=xt[n],t="",i=this.getSelection();o.defaultPrevented||(e||(e=String.fromCharCode(n).toLowerCase(),/^[A-Za-z0-9]$/.test(e)||(e="")),111<n&&n<124&&(e="F"+(n-111)),e!=="Backspace"&&e!=="Delete"&&(o.altKey&&(t+="Alt-"),o.ctrlKey&&(t+="Ctrl-"),o.metaKey&&(t+="Meta-"),o.shiftKey&&(t+="Shift-")),fe&&o.shiftKey&&e==="Delete"&&(t+="Shift-"),e=t+e,this._keyHandlers[e]?this._keyHandlers[e](this,o,i):!i.collapsed&&!o.isComposing&&!o.ctrlKey&&!o.metaKey&&(o.key||e).length===1&&(this.saveUndoState(i),H(i,this._root),this._ensureBottomLine(),this.setSelection(i),this._updatePath(i,!0)))},v={Backspace:Je,Delete:et,Tab:tt,"Shift-Tab":nt,Space:ot,ArrowLeft(o){o._removeZWS()},ArrowRight(o,n,e){o._removeZWS();let t=o.getRoot();if(Z(e,t)){_(e);let i=e.endContainer;do if(i.nodeName==="CODE"){let s=i.nextSibling;if(!(s instanceof Text)){let r=document.createTextNode("\xA0");i.parentNode.insertBefore(r,s),s=r}e.setStart(s,1),o.setSelection(e),n.preventDefault();break}while(!i.nextSibling&&(i=i.parentNode)&&i!==t)}}};Pe||(v.Enter=we,v["Shift-Enter"]=we);!de&&!_e&&(v.PageUp=o=>{o.moveCursorToStart()},v.PageDown=o=>{o.moveCursorToEnd()});var ie=(o,n)=>(n=n||null,(e,t)=>{t.preventDefault();let i=e.getSelection();e.hasFormat(o,null,i)?e.changeFormat(null,{tag:o},i):e.changeFormat({tag:o},n,i)});v[B+"b"]=ie("B");v[B+"i"]=ie("I");v[B+"u"]=ie("U");v[B+"Shift-7"]=ie("S");v[B+"Shift-5"]=ie("SUB",{tag:"SUP"});v[B+"Shift-6"]=ie("SUP",{tag:"SUB"});v[B+"Shift-8"]=(o,n)=>{n.preventDefault();let e=o.getPath();/(?:^|>)UL/.test(e)?o.removeList():o.makeUnorderedList()};v[B+"Shift-9"]=(o,n)=>{n.preventDefault();let e=o.getPath();/(?:^|>)OL/.test(e)?o.removeList():o.makeOrderedList()};v[B+"["]=(o,n)=>{n.preventDefault();let e=o.getPath();/(?:^|>)BLOCKQUOTE/.test(e)||!/(?:^|>)[OU]L/.test(e)?o.decreaseQuoteLevel():o.decreaseListLevel()};v[B+"]"]=(o,n)=>{n.preventDefault();let e=o.getPath();/(?:^|>)BLOCKQUOTE/.test(e)||!/(?:^|>)[OU]L/.test(e)?o.increaseQuoteLevel():o.increaseListLevel()};v[B+"d"]=(o,n)=>{n.preventDefault(),o.toggleCode()};v[B+"z"]=(o,n)=>{n.preventDefault(),o.undo()};v[B+"y"]=v[B+"Shift-z"]=(o,n)=>{n.preventDefault(),o.redo()};var Ce=class{constructor(n,e){this.customEvents=new Set(["pathChange","select","input","pasteImage","undoStateChange"]);this.startSelectionId="squire-selection-start";this.endSelectionId="squire-selection-end";this.linkRegExp=/\b(?:((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9][a-z0-9.\-]*[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:[^\s?&`!()\[\]{};:'".,<>«»“”‘’]|\([^\s()<>]+\)))|([\w\-.%+]+@(?:[\w\-]+\.)+[a-z]{2,}\b(?:[?][^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+(?:&[^&?\s]+=[^\s?&`!()\[\]{};:'".,<>«»“”‘’]+)*)?))/i;this.tagAfterSplit={DT:"DD",DD:"DT",LI:"LI",PRE:"PRE"};this._root=n,this._config=this._makeConfig(e),this._isFocused=!1,this._lastSelection=$(n,0),this._willRestoreSelection=!1,this._mayHaveZWS=!1,this._lastAnchorNode=null,this._lastFocusNode=null,this._path="",this._events=new Map,this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0,this._isInUndoState=!1,this._ignoreChange=!1,this._ignoreAllChanges=!1,this.addEventListener("selectionchange",this._updatePathOnEvent),this.addEventListener("blur",this._enableRestoreSelection),this.addEventListener("mousedown",this._disableRestoreSelection),this.addEventListener("touchstart",this._disableRestoreSelection),this.addEventListener("focus",this._restoreSelection),this._isShiftDown=!1,this.addEventListener("cut",Xe),this.addEventListener("copy",$e),this.addEventListener("paste",Ve),this.addEventListener("drop",Ye),this.addEventListener("keydown",Ae),this.addEventListener("keyup",Ae),this.addEventListener("keydown",it),this._keyHandlers=Object.create(v);let t=new MutationObserver(()=>this._docWasChanged());t.observe(n,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=t,n.setAttribute("contenteditable","true"),this.addEventListener("beforeinput",this._beforeInput),this.setHTML("")}destroy(){this._events.forEach((n,e)=>{this.removeEventListener(e)}),this._mutation.disconnect(),this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0}_makeConfig(n){let e={blockTag:"DIV",blockAttributes:null,tagAttributes:{},classNames:{color:"color",fontFamily:"font",fontSize:"size",highlight:"highlight"},undo:{documentSizeThreshold:-1,undoLimit:-1},addLinks:!0,willCutCopy:null,toPlainText:null,sanitizeToDOMFragment:t=>{let i=DOMPurify.sanitize(t,{ALLOW_UNKNOWN_PROTOCOLS:!0,WHOLE_DOCUMENT:!1,RETURN_DOM:!0,RETURN_DOM_FRAGMENT:!0,FORCE_BODY:!1});return i?document.importNode(i,!0):document.createDocumentFragment()},didError:t=>console.log(t)};return n&&(Object.assign(e,n),e.blockTag=e.blockTag.toUpperCase()),e}setKeyHandler(n,e){return this._keyHandlers[n]=e,this}_beforeInput(n){switch(n.inputType){case"insertText":Fe&&n.data&&n.data.includes(`
|
||
`)&&n.preventDefault();break;case"insertLineBreak":n.preventDefault(),this.splitBlock(!0);break;case"insertParagraph":n.preventDefault(),this.splitBlock(!1);break;case"insertOrderedList":n.preventDefault(),this.makeOrderedList();break;case"insertUnoderedList":n.preventDefault(),this.makeUnorderedList();break;case"historyUndo":n.preventDefault(),this.undo();break;case"historyRedo":n.preventDefault(),this.redo();break;case"formatBold":n.preventDefault(),this.bold();break;case"formaItalic":n.preventDefault(),this.italic();break;case"formatUnderline":n.preventDefault(),this.underline();break;case"formatStrikeThrough":n.preventDefault(),this.strikethrough();break;case"formatSuperscript":n.preventDefault(),this.superscript();break;case"formatSubscript":n.preventDefault(),this.subscript();break;case"formatJustifyFull":case"formatJustifyCenter":case"formatJustifyRight":case"formatJustifyLeft":{n.preventDefault();let e=n.inputType.slice(13).toLowerCase();e==="full"&&(e="justify"),this.setTextAlignment(e);break}case"formatRemove":n.preventDefault(),this.removeAllFormatting();break;case"formatSetBlockTextDirection":{n.preventDefault();let e=n.data;e==="null"&&(e=null),this.setTextDirection(e);break}case"formatBackColor":n.preventDefault(),this.setHighlightColor(n.data);break;case"formatFontColor":n.preventDefault(),this.setTextColor(n.data);break;case"formatFontName":n.preventDefault(),this.setFontFace(n.data);break}}handleEvent(n){this.fireEvent(n.type,n)}fireEvent(n,e){let t=this._events.get(n);if(/^(?:focus|blur)/.test(n)){let i=this._root===document.activeElement;if(n==="focus"){if(!i||this._isFocused)return this;this._isFocused=!0}else{if(i||!this._isFocused)return this;this._isFocused=!1}}if(t){let i=e instanceof Event?e:new CustomEvent(n,{detail:e});t=t.slice();for(let s of t)try{"handleEvent"in s?s.handleEvent(i):s.call(this,i)}catch(r){this._config.didError(r)}}return this}addEventListener(n,e){let t=this._events.get(n),i=this._root;return t||(t=[],this._events.set(n,t),this.customEvents.has(n)||(n==="selectionchange"&&(i=document),i.addEventListener(n,this,!0))),t.push(e),this}removeEventListener(n,e){let t=this._events.get(n),i=this._root;if(t){if(e){let s=t.length;for(;s--;)t[s]===e&&t.splice(s,1)}else t.length=0;t.length||(this._events.delete(n),this.customEvents.has(n)||(n==="selectionchange"&&(i=document),i.removeEventListener(n,this,!0)))}return this}focus(){return this._root.focus({preventScroll:!0}),this}blur(){return this._root.blur(),this}_enableRestoreSelection(){this._willRestoreSelection=!0}_disableRestoreSelection(){this._willRestoreSelection=!1}_restoreSelection(){this._willRestoreSelection&&this.setSelection(this._lastSelection)}_removeZWS(){this._mayHaveZWS&&(re(this._root),this._mayHaveZWS=!1)}_saveRangeToBookmark(n){let e=p("INPUT",{id:this.startSelectionId,type:"hidden"}),t=p("INPUT",{id:this.endSelectionId,type:"hidden"}),i;j(n,e),n.collapse(!1),j(n,t),e.compareDocumentPosition(t)&Node.DOCUMENT_POSITION_PRECEDING&&(e.id=this.endSelectionId,t.id=this.startSelectionId,i=e,e=t,t=i),n.setStartAfter(e),n.setEndBefore(t)}_getRangeAndRemoveBookmark(n){let e=this._root,t=e.querySelector("#"+this.startSelectionId),i=e.querySelector("#"+this.endSelectionId);if(t&&i){let s=t.parentNode,r=i.parentNode,l=Array.from(s.childNodes).indexOf(t),a=Array.from(r.childNodes).indexOf(i);s===r&&(a-=1),t.remove(),i.remove(),n||(n=document.createRange()),n.setStart(s,l),n.setEnd(r,a),ne(s,n),s!==r&&ne(r,n),n.collapsed&&(s=n.startContainer,s instanceof Text&&(r=s.childNodes[n.startOffset],(!r||!(r instanceof Text))&&(r=s.childNodes[n.startOffset-1]),r&&r instanceof Text&&(n.setStart(r,0),n.collapse(!0))))}return n||null}getSelection(){let n=window.getSelection(),e=this._root,t=null;if(this._isFocused&&n&&n.rangeCount){t=n.getRangeAt(0).cloneRange();let i=t.startContainer,s=t.endContainer;i&&M(i)&&t.setStartBefore(i),s&&M(s)&&t.setEndBefore(s)}return t&&e.contains(t.commonAncestorContainer)?this._lastSelection=t:(t=this._lastSelection,document.contains(t.commonAncestorContainer)||(t=null)),t||(t=$(e.firstElementChild||e,0)),t}setSelection(n){if(this._lastSelection=n,!this._isFocused)this._enableRestoreSelection();else{let e=window.getSelection();e&&("setBaseAndExtent"in Selection.prototype?e.setBaseAndExtent(n.startContainer,n.startOffset,n.endContainer,n.endOffset):(e.removeAllRanges(),e.addRange(n)))}return this}_moveCursorTo(n){let e=this._root,t=$(e,n?0:e.childNodes.length);return _(t),this.setSelection(t),this}moveCursorToStart(){return this._moveCursorTo(!0)}moveCursorToEnd(){return this._moveCursorTo(!1)}getCursorPosition(){let n=this.getSelection(),e=n.getBoundingClientRect();if(e&&!e.top){this._ignoreChange=!0;let t=p("SPAN");t.textContent=k,j(n,t),e=t.getBoundingClientRect();let i=t.parentNode;i.removeChild(t),ne(i,n)}return e}getPath(){return this._path}_updatePathOnEvent(){this._isFocused&&this._updatePath(this.getSelection())}_updatePath(n,e){let t=n.startContainer,i=n.endContainer,s;(e||t!==this._lastAnchorNode||i!==this._lastFocusNode)&&(this._lastAnchorNode=t,this._lastFocusNode=i,s=t&&i?t===i?this._getPath(i):"(selection)":"",this._path!==s&&(this._path=s,this.fireEvent("pathChange",{path:s}))),this.fireEvent(n.collapsed?"cursor":"select",{range:n})}_getPath(n){let e=this._root,t=this._config,i="";if(n&&n!==e){let s=n.parentNode;if(i=s?this._getPath(s):"",n instanceof HTMLElement){let r=n.id,l=n.classList,a=Array.from(l).sort(),d=n.dir,c=t.classNames;i+=(i?">":"")+n.nodeName,r&&(i+="#"+r),a.length&&(i+=".",i+=a.join(".")),d&&(i+="[dir="+d+"]"),l.contains(c.highlight)&&(i+="[backgroundColor="+n.style.backgroundColor.replace(/ /g,"")+"]"),l.contains(c.color)&&(i+="[color="+n.style.color.replace(/ /g,"")+"]"),l.contains(c.fontFamily)&&(i+="[fontFamily="+n.style.fontFamily.replace(/ /g,"")+"]"),l.contains(c.fontSize)&&(i+="[fontSize="+n.style.fontSize+"]")}}return i}modifyDocument(n){let e=this._mutation;return e&&(e.takeRecords().length&&this._docWasChanged(),e.disconnect()),this._ignoreAllChanges=!0,n(),this._ignoreAllChanges=!1,e&&(e.observe(this._root,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._ignoreChange=!1),this}_docWasChanged(){if(We(),this._mayHaveZWS=!0,!this._ignoreAllChanges){if(this._ignoreChange){this._ignoreChange=!1;return}this._isInUndoState&&(this._isInUndoState=!1,this.fireEvent("undoStateChange",{canUndo:!0,canRedo:!1})),this.fireEvent("input")}}_recordUndoState(n,e){if(!this._isInUndoState||e){let t=this._undoIndex,i=this._undoStack,s=this._config.undo,r=s.documentSizeThreshold,l=s.undoLimit;e||(t+=1),t<this._undoStackLength&&(i.length=this._undoStackLength=t),n&&this._saveRangeToBookmark(n);let a=this._getRawHTML();r>-1&&a.length*2>r&&l>-1&&t>l&&(i.splice(0,t-l),t=l,this._undoStackLength=l),i[t]=a,this._undoIndex=t,this._undoStackLength+=1,this._isInUndoState=!0}return this}saveUndoState(n){return n||(n=this.getSelection()),this._recordUndoState(n,this._isInUndoState),this._getRangeAndRemoveBookmark(n),this}undo(){if(this._undoIndex!==0||!this._isInUndoState){this._recordUndoState(this.getSelection(),!1),this._undoIndex-=1,this._setRawHTML(this._undoStack[this._undoIndex]);let n=this._getRangeAndRemoveBookmark();n&&this.setSelection(n),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:this._undoIndex!==0,canRedo:!0}),this.fireEvent("input")}return this}redo(){let n=this._undoIndex,e=this._undoStackLength;if(n+1<e&&this._isInUndoState){this._undoIndex+=1,this._setRawHTML(this._undoStack[this._undoIndex]);let t=this._getRangeAndRemoveBookmark();t&&this.setSelection(t),this.fireEvent("undoStateChange",{canUndo:!0,canRedo:n+2<e}),this.fireEvent("input")}return this}getRoot(){return this._root}_getRawHTML(){return this._root.innerHTML}_setRawHTML(n){let e=this._root;e.innerHTML=n;let t=e,i=t.firstChild;if(!i||i.nodeName==="BR"){let s=this.createDefaultBlock();i?t.replaceChild(s,i):t.appendChild(s)}else for(;t=W(t,e);)x(t);return this._ignoreChange=!0,this}getHTML(n){let e;n&&(e=this.getSelection(),this._saveRangeToBookmark(e));let t=this._getRawHTML().replace(/\u200B/g,"");return n&&this._getRangeAndRemoveBookmark(e),t}setHTML(n){let e=this._config.sanitizeToDOMFragment(n,this),t=this._root;Ne(e,this._config),le(e,t,!1),D(e,t);let i=e,s=i.firstChild;if(!s||s.nodeName==="BR"){let l=this.createDefaultBlock();s?i.replaceChild(l,s):i.appendChild(l)}else for(;i=W(i,t);)x(i);for(this._ignoreChange=!0;s=t.lastChild;)t.removeChild(s);t.appendChild(e),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;let r=this._getRangeAndRemoveBookmark()||$(t.firstElementChild||t,0);return this.saveUndoState(r),this.setSelection(r),this._updatePath(r,!0),this}insertHTML(n,e){let t=this._config,i=t.sanitizeToDOMFragment(n,this),s=this.getSelection();this.saveUndoState(s);try{let r=this._root;t.addLinks&&this.addDetectedLinks(i,i),Ne(i,this._config),le(i,r,!1),Se(i),i.normalize();let l=i;for(;l=W(l,i);)x(l);let a=!0;if(e){let d=new CustomEvent("willPaste",{detail:{fragment:i}});this.fireEvent("willPaste",d),i=d.detail.fragment,a=!d.defaultPrevented}a&&(je(s,i,r),s.collapse(!1),Oe(s,"A",r),this._ensureBottomLine()),this.setSelection(s),this._updatePath(s,!0),e&&this.focus()}catch(r){this._config.didError(r)}return this}insertElement(n,e){if(e||(e=this.getSelection()),e.collapse(!0),N(n))j(e,n),e.setStartAfter(n);else{let t=this._root,i=L(e,t),s=i||t,r=null;for(;s!==t&&!s.nextSibling;)s=s.parentNode;if(s!==t){let a=s.parentNode;r=w(a,s.nextSibling,t,t)}i&&ce(i)&&C(i),t.insertBefore(n,r);let l=this.createDefaultBlock();t.insertBefore(l,r),e.setStart(l,0),e.setEnd(l,0),_(e)}return this.focus(),this.setSelection(e),this._updatePath(e),this}insertImage(n,e){let t=p("IMG",Object.assign({src:n},e));return this.insertElement(t),t}insertPlainText(n,e){let t=this.getSelection();if(t.collapsed&&S(t.startContainer,this._root,"PRE")){let c=t.startContainer,f=t.startOffset,u;if(!c||!(c instanceof Text)){let h=document.createTextNode("");c.insertBefore(h,c.childNodes[f]),u=h,f=0}else u=c;let m=!0;if(e){let h=new CustomEvent("willPaste",{detail:{text:n}});this.fireEvent("willPaste",h),n=h.detail.text,m=!h.defaultPrevented}return m&&(u.insertData(f,n),t.setStart(u,f+n.length),t.collapse(!0)),this.setSelection(t),this}let i=n.split(`
|
||
`),s=this._config,r=s.blockTag,l=s.blockAttributes,a="</"+r+">",d="<"+r;for(let c in l)d+=" "+c+'="'+ke(l[c])+'"';d+=">";for(let c=0,f=i.length;c<f;c+=1){let u=i[c];u=ke(u).replace(/ (?=(?: |$))/g," "),c&&(u=d+(u||"<BR>")+a),i[c]=u}return this.insertHTML(i.join(""),e)}getSelectedText(n){return ge(n||this.getSelection())}getFontInfo(n){let e={color:void 0,backgroundColor:void 0,fontFamily:void 0,fontSize:void 0};n||(n=this.getSelection());let t=0,i=n.commonAncestorContainer;if(n.collapsed||i instanceof Text)for(i instanceof Text&&(i=i.parentNode);t<4&&i;){let s=i.style;if(s){let r=s.color;!e.color&&r&&(e.color=r,t+=1);let l=s.backgroundColor;!e.backgroundColor&&l&&(e.backgroundColor=l,t+=1);let a=s.fontFamily;!e.fontFamily&&a&&(e.fontFamily=a,t+=1);let d=s.fontSize;!e.fontSize&&d&&(e.fontSize=d,t+=1)}i=i.parentNode}return e}hasFormat(n,e,t){n=n.toUpperCase(),e||(e={}),t||(t=this.getSelection()),!t.collapsed&&t.startContainer instanceof Text&&t.startOffset===t.startContainer.length&&t.startContainer.nextSibling&&t.setStartBefore(t.startContainer.nextSibling),!t.collapsed&&t.endContainer instanceof Text&&t.endOffset===0&&t.endContainer.previousSibling&&t.setEndAfter(t.endContainer.previousSibling);let i=this._root,s=t.commonAncestorContainer;if(S(s,i,n,e))return!0;if(s instanceof Text)return!1;let r=new E(s,4,d=>K(t,d,!0)),l=!1,a;for(;a=r.nextNode();){if(!S(a,i,n,e))return!1;l=!0}return l}changeFormat(n,e,t,i){return t||(t=this.getSelection()),this.saveUndoState(t),e&&(t=this._removeFormat(e.tag.toUpperCase(),e.attributes||{},t,i)),n&&(t=this._addFormat(n.tag.toUpperCase(),n.attributes||{},t)),this.setSelection(t),this._updatePath(t,!0),this.focus()}_addFormat(n,e,t){let i=this._root;if(t.collapsed){let s=x(p(n,e));j(t,s);let r=s.firstChild||s,l=r instanceof Text?r.length:0;t.setStart(r,l),t.collapse(!0);let a=s;for(;N(a);)a=a.parentNode;re(a,s)}else{let s=new E(t.commonAncestorContainer,5,c=>(c instanceof Text||c.nodeName==="BR"||c.nodeName==="IMG")&&K(t,c,!0)),{startContainer:r,startOffset:l,endContainer:a,endOffset:d}=t;if(s.currentNode=r,!(r instanceof Element)&&!(r instanceof Text)||!s.filter(r)){let c=s.nextNode();if(!c)return t;r=c,l=0}do{let c=s.currentNode;if(!S(c,i,n,e)){c===a&&c.length>d&&c.splitText(d),c===r&&l&&(c=c.splitText(l),a===r?(a=c,d-=l):a===r.parentNode&&(d+=1),r=c,l=0);let u=p(n,e);R(c,u),u.appendChild(c)}}while(s.nextNode());t=$(r,l,a,d)}return t}_removeFormat(n,e,t,i){this._saveRangeToBookmark(t);let s;t.collapsed&&(V?s=document.createTextNode(k):s=document.createTextNode(""),j(t,s));let r=t.commonAncestorContainer;for(;N(r);)r=r.parentNode;let l=t.startContainer,a=t.startOffset,d=t.endContainer,c=t.endOffset,f=[],u=(h,g)=>{if(K(t,h,!1))return;let b,O;if(!K(t,h,!0)){!(h instanceof HTMLInputElement)&&(!(h instanceof Text)||h.data)&&f.push([g,h]);return}if(h instanceof Text)h===d&&c!==h.length&&f.push([g,h.splitText(c)]),h===l&&a&&(h.splitText(a),f.push([g,h]));else for(b=h.firstChild;b;b=O)O=b.nextSibling,u(b,g)},m=Array.from(r.getElementsByTagName(n)).filter(h=>K(t,h,!0)&&he(h,n,e));if(i||m.forEach(h=>{u(h,h)}),f.forEach(([h,g])=>{h=h.cloneNode(!1),R(g,h),h.appendChild(g)}),m.forEach(h=>{R(h,T(h))}),V&&s){s=s.parentNode;let h=s;for(;h&&N(h);)h=h.parentNode;h&&re(h,s)}return this._getRangeAndRemoveBookmark(t),s&&t.collapse(!1),ne(r,t),t}bold(){return this.changeFormat({tag:"B"})}removeBold(){return this.changeFormat(null,{tag:"B"})}italic(){return this.changeFormat({tag:"I"})}removeItalic(){return this.changeFormat(null,{tag:"I"})}underline(){return this.changeFormat({tag:"U"})}removeUnderline(){return this.changeFormat(null,{tag:"U"})}strikethrough(){return this.changeFormat({tag:"S"})}removeStrikethrough(){return this.changeFormat(null,{tag:"S"})}subscript(){return this.changeFormat({tag:"SUB"},{tag:"SUP"})}removeSubscript(){return this.changeFormat(null,{tag:"SUB"})}superscript(){return this.changeFormat({tag:"SUP"},{tag:"SUB"})}removeSuperscript(){return this.changeFormat(null,{tag:"SUP"})}makeLink(n,e){let t=this.getSelection();if(t.collapsed){let i=n.indexOf(":")+1;if(i)for(;n[i]==="/";)i+=1;j(t,document.createTextNode(n.slice(i)))}return e=Object.assign({href:n},this._config.tagAttributes.a,e),this.changeFormat({tag:"A",attributes:e},{tag:"A"},t)}removeLink(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0)}addDetectedLinks(n,e){let t=new E(n,4,l=>!S(l,e||this._root,"A")),i=this.linkRegExp,s=this._config.tagAttributes.a,r;for(;r=t.nextNode();){let l=r.parentNode,a=r.data,d;for(;d=i.exec(a);){let c=d.index,f=c+d[0].length;c&&l.insertBefore(document.createTextNode(a.slice(0,c)),r);let u=p("A",Object.assign({href:d[1]?/^(?:ht|f)tps?:/i.test(d[1])?d[1]:"http://"+d[1]:"mailto:"+d[0]},s));u.textContent=a.slice(c,f),l.insertBefore(u,r),r.data=a=a.slice(f)}}return this}setFontFace(n){let e=this._config.classNames.fontFamily;return this.changeFormat(n?{tag:"SPAN",attributes:{class:e,style:"font-family: "+n+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:e}})}setFontSize(n){let e=this._config.classNames.fontSize;return this.changeFormat(n?{tag:"SPAN",attributes:{class:e,style:"font-size: "+(typeof n=="number"?n+"px":n)}}:null,{tag:"SPAN",attributes:{class:e}})}setTextColor(n){let e=this._config.classNames.color;return this.changeFormat(n?{tag:"SPAN",attributes:{class:e,style:"color:"+n}}:null,{tag:"SPAN",attributes:{class:e}})}setHighlightColor(n){let e=this._config.classNames.highlight;return this.changeFormat(n?{tag:"SPAN",attributes:{class:e,style:"background-color:"+n}}:null,{tag:"SPAN",attributes:{class:e}})}_ensureBottomLine(){let n=this._root,e=n.lastElementChild;(!e||e.nodeName!==this._config.blockTag||!P(e))&&n.appendChild(this.createDefaultBlock())}createDefaultBlock(n){let e=this._config;return x(p(e.blockTag,e.blockAttributes,n))}splitBlock(n,e){e||(e=this.getSelection());let t=this._root,i,s,r,l;if(this._recordUndoState(e),this._removeZWS(),this._getRangeAndRemoveBookmark(e),e.collapsed||H(e,t),this._config.addLinks){_(e);let u=e.startContainer,m=e.startOffset;setTimeout(()=>{Te(this,u,m)},0)}if(i=L(e,t),i&&(s=S(i,t,"PRE"))){_(e),r=e.startContainer;let u=e.startOffset;return r instanceof Text||(r=document.createTextNode(""),s.insertBefore(r,s.firstChild)),!n&&r instanceof Text&&(r.data.charAt(u-1)===`
|
||
`||X(e,t))&&(r.data.charAt(u)===`
|
||
`||Z(e,t))?(r.deleteData(u&&u-1,u?2:1),l=w(r,u&&u-1,t,t),r=l.previousSibling,r.textContent||C(r),r=this.createDefaultBlock(),l.parentNode.insertBefore(r,l),l.textContent||C(l),e.setStart(r,0)):(r.insertData(u,`
|
||
`),x(s),r.length===u+1?e.setStartAfter(r):e.setStart(r,u+1)),e.collapse(!0),this.setSelection(e),this._updatePath(e,!0),this._docWasChanged(),this}if(!i||n||/^T[HD]$/.test(i.nodeName))return Oe(e,"A",t),j(e,p("BR")),e.collapse(!1),this.setSelection(e),this._updatePath(e,!0),this;if((s=S(i,t,"LI"))&&(i=s),ce(i)){if(S(i,t,"UL")||S(i,t,"OL"))return this.decreaseListLevel(e),this;if(S(i,t,"BLOCKQUOTE"))return this.removeQuote(e),this}r=e.startContainer;let a=e.startOffset,d=this.tagAfterSplit[i.nodeName];l=w(r,a,i.parentNode,this._root);let c=this._config,f=null;for(d||(d=c.blockTag,f=c.blockAttributes),he(l,d,f)||(i=p(d,f),l.dir&&(i.dir=l.dir),R(l,i),i.appendChild(T(l)),l=i),re(i),Se(i),x(i);l instanceof Element;){let u=l.firstChild,m;if(l.nodeName==="A"&&(!l.textContent||l.textContent===k)){u=document.createTextNode(""),R(l,u),l=u;break}for(;u&&u instanceof Text&&!u.data&&(m=u.nextSibling,!(!m||m.nodeName==="BR"));)C(u),u=m;if(!u||u.nodeName==="BR"||u instanceof Text)break;l=u}return e=$(l,0),this.setSelection(e),this._updatePath(e,!0),this}forEachBlock(n,e,t){t||(t=this.getSelection()),e&&this.saveUndoState(t);let i=this._root,s=L(t,i),r=G(t,i);if(s&&r)do if(n(s)||s===r)break;while(s=W(s,i));return e&&(this.setSelection(t),this._updatePath(t,!0)),this}modifyBlocks(n,e){e||(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState);let t=this._root;ye(e,t),q(e,t,t,t);let i=Be(e,t,t);if(!e.collapsed){let s=e.endContainer;if(s===t)e.collapse(!1);else{for(;s.parentNode!==t;)s=s.parentNode;e.setStartBefore(s),e.collapse(!0)}}return j(e,n.call(this,i)),e.endOffset<e.endContainer.childNodes.length&&F(e.endContainer.childNodes[e.endOffset],t),F(e.startContainer.childNodes[e.startOffset],t),this._getRangeAndRemoveBookmark(e),this.setSelection(e),this._updatePath(e,!0),this}setTextAlignment(n){return this.forEachBlock(e=>{let t=e.className.split(/\s+/).filter(i=>!!i&&!/^align/.test(i)).join(" ");n?(e.className=t+" align-"+n,e.style.textAlign=n):(e.className=t,e.style.textAlign="")},!0),this.focus()}setTextDirection(n){return this.forEachBlock(e=>{n?e.dir=n:e.removeAttribute("dir")},!0),this.focus()}_getListSelection(n,e){let t=n.commonAncestorContainer,i=n.startContainer,s=n.endContainer;for(;t&&t!==e&&!/^[OU]L$/.test(t.nodeName);)t=t.parentNode;if(!t||t===e)return null;for(i===t&&(i=i.childNodes[n.startOffset]),s===t&&(s=s.childNodes[n.endOffset]);i&&i.parentNode!==t;)i=i.parentNode;for(;s&&s.parentNode!==t;)s=s.parentNode;return[t,i,s]}increaseListLevel(n){n||(n=this.getSelection());let e=this._root,t=this._getListSelection(n,e);if(!t)return this.focus();let[i,s,r]=t;if(!s||s===i.firstChild)return this.focus();this._recordUndoState(n,this._isInUndoState);let l=i.nodeName,a=s.previousSibling,d,c;a.nodeName!==l&&(d=this._config.tagAttributes[l.toLowerCase()],a=p(l,d),i.insertBefore(a,s));do c=s===r?null:s.nextSibling,a.appendChild(s);while(s=c);return c=a.nextSibling,c&&F(c,e),this._getRangeAndRemoveBookmark(n),this.setSelection(n),this._updatePath(n,!0),this.focus()}decreaseListLevel(n){n||(n=this.getSelection());let e=this._root,t=this._getListSelection(n,e);if(!t)return this.focus();let[i,s,r]=t;s||(s=i.firstChild),r||(r=i.lastChild),this._recordUndoState(n,this._isInUndoState);let l,a=null;if(s){let d=i.parentNode;if(a=r.nextSibling?w(i,r.nextSibling,d,e):i.nextSibling,d!==e&&d.nodeName==="LI"){for(d=d.parentNode;a;)l=a.nextSibling,r.appendChild(a),a=l;a=i.parentNode.nextSibling}let c=!/^[OU]L$/.test(d.nodeName);do l=s===r?null:s.nextSibling,i.removeChild(s),c&&s.nodeName==="LI"&&(s=this.createDefaultBlock([T(s)])),d.insertBefore(s,a);while(s=l)}return i.firstChild||C(i),a&&F(a,e),this._getRangeAndRemoveBookmark(n),this.setSelection(n),this._updatePath(n,!0),this.focus()}_makeList(n,e){let t=ae(n,this._root),i=this._config.tagAttributes,s=i[e.toLowerCase()],r=i.li,l;for(;l=t.nextNode();)if(l.parentNode instanceof HTMLLIElement&&(l=l.parentNode,t.currentNode=l.lastChild),l instanceof HTMLLIElement){l=l.parentNode;let a=l.nodeName;a!==e&&/^[OU]L$/.test(a)&&R(l,p(e,s,[T(l)]))}else{let a=p("LI",r);l.dir&&(a.dir=l.dir);let d=l.previousSibling;d&&d.nodeName===e?(d.appendChild(a),C(l)):R(l,p(e,s,[a])),a.appendChild(T(l)),t.currentNode=a}return n}makeUnorderedList(){return this.modifyBlocks(n=>this._makeList(n,"UL")),this.focus()}makeOrderedList(){return this.modifyBlocks(n=>this._makeList(n,"OL")),this.focus()}removeList(){return this.modifyBlocks(n=>{let e=n.querySelectorAll("UL, OL"),t=n.querySelectorAll("LI"),i=this._root;for(let s=0,r=e.length;s<r;s+=1){let l=e[s],a=T(l);D(a,i),R(l,a)}for(let s=0,r=t.length;s<r;s+=1){let l=t[s];P(l)?R(l,this.createDefaultBlock([T(l)])):(D(l,i),R(l,T(l)))}return n}),this.focus()}increaseQuoteLevel(n){return this.modifyBlocks(e=>p("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e]),n),this.focus()}decreaseQuoteLevel(n){return this.modifyBlocks(e=>(Array.from(e.querySelectorAll("blockquote")).filter(t=>!S(t.parentNode,e,"BLOCKQUOTE")).forEach(t=>{R(t,T(t))}),e),n),this.focus()}removeQuote(n){return this.modifyBlocks(()=>this.createDefaultBlock([p("INPUT",{id:this.startSelectionId,type:"hidden"}),p("INPUT",{id:this.endSelectionId,type:"hidden"})]),n),this.focus()}code(){let n=this.getSelection();return n.collapsed||Q(n.commonAncestorContainer)?(this.modifyBlocks(e=>{let t=this._root,i=document.createDocumentFragment(),s=ae(e,t),r;for(;r=s.nextNode();){let a=r.querySelectorAll("BR"),d=[],c=a.length;for(let f=0;f<c;f+=1)d[f]=te(a[f],!1);for(;c--;){let f=a[c];d[c]?R(f,document.createTextNode(`
|
||
`)):C(f)}for(a=r.querySelectorAll("CODE"),c=a.length;c--;)R(a[c],T(a[c]));i.childNodes.length&&i.appendChild(document.createTextNode(`
|
||
`)),i.appendChild(T(r))}let l=new E(i,4);for(;r=l.nextNode();)r.data=r.data.replace(/ /g," ");return i.normalize(),x(p("PRE",this._config.tagAttributes.pre,[i]))},n),this.focus()):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,n),this}removeCode(){let n=this.getSelection(),e=n.commonAncestorContainer;return S(e,this._root,"PRE")?(this.modifyBlocks(i=>{let s=this._root,r=i.querySelectorAll("PRE"),l=r.length;for(;l--;){let a=r[l],d=new E(a,4),c;for(;c=d.nextNode();){let f=c.data;f=f.replace(/ (?= )/g,"\xA0");let u=document.createDocumentFragment(),m;for(;(m=f.indexOf(`
|
||
`))>-1;)u.appendChild(document.createTextNode(f.slice(0,m))),u.appendChild(p("BR")),f=f.slice(m+1);c.parentNode.insertBefore(u,c),c.data=f}D(a,s),R(a,T(a))}return i},n),this.focus()):this.changeFormat(null,{tag:"CODE"},n),this}toggleCode(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this}_removeFormatting(n,e){for(let t=n.firstChild,i;t;t=i){if(i=t.nextSibling,N(t)){if(t instanceof Text||t.nodeName==="BR"||t.nodeName==="IMG"){e.appendChild(t);continue}}else if(P(t)){e.appendChild(this.createDefaultBlock([this._removeFormatting(t,document.createDocumentFragment())]));continue}this._removeFormatting(t,e)}return e}removeAllFormatting(n){if(n||(n=this.getSelection()),n.collapsed)return this.focus();let e=this._root,t=n.commonAncestorContainer;for(;t&&!P(t);)t=t.parentNode;if(t||(ye(n,e),t=e),t instanceof Text)return this.focus();this.saveUndoState(n),q(n,t,t,e);let i=n.startContainer,s=n.startOffset,r=n.endContainer,l=n.endOffset,a=document.createDocumentFragment(),d=document.createDocumentFragment(),c=w(r,l,t,e),f=w(i,s,t,e),u;for(;f!==c;)u=f.nextSibling,a.appendChild(f),f=u;if(this._removeFormatting(a,d),d.normalize(),f=d.firstChild,u=d.lastChild,f){t.insertBefore(d,c);let m=Array.from(t.childNodes);s=m.indexOf(f),l=u?m.indexOf(u)+1:0}else c&&(s=Array.from(t.childNodes).indexOf(c),l=s);return n.setStart(t,s),n.setEnd(t,l),ne(t,n),_(n),this.setSelection(n),this._updatePath(n,!0),this.focus()}};var Fo=Ce;export{Fo as default};
|
||
//# sourceMappingURL=squire.mjs.map
|