mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 15:23:29 -05:00
13 lines
50 KiB
JavaScript
13 lines
50 KiB
JavaScript
|
var ot=()=>!0,C=class{root;currentNode;nodeType;filter;constructor(n,e,t){this.root=n,this.currentNode=n,this.nodeType=e,this.filter=t||ot}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 W="\u200B",V=navigator.userAgent,ce=/Mac OS X/.test(V),de=/Windows NT/.test(V),Ce=/iP(?:ad|hone|od)/.test(V)||ce&&!!navigator.maxTouchPoints,Me=/Android/.test(V),Fe=/Gecko\//.test(V),se=/Edge\//.test(V),st=!se&&/WebKit\//.test(V),k=ce||Ce?"Meta-":"Ctrl-",re=st,He="onbeforeinput"in document&&"inputType"in new InputEvent("input"),w=/[^ \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,_e=1,Pe=2,Ue=3,fe=new WeakMap,qe=()=>{fe=new WeakMap},I=i=>ct.has(i.nodeName),xe=i=>{switch(i.nodeType){case 3:return _e;case 1:case 11:if(fe.has(i))return fe.get(i);break;default:return dt}let n;return Array.from(i.childNodes).every(N)?at.test(i.nodeName)?n=_e:n=Pe:n=Ue,fe.set(i,n),n},N=i=>xe(i)===_e,H=i=>xe(i)===Pe,j=i=>xe(i)===Ue;var m=(i,n,e)=>{let t=document.createElement(i);if(n instanceof Array&&(e=n,n=null),n)for(let o in n){let s=n[o];s!==void 0&&t.setAttribute(o,s)}return e&&e.forEach(o=>t.appendChild(o)),t},ve=(i,n)=>I(i)||i.nodeType!==n.nodeType||i.nodeName!==n.nodeName?!1:i instanceof HTMLElement&&n instanceof HTMLElement?i.nodeName!=="A"&&i.className===n.className&&i.style.cssText===n.style.cssText:!0,ue=(i,n,e)=>{if(i.nodeName!==n)return!1;for(let t in e)if(!("getAttribute"in i)||i.getAttribute(t)!==e[t])return!1;return!0},g=(i,n,e,t)=>{for(;i&&i!==n;){if(ue(i,e,t))return i;i=i.parentNode}return null},he=(i,n)=>{let e=i.childNodes;for(;n&&i instanceof Element;)i=e[n-1],e=i.childNodes,n=e.length;return i},Le=(i,n)=>{let e=i;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},O=i=>i instanceof Element||i instanceof DocumentFragment?i.childNodes.length:i instanceof CharacterData?i.length:0,S=i=>{let n=document.createDocumentFragment(),e=i.firstChild;for(;e;)n.appendChild(e),e=i.firstChild;return n},E=i=>{let n=i.parentNode;return n&&n.removeChild(i),i},y=(i,n)=>{let e=i.parentNode;e&&e.replaceChild(n,i)};var ft=i=>i instanceof Element?i.nodeName==="BR":w.test(i.data),Y=(i,n)=>{let e=i.parentNode;for(;N(e);)e=e.parentNode;let t=new C(e,5,ft);return t.currentNode=i,!!t.nextNode()||n&&!t.previousNode()},me=(i,n)=>{let e=new C(i,4),t,o;for(;t=e.nextNode();)for(;(o=t.data.indexOf(W))>-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)||O(r)));)s=r,r=s.parentNode;break}else t.deleteData(o,1)};var ut=0,ht=1,mt=2,pt=3,K=(i,n,e)=>{let t=document.createRange();if(t.selectNode(n),e){let o=i.compareBoundaryPoints(pt,t)>-1,s=i.compareBoundaryPoints(ht,t)<1;return!o&&!s}else{let o=i.compareBoundaryPoints(ut,t)<1,s=i.compareBoundaryPoints(mt,t)>-1;return o&&s}},x=i=>{let{startContainer:n,startOffset:e,endContainer:t,endOffset:o}=i;for(;!(n instanceof Text);){let s=n.childNodes[e];if(!s||I(s)){e&&(s=n.childNodes[e-1],s instanceof Text&&(n=s,e=s.data.length));break}n=s,e=0}if(o)for(;!(t instanceof Text);){let s=t.childNodes[o-1];if(!s||I(s)){if(s&&s.nodeName==="BR"&&!Y(s,!1)){o-=1;continue}break}t=s,o
|
|||
|
`)),!s&&d&&c!==d&&r.setData("text/html",d),r.setData("text/plain",c),i.preventDefault()},je=(i,n,e,t,o,s,r)=>{if(!se&&i.clipboardData){let l=L(n,e),a=G(n,e),d=e;l===a&&l?.contains(n.commonAncestorContainer)&&(d=l);let c;t?c=F(n,e):(n=n.cloneRange(),x(n),P(n,d,d,e),c=n.cloneContents());let f=n.commonAncestorContainer;for(f instanceof Text&&(f=f.parentNode);f&&f!==d;){let u=f.cloneNode(!1);u.appendChild(c),c=u,f=f.parentNode}return _t(i,c,e,o,s,r),!0}return!1},Qe=function(i){let n=this.getSelection(),e=this._root;if(n.collapsed){i.preventDefault();return}this.saveUndoState(n),je(i,n,e,!0,this._config.willCutCopy,null,!1)||setTimeout(()=>{try{this._ensureBottomLine()}catch(o){this._config.didError(o)}},0),this.setSelection(n)},Xe=function(i){je(i,this.getSelection(),this._root,!1,this._config.willCutCopy,null,!1)},De=function(i){this._isShiftDown=i.shiftKey},$e=function(i){let n=i.clipboardData,e=n?.items,t=this._isShiftDown,o=!1,s=!1,r=null,l=null;if(e){let b=e.length;for(;b--;){let R=e[b],D=R.type;D==="text/html"?l=R:D==="text/plain"||D==="text/uri-list"?r=R:D==="text/rtf"?o=!0:/^image\/.*/.test(D)&&(s=!0)}if(s&&!(o&&l)){i.preventDefault(),this.fireEvent("pasteImage",{clipboardData:n});return}if(!se){i.preventDefault(),l&&(!t||!r)?l.getAsString(R=>{this.insertHTML(R,!0)}):r&&r.getAsString(R=>{let D=!1,we=this.getSelection();if(!we.collapsed&&w.test(we.toString())){let Ie=this.linkRegExp.exec(R);D=!!Ie&&Ie[0].length===R.length}D?this.makeLink(R):this.insertPlainText(R,!0)});return}}let a=n?.types;if(!se&&a&&(Be.call(a,"text/html")>-1||!Fe&&Be.call(a,"text/plain")>-1&&Be.call(a,"text/rtf")<0)){i.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,p=c.endContainer,h=c.endOffset,T=m("DIV",{contenteditable:"true",style:"position:fixed; overflow:hidden; top:0; right:100%; width:1px; height:1px;"});d.appendChild(T),c.selectNodeContents(T),this.setSelection(c),setTimeout(()=>{try{let b="",R=T,D;for(;T=R;)R=T.nextSibling,E(T),D=T.firstChild,D&&D===T.lastChild&&D instanceof HTMLDivElement&&(T=D),b+=T.innerHTML;this.setSelection(X(f,u,p,h)),b&&this.insertHTML(b,!0)}catch(b){this._config.didError(b)}},0)},Ve=function(i){if(!i.dataTransfer)return;let n=i.dataTransfer.types,e=n.length,t=!1,o=!1;for(;e--;)switch(n[e]){case"text/plain":t=!0;break;case"text/html":o=!0;break;default:return}(o||t&&this.saveUndoState)&&this.saveUndoState()};var Ae=(i,n,e)=>{n.preventDefault(),i.splitBlock(n.shiftKey,e)};var te=(i,n)=>{try{n||(n=i.getSelection());let e=n.startContainer;e instanceof Text&&(e=e.parentNode);let t=e;for(;N(t)&&(!t.textContent||t.textContent===W);)e=t,t=e.parentNode;e!==t&&(n.setStart(t,Array.from(t.childNodes).indexOf(e)),n.collapse(!0),t.removeChild(e),H(t)||(t=z(t,i._root)||i._root),_(t),x(n)),e===i._root&&(e=e.firstChild)&&e.nodeName==="BR"&&E(e),i._ensureBottomLine(),i.setSelection(n),i._updatePath(n,!0)}catch(e){i._config.didError(e)}},Se=(i,n)=>{let e;for(;(e=i.parentNode)&&!(e===n||e.isContentEditable);)i=e;E(i)},Ee=(i,n,e)=>{if(g(n,i._root,"A"))return;let t=n.data||"",o=Math.max(t.lastIndexOf(" ",e-1),t.lastIndexOf("\xA0",e-1))+1,s=t.slice(o,e),r=i.linkRegExp.exec(s);if(r){let l=i.getSelection();i._docWasChanged(),i._recordUndoState(l),i._getRangeAndRemoveBookmark(l);let a=o+r.index,d=a+r[0].length;a&&(n=n.splitText(a));let c=i._config.tagAttributes.a,f=m("A",Object.assign({href:r[1]?/^(?:ht|f)tps?:/i.test(r[1])?r[1]:"http://"+r[1]:"mailto:"+r[0]},c));f.textContent=t.slice(a,d),n.parentNode.insertBefore(f,n);let u=l.startOffset;if(n.data=t.slice(d),l.startContainer===n){let p=u-d;l.setStart(n,p),l.setEnd(n,p)}i.setSelection(l)}};var Ye=(i,n,e)=>{let t=i._root;if(i._removeZWS(),i.saveUndoState(e),!e.collapsed)n.preventDefault(),F(e,t),te(i,e);else if(Q(e,t)){n.preventDefault();let o=L(e,t);if(!o)return;let s=o;B(s.parentNode,t);let r=z(s,t);if(r){if(!r.isContentEditable){Se(r,t);return}for($(r,s,e,t),s=r.parentNode;s!==t&&!s.ne
|
|||
|
`)&&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 o=this._root===document.activeElement;if(n==="focus"){if(!o||this._isFocused)return this;this._isFocused=!0}else{if(o||!this._isFocused)return this;this._isFocused=!1}}if(t){let o=e instanceof Event?e:new CustomEvent(n,{detail:e});t=t.slice();for(let s of t)try{"handleEvent"in s?s.handleEvent(o):s.call(this,o)}catch(r){this._config.didError(r)}}return this}customEvents=new Set(["pathChange","select","input","pasteImage","undoStateChange"]);addEventListener(n,e){let t=this._events.get(n),o=this._root;return t||(t=[],this._events.set(n,t),this.customEvents.has(n)||(n==="selectionchange"&&(o=document),o.addEventListener(n,this,!0))),t.push(e),this}removeEventListener(n,e){let t=this._events.get(n),o=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"&&(o=document),o.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&&(me(this._root),this._mayHaveZWS=!1)}startSelectionId="squire-selection-start";endSelectionId="squire-selection-end";_saveRangeToBookmark(n){let e=m("INPUT",{id:this.startSelectionId,type:"hidden"}),t=m("INPUT",{id:this.endSelectionId,type:"hidden"}),o;Z(n,e),n.collapse(!1),Z(n,t),e.compareDocumentPosition(t)&Node.DOCUMENT_POSITION_PRECEDING&&(e.id=this.endSelectionId,t.id=this.startSelectionId,o=e,e=t,t=o),n.setStartAfter(e),n.setEndBefore(t)}_getRangeAndRemoveBookmark(n){let e=this._root,t=e.querySelector("#"+this.startSelectionId),o=e.querySelector("#"+this.endSelectionId);if(t&&o){let s=t.parentNode,r=o.parentNode,l=Array.from(s.childNodes).indexOf(t),a=Array.from(r.childNodes).indexOf(o);s===r&&(a-=1),t.remove(),o.remove(),n||(n=document.createRange()),n.setStart(s,l),n.setEnd(r,a),J(s,n),s!==r&&J(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 o=t.startContainer,s=t.endContainer;o&&I(o)&&t.setStartBefore(o),s&&I(s)&&t.setEndBefore(s)}return t&&e.contains(t.c
|
|||
|
`),s=this._config,r=s.blockTag,l=s.blockAttributes,a="</"+r+">",d="<"+r;for(let c in l)d+=" "+c+'="'+Oe(l[c])+'"';d+=">";for(let c=0,f=o.length;c<f;c+=1){let u=o[c];u=Oe(u).replace(/ (?=(?: |$))/g," "),c&&(u=d+(u||"<BR>")+a),o[c]=u}return this.insertHTML(o.join(""),e)}getSelectedText(){let n=this.getSelection();if(n.collapsed)return"";let e=n.startContainer,t=n.endContainer,o=new C(n.commonAncestorContainer,5,d=>K(n,d,!0));o.currentNode=e;let s=e,r="",l=!1,a;for((!(s instanceof Element)&&!(s instanceof Text)||!o.filter(s))&&(s=o.nextNode());s;)s instanceof Text?(a=s.data,a&&/\S/.test(a)&&(s===t&&(a=a.slice(0,n.endOffset)),s===e&&(a=a.slice(n.startOffset)),r+=a,l=!0)):(s.nodeName==="BR"||l&&!N(s))&&(r+=`
|
|||
|
`,l=!1),s=o.nextNode();return r}getFontInfo(n){let e={color:void 0,backgroundColor:void 0,fontFamily:void 0,fontSize:void 0};n||(n=this.getSelection());let t=0,o=n.commonAncestorContainer;if(n.collapsed||o instanceof Text)for(o instanceof Text&&(o=o.parentNode);t<4&&o;){let s=o.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)}o=o.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 o=this._root,s=t.commonAncestorContainer;if(g(s,o,n,e))return!0;if(s instanceof Text)return!1;let r=new C(s,4,d=>K(t,d,!0)),l=!1,a;for(;a=r.nextNode();){if(!g(a,o,n,e))return!1;l=!0}return l}changeFormat(n,e,t,o){return t||(t=this.getSelection()),this.saveUndoState(t),e&&(t=this._removeFormat(e.tag.toUpperCase(),e.attributes||{},t,o)),n&&(t=this._addFormat(n.tag.toUpperCase(),n.attributes||{},t)),this.setSelection(t),this._updatePath(t,!0),this.focus()}_addFormat(n,e,t){let o=this._root;if(t.collapsed){let s=_(m(n,e));Z(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;me(a,s)}else{let s=new C(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(!g(c,o,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=m(n,e);y(c,u),u.appendChild(c)}}while(s.nextNode());t=X(r,l,a,d)}return t}_removeFormat(n,e,t,o){this._saveRangeToBookmark(t);let s;t.collapsed&&(re?s=document.createTextNode(W):s=document.createTextNode(""),Z(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,T)=>{if(K(t,h,!1))return;let b,R;if(!K(t,h,!0)){!(h instanceof HTMLInputElement)&&(!(h instanceof Text)||h.data)&&f.push([T,h]);return}if(h instanceof Text)h===d&&c!==h.length&&f.push([T,h.splitText(c)]),h===l&&a&&(h.splitText(a),f.push([T,h]));else for(b=h.firstChild;b;b=R)R=b.nextSibling,u(b,T)},p=Array.from(r.getElementsByTagName(n)).filter(h=>K(t,h,!0)&&ue(h,n,e));return o||p.forEach(h=>{u(h,h)}),f.forEach(([h,T])=>{h=h.cloneNode(!1),y(T,h),h.appendChild(T)}),p.forEach(h=>{y(h,S(h))}),this._getRangeAndRemoveBookmark(t),s&&t.collapse(!1),J(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 o=n.indexOf(":")+1;if(o)for(;n[o]==="/";)o+=1;Z(t,document.createTextNode(n.slice(o)))}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)}linkRegExp=/\b(?:((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9][a-z0-9.\-]*[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:[^\s?&
|
|||
|
`||Q(e,t))&&(r.data.charAt(u)===`
|
|||
|
`||ie(e,t))?(r.deleteData(u&&u-1,u?2:1),l=A(r,u&&u-1,t,t),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,`
|
|||
|
`),_(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(!o||n||/^T[HD]$/.test(o.nodeName))return Re(e,"A",t),Z(e,m("BR")),e.collapse(!1),this.setSelection(e),this._updatePath(e,!0),this;if((s=g(o,t,"LI"))&&(o=s),ae(o)){if(g(o,t,"UL")||g(o,t,"OL"))return this.decreaseListLevel(e),this;if(g(o,t,"BLOCKQUOTE"))return this.removeQuote(e),this}r=e.startContainer;let a=e.startOffset,d=this.tagAfterSplit[o.nodeName];l=A(r,a,o.parentNode,this._root);let c=this._config,f=null;for(d||(d=c.blockTag,f=c.blockAttributes),ue(l,d,f)||(o=m(d,f),l.dir&&(o.dir=l.dir),y(l,o),o.appendChild(S(l)),l=o),me(o),ge(o),_(o);l instanceof Element;){let u=l.firstChild,p;if(l.nodeName==="A"&&(!l.textContent||l.textContent===W)){u=document.createTextNode(""),y(l,u),l=u;break}for(;u&&u instanceof Text&&!u.data&&(p=u.nextSibling,!(!p||p.nodeName==="BR"));)E(u),u=p;if(!u||u.nodeName==="BR"||u instanceof Text)break;l=u}return e=X(l,0),this.setSelection(e),this._updatePath(e,!0),this}forEachBlock(n,e,t){t||(t=this.getSelection()),e&&this.saveUndoState(t);let o=this._root,s=L(t,o),r=G(t,o);if(s&&r)do if(n(s)||s===r)break;while(s=U(s,o));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;ke(e,t),P(e,t,t,t);let o=ye(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 Z(e,n.call(this,o)),e.endOffset<e.endContainer.childNodes.length&&M(e.endContainer.childNodes[e.endOffset],t),M(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(o=>!!o&&!/^align/.test(o)).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,o=n.startContainer,s=n.endContainer;for(;t&&t!==e&&!/^[OU]L$/.test(t.nodeName);)t=t.parentNode;if(!t||t===e)return null;for(o===t&&(o=o.childNodes[n.startOffset]),s===t&&(s=s.childNodes[n.endOffset]);o&&o.parentNode!==t;)o=o.parentNode;for(;s&&s.parentNode!==t;)s=s.parentNode;return[t,o,s]}increaseListLevel(n){n||(n=this.getSelection());let e=this._root,t=this._getListSelection(n,e);if(!t)return this.focus();let[o,s,r]=t;if(!s||s===o.firstChild)return this.focus();this._recordUndoState(n,this._isInUndoState);let l=o.nodeName,a=s.previousSibling,d,c;a.nodeName!==l&&(d=this._config.tagAttributes[l.toLowerCase()],a=m(l,d),o.insertBefore(a,s));do c=s===r?null:s.nextSibling,a.appendChild(s);while(s=c);return c=a.nextSibling,c&&M(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[o,s,r]=t;s||(s=o.firstChild),r||(r=o.lastChild),this._recordUndoState(n,this._isInUndoState);let l,a=null;if(s){let d=o.parentNode;if(a=r.nextSibling?A(o,r.nextSibling,d,e):o.nextSibling,d!==e&&d.nodeName==="LI"){for(d=d.parentNode;a;)l=a.nextSibling,r.appendChild(a),a=l;a=o.parentNode.nextSibling}let c=!/^[OU]L$/.test(d.nodeName);do l=s===r?null:s.nextSibling,o.removeChild(s),c&&s.nodeName==="LI"&&(s=this.createDefaultBlock([S(s)])),d.insertBefore(s,a);while(s=l)}return o.firstChild||E(o),a&&M(a,e),this._getRangeAndRemoveBookmark(n),this.setSelection(n),this._updatePath(n,!0),this.focus()}_makeList(n,e){let t=le(n,this._root),o=this._config.tagAttributes,s=o[e.toLowerCase()],r=o.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)&&y(l,m(e,s,[S(l)]))}else{let a=m("LI",r);l.dir&&(a.dir=l.dir);let
|
|||
|
`)):E(f)}for(a=r.querySelectorAll("CODE"),c=a.length;c--;)y(a[c],S(a[c]));o.childNodes.length&&o.appendChild(document.createTextNode(`
|
|||
|
`)),o.appendChild(S(r))}let l=new C(o,4);for(;r=l.nextNode();)r.data=r.data.replace(/ /g," ");return o.normalize(),_(m("PRE",this._config.tagAttributes.pre,[o]))},n),this.focus()):this.changeFormat({tag:"CODE",attributes:this._config.tagAttributes.code},null,n),this}removeCode(){let n=this.getSelection(),e=n.commonAncestorContainer;return g(e,this._root,"PRE")?(this.modifyBlocks(o=>{let s=this._root,r=o.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(),p;for(;(p=f.indexOf(`
|
|||
|
`))>-1;)u.appendChild(document.createTextNode(f.slice(0,p))),u.appendChild(m("BR")),f=f.slice(p+1);c.parentNode.insertBefore(u,c),c.data=f}B(a,s),y(a,S(a))}return o},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,o;t;t=o){if(o=t.nextSibling,N(t)){if(t instanceof Text||t.nodeName==="BR"||t.nodeName==="IMG"){e.appendChild(t);continue}}else if(H(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;let e=this._root,t=n.commonAncestorContainer;for(;t&&!H(t);)t=t.parentNode;if(t||(ke(n,e),t=e),t instanceof Text)return this;this.saveUndoState(n),P(n,t,t,e);let o=n.startContainer,s=n.startOffset,r=n.endContainer,l=n.endOffset,a=document.createDocumentFragment(),d=document.createDocumentFragment(),c=A(r,l,t,e),f=A(o,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 p=Array.from(t.childNodes);s=p.indexOf(f),l=u?p.indexOf(u)+1:0}else c&&(s=Array.from(t.childNodes).indexOf(c),l=s);return n.setStart(t,s),n.setEnd(t,l),J(t,n),x(n),this.setSelection(n),this._updatePath(n,!0),this.focus()}};var vi=Te;export{vi as default};
|
|||
|
//# sourceMappingURL=squire.mjs.map
|