mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 07:13:08 -05:00
12 lines
51 KiB
JavaScript
12 lines
51 KiB
JavaScript
"use strict";(()=>{var st=()=>!0,E=class{constructor(t,e,n){this.root=t,this.currentNode=t,this.nodeType=e,this.filter=n||st}isAcceptableNode(t){let e=t.nodeType;return!!((e===Node.ELEMENT_NODE?1:e===Node.TEXT_NODE?4:0)&this.nodeType)&&this.filter(t)}nextNode(){let t=this.root,e=this.currentNode,n;for(;;){for(n=e.firstChild;!n&&e&&e!==t;)n=e.nextSibling,n||(e=e.parentNode);if(!n)return null;if(this.isAcceptableNode(n))return this.currentNode=n,n;e=n}}previousNode(){let t=this.root,e=this.currentNode,n;for(;;){if(e===t)return null;if(n=e.previousSibling,n)for(;e=n.lastChild;)n=e;else n=e.parentNode;if(!n)return null;if(this.isAcceptableNode(n))return this.currentNode=n,n;e=n}}previousPONode(){let t=this.root,e=this.currentNode,n;for(;;){for(n=e.lastChild;!n&&e&&e!==t;)n=e.previousSibling,n||(e=e.parentNode);if(!n)return null;if(this.isAcceptableNode(n))return this.currentNode=n,n;e=n}}};var k="\u200B",J=navigator.userAgent,ce=/Mac OS X/.test(J),de=/Windows NT/.test(J),_e=/iP(?:ad|hone|od)/.test(J)||ce&&!!navigator.maxTouchPoints,Fe=/Android/.test(J),He=/Gecko\//.test(J),se=/Edge\//.test(J),rt=!se&&/WebKit\//.test(J),B=ce||_e?"Meta-":"Ctrl-",ee=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,fe=new WeakMap,We=()=>{fe=new WeakMap},M=o=>dt.has(o.nodeName),be=o=>{switch(o.nodeType){case 3:return xe;case 1:case 11:if(fe.has(o))return fe.get(o);break;default:return ft}let t;return Array.from(o.childNodes).every(N)?ct.test(o.nodeName)?t=xe:t=Ue:t=qe,fe.set(o,t),t},N=o=>be(o)===xe,P=o=>be(o)===Ue,Q=o=>be(o)===qe;var p=(o,t,e)=>{let n=document.createElement(o);if(t instanceof Array&&(e=t,t=null),t)for(let i in t){let s=t[i];s!==void 0&&n.setAttribute(i,s)}return e&&e.forEach(i=>n.appendChild(i)),n},Le=(o,t)=>M(o)||o.nodeType!==t.nodeType||o.nodeName!==t.nodeName?!1:o instanceof HTMLElement&&t instanceof HTMLElement?o.nodeName!=="A"&&o.className===t.className&&o.style.cssText===t.style.cssText:!0,ue=(o,t,e)=>{if(o.nodeName!==t)return!1;for(let n in e)if(!("getAttribute"in o)||o.getAttribute(n)!==e[n])return!1;return!0},S=(o,t,e,n)=>{for(;o&&o!==t;){if(ue(o,e,n))return o;o=o.parentNode}return null},he=(o,t)=>{let e=o.childNodes;for(;t&&o instanceof Element;)o=e[t-1],e=o.childNodes,t=e.length;return o},Re=(o,t)=>{let e=o;if(e instanceof Element){let n=e.childNodes;if(t<n.length)e=n[t];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 t=document.createDocumentFragment(),e=o.firstChild;for(;e;)t.appendChild(e),e=o.firstChild;return t},C=o=>{let t=o.parentNode;return t&&t.removeChild(o),o},R=(o,t)=>{let e=o.parentNode;e&&e.replaceChild(t,o)};var ut=o=>o instanceof Element?o.nodeName==="BR":I.test(o.data),te=(o,t)=>{let e=o.parentNode;for(;N(e);)e=e.parentNode;let n=new E(e,5,ut);return n.currentNode=o,!!n.nextNode()||t&&!n.previousNode()},me=(o,t)=>{let e=new E(o,4),n,i;for(;n=e.nextNode();)for(;(i=n.data.indexOf(k))>-1&&(!t||n.parentNode!==t);)if(n.length===1){let s=n,r=s.parentNode;for(;r&&(r.removeChild(s),e.currentNode=r,!(!N(r)||y(r)));)s=r,r=s.parentNode;break}else n.deleteData(i,1)};var ht=0,mt=1,pt=2,Nt=3,K=(o,t,e)=>{let n=document.createRange();if(n.selectNode(t),e){let i=o.compareBoundaryPoints(Nt,n)>-1,s=o.compareBoundaryPoints(mt,n)<1;return!i&&!s}else{let i=o.compareBoundaryPoints(ht,n)<1,s=o.compareBoundaryPoints(pt,n)>-1;return i&&s}},_=o=>{let{startContainer:t,startOffset:e,endContainer:n,endOffset:i}=o;for(;!(t instanceof Text);){let s=t.childNodes[e];if(!s||M(s)){if(e&&(s=t.childNodes[e-1],s instanceof Text)){let r=s,l;for(;!r.length&&(l=r.previousSibling)&&l instanceof Text;)r.remove(),r=l;t=r,e=r.data.length}break}t=s,e=0}if(i)for(;!(n instanceof Text);){let s=n.childNodes[i-1];if(!s||M(s)){if(s&&s.nodeName==="BR"&&!te(s,!1)){i-=1;continue}break}n=s,i=y(n)}else for(;!(n instanceof Text);){let s=n.firstChild;if(!s||M(s))break;n=s}o.setStart(t,e),o.setEnd(n,i)},q=(o,t,e,n)=>{let i=o.startContainer,s=o.startOffset,r=o.endContainer,l=o.endOffset,a;for(t||(t=o.commonAncestorContainer),e||(e=t);!s&&i!==t&&i!==n;)a=i.parentNode,s=Array.from(a.childNodes).indexOf(i),i=a;for(;!(r===e||r===n||(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,t,e)=>{let n=S(o.endContainer,e,t);if(n&&(n=n.parentNode)){let i=o.cloneRange();q(i,n,n,e),i.endContainer===n&&(o.setStart(i.endContainer,i.endOffset),o.setEnd(i.endContainer,i.endOffset))}return o};var x=o=>{let t=null;if(o instanceof Text)return o;if(N(o)){let e=o.firstChild;if(ee)for(;e&&e instanceof Text&&!e.data;)o.removeChild(e),e=o.firstChild;e||(ee?t=document.createTextNode(k):t=document.createTextNode(""))}else if(o instanceof Element&&!o.querySelector("BR")){t=p("BR");let e=o,n;for(;(n=e.lastElementChild)&&!N(n);)e=n}if(t)try{o.appendChild(t)}catch(e){}return o},A=(o,t)=>{let e=null;return Array.from(o.childNodes).forEach(n=>{let i=n.nodeName==="BR";!i&&N(n)?(e||(e=p("DIV")),e.appendChild(n)):(i||e)&&(e||(e=p("DIV")),x(e),i?o.replaceChild(e,n):o.insertBefore(e,n),e=null),Q(n)&&A(n,t)}),e&&o.appendChild(x(e)),o},w=(o,t,e,n)=>{if(o instanceof Text&&o!==e){if(typeof t!="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(t),e,n)}let i=typeof t=="number"?t<o.childNodes.length?o.childNodes[t]:null:t,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,n,"BLOCKQUOTE")&&(r.start=(+o.start||1)+o.childNodes.length-1),x(o),x(r),s.insertBefore(r,o.nextSibling),w(s,r,e,n)},Ke=(o,t)=>{let e=o.childNodes,n=e.length,i=[];for(;n--;){let s=e[n],r=n?e[n-1]:null;if(r&&N(s)&&Le(s,r))t.startContainer===s&&(t.startContainer=r,t.startOffset+=y(r)),t.endContainer===s&&(t.endContainer=r,t.endOffset+=y(r)),t.startContainer===o&&(t.startOffset>n?t.startOffset-=1:t.startOffset===n&&(t.startContainer=r,t.startOffset=y(r))),t.endContainer===o&&(t.endOffset>n?t.endOffset-=1:t.endOffset===n&&(t.endContainer=r,t.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,t)}}},ne=(o,t)=>{let e=o instanceof Text?o.parentNode:o;if(e instanceof Element){let n={startContainer:t.startContainer,startOffset:t.startOffset,endContainer:t.endContainer,endOffset:t.endOffset};Ke(e,n),t.setStart(n.startContainer,n.startOffset),t.setEnd(n.endContainer,n.endOffset)}},Y=(o,t,e,n)=>{let i=t,s,r;for(;(s=i.parentNode)&&s!==n&&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(t)),e.setStart(o,r),e.collapse(!0),ne(o,e)},F=(o,t)=>{let e=o.previousSibling,n=o.firstChild,i=o.nodeName==="LI";if(!(i&&(!n||!/^[OU]L$/.test(n.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&&A(e,t),n&&F(n,t)}else if(i){let s=p("DIV");o.insertBefore(s,n),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,t){return p("SPAN",{class:o.fontFamily,style:"font-family:"+t})}},"font-size":{regexp:I,replace(o,t){return p("SPAN",{class:o.fontSize,style:"font-size:"+t})}},"text-decoration":{regexp:/^underline/i,replace(){return p("U")}}},St=(o,t,e)=>{let n=o.style,i,s;for(let r in ze){let l=ze[r],a=n.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=>(t,e)=>{let n=p(o),i=t.attributes;for(let s=0,r=i.length;s<r;s+=1){let l=i[s];n.setAttribute(l.name,l.value)}return e.replaceChild(n,t),n.appendChild(T(t)),n},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,t,e)=>{let n=o,i=n.face,s=n.size,r=n.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")),t.replaceChild(u,n),f.appendChild(T(n)),f},TT:(o,t,e)=>{let n=p("SPAN",{class:e.classNames.fontFamily,style:'font-family:menlo,consolas,"courier new",monospace'});return t.replaceChild(n,o),n.appendChild(T(o)),n}},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,t,e)=>{let n=o.childNodes,i=o;for(;N(i);)i=i.parentNode;let s=new E(i,5);for(let r=0,l=n.length;r<l;r+=1){let a=n[r],d=a.nodeName,c=Et[d];if(a instanceof HTMLElement){let f=a.childNodes.length;if(c)a=c(a,o,t);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,t,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 t=o.childNodes,e=t.length;for(;e--;){let n=t[e];n instanceof Element&&!M(n)?(Se(n),N(n)&&!n.firstChild&&o.removeChild(n)):n instanceof Text&&!n.data&&o.removeChild(n)}},re=(o,t,e)=>{let n=o.querySelectorAll("BR"),i=[],s=n.length;for(let r=0;r<s;r+=1)i[r]=te(n[r],e);for(;s--;){let r=n[s],l=r.parentNode;l&&(i[s]?N(l)||A(l,t):C(r))}},ke=o=>o.split("&").join("&").split("<").join("<").split(">").join(">").split('"').join(""");var le=(o,t)=>{let e=new E(t,1,P);return e.currentNode=o,e},z=(o,t)=>{let e=le(o,t).previousNode();return e!==t?e:null},W=(o,t)=>{let e=le(o,t).nextNode();return e!==t?e:null},ae=o=>!o.textContent&&!o.querySelector("IMG");var L=(o,t)=>{let e=o.startContainer,n;if(N(e))n=z(e,t);else if(e!==t&&e instanceof HTMLElement&&P(e))n=e;else{let i=he(e,o.startOffset);n=W(i,t)}return n&&K(o,n,!0)?n:null},G=(o,t)=>{let e=o.endContainer,n;if(N(e))n=z(e,t);else if(e!==t&&e instanceof HTMLElement&&P(e))n=e;else{let i=Re(e,o.endOffset);if(!i||!t.contains(i)){i=t;let s;for(;s=i.lastChild;)i=s}n=z(i,t)}return n&&K(o,n,!0)?n:null},Ge=o=>o instanceof Text?I.test(o.data):o.nodeName==="IMG",X=(o,t)=>{let e=o.startContainer,n=o.startOffset,i;if(e instanceof Text){let l=e.data;for(let a=n;a>0;a-=1)if(l.charAt(a-1)!==k)return!1;i=e}else if(i=Re(e,n),i&&!t.contains(i)&&(i=null),!i&&(i=he(e,n),i instanceof Text&&i.length))return!1;let s=L(o,t);if(!s)return!1;let r=new E(s,5,Ge);return r.currentNode=i,!r.previousNode()},Z=(o,t)=>{let e=o.endContainer,n=o.endOffset,i;if(e instanceof Text){let l=e.data,a=l.length;for(let d=n;d<a;d+=1)if(l.charAt(d)!==k)return!1;i=e}else i=he(e,n);let s=G(o,t);if(!s)return!1;let r=new E(s,5,Ge);return r.currentNode=i,!r.nextNode()},ye=(o,t)=>{let e=L(o,t),n=G(o,t),i;e&&n&&(i=e.parentNode,o.setStart(i,Array.from(i.childNodes).indexOf(e)),i=n.parentNode,o.setEnd(i,Array.from(i.childNodes).indexOf(n)+1))};function $(o,t,e,n){let i=document.createRange();return i.setStart(o,t),e&&typeof n=="number"?i.setEnd(e,n):i.setEnd(o,t),i}var j=(o,t)=>{let{startContainer:e,startOffset:n,endContainer:i,endOffset:s}=o,r;if(e instanceof Text){let a=e.parentNode;if(r=a.childNodes,n===e.length)n=Array.from(r).indexOf(e)+1,o.collapsed&&(i=a,s=n);else{if(n){let d=e.splitText(n);i===e?(s-=n,i=d):i===a&&(s+=1),e=d}n=Array.from(r).indexOf(e)}e=a}else r=e.childNodes;let l=r.length;n===l?e.appendChild(t):e.insertBefore(t,r[n]),e===i&&(s+=r.length-l),o.setStart(e,n),o.setEnd(i,s)},Be=(o,t,e)=>{let n=document.createDocumentFragment();if(o.collapsed)return n;t||(t=o.commonAncestorContainer),t instanceof Text&&(t=t.parentNode);let i=o.startContainer,s=o.startOffset,r=w(o.endContainer,o.endOffset,t,e),l=0,a=w(i,s,t,e);for(;a&&a!==r;){let d=a.nextSibling;n.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(t,t.childNodes.length),x(t),n},Ze=(o,t,e)=>{o.currentNode=e;let n;for(;n=o[t]();){if(n instanceof Text||M(n))return n;if(!N(n))return null}return null},H=(o,t)=>{let e=L(o,t),n=G(o,t),i=e!==n;e&&n&&(_(o),q(o,e,n,t));let s=Be(o,null,t);_(o),i&&(n=G(o,t),e&&n&&e!==n&&Y(e,n,o,t)),e&&x(e);let r=t.firstChild;(!r||r.nodeName==="BR")&&(x(t),t.firstChild&&o.selectNodeContents(t.firstChild)),o.collapse(!0);let l=o.startContainer,a=o.startOffset,d=new E(t,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,t)?(h=c,g=f):u instanceof Text&&u.data.charAt(m)===" "&&(c instanceof Text&&c.data.charAt(f)===" "||Z(o,t))&&(h=u,g=m),h&&h.replaceData(g,1,"\xA0"),o.setStart(l,a),o.collapse(!0),s},je=(o,t,e)=>{let n=t.firstChild&&N(t.firstChild),i;for(A(t,e),i=t;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(t,t),d=!n&&!!r&&ae(r);if(r&&a&&!d&&!S(a,t,"PRE")&&!S(a,t,"TABLE")){q(o,r,r,e),o.collapse(!0);let c=o.endContainer,f=o.endOffset;if(re(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);Y(c,a,o,e),f=Array.from(c.parentNode.childNodes).indexOf(c)+1,c=c.parentNode,o.setEnd(c,f)}if(y(t)){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(t,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();Y(r,l,c,e),o.setEnd(c.endContainer,c.endOffset)}_(o)};var ge=o=>{if(o.collapsed)return"";let t=o.startContainer,e=o.endContainer,n=new E(o.commonAncestorContainer,5,a=>K(o,a,!0));n.currentNode=t;let i=t,s="",r=!1,l;for((!(i instanceof Element)&&!(i instanceof Text)||!n.filter(i))&&(i=n.nextNode());i;)i instanceof Text?(l=i.data,l&&/\S/.test(l)&&(i===e&&(l=l.slice(0,o.endOffset)),i===t&&(l=l.slice(o.startOffset)),s+=l,r=!0)):(i.nodeName==="BR"||r&&!N(i))&&(s+=`
|
||
`,r=!1),i=n.nextNode();return s=s.replace(/ /g," "),s};var Ae=Array.prototype.indexOf,Qe=(o,t,e,n,i,s,r)=>{let l=o.clipboardData;if(se||!l)return!1;let a=s?"":ge(t),d=L(t,e),c=G(t,e),f=e;d===c&&(d!=null&&d.contains(t.commonAncestorContainer))&&(f=d);let u;n?u=H(t,e):(t=t.cloneRange(),_(t),q(t,f,f,e),u=t.cloneContents());let m=t.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 r||s&&h!==void 0&&(a=s(h)),de&&(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 t=this.getSelection(),e=this._root;if(t.collapsed){o.preventDefault();return}this.saveUndoState(t),Qe(o,t,e,!0,this._config.willCutCopy,this._config.toPlainText,!1)||setTimeout(()=>{try{this._ensureBottomLine()}catch(i){this._config.didError(i)}},0),this.setSelection(t)},$e=function(o){Qe(o,this.getSelection(),this._root,!1,this._config.willCutCopy,this._config.toPlainText,!1)},De=function(o){this._isShiftDown=o.shiftKey},Ve=function(o){let t=o.clipboardData,e=t==null?void 0:t.items,n=this._isShiftDown,i=!1,s=!1,r=null,l=null;if(e){let v=e.length;for(;v--;){let O=e[v],D=O.type;D==="text/html"?l=O:D==="text/plain"||D==="text/uri-list"?r=O:D==="text/rtf"?i=!0:/^image\/.*/.test(D)&&(s=!0)}if(s&&!(i&&l)){o.preventDefault(),this.fireEvent("pasteImage",{clipboardData:t});return}if(!se){o.preventDefault(),l&&(!n||!r)?l.getAsString(O=>{this.insertHTML(O,!0)}):r&&r.getAsString(O=>{let D=!1,Ie=this.getSelection();if(!Ie.collapsed&&I.test(Ie.toString())){let Me=this.linkRegExp.exec(O);D=!!Me&&Me[0].length===O.length}D?this.makeLink(O):this.insertPlainText(O,!0)});return}}let a=t==null?void 0:t.types;if(!se&&a&&(Ae.call(a,"text/html")>-1||!He&&Ae.call(a,"text/plain")>-1&&Ae.call(a,"text/rtf")<0)){o.preventDefault();let v;!n&&(v=t.getData("text/html"))?this.insertHTML(v,!0):((v=t.getData("text/plain"))||(v=t.getData("text/uri-list")))&&this.insertPlainText(v,!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 v="",O=g,D;for(;g=O;)O=g.nextSibling,C(g),D=g.firstChild,D&&D===g.lastChild&&D instanceof HTMLDivElement&&(g=D),v+=g.innerHTML;this.setSelection($(f,u,m,h)),v&&this.insertHTML(v,!0)}catch(v){this._config.didError(v)}},0)},Ye=function(o){if(!o.dataTransfer)return;let t=o.dataTransfer.types,e=t.length,n=!1,i=!1;for(;e--;)switch(t[e]){case"text/plain":n=!0;break;case"text/html":i=!0;break;default:return}(i||n&&this.saveUndoState)&&this.saveUndoState()};var we=(o,t,e)=>{t.preventDefault(),o.splitBlock(t.shiftKey,e)};var oe=(o,t)=>{try{t||(t=o.getSelection());let e=t.startContainer;e instanceof Text&&(e=e.parentNode);let n=e;for(;N(n)&&(!n.textContent||n.textContent===k);)e=n,n=e.parentNode;e!==n&&(t.setStart(n,Array.from(n.childNodes).indexOf(e)),t.collapse(!0),n.removeChild(e),P(n)||(n=z(n,o._root)||o._root),x(n),_(t)),e===o._root&&(e=e.firstChild)&&e.nodeName==="BR"&&C(e),o._ensureBottomLine(),o.setSelection(t),o._updatePath(t,!0)}catch(e){o._config.didError(e)}},Ee=(o,t)=>{let e;for(;(e=o.parentNode)&&!(e===t||e.isContentEditable);)o=e;C(o)},Te=(o,t,e)=>{if(S(t,o._root,"A"))return;let n=t.data||"",i=Math.max(n.lastIndexOf(" ",e-1),n.lastIndexOf("\xA0",e-1))+1,s=n.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===t,f=l.startOffset-d;a&&(t=t.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=n.slice(a,d),t.parentNode.insertBefore(m,t),t.data=n.slice(d),c&&(l.setStart(t,f),l.setEnd(t,f)),o.setSelection(l)}};var Je=(o,t,e)=>{let n=o._root;if(o._removeZWS(),o.saveUndoState(e),!e.collapsed)t.preventDefault(),H(e,n),oe(o,e);else if(X(e,n)){t.preventDefault();let i=L(e,n);if(!i)return;let s=i;A(s.parentNode,n);let r=z(s,n);if(r){if(!r.isContentEditable){Ee(r,n);return}for(Y(r,s,e,n),s=r.parentNode;s!==n&&!s.nextSibling;)s=s.parentNode;s!==n&&(s=s.nextSibling)&&F(s,n),o.setSelection(e)}else if(s){if(S(s,n,"UL")||S(s,n,"OL")){o.decreaseListLevel(e);return}else if(S(s,n,"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(),t.preventDefault()):(o.setSelection(e),setTimeout(()=>{oe(o)},0))}};var et=(o,t,e)=>{let n=o._root,i,s,r,l,a,d;if(o._removeZWS(),o.saveUndoState(e),!e.collapsed)t.preventDefault(),H(e,n),oe(o,e);else if(Z(e,n)){if(t.preventDefault(),i=L(e,n),!i)return;if(A(i.parentNode,n),s=W(i,n),s){if(!s.isContentEditable){Ee(s,n);return}for(Y(i,s,e,n),s=i.parentNode;s!==n&&!s.nextSibling;)s=s.parentNode;s!==n&&(s=s.nextSibling)&&F(s,n),o.setSelection(e),o._updatePath(e,!0)}}else{if(r=e.cloneRange(),q(e,n,n,n),l=e.endContainer,a=e.endOffset,l instanceof Element&&(d=l.childNodes[a],d&&d.nodeName==="IMG")){t.preventDefault(),C(d),_(e),oe(o,e);return}o.setSelection(r),setTimeout(()=>{oe(o)},0)}};var tt=(o,t,e)=>{let n=o._root;if(o._removeZWS(),e.collapsed&&X(e,n)){let i=L(e,n),s;for(;s=i.parentNode;){if(s.nodeName==="UL"||s.nodeName==="OL"){t.preventDefault(),o.increaseListLevel(e);break}i=s}}},nt=(o,t,e)=>{let n=o._root;if(o._removeZWS(),e.collapsed&&X(e,n)){let i=e.startContainer;(S(i,n,"UL")||S(i,n,"OL"))&&(t.preventDefault(),o.decreaseListLevel(e))}};var ot=(o,t,e)=>{var s;let n,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 r=L(e,i);if(r&&r.nodeName!=="PRE"){let l=(s=r.textContent)==null?void 0:s.trimEnd().replace(k,"");if(l==="*"||l==="1."){t.preventDefault();let a=new E(r,4),d;for(;d=a.nextNode();)d.data=ee?k:"";l==="*"?o.makeUnorderedList():o.makeOrderedList();return}}}if(n=e.endContainer,e.endOffset===y(n))do if(n.nodeName==="A"){e.setStartAfter(n);break}while(!n.nextSibling&&(n=n.parentNode)&&n!==i);if(o._config.addLinks){let r=e.cloneRange();_(r);let l=r.startContainer,a=r.startOffset;setTimeout(()=>{Te(o,l,a)},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 t=o.keyCode,e=xt[t],n="",i=this.getSelection();o.defaultPrevented||(e||(e=String.fromCharCode(t).toLowerCase(),/^[A-Za-z0-9]$/.test(e)||(e="")),111<t&&t<124&&(e="F"+(t-111)),e!=="Backspace"&&e!=="Delete"&&(o.altKey&&(n+="Alt-"),o.ctrlKey&&(n+="Ctrl-"),o.metaKey&&(n+="Meta-"),o.shiftKey&&(n+="Shift-")),de&&o.shiftKey&&e==="Delete"&&(n+="Shift-"),e=n+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)))},b={Backspace:Je,Delete:et,Tab:tt,"Shift-Tab":nt,Space:ot,ArrowLeft(o){o._removeZWS()},ArrowRight(o,t,e){o._removeZWS();let n=o.getRoot();if(Z(e,n)){_(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),t.preventDefault();break}while(!i.nextSibling&&(i=i.parentNode)&&i!==n)}}};Pe||(b.Enter=we,b["Shift-Enter"]=we);!ce&&!_e&&(b.PageUp=o=>{o.moveCursorToStart()},b.PageDown=o=>{o.moveCursorToEnd()});var ie=(o,t)=>(t=t||null,(e,n)=>{n.preventDefault();let i=e.getSelection();e.hasFormat(o,null,i)?e.changeFormat(null,{tag:o},i):e.changeFormat({tag:o},t,i)});b[B+"b"]=ie("B");b[B+"i"]=ie("I");b[B+"u"]=ie("U");b[B+"Shift-7"]=ie("S");b[B+"Shift-5"]=ie("SUB",{tag:"SUP"});b[B+"Shift-6"]=ie("SUP",{tag:"SUB"});b[B+"Shift-8"]=(o,t)=>{t.preventDefault();let e=o.getPath();/(?:^|>)UL/.test(e)?o.removeList():o.makeUnorderedList()};b[B+"Shift-9"]=(o,t)=>{t.preventDefault();let e=o.getPath();/(?:^|>)OL/.test(e)?o.removeList():o.makeOrderedList()};b[B+"["]=(o,t)=>{t.preventDefault();let e=o.getPath();/(?:^|>)BLOCKQUOTE/.test(e)||!/(?:^|>)[OU]L/.test(e)?o.decreaseQuoteLevel():o.decreaseListLevel()};b[B+"]"]=(o,t)=>{t.preventDefault();let e=o.getPath();/(?:^|>)BLOCKQUOTE/.test(e)||!/(?:^|>)[OU]L/.test(e)?o.increaseQuoteLevel():o.increaseListLevel()};b[B+"d"]=(o,t)=>{t.preventDefault(),o.toggleCode()};b[B+"z"]=(o,t)=>{t.preventDefault(),o.undo()};b[B+"y"]=b[B+"Shift-z"]=(o,t)=>{t.preventDefault(),o.redo()};var Ce=class{constructor(t,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=t,this._config=this._makeConfig(e),this._isFocused=!1,this._lastSelection=$(t,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",De),this.addEventListener("keyup",De),this.addEventListener("keydown",it),this._keyHandlers=Object.create(b);let n=new MutationObserver(()=>this._docWasChanged());n.observe(t,{childList:!0,attributes:!0,characterData:!0,subtree:!0}),this._mutation=n,t.setAttribute("contenteditable","true"),this.addEventListener("beforeinput",this._beforeInput),this.setHTML("")}destroy(){this._events.forEach((t,e)=>{this.removeEventListener(e)}),this._mutation.disconnect(),this._undoIndex=-1,this._undoStack=[],this._undoStackLength=0}_makeConfig(t){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:n=>{let i=DOMPurify.sanitize(n,{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:n=>console.log(n)};return t&&(Object.assign(e,t),e.blockTag=e.blockTag.toUpperCase()),e}setKeyHandler(t,e){return this._keyHandlers[t]=e,this}_beforeInput(t){switch(t.inputType){case"insertText":Fe&&t.data&&t.data.includes(`
|
||
`)&&t.preventDefault();break;case"insertLineBreak":t.preventDefault(),this.splitBlock(!0);break;case"insertParagraph":t.preventDefault(),this.splitBlock(!1);break;case"insertOrderedList":t.preventDefault(),this.makeOrderedList();break;case"insertUnoderedList":t.preventDefault(),this.makeUnorderedList();break;case"historyUndo":t.preventDefault(),this.undo();break;case"historyRedo":t.preventDefault(),this.redo();break;case"formatBold":t.preventDefault(),this.bold();break;case"formaItalic":t.preventDefault(),this.italic();break;case"formatUnderline":t.preventDefault(),this.underline();break;case"formatStrikeThrough":t.preventDefault(),this.strikethrough();break;case"formatSuperscript":t.preventDefault(),this.superscript();break;case"formatSubscript":t.preventDefault(),this.subscript();break;case"formatJustifyFull":case"formatJustifyCenter":case"formatJustifyRight":case"formatJustifyLeft":{t.preventDefault();let e=t.inputType.slice(13).toLowerCase();e==="full"&&(e="justify"),this.setTextAlignment(e);break}case"formatRemove":t.preventDefault(),this.removeAllFormatting();break;case"formatSetBlockTextDirection":{t.preventDefault();let e=t.data;e==="null"&&(e=null),this.setTextDirection(e);break}case"formatBackColor":t.preventDefault(),this.setHighlightColor(t.data);break;case"formatFontColor":t.preventDefault(),this.setTextColor(t.data);break;case"formatFontName":t.preventDefault(),this.setFontFace(t.data);break}}handleEvent(t){this.fireEvent(t.type,t)}fireEvent(t,e){let n=this._events.get(t);if(/^(?:focus|blur)/.test(t)){let i=this._root===document.activeElement;if(t==="focus"){if(!i||this._isFocused)return this;this._isFocused=!0}else{if(i||!this._isFocused)return this;this._isFocused=!1}}if(n){let i=e instanceof Event?e:new CustomEvent(t,{detail:e});n=n.slice();for(let s of n)try{"handleEvent"in s?s.handleEvent(i):s.call(this,i)}catch(r){this._config.didError(r)}}return this}addEventListener(t,e){let n=this._events.get(t),i=this._root;return n||(n=[],this._events.set(t,n),this.customEvents.has(t)||(t==="selectionchange"&&(i=document),i.addEventListener(t,this,!0))),n.push(e),this}removeEventListener(t,e){let n=this._events.get(t),i=this._root;if(n){if(e){let s=n.length;for(;s--;)n[s]===e&&n.splice(s,1)}else n.length=0;n.length||(this._events.delete(t),this.customEvents.has(t)||(t==="selectionchange"&&(i=document),i.removeEventListener(t,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&&(me(this._root),this._mayHaveZWS=!1)}_saveRangeToBookmark(t){let e=p("INPUT",{id:this.startSelectionId,type:"hidden"}),n=p("INPUT",{id:this.endSelectionId,type:"hidden"}),i;j(t,e),t.collapse(!1),j(t,n),e.compareDocumentPosition(n)&Node.DOCUMENT_POSITION_PRECEDING&&(e.id=this.endSelectionId,n.id=this.startSelectionId,i=e,e=n,n=i),t.setStartAfter(e),t.setEndBefore(n)}_getRangeAndRemoveBookmark(t){let e=this._root,n=e.querySelector("#"+this.startSelectionId),i=e.querySelector("#"+this.endSelectionId);if(n&&i){let s=n.parentNode,r=i.parentNode,l=Array.from(s.childNodes).indexOf(n),a=Array.from(r.childNodes).indexOf(i);s===r&&(a-=1),n.remove(),i.remove(),t||(t=document.createRange()),t.setStart(s,l),t.setEnd(r,a),ne(s,t),s!==r&&ne(r,t),t.collapsed&&(s=t.startContainer,s instanceof Text&&(r=s.childNodes[t.startOffset],(!r||!(r instanceof Text))&&(r=s.childNodes[t.startOffset-1]),r&&r instanceof Text&&(t.setStart(r,0),t.collapse(!0))))}return t||null}getSelection(){let t=window.getSelection(),e=this._root,n=null;if(this._isFocused&&t&&t.rangeCount){n=t.getRangeAt(0).cloneRange();let i=n.startContainer,s=n.endContainer;i&&M(i)&&n.setStartBefore(i),s&&M(s)&&n.setEndBefore(s)}return n&&e.contains(n.commonAncestorContainer)?this._lastSelection=n:(n=this._lastSelection,document.contains(n.commonAncestorContainer)||(n=null)),n||(n=$(e.firstElementChild||e,0)),n}setSelection(t){if(this._lastSelection=t,!this._isFocused)this._enableRestoreSelection();else{let e=window.getSelection();e&&("setBaseAndExtent"in Selection.prototype?e.setBaseAndExtent(t.startContainer,t.startOffset,t.endContainer,t.endOffset):(e.removeAllRanges(),e.addRange(t)))}return this}_moveCursorTo(t){let e=this._root,n=$(e,t?0:e.childNodes.length);return _(n),this.setSelection(n),this}moveCursorToStart(){return this._moveCursorTo(!0)}moveCursorToEnd(){return this._moveCursorTo(!1)}getCursorPosition(){let t=this.getSelection(),e=t.getBoundingClientRect();if(e&&!e.top){this._ignoreChange=!0;let n=p("SPAN");n.textContent=k,j(t,n),e=n.getBoundingClientRect();let i=n.parentNode;i.removeChild(n),ne(i,t)}return e}getPath(){return this._path}_updatePathOnEvent(){this._isFocused&&this._updatePath(this.getSelection())}_updatePath(t,e){let n=t.startContainer,i=t.endContainer,s;(e||n!==this._lastAnchorNode||i!==this._lastFocusNode)&&(this._lastAnchorNode=n,this._lastFocusNode=i,s=n&&i?n===i?this._getPath(i):"(selection)":"",this._path!==s&&(this._path=s,this.fireEvent("pathChange",{path:s}))),this.fireEvent(t.collapsed?"cursor":"select",{range:t})}_getPath(t){let e=this._root,n=this._config,i="";if(t&&t!==e){let s=t.parentNode;if(i=s?this._getPath(s):"",t instanceof HTMLElement){let r=t.id,l=t.classList,a=Array.from(l).sort(),d=t.dir,c=n.classNames;i+=(i?">":"")+t.nodeName,r&&(i+="#"+r),a.length&&(i+=".",i+=a.join(".")),d&&(i+="[dir="+d+"]"),l.contains(c.highlight)&&(i+="[backgroundColor="+t.style.backgroundColor.replace(/ /g,"")+"]"),l.contains(c.color)&&(i+="[color="+t.style.color.replace(/ /g,"")+"]"),l.contains(c.fontFamily)&&(i+="[fontFamily="+t.style.fontFamily.replace(/ /g,"")+"]"),l.contains(c.fontSize)&&(i+="[fontSize="+t.style.fontSize+"]")}}return i}modifyDocument(t){let e=this._mutation;return e&&(e.takeRecords().length&&this._docWasChanged(),e.disconnect()),this._ignoreAllChanges=!0,t(),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(t,e){if(!this._isInUndoState||e){let n=this._undoIndex,i=this._undoStack,s=this._config.undo,r=s.documentSizeThreshold,l=s.undoLimit;e||(n+=1),n<this._undoStackLength&&(i.length=this._undoStackLength=n),t&&this._saveRangeToBookmark(t);let a=this._getRawHTML();r>-1&&a.length*2>r&&l>-1&&n>l&&(i.splice(0,n-l),n=l,this._undoStackLength=l),i[n]=a,this._undoIndex=n,this._undoStackLength+=1,this._isInUndoState=!0}return this}saveUndoState(t){return t||(t=this.getSelection()),this._recordUndoState(t,this._isInUndoState),this._getRangeAndRemoveBookmark(t),this}undo(){if(this._undoIndex!==0||!this._isInUndoState){this._recordUndoState(this.getSelection(),!1),this._undoIndex-=1,this._setRawHTML(this._undoStack[this._undoIndex]);let t=this._getRangeAndRemoveBookmark();t&&this.setSelection(t),this._isInUndoState=!0,this.fireEvent("undoStateChange",{canUndo:this._undoIndex!==0,canRedo:!0}),this.fireEvent("input")}return this}redo(){let t=this._undoIndex,e=this._undoStackLength;if(t+1<e&&this._isInUndoState){this._undoIndex+=1,this._setRawHTML(this._undoStack[this._undoIndex]);let n=this._getRangeAndRemoveBookmark();n&&this.setSelection(n),this.fireEvent("undoStateChange",{canUndo:!0,canRedo:t+2<e}),this.fireEvent("input")}return this}getRoot(){return this._root}_getRawHTML(){return this._root.innerHTML}_setRawHTML(t){let e=this._root;e.innerHTML=t;let n=e,i=n.firstChild;if(!i||i.nodeName==="BR"){let s=this.createDefaultBlock();i?n.replaceChild(s,i):n.appendChild(s)}else for(;n=W(n,e);)x(n);return this._ignoreChange=!0,this}getHTML(t){let e;t&&(e=this.getSelection(),this._saveRangeToBookmark(e));let n=this._getRawHTML().replace(/\u200B/g,"");return t&&this._getRangeAndRemoveBookmark(e),n}setHTML(t){let e=this._config.sanitizeToDOMFragment(t,this),n=this._root;Ne(e,this._config),re(e,n,!1),A(e,n);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,n);)x(i);for(this._ignoreChange=!0;s=n.lastChild;)n.removeChild(s);n.appendChild(e),this._undoIndex=-1,this._undoStack.length=0,this._undoStackLength=0,this._isInUndoState=!1;let r=this._getRangeAndRemoveBookmark()||$(n.firstElementChild||n,0);return this.saveUndoState(r),this.setSelection(r),this._updatePath(r,!0),this}insertHTML(t,e){let n=this._config,i=n.sanitizeToDOMFragment(t,this),s=this.getSelection();this.saveUndoState(s);try{let r=this._root;n.addLinks&&this.addDetectedLinks(i,i),Ne(i,this._config),re(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(t,e){if(e||(e=this.getSelection()),e.collapse(!0),N(t))j(e,t),e.setStartAfter(t);else{let n=this._root,i=L(e,n),s=i||n,r=null;for(;s!==n&&!s.nextSibling;)s=s.parentNode;if(s!==n){let a=s.parentNode;r=w(a,s.nextSibling,n,n)}i&&ae(i)&&C(i),n.insertBefore(t,r);let l=this.createDefaultBlock();n.insertBefore(l,r),e.setStart(l,0),e.setEnd(l,0),_(e)}return this.focus(),this.setSelection(e),this._updatePath(e),this}insertImage(t,e){let n=p("IMG",Object.assign({src:t},e));return this.insertElement(n),n}insertPlainText(t,e){let n=this.getSelection();if(n.collapsed&&S(n.startContainer,this._root,"PRE")){let c=n.startContainer,f=n.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:t}});this.fireEvent("willPaste",h),t=h.detail.text,m=!h.defaultPrevented}return m&&(u.insertData(f,t),n.setStart(u,f+t.length),n.collapse(!0)),this.setSelection(n),this}let i=t.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(t){return ge(t||this.getSelection())}getFontInfo(t){let e={color:void 0,backgroundColor:void 0,fontFamily:void 0,fontSize:void 0};t||(t=this.getSelection());let n=0,i=t.commonAncestorContainer;if(t.collapsed||i instanceof Text)for(i instanceof Text&&(i=i.parentNode);n<4&&i;){let s=i.style;if(s){let r=s.color;!e.color&&r&&(e.color=r,n+=1);let l=s.backgroundColor;!e.backgroundColor&&l&&(e.backgroundColor=l,n+=1);let a=s.fontFamily;!e.fontFamily&&a&&(e.fontFamily=a,n+=1);let d=s.fontSize;!e.fontSize&&d&&(e.fontSize=d,n+=1)}i=i.parentNode}return e}hasFormat(t,e,n){t=t.toUpperCase(),e||(e={}),n||(n=this.getSelection()),!n.collapsed&&n.startContainer instanceof Text&&n.startOffset===n.startContainer.length&&n.startContainer.nextSibling&&n.setStartBefore(n.startContainer.nextSibling),!n.collapsed&&n.endContainer instanceof Text&&n.endOffset===0&&n.endContainer.previousSibling&&n.setEndAfter(n.endContainer.previousSibling);let i=this._root,s=n.commonAncestorContainer;if(S(s,i,t,e))return!0;if(s instanceof Text)return!1;let r=new E(s,4,d=>K(n,d,!0)),l=!1,a;for(;a=r.nextNode();){if(!S(a,i,t,e))return!1;l=!0}return l}changeFormat(t,e,n,i){return n||(n=this.getSelection()),this.saveUndoState(n),e&&(n=this._removeFormat(e.tag.toUpperCase(),e.attributes||{},n,i)),t&&(n=this._addFormat(t.tag.toUpperCase(),t.attributes||{},n)),this.setSelection(n),this._updatePath(n,!0),this.focus()}_addFormat(t,e,n){let i=this._root;if(n.collapsed){let s=x(p(t,e));j(n,s);let r=s.firstChild||s,l=r instanceof Text?r.length:0;n.setStart(r,l),n.collapse(!0);let a=s;for(;N(a);)a=a.parentNode;me(a,s)}else{let s=new E(n.commonAncestorContainer,5,c=>(c instanceof Text||c.nodeName==="BR"||c.nodeName==="IMG")&&K(n,c,!0)),{startContainer:r,startOffset:l,endContainer:a,endOffset:d}=n;if(s.currentNode=r,!(r instanceof Element)&&!(r instanceof Text)||!s.filter(r)){let c=s.nextNode();if(!c)return n;r=c,l=0}do{let c=s.currentNode;if(!S(c,i,t,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(t,e);R(c,u),u.appendChild(c)}}while(s.nextNode());n=$(r,l,a,d)}return n}_removeFormat(t,e,n,i){this._saveRangeToBookmark(n);let s;n.collapsed&&(ee?s=document.createTextNode(k):s=document.createTextNode(""),j(n,s));let r=n.commonAncestorContainer;for(;N(r);)r=r.parentNode;let l=n.startContainer,a=n.startOffset,d=n.endContainer,c=n.endOffset,f=[],u=(h,g)=>{if(K(n,h,!1))return;let v,O;if(!K(n,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(v=h.firstChild;v;v=O)O=v.nextSibling,u(v,g)},m=Array.from(r.getElementsByTagName(t)).filter(h=>K(n,h,!0)&&ue(h,t,e));return 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))}),this._getRangeAndRemoveBookmark(n),s&&n.collapse(!1),ne(r,n),n}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(t,e){let n=this.getSelection();if(n.collapsed){let i=t.indexOf(":")+1;if(i)for(;t[i]==="/";)i+=1;j(n,document.createTextNode(t.slice(i)))}return e=Object.assign({href:t},this._config.tagAttributes.a,e),this.changeFormat({tag:"A",attributes:e},{tag:"A"},n)}removeLink(){return this.changeFormat(null,{tag:"A"},this.getSelection(),!0)}addDetectedLinks(t,e){let n=new E(t,4,l=>!S(l,e||this._root,"A")),i=this.linkRegExp,s=this._config.tagAttributes.a,r;for(;r=n.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(t){let e=this._config.classNames.fontFamily;return this.changeFormat(t?{tag:"SPAN",attributes:{class:e,style:"font-family: "+t+", sans-serif;"}}:null,{tag:"SPAN",attributes:{class:e}})}setFontSize(t){let e=this._config.classNames.fontSize;return this.changeFormat(t?{tag:"SPAN",attributes:{class:e,style:"font-size: "+(typeof t=="number"?t+"px":t)}}:null,{tag:"SPAN",attributes:{class:e}})}setTextColor(t){let e=this._config.classNames.color;return this.changeFormat(t?{tag:"SPAN",attributes:{class:e,style:"color:"+t}}:null,{tag:"SPAN",attributes:{class:e}})}setHighlightColor(t){let e=this._config.classNames.highlight;return this.changeFormat(t?{tag:"SPAN",attributes:{class:e,style:"background-color:"+t}}:null,{tag:"SPAN",attributes:{class:e}})}_ensureBottomLine(){let t=this._root,e=t.lastElementChild;(!e||e.nodeName!==this._config.blockTag||!P(e))&&t.appendChild(this.createDefaultBlock())}createDefaultBlock(t){let e=this._config;return x(p(e.blockTag,e.blockAttributes,t))}splitBlock(t,e){e||(e=this.getSelection());let n=this._root,i,s,r,l;if(this._recordUndoState(e),this._removeZWS(),this._getRangeAndRemoveBookmark(e),e.collapsed||H(e,n),this._config.addLinks){_(e);let u=e.startContainer,m=e.startOffset;setTimeout(()=>{Te(this,u,m)},0)}if(i=L(e,n),i&&(s=S(i,n,"PRE"))){_(e),r=e.startContainer;let u=e.startOffset;return r instanceof Text||(r=document.createTextNode(""),s.insertBefore(r,s.firstChild)),!t&&r instanceof Text&&(r.data.charAt(u-1)===`
|
||
`||X(e,n))&&(r.data.charAt(u)===`
|
||
`||Z(e,n))?(r.deleteData(u&&u-1,u?2:1),l=w(r,u&&u-1,n,n),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||t||/^T[HD]$/.test(i.nodeName))return Oe(e,"A",n),j(e,p("BR")),e.collapse(!1),this.setSelection(e),this._updatePath(e,!0),this;if((s=S(i,n,"LI"))&&(i=s),ae(i)){if(S(i,n,"UL")||S(i,n,"OL"))return this.decreaseListLevel(e),this;if(S(i,n,"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),ue(l,d,f)||(i=p(d,f),l.dir&&(i.dir=l.dir),R(l,i),i.appendChild(T(l)),l=i),me(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(t,e,n){n||(n=this.getSelection()),e&&this.saveUndoState(n);let i=this._root,s=L(n,i),r=G(n,i);if(s&&r)do if(t(s)||s===r)break;while(s=W(s,i));return e&&(this.setSelection(n),this._updatePath(n,!0)),this}modifyBlocks(t,e){e||(e=this.getSelection()),this._recordUndoState(e,this._isInUndoState);let n=this._root;ye(e,n),q(e,n,n,n);let i=Be(e,n,n);if(!e.collapsed){let s=e.endContainer;if(s===n)e.collapse(!1);else{for(;s.parentNode!==n;)s=s.parentNode;e.setStartBefore(s),e.collapse(!0)}}return j(e,t.call(this,i)),e.endOffset<e.endContainer.childNodes.length&&F(e.endContainer.childNodes[e.endOffset],n),F(e.startContainer.childNodes[e.startOffset],n),this._getRangeAndRemoveBookmark(e),this.setSelection(e),this._updatePath(e,!0),this}setTextAlignment(t){return this.forEachBlock(e=>{let n=e.className.split(/\s+/).filter(i=>!!i&&!/^align/.test(i)).join(" ");t?(e.className=n+" align-"+t,e.style.textAlign=t):(e.className=n,e.style.textAlign="")},!0),this.focus()}setTextDirection(t){return this.forEachBlock(e=>{t?e.dir=t:e.removeAttribute("dir")},!0),this.focus()}_getListSelection(t,e){let n=t.commonAncestorContainer,i=t.startContainer,s=t.endContainer;for(;n&&n!==e&&!/^[OU]L$/.test(n.nodeName);)n=n.parentNode;if(!n||n===e)return null;for(i===n&&(i=i.childNodes[t.startOffset]),s===n&&(s=s.childNodes[t.endOffset]);i&&i.parentNode!==n;)i=i.parentNode;for(;s&&s.parentNode!==n;)s=s.parentNode;return[n,i,s]}increaseListLevel(t){t||(t=this.getSelection());let e=this._root,n=this._getListSelection(t,e);if(!n)return this.focus();let[i,s,r]=n;if(!s||s===i.firstChild)return this.focus();this._recordUndoState(t,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(t),this.setSelection(t),this._updatePath(t,!0),this.focus()}decreaseListLevel(t){t||(t=this.getSelection());let e=this._root,n=this._getListSelection(t,e);if(!n)return this.focus();let[i,s,r]=n;s||(s=i.firstChild),r||(r=i.lastChild),this._recordUndoState(t,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(t),this.setSelection(t),this._updatePath(t,!0),this.focus()}_makeList(t,e){let n=le(t,this._root),i=this._config.tagAttributes,s=i[e.toLowerCase()],r=i.li,l;for(;l=n.nextNode();)if(l.parentNode instanceof HTMLLIElement&&(l=l.parentNode,n.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)),n.currentNode=a}return t}makeUnorderedList(){return this.modifyBlocks(t=>this._makeList(t,"UL")),this.focus()}makeOrderedList(){return this.modifyBlocks(t=>this._makeList(t,"OL")),this.focus()}removeList(){return this.modifyBlocks(t=>{let e=t.querySelectorAll("UL, OL"),n=t.querySelectorAll("LI"),i=this._root;for(let s=0,r=e.length;s<r;s+=1){let l=e[s],a=T(l);A(a,i),R(l,a)}for(let s=0,r=n.length;s<r;s+=1){let l=n[s];P(l)?R(l,this.createDefaultBlock([T(l)])):(A(l,i),R(l,T(l)))}return t}),this.focus()}increaseQuoteLevel(t){return this.modifyBlocks(e=>p("BLOCKQUOTE",this._config.tagAttributes.blockquote,[e]),t),this.focus()}decreaseQuoteLevel(t){return this.modifyBlocks(e=>(Array.from(e.querySelectorAll("blockquote")).filter(n=>!S(n.parentNode,e,"BLOCKQUOTE")).forEach(n=>{R(n,T(n))}),e),t),this.focus()}removeQuote(t){return this.modifyBlocks(()=>this.createDefaultBlock([p("INPUT",{id:this.startSelectionId,type:"hidden"}),p("INPUT",{id:this.endSelectionId,type:"hidden"})]),t),this.focus()}code(){let t=this.getSelection();return t.collapsed||Q(t.commonAncestorContainer)?(this.modifyBlocks(e=>{let n=this._root,i=document.createDocumentFragment(),s=le(e,n),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]))},t),this.focus()):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,t),this}removeCode(){let t=this.getSelection(),e=t.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}A(a,s),R(a,T(a))}return i},t),this.focus()):this.changeFormat(null,{tag:"CODE"},t),this}toggleCode(){return this.hasFormat("PRE")||this.hasFormat("CODE")?this.removeCode():this.code(),this}_removeFormatting(t,e){for(let n=t.firstChild,i;n;n=i){if(i=n.nextSibling,N(n)){if(n instanceof Text||n.nodeName==="BR"||n.nodeName==="IMG"){e.appendChild(n);continue}}else if(P(n)){e.appendChild(this.createDefaultBlock([this._removeFormatting(n,document.createDocumentFragment())]));continue}this._removeFormatting(n,e)}return e}removeAllFormatting(t){if(t||(t=this.getSelection()),t.collapsed)return this.focus();let e=this._root,n=t.commonAncestorContainer;for(;n&&!P(n);)n=n.parentNode;if(n||(ye(t,e),n=e),n instanceof Text)return this.focus();this.saveUndoState(t),q(t,n,n,e);let i=t.startContainer,s=t.startOffset,r=t.endContainer,l=t.endOffset,a=document.createDocumentFragment(),d=document.createDocumentFragment(),c=w(r,l,n,e),f=w(i,s,n,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){n.insertBefore(d,c);let m=Array.from(n.childNodes);s=m.indexOf(f),l=u?m.indexOf(u)+1:0}else c&&(s=Array.from(n.childNodes).indexOf(c),l=s);return t.setStart(n,s),t.setEnd(n,l),ne(n,t),_(t),this.setSelection(t),this._updatePath(t,!0),this.focus()}};window.Squire=Ce;})();
|
||
//# sourceMappingURL=squire.js.map
|