0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 15:23:29 -05:00
Squire/dist/squire.mjs

12 lines
50 KiB
JavaScript
Raw Normal View History

2024-07-17 01:21:35 -05:00
var it=()=>!0,C=class{constructor(t,e,n){this.root=t,this.currentNode=t,this.nodeType=e,this.filter=n||it}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 B="\u200B",J=navigator.userAgent,de=/Mac OS X/.test(J),fe=/Windows NT/.test(J),_e=/iP(?:ad|hone|od)/.test(J)||de&&!!navigator.maxTouchPoints,xt=/Android/.test(J),Fe=/Gecko\//.test(J),se=/Edge\//.test(J),st=!se&&/WebKit\//.test(J),k=de||_e?"Meta-":"Ctrl-",oe=st,He="onbeforeinput"in document&&"inputType"in new InputEvent("input"),I=/[^ \t\r\n]/;var at=/^(?:#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)$/,ct=new Set(["BR","HR","IFRAME","IMG","INPUT"]),dt=0,xe=1,Pe=2,Ue=3,ue=new WeakMap,qe=()=>{ue=new WeakMap},M=o=>ct.has(o.nodeName),be=o=>{switch(o.nodeType){case 3:return xe;case 1:case 11:if(ue.has(o))return ue.get(o);break;default:return dt}let t;return Array.from(o.childNodes).every(N)?at.test(o.nodeName)?t=xe:t=Pe:t=Ue,ue.set(o,t),t},N=o=>be(o)===xe,P=o=>be(o)===Pe,Q=o=>be(o)===Ue;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,he=(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(he(o,e,n))return o;o=o.parentNode}return null},me=(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,_=o=>{let t=document.createDocumentFragment(),e=o.firstChild;for(;e;)t.appendChild(e),e=o.firstChild;return t},E=o=>{let t=o.parentNode;return t&&t.removeChild(o),o},R=(o,t)=>{let e=o.parentNode;e&&e.replaceChild(t,o)};var ft=o=>o instanceof Element?o.nodeName==="BR":I.test(o.data),ee=(o,t)=>{let e=o.parentNode;for(;N(e);)e=e.parentNode;let n=new C(e,5,ft);return n.currentNode=o,!!n.nextNode()||t&&!n.previousNode()},re=(o,t)=>{let e=new C(o,4),n,i;for(;n=e.nextNode();)for(;(i=n.data.indexOf(B))>-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 ut=0,ht=1,mt=2,pt=3,K=(o,t,e)=>{let n=document.createRange();if(n.selectNode(t),e){let i=o.compareBoundaryPoints(pt,n)>-1,s=o.compareBoundaryPoints(ht,n)<1;return!i&&!s}else{let i=o.compareBoundaryPoints(ut,n)<1,s=o.compareBoundaryPoints(mt,n)>-1;return i&&s}},T=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.
`,r=!1),i=n.nextNode();return s=s.replace(/ /g," "),s};var De=Array.prototype.indexOf,je=(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?.contains(t.commonAncestorContainer)&&(f=d);let u;n?u=H(t,e):(t=t.cloneRange(),T(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 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},Qe=function(o){let t=this.getSelection(),e=this._root;if(t.collapsed){o.preventDefault();return}this.saveUndoState(t),je(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)},Xe=function(o){je(o,this.getSelection(),this._root,!1,this._config.willCutCopy,this._config.toPlainText,!1)},Ae=function(o){this._isShiftDown=o.shiftKey},$e=function(o){let t=o.clipboardData,e=t?.items,n=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:t});return}if(!se){o.preventDefault(),l&&(!n||!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=t?.types;if(!se&&a&&(De.call(a,"text/html")>-1||!Fe&&De.call(a,"text/plain")>-1&&De.call(a,"text/rtf")<0)){o.preventDefault();let b;!n&&(b=t.getData("text/html"))?this.insertHTML(b,!0):((b=t.getData("text/plain"))||(b=t.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,E(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)},Ve=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 ne=(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===B);)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(t)),e===o._root&&(e=e.firstChild)&&e.nodeName==="BR"&&E(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;E(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 Ye=(o,t,e)=>{let n=o._root;if(o._removeZWS(),o.saveUndoState(e),!e.collapsed)t.preventDefault(),H(e,n),ne(o,e);else if(X(e,n)){t.preventDefault();let i=L(e,n);if(!i)return;let s=i;D(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{T(e);let i=e.startContainer,s=e.startOffset,r=i.parentNode;i instanceof Text&&r instanceof HTMLA
`),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,"&nbsp;"),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()),T(t);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 C(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;re(a,s)}else{let s=new C(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&&(oe?s=document.createTextNode(B):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 b,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(b=h.firstChild;b;b=O)O=b.nextSibling,u(b,g)},m=Array.from(r.getElementsByTagName(t)).filter(h=>K(n,h,!0)&&he(h,t,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,_(h))}),oe&&s){s=s.parentNode;let h=s;for(;h&&N(h);)h=h.parentNode;h&&re(h,s)}return this._getRangeAndRemoveBookmark(n),s&&n.collapse(!1),te(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]==
2024-01-31 19:51:02 -05:00
`||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||E(r),r=this.createDefaultBlock(),l.parentNode.insertBefore(r,l),l.textContent||E(l),e.setStart(r,0)):(r.insertData(u,`
2024-07-17 01:21:35 -05:00
`),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),ce(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),he(l,d,f)||(i=p(d,f),l.dir&&(i.dir=l.dir),R(l,i),i.appendChild(_(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===B)){u=document.createTextNode(""),R(l,u),l=u;break}for(;u&&u instanceof Text&&!u.data&&(m=u.nextSibling,!(!m||m.nodeName==="BR"));)E(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([_(s)])),d.insertBefore(s,a);while(s=l)}return i.firstChild||E(i),a&&F(a,e),this._getRangeAndRemoveBookmark(t),this.setSelection(t),this._updatePath(t,!0),this.focus()}_makeList(t,e){let n=ae(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,[_(l)]))}else{let a=p("LI",r);l.dir&&(a.dir=l.dir);let
`)):E(f)}for(a=r.querySelectorAll("CODE"),c=a.length;c--;)R(a[c],_(a[c]));i.childNodes.length&&i.appendChild(document.createTextNode(`
`)),i.appendChild(_(r))}let l=new C(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 C(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,_(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),te(n,t),T(t),this.setSelection(t),this._updatePath(t,!0),this.focus()}};var Mo=Ce;export{Mo as default};
//# sourceMappingURL=squire.mjs.map