o&&c.type instanceof Po){let d=Math.max(o,c.from)-o,p=Math.min(a,c.to)-o;di.map(t,n,Oa));return fo.from(r)}forChild(t,n){if(n.isLeaf)return wt.empty;let r=[];for(let i=0;in instanceof wt)?t:t.reduce((n,r)=>n.concat(r instanceof wt?r:r.members),[]))}}}function yH(e,t,n,r,i,o,a){let s=e.slice();for(let d=0,p=o;d{let b=T-E-(g-f);for(let S=0;Sy+p-_)continue;let R=s[S]+p-_;g>=R?s[S+1]=f<=R?-2:-1:f>=p&&b&&(s[S]+=b,s[S+1]+=b)}_+=b}),p=n.maps[d].map(p,-1)}let c=!1;for(let d=0;d=r.content.size){c=!0;continue}let f=n.map(e[d+1]+o,-1),g=f-i,{index:E,offset:T}=r.content.findIndex(_),b=r.maybeChild(E);if(b&&T==_&&T+b.nodeSize==g){let S=s[d+2].mapInner(n,b,p+1,e[d]+o+1,a);S!=cn?(s[d]=_,s[d+1]=g,s[d+2]=S):(s[d+1]=-2,c=!0)}else c=!0}if(c){let d=CH(s,e,t,n,i,o,a),p=h_(d,r,0,a);t=p.local;for(let _=0;_n&&a.to{let d=$D(e,s,c+n);if(d){o=!0;let p=h_(d,s,n+c+1,r);p!=cn&&i.push(c,c+s.nodeSize,p)}});let a=HD(o?VD(e):e,-n).sort(Aa);for(let s=0;s0;)t++;e.splice(t,0,n)}function qg(e){let t=[];return e.someProp("decorations",n=>{let r=n(e.state);r&&r!=cn&&t.push(r)}),e.cursorWrapper&&t.push(wt.create(e.state.doc,[e.cursorWrapper.deco])),fo.from(t)}const RH={childList:!0,characterData:!0,characterDataOldValue:!0,attributes:!0,attributeOldValue:!0,subtree:!0},NH=qn&&Lo<=11;class vH{constructor(){this.anchorNode=null,this.anchorOffset=0,this.focusNode=null,this.focusOffset=0}set(t){this.anchorNode=t.anchorNode,this.anchorOffset=t.anchorOffset,this.focusNode=t.focusNode,this.focusOffset=t.focusOffset}clear(){this.anchorNode=this.focusNode=null}eq(t){return t.anchorNode==this.anchorNode&&t.anchorOffset==this.anchorOffset&&t.focusNode==this.focusNode&&t.focusOffset==this.focusOffset}}class OH{constructor(t,n){this.view=t,this.handleDOMChange=n,this.queue=[],this.flushingSoon=-1,this.observer=null,this.currentSelection=new vH,this.onCharData=null,this.suppressingSelectionUpdates=!1,this.lastChangedTextNode=null,this.observer=window.MutationObserver&&new window.MutationObserver(r=>{for(let i=0;ii.type=="childList"&&i.removedNodes.length||i.type=="characterData"&&i.oldValue.length>i.target.nodeValue.length)?this.flushSoon():this.flush()}),NH&&(this.onCharData=r=>{this.queue.push({target:r.target,type:"characterData",oldValue:r.prevValue}),this.flushSoon()}),this.onSelectionChange=this.onSelectionChange.bind(this)}flushSoon(){this.flushingSoon<0&&(this.flushingSoon=window.setTimeout(()=>{this.flushingSoon=-1,this.flush()},20))}forceFlush(){this.flushingSoon>-1&&(window.clearTimeout(this.flushingSoon),this.flushingSoon=-1,this.flush())}start(){this.observer&&(this.observer.takeRecords(),this.observer.observe(this.view.dom,RH)),this.onCharData&&this.view.dom.addEventListener("DOMCharacterDataModified",this.onCharData),this.connectSelection()}stop(){if(this.observer){let t=this.observer.takeRecords();if(t.length){for(let n=0;nthis.flush(),20)}this.observer.disconnect()}this.onCharData&&this.view.dom.removeEventListener("DOMCharacterDataModified",this.onCharData),this.disconnectSelection()}connectSelection(){this.view.dom.ownerDocument.addEventListener("selectionchange",this.onSelectionChange)}disconnectSelection(){this.view.dom.ownerDocument.removeEventListener("selectionchange",this.onSelectionChange)}suppressSelectionUpdates(){this.suppressingSelectionUpdates=!0,setTimeout(()=>this.suppressingSelectionUpdates=!1,50)}onSelectionChange(){if(DN(this.view)){if(this.suppressingSelectionUpdates)return Ui(this.view);if(qn&&Lo<=11&&!this.view.state.selection.empty){let t=this.view.domSelectionRange();if(t.focusNode&&Fa(t.focusNode,t.focusOffset,t.anchorNode,t.anchorOffset))return this.flushSoon()}this.flush()}}setCurSelection(){this.currentSelection.set(this.view.domSelectionRange())}ignoreSelectionChange(t){if(!t.focusNode)return!0;let n=new Set,r;for(let o=t.focusNode;o;o=Zc(o))n.add(o);for(let o=t.anchorNode;o;o=Zc(o))if(n.has(o)){r=o;break}let i=r&&this.view.docView.nearestDesc(r);if(i&&i.ignoreMutation({type:"selection",target:r.nodeType==3?r.parentNode:r}))return this.setCurSelection(),!0}pendingRecords(){if(this.observer)for(let t of this.observer.takeRecords())this.queue.push(t);return this.queue}flush(){let{view:t}=this;if(!t.docView||this.flushingSoon>-1)return;let n=this.pendingRecords();n.length&&(this.queue=[]);let r=t.domSelectionRange(),i=!this.suppressingSelectionUpdates&&!this.currentSelection.eq(r)&&DN(t)&&!this.ignoreSelectionChange(r),o=-1,a=-1,s=!1,c=[];if(t.editable)for(let p=0;p_.nodeName=="BR");if(p.length==2){let[_,f]=p;_.parentNode&&_.parentNode.parentNode==f.parentNode?f.remove():_.remove()}else{let{focusNode:_}=this.currentSelection;for(let f of p){let g=f.parentNode;g&&g.nodeName=="LI"&&(!_||DH(t,_)!=g)&&f.remove()}}}let d=null;o<0&&i&&t.input.lastFocus>Date.now()-200&&Math.max(t.input.lastTouch,t.input.lastClick.time)-1||i)&&(o>-1&&(t.docView.markDirty(o,a),AH(t)),this.handleDOMChange(o,a,s,c),t.docView&&t.docView.dirty?t.updateState(t.state):this.currentSelection.eq(r)||Ui(t),this.currentSelection.set(r))}registerMutation(t,n){if(n.indexOf(t.target)>-1)return null;let r=this.view.docView.nearestDesc(t.target);if(t.type=="attributes"&&(r==this.view.docView||t.attributeName=="contenteditable"||t.attributeName=="style"&&!t.oldValue&&!t.target.getAttribute("style"))||!r||r.ignoreMutation(t))return null;if(t.type=="childList"){for(let p=0;pi;b--){let S=r.childNodes[b-1],y=S.pmViewDesc;if(S.nodeName=="BR"&&!y){o=b;break}if(!y||y.size)break}let _=e.state.doc,f=e.someProp("domParser")||rT.fromSchema(e.state.schema),g=_.resolve(a),E=null,T=f.parse(r,{topNode:g.parent,topMatch:g.parent.contentMatchAt(g.index()),topOpen:!0,from:i,to:o,preserveWhitespace:g.parent.type.whitespace=="pre"?"full":!0,findPositions:d,ruleFromNode:xH,context:g});if(d&&d[0].pos!=null){let b=d[0].pos,S=d[1]&&d[1].pos;S==null&&(S=b),E={anchor:b+a,head:S+a}}return{doc:T,sel:E,from:a,to:s}}function xH(e){let t=e.pmViewDesc;if(t)return t.parseRule();if(e.nodeName=="BR"&&e.parentNode){if(vn&&/^(ul|ol)$/i.test(e.parentNode.nodeName)){let n=document.createElement("div");return n.appendChild(document.createElement("li")),{skip:n}}else if(e.parentNode.lastChild==e||vn&&/^(tr|table)$/i.test(e.parentNode.nodeName))return{ignore:!0}}else if(e.nodeName=="IMG"&&e.getAttribute("mark-placeholder"))return{ignore:!0};return null}const MH=/^(a|abbr|acronym|b|bd[io]|big|br|button|cite|code|data(list)?|del|dfn|em|i|ins|kbd|label|map|mark|meter|output|q|ruby|s|samp|small|span|strong|su[bp]|time|u|tt|var)$/i;function LH(e,t,n,r,i){let o=e.input.compositionPendingChanges||(e.composing?e.input.compositionID:0);if(e.input.compositionPendingChanges=0,t<0){let W=e.input.lastSelectionTime>Date.now()-50?e.input.lastSelectionOrigin:null,H=lT(e,W);if(H&&!e.state.selection.eq(H)){if(Rn&&$r&&e.input.lastKeyCode===13&&Date.now()-100_e(e,pa(13,"Enter"))))return;let le=e.state.tr.setSelection(H);W=="pointer"?le.setMeta("pointer",!0):W=="key"&&le.scrollIntoView(),o&&le.setMeta("composition",o),e.dispatch(le)}return}let a=e.state.doc.resolve(t),s=a.sharedDepth(n);t=a.before(s+1),n=e.state.doc.resolve(n).after(s+1);let c=e.state.selection,d=wH(e,t,n),p=e.state.doc,_=p.slice(d.from,d.to),f,g;e.input.lastKeyCode===8&&Date.now()-100Date.now()-225||$r)&&i.some(W=>W.nodeType==1&&!MH.test(W.nodeName))&&(!E||E.endA>=E.endB)&&e.someProp("handleKeyDown",W=>W(e,pa(13,"Enter")))){e.input.lastIOSEnter=0;return}if(!E)if(r&&c instanceof Ie&&!c.empty&&c.$head.sameParent(c.$anchor)&&!e.composing&&!(d.sel&&d.sel.anchor!=d.sel.head))E={start:c.from,endA:c.to,endB:c.to};else{if(d.sel){let W=HN(e,e.state.doc,d.sel);if(W&&!W.eq(e.state.selection)){let H=e.state.tr.setSelection(W);o&&H.setMeta("composition",o),e.dispatch(H)}}return}e.input.domChangeCount++,e.state.selection.frome.state.selection.from&&E.start<=e.state.selection.from+2&&e.state.selection.from>=d.from?E.start=e.state.selection.from:E.endA=e.state.selection.to-2&&e.state.selection.to<=d.to&&(E.endB+=e.state.selection.to-E.endA,E.endA=e.state.selection.to)),qn&&Lo<=11&&E.endB==E.start+1&&E.endA==E.start&&E.start>d.from&&d.doc.textBetween(E.start-d.from-1,E.start-d.from+1)==" "&&(E.start--,E.endA--,E.endB--);let T=d.doc.resolveNoCache(E.start-d.from),b=d.doc.resolveNoCache(E.endB-d.from),S=p.resolve(E.start),y=T.sameParent(b)&&T.parent.inlineContent&&S.end()>=E.endA,R;if((rl&&e.input.lastIOSEnter>Date.now()-225&&(!y||i.some(W=>W.nodeName=="DIV"||W.nodeName=="P"))||!y&&T.posW(e,pa(13,"Enter")))){e.input.lastIOSEnter=0;return}if(e.state.selection.anchor>E.start&&PH(p,E.start,E.endA,T,b)&&e.someProp("handleKeyDown",W=>W(e,pa(8,"Backspace")))){$r&&Rn&&e.domObserver.suppressSelectionUpdates();return}Rn&&$r&&E.endB==E.start&&(e.input.lastAndroidDelete=Date.now()),$r&&!y&&T.start()!=b.start()&&b.parentOffset==0&&T.depth==b.depth&&d.sel&&d.sel.anchor==d.sel.head&&d.sel.head==E.endA&&(E.endB-=2,b=d.doc.resolveNoCache(E.endB-d.from),setTimeout(()=>{e.someProp("handleKeyDown",function(W){return W(e,pa(13,"Enter"))})},20));let A=E.start,D=E.endA,M,k,z;if(y){if(T.pos==b.pos)qn&&Lo<=11&&T.parentOffset==0&&(e.domObserver.suppressSelectionUpdates(),setTimeout(()=>Ui(e),20)),M=e.state.tr.delete(A,D),k=p.resolve(E.start).marksAcross(p.resolve(E.endA));else if(E.endA==E.endB&&(z=kH(T.parent.content.cut(T.parentOffset,b.parentOffset),S.parent.content.cut(S.parentOffset,E.endA-S.start()))))M=e.state.tr,z.type=="add"?M.addMark(A,D,z.mark):M.removeMark(A,D,z.mark);else if(T.parent.child(T.index()).isText&&T.index()==b.index()-(b.textOffset?0:1)){let W=T.parent.textBetween(T.parentOffset,b.parentOffset);if(e.someProp("handleTextInput",H=>H(e,A,D,W)))return;M=e.state.tr.insertText(W,A,D)}}if(M||(M=e.state.tr.replace(A,D,d.doc.slice(E.start-d.from,E.endB-d.from))),d.sel){let W=HN(e,M.doc,d.sel);W&&!(Rn&&$r&&e.composing&&W.empty&&(E.start!=E.endB||e.input.lastAndroidDeletet.content.size?null:cT(e,t.resolve(n.anchor),t.resolve(n.head))}function kH(e,t){let n=e.firstChild.marks,r=t.firstChild.marks,i=n,o=r,a,s,c;for(let p=0;pp.mark(s.addToSet(p.marks));else if(i.length==0&&o.length==1)s=o[0],a="remove",c=p=>p.mark(s.removeFromSet(p.marks));else return null;let d=[];for(let p=0;pn||Kg(a,!0,!1)0&&(t||e.indexAfter(r)==e.node(r).childCount);)r--,i++,t=!1;if(n){let o=e.node(r).maybeChild(e.indexAfter(r));for(;o&&!o.isLeaf;)o=o.firstChild,i++}return i}function UH(e,t,n,r,i){let o=e.findDiffStart(t,n);if(o==null)return null;let{a,b:s}=e.findDiffEnd(t,n+e.size,n+t.size);if(i=="end"){let c=Math.max(0,o-Math.min(a,s));r-=a+c-o}if(a=a?o-r:0;o-=c,o&&o