@usememos/editor 26.10.29 → 26.10.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +14 -14
- package/dist/index.js.map +3 -3
- package/dist/types/editor.d.ts +8 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
var Ya=Object.defineProperty;var Ja=(o,e)=>{for(var t in e)Ya(o,t,{get:e[t],enumerable:!0})};var wi={};Ja(wi,{activeLink:()=>Qe,addColumn:()=>ti,addRow:()=>Jt,appendParagraph:()=>oi,applyAutolinkInputRule:()=>ki,applyBlockInputRules:()=>fi,applyInlineInputRules:()=>mi,applyTokenInputRules:()=>gi,balanceSections:()=>li,clampListIndents:()=>yi,cloneBlock:()=>dt,closeSections:()=>rn,convertibleBlocks:()=>zn,deleteBackward:()=>nn,deleteBlock:()=>Bt,deleteBlocks:()=>jn,deleteForward:()=>_n,deleteRange:()=>ye,deleteSelection:()=>J,dropBrokenTokens:()=>bi,duplicateBlock:()=>di,duplicateBlocks:()=>ui,exitBlock:()=>on,indentCode:()=>ln,indentListItems:()=>Wn,insertBlock:()=>Rt,insertBlocks:()=>Xn,insertFragment:()=>cn,insertLineBreak:()=>ii,insertText:()=>ue,insertToken:()=>ni,isMarkActive:()=>Vn,moveCell:()=>hi,nextGrapheme:()=>tn,previousGrapheme:()=>en,quoteRun:()=>Nt,replaceBlocks:()=>Gn,sectionEnd:()=>At,sectionTitle:()=>si,selectAll:()=>xi,selectImage:()=>je,selectedBlocks:()=>vi,selectedImage:()=>De,selectedImageBlock:()=>ke,setBlockType:()=>Ge,setCallout:()=>sn,setCodeLanguage:()=>ci,setLink:()=>ut,spanBlocks:()=>an,splitBlock:()=>Kn,splitCollapsedTitle:()=>ri,tableAction:()=>Zn,toggleBlockType:()=>ht,toggleMark:()=>We,toggleTask:()=>Qn,updateImage:()=>Un});var et=["bold","italic","strike","code","html"],Ke="\uFFFC",tt=5,Ve=["note","tip","important","warning","caution"],Wr=0,ec=Math.random().toString(36).slice(2,7);function ee(){return Wr+=1,`${ec}${Wr.toString(36)}`}function O(o=[],e=ee()){return{id:e,type:"paragraph",content:G(o)}}function nt(o,e=[],t=ee()){return{id:t,type:"heading",level:o,content:G(e)}}function Ue(o,e=[],t={}){let n={id:t.id??ee(),type:"listItem",listType:o,indent:Math.max(0,Math.min(5,t.indent??0)),checked:o==="task"?t.checked??!1:!1,content:G(e)},i=o==="ordered"&&t.start!==void 0&&t.start!==1?{...n,start:t.start}:n;return t.quote?U(i,t.quote):i}function Ne(o=[],e=ee(),t){let n={id:e,type:"quote",content:G(o)};return t?{...n,callout:t}:n}function Ee(o="",e="",t=ee()){return{id:t,type:"code",language:o,content:ge(e)}}function ot(o,e="",t=ee()){return{id:t,type:"raw",syntax:o,content:ge(e)}}function Dn(o="",e=ee()){return ot("html",o,e)}function zt(o,e,t=ee()){return{id:t,type:"table",align:o,rows:e.map(n=>o.map((i,r)=>G(n[r]??[])))}}function Qr(o=3,e=3,t=ee()){return zt(Array.from({length:e},()=>null),Array.from({length:o},()=>Array.from({length:e},()=>[])),t)}function it(o=ee()){return{id:o,type:"divider"}}function St(o=[],e={}){return{id:e.id??ee(),type:"details",open:e.open??!1,content:Ct(o)}}function Tt(o=ee()){return{id:o,type:"detailsEnd"}}function ge(o){return o?[{text:o,marks:[]}]:[]}function Wt(o){return o.type!=="paragraph"&&o.type!=="quote"&&o.type!=="details"&&o.type!=="detailsEnd"}function M(o){return o&&(o.type==="quote"||Wt(o)&&o.quote)?o.callout??"quote":null}function
|
|
1
|
+
var Ya=Object.defineProperty;var Ja=(o,e)=>{for(var t in e)Ya(o,t,{get:e[t],enumerable:!0})};var wi={};Ja(wi,{activeLink:()=>Qe,addColumn:()=>ti,addRow:()=>Jt,appendParagraph:()=>oi,applyAutolinkInputRule:()=>ki,applyBlockInputRules:()=>fi,applyInlineInputRules:()=>mi,applyTokenInputRules:()=>gi,balanceSections:()=>li,clampListIndents:()=>yi,cloneBlock:()=>dt,closeSections:()=>rn,convertibleBlocks:()=>zn,deleteBackward:()=>nn,deleteBlock:()=>Bt,deleteBlocks:()=>jn,deleteForward:()=>_n,deleteRange:()=>ye,deleteSelection:()=>J,dropBrokenTokens:()=>bi,duplicateBlock:()=>di,duplicateBlocks:()=>ui,exitBlock:()=>on,indentCode:()=>ln,indentListItems:()=>Wn,insertBlock:()=>Rt,insertBlocks:()=>Xn,insertFragment:()=>cn,insertLineBreak:()=>ii,insertText:()=>ue,insertToken:()=>ni,isMarkActive:()=>Vn,moveCell:()=>hi,nextGrapheme:()=>tn,previousGrapheme:()=>en,quoteRun:()=>Nt,replaceBlocks:()=>Gn,sectionEnd:()=>At,sectionTitle:()=>si,selectAll:()=>xi,selectImage:()=>je,selectedBlocks:()=>vi,selectedImage:()=>De,selectedImageBlock:()=>ke,setBlockType:()=>Ge,setCallout:()=>sn,setCodeLanguage:()=>ci,setLink:()=>ut,spanBlocks:()=>an,splitBlock:()=>Kn,splitCollapsedTitle:()=>ri,tableAction:()=>Zn,toggleBlockType:()=>ht,toggleMark:()=>We,toggleTask:()=>Qn,updateImage:()=>Un});var et=["bold","italic","strike","code","html"],Ke="\uFFFC",tt=5,Ve=["note","tip","important","warning","caution"],Wr=0,ec=Math.random().toString(36).slice(2,7);function ee(){return Wr+=1,`${ec}${Wr.toString(36)}`}function O(o=[],e=ee()){return{id:e,type:"paragraph",content:G(o)}}function nt(o,e=[],t=ee()){return{id:t,type:"heading",level:o,content:G(e)}}function Ue(o,e=[],t={}){let n={id:t.id??ee(),type:"listItem",listType:o,indent:Math.max(0,Math.min(5,t.indent??0)),checked:o==="task"?t.checked??!1:!1,content:G(e)},i=o==="ordered"&&t.start!==void 0&&t.start!==1?{...n,start:t.start}:n;return t.quote?U(i,t.quote):i}function Ne(o=[],e=ee(),t){let n={id:e,type:"quote",content:G(o)};return t?{...n,callout:t}:n}function Ee(o="",e="",t=ee()){return{id:t,type:"code",language:o,content:ge(e)}}function ot(o,e="",t=ee()){return{id:t,type:"raw",syntax:o,content:ge(e)}}function Dn(o="",e=ee()){return ot("html",o,e)}function zt(o,e,t=ee()){return{id:t,type:"table",align:o,rows:e.map(n=>o.map((i,r)=>G(n[r]??[])))}}function Qr(o=3,e=3,t=ee()){return zt(Array.from({length:e},()=>null),Array.from({length:o},()=>Array.from({length:e},()=>[])),t)}function it(o=ee()){return{id:o,type:"divider"}}function St(o=[],e={}){return{id:e.id??ee(),type:"details",open:e.open??!1,content:Ct(o)}}function Tt(o=ee()){return{id:o,type:"detailsEnd"}}function ge(o){return o?[{text:o,marks:[]}]:[]}function Wt(o){return o.type!=="paragraph"&&o.type!=="quote"&&o.type!=="details"&&o.type!=="detailsEnd"}function M(o){return o&&(o.type==="quote"||Wt(o)&&o.quote)?o.callout??"quote":null}function oe(o,e){let t=M(o);return t!==null&&t===M(e)&&!Et(e)}function Et(o){return M(o)!==null&&o.quoteStart===!0}function Qt(o){return M(o)===null||Et(o)?o:{...o,quoteStart:!0}}function On(o){if(!Et(o))return o;let{quoteStart:e,...t}=o;return t}function Re(o,e){return Et(o)?Qt(e):e}function U(o,e){if(M(o)===e)return o;if(o.type==="paragraph")return e?Ne(o.content,o.id,e==="quote"?void 0:e):o;if(o.type==="quote"){if(!e)return O(o.content,o.id);let{callout:l,...a}=o;return e==="quote"?a:{...a,callout:e}}if(!Wt(o))return o;let{quote:t,callout:n,quoteStart:i,...r}=o;if(!e)return r;let s=e==="quote"?{...r,quote:!0}:{...r,quote:!0,callout:e};return i?{...s,quoteStart:i}:s}function Mt(o){return U(o,null)}function q(o){return o.type!=="table"&&o.type!=="divider"&&o.type!=="detailsEnd"}function Z(o){return o.type==="divider"||o.type==="detailsEnd"}function Q(o){return o.type==="code"||o.type==="raw"||o.type==="details"}function Y(o){if(o.type!=="paragraph")return null;let e=null,t=0;for(let n of o.content){if(n.image){if(e!==null||n.link||n.marks.length>0)return null;e=t}else if(n.token||n.link||!/^[ \t]*$/.test(n.text))return null;t+=n.text.length}return e}function jr(o){return Y(o)!==null}function F(o,e){return o.type==="table"?e?o.rows[e.row]?.[e.col]??null:null:Z(o)?null:o.content}function ze(o,e,t){if(o.type==="table"){if(!e)return o;let n=o.rows.map((i,r)=>r===e.row?i.map((s,l)=>l===e.col?G(t):s):i);return{...o,rows:n}}return Z(o)?o:z(o,t)}function z(o,e){let t=Q(o)?Ct(e):G(e);return{...o,content:t}}function Ct(o){return ge(o.map(e=>e.image?e.image.alt:e.text).join(""))}function rt(o){let e=new Set(o);return et.filter(t=>e.has(t))}function jt(o,e){return o.length===e.length&&o.every((t,n)=>t===e[n])}function tc(o,e){return!o.image&&!e.image&&ae(o,e)&&o.token===e.token&&jt(o.marks,e.marks)}function ae(o,e){return o.link===e?.link&&o.linkTitle===e?.linkTitle}function I(o){return o.map(e=>e.text).join("")}function B(o){let e=0;for(let t of o)e+=t.text.length;return e}function Mf(o){let e=[[]];for(let t of o){let n=t.text.split(`
|
|
2
2
|
`);for(let i=0;i<n.length;i++){i>0&&e.push([]);let r=n[i];r&&e[e.length-1].push(r===t.text?t:{...t,text:r})}}return e}function G(o){let e=[];for(let t of o){if(t.text.length===0)continue;let n=e[e.length-1];n&&tc(n,t)?e[e.length-1]={...n,text:n.text+t.text}:e.push(t)}return e}function C(o,e,t=1/0){let n=[],i=0;for(let r of o){let s=i+r.text.length,l=Math.max(e,i),a=Math.min(t,s);l<a&&n.push(l===i&&a===s?r:{...r,text:r.text.slice(l-i,a-i)}),i=s}return n}function ce(...o){return G(o.flat())}function wt(o,e){let t=0;for(let n of o){let i=t+n.text.length;if(e>=t&&e<i)return n;t=i}}function nc(o,e){let t=e>0?wt(o,e-1):wt(o,0);return t&&!t.image?t.marks:[]}function Gt(o,e){let t=nc(o,e),n=e>0?wt(o,e-1):void 0,i=wt(o,e),r=n?.link&&ae(n,i)?n:void 0,s=n?.token&&n.token===i?.token?n.token:void 0,l=r?{marks:t,...Qo(r)}:{marks:t};return s?{...l,token:s}:l}function Qo(o){return o.link?o.linkTitle===void 0?{link:o.link}:{link:o.link,linkTitle:o.linkTitle}:{}}function Gr(o,e,t,n){let i={text:t,marks:n.marks,...Qo(n)};return n.token&&(i={...i,token:n.token}),ce(C(o,0,e),[i],C(o,e))}function st(o,e,t){return ce(C(o,0,e),C(o,t))}function jo(o,e,t,n){let i=C(o,e,t).filter(r=>!r.image);return i.length>0&&i.every(r=>r.marks.includes(n))}function Zt(o,e,t,n,i){let r=C(o,e,t).map(s=>s.image?s:{...s,marks:i?rt([...s.marks,n]):s.marks.filter(l=>l!==n)});return ce(C(o,0,e),r,C(o,t))}function Zr(o,e,t,n){let i=C(o,e,t).map(r=>({...r,token:n}));return ce(C(o,0,e),i,C(o,t))}function Go(o,e,t,n){let i=C(o,e,t),r=i.find(l=>l.link!==void 0&&!l.image)?.linkTitle,s=i.map(l=>{if(l.image)return l;let{link:a,linkTitle:d,...u}=l;return n?{...u,...Qo({link:n,linkTitle:r})}:u});return ce(C(o,0,e),s,C(o,t))}function Xr(o,e){let t=[],n=0;for(let i of o){if(n>e)break;let r=n+i.text.length;i.token&&e<=r&&t.push({token:i.token,from:n}),n=r}return t}function Xt(o,e){let t=wt(o,e)??(e>0?wt(o,e-1):void 0);if(!t?.link)return null;let n=t.link,i=0,r=[];for(let l of o){let a=i+l.text.length;if(ae(l,t)){let d=r[r.length-1];d&&d.to===i?d.to=a:r.push({from:i,to:a})}i=a}let s=r.find(l=>e>=l.from&&e<=l.to);return s?{href:n,from:s.from,to:s.to}:null}function L(o,e){return o.blocks.findIndex(t=>t.id===e)}function N(o,e){let t=o.blocks.find(n=>n.id===e);if(!t)throw new Error(`Unknown block ${e}`);return t}function de(o,e){return o?.row===e?.row&&o?.col===e?.col}function be(o,e){return o.blockId===e.blockId&&o.offset===e.offset&&de(o.cell,e.cell)}function oc(o,e,t){return e.blockId!==t.blockId?L(o,e.blockId)-L(o,t.blockId):e.cell&&t.cell&&!de(e.cell,t.cell)?e.cell.row-t.cell.row||e.cell.col-t.cell.col:e.offset-t.offset}function R(o){return be(o.anchor,o.focus)}function Me(o,e){return oc(o,e.anchor,e.focus)<=0?{from:e.anchor,to:e.focus}:{from:e.focus,to:e.anchor}}function $(o,e,t){let n=t?{blockId:o,offset:e,cell:t}:{blockId:o,offset:e};return{anchor:n,focus:n}}function V(o){return o.type==="table"?{blockId:o.id,offset:0,cell:{row:0,col:0}}:{blockId:o.id,offset:0}}function P(o){if(o.type==="table"){let e=o.rows.length-1,t=o.align.length-1;return{blockId:o.id,offset:B(o.rows[e][t]),cell:{row:e,col:t}}}return Z(o)?{blockId:o.id,offset:0}:{blockId:o.id,offset:B(o.content)}}function te(o,e){let t=n=>{let i=o.blocks.find(s=>s.id===n.blockId);if(!i)return V(o.blocks[0]);let r=F(i,n.cell);return{...n,offset:Math.min(n.offset,r?B(r):0)}};return{anchor:t(e.anchor),focus:t(e.focus)}}function Hn(o){let e=o.blocks.length>0?o.blocks:[O()],t=e.find(q)??e[0],n=V(t);return{doc:{blocks:e},selection:{anchor:n,focus:n},storedMarks:null}}var Zo=/(?:https?|ftp):\/\//iy,Xo=/[A-Za-z0-9]/,ic=`?!.,:*_~'"`;function It(o,e){return o===void 0?!0:e?/[ \t\n\r*_~(]/.test(o):!/[A-Za-z]/.test(o)}function rc(o,e,t){let n=0,i=0,r=0,s=e+1;for(;s<o.length-1;s++){o[s]==="\\"&&s<o.length-2&&s++;let l=o[s];if(l==="_")r++;else if(l===".")i=r,r=0,n++;else if(!Xo.test(l)&&l!=="-")break}return(i>0||r>0)&&n<=10||t&&n===0?-1:s}function sc(o,e,t){let n=0,i=0;for(let r=e;r<t;r++)o[r]==="("?n++:o[r]===")"&&i++;for(;t>e;){let r=o[t-1];if(ic.includes(r))t--;else if(r===")"){if(i<=n)return t;i--,t--}else if(r===";"){let s=t-2;for(;s>e&&/[A-Za-z]/.test(o[s]);)s--;t=s<t-2&&o[s]==="&"?s:t-1}else return t}return t}function lt(o,e){let t,n=o.slice(e,e+4).toLowerCase()==="www.";if(n)t=e;else if(Zo.lastIndex=e,!Zo.test(o)||(t=Zo.lastIndex,!/[\p{L}\p{N}]/u.test(o[t]??"")))return null;let i=rc(o,t,n);if(i<0)return null;let r=i;for(;r<o.length&&!/[ \t\n\r<]/.test(o[r]);)r++;if(r=sc(o,e,r),r<=t)return null;let s=o.slice(e,r);return{end:r,href:n?"http://"+s:s}}function at(o){let e=[],t=0;for(let n=o.indexOf("@");n!==-1;n=o.indexOf("@",n+1)){let i=n;for(;i>t&&/[A-Za-z0-9.+\-_]/.test(o[i-1]);)i--;if(i===n)continue;let r=n,s=0,l=0;for(;r<o.length;r++){let a=o[r];if(!Xo.test(a)){if(a==="@"){if(++s>1)break}else if(a==="."&&Xo.test(o[r+1]??""))l++;else if(a!=="-"&&a!=="_")break}}r-n<2||s!==1||l===0||!/[A-Za-z]/.test(o[r-1])||(e.push({from:i,to:r}),t=r,n=r-1)}return e}function Yr(o){let e=lt(o,0);if(e)return e.end===o.length?e.href:null;let t=at(o);return t.length===1&&t[0].from===0&&t[0].to===o.length?"mailto:"+o:null}function lc(o){let e=[],t=0;for(let n of o){let i=t+n.text.length;if(ct(n)){let r=e[e.length-1];r&&r.to===t?r.to=i:e.push({from:t,to:i})}t=i}return e}function ct(o){return!!o.link||!!o.image||!!o.token||o.marks.includes("code")||o.marks.includes("html")}function Pn(o,e,t){return{block:o,cell:t,content:e,text:I(e),excluded:lc(e)}}function es(o,e,t){let n=0,i=t.length;for(;n<i;){let s=n+i>>1;t[s].to<=o?n=s+1:i=s}let r=t[n];return r!==void 0&&e>r.from}function ac(o,e){return o.from===e.from&&o.to===e.to&&o.class===e.class&&cc(o.attributes,e.attributes)}function $n(o,e){if(o.length!==e.length)return!1;for(let t=0;t<o.length;t++)if(!ac(o[t],e[t]))return!1;return!0}function cc(o,e){if(o===e)return!0;if(!o||!e)return!1;let t=Object.keys(o);return t.length===Object.keys(e).length&&t.every(n=>o[n]===e[n])}function Yo(o,e,t){let n=e.filter(c=>c.to>c.from);if(n.length===0){let c=0;return o.map(h=>{let f={span:h,decorations:[],atomic:!!t?.(h,c)};return c+=h.text.length,f})}let i=[...new Set(n.flatMap(c=>[c.from,c.to]))].sort((c,h)=>c-h),r=n.map((c,h)=>({d:c,index:h})).sort((c,h)=>c.d.from-h.d.from),s=0,l=[],a=(c,h)=>{for(;s<r.length&&r[s].d.from<=c;)l.push(r[s++]);for(let f=l.length-1;f>=0;f--)l[f].d.to<=c&&l.splice(f,1);return l.filter(f=>f.d.to>=h).sort((f,p)=>f.index-p.index).map(f=>f.d)},d=[],u=0;for(let c of o){let h=u+c.text.length,f=!!t?.(c,u),p=c.image||f?[]:i.slice(Jr(i,u),Jr(i,h-1)),m=u;for(let y of[...p,h]){let g=m===u&&y===h?c:{...c,text:c.text.slice(m-u,y-u)};d.push({span:g,decorations:a(m,y),atomic:f}),m=y}u=h}return d}function Jr(o,e){let t=0,n=o.length;for(;t<n;){let i=t+n>>1;o[i]<=e?t=i+1:n=i}return t}function D(o,e,t,...n){let i=o.doc.blocks.slice();return i.splice(e,t,...n),i}function H(o,e,t=o.selection){return{doc:{blocks:e},selection:t,storedMarks:null}}function j(o,e,t){let n=L(o.doc,e.id);return H(o,D(o,n,1,e),t)}function dt(o){return{...o,id:ee()}}function Ce(o,e){return F(o,e)??[]}function Fn(o){return o.type==="listItem"?Ue(o.listType,[],{indent:o.indent,quote:M(o)}):U(O(),M(o))}function ye(o,e,t){let n=L(o.doc,e.blockId),i=L(o.doc,t.blockId),r=o.doc.blocks[n],s=o.doc.blocks[i];if(n===i){if(Z(r))return o;if(r.type==="table")return dc(o,r,e,t);let c=st(r.content,e.offset,t.offset);return j(o,z(r,c),$(e.blockId,e.offset))}let l=q(r)?z(r,C(r.content,0,e.offset)):null,a=q(s)?C(s.content,t.offset):null,d,u;if(l)d=[a?z(l,ce(l.content,a)):l],u=$(l.id,e.offset);else if(a&&q(s))d=[z(s,a)],u=$(s.id,0);else{let c=O();d=[c],u=$(c.id,0)}return H(o,D(o,n,i-n+1,...d),u)}function dc(o,e,t,n){let i=t.cell??{row:0,col:0},r=n.cell??{row:e.rows.length-1,col:e.align.length-1},s=e.align.length,l=i.row*s+i.col,a=r.row*s+r.col,d=e.rows.map((u,c)=>u.map((h,f)=>{let p=c*s+f;if(p<l||p>a)return h;let m=p===l?t.offset:0,y=p===a?n.offset:B(h);return st(h,m,y)}));return j(o,{...e,rows:d},$(e.id,t.offset,i))}function J(o){if(R(o.selection))return o;let{from:e,to:t}=Me(o.doc,o.selection);return ye(o,e,t)}var qn=typeof Intl<"u"&&"Segmenter"in Intl?new Intl.Segmenter:null;function en(o,e){if(!qn)return Math.max(0,e-1);let t=0;for(let{index:n}of qn.segment(o)){if(n>=e)break;t=n}return t}function tn(o,e){if(!qn)return Math.min(o.length,e+1);for(let{index:t,segment:n}of qn.segment(o))if(t+n.length>e)return t+n.length;return o.length}function nn(o){if(!R(o.selection)){let s=ke(o);return s?Lt(o,L(o.doc,s.pos.blockId),-1):J(o)}let e=o.selection.focus,t=L(o.doc,e.blockId),n=o.doc.blocks[t];if(Z(n))return Lt(o,t,-1);if(e.offset>0){let s=I(Ce(n,e.cell)),l=en(s,e.offset);return ye(o,{...e,offset:l},e)}if(n.type==="table"){let s=e.cell,l=s.col>0?{row:s.row,col:s.col-1}:s.row>0?{row:s.row-1,col:n.align.length-1}:null;return l?{...o,selection:$(n.id,B(Ce(n,l)),l)}:o}if(n.type==="listItem"&&n.indent>0)return j(o,{...n,indent:n.indent-1});if(n.type!=="paragraph"){let s=n.type==="quote"?null:M(n);return j(o,Re(n,U(O(n.content,n.id),s)))}let i=o.doc.blocks[t-1];if(!i)return o;if(i.type==="detailsEnd"){let s=os(o.doc.blocks,t);if(!s)return o;let l={anchor:s,focus:s};return B(n.content)===0?H(o,D(o,t,1),l):{...o,selection:l,storedMarks:null}}if(i.type==="divider")return H(o,D(o,t-1,1));let r=Y(i);if(r!==null){let s=rs(i.id,r);return B(n.content)===0?H(o,D(o,t,1),s):{...o,selection:s,storedMarks:null}}if(i.type==="table"){let s=P(i),l={anchor:s,focus:s};return B(n.content)===0?H(o,D(o,t,1),l):{...o,selection:l}}return ye(o,P(i),{blockId:n.id,offset:0})}function _n(o){if(!R(o.selection)){let l=ke(o);return l?Lt(o,L(o.doc,l.pos.blockId),1):J(o)}let e=o.selection.focus,t=L(o.doc,e.blockId),n=o.doc.blocks[t];if(Z(n))return Lt(o,t,1);let i=I(Ce(n,e.cell));if(e.offset<i.length)return ye(o,e,{...e,offset:tn(i,e.offset)});if(n.type==="table")return o;let r=o.doc.blocks[t+1];if(!r||r.type==="table"||r.type==="detailsEnd")return o;if(r.type==="details")return B(Ce(n))===0?Lt(o,t,1):o;if(r.type==="divider")return H(o,D(o,t+1,1));let s=Y(r);if(s!==null){let l=rs(r.id,s);return n.type==="paragraph"&&B(n.content)===0?H(o,D(o,t,1),l):{...o,selection:l,storedMarks:null}}return ye(o,e,{blockId:r.id,offset:0})}function os(o,e){for(let t=e-1;t>=0;t--)if(!Z(o[t]))return P(o[t]);return null}function Lt(o,e,t){let n=D(o,e,1);if(n.length===0){let a=O();return H(o,[a],$(a.id,0))}let i=n[e-1],r=n[e],s=t===-1?i??r:r??i,l=s===i?P(s):V(s);return H(o,n,{anchor:l,focus:l})}function ue(o,e){let t=o.storedMarks,n=J(o),i=n.selection.focus,r=N(n.doc,i.blockId),s=F(r,i.cell);if(s===null)return o;let l=e;(r.type==="table"||r.type==="details")&&(l=l.replace(/\r?\n/g," "));let a=Q(r)?{marks:[]}:t?{marks:t}:Gt(s,i.offset),d=Gr(s,i.offset,l,a);return j(n,ze(r,i.cell,d),$(r.id,i.offset+l.length,i.cell))}function ni(o,e){let t=o.selection.focus,n=F(N(o.doc,t.blockId),t.cell);if(n===null)return o;let i=I(n),r=t.offset>0&&!/\s/.test(i[t.offset-1])?" ":"",s=i[t.offset]===" "||i[t.offset]===" ",l=ue({...o,selection:{anchor:t,focus:t}},r+e+(s?"":" "));if(!s)return l;let a={...l.selection.focus,offset:l.selection.focus.offset+1};return{...l,selection:{anchor:a,focus:a}}}function on(o,e){let t=ke(o);if(!R(o.selection)&&!t)return o;let n=t?.pos??o.selection.focus,i=L(o.doc,n.blockId),r=o.doc.blocks[i];if(r.type!=="code"&&r.type!=="table"&&r.type!=="divider"&&!t||i!==(e===1?o.doc.blocks.length-1:0))return o;if(r.type==="table"){if((n.cell?.row??-1)!==(e===1?r.rows.length-1:0))return o}else if(r.type==="code"){let l=I(r.content);if((e===1?l.slice(n.offset):l.slice(0,n.offset)).includes(`
|
|
3
3
|
`))return o}let s=O();return H(o,D(o,e===1?i+1:0,0,s),$(s.id,0))}function oi(o){let e=O();return H(o,[...o.doc.blocks,e],$(e.id,0))}function ii(o){let e=N(o.doc,o.selection.focus.blockId);return e.type==="table"||Z(e)||e.type==="details"?o:ue(o,`
|
|
4
4
|
`)}function Kn(o){let e=J(o),t=e.selection.focus,n=L(e.doc,t.blockId),i=e.doc.blocks[n];if(Z(i)){let c=O();return H(e,D(e,n+1,0,c),$(c.id,0))}if(i.type==="table")return uc(e,i,t);if(i.type==="details")return is(e,i,n,t.offset);let r=I(i.content),s=r.length;if(Q(i)){if(t.offset===s&&r.endsWith(`
|
|
5
5
|
|
|
6
6
|
`)){let c=z(i,C(i.content,0,s-2)),h=U(O(),M(i));return H(e,D(e,n,1,c,h),$(h.id,0))}return ue(e,`
|
|
7
7
|
`)}let l=(i.type==="paragraph"||i.type==="quote")&&t.offset===s?/^```([^`\s]*)$/.exec(r):null;if(l){let c=Re(i,U(Ee(l[1],"",i.id),M(i)));return j(e,c,$(c.id,0))}if(s===0&&i.type==="listItem"){let c=i.indent>0?{...i,indent:i.indent-1}:i.quote?Re(i,Ne([],i.id,i.callout)):O([],i.id);return j(e,c)}if(s===0&&i.type==="quote")return j(e,O([],i.id));if(s===0&&i.type==="paragraph"&&e.doc.blocks[n+1]?.type==="detailsEnd"){let c=e.doc.blocks.slice();return c.splice(n,1),c.splice(n+1,0,i),H(e,c,e.selection)}if(t.offset===0&&s>0)return Et(i)?H(e,D(e,n,1,Re(i,Fn(i)),On(i)),e.selection):H(e,D(e,n,0,Fn(i)),e.selection);let a=z(i,C(i.content,0,t.offset)),d=C(i.content,t.offset),u;return i.type==="heading"?u=U(d.length>0?nt(i.level,d):O(),M(i)):u=z(Fn(i),d),H(e,D(e,n,1,a,u),$(u.id,0))}function is(o,e,t,n,i=!1){let r=B(e.content);if(n===0&&r>0)return H(o,D(o,t,0,O()),o.selection);let s=o.doc.blocks[t+1];if(!i&&n===r&&s?.type==="paragraph"&&B(s.content)===0)return{...o,selection:$(s.id,0),storedMarks:null};let l=z(e,C(e.content,0,n)),a=O(C(e.content,n)),d=o.doc.blocks.slice();return d[t]=l,d.splice(i?(At(d,t)??t)+1:t+1,0,a),H(o,d,$(a.id,0))}function ri(o){let e=J(o),t=e.selection.focus,n=L(e.doc,t.blockId),i=e.doc.blocks[n];return i.type==="details"?is(e,i,n,t.offset,!0):Kn(o)}function At(o,e){let t=0;for(let n=e;n<o.length;n++){let i=o[n].type;if(i==="details")t++;else if(i==="detailsEnd"&&--t===0)return n}return null}function si(o,e){let t=0;for(let n=e;n>=0;n--){let i=o[n].type;if(i==="detailsEnd")t++;else if(i==="details"&&--t===0)return n}return null}function rn(o){if(!o.some(r=>r.type==="details"||r.type==="detailsEnd"))return o;let e=[],t=new Set;if(o.forEach((r,s)=>{r.type==="details"?e.push(s):r.type==="detailsEnd"&&e.pop()===void 0&&t.add(s)}),t.size===0&&e.length===0)return o;let n=new Set(e),i=[];return o.forEach((r,s)=>{t.has(s)||i.push(n.has(s)&&r.type==="details"?O(r.content,r.id):r)}),i.length>0?i:[O()]}function li(o){if(!o.doc.blocks.some(l=>l.type==="details"||l.type==="detailsEnd"))return o;let e=rn(o.doc.blocks),t=o;if(e!==o.doc.blocks){let l={blocks:e};t={...o,doc:l,selection:te(l,o.selection)}}let n=l=>t.doc.blocks.find(a=>a.id===l.blockId)?.type==="detailsEnd",{anchor:i,focus:r}=t.selection;if(!n(i)&&!n(r))return t;let s=l=>n(l)?os(t.doc.blocks,L(t.doc,l.blockId))??l:l;return{...t,selection:{anchor:s(i),focus:s(r)}}}function uc(o,e,t){let n=t.cell,i=e;n.row===e.rows.length-1&&(i=Jt(e,n.row+1));let r={row:n.row+1,col:n.col};return j(o,i,$(i.id,0,r))}function ai(o,e,t,n){let i=L(o.doc,e.blockId),r=L(o.doc,t.blockId),s=o.doc.blocks.slice(i,r+1).map(l=>{if(Z(l)||Q(l))return l;if(l.type==="table"){let c=l.align.length,h=l.id===e.blockId&&e.cell?e.cell.row*c+e.cell.col:0,f=l.id===t.blockId&&t.cell?t.cell.row*c+t.cell.col:1/0,p=l.rows.map((m,y)=>m.map((g,x)=>{let b=y*c+x;if(b<h||b>f)return g;let w=b===h&&l.id===e.blockId?e.offset:0,k=b===f&&l.id===t.blockId?t.offset:B(g);return w<k?ts(g,n(g,w,k)):g}));return p.every((m,y)=>m.every((g,x)=>g===l.rows[y][x]))?l:{...l,rows:p}}let a=l.id===e.blockId?e.offset:0,d=l.id===t.blockId?t.offset:B(l.content);if(a>=d)return l;let u=ts(l.content,n(l.content,a,d));return u===l.content?l:z(l,u)});return D(o,i,s.length,...s)}function ts(o,e){if(o.length!==e.length)return e;for(let t=0;t<o.length;t++){let n=o[t],i=e[t];if(n.text!==i.text||!ae(n,i)||n.token!==i.token||n.image!==i.image||!jt(n.marks,i.marks))return e}return o}function Vn(o,e){if(R(o.selection)){let r=o.selection.focus,s=N(o.doc,r.blockId);return(o.storedMarks??Gt(Ce(s,r.cell),r.offset).marks).includes(e)}let{from:t,to:n}=Me(o.doc,o.selection),i=[];return ai(o,t,n,(r,s,l)=>(i.push(jo(r,s,l,e)),r)),i.length>0&&i.every(Boolean)}function We(o,e){let{from:t,to:n}=Me(o.doc,o.selection);if(R(o.selection)){let s=N(o.doc,t.blockId);if(Q(s))return o;let l=o.storedMarks??Gt(Ce(s,t.cell),t.offset).marks,a=l.includes(e)?l.filter(d=>d!==e):rt([...l,e]);return{...o,storedMarks:a}}let i=!Vn(o,e),r=ai(o,t,n,(s,l,a)=>Zt(s,l,a,e,i));return{...o,doc:{blocks:r},storedMarks:null}}function Qe(o){let{from:e}=Me(o.doc,o.selection),t=N(o.doc,e.blockId),n=F(t,e.cell);if(!n)return null;let i=Xt(n,e.offset);return i?{...i,pos:e}:null}function ut(o,e){let t,n;if(R(o.selection)){let r=Qe(o);if(!r)return o;t={...r.pos,offset:r.from},n={...r.pos,offset:r.to}}else({from:t,to:n}=Me(o.doc,o.selection));let i=ai(o,t,n,(r,s,l)=>Go(r,s,l,e));return{...o,doc:{blocks:i},storedMarks:null}}function De(o){let{anchor:e,focus:t}=o.selection;if(e.blockId!==t.blockId||!de(e.cell,t.cell)||Math.abs(e.offset-t.offset)!==1)return null;let n=Math.min(e.offset,t.offset),i=F(N(o.doc,e.blockId),e.cell),r=i?C(i,n,n+1)[0]?.image:void 0;return r?{pos:{...e,offset:n},image:r}:null}function rs(o,e){return{anchor:{blockId:o,offset:e},focus:{blockId:o,offset:e+1}}}function ke(o){let e=De(o);return!e||e.pos.cell?null:Y(N(o.doc,e.pos.blockId))===e.pos.offset?e:null}function je(o,e){let t=o.doc.blocks.find(i=>i.id===e.blockId),n=t&&F(t,e.cell);return!n||!C(n,e.offset,e.offset+1)[0]?.image?o:{...o,selection:{anchor:e,focus:{...e,offset:e.offset+1}},storedMarks:null}}function Un(o,e,t){let n=o.doc.blocks.find(b=>b.id===e.blockId),i=n&&F(n,e.cell),r=i?C(i,e.offset,e.offset+1)[0]:void 0;if(!n||!i||!r?.image)return o;let{title:s,align:l,width:a,height:d,...u}=r.image,c=r.image.title;t.title!==void 0&&(c=t.title||void 0);let h=t.align!==void 0?t.align??void 0:r.image.align,f=r.image.width,p=r.image.height;t.width!==void 0&&(f=t.width===null?void 0:Math.max(1,Math.min(99999,Math.round(t.width))),f!==r.image.width&&(p=void 0));let m={...u,...t.alt!==void 0?{alt:t.alt}:{},...c?{title:c}:{},...h?{align:h}:{},...f?{width:f}:{},...f&&p?{height:p}:{}},y=je(o,e).selection;if(m.alt===r.image.alt&&m.title===r.image.title&&m.align===r.image.align&&m.width===r.image.width&&m.height===r.image.height)return y===o.selection?o:{...o,selection:y,storedMarks:null};let x=ce(C(i,0,e.offset),[{...r,text:"\uFFFC",image:m}],C(i,e.offset+1));return j(o,ze(n,e.cell,x),y)}function ss(o,e){return Re(o,hc(o,e))}function hc(o,e){let t=M(o);if(e.type==="quote")return U(o,e.callout??t??"quote");let n=o.content,i;switch(e.type){case"paragraph":if(i=O(n,o.id),o.type==="quote")return i;break;case"heading":i=nt(e.level,n,o.id);break;case"listItem":i=Ue(e.listType,n,{id:o.id,indent:o.type==="listItem"?o.indent:0,checked:o.type==="listItem"?o.checked:!1,start:o.type==="listItem"&&o.listType===e.listType?o.start:void 0});break;case"code":i=Ee(e.language??(o.type==="code"?o.language:""),I(Ct(n)),o.id);break;default:return o}return U(i,t)}function Yt(o){return q(o)&&o.type!=="raw"}function Ge(o,e){return e.type==="table"||e.type==="divider"||e.type==="details"?Rt(o,e):ls(o,e,t=>ss(t,e))}function ls(o,e,t){let{from:n,to:i}=Me(o.doc,o.selection),r=L(o.doc,n.blockId),s=L(o.doc,i.blockId),l=o.doc.blocks.slice(r,s+1);if(e.type==="code"&&l.filter(Yt).length>1)return fc(o,r,l,e.language);let a=l.map(u=>Yt(u)?t(u):u),d={blocks:D(o,r,a.length,...a)};return{doc:d,selection:te(d,o.selection),storedMarks:null}}function fc(o,e,t,n){let i=[],r=[],s=[],l=()=>{let d=s[0];if(!d)return;let u=s.map(h=>I(Ct(h.content))).join(`
|
|
8
|
-
`),c=U(Ee(n??(d.type==="code"?d.language:""),u,d.id),M(d));i.push(c),r.push(c),s=[]};for(let d of t)Yt(d)?s.push(d):(l(),i.push(d));l();let a={anchor:V(r[0]),focus:P(r[r.length-1])};return{doc:{blocks:D(o,e,t.length,...i)},selection:a,storedMarks:null}}function Nt(o,e){let t=o.doc.blocks,n=L(o.doc,e),i=t[n];if(M(i)===null)return null;let r=n,s=n;for(;
|
|
8
|
+
`),c=U(Ee(n??(d.type==="code"?d.language:""),u,d.id),M(d));i.push(c),r.push(c),s=[]};for(let d of t)Yt(d)?s.push(d):(l(),i.push(d));l();let a={anchor:V(r[0]),focus:P(r[r.length-1])};return{doc:{blocks:D(o,e,t.length,...i)},selection:a,storedMarks:null}}function Nt(o,e){let t=o.doc.blocks,n=L(o.doc,e),i=t[n];if(M(i)===null)return null;let r=n,s=n;for(;oe(t[r-1],t[r]);)r--;for(;oe(t[s],t[s+1]);)s++;return{start:r,end:s}}function sn(o,e,t){let n=Nt(o,e);if(!n)return o;let i=o.doc.blocks.slice(n.start,n.end+1);if(i.every(l=>M(l)===(t??"quote")))return o;let r=i.map(l=>U(l,t??"quote"));return{doc:{blocks:D(o,n.start,r.length,...r)},selection:o.selection,storedMarks:o.storedMarks}}function zn(o){let{from:e,to:t}=Me(o.doc,o.selection);return o.doc.blocks.slice(L(o.doc,e.blockId),L(o.doc,t.blockId)+1).filter(Yt)}function pc(o,e){switch(e.type){case"heading":return o.type==="heading"&&o.level===e.level;case"listItem":return o.type==="listItem"&&o.listType===e.listType;case"code":return o.type==="code";case"quote":{let t=M(o);return t!==null&&(!e.callout||t===e.callout)}default:return o.type===e.type}}function ht(o,e){let t=zn(o);if(t.length===0)return o;if(!t.every(r=>pc(r,e)))return Ge(o,e);let i={type:"paragraph"};return ls(o,i,r=>e.type==="quote"?U(r,null):ss(r,i))}function Rt(o,e){let t=o.selection.focus,n=L(o.doc,t.blockId),i=o.doc.blocks[n];if(e.type==="details")return mc(o,n);if(e.type!=="table"&&e.type!=="divider")return Ge(o,e);let r=q(i)&&B(i.content)===0,s=M(i),l=U(e.type==="table"?Qr(e.rows??3,e.cols??3):it(),s),a=[r?Re(i,l):l],d=l;if(l.type==="divider"){let h=o.doc.blocks[n+1];h&&q(h)?d=h:(d=U(O(),s),a.push(d))}let u=V(d),c=r?n:n+1;return H(o,D(o,c,r?1:0,...a),{anchor:u,focus:u})}function mc(o,e){let t=o.doc.blocks[e],n=Yt(t)&&t.type!=="details",i=St(n?t.content:[],n?{id:t.id}:{}),r=O(),s=n?e:e+1,l=D(o,s,n?1:0,i,r,Tt()),a=B(i.content)===0?$(i.id,0):$(r.id,0);return H(o,l,a)}function Wn(o,e){let{from:t,to:n}=Me(o.doc,o.selection),i=L(o.doc,t.blockId),r=L(o.doc,n.blockId),s=!1,l=o.doc.blocks.slice();for(let a=i;a<=r;a++){let d=l[a];if(d.type!=="listItem")continue;let u=as(l[a-1],d),c=Math.max(0,Math.min(u,d.indent+e));c!==d.indent&&(l[a]={...d,indent:c},s=!0)}return s?H(o,l):o}var Jo=" ";function ln(o,e){let{anchor:t,focus:n}=o.selection;if(t.blockId!==n.blockId)return o;let i=N(o.doc,n.blockId);if(i.type!=="code")return o;if(e===1&&R(o.selection))return ue(o,Jo);let r=I(i.content),s=Math.min(t.offset,n.offset),l=Math.max(t.offset,n.offset),a=[];for(let f=r.lastIndexOf(`
|
|
9
9
|
`,s-1)+1;;){let p=/^(?: {1,2}|\t)/.exec(r.slice(f))?.[0].length??0;a.push({at:f,remove:e===1?0:p});let m=r.indexOf(`
|
|
10
10
|
`,f);if(m<0||m>=l)break;f=m+1}if(e===-1&&a.every(f=>f.remove===0))return o;let d="",u=0;for(let f of a)d+=r.slice(u,f.at)+(e===1?Jo:""),u=f.at+f.remove;d+=r.slice(u);let c=f=>{let p=0;for(let m of a){if(m.at>f)break;p+=e===1?Jo.length:-Math.min(m.remove,f-m.at)}return f+p},h=z(i,ge(d));return j(o,h,{anchor:{...t,offset:c(t.offset)},focus:{...n,offset:c(n.offset)}})}function Qn(o,e){let t=N(o.doc,e);if(t.type!=="listItem"||t.listType!=="task")return o;let n={...t,checked:!t.checked};return j(o,n)}function ci(o,e,t){let n=N(o.doc,e);return n.type!=="code"||n.language===t?o:j(o,{...n,language:t})}function di(o,e){let t=L(o.doc,e),n=dt(o.doc.blocks[t]),i=V(n);return H(o,D(o,t+1,0,n),{anchor:i,focus:i})}function Bt(o,e){return Lt(o,L(o.doc,e),-1)}function an(o,e){return{anchor:V(o[e.from]),focus:P(o[e.to])}}function as(o,e){return o?.type==="listItem"&&M(o)===M(e)?Math.min(5,o.indent+1):0}function ei(o,e){for(let t=Math.max(0,e);t<o.length;t++){let n=o[t];if(n.type!=="listItem")return;let i=as(o[t-1],n);if(n.indent<=i)return;o[t]={...n,indent:i}}}function jn(o,e){return Gn(o,e,[])}function ui(o,e){let t=o.doc.blocks.slice(e.from,e.to+1).map(dt),n=D(o,e.to+1,0,...t);ei(n,e.to+1);let i={from:e.to+1,to:e.to+t.length};return H(o,n,an(n,i))}function Gn(o,e,t){let n=D(o,e.from,e.to-e.from+1,...t);if(n.length===0){let r=O();return H(o,[r],$(r.id,0))}ei(n,e.from),ei(n,e.from+t.length);let i;return t.length>0?i=P(n[e.from+t.length-1]):e.from>0?i=P(n[e.from-1]):i=V(n[0]),H(o,n,{anchor:i,focus:i})}function Jt(o,e){let t=o.rows.slice();return t.splice(Math.max(1,e),0,o.align.map(()=>[])),{...o,rows:t}}function ti(o,e){let t=o.align.slice();t.splice(e,0,null);let n=o.rows.map(i=>{let r=i.slice();return r.splice(e,0,[]),r});return{...o,align:t,rows:n}}function Zn(o,e){let t=o.selection.focus,n=N(o.doc,t.blockId);if(n.type!=="table"||!t.cell)return o;let{row:i,col:r}=t.cell,s=n,l=t.cell;switch(e.kind){case"addRowAbove":s=Jt(n,i===0?1:i),l={row:i===0?1:i,col:r};break;case"addRowBelow":s=Jt(n,i+1),l={row:i+1,col:r};break;case"addColumnLeft":s=ti(n,r);break;case"addColumnRight":s=ti(n,r+1),l={row:i,col:r+1};break;case"deleteRow":if(n.rows.length<=1)return Bt(o,n.id);s={...n,rows:n.rows.filter((d,u)=>u!==i)},l={row:Math.min(i,s.rows.length-1),col:r};break;case"deleteColumn":{if(n.align.length<=1)return Bt(o,n.id);s={...n,align:n.align.filter((d,u)=>u!==r),rows:n.rows.map(d=>d.filter((u,c)=>c!==r))},l={row:i,col:Math.min(r,s.align.length-1)};break}case"align":s={...n,align:n.align.map((d,u)=>u===r?e.align:d)};break}let a=Math.min(t.offset,B(Ce(s,l)));return j(o,s,$(s.id,l===t.cell?a:0,l))}function hi(o,e){let t=o.selection.focus,n=N(o.doc,t.blockId);if(n.type!=="table"||!t.cell)return o;let i=n.align.length,r=t.cell.row*i+t.cell.col+e;if(r<0)return o;let s=n;r>=n.rows.length*i&&(s=Jt(n,n.rows.length));let l={row:Math.floor(r/i),col:r%i},a=Ce(s,l),d={anchor:{blockId:n.id,offset:0,cell:l},focus:{blockId:n.id,offset:B(a),cell:l}};return s===n?{...o,selection:d}:H(o,D(o,L(o.doc,n.id),1,s),d)}var gc=[[/^(#{1,6}) $/,o=>({type:"heading",level:o[1].length})],[/^[-*+] $/,()=>({type:"listItem",listType:"bullet"})],[/^\d{1,9}[.)] $/,()=>({type:"listItem",listType:"ordered"})],[/^\[[ xX]?\] $/,()=>({type:"listItem",listType:"task"})],[/^> $/,()=>({type:"quote"})],[new RegExp(`^\\[!(${Ve.join("|")})\\] $`,"i"),o=>({type:"quote",callout:o[1].toLowerCase()})],[/^```([^`\s]*) $/,o=>({type:"code",language:o[1]})],[/^>>> $/,()=>({type:"details"})]];function fi(o,e={}){if(!R(o.selection))return null;let t=o.selection.focus,n=N(o.doc,t.blockId);if(!q(n)||t.cell)return null;let i=I(n.content).slice(0,t.offset);if((n.type==="paragraph"||n.type==="quote")&&i==="---"){let s=L(o.doc,n.id),l=On(z(n,C(n.content,t.offset))),a=Re(n,U(it(),M(n)));return H(o,D(o,s,1,a,l),$(l.id,0))}if(!(n.type==="paragraph"||n.type==="quote"||n.type==="listItem"&&n.listType==="bullet"))return null;for(let[s,l]of gc){let a=s.exec(i);if(!a)continue;let d=l(a);if(d.type==="quote"&&d.callout&&e.callouts===!1||d.type==="details"&&e.details===!1||n.type==="listItem"&&!(d.type==="listItem"&&d.listType==="task"))continue;if(n.type==="quote"){if(d.type==="quote"&&d.callout)return sn(ye(o,{blockId:n.id,offset:0},t),n.id,d.callout);if(d.type==="quote"||d.type==="details")continue}let u=ye(o,{blockId:n.id,offset:0},t),c=Ge(u,d),h=N(c.doc,n.id);if(h.type==="listItem"){let f=/^(\d{1,9})/.exec(i),p=/^\[[xX]\]/.test(i),m=Ue(h.listType,h.content,{id:h.id,indent:h.indent,checked:p,start:h.listType==="ordered"&&f?Number(f[1]):void 0,quote:M(h)});c=j(c,Re(h,m),c.selection)}return c}return null}function pi(o){if(!R(o.selection))return null;let e=o.selection.focus,t=N(o.doc,e.blockId);if(Q(t))return null;let n=F(t,e.cell);return n?{pos:e,block:t,content:n}:null}var bc=[{re:/(?:^|[^*])\*\*([^*\s](?:[^*]*[^*\s])?)\*\*$/,mark:"bold",delim:2},{re:/(?:^|[^_\p{L}\p{N}])__([^_\s](?:[^_]*[^_\s])?)__$/u,mark:"bold",delim:2},{re:/~~([^~\s](?:[^~]*[^~\s])?)~~$/,mark:"strike",delim:2},{re:/(?:^|[^*])\*([^*\s](?:[^*]*[^*\s])?)\*$/,mark:"italic",delim:1},{re:/(?:^|[^_\p{L}\p{N}])_([^_\s](?:[^_]*[^_\s])?)_$/u,mark:"italic",delim:1},{re:/`([^`]+)`$/,mark:"code",delim:1}];function mi(o){let e=pi(o);if(!e)return null;let{pos:t,block:n,content:i}=e,r=I(i).slice(0,t.offset);for(let s of bc){let l=s.re.exec(r);if(!l)continue;let a=l[1],d=t.offset,u=d-s.delim,c=u-a.length,h=c-s.delim;if(ns(r,h)||s.mark!=="code"&&ns(r,u)||jo(i,h,d,"code")||C(i,h,d).some(x=>x.image))continue;let f=Zt(i,c,u,s.mark,!0);f=st(f,u,d),f=st(f,h,c);let p=u-s.delim,m=ze(n,t.cell,f),y=j(o,m,$(n.id,p,t.cell)),g=Gt(f,p).marks.filter(x=>x!==s.mark);return{...y,storedMarks:g}}return null}function ns(o,e){let t=0;for(;e-t-1>=0&&o[e-t-1]==="\\";)t++;return t%2===1}function gi(o,e){let t=e.length>0?pi(o):null;if(!t)return null;let{pos:n,block:i,content:r}=t,s=I(r),l=n.offset,a=Math.max(s.lastIndexOf(`
|
|
11
11
|
`,l-1)+1,l-1e3);for(let d=l-1;d>=a;d--)for(let u of e){if(!u.triggers.includes(s[d])||u.match(s,d)!==l||!C(r,d,l).every(f=>!f.token&&!f.image&&!f.marks.includes("code")&&!f.marks.includes("html")))continue;let h=ze(i,n.cell,Zr(r,d,l,u.name));return j(o,h,o.selection)}return null}function bi(o,e,t){if(t.length===0||o.doc===e.doc)return o;let n=new Set(e.doc.blocks),i=null;return o.doc.blocks.forEach((r,s)=>{if(n.has(r)||r.type==="divider")return;let l=r.type==="table"?r.rows.flatMap((d,u)=>d.map((c,h)=>({row:u,col:h}))):[void 0],a=r;for(let d of l){let u=F(a,d);if(!u?.some(h=>h.token))continue;let c=yc(u,t);c!==u&&(a=ze(a,d,c))}a!==r&&((i??=[...o.doc.blocks])[s]=a)}),i?{...o,doc:{...o.doc,blocks:i}}:o}function yi(o,e){if(o.doc===e.doc)return o;let t=null,n=0;return o.doc.blocks.forEach((i,r)=>{(i.type!=="listItem"||M(i)!==M(o.doc.blocks[r-1]))&&(n=0),i.type==="listItem"&&(i.indent>n&&((t??=[...o.doc.blocks])[r]={...i,indent:n}),n=Math.min(i.indent,n)+1)}),t?{...o,doc:{...o.doc,blocks:t}}:o}function yc(o,e){let t=I(o),n=o,i=0;for(let r of o){let s=i,l=i+=r.text.length;if(!r.token)continue;let a=r.marks.includes("code")?void 0:e.find(h=>h.name===r.token),d=s;for(;a&&d<l;){let h=a.match(t,d);if(h<=d||h>l)break;d=h}if(d===l)continue;let{token:u,...c}=r;n=ce(C(n,0,s),[c],C(n,l))}return n}function ki(o){let e=pi(o);if(!e)return null;let{pos:t,block:n,content:i}=e,r=I(i),s=t.offset-1;if(s<=0||!/\s/.test(r[s]))return null;let l=s;for(;l>0&&!/\s/.test(r[l-1]);)l--;let a=r.slice(l,s),d=null;for(let c of a.matchAll(/www\.|(?:https?|ftp):\/\//gi)){if(!It(c.index===0?void 0:a[c.index-1],c[0].toLowerCase()==="www."))continue;let h=lt(a,c.index);if(h){d={from:l+c.index,to:l+h.end,href:h.href};break}}if(!d){let c=at(a)[0];c&&(d={from:l+c.from,to:l+c.to,href:"mailto:"+a.slice(c.from,c.to)})}if(!d||C(i,d.from,d.to).some(ct))return null;let u=ze(n,t.cell,Go(i,d.from,d.to,d.href));return j(o,u,o.selection)}function vi(o){let{from:e,to:t}=Me(o.doc,o.selection),n=L(o.doc,e.blockId),i=L(o.doc,t.blockId);if(n===i){let r=o.doc.blocks[n];if(q(r))return[O(C(r.content,e.offset,t.offset))];if(r.type==="table"&&e.cell&&de(e.cell,t.cell))return[O(C(Ce(r,e.cell),e.offset,t.offset))]}return o.doc.blocks.slice(n,i+1).map(r=>{if(!q(r))return dt(r);let s=r.id===e.blockId?e.offset:0,l=r.id===t.blockId?t.offset:1/0;return dt(z(r,C(r.content,s,l)))})}function Xn(o,e){if(e.length===0)return J(o);let t=J(o),n=t.selection.focus,i=L(t.doc,n.blockId),r=t.doc.blocks[i];if(!q(r)||Q(r)){let f=e[0];if(r.type==="table"&&e.length===1&&q(f)&&!Q(f)){let m=Ce(r,n.cell),y=f.content.map(b=>({...b,text:b.text.replace(/\n/g," ")})),g=ce(C(m,0,n.offset),y,C(m,n.offset)),x=n.offset+B(y);return j(t,ze(r,n.cell,g),$(r.id,x,n.cell))}let p=e.map(m=>q(m)?I(Ct(m.content)):"").filter(Boolean).join(`
|
|
12
|
-
`);return ue(t,p)}let s=C(r.content,0,n.offset),l=C(r.content,n.offset),a=e[0];if(e.length===1&&q(a)&&!(Q(a)&&B(r.content)>0)){let f=B(r.content)===0?{...a,id:r.id}:r,p=ce(s,a.content,l);return j(t,z(f,p),$(r.id,n.offset+B(a.content)))}let d=[],u=e.slice();s.length>0?q(a)&&!Q(a)?(d.push(z(r,ce(s,a.content))),u=u.slice(1)):d.push(z(r,s)):q(a)&&(d.push({...a,id:r.id}),u=u.slice(1)),d.push(...u);let c=d[d.length-1],h;if(q(c)&&!Q(c))h={blockId:c.id,offset:B(c.content)},l.length>0&&(d[d.length-1]=z(c,ce(c.content,l)));else{let f=z(Fn(r),l);d.push(f),h={blockId:f.id,offset:0}}return H(t,D(t,i,1,...d),{anchor:h,focus:h})}function xi(o){let e=o.doc.blocks,t={anchor:V(e[0]),focus:P(e[e.length-1])};return{...o,selection:t,storedMarks:null}}function cn(o,e,t={}){if(e.length===0)return o;let n=t.at??"selection",i=o,r;if(n==="selection")i=J(o),r=i.selection.focus;else if(n==="end"){let u=o.doc.blocks[o.doc.blocks.length-1];r=P(u)}else r=te(o.doc,{anchor:n,focus:n}).focus;let s,l;if(t.block||n==="end"?{state:s,after:l}=kc(i,e,r):(s=Xn({...i,selection:{anchor:r,focus:r}},e),l=s.selection.focus),t.select!=="keep")return s;let a=u=>u.blockId===r.blockId&&de(u.cell,r.cell)&&u.offset>r.offset?{...l,offset:l.offset+u.offset-r.offset}:u,d=te(s.doc,{anchor:a(i.selection.anchor),focus:a(i.selection.focus)});return{...s,selection:d}}function kc(o,e,t){let n=L(o.doc,t.blockId),i=o.doc.blocks[n],r=(d,u,c)=>{let h=P(u);return{state:H(o,d,{anchor:h,focus:h}),after:c}},s=e[e.length-1];if(i.type==="paragraph"&&B(i.content)===0)return r(D(o,n,1,...e),s,P(s));if(!q(i)||Q(i)||t.offset>=B(i.content))return r(D(o,n+1,0,...e),s,t);if(t.offset===0)return r(D(o,n,0,...e),s,V(i));let l=z(i,C(i.content,0,t.offset)),a={...z(i,C(i.content,t.offset)),id:ee()};return a.type==="listItem"&&a.start!==void 0&&(a={...a,start:void 0}),a=On(a),r(D(o,n,1,l,...e,a),s,V(a))}function dn(o){let e="",t=o.blocks,n=0;for(;n<t.length;){let i=t[n],r=M(i);if(r){let s=n+1;for(;s<t.length&&
|
|
12
|
+
`);return ue(t,p)}let s=C(r.content,0,n.offset),l=C(r.content,n.offset),a=e[0];if(e.length===1&&q(a)&&!(Q(a)&&B(r.content)>0)){let f=B(r.content)===0?{...a,id:r.id}:r,p=ce(s,a.content,l);return j(t,z(f,p),$(r.id,n.offset+B(a.content)))}let d=[],u=e.slice();s.length>0?q(a)&&!Q(a)?(d.push(z(r,ce(s,a.content))),u=u.slice(1)):d.push(z(r,s)):q(a)&&(d.push({...a,id:r.id}),u=u.slice(1)),d.push(...u);let c=d[d.length-1],h;if(q(c)&&!Q(c))h={blockId:c.id,offset:B(c.content)},l.length>0&&(d[d.length-1]=z(c,ce(c.content,l)));else{let f=z(Fn(r),l);d.push(f),h={blockId:f.id,offset:0}}return H(t,D(t,i,1,...d),{anchor:h,focus:h})}function xi(o){let e=o.doc.blocks,t={anchor:V(e[0]),focus:P(e[e.length-1])};return{...o,selection:t,storedMarks:null}}function cn(o,e,t={}){if(e.length===0)return o;let n=t.at??"selection",i=o,r;if(n==="selection")i=J(o),r=i.selection.focus;else if(n==="end"){let u=o.doc.blocks[o.doc.blocks.length-1];r=P(u)}else r=te(o.doc,{anchor:n,focus:n}).focus;let s,l;if(t.block||n==="end"?{state:s,after:l}=kc(i,e,r):(s=Xn({...i,selection:{anchor:r,focus:r}},e),l=s.selection.focus),t.select!=="keep")return s;let a=u=>u.blockId===r.blockId&&de(u.cell,r.cell)&&u.offset>r.offset?{...l,offset:l.offset+u.offset-r.offset}:u,d=te(s.doc,{anchor:a(i.selection.anchor),focus:a(i.selection.focus)});return{...s,selection:d}}function kc(o,e,t){let n=L(o.doc,t.blockId),i=o.doc.blocks[n],r=(d,u,c)=>{let h=P(u);return{state:H(o,d,{anchor:h,focus:h}),after:c}},s=e[e.length-1];if(i.type==="paragraph"&&B(i.content)===0)return r(D(o,n,1,...e),s,P(s));if(!q(i)||Q(i)||t.offset>=B(i.content))return r(D(o,n+1,0,...e),s,t);if(t.offset===0)return r(D(o,n,0,...e),s,V(i));let l=z(i,C(i.content,0,t.offset)),a={...z(i,C(i.content,t.offset)),id:ee()};return a.type==="listItem"&&a.start!==void 0&&(a={...a,start:void 0}),a=On(a),r(D(o,n,1,l,...e,a),s,V(a))}function dn(o){let e="",t=o.blocks,n=0;for(;n<t.length;){let i=t[n],r=M(i);if(r){let s=n+1;for(;s<t.length&&oe(i,t[s]);)s++;e+=xc(r==="quote"?void 0:r,dn({blocks:t.slice(n,s).map(Mt)})),n=s}else if(i.type==="listItem"){let s=n;for(;s<t.length&&t[s].type==="listItem"&&M(t[s])===null;)s++;e+=wc(t.slice(n,s)),n=s}else e+=vc(i),n++}return e}function vc(o){switch(o.type){case"paragraph":{let e=Y(o),t=e===null?void 0:C(o.content,e,e+1)[0]?.image?.align;return`<p${t?` style="text-align:${t}"`:""}>${Yn(o.content)}</p>`}case"heading":return`<h${o.level}>${Yn(o.content)}</h${o.level}>`;case"code":return`<pre><code${o.language?` class="language-${Ie(o.language)}"`:""}>${Ie(I(o.content))}</code></pre>`;case"raw":return o.syntax==="html"?I(o.content):`<pre>${Ie(I(o.content))}</pre>`;case"divider":return"<hr>";case"details":return`<details${o.open?" open":""}><summary>${Ie(I(o.content))}</summary>`;case"detailsEnd":return"</details>";case"table":{let e=(r,s,l)=>{let a=o.align[l];return`<${r}${a?` style="text-align: ${a}"`:""}>${Yn(s)}</${r}>`},[t,...n]=o.rows,i="<table>";return t&&(i+=`<thead><tr>${t.map((r,s)=>e("th",r,s)).join("")}</tr></thead>`),n.length>0&&(i+=`<tbody>${n.map(r=>`<tr>${r.map((s,l)=>e("td",s,l)).join("")}</tr>`).join("")}</tbody>`),i+"</table>"}case"listItem":case"quote":return dn({blocks:[o]})}}function xc(o,e){if(!o)return`<blockquote>${e}</blockquote>`;let t=o[0].toUpperCase()+o.slice(1);return`<blockquote class="markdown-alert markdown-alert-${o}"><p class="markdown-alert-title"><strong>${t}</strong></p>${e}</blockquote>`}function wc(o){let e="",t=[],n=i=>{for(;t.length>i;){let r=t.pop();r.liOpen&&(e+="</li>"),e+=`</${r.tag}>`}};for(let i of o){let r=i.listType==="ordered"?"ol":"ul",s=Math.min(i.indent,t.length);n(s+1);let l=t[s];if(l&&l.tag!==r&&n(s),!t[s]){let u=i.listType==="ordered"&&i.start!==void 0?` start="${i.start}"`:"";e+=`<${r}${u}>`,t.push({tag:r,liOpen:!1})}let a=t[s];a.liOpen&&(e+="</li>");let d=i.listType==="task"?`<input type="checkbox" disabled${i.checked?" checked":""}> `:"";e+=`<li>${d}${Yn(i.content)}`,a.liOpen=!0}return n(0),e}function Ie(o){return o.replace(/[&<>"]/g,e=>e==="&"?"&":e==="<"?"<":e===">"?">":""")}function Yn(o){let e="",t=0;for(;t<o.length;){let{link:n,linkTitle:i}=o[t],r=t,s="";for(;r<o.length&&ae(o[r],o[t]);)s+=Sc(o[r++]);let l=i?` title="${Ie(i)}"`:"";e+=n===void 0?s:`<a href="${Ie(n)}"${l}>${s}</a>`,t=r}return e}function Sc(o){let e;if(o.image){let{src:t,alt:n,title:i,width:r,height:s}=o.image;e=`<img src="${Ie(t)}" alt="${Ie(n)}"${i?` title="${Ie(i)}"`:""}`,r&&(e+=` width="${r}"${s?` height="${s}"`:""}`),e+=">"}else o.marks.includes("html")?e=o.text:e=Ie(o.text).replace(/\n/g,"<br>");return o.marks.includes("code")&&(e=`<code>${e}</code>`),o.marks.includes("strike")&&(e=`<s>${e}</s>`),o.marks.includes("italic")&&(e=`<em>${e}</em>`),o.marks.includes("bold")&&(e=`<strong>${e}</strong>`),e}function no(o,e){let t=e??(typeof DOMParser>"u"?null:new DOMParser);if(!t)throw new Error("htmlToDoc needs a DOMParser (pass one in outside the browser)");let n=t.parseFromString(o,"text/html"),i=new Ei;return i.blocks(n.body,Cc),{blocks:i.finish()}}var Tc=new Set(["html","body","meta","div","span","br","font"]),Ec=/font-weight\s*:\s*(?:bold|[6-9]00)|font-style\s*:\s*italic|text-decoration[^;"']*line-through/i;function ds(o){let e=o.replace(/<!--[\s\S]*?-->/g,"").replace(/<(style|script|head|title)\b[\s\S]*?<\/\1\s*>/gi,"");for(let t of e.matchAll(/<([A-Za-z][A-Za-z0-9-]*)\b([^>]*)>/g))if(!Tc.has(t[1].toLowerCase())||Ec.test(t[2]))return!1;return!0}var Jn=1,eo=3,to=new Set(["script","style","head","title","meta","link","template","noscript","iframe","object","embed","svg","canvas","audio","video","select","textarea","button","input","colgroup"]),un=new Set(["address","article","aside","blockquote","body","center","dd","details","dialog","dir","div","dl","dt","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","header","hgroup","hr","li","main","menu","nav","ol","p","pre","section","summary","table","ul","caption","thead","tbody","tfoot","tr","td","th"]),Mc=[...un].join(",");function ve(o){return o.tagName.toLowerCase()}function Si(o){return o==="ul"||o==="ol"||o==="menu"||o==="dir"}function us(o){let e=new Map,t=o.getAttribute("style");if(!t)return e;for(let n of t.split(";")){let i=n.indexOf(":");i<0||e.set(n.slice(0,i).trim().toLowerCase(),n.slice(i+1).trim().toLowerCase())}return e}var Cc={marks:[],whiteSpace:"normal"},Ic=/monospace|courier|consolas|menlo|monaco|source code|roboto mono|fira code/;function Dt(o,e){let t=ve(o),n=new Set(e.marks),i=e.link,r=e.whiteSpace;if((t==="b"||t==="strong")&&n.add("bold"),(t==="i"||t==="em"||t==="cite"||t==="dfn"||t==="var")&&n.add("italic"),(t==="s"||t==="del"||t==="strike")&&n.add("strike"),(t==="code"||t==="kbd"||t==="samp"||t==="tt")&&n.add("code"),t==="pre"&&(r="pre"),t==="a"){let c=o.getAttribute("href")?.trim();c&&!/^(?:javascript|vbscript|data):/i.test(c)&&(i=c)}let s=us(o),l=s.get("font-weight");l!==void 0&&(/^(?:bold|bolder|[6-9]00)$/.test(l)?n.add("bold"):/^(?:normal|lighter|[1-5]00)$/.test(l)&&n.delete("bold"));let a=s.get("font-style");a!==void 0&&(/^(?:italic|oblique)/.test(a)?n.add("italic"):a==="normal"&&n.delete("italic")),/line-through/.test(s.get("text-decoration")??s.get("text-decoration-line")??"")&&n.add("strike"),Ic.test(s.get("font-family")??"")&&n.add("code");let d=s.get("white-space");d!==void 0&&(r=/^(?:pre|pre-wrap|break-spaces)$/.test(d)?"pre":d==="pre-line"?"pre-line":"normal");let u=rt(n);return i===void 0?{marks:u,whiteSpace:r}:{marks:u,link:i,whiteSpace:r}}function cs(o){let t=/(?:^|\s)markdown-alert-([a-z]+)(?:\s|$)/.exec(o.getAttribute("class")??"")?.[1];return t&&Ve.includes(t)?t:void 0}function Lc(o,e){return(o.getAttribute("class")??"").split(/\s+/).includes(e)}var Ei=class o{out=[];spans=[];finish(){return this.flush(),this.out}blocks(e,t,n){for(let i of Array.from(e.childNodes))i.nodeType===eo?this.text(i.nodeValue??"",t):i.nodeType===Jn&&!n?.(i)&&this.element(i,t)}element(e,t){let n=ve(e);if(to.has(n))return;let i=Dt(e,t);if(/^h[1-6]$/.test(n)){this.flush();let r=this.inlineOf(e,i);this.out.push(nt(Number(n[1]),r))}else if(Si(n))this.flush(),this.list(e,0,i);else if(n==="li")this.flush(),this.listItem(e,"bullet",0,void 0,i);else if(n==="details"){this.flush();let r=Array.from(e.children).find(l=>ve(l)==="summary"),s=(r?.textContent??"").replace(/\s+/g," ").trim();this.out.push(St(ge(s),{open:e.hasAttribute("open")})),this.blocks(e,i,l=>l===r),this.flush(),this.out.push(Tt())}else n==="blockquote"||cs(e)?(this.flush(),this.blockquote(e,i)):n==="pre"?(this.flush(),this.out.push(Ee(Bc(e),Ac(e)))):n==="table"?(this.flush(),this.table(e,i)):n==="hr"?(this.flush(),this.out.push(it())):un.has(n)?(this.flush(),this.blocks(e,i),this.flush()):e.querySelector(Mc)?this.blocks(e,i):this.inline(e,t)}push(e,t,n){let i=t.link===void 0?{text:e,marks:t.marks}:{text:e,marks:t.marks,link:t.link};this.spans.push(n?{...i,...n}:i)}atLineStart(){let e=this.lastChar();return e===""||e===`
|
|
13
13
|
`}lastChar(){for(let e=this.spans.length-1;e>=0;e--){let t=this.spans[e].text;if(t)return t[t.length-1]}return""}text(e,t){let n=e.replace(/\r\n?/g,`
|
|
14
14
|
`);if(t.whiteSpace==="normal"?n=n.replace(/[ \t\n\f]+/g," "):t.whiteSpace==="pre-line"&&(n=n.replace(/[ \t\f]+/g," ").replace(/ ?\n ?/g,`
|
|
15
15
|
`)),t.whiteSpace!=="pre"){let i=this.lastChar();n.startsWith(" ")&&(i===""||i===" "||i===`
|
|
@@ -39,7 +39,7 @@ ${ko(s.length-1)}
|
|
|
39
39
|
`),slots:s}}function Pt(o,e={}){if(e.source)return pl(o.blocks,e.source,e);let t=o.blocks.length;for(;t>0&&er(o.blocks[t-1]);)t--;let n=Pe(o.blocks.slice(0,t),e);return n?n+`
|
|
40
40
|
`:""}function Pe(o,e){let t=ll(e),n="",i=null,r=new Xi(t.paragraph);for(let s=0;s<o.length;s++){let l=o[s],a,d=`
|
|
41
41
|
|
|
42
|
-
`,u=M(l);if(u){let c=s+1;for(;c<o.length&&
|
|
42
|
+
`,u=M(l);if(u){let c=s+1;for(;c<o.length&&oe(l,o[c]);)c++;let h=o.slice(s,c).map(Mt),f=Pe(h,e);u!=="quote"&&(f=`[!${u.toUpperCase()}]
|
|
43
43
|
${Ji(h[0])?"":`
|
|
44
44
|
`}${f}`),n&&(n+=d),n+=tr(f,"> ",">"),i=o[c-1],s=c-1;continue}switch(l.type){case"paragraph":a=l.content.length===0?" ":kn(l.content,t.paragraph);break;case"quote":continue;case"heading":a=jd(l.level,l.content,t.heading);break;case"listItem":{let c=i?.type==="listItem"&&!i.quote?i:null,h=r.next(l,c);a=h.text,c&&!h.blankLineBefore&&(d=`
|
|
45
45
|
`);break}case"code":a=Gd(l.language,I(l.content));break;case"raw":a=I(l.content);break;case"table":a=Zd(l.align,l.rows,t.cell);break;case"divider":a="---";break;case"details":{let c=I(l.content).replace(/[&<>]/g,h=>h==="&"?"&":h==="<"?"<":">");a=`<details${l.open?" open":""}>
|
|
@@ -62,19 +62,19 @@ ${e?e+`
|
|
|
62
62
|
`||su.test(c)?"\\\\":d;break;case"`":case"*":case"~":case"[":case"]":l+=t(d);break;case"_":{let h=u!==void 0&&c!==void 0&&il.test(u)&&il.test(c);l+=h?d:t("_");break}case"<":l+=c===void 0||/[A-Za-z/!?]/.test(c)?t("<"):d;break;case"&":l+=lu.test(o.slice(a,a+40))?t("&"):d;break;case"!":l+=c==="["?t("!"):d;break;default:l+=e.escape&&e.escape.includes(d)?t(d):d}}return l}function cu(o,e,t){let n=at(o);if(!n.length&&!/www\.|:\/\//i.test(o))return null;let i=new Set,r=o+(t??"");for(let s of o.matchAll(/www\.|(?:https?|ftp):\/\//gi)){let l=s.index,a=s[0].toLowerCase()==="www.";!It(l>0?o[l-1]:e,a)||!lt(r,l)||i.add(l+s[0].indexOf(a?".":":"))}for(let{from:s}of n)i.add(o.indexOf("@",s));return i}function du(o){if(/^#{1,6}(?:[ \t]|$)/.test(o)||o[0]===">"||/^[-+](?:[ \t]|$)/.test(o)||/^-+[ \t]*$/.test(o)||/^(?:-[ \t]*){3,}$/.test(o)||/^=+[ \t]*$/.test(o))return 0;let e=/^(\d{1,9})[.)](?:[ \t]|$)/.exec(o);return e?e[1].length:/^[|:\- \t]+$/.test(o)&&o.includes("|")&&o.includes("-")?o.indexOf("-"):-1}function uu(o){let e="`".repeat(sl(o,"`")+1);return o.startsWith("`")||o.endsWith("`")||o.startsWith(" ")&&o.endsWith(" ")&&/[^ ]/.test(o)?`${e} ${o} ${e}`:`${e}${o}${e}`}var hu=new RegExp(`^${Fi}$`),fu=new RegExp(`^${qi}$`);function ul(o){return o.link===void 0||o.linkTitle!==void 0||o.image||o.token||o.marks.includes("code")||o.marks.includes("html")?!1:Yr(o.text)===o.link}function pu(o){return o.image||o.marks.includes("code")||o.marks.includes("html")||o.link===void 0||o.linkTitle!==void 0?null:o.text===o.link&&hu.test(o.link)?`<${o.link}>`:o.link==="mailto:"+o.text&&fu.test(o.text)?`<${o.text}>`:null}function vo(o){return o.replace(/&(?=#?[A-Za-z0-9]+;)/g,"\\&")}function hl(o){if(o==="")return"<>";let e=(()=>{let t=0;for(let n of o)if(n==="(")t++;else if(n===")"&&--t<0)return!1;return t===0})();return/[\x00-\x20<>\x7f]/.test(o)||!e?"<"+vo(o.replace(/[\\<>]/g,"\\$&")).replace(/\n/g,"%0A")+">":vo(o.replace(/\\/g,"\\\\"))}function fl(o){return o?` "${vo(o.replace(/\n(?:[ \t]*\n)+/g,`
|
|
63
63
|
`).replace(/["\\]/g,"\\$&"))}"`:""}function mu(o,e){let t=e.newline==="soft"?{...e,newline:"break"}:e,n=dl({text:o.alt,encodeFirst:!1,encodeLast:!1},t,{before:"[",after:"]",lineStart:!1,atEnd:!1}),i=fl(o.title),r=hn(o),s=io(n+r);return`![${((s?s.index===n.length&&hn(s)===r:!r)?n:n.replace(/\|(?=[^|]*$)/,"|"))+r}](${hl(o.src)}${i})`}function Le(o,e,t=!0){let n=he(o,e),{lines:i}=co(o),{starts:r,ends:s}=i,l=[],a={text:o,lines:i,onQuoteChild:(y,g)=>l.push({node:y,blocks:g})},d=[],u=[],c=new WeakMap,h=new WeakMap,f=t?{lines:i,maps:h}:void 0,p=0,m=(e??pe).details===!1?null:Js(n.children);for(let y=0;y<n.children.length;y++){let g=n.children[y],x=m?.get(y),b=x?y:Ys(n.children,y),w=r[n.lines[y].start-1]??o.length,k=n.lines[b],S=s[Math.max(k.start,k.end)-1]??o.length,v=o.slice(w,S),T=Se=>(f&&h.set(Se,[gs(0,v.length,w)]),Se);l=[];let E=x?[x]:b>y?[T(Dn(v))]:fn(g,e??pe,d[d.length-1],f,a)??[T(ot("markdown",v))];if(y=b,d.push(...E),E.length===0||w<p||S<w)continue;p=S;let A=g.type==="codeBlock"&&g.fenced||g.type==="htmlBlock"?gu(v,e):!1,fe=g.type==="blockQuote"&&b===y?yu(l,E,k.end,o,i):void 0,me=fe?{index:u.length,from:w,to:S,blocks:E,open:A,quote:fe}:{index:u.length,from:w,to:S,blocks:E,open:A};u.push(me);for(let Se of E)c.set(Se,me)}return{doc:{blocks:d},source:{text:o,groups:u,groupOf:c,definitions:n.definitions,mapOf:h}}}function gu(o,e){return he(o+`
|
|
64
64
|
|
|
65
|
-
x`,e).children.length<2}var bu=o=>/^[ \t]*$/.test(ho(o));function yu(o,e,t,n,i){let r=M(e[0]);if(r===null)return;let s=new Map(e.map(u=>[u.id,u])),l=[];for(let[u,{node:c,blocks:h}]of o.entries()){if(h.length===0)continue;let f=ku(c);if(f===null)return;l.push({start:u===0&&r!=="quote"?f+1:f,blocks:h.map(p=>s.get(p.id)??p)})}let a=u=>n.slice(i.starts[u-1]??n.length,i.ends[u-1]??n.length),d=[];return l.forEach(({start:u,blocks:c},h)=>{let f=i.starts[u-1]??n.length,p=l[h+1]?l[h+1].start-1:t;for(;p>u&&bu(a(p));)p--;let m=i.ends[p-1]??f;d.push({from:f,to:Math.max(f,m),blocks:c})}),{kind:r,children:d}}function ku(o){switch(o.type){case"paragraph":case"heading":case"htmlBlock":case"raw":return o.source?.lines[0]?.line??null;default:return o.lines?.start??null}}function ml(o,e){return M(o)!==null||M(e)!==null?
|
|
65
|
+
x`,e).children.length<2}var bu=o=>/^[ \t]*$/.test(ho(o));function yu(o,e,t,n,i){let r=M(e[0]);if(r===null)return;let s=new Map(e.map(u=>[u.id,u])),l=[];for(let[u,{node:c,blocks:h}]of o.entries()){if(h.length===0)continue;let f=ku(c);if(f===null)return;l.push({start:u===0&&r!=="quote"?f+1:f,blocks:h.map(p=>s.get(p.id)??p)})}let a=u=>n.slice(i.starts[u-1]??n.length,i.ends[u-1]??n.length),d=[];return l.forEach(({start:u,blocks:c},h)=>{let f=i.starts[u-1]??n.length,p=l[h+1]?l[h+1].start-1:t;for(;p>u&&bu(a(p));)p--;let m=i.ends[p-1]??f;d.push({from:f,to:Math.max(f,m),blocks:c})}),{kind:r,children:d}}function ku(o){switch(o.type){case"paragraph":case"heading":case"htmlBlock":case"raw":return o.source?.lines[0]?.line??null;default:return o.lines?.start??null}}function ml(o,e){return M(o)!==null||M(e)!==null?oe(o,e):o?.type==="listItem"&&e?.type==="listItem"}function pl(o,e,t){let n=e.text,i,r,s=[];for(let b=0;b<o.length;){let w=o[b],k=e.groupOf.get(w);if(k&&k.blocks[0]===w&&k.blocks.every((E,A)=>o[b+A]===E)){let E=o[b+k.blocks.length];if(!(k.quote&&E&&oe(k.blocks[k.blocks.length-1],E))){s.push({group:k,blocks:[...k.blocks]}),b+=k.blocks.length;continue}}i??=new Map(e.groups.map(E=>[E.blocks[0].id,E]));let S=xu(o,b,i,e,t);if(S){s.push(S),b+=S.blocks.length;continue}r??=wu(e);let v=Su(o,b,r,e,t);if(v){s.push(v),b+=v.blocks.length;continue}let T=s[s.length-1];T&&!T.group?T.blocks.push(w):s.push({group:null,blocks:[w]}),b++}let l=(b,w)=>!!b&&(!b.group||!w.group||w.group.index!==b.group.index+1),a=b=>!!b.group&&/^[ \t]/.test(n.slice(b.group.from,b.group.to));for(let b=!0;b;){b=!1,s.forEach((w,k)=>{if(!w.group)return;let S=s[k-1],v=s[k+1],T=w.blocks[0],E=w.blocks[w.blocks.length-1],A=S?.blocks[S.blocks.length-1];(l(S,w)&&(ml(A,T)||A?.type==="listItem"&&a(w))||v&&l(w,v)&&ml(E,v.blocks[0])||w.group.open&&v)&&(w.group=null,b=!0)});for(let w=s.length-1;w>0;w--)!s[w].group&&!s[w-1].group&&(s[w-1].blocks.push(...s[w].blocks),s.splice(w,1))}let d=s[s.length-1];if(d&&!d.group){for(;d.blocks.length>0&&er(d.blocks[d.blocks.length-1]);)d.blocks.pop();d.blocks.length===0&&s.pop()}if(s.length===0&&e.groups.length===0)return n;let u=new Set;for(let b of s)b.group&&u.add(b.group.index);let c=(b,w)=>{if(!b||!w)return null;let k=n.slice(b.to,w.from);return/(?:\r\n|\n|\r)[ \t]*(?:\r\n|\n|\r)/.test(k)?k:null},h="",f;for(let b of s){let w=b.group?b.text??n.slice(b.group.from,b.group.to):Pe(b.blocks,t);if(w===""&&!b.group)continue;let k=b.group,S=f?.group;if(!f)k?.index===0&&(h+=n.slice(0,k.from));else if(S&&k&&k.index===S.index+1)h+=n.slice(S.to,k.from);else{let v=null;S&&!k&&!u.has(S.index+1)&&(v=c(S,e.groups[S.index+1])),k&&!S&&!u.has(k.index-1)&&(v=c(e.groups[k.index-1],k)),h+=v??`
|
|
66
66
|
|
|
67
67
|
`}h+=w,f=b}let p=f?.group,m=n===""||/[\r\n]$/.test(n)?`
|
|
68
68
|
`:"",y=p&&p.index===e.groups.length-1?n.slice(p.to):h===""?"":m,g=gl(h+y),x=e.definitions.filter(b=>!g.includes(gl(b)));if(x.length>0){let b=h.replace(/[\r\n]+$/,"");h=(b?b+`
|
|
69
69
|
|
|
70
70
|
`:"")+x.join(`
|
|
71
|
-
`),y=m}return h+y}var vu=/^((?:[ \t]*>)*[ \t]*(?:[-+*]|\d{1,9}[.)])[ \t]+\[)([ xX])(?=\][ \t]|\]$)/gm;function xu(o,e,t,n,i){let r=t.get(o[e].id);if(!r||r.open)return null;let s=o.slice(e,e+r.blocks.length);if(s.length!==r.blocks.length)return null;let l=!1;for(let f=0;f<s.length;f++){let p=r.blocks[f],m=s[f];if(p!==m){if(!Tu(p,m))return null;l=!0}}if(!l)return null;let a=s.filter(f=>f.type==="listItem"&&f.listType==="task"),d=n.text.slice(r.from,r.to),u=0,c=d.replace(vu,(f,p,m)=>{let y=a[u++];return!y||y.type!=="listItem"||m!==" "===y.checked?f:p+(y.checked?"x":" ")});if(u!==a.length)return null;let h=Le(c,i,!1).doc.blocks;return Pe(h,i)!==Pe(s,i)?null:{group:r,blocks:s,text:c}}function wu(o){let e=new Map;for(let t of o.groups)t.quote?.children.forEach((n,i)=>{for(let r of n.blocks)e.set(r.id,{group:t,child:i})});return e}function Su(o,e,t,n,i){let r=M(o[e]);if(r===null)return null;let s=e+1;for(;s<o.length&&
|
|
71
|
+
`),y=m}return h+y}var vu=/^((?:[ \t]*>)*[ \t]*(?:[-+*]|\d{1,9}[.)])[ \t]+\[)([ xX])(?=\][ \t]|\]$)/gm;function xu(o,e,t,n,i){let r=t.get(o[e].id);if(!r||r.open)return null;let s=o.slice(e,e+r.blocks.length);if(s.length!==r.blocks.length)return null;let l=!1;for(let f=0;f<s.length;f++){let p=r.blocks[f],m=s[f];if(p!==m){if(!Tu(p,m))return null;l=!0}}if(!l)return null;let a=s.filter(f=>f.type==="listItem"&&f.listType==="task"),d=n.text.slice(r.from,r.to),u=0,c=d.replace(vu,(f,p,m)=>{let y=a[u++];return!y||y.type!=="listItem"||m!==" "===y.checked?f:p+(y.checked?"x":" ")});if(u!==a.length)return null;let h=Le(c,i,!1).doc.blocks;return Pe(h,i)!==Pe(s,i)?null:{group:r,blocks:s,text:c}}function wu(o){let e=new Map;for(let t of o.groups)t.quote?.children.forEach((n,i)=>{for(let r of n.blocks)e.set(r.id,{group:t,child:i})});return e}function Su(o,e,t,n,i){let r=M(o[e]);if(r===null)return null;let s=e+1;for(;s<o.length&&oe(o[s-1],o[s]);)s++;let l=o.slice(e,s),a=l.map(k=>t.get(k.id)?.group).find(k=>k!==void 0),d=a?.quote;if(!a||!d||d.kind!==r)return null;let u=n.text,c=[];for(let k=0;k<l.length;){let S=t.get(l[k].id);if(S&&S.group!==a)return null;let v=S?d.children[S.child]:null;if(v&&v.blocks.every((E,A)=>l[k+A]===E)){c.push({blocks:[...v.blocks],child:S.child}),k+=v.blocks.length;continue}let T=c[c.length-1];T&&T.child===null?T.blocks.push(l[k]):c.push({blocks:[l[k]],child:null}),k++}if(!c.some(k=>k.child!==null))return null;let h=k=>u.slice(d.children[k].from,d.children[k].to),f=(k,S)=>k.child!==null&&S.child!==null&&S.child===k.child+1,p=(k,S)=>k.blocks[k.blocks.length-1].type==="listItem"&&S.blocks[0].type==="listItem",m=k=>k.child!==null&&/^[ \t]/.test(ho(h(k.child).split(/\r\n|\n|\r/,1)[0])),y=new Map,g=k=>{if(k.child===null)return!1;let S=y.get(k.child);if(S===void 0){let v=he(`${h(k.child)}
|
|
72
72
|
>
|
|
73
73
|
> x`,i).children[0];S=v?.type==="blockQuote"&&v.children.length<2,y.set(k.child,S)}return S};for(let k=!0;k;){k=!1,c.forEach((S,v)=>{if(S.child===null)return;let T=c[v-1],E=c[v+1];(T&&!f(T,S)&&(p(T,S)||T.blocks[T.blocks.length-1].type==="listItem"&&m(S))||E&&!f(S,E)&&(p(S,E)||g(S)))&&(S.child=null,k=!0)});for(let S=c.length-1;S>0;S--)c[S].child===null&&c[S-1].child===null&&(c[S-1].blocks.push(...c[S].blocks),c.splice(S,1))}if(!c.some(k=>k.child!==null))return null;let x=d.children.length-1,b="";return c.forEach((k,S)=>{let v=c[S-1];v?f(v,k)?b+=u.slice(d.children[v.child].to,d.children[k.child].from):b+=`
|
|
74
74
|
>
|
|
75
75
|
`:k.child===0?b+=u.slice(a.from,d.children[0].from):r!=="quote"&&(b+=`> [!${r.toUpperCase()}]
|
|
76
76
|
${Ji(k.blocks[0])?"":`>
|
|
77
|
-
`}`),b+=k.child!==null?h(k.child):tr(Pe(k.blocks.map(Mt),i),"> ",">")}),c[c.length-1].child===x&&(b+=u.slice(d.children[x].to,a.to)),{group:a,blocks:l,text:b}}function Tu(o,e){return o.type==="listItem"&&e.type==="listItem"&&o.listType==="task"&&e.listType==="task"&&o.id===e.id&&o.indent===e.indent&&o.content===e.content}function gl(o){return o.replace(/\s+/g," ").trim()}function bt(o,e){return Le(o,e,!1).doc}var Eu=["--mbe-font","--mbe-mono","--mbe-text","--mbe-muted","--mbe-bg","--mbe-popup-bg","--mbe-popup-border","--mbe-popup-shadow","--mbe-hover","--mbe-accent","--mbe-danger","--mbe-radius","--mbe-popup-z"],or=new WeakMap;function ir(o,e){e?or.set(o,e):or.delete(o)}function Mu(o){return or.get(o)??o.closest('dialog[open], [aria-modal="true"]')??o.ownerDocument.body}function $t(o,e){let t=Mu(e);o.parentElement!==t&&t.appendChild(o),o.style.left="0px",o.style.top="0px";let n=o.getBoundingClientRect();return{x:n.left,y:n.top}}function xo(o){let e=o.visualViewport;return e?{left:e.offsetLeft,top:e.offsetTop,width:e.width,height:e.height}:{left:0,top:0,width:o.innerWidth,height:o.innerHeight}}var re=typeof navigator<"u"&&/Mac|iP(hone|ad|od)/.test(navigator.platform),bl=typeof navigator<"u"&&/AppleWebKit/.test(navigator.userAgent)&&!/Chrome|Chromium|CriOS|Edg|Android/.test(navigator.userAgent),Ft=()=>typeof matchMedia=="function"&&matchMedia("(pointer: coarse)").matches,we=class{constructor(e,t){this.owner=e;let n=e.ownerDocument;this.el=n.createElement("div"),this.el.className=`mbe-ui ${t}`,this.el.hidden=!0,this.el.addEventListener("mousedown",i=>{i.target instanceof HTMLInputElement||i.preventDefault()})}el;open=!1;placed="below";get isOpen(){return this.open}get side(){return this.placed}show(e,t="below",n=6,i="start"){let r=this.owner.ownerDocument;this.open||this.copyTheme(),this.el.hidden=!1,this.open=!0;let s=$t(this.el,this.owner),l=r.defaultView,a=xo(l),d=this.el.scrollTop;this.el.style.maxHeight="";let u=this.el.offsetWidth,c=this.el.offsetHeight,h=a.top+a.height-8-(e.bottom+n),f=e.top-n-8-a.top,p=t;p==="below"&&c>h&&f>h?p="above":p==="above"&&c>f&&h>f&&(p="below");let m=p==="below"?h:f;if(c>m){let b=l.getComputedStyle(this.el),w=b.boxSizing==="border-box"?0:["paddingTop","paddingBottom","borderTopWidth","borderBottomWidth"].reduce((k,S)=>k+parseFloat(b[S]),0);this.el.style.maxHeight=`${Math.max(Math.floor(m-w),80)}px`,c=this.el.offsetHeight,this.el.scrollTop=d}this.placed=p;let y=p==="below"?e.bottom+n:e.top-c-n,g=i==="end"?(e.right??e.left)-u:e.left,x=Math.max(a.left+8,Math.min(g,a.left+a.width-u-8));this.el.style.left=`${Math.round(x-s.x)}px`,this.el.style.top=`${Math.round(y-s.y)}px`}showBeside(e,t,n,i=4){let r=this.owner.ownerDocument;this.open||this.copyTheme(),this.el.hidden=!1,this.open=!0;let s=$t(this.el,this.owner),l=xo(r.defaultView);this.el.style.maxHeight="";let a=this.el.offsetWidth,d=this.el.offsetHeight,u=e.right+i;u+a>l.left+l.width-8&&e.left-i-a>=l.left+8&&(u=e.left-i-a),u=Math.max(l.left+8,Math.min(u,l.left+l.width-a-8));let c=Math.max(l.top+8,Math.min(t-n,l.top+l.height-d-8));this.el.style.left=`${Math.round(u-s.x)}px`,this.el.style.top=`${Math.round(c-s.y)}px`}hide(){this.open&&(this.open=!1,this.el.hidden=!0)}destroy(){this.el.remove()}copyTheme(){let e=this.owner.ownerDocument.defaultView.getComputedStyle(this.owner);for(let t of Eu){let n=e.getPropertyValue(t);n&&this.el.style.setProperty(t,n)}this.el.style.colorScheme=e.colorScheme}},Cu=0,Xe=class o{constructor(e,t,n="listbox"){this.owner=e;this.className=t;this.popup=new we(e,`mbe-menu ${t}`),this.popup.el.setAttribute("role",n),this.popup.el.id=this.id}popup;items=[];index=0;id=`mbe-menu-${++Cu}`;child=null;childIndex=-1;checkIcon;submenuIcon;onPicked=null;onHighlight=null;get el(){return this.popup.el}get isOpen(){return this.popup.isOpen}get activeDescendant(){return this.child?.isOpen?this.child.activeDescendant:this.items.length?`${this.id}-${this.index}`:null}get submenu(){return this.child?.isOpen?this.child:null}contains(e){return this.el.contains(e)||!!this.child?.contains(e)}openSubmenu(e=this.index){let t=this.items[e],n=this.el.querySelector(`#${this.id}-${e}`);if(!t?.submenu||!n)return!1;if(this.child?.isOpen&&this.childIndex===e)return!0;this.highlight(e),this.child||(this.child=new o(this.owner,`mbe-submenu ${this.className}-submenu`,"menu"),this.child.onPicked=()=>{this.closeSubmenu(),this.onPicked?.()},this.child.onHighlight=()=>this.onHighlight?.(this.index)),this.child.checkIcon=this.checkIcon,this.child.el.setAttribute("aria-label",t.submenu.title??t.label),this.child.setSections([t.submenu]);let i=t.submenu.items.find(d=>d.checked);i&&this.child.highlightId(i.id),this.childIndex=e,n.setAttribute("aria-expanded","true");let r=this.el.getBoundingClientRect(),s=n.getBoundingClientRect().top;this.child.popup.showBeside(r,s,0);let l=this.child.el.querySelector(".mbe-menu-item"),a=l?l.getBoundingClientRect().top-this.child.el.getBoundingClientRect().top:0;return a&&this.child.popup.showBeside(r,s,a),this.onHighlight?.(this.index),!0}closeSubmenu(){this.child?.isOpen&&(this.child.hide(),this.el.querySelector(`#${this.id}-${this.childIndex}`)?.setAttribute("aria-expanded","false"),this.childIndex=-1,this.onHighlight?.(this.index))}setSections(e,t){let n=this.el.ownerDocument;this.closeSubmenu(),this.el.replaceChildren(),this.items=[],this.index=0;for(let i of e)if(i.items.length!==0){if(i.title){let r=n.createElement("div");r.className="mbe-menu-title",r.textContent=i.title,r.setAttribute("role","presentation"),this.el.appendChild(r)}for(let r of i.items){let s=this.items.length;this.items.push(r);let l=n.createElement("div");l.className="mbe-menu-item",l.id=`${this.id}-${s}`;let a=this.el.getAttribute("role")==="menu"?r.checked===void 0?"menuitem":"menuitemradio":"option";l.setAttribute("role",a),r.checked!==void 0&&l.setAttribute("aria-checked",String(r.checked)),r.keyShortcuts&&l.setAttribute("aria-keyshortcuts",r.keyShortcuts),r.danger&&l.classList.add("mbe-danger");let d=n.createElement("span");d.className="mbe-menu-icon";let u=X(n,r.icon);u&&d.appendChild(u);let c=n.createElement("span");if(c.className=r.labelClass?`mbe-menu-label ${r.labelClass}`:"mbe-menu-label",Iu(c,r.label,r.mark),l.append(d,c),r.detail){let h=n.createElement("span");h.className="mbe-menu-detail",h.textContent=r.detail,l.appendChild(h)}if(r.checked){let h=n.createElement("span");h.className="mbe-menu-check";let f=X(n,this.checkIcon);f&&h.appendChild(f),l.appendChild(h)}if(r.submenu){l.setAttribute("aria-haspopup","menu"),l.setAttribute("aria-expanded","false");let h=n.createElement("span");h.className="mbe-menu-submenu";let f=X(n,this.submenuIcon);f&&h.appendChild(f),l.appendChild(h)}l.addEventListener("mousemove",()=>{(this.index!==s||!this.child?.isOpen)&&this.highlight(s),r.submenu?this.openSubmenu(s):this.closeSubmenu()}),l.addEventListener("click",()=>this.pick(s)),this.el.appendChild(l)}}if(this.items.length===0&&t){let i=n.createElement("div");i.className="mbe-menu-empty",i.textContent=t,this.el.appendChild(i)}this.highlight(0)}setMessage(e,t){let i=this.el.ownerDocument.createElement("div");i.className=t,i.textContent=e,this.el.replaceChildren(i),this.items=[],this.index=0}set busy(e){e?this.el.setAttribute("aria-busy","true"):this.el.removeAttribute("aria-busy")}get count(){return this.items.length}get activeId(){return this.items[this.index]?.id??null}highlightId(e){let t=this.items.findIndex(n=>n.id===e);return t<0?!1:(this.highlight(t),!0)}page(e){let t=this.el.querySelector(".mbe-menu-item"),n=t&&t.offsetHeight>0?Math.max(1,Math.floor(this.el.clientHeight/t.offsetHeight)-1):8;this.highlight(Math.max(0,Math.min(this.items.length-1,this.index+e*n)))}highlight(e){this.items.length!==0&&(this.index=(e+this.items.length)%this.items.length,this.el.querySelectorAll(".mbe-menu-item").forEach((t,n)=>{t.classList.toggle("mbe-highlighted",n===this.index),t.setAttribute("aria-selected",String(n===this.index))}),this.el.querySelector(`#${this.id}-${this.index}`)?.scrollIntoView?.({block:"nearest"}),this.onHighlight?.(this.index))}get activeIndex(){return this.items.length?this.index:-1}move(e){this.highlight(this.index+e)}pick(e=this.index){let t=this.items[e];if(t){if(t.submenu){this.openSubmenu(e);return}t.run(),this.onPicked?.()}}hide(){this.closeSubmenu(),this.popup.hide()}destroy(){this.child?.destroy(),this.popup.destroy()}};function Iu(o,e,t){let n=t?e.toLowerCase().indexOf(t.toLowerCase()):-1;if(n<0||e.toLowerCase().length!==e.length){o.textContent=e;return}let r=o.ownerDocument.createElement("mark");r.className="mbe-menu-match",r.textContent=e.slice(n,n+t.length),o.replaceChildren(e.slice(0,n),r,e.slice(n+t.length))}function yl(o,e){let t=e.trim().toLowerCase();return t?[o.label,...o.aliases??[]].map(i=>i.toLowerCase()).some(i=>i.includes(t)||i.replace(/\s+/g,"").includes(t.replace(/\s+/g,""))):!0}function rr(){let o={"Mod-b":({editor:e})=>e.commands.toggleBold(),"Mod-i":({editor:e})=>e.commands.toggleItalic(),"Mod-e":({editor:e})=>e.commands.toggleCode(),"Mod-Shift-x":({editor:e})=>e.commands.toggleStrike(),"Mod-Shift-s":({editor:e})=>e.commands.toggleStrike(),"Mod-k":({editor:e})=>e.openLinkPopover(),"Mod-Alt-0":({editor:e})=>e.commands.setParagraph()||!0,"Mod-Alt-1":({editor:e})=>e.commands.toggleHeading({level:1})||!0,"Mod-Alt-2":({editor:e})=>e.commands.toggleHeading({level:2})||!0,"Mod-Alt-3":({editor:e})=>e.commands.toggleHeading({level:3})||!0,"Mod-Alt-4":({editor:e})=>e.commands.toggleHeading({level:4})||!0,"Mod-Alt-c":({editor:e})=>e.commands.toggleCodeBlock()||!0,"Mod-Shift-7":({editor:e})=>e.commands.toggleOrderedList()||!0,"Mod-Shift-8":({editor:e})=>e.commands.toggleBulletList()||!0,"Mod-Shift-9":({editor:e})=>e.commands.toggleTaskList()||!0,"Mod-Shift-6":({editor:e})=>e.commands.toggleDetails()||!0,"Mod-a":({editor:e})=>Lu(e),"Mod-z":({editor:e})=>e.commands.undo()||!0,"Mod-Shift-z":({editor:e})=>e.commands.redo()||!0,"Mod-Enter":({editor:e})=>e.submit(),"Ctrl-Enter":({editor:e})=>e.submit(),Tab:({editor:e})=>kl(e,1),"Shift-Tab":({editor:e})=>kl(e,-1),Escape:({editor:e})=>e.commands.selectBlocks()||e.commands.blur(),"Ctrl-Space":({editor:e})=>e.suggestion.open()};return re&&(o["Alt-Escape"]=({editor:e})=>e.suggestion.open()),re||(o["Ctrl-y"]=({editor:e})=>e.commands.redo()||!0),{name:"defaultKeymap",keymap:o}}function kl(o,e){return o.isActive("table")?o.run(t=>hi(t,e)):o.isActive("codeBlock")?o.run(t=>ln(t,e)):e===1?o.commands.indent():o.commands.outdent(),!0}function Lu(o){let{doc:e,selection:t}=o.state,{anchor:n,focus:i}=t;if(n.blockId===i.blockId&&de(n.cell,i.cell)){let r=N(e,i.blockId),s=i.cell||r.type==="code"?F(r,i.cell):null;if(s){let l=B(s);if(Math.min(n.offset,i.offset)!==0||Math.max(n.offset,i.offset)!==l)return o.setSelection({anchor:{...i,offset:0},focus:{...i,offset:l}}),!0}}return o.commands.selectAll()}var xn="mbe-source";function Bu(o){return{blocks:[Ee("",o,xn)]}}function So(o){return o.blocks.length===1&&o.blocks[0].id===xn}function K(o){return I(o.blocks[0].content)}function vl(o){return{blockId:xn,offset:o}}function Fe(o,e,t=e){return{doc:Bu(o),selection:{anchor:vl(vn(e,o)),focus:vl(vn(t,o))},storedMarks:null}}function vn(o,e){return Math.max(0,Math.min(o,e.length))}function ne(o){let e=o.selection.anchor.offset,t=o.selection.focus.offset;return{from:Math.min(e,t),to:Math.max(e,t)}}function Be(o,e,t,n,i,r=i){let s=K(o.doc);return Fe(s.slice(0,e)+n+s.slice(t),i,r)}function Ae(o,e){let{from:t,to:n}=ne(o);return Be(o,t,n,e,t+e.length)}function To(o,e,t,n,i){let r=K(o.doc),s=vn(e,r),l=t;if(n&&(s>0&&r[s-1]!==`
|
|
77
|
+
`}`),b+=k.child!==null?h(k.child):tr(Pe(k.blocks.map(Mt),i),"> ",">")}),c[c.length-1].child===x&&(b+=u.slice(d.children[x].to,a.to)),{group:a,blocks:l,text:b}}function Tu(o,e){return o.type==="listItem"&&e.type==="listItem"&&o.listType==="task"&&e.listType==="task"&&o.id===e.id&&o.indent===e.indent&&o.content===e.content}function gl(o){return o.replace(/\s+/g," ").trim()}function bt(o,e){return Le(o,e,!1).doc}var Eu=["--mbe-font","--mbe-mono","--mbe-text","--mbe-muted","--mbe-bg","--mbe-popup-bg","--mbe-popup-border","--mbe-popup-shadow","--mbe-hover","--mbe-accent","--mbe-danger","--mbe-radius","--mbe-popup-z"],or=new WeakMap;function ir(o,e){e?or.set(o,e):or.delete(o)}function Mu(o){return or.get(o)??o.closest('dialog[open], [aria-modal="true"]')??o.ownerDocument.body}function $t(o,e){let t=Mu(e);o.parentElement!==t&&t.appendChild(o),o.style.left="0px",o.style.top="0px";let n=o.getBoundingClientRect();return{x:n.left,y:n.top}}function xo(o){let e=o.visualViewport;return e?{left:e.offsetLeft,top:e.offsetTop,width:e.width,height:e.height}:{left:0,top:0,width:o.innerWidth,height:o.innerHeight}}var ie=typeof navigator<"u"&&/Mac|iP(hone|ad|od)/.test(navigator.platform),bl=typeof navigator<"u"&&/AppleWebKit/.test(navigator.userAgent)&&!/Chrome|Chromium|CriOS|Edg|Android/.test(navigator.userAgent),Ft=()=>typeof matchMedia=="function"&&matchMedia("(pointer: coarse)").matches,we=class{constructor(e,t){this.owner=e;let n=e.ownerDocument;this.el=n.createElement("div"),this.el.className=`mbe-ui ${t}`,this.el.hidden=!0,this.el.addEventListener("mousedown",i=>{i.target instanceof HTMLInputElement||i.preventDefault()})}el;open=!1;placed="below";get isOpen(){return this.open}get side(){return this.placed}show(e,t="below",n=6,i="start"){let r=this.owner.ownerDocument;this.open||this.copyTheme(),this.el.hidden=!1,this.open=!0;let s=$t(this.el,this.owner),l=r.defaultView,a=xo(l),d=this.el.scrollTop;this.el.style.maxHeight="";let u=this.el.offsetWidth,c=this.el.offsetHeight,h=a.top+a.height-8-(e.bottom+n),f=e.top-n-8-a.top,p=t;p==="below"&&c>h&&f>h?p="above":p==="above"&&c>f&&h>f&&(p="below");let m=p==="below"?h:f;if(c>m){let b=l.getComputedStyle(this.el),w=b.boxSizing==="border-box"?0:["paddingTop","paddingBottom","borderTopWidth","borderBottomWidth"].reduce((k,S)=>k+parseFloat(b[S]),0);this.el.style.maxHeight=`${Math.max(Math.floor(m-w),80)}px`,c=this.el.offsetHeight,this.el.scrollTop=d}this.placed=p;let y=p==="below"?e.bottom+n:e.top-c-n,g=i==="end"?(e.right??e.left)-u:e.left,x=Math.max(a.left+8,Math.min(g,a.left+a.width-u-8));this.el.style.left=`${Math.round(x-s.x)}px`,this.el.style.top=`${Math.round(y-s.y)}px`}showBeside(e,t,n,i=4){let r=this.owner.ownerDocument;this.open||this.copyTheme(),this.el.hidden=!1,this.open=!0;let s=$t(this.el,this.owner),l=xo(r.defaultView);this.el.style.maxHeight="";let a=this.el.offsetWidth,d=this.el.offsetHeight,u=e.right+i;u+a>l.left+l.width-8&&e.left-i-a>=l.left+8&&(u=e.left-i-a),u=Math.max(l.left+8,Math.min(u,l.left+l.width-a-8));let c=Math.max(l.top+8,Math.min(t-n,l.top+l.height-d-8));this.el.style.left=`${Math.round(u-s.x)}px`,this.el.style.top=`${Math.round(c-s.y)}px`}hide(){this.open&&(this.open=!1,this.el.hidden=!0)}destroy(){this.el.remove()}copyTheme(){let e=this.owner.ownerDocument.defaultView.getComputedStyle(this.owner);for(let t of Eu){let n=e.getPropertyValue(t);n&&this.el.style.setProperty(t,n)}this.el.style.colorScheme=e.colorScheme}},Cu=0,Xe=class o{constructor(e,t,n="listbox"){this.owner=e;this.className=t;this.popup=new we(e,`mbe-menu ${t}`),this.popup.el.setAttribute("role",n),this.popup.el.id=this.id}popup;items=[];index=0;id=`mbe-menu-${++Cu}`;child=null;childIndex=-1;checkIcon;submenuIcon;onPicked=null;onHighlight=null;get el(){return this.popup.el}get isOpen(){return this.popup.isOpen}get activeDescendant(){return this.child?.isOpen?this.child.activeDescendant:this.items.length?`${this.id}-${this.index}`:null}get submenu(){return this.child?.isOpen?this.child:null}contains(e){return this.el.contains(e)||!!this.child?.contains(e)}openSubmenu(e=this.index){let t=this.items[e],n=this.el.querySelector(`#${this.id}-${e}`);if(!t?.submenu||!n)return!1;if(this.child?.isOpen&&this.childIndex===e)return!0;this.highlight(e),this.child||(this.child=new o(this.owner,`mbe-submenu ${this.className}-submenu`,"menu"),this.child.onPicked=()=>{this.closeSubmenu(),this.onPicked?.()},this.child.onHighlight=()=>this.onHighlight?.(this.index)),this.child.checkIcon=this.checkIcon,this.child.el.setAttribute("aria-label",t.submenu.title??t.label),this.child.setSections([t.submenu]);let i=t.submenu.items.find(d=>d.checked);i&&this.child.highlightId(i.id),this.childIndex=e,n.setAttribute("aria-expanded","true");let r=this.el.getBoundingClientRect(),s=n.getBoundingClientRect().top;this.child.popup.showBeside(r,s,0);let l=this.child.el.querySelector(".mbe-menu-item"),a=l?l.getBoundingClientRect().top-this.child.el.getBoundingClientRect().top:0;return a&&this.child.popup.showBeside(r,s,a),this.onHighlight?.(this.index),!0}closeSubmenu(){this.child?.isOpen&&(this.child.hide(),this.el.querySelector(`#${this.id}-${this.childIndex}`)?.setAttribute("aria-expanded","false"),this.childIndex=-1,this.onHighlight?.(this.index))}setSections(e,t){let n=this.el.ownerDocument;this.closeSubmenu(),this.el.replaceChildren(),this.items=[],this.index=0;for(let i of e)if(i.items.length!==0){if(i.title){let r=n.createElement("div");r.className="mbe-menu-title",r.textContent=i.title,r.setAttribute("role","presentation"),this.el.appendChild(r)}for(let r of i.items){let s=this.items.length;this.items.push(r);let l=n.createElement("div");l.className="mbe-menu-item",l.id=`${this.id}-${s}`;let a=this.el.getAttribute("role")==="menu"?r.checked===void 0?"menuitem":"menuitemradio":"option";l.setAttribute("role",a),r.checked!==void 0&&l.setAttribute("aria-checked",String(r.checked)),r.keyShortcuts&&l.setAttribute("aria-keyshortcuts",r.keyShortcuts),r.danger&&l.classList.add("mbe-danger");let d=n.createElement("span");d.className="mbe-menu-icon";let u=X(n,r.icon);u&&d.appendChild(u);let c=n.createElement("span");if(c.className=r.labelClass?`mbe-menu-label ${r.labelClass}`:"mbe-menu-label",Iu(c,r.label,r.mark),l.append(d,c),r.detail){let h=n.createElement("span");h.className="mbe-menu-detail",h.textContent=r.detail,l.appendChild(h)}if(r.checked){let h=n.createElement("span");h.className="mbe-menu-check";let f=X(n,this.checkIcon);f&&h.appendChild(f),l.appendChild(h)}if(r.submenu){l.setAttribute("aria-haspopup","menu"),l.setAttribute("aria-expanded","false");let h=n.createElement("span");h.className="mbe-menu-submenu";let f=X(n,this.submenuIcon);f&&h.appendChild(f),l.appendChild(h)}l.addEventListener("mousemove",()=>{(this.index!==s||!this.child?.isOpen)&&this.highlight(s),r.submenu?this.openSubmenu(s):this.closeSubmenu()}),l.addEventListener("click",()=>this.pick(s)),this.el.appendChild(l)}}if(this.items.length===0&&t){let i=n.createElement("div");i.className="mbe-menu-empty",i.textContent=t,this.el.appendChild(i)}this.highlight(0)}setMessage(e,t){let i=this.el.ownerDocument.createElement("div");i.className=t,i.textContent=e,this.el.replaceChildren(i),this.items=[],this.index=0}set busy(e){e?this.el.setAttribute("aria-busy","true"):this.el.removeAttribute("aria-busy")}get count(){return this.items.length}get activeId(){return this.items[this.index]?.id??null}highlightId(e){let t=this.items.findIndex(n=>n.id===e);return t<0?!1:(this.highlight(t),!0)}page(e){let t=this.el.querySelector(".mbe-menu-item"),n=t&&t.offsetHeight>0?Math.max(1,Math.floor(this.el.clientHeight/t.offsetHeight)-1):8;this.highlight(Math.max(0,Math.min(this.items.length-1,this.index+e*n)))}highlight(e){this.items.length!==0&&(this.index=(e+this.items.length)%this.items.length,this.el.querySelectorAll(".mbe-menu-item").forEach((t,n)=>{t.classList.toggle("mbe-highlighted",n===this.index),t.setAttribute("aria-selected",String(n===this.index))}),this.el.querySelector(`#${this.id}-${this.index}`)?.scrollIntoView?.({block:"nearest"}),this.onHighlight?.(this.index))}get activeIndex(){return this.items.length?this.index:-1}move(e){this.highlight(this.index+e)}pick(e=this.index){let t=this.items[e];if(t){if(t.submenu){this.openSubmenu(e);return}t.run(),this.onPicked?.()}}hide(){this.closeSubmenu(),this.popup.hide()}destroy(){this.child?.destroy(),this.popup.destroy()}};function Iu(o,e,t){let n=t?e.toLowerCase().indexOf(t.toLowerCase()):-1;if(n<0||e.toLowerCase().length!==e.length){o.textContent=e;return}let r=o.ownerDocument.createElement("mark");r.className="mbe-menu-match",r.textContent=e.slice(n,n+t.length),o.replaceChildren(e.slice(0,n),r,e.slice(n+t.length))}function yl(o,e){let t=e.trim().toLowerCase();return t?[o.label,...o.aliases??[]].map(i=>i.toLowerCase()).some(i=>i.includes(t)||i.replace(/\s+/g,"").includes(t.replace(/\s+/g,""))):!0}function rr(){let o={"Mod-b":({editor:e})=>e.commands.toggleBold(),"Mod-i":({editor:e})=>e.commands.toggleItalic(),"Mod-e":({editor:e})=>e.commands.toggleCode(),"Mod-Shift-x":({editor:e})=>e.commands.toggleStrike(),"Mod-Shift-s":({editor:e})=>e.commands.toggleStrike(),"Mod-k":({editor:e})=>e.openLinkPopover(),"Mod-Alt-0":({editor:e})=>e.commands.setParagraph()||!0,"Mod-Alt-1":({editor:e})=>e.commands.toggleHeading({level:1})||!0,"Mod-Alt-2":({editor:e})=>e.commands.toggleHeading({level:2})||!0,"Mod-Alt-3":({editor:e})=>e.commands.toggleHeading({level:3})||!0,"Mod-Alt-4":({editor:e})=>e.commands.toggleHeading({level:4})||!0,"Mod-Alt-c":({editor:e})=>e.commands.toggleCodeBlock()||!0,"Mod-Shift-7":({editor:e})=>e.commands.toggleOrderedList()||!0,"Mod-Shift-8":({editor:e})=>e.commands.toggleBulletList()||!0,"Mod-Shift-9":({editor:e})=>e.commands.toggleTaskList()||!0,"Mod-Shift-6":({editor:e})=>e.commands.toggleDetails()||!0,"Mod-a":({editor:e})=>Lu(e),"Mod-z":({editor:e})=>e.commands.undo()||!0,"Mod-Shift-z":({editor:e})=>e.commands.redo()||!0,"Mod-Enter":({editor:e})=>e.submit(),"Ctrl-Enter":({editor:e})=>e.submit(),Tab:({editor:e})=>kl(e,1),"Shift-Tab":({editor:e})=>kl(e,-1),Escape:({editor:e})=>e.commands.selectBlocks()||e.commands.blur(),"Ctrl-Space":({editor:e})=>e.suggestion.open()};return ie&&(o["Alt-Escape"]=({editor:e})=>e.suggestion.open()),ie||(o["Ctrl-y"]=({editor:e})=>e.commands.redo()||!0),{name:"defaultKeymap",keymap:o}}function kl(o,e){return o.isActive("table")?o.run(t=>hi(t,e)):o.isActive("codeBlock")?o.run(t=>ln(t,e)):e===1?o.commands.indent():o.commands.outdent(),!0}function Lu(o){let{doc:e,selection:t}=o.state,{anchor:n,focus:i}=t;if(n.blockId===i.blockId&&de(n.cell,i.cell)){let r=N(e,i.blockId),s=i.cell||r.type==="code"?F(r,i.cell):null;if(s){let l=B(s);if(Math.min(n.offset,i.offset)!==0||Math.max(n.offset,i.offset)!==l)return o.setSelection({anchor:{...i,offset:0},focus:{...i,offset:l}}),!0}}return o.commands.selectAll()}var xn="mbe-source";function Bu(o){return{blocks:[Ee("",o,xn)]}}function So(o){return o.blocks.length===1&&o.blocks[0].id===xn}function K(o){return I(o.blocks[0].content)}function vl(o){return{blockId:xn,offset:o}}function Fe(o,e,t=e){return{doc:Bu(o),selection:{anchor:vl(vn(e,o)),focus:vl(vn(t,o))},storedMarks:null}}function vn(o,e){return Math.max(0,Math.min(o,e.length))}function ne(o){let e=o.selection.anchor.offset,t=o.selection.focus.offset;return{from:Math.min(e,t),to:Math.max(e,t)}}function Be(o,e,t,n,i,r=i){let s=K(o.doc);return Fe(s.slice(0,e)+n+s.slice(t),i,r)}function Ae(o,e){let{from:t,to:n}=ne(o);return Be(o,t,n,e,t+e.length)}function To(o,e,t,n,i){let r=K(o.doc),s=vn(e,r),l=t;if(n&&(s>0&&r[s-1]!==`
|
|
78
78
|
`&&(l=`
|
|
79
79
|
`+l),s<r.length&&r[s]!==`
|
|
80
80
|
`&&!l.endsWith(`
|
|
@@ -96,7 +96,7 @@ ${Ji(k.blocks[0])?"":`>
|
|
|
96
96
|
`+e.slice(d.from,d.quoteEnd)+"```";return Be(o,a.from,d.to,c+e.slice(a.from,d.to)+h,...lr(o,t+c.length,n+c.length))}function _l(o,e){let{from:t,to:n}=ne(o);return t===n?null:Ae(o,`[${K(o.doc).slice(t,n)}](${e})`)}function Kl(o){let e=K(o.doc),t=Ye(e);return t[qt(t,ne(o).from)].fence!==-1}function Vl(o,e,t,n){let i=e??o;if(!i||o&&n&&(o.bottom<=n.top||o.top>=n.bottom||o.right<n.left||o.left>n.right))return null;let r=o&&e&&Math.abs(o.top-e.top)<Math.max(1,Math.min(o.bottom-o.top,e.bottom-e.top)/2),s=o&&(r||!e)?o:i,l=t?s.right:s.left;return{rect:{left:l,right:l,top:i.top,bottom:i.bottom},align:t?"end":"start"}}function Ul(o){let e=o.ownerDocument.defaultView,t={left:0,top:0,right:e.innerWidth,bottom:e.innerHeight},n=o.ownerDocument.documentElement;for(let i=o;i&&i!==n&&i!==o.ownerDocument.body;i=i.parentElement){let r=e.getComputedStyle(i);if(r.overflowX==="visible"&&r.overflowY==="visible")continue;let s=i.getBoundingClientRect();t={left:Math.max(t.left,s.left),top:Math.max(t.top,s.top),right:Math.min(t.right,s.right),bottom:Math.min(t.bottom,s.bottom)}}return t}function mr({allowSpaces:o=!1,allowAnywhere:e=!1,maxLength:t=30}={}){let n=o?/\s\s|\n/:/\s/;return({text:i,caret:r,triggers:s})=>{let l=i.lastIndexOf(`
|
|
97
97
|
`,r-1)+1,a=Math.max(0,...s.map(u=>u.length)),d=Math.max(l,r-t-a);for(let u=r-1;u>=d;u--){let c=Mn(i,u,s,r);if(!c)continue;let h=i.slice(u+c.length,r);if(h.length>t||n.test(h))return null;let f=i[u-1];if(!e&&f!==void 0&&!/\s/.test(f))continue;let p=r;if(!o)for(;p<i.length&&!/\s/.test(i[p]);)p++;return{from:u,to:p,query:h}}return null}}var Fu=/^[\p{M}\u200c\u200d\ufe00-\ufe0f\u{e0100}-\u{e01ef}]/u;function qu(o,{probe:e="a",maxLength:t=30}={}){return({text:n,caret:i,triggers:r})=>{let s=n.codePointAt(i);if(s!==void 0&&Fu.test(String.fromCodePoint(s)))return null;let l=n,a=r[0];if(a)for(let m of r)m!==a&&m.length===a.length&&(l=l.split(m).join(a));let d=l.slice(0,i)+e+l.slice(i),u=n.lastIndexOf(`
|
|
98
98
|
`,i-1)+1,c=n.indexOf(`
|
|
99
|
-
`,i);c<0&&(c=n.length);let h=t+16,f=Math.max(u,i-h);for(;f>u&&!/\s/.test(n[f-1]);)f--;let p=Math.min(c+e.length,i+e.length+h);for(let m of o(d,f,p)){if(m.from>=i||m.to<i+e.length)continue;let y=Mn(n,m.from,r,i);if(!y)return null;let g=n.slice(m.from+y.length,i);return g.length>t?null:{from:m.from,to:m.to-e.length,query:g}}return null}}function Mn(o,e,t,n=o.length){let i=null;for(let r of t)r&&e+r.length<=n&&o.startsWith(r,e)&&(!i||r.length>i.length)&&(i=r);return i}function zl(o,e){let t=0;for(let n of o){let i=t+n.text.length;if(e<i)return ct(n);t=i}return!1}var Mo=class{request=0;controller=null;debounceTimer=null;loadingTimer=null;waiting=!1;get pending(){return this.waiting}run(e,t,n=0){this.cancel();let i=this.request;this.waiting=!0,this.loadingTimer=setTimeout(()=>{this.loadingTimer=null,i===this.request&&t.loading()},300);let r=l=>{i===this.request&&(this.settle(),t.show(l))},s=()=>{this.debounceTimer=null;let l=new AbortController;this.controller=l;let a;try{a=e(l.signal)}catch{r(null);return}_u(a)?a.then(r,()=>r(null)):r(a)};n>0?this.debounceTimer=setTimeout(s,n):s()}cancel(){this.request++,this.controller?.abort(),this.settle()}settle(){this.waiting=!1,this.controller=null,this.debounceTimer!==null&&clearTimeout(this.debounceTimer),this.loadingTimer!==null&&clearTimeout(this.loadingTimer),this.debounceTimer=this.loadingTimer=null}};function _u(o){return typeof o?.then=="function"}var Ku=75,Vu=/[\s.,;:!?)\]}>'"’”»)」』】、。,.:;!?]/,Uu=0,Co=class{constructor(e,t){this.host=e;this.entries=t.map(i=>{let r=null;return i.render!==!1&&(r=new Xe(e.root,`mbe-suggestion-menu ${i.className??""}`.trim(),"listbox"),r.el.setAttribute("aria-label",i.label??e.editor.labels.suggestions),r.el.setAttribute("aria-expanded","true"),r.onHighlight=s=>{this.session?.entry.menu!==r||this.active===s||(this.active=s,this.host.syncActiveDescendant(),this.report())}),{config:i,matcher:i.match??mr(i),triggers:typeof i.trigger=="string"?[i.trigger]:i.trigger,menu:r,listboxId:r?.el.id??`mbe-suggestion-${++Uu}`,source:new Mo}}),this.entries.length>0&&e.root.setAttribute("aria-autocomplete","list");let n=e.root.ownerDocument.defaultView;if(n&&this.entries.some(i=>i.menu)){let i=()=>this.schedule(),r=[n];n.visualViewport&&r.push(n.visualViewport);for(let s of r)for(let l of s===n?["resize"]:["resize","scroll"])s.addEventListener(l,i),this.cleanup.push(()=>s.removeEventListener(l,i))}}entries;session=null;dismissed=null;items=[];active=-1;message=null;side="below";frame=null;requestedComposing=!1;keysFrom=0;reported="";cleanup=[];get isActive(){return this.session!==null}get isOpen(){let e=this.session;return!e||!this.shown?!1:e.entry.menu?e.entry.menu.isOpen:!0}get activeDescendant(){return this.isOpen&&this.active>=0?`${this.session.entry.listboxId}-${this.active}`:null}get state(){let e=this.session;if(!e)return null;let{entry:t,blockId:n,cell:i,from:r,to:s,query:l}=e,a=u=>i?{blockId:n,offset:u,cell:i}:{blockId:n,offset:u},d=t.listboxId;return{name:t.config.name,query:l,range:{from:a(r),to:a(s)},items:this.items,active:this.active,loading:t.source.pending,listboxId:d,optionId:u=>`${d}-${u}`,anchorRect:()=>{if(this.session!==e)return null;let u=this.anchor();return u?new DOMRect(u.rect.left,u.rect.top,0,u.rect.bottom-u.rect.top):null}}}get shown(){return this.items.length>0||this.message!==null}get editor(){return this.host.editor}setConfigs(e){for(let t of e){let n=this.entries.find(i=>i.config.name===t.name);!n||n.config===t||(t.match!==n.config.match&&(n.matcher=t.match??mr(t)),n.config=t)}}typed(){if(this.entries.length===0||!this.editor.isEditable)return!1;let e=this.find(!1);if(!e)return!1;let t=this.dismissed;if(t&&t.blockId===e.blockId&&de(t.cell,e.cell)&&t.from===e.from)return!1;let n=this.session;return n&&n.entry===e.entry&&n.blockId===e.blockId&&de(n.cell,e.cell)&&n.from===e.from?!1:(this.close(),this.session={...e,explicit:!1},!0)}open(e){if(!this.editor.isEditable)return!1;let t=e===void 0?void 0:this.entries.find(i=>i.config.name===e);if(e!==void 0&&!t)return!1;let n=this.find(!0,t);return n?(this.close(),this.dismissed=null,this.session={...n,explicit:!0},this.host.refresh(),!0):!1}update(e){let t=this.dismissed;if(t){let s=e?this.find(!0):null;(!s||s.blockId!==t.blockId||!de(s.cell,t.cell)||s.from!==t.from)&&(this.dismissed=null)}let n=this.session;if(!n)return;let i=e&&this.editor.isEditable?this.find(n.explicit,n.entry):null;if(!(i&&i.blockId===n.blockId&&de(i.cell,n.cell)&&(i.from===n.from||i.text!==n.text))){this.close();return}this.session={...n,...i},this.refresh(),this.report()}close(){let e=this.session;e&&(e.entry.menu?.hide(),e.entry.menu&&(e.entry.menu.busy=!1),e.entry.source.cancel()),this.session=null,this.items=[],this.active=-1,this.message=null,this.requestedComposing=!1,this.side="below",this.keysFrom=0;for(let t of["aria-activedescendant","aria-controls","aria-haspopup"])this.host.root.removeAttribute(t);this.report()}move(e){!this.session||this.items.length===0||this.highlight(((this.active+e)%this.items.length+this.items.length)%this.items.length)}accept(e=this.active){let t=this.items[e];return!this.session||!t?!1:(this.pick(t),!0)}keydown(e){if(!this.isOpen)return!1;let t=!e.altKey&&!e.ctrlKey&&!e.metaKey&&!e.shiftKey;if(t&&e.key==="Escape")return this.dismiss(),!0;let n=
|
|
99
|
+
`,i);c<0&&(c=n.length);let h=t+16,f=Math.max(u,i-h);for(;f>u&&!/\s/.test(n[f-1]);)f--;let p=Math.min(c+e.length,i+e.length+h);for(let m of o(d,f,p)){if(m.from>=i||m.to<i+e.length)continue;let y=Mn(n,m.from,r,i);if(!y)return null;let g=n.slice(m.from+y.length,i);return g.length>t?null:{from:m.from,to:m.to-e.length,query:g}}return null}}function Mn(o,e,t,n=o.length){let i=null;for(let r of t)r&&e+r.length<=n&&o.startsWith(r,e)&&(!i||r.length>i.length)&&(i=r);return i}function zl(o,e){let t=0;for(let n of o){let i=t+n.text.length;if(e<i)return ct(n);t=i}return!1}var Mo=class{request=0;controller=null;debounceTimer=null;loadingTimer=null;waiting=!1;get pending(){return this.waiting}run(e,t,n=0){this.cancel();let i=this.request;this.waiting=!0,this.loadingTimer=setTimeout(()=>{this.loadingTimer=null,i===this.request&&t.loading()},300);let r=l=>{i===this.request&&(this.settle(),t.show(l))},s=()=>{this.debounceTimer=null;let l=new AbortController;this.controller=l;let a;try{a=e(l.signal)}catch{r(null);return}_u(a)?a.then(r,()=>r(null)):r(a)};n>0?this.debounceTimer=setTimeout(s,n):s()}cancel(){this.request++,this.controller?.abort(),this.settle()}settle(){this.waiting=!1,this.controller=null,this.debounceTimer!==null&&clearTimeout(this.debounceTimer),this.loadingTimer!==null&&clearTimeout(this.loadingTimer),this.debounceTimer=this.loadingTimer=null}};function _u(o){return typeof o?.then=="function"}var Ku=75,Vu=/[\s.,;:!?)\]}>'"’”»)」』】、。,.:;!?]/,Uu=0,Co=class{constructor(e,t){this.host=e;this.entries=t.map(i=>{let r=null;return i.render!==!1&&(r=new Xe(e.root,`mbe-suggestion-menu ${i.className??""}`.trim(),"listbox"),r.el.setAttribute("aria-label",i.label??e.editor.labels.suggestions),r.el.setAttribute("aria-expanded","true"),r.onHighlight=s=>{this.session?.entry.menu!==r||this.active===s||(this.active=s,this.host.syncActiveDescendant(),this.report())}),{config:i,matcher:i.match??mr(i),triggers:typeof i.trigger=="string"?[i.trigger]:i.trigger,menu:r,listboxId:r?.el.id??`mbe-suggestion-${++Uu}`,source:new Mo}}),this.entries.length>0&&e.root.setAttribute("aria-autocomplete","list");let n=e.root.ownerDocument.defaultView;if(n&&this.entries.some(i=>i.menu)){let i=()=>this.schedule(),r=[n];n.visualViewport&&r.push(n.visualViewport);for(let s of r)for(let l of s===n?["resize"]:["resize","scroll"])s.addEventListener(l,i),this.cleanup.push(()=>s.removeEventListener(l,i))}}entries;session=null;dismissed=null;items=[];active=-1;message=null;side="below";frame=null;requestedComposing=!1;keysFrom=0;reported="";cleanup=[];get isActive(){return this.session!==null}get isOpen(){let e=this.session;return!e||!this.shown?!1:e.entry.menu?e.entry.menu.isOpen:!0}get activeDescendant(){return this.isOpen&&this.active>=0?`${this.session.entry.listboxId}-${this.active}`:null}get state(){let e=this.session;if(!e)return null;let{entry:t,blockId:n,cell:i,from:r,to:s,query:l}=e,a=u=>i?{blockId:n,offset:u,cell:i}:{blockId:n,offset:u},d=t.listboxId;return{name:t.config.name,query:l,range:{from:a(r),to:a(s)},items:this.items,active:this.active,loading:t.source.pending,listboxId:d,optionId:u=>`${d}-${u}`,anchorRect:()=>{if(this.session!==e)return null;let u=this.anchor();return u?new DOMRect(u.rect.left,u.rect.top,0,u.rect.bottom-u.rect.top):null}}}get shown(){return this.items.length>0||this.message!==null}get editor(){return this.host.editor}setConfigs(e){for(let t of e){let n=this.entries.find(i=>i.config.name===t.name);!n||n.config===t||(t.match!==n.config.match&&(n.matcher=t.match??mr(t)),n.config=t)}}typed(){if(this.entries.length===0||!this.editor.isEditable)return!1;let e=this.find(!1);if(!e)return!1;let t=this.dismissed;if(t&&t.blockId===e.blockId&&de(t.cell,e.cell)&&t.from===e.from)return!1;let n=this.session;return n&&n.entry===e.entry&&n.blockId===e.blockId&&de(n.cell,e.cell)&&n.from===e.from?!1:(this.close(),this.session={...e,explicit:!1},!0)}open(e){if(!this.editor.isEditable)return!1;let t=e===void 0?void 0:this.entries.find(i=>i.config.name===e);if(e!==void 0&&!t)return!1;let n=this.find(!0,t);return n?(this.close(),this.dismissed=null,this.session={...n,explicit:!0},this.host.refresh(),!0):!1}update(e){let t=this.dismissed;if(t){let s=e?this.find(!0):null;(!s||s.blockId!==t.blockId||!de(s.cell,t.cell)||s.from!==t.from)&&(this.dismissed=null)}let n=this.session;if(!n)return;let i=e&&this.editor.isEditable?this.find(n.explicit,n.entry):null;if(!(i&&i.blockId===n.blockId&&de(i.cell,n.cell)&&(i.from===n.from||i.text!==n.text))){this.close();return}this.session={...n,...i},this.refresh(),this.report()}close(){let e=this.session;e&&(e.entry.menu?.hide(),e.entry.menu&&(e.entry.menu.busy=!1),e.entry.source.cancel()),this.session=null,this.items=[],this.active=-1,this.message=null,this.requestedComposing=!1,this.side="below",this.keysFrom=0;for(let t of["aria-activedescendant","aria-controls","aria-haspopup"])this.host.root.removeAttribute(t);this.report()}move(e){!this.session||this.items.length===0||this.highlight(((this.active+e)%this.items.length+this.items.length)%this.items.length)}accept(e=this.active){let t=this.items[e];return!this.session||!t?!1:(this.pick(t),!0)}keydown(e){if(!this.isOpen)return!1;let t=!e.altKey&&!e.ctrlKey&&!e.metaKey&&!e.shiftKey;if(t&&e.key==="Escape")return this.dismiss(),!0;let n=ie&&e.ctrlKey&&!e.altKey&&!e.metaKey&&!e.shiftKey,i=e.key,r=null;if(t&&i==="ArrowDown"?r="down":t&&i==="ArrowUp"?r="up":n&&i.toLowerCase()==="n"?r="down":n&&i.toLowerCase()==="p"?r="up":t&&i==="PageDown"?r="pageDown":t&&i==="PageUp"?r="pageUp":t&&(i==="Enter"||i==="Tab")&&(r="accept"),!r||Date.now()<this.keysFrom)return!1;if(this.items.length===0)return i==="Enter"&&this.close(),!1;let s=this.session.entry.menu;return r==="down"?this.move(1):r==="up"?this.move(-1):r==="accept"?this.accept():s?s.page(r==="pageDown"?1:-1):this.highlight(Math.max(0,Math.min(this.items.length-1,this.active+(r==="pageDown"?8:-8)))),!0}dismiss(){let e=this.session;e&&(this.dismissed={blockId:e.blockId,cell:e.cell,from:e.from}),this.close()}reset(){this.dismissed=null,this.close()}reposition(){this.session&&this.shown&&this.place()}destroy(){this.close(),this.frame!==null&&cancelAnimationFrame(this.frame);for(let e of this.cleanup)e();for(let{menu:e}of this.entries)e?.destroy()}schedule(){this.frame!==null||!this.shown||(this.frame=requestAnimationFrame(()=>{this.frame=null,this.reposition()}))}highlight(e){let t=this.session?.entry.menu;t?t.highlight(e):this.active!==e&&(this.active=e,this.host.syncActiveDescendant(),this.report())}report(){let e=this.session,t=e?`${e.entry.config.name}
|
|
100
100
|
${e.blockId}
|
|
101
101
|
${e.cell?.row},${e.cell?.col}
|
|
102
102
|
${e.from}
|
|
@@ -105,25 +105,25 @@ ${e.query}
|
|
|
105
105
|
${this.active}
|
|
106
106
|
${e.entry.source.pending}
|
|
107
107
|
${this.message}`:"";t===this.reported&&!(e&&this.itemsChanged)||(this.reported=t,this.itemsChanged=!1,this.host.changed(this.state))}itemsChanged=!1;find(e,t){let{doc:n,selection:i}=this.editor.state;if(!R(i))return null;let r=i.focus,s=n.blocks.find(p=>p.id===r.blockId);if(!s)return null;let l=So(n)&&s.type==="code"?s:null,a=l?this.editor.sourceSpans(l):null;if(!a&&Q(s))return null;let d=a??F(s,r.cell);if(!d)return null;let u=I(l?l.content:d),c=r.offset,h=a?"source":"rich",f=null;for(let p of t?[t]:this.entries){if(a&&p.config.richOnly)continue;let m=p.matcher({text:u,caret:c,content:d,triggers:p.triggers});if(!m||f&&m.from<=f.from||m.from<0||m.from>=c||m.to<c||m.to>u.length||!Mn(u,m.from,p.triggers,c)||m.query.length>(p.config.maxLength??30))continue;let y=zl(d,m.from),g=p.config.allow;(g?g({editor:this.editor,block:s,cell:r.cell,from:m.from,query:m.query,textBefore:u.slice(0,m.from),excluded:y,explicit:e,mode:h}):!y)&&(f={...m,entry:p,blockId:s.id,cell:r.cell,text:u,mode:h})}return f}refresh(){let e=this.session,{menu:t,source:n}=e.entry,i=e.query,r=this.editor.isComposing;if(e.requested===i&&!(this.requestedComposing&&!r)){this.shown&&this.place();return}this.session={...e,requested:i};let s=this.session,l=s.entry,a=()=>this.session?.entry===l&&this.session.requested===i,d=!0,u=h=>{if(!a())return;let f=l.config;t&&(t.busy=!1);let p=(h??[]).slice(0,f.limit??50);if(p.length===0&&r&&this.items.length>0)return this.report();let m=p.length===0&&(!f.emptyLabel||i.length>3);!m&&!d&&!this.isOpen&&(this.keysFrom=Date.now()+Ku);let y=this.items[this.active]?.id,g=y===void 0?-1:p.findIndex(b=>b.id===y),x=p.length===0?-1:Math.max(0,g);this.items=p,this.active=x,this.message=p.length===0&&!m?f.emptyLabel:null,this.itemsChanged=!0,t&&(m?t.hide():(t.setSections(Wu(p,i,b=>this.pick(b)),f.emptyLabel),t.highlight(x))),m?(this.host.root.removeAttribute("aria-haspopup"),this.host.root.removeAttribute("aria-controls"),this.host.syncActiveDescendant()):this.place(),this.report()},c=()=>{if(!a()||this.shown)return this.report();this.message=this.editor.labels.loading,t?.setMessage(this.message,"mbe-menu-loading"),this.place(),this.report()};this.requestedComposing=r,n.run(h=>l.config.items({query:i,editor:this.editor,explicit:s.explicit,composing:r,signal:h}),{show:u,loading:c},l.config.debounce??0),d=!1,n.pending&&t?.isOpen&&(t.busy=!0)}anchor(){let e=this.session,t=r=>e.cell?{blockId:e.blockId,offset:r,cell:e.cell}:{blockId:e.blockId,offset:r},n=e.from+(Mn(e.text,e.from,e.entry.triggers)?.length??1),i=this.editor.textRect(t(e.from),t(n));return Vl(i?.rect??null,this.editor.caretRect(),i?.rtl??!1,Ul(this.host.root))}place(){let e=this.session,{menu:t,listboxId:n}=e.entry,i=this.host.root,r=t?this.anchor():null;t&&r?t.popup.show(r.rect,this.side,4,r.align):t?.hide(),t&&(this.side=t.popup.side),this.isOpen?(i.setAttribute("aria-haspopup","listbox"),i.setAttribute("aria-controls",n)):(i.removeAttribute("aria-haspopup"),i.removeAttribute("aria-controls")),this.host.syncActiveDescendant()}pick(e){this.editor.isComposing&&this.host.endComposition();let t=this.session;if(!t)return;let{entry:n,blockId:i,cell:r,from:s,to:l,query:a,text:d,mode:u}=t,c=y=>r?{blockId:i,offset:y,cell:r}:{blockId:i,offset:y},h={from:c(s),to:c(l)};if(this.close(),n.config.command){n.config.command({editor:this.editor,range:h,item:e,query:a});return}let f=d[l],m=f!==void 0&&!(f===`
|
|
108
|
-
`&&u==="source")&&Vu.test(f)?"":" ";this.editor.commands.insertText(n.triggers[0]+(e.value??e.id??e.label)+m,{range:h})}};function zu(o,e){return o.id??`${e}`}function Wu(o,e,t){let n=[];return o.forEach((i,r)=>{let s=n[n.length-1];(!s||i.section!==s.title)&&n.push(s={title:i.section,items:[]}),s.items.push({id:zu(i,r),label:i.label,detail:i.detail,icon:i.icon,mark:e,run:()=>t(i)})}),n}var kt=class{constructor(e,t,n,i){this.element=e;this.render=n;this.onRemove=i;this.blockId=t,this.paint()}blockId;offset=0;isRemoved=!1;cleanup=null;update(e){this.isRemoved||(e&&(this.render=e),this.paint())}remove(){this.isRemoved||(this.isRemoved=!0,this.cleanup?.(),this.cleanup=null,this.element.remove(),this.onRemove(this))}paint(){this.cleanup?.(),this.element.replaceChildren();let e=this.render(this.element);this.cleanup=typeof e=="function"?e:null}};function Wl(o,e,t){let n=o.split(/-(?!$)/),i=n.pop(),r=!1,s=!1,l=!1,a=!1;for(let d of n)if(/^(cmd|meta|m)$/i.test(d))a=!0;else if(/^a(lt)?$/i.test(d))s=!0;else if(/^(c|ctrl|control)$/i.test(d))r=!0;else if(/^s(hift)?$/i.test(d))l=!0;else if(/^mod$/i.test(d))t?a=!0:r=!0;else throw new Error(`Unrecognized modifier name: ${d}`);return i==="Space"&&(i=" "),{key:i.length===1?i.toLowerCase():i,ctrl:r,alt:s,shift:l,meta:a,handler:e}}function Qu(o){let e=/^(?:Key([A-Z])|Digit([0-9]))$/.exec(o.code);return e?(e[1]??e[2]).toLowerCase():null}function Ql(o,e){if(o.ctrl!==e.ctrlKey||o.alt!==e.altKey||o.meta!==e.metaKey)return!1;let t=e.key.length===1?e.key.toLowerCase():e.key;return t===o.key&&(o.shift===e.shiftKey||e.key.length===1&&!o.shift&&/[^a-z0-9]/.test(t))?!0:o.shift===e.shiftKey&&Qu(e)===o.key}var Zl=[["textStyle"],["bold","italic","strike","inlineCode","link"],["quote","codeBlock","list"]],Xl=Zl.flat(),ju=["textStyle","quote","codeBlock","list"],se=o=>`${re?"Meta":"Control"}+${o}`,jl={bold:{mark:"bold",className:"mbe-tool-bold",keys:se("B")},italic:{mark:"italic",className:"mbe-tool-italic",keys:se("I")},strike:{mark:"strike",className:"mbe-tool-strike",keys:se("Shift+X")},inlineCode:{mark:"code",className:"mbe-tool-code",keys:se("E")}},Gu=[{id:"text",spec:{type:"paragraph"},keys:se("Alt+0")},{id:"heading1",spec:{type:"heading",level:1},keys:se("Alt+1")},{id:"heading2",spec:{type:"heading",level:2},keys:se("Alt+2")},{id:"heading3",spec:{type:"heading",level:3},keys:se("Alt+3")},{id:"heading4",spec:{type:"heading",level:4},keys:se("Alt+4")}],gr=[{id:"bulletList",listType:"bullet",keys:se("Shift+8")},{id:"numberedList",listType:"ordered",keys:se("Shift+7")},{id:"todoList",listType:"task",keys:se("Shift+9")}],Zu=500,Xu=300;function Gl(o){let e=zn(o.state),t=r=>r.type==="paragraph"?"text":r.type==="heading"&&r.level<=4?`heading${r.level}`:null,n=r=>{let s=e[0]?r(e[0]):null;return e.every(l=>r(l)===s)?s:null},i=e.length>0;return{style:n(t),allHeadings:i&&e.every(r=>r.type==="heading"),listType:n(r=>r.type==="listItem"?r.listType:null),allListItems:i&&e.every(r=>r.type==="listItem"),allQuotes:i&&e.every(r=>M(r)!==null)}}var Io=class{constructor(e,t=Xl){this.editor=e;let n=e.root.ownerDocument;this.popup=new we(e.root,"mbe-toolbar");let i=this.popup.el;i.setAttribute("role","toolbar"),this.tooltip=new we(e.root,"mbe-tooltip"),this.tooltip.el.setAttribute("aria-hidden","true");let r=e.labels,s=(d,u,c,h,f)=>{let p=n.createElement("button");p.type="button",p.className=`mbe-tool ${c}`;let m=X(n,e.icon(u));return m&&p.appendChild(m),p.setAttribute("aria-label",r[d==="codeBlock"?"code":d]),h&&p.setAttribute("aria-keyshortcuts",h),p.addEventListener("click",f),p.addEventListener("pointerenter",y=>{y.pointerType==="mouse"&&this.scheduleTooltip(p)}),p.addEventListener("pointerleave",()=>this.hideTooltip()),this.buttons.set(d,p),p},l=(d,u,c)=>{let h=s(d,d==="list"?"bulletList":"text",u,null,()=>this.toggleMenu(g));h.classList.add("mbe-tool-menu"),h.setAttribute("aria-haspopup","menu"),h.setAttribute("aria-expanded","false");let f=n.createElement("span");f.className="mbe-tool-icon",f.append(...h.childNodes);let p=n.createElement("span");p.className="mbe-tool-chevron";let m=X(n,e.icon("chevronDown"));m&&p.appendChild(m),h.append(f,p);let y=new Xe(e.root,c,"menu");y.el.setAttribute("aria-label",r[d]),y.checkIcon=e.icon("selected"),y.onPicked=()=>this.closeMenu(),y.onHighlight=()=>this.onMenuChange?.(),h.setAttribute("aria-controls",y.el.id);let g={button:h,icon:f,menu:y,iconName:d==="list"?"bulletList":"text"};return g},a=new Set(t);for(let d of Zl){let u=d.filter(c=>a.has(c));if(u.length!==0){if(this.buttons.size>0){let c=n.createElement("span");c.className="mbe-tool-sep",i.appendChild(c),this.separators.push(c)}for(let c of u){let h=jl[c],f;h?(f=s(c,c,h.className,h.keys,()=>this.apply(p=>We(p,h.mark))),f.setAttribute("aria-pressed","false")):c==="link"?(f=s(c,"link","mbe-tool-link",se("K"),()=>e.openLinkPopover()),f.setAttribute("aria-pressed","false")):c==="quote"?(f=s(c,"quote","mbe-tool-quote",null,()=>this.apply(p=>ht(p,{type:"quote"}))),f.setAttribute("aria-pressed","false")):c==="codeBlock"?f=s(c,"code","mbe-tool-code-block",se("Alt+C"),()=>this.apply(p=>ht(p,{type:"code"}))):c==="textStyle"?(this.textStyle=l(c,"mbe-tool-text-style","mbe-text-style-menu"),f=this.textStyle.button):(this.list=l("list","mbe-tool-list","mbe-list-menu"),f=this.list.button),i.appendChild(f)}}}}popup;tooltip;buttons=new Map;separators=[];textStyle=null;list=null;openTrigger=null;tooltipTimer;tooltipHiddenAt=0;onMenuChange=null;get isOpen(){return this.popup.isOpen}get menu(){return this.openTrigger?.menu??null}contains(e){return this.popup.el.contains(e)||!!this.openTrigger?.menu.el.contains(e)}show(){let e=this.editor.selectionRect();if(!e)return this.hide();this.update();let t=this.editor.root.ownerDocument.defaultView;this.popup.el.style.maxWidth=`${Math.max(xo(t).width-16,0)}px`,this.popup.show(e,Ft()?"below":"above",8),this.openTrigger&&this.placeMenu(this.openTrigger)}hide(){this.closeMenu(),this.hideTooltip(),this.popup.hide()}closeMenu(){let e=this.openTrigger;e&&(this.openTrigger=null,e.menu.hide(),e.button.classList.remove("mbe-open"),e.button.setAttribute("aria-expanded","false"),this.onMenuChange?.())}destroy(){clearTimeout(this.tooltipTimer),this.popup.destroy(),this.tooltip.destroy(),this.textStyle?.menu.destroy(),this.list?.menu.destroy()}apply(e){this.hideTooltip(),this.editor.run(e),this.isOpen&&this.update()}update(){let e=this.editor;for(let[a,d]of this.buttons){let u=jl[a];u&&br(d,e.isActive(u.mark))}let t=this.buttons.get("link");t&&br(t,e.isActive("link"));let{anchor:n,focus:i}=e.state.selection,r=!!i.cell&&n.blockId===i.blockId;for(let a of ju)this.buttons.get(a)?.toggleAttribute("hidden",r);r&&this.closeMenu();for(let a of this.separators){let d=(u,c)=>{for(let h=u;h&&!h.classList.contains("mbe-tool-sep");h=h[c])if(!h.hidden)return!0;return!1};a.hidden=!d(a.previousElementSibling,"previousElementSibling")||!d(a.nextElementSibling,"nextElementSibling")}if(r)return;let s=Gl(e),l=this.buttons.get("quote");if(l&&br(l,s.allQuotes),this.textStyle&&(this.setTriggerIcon(this.textStyle,s.style??"text"),this.textStyle.button.classList.toggle("mbe-active",s.allHeadings)),this.list){let a=gr.find(d=>d.listType===s.listType);this.setTriggerIcon(this.list,a?.id??"bulletList"),this.list.button.classList.toggle("mbe-active",s.allListItems)}}setTriggerIcon(e,t){if(e.iconName===t)return;e.iconName=t;let n=X(e.icon.ownerDocument,this.editor.icon(t));e.icon.replaceChildren(...n?[n]:[])}toggleMenu(e){let t=this.openTrigger===e;if(this.closeMenu(),this.hideTooltip(),t)return;let n=Gl(this.editor),i=this.editor.labels,r,s;e===this.textStyle?(s=n.style??void 0,r=Gu.map(({id:l,spec:a,keys:d})=>({id:l,label:i[l],icon:this.editor.icon(l),labelClass:`mbe-menu-${l}`,keyShortcuts:d,checked:l===s,run:()=>l!==s&&this.apply(u=>Ge(u,a))}))):(s=gr.find(l=>l.listType===n.listType)?.id,r=gr.map(({id:l,listType:a,keys:d})=>({id:l,label:i[l],icon:this.editor.icon(l),keyShortcuts:d,checked:l===s,run:()=>this.apply(u=>ht(u,{type:"listItem",listType:a}))}))),e.menu.setSections([{items:r}]),s&&e.menu.highlightId(s),this.openTrigger=e,e.button.classList.add("mbe-open"),e.button.setAttribute("aria-expanded","true"),this.placeMenu(e),this.onMenuChange?.()}placeMenu(e){let t=this.editor.root.ownerDocument.defaultView.getComputedStyle(this.editor.root).direction==="rtl";e.menu.popup.show(e.button.getBoundingClientRect(),this.popup.side,4,t?"end":"start")}scheduleTooltip(e){if(clearTimeout(this.tooltipTimer),this.openTrigger)return;let t=Date.now()-this.tooltipHiddenAt<Xu?0:Zu;this.tooltipTimer=setTimeout(()=>{if(!this.isOpen||this.openTrigger||!e.isConnected)return;this.tooltip.el.textContent=e.getAttribute("aria-label"),this.tooltip.show(e.getBoundingClientRect(),this.popup.side,6);let n=e.getBoundingClientRect(),i=this.tooltip.el.getBoundingClientRect(),r=this.editor.root.ownerDocument.defaultView,s=Math.max(8,Math.min(n.left+(n.width-i.width)/2,r.innerWidth-i.width-8));this.tooltip.el.style.left=`${parseFloat(this.tooltip.el.style.left)+s-i.left}px`},t)}hideTooltip(){clearTimeout(this.tooltipTimer),this.tooltip.isOpen&&(this.tooltip.hide(),this.tooltipHiddenAt=Date.now())}};function br(o,e){o.classList.toggle("mbe-active",e),o.setAttribute("aria-pressed",String(e))}var Lo=class{popup;input;removeButton;done=null;constructor(e){let t=e.root.ownerDocument;this.popup=new we(e.root,"mbe-input-popover"),this.input=t.createElement("input"),this.input.className="mbe-popover-input",this.removeButton=t.createElement("button"),this.removeButton.type="button",this.removeButton.className="mbe-tool",this.popup.el.append(this.input,this.removeButton),this.input.addEventListener("keydown",n=>{n.key==="Enter"?(n.preventDefault(),this.finish(this.input.value.trim())):n.key==="Escape"&&(n.preventDefault(),this.finish(null))}),this.input.addEventListener("blur",()=>{setTimeout(()=>{this.popup.isOpen&&t.activeElement!==this.input&&this.close()},150)}),this.removeButton.addEventListener("click",()=>this.finish(""))}get isOpen(){return this.popup.isOpen}contains(e){return this.popup.el.contains(e)}open(e,t,n){this.done=n,this.input.type=t.type,this.input.value=t.value,this.input.placeholder=t.placeholder,this.input.setAttribute("aria-label",t.label),this.removeButton.textContent=t.removeLabel??"",this.removeButton.hidden=!t.removeLabel||t.value==="",this.popup.show(e,"below"),this.input.focus(),this.input.select()}close(){this.done=null,this.popup.hide()}finish(e){let t=this.done;this.done=null,this.popup.hide(),t?.(e)}destroy(){this.popup.destroy()}};var Yl=24,Yu=8,Jl=16,Bo=class{constructor(e){this.editor=e;let t=e.root,n=t.ownerDocument,i=e.labels;this.frame=n.createElement("div"),this.frame.className="mbe-ui mbe-image-frame",this.frame.hidden=!0;let r=l=>{let a=n.createElement("div");return a.className=`mbe-resize-handle mbe-resize-handle-${l}`,a.dataset.side=l,a.title=i.resizeImage,a.setAttribute("aria-hidden","true"),this.frame.appendChild(a),a};this.handles={left:r("left"),right:r("right")};let s=(l,a,d)=>{l.addEventListener(a,d),this.cleanup.push(()=>l.removeEventListener(a,d))};s(t,"mouseover",l=>this.onOver(l)),s(t,"mouseleave",()=>this.scheduleUnhover());for(let l of[this.frame,this.handles.left,this.handles.right])s(l,"mouseenter",()=>this.cancelUnhover()),s(l,"mouseleave",()=>this.scheduleUnhover());s(this.frame,"mousedown",l=>l.preventDefault());for(let l of["left","right"])s(this.handles[l],"pointerdown",a=>this.startDrag(a,l));s(this.frame,"pointermove",l=>this.moveDrag(l)),s(this.frame,"pointerup",l=>this.endDrag(l,!0)),s(this.frame,"pointercancel",l=>this.endDrag(l,!1)),s(this.frame,"lostpointercapture",l=>this.endDrag(l,!0))}frame;handles;hovered=null;selected=null;framed=null;drag=null;hideTimer=null;cleanup=[];contains(e){return this.frame.contains(e)}get isDragging(){return this.drag!==null}update(e){this.selected=e,this.drag||this.placeFrame()}reposition(){this.drag||(this.selected&&!this.selected.el.isConnected&&(this.selected=null),this.placeFrame())}hide(){this.drag||(this.hovered=null,this.selected=null,this.placeFrame())}destroy(){this.cancelUnhover();for(let e of this.cleanup)e();this.frame.remove()}onOver(e){if(this.drag||!this.editor.isEditable)return;let t=e.target,n=t instanceof HTMLImageElement&&t.classList.contains("mbe-image")?t:null;n?(this.cancelUnhover(),this.hovered=n,this.placeFrame()):this.hovered&&this.scheduleUnhover()}scheduleUnhover(){this.cancelUnhover(),this.hideTimer=setTimeout(()=>{this.hideTimer=null,this.hovered=null,this.placeFrame()},250)}cancelUnhover(){this.hideTimer!==null&&clearTimeout(this.hideTimer),this.hideTimer=null}placeFrame(){let e=this.editor.isEditable,t=this.hovered?.isConnected?this.hovered:null,n=this.drag?.el??(e?t??this.selected?.el??null:null),i=n instanceof HTMLImageElement&&n.isConnected?n:null,r=i?.getBoundingClientRect();if(!i||!r||r.width===0||r.height===0){this.frame.hidden=!0,this.framed=null;return}this.framed!==i&&(this.framed=i,i.complete||i.addEventListener("load",()=>this.framed===i&&this.placeFrame(),{once:!0}));let s=yr(i,r);if(!s||s.width<Math.min(r.width,Jl)||s.height<Math.min(r.height,Jl)){this.frame.hidden=!0;return}this.frame.hidden=!1;let l=$t(this.frame,this.editor.root);this.frame.style.left=`${Math.round(s.left-l.x)}px`,this.frame.style.top=`${Math.round(s.top-l.y)}px`,this.frame.style.width=`${Math.round(s.width)}px`,this.frame.style.height=`${Math.round(s.height)}px`,this.frame.classList.toggle("mbe-image-frame-small",s.width<64||s.height<40)}startDrag(e,t){let n=this.framed;if(!n||this.drag||e.button!==0||!this.editor.isEditable)return;let i=this.editor.imageTarget(n);if(!i)return;e.preventDefault(),this.handles[t].setPointerCapture?.(e.pointerId);let r=n.getBoundingClientRect().width,s=Array.from(this.editor.root.querySelectorAll("img.mbe-image")).filter(d=>d!==n).map(d=>d.getBoundingClientRect().width).filter(d=>d>0),l=Math.max(Yl,Ju(n)),a=n.closest(".mbe-image-block")?.getAttribute("data-align")==="center"?2:1;this.drag={el:n,side:t,pointer:e.pointerId,startX:e.clientX,startWidth:r,max:l,snaps:[...s,l],width:r,factor:a},this.frame.classList.add("mbe-image-resizing"),this.editor.selectImageAt(i.pos)}moveDrag(e){let t=this.drag;if(!t||e.pointerId!==t.pointer)return;e.preventDefault();let n=(e.clientX-t.startX)*(t.side==="right"?1:-1)*t.factor,i=Math.min(t.max,Math.max(Yl,t.startWidth+n)),r=t.snaps.find(s=>Math.abs(s-i)<=Yu);r!==void 0&&(i=Math.min(t.max,r)),t.width=Math.round(i),t.el.style.width=`${t.width}px`,t.el.style.height="auto",this.placeFrame()}endDrag(e,t){let n=this.drag;if(!n||e.pointerId!==n.pointer)return;this.drag=null,this.frame.classList.remove("mbe-image-resizing"),this.handles[n.side].releasePointerCapture?.(e.pointerId),n.el.style.width="",n.el.style.height="";let i=n.el.isConnected?this.editor.imageTarget(n.el):null;t&&i&&Math.round(n.width)!==Math.round(n.startWidth)?this.editor.updateImageAt(i.pos,{width:n.width}):this.editor.refreshImageUi()}};function yr(o,e){let t=o.ownerDocument,n=t.defaultView,i=e.left,r=e.top,s=e.right,l=e.bottom,a=(d,u,c,h)=>{i=Math.max(i,d),r=Math.max(r,u),s=Math.min(s,c),l=Math.min(l,h)};for(let d=o.parentElement;d&&d!==t.body&&d!==t.documentElement;d=d.parentElement){let u=n?.getComputedStyle(d),c=d.scrollHeight>d.clientHeight&&/auto|scroll|overlay/.test(u?.overflowY??""),h=d.scrollWidth>d.clientWidth&&/auto|scroll|overlay/.test(u?.overflowX??"");if(!c&&!h)continue;let f=d.getBoundingClientRect(),p=f.left+d.clientLeft,m=f.top+d.clientTop;a(p,m,p+d.clientWidth,m+d.clientHeight)}return a(0,0,t.documentElement.clientWidth,t.documentElement.clientHeight),s<=i||l<=r?null:{left:i,top:r,width:s-i,height:l-r}}function Ju(o){let e=o.parentElement,t=o.ownerDocument.defaultView;for(;e&&t.getComputedStyle(e).display.startsWith("inline");)e=e.parentElement;if(!e)return o.getBoundingClientRect().width;let n=t.getComputedStyle(e);return e.clientWidth-parseFloat(n.paddingLeft||"0")-parseFloat(n.paddingRight||"0")}var eh=[{align:"left",icon:"imageAlignLeft"},{align:"center",icon:"imageAlignCenter"},{align:"right",icon:"imageAlignRight"}],Ao=class{constructor(e,t){this.editor=e;let n=e.root.ownerDocument,i=e.labels;this.popup=new we(e.root,"mbe-image-toolbar");let r=this.popup.el;r.setAttribute("role","toolbar"),r.setAttribute("aria-label",i.imageSection);let s=(a,d,u)=>{let c=n.createElement("button");c.type="button",c.className=`mbe-tool ${d}`;let h=X(n,e.icon(a));return h&&c.appendChild(h),c.setAttribute("aria-label",i[a]),c.title=i[a],c.addEventListener("click",()=>{this.target&&u()}),c};for(let{align:a,icon:d}of eh){let u=s(d,`mbe-tool-${d}`,()=>t.align(this.target,a));u.setAttribute("aria-pressed","false"),this.alignButtons.push({align:a,button:u}),r.appendChild(u)}this.alignSep=n.createElement("span"),this.alignSep.className="mbe-tool-sep",r.appendChild(this.alignSep),r.appendChild(s("altText","mbe-tool-altText",()=>t.altText(this.target))),this.resetButton=s("resetImageSize","mbe-tool-resetImageSize",()=>t.resetSize(this.target)),r.appendChild(this.resetButton);let l=n.createElement("span");l.className="mbe-tool-sep",r.appendChild(l),r.appendChild(s("delete","mbe-tool-delete",()=>t.remove(this.target)))}popup;alignButtons=[];alignSep;resetButton;target=null;get isOpen(){return this.popup.isOpen}contains(e){return this.popup.el.contains(e)}show(e,t){let n=yr(e.el,e.el.getBoundingClientRect());if(!n)return this.hide();this.target=e;let i=e.image.align??"left";for(let{align:d,button:u}of this.alignButtons)u.hidden=!t,u.classList.toggle("mbe-active",t&&d===i),u.setAttribute("aria-pressed",String(t&&d===i));this.alignSep.hidden=!t,this.resetButton.hidden=!e.image.width;let{left:r,top:s,width:l,height:a}=n;this.popup.show({left:r,top:s,right:r+l,bottom:s+a},Ft()?"below":"above",8)}hide(){this.target=null,this.popup.hide()}destroy(){this.popup.destroy()}};var W="data-block-id";function Cr(o){return`code:${o.trim().toLowerCase()}`}var Kt="data-content",In="mbe-source-text",Ir="mbe-source-line",$o="data-mbe-fill",xr="data-mbe-pending",ia="data-atom",ra="data-list-start",th="data-quote-joined",nh="data-quote-continued",oh="data-section-hidden",ih="data-collapsed",rh="data-in-section",sh={bold:["strong"],italic:["em"],strike:["s"],code:["code"],html:["span","mbe-raw-html"]},lh={};function qe(...o){return o.filter(Boolean).join(" ")}function ah(o,e){let t=ch(o,e);return Wt(o)&&o.quote?qe(t,e.blockquote)||void 0:t}function ch(o,e){switch(o.type){case"paragraph":return e.paragraph;case"heading":return e.heading?.[o.level];case"quote":return e.blockquote;case"listItem":return e[`${o.listType}List`];case"code":return e.codeBlock;case"table":return e.table;case"raw":return e.raw;case"divider":return;case"details":return e.details;case"detailsEnd":return}}var Ln=o=>{let e=o.replace(/^[\u0000- ]+|[\u0000- ]+$/g,"").replace(/[\t\n\r]/g,""),t=/^([a-z][a-z0-9+.-]*):/i.exec(e);return t?["http","https","mailto","tel"].includes(t[1].toLowerCase())?e:null:e},Je="block";function Cn(o,e){return o?`${o.row}:${o.col}@${e}`:`@${e}`}function Lr(o,e){if(o===e)return!0;if(!o||!e||o.size!==e.size)return!1;for(let t of o)if(!e.has(t))return!1;return!0}function Br(o){return`callout${o[0].toUpperCase()}${o.slice(1)}`}function dh(o,e){let t=Y(e);if(t===null)return;o.classList.add("mbe-image-block");let n=C(e.content,t,t+1)[0]?.image?.align;n&&(o.dataset.align=n)}function uh(o,e,t,n){let i=t.doc,r=i.createElement("blockquote");r.dir="auto",r.dataset.callout=e;let s=i.createElement("div");return s.className="mbe-content",vt(s,o.content,t,o,n),r.append(No(e,t),s),r}function No(o,e){let t=Br(o),n=e.doc.createElement("div");n.className="mbe-callout-icon",n.contentEditable="false",n.setAttribute("role","img"),n.setAttribute("aria-label",e.labels[t]);let i=e.icon?.(t);return i&&n.appendChild(i),n}function sa(o,e,t){let{doc:n,labels:i}=e,r=(c,h)=>{let f=n.createElement(c);return h&&(f.className=h),f},s,l=t?.has(Je)||e.source?null:e.blockView?.(o);if(l)s=r("div","mbe-view-block mbe-block-view"),s.contentEditable="false",s.appendChild(l);else switch(o.type){case"paragraph":case"heading":case"quote":if(o.type==="quote"&&o.callout){s=uh(o,o.callout,e,t);break}s=r(o.type==="paragraph"?"p":o.type==="quote"?"blockquote":`h${o.level}`),s.dir="auto",vt(s,o.content,e,o,t),o.type==="paragraph"&&dh(s,o);break;case"listItem":{s=r(o.quote?"blockquote":"div"),o.callout&&(s.dataset.callout=o.callout,s.appendChild(No(o.callout,e))),s.dir="auto",s.dataset.listType=o.listType,s.dataset.indent=String(o.indent);let c=r("span","mbe-marker");if(c.contentEditable="false",o.listType==="task"){let f=r("input","mbe-checkbox");f.type="checkbox",f.tabIndex=-1,f.checked=o.checked,c.appendChild(f)}let h=r("div","mbe-content");vt(h,o.content,e,o,t),s.append(c,h);break}case"code":{if(e.source){s=r("div"),s.spellcheck=!1;let y=r("div",In);y.setAttribute(Kt,""),aa(y,o,e),s.appendChild(y);break}let c=t?.has(Je)?null:e.view?.(Cr(o.language),I(o.content),"block");if(c){s=c,s.classList.add("mbe-view-block"),s.contentEditable="false",s.dataset.language=o.language;break}s=r("div"),s.dataset.language=o.language;let h=r("div","mbe-code-bar");h.contentEditable="false";let f=r("input","mbe-code-lang");f.value=o.language,f.spellcheck=!1,f.setAttribute("aria-label",i.language),f.placeholder=i.plainText,h.appendChild(f),e.codeCopy&&h.appendChild(bh(e));let p=r("pre"),m=r("code");m.spellcheck=!1,vt(m,o.content,e,o,void 0,void 0,!1),p.appendChild(m),s.append(h,p);break}case"raw":{let c=I(o.content),h=t?.has(Je)===!0,f=o.syntax==="html"&&!h&&e.views?.has("html")?tl(c,e.markdown):null,p=o.syntax==="footnote"&&!h&&e.views?.has("footnote")?da(c):null,m=o.syntax==="markdown"&&!h&&e.views?.get("markdown")===Nr,y=h?null:m?wh(c,e):p?ua(p,e):e.view?.(o.syntax,f?f.html:c,"block");y?(s=y,s.classList.add("mbe-view-block"),o.syntax==="html"&&s.classList.add("mbe-view-html"),s.contentEditable="false",f&&Ar(s,f.slots.length,g=>yh(f.slots[g],e),e.doc)):(s=r("pre"),s.spellcheck=!1,vt(s,o.content,e,o,void 0,void 0,!1)),s.dataset.syntax=o.syntax;break}case"divider":s=r("div"),s.contentEditable="false",s.appendChild(r("hr",e.classes.divider));break;case"details":{s=r("div"),s.dir="auto";let c=r("span","mbe-details-marker");c.contentEditable="false";let h=r("button","mbe-details-toggle");h.type="button",h.tabIndex=-1;let f=e.icon?.("chevronRight");f&&h.appendChild(f),c.appendChild(h);let p=r("div","mbe-content");vt(p,o.content,e,o,void 0,void 0,!1),s.append(c,p);break}case"detailsEnd":s=r("div"),s.contentEditable="false";break;case"table":{s=r("div"),s.contentEditable="false";let c=r("table"),h=r("tbody");o.rows.forEach((f,p)=>{let m=r("tr");f.forEach((y,g)=>{let x=r(p===0?"th":"td");x.contentEditable="true",x.dir="auto",x.dataset.row=String(p),x.dataset.col=String(g);let b=o.align[g];b&&(x.style.textAlign=b),vt(x,y,e,o,t,{row:p,col:g}),m.appendChild(x)}),h.appendChild(m)}),c.appendChild(h),s.appendChild(c);break}}if(!l&&!e.source&&o.type!=="listItem"&&Wt(o)&&o.quote){let c=s;s=r("blockquote"),o.type==="heading"&&(s.dir="auto"),o.callout&&(s.dataset.callout=o.callout,s.appendChild(No(o.callout,e))),c.classList.add("mbe-content"),c.contentEditable==="false"&&(s.contentEditable="false"),c.classList.contains("mbe-view-block")&&s.classList.add("mbe-view-block");for(let h of["language","syntax"])c.dataset[h]!==void 0&&(s.dataset[h]=c.dataset[h]);s.appendChild(c)}s.setAttribute(W,o.id);let a=o.type==="listItem"?"list-item":o.type==="detailsEnd"?"details-end":o.type==="code"&&e.source?"source":o.type,d=`${s.className?s.className+" ":""}mbe-block mbe-${a}`;o.type==="listItem"&&o.checked&&(d+=" mbe-checked"),"content"in o&&B(o.content)===0&&(d+=" mbe-empty");let u=ah(o,e.classes);return u&&(d+=" "+u),s.className=d,s}var wr=new WeakMap,hh=[];function fh(o,e){let t=e.map((s,l)=>({d:s,index:l})).filter(s=>s.d.to>s.d.from).sort((s,l)=>s.d.from-l.d.from),n=0,i=[],r=[];for(let s=0;;){let l=o.indexOf(`
|
|
108
|
+
`&&u==="source")&&Vu.test(f)?"":" ";this.editor.commands.insertText(n.triggers[0]+(e.value??e.id??e.label)+m,{range:h})}};function zu(o,e){return o.id??`${e}`}function Wu(o,e,t){let n=[];return o.forEach((i,r)=>{let s=n[n.length-1];(!s||i.section!==s.title)&&n.push(s={title:i.section,items:[]}),s.items.push({id:zu(i,r),label:i.label,detail:i.detail,icon:i.icon,mark:e,run:()=>t(i)})}),n}var kt=class{constructor(e,t,n,i){this.element=e;this.render=n;this.onRemove=i;this.blockId=t,this.paint()}blockId;offset=0;isRemoved=!1;cleanup=null;update(e){this.isRemoved||(e&&(this.render=e),this.paint())}remove(){this.isRemoved||(this.isRemoved=!0,this.cleanup?.(),this.cleanup=null,this.element.remove(),this.onRemove(this))}paint(){this.cleanup?.(),this.element.replaceChildren();let e=this.render(this.element);this.cleanup=typeof e=="function"?e:null}};function Wl(o,e,t){let n=o.split(/-(?!$)/),i=n.pop(),r=!1,s=!1,l=!1,a=!1;for(let d of n)if(/^(cmd|meta|m)$/i.test(d))a=!0;else if(/^a(lt)?$/i.test(d))s=!0;else if(/^(c|ctrl|control)$/i.test(d))r=!0;else if(/^s(hift)?$/i.test(d))l=!0;else if(/^mod$/i.test(d))t?a=!0:r=!0;else throw new Error(`Unrecognized modifier name: ${d}`);return i==="Space"&&(i=" "),{key:i.length===1?i.toLowerCase():i,ctrl:r,alt:s,shift:l,meta:a,handler:e}}function Qu(o){let e=/^(?:Key([A-Z])|Digit([0-9]))$/.exec(o.code);return e?(e[1]??e[2]).toLowerCase():null}function Ql(o,e){if(o.ctrl!==e.ctrlKey||o.alt!==e.altKey||o.meta!==e.metaKey)return!1;let t=e.key.length===1?e.key.toLowerCase():e.key;return t===o.key&&(o.shift===e.shiftKey||e.key.length===1&&!o.shift&&/[^a-z0-9]/.test(t))?!0:o.shift===e.shiftKey&&Qu(e)===o.key}var Zl=[["textStyle"],["bold","italic","strike","inlineCode","link"],["quote","codeBlock","list"]],Xl=Zl.flat(),ju=["textStyle","quote","codeBlock","list"],re=o=>`${ie?"Meta":"Control"}+${o}`,jl={bold:{mark:"bold",className:"mbe-tool-bold",keys:re("B")},italic:{mark:"italic",className:"mbe-tool-italic",keys:re("I")},strike:{mark:"strike",className:"mbe-tool-strike",keys:re("Shift+X")},inlineCode:{mark:"code",className:"mbe-tool-code",keys:re("E")}},Gu=[{id:"text",spec:{type:"paragraph"},keys:re("Alt+0")},{id:"heading1",spec:{type:"heading",level:1},keys:re("Alt+1")},{id:"heading2",spec:{type:"heading",level:2},keys:re("Alt+2")},{id:"heading3",spec:{type:"heading",level:3},keys:re("Alt+3")},{id:"heading4",spec:{type:"heading",level:4},keys:re("Alt+4")}],gr=[{id:"bulletList",listType:"bullet",keys:re("Shift+8")},{id:"numberedList",listType:"ordered",keys:re("Shift+7")},{id:"todoList",listType:"task",keys:re("Shift+9")}],Zu=500,Xu=300;function Gl(o){let e=zn(o.state),t=r=>r.type==="paragraph"?"text":r.type==="heading"&&r.level<=4?`heading${r.level}`:null,n=r=>{let s=e[0]?r(e[0]):null;return e.every(l=>r(l)===s)?s:null},i=e.length>0;return{style:n(t),allHeadings:i&&e.every(r=>r.type==="heading"),listType:n(r=>r.type==="listItem"?r.listType:null),allListItems:i&&e.every(r=>r.type==="listItem"),allQuotes:i&&e.every(r=>M(r)!==null)}}var Io=class{constructor(e,t=Xl){this.editor=e;let n=e.root.ownerDocument;this.popup=new we(e.root,"mbe-toolbar");let i=this.popup.el;i.setAttribute("role","toolbar"),this.tooltip=new we(e.root,"mbe-tooltip"),this.tooltip.el.setAttribute("aria-hidden","true");let r=e.labels,s=(d,u,c,h,f)=>{let p=n.createElement("button");p.type="button",p.className=`mbe-tool ${c}`;let m=X(n,e.icon(u));return m&&p.appendChild(m),p.setAttribute("aria-label",r[d==="codeBlock"?"code":d]),h&&p.setAttribute("aria-keyshortcuts",h),p.addEventListener("click",f),p.addEventListener("pointerenter",y=>{y.pointerType==="mouse"&&this.scheduleTooltip(p)}),p.addEventListener("pointerleave",()=>this.hideTooltip()),this.buttons.set(d,p),p},l=(d,u,c)=>{let h=s(d,d==="list"?"bulletList":"text",u,null,()=>this.toggleMenu(g));h.classList.add("mbe-tool-menu"),h.setAttribute("aria-haspopup","menu"),h.setAttribute("aria-expanded","false");let f=n.createElement("span");f.className="mbe-tool-icon",f.append(...h.childNodes);let p=n.createElement("span");p.className="mbe-tool-chevron";let m=X(n,e.icon("chevronDown"));m&&p.appendChild(m),h.append(f,p);let y=new Xe(e.root,c,"menu");y.el.setAttribute("aria-label",r[d]),y.checkIcon=e.icon("selected"),y.onPicked=()=>this.closeMenu(),y.onHighlight=()=>this.onMenuChange?.(),h.setAttribute("aria-controls",y.el.id);let g={button:h,icon:f,menu:y,iconName:d==="list"?"bulletList":"text"};return g},a=new Set(t);for(let d of Zl){let u=d.filter(c=>a.has(c));if(u.length!==0){if(this.buttons.size>0){let c=n.createElement("span");c.className="mbe-tool-sep",i.appendChild(c),this.separators.push(c)}for(let c of u){let h=jl[c],f;h?(f=s(c,c,h.className,h.keys,()=>this.apply(p=>We(p,h.mark))),f.setAttribute("aria-pressed","false")):c==="link"?(f=s(c,"link","mbe-tool-link",re("K"),()=>e.openLinkPopover()),f.setAttribute("aria-pressed","false")):c==="quote"?(f=s(c,"quote","mbe-tool-quote",null,()=>this.apply(p=>ht(p,{type:"quote"}))),f.setAttribute("aria-pressed","false")):c==="codeBlock"?f=s(c,"code","mbe-tool-code-block",re("Alt+C"),()=>this.apply(p=>ht(p,{type:"code"}))):c==="textStyle"?(this.textStyle=l(c,"mbe-tool-text-style","mbe-text-style-menu"),f=this.textStyle.button):(this.list=l("list","mbe-tool-list","mbe-list-menu"),f=this.list.button),i.appendChild(f)}}}}popup;tooltip;buttons=new Map;separators=[];textStyle=null;list=null;openTrigger=null;tooltipTimer;tooltipHiddenAt=0;onMenuChange=null;get isOpen(){return this.popup.isOpen}get menu(){return this.openTrigger?.menu??null}contains(e){return this.popup.el.contains(e)||!!this.openTrigger?.menu.el.contains(e)}show(){let e=this.editor.selectionRect();if(!e)return this.hide();this.update();let t=this.editor.root.ownerDocument.defaultView;this.popup.el.style.maxWidth=`${Math.max(xo(t).width-16,0)}px`,this.popup.show(e,Ft()?"below":"above",8),this.openTrigger&&this.placeMenu(this.openTrigger)}hide(){this.closeMenu(),this.hideTooltip(),this.popup.hide()}closeMenu(){let e=this.openTrigger;e&&(this.openTrigger=null,e.menu.hide(),e.button.classList.remove("mbe-open"),e.button.setAttribute("aria-expanded","false"),this.onMenuChange?.())}destroy(){clearTimeout(this.tooltipTimer),this.popup.destroy(),this.tooltip.destroy(),this.textStyle?.menu.destroy(),this.list?.menu.destroy()}apply(e){this.hideTooltip(),this.editor.run(e),this.isOpen&&this.update()}update(){let e=this.editor;for(let[a,d]of this.buttons){let u=jl[a];u&&br(d,e.isActive(u.mark))}let t=this.buttons.get("link");t&&br(t,e.isActive("link"));let{anchor:n,focus:i}=e.state.selection,r=!!i.cell&&n.blockId===i.blockId;for(let a of ju)this.buttons.get(a)?.toggleAttribute("hidden",r);r&&this.closeMenu();for(let a of this.separators){let d=(u,c)=>{for(let h=u;h&&!h.classList.contains("mbe-tool-sep");h=h[c])if(!h.hidden)return!0;return!1};a.hidden=!d(a.previousElementSibling,"previousElementSibling")||!d(a.nextElementSibling,"nextElementSibling")}if(r)return;let s=Gl(e),l=this.buttons.get("quote");if(l&&br(l,s.allQuotes),this.textStyle&&(this.setTriggerIcon(this.textStyle,s.style??"text"),this.textStyle.button.classList.toggle("mbe-active",s.allHeadings)),this.list){let a=gr.find(d=>d.listType===s.listType);this.setTriggerIcon(this.list,a?.id??"bulletList"),this.list.button.classList.toggle("mbe-active",s.allListItems)}}setTriggerIcon(e,t){if(e.iconName===t)return;e.iconName=t;let n=X(e.icon.ownerDocument,this.editor.icon(t));e.icon.replaceChildren(...n?[n]:[])}toggleMenu(e){let t=this.openTrigger===e;if(this.closeMenu(),this.hideTooltip(),t)return;let n=Gl(this.editor),i=this.editor.labels,r,s;e===this.textStyle?(s=n.style??void 0,r=Gu.map(({id:l,spec:a,keys:d})=>({id:l,label:i[l],icon:this.editor.icon(l),labelClass:`mbe-menu-${l}`,keyShortcuts:d,checked:l===s,run:()=>l!==s&&this.apply(u=>Ge(u,a))}))):(s=gr.find(l=>l.listType===n.listType)?.id,r=gr.map(({id:l,listType:a,keys:d})=>({id:l,label:i[l],icon:this.editor.icon(l),keyShortcuts:d,checked:l===s,run:()=>this.apply(u=>ht(u,{type:"listItem",listType:a}))}))),e.menu.setSections([{items:r}]),s&&e.menu.highlightId(s),this.openTrigger=e,e.button.classList.add("mbe-open"),e.button.setAttribute("aria-expanded","true"),this.placeMenu(e),this.onMenuChange?.()}placeMenu(e){let t=this.editor.root.ownerDocument.defaultView.getComputedStyle(this.editor.root).direction==="rtl";e.menu.popup.show(e.button.getBoundingClientRect(),this.popup.side,4,t?"end":"start")}scheduleTooltip(e){if(clearTimeout(this.tooltipTimer),this.openTrigger)return;let t=Date.now()-this.tooltipHiddenAt<Xu?0:Zu;this.tooltipTimer=setTimeout(()=>{if(!this.isOpen||this.openTrigger||!e.isConnected)return;this.tooltip.el.textContent=e.getAttribute("aria-label"),this.tooltip.show(e.getBoundingClientRect(),this.popup.side,6);let n=e.getBoundingClientRect(),i=this.tooltip.el.getBoundingClientRect(),r=this.editor.root.ownerDocument.defaultView,s=Math.max(8,Math.min(n.left+(n.width-i.width)/2,r.innerWidth-i.width-8));this.tooltip.el.style.left=`${parseFloat(this.tooltip.el.style.left)+s-i.left}px`},t)}hideTooltip(){clearTimeout(this.tooltipTimer),this.tooltip.isOpen&&(this.tooltip.hide(),this.tooltipHiddenAt=Date.now())}};function br(o,e){o.classList.toggle("mbe-active",e),o.setAttribute("aria-pressed",String(e))}var Lo=class{popup;input;removeButton;done=null;constructor(e){let t=e.root.ownerDocument;this.popup=new we(e.root,"mbe-input-popover"),this.input=t.createElement("input"),this.input.className="mbe-popover-input",this.removeButton=t.createElement("button"),this.removeButton.type="button",this.removeButton.className="mbe-tool",this.popup.el.append(this.input,this.removeButton),this.input.addEventListener("keydown",n=>{n.key==="Enter"?(n.preventDefault(),this.finish(this.input.value.trim())):n.key==="Escape"&&(n.preventDefault(),this.finish(null))}),this.input.addEventListener("blur",()=>{setTimeout(()=>{this.popup.isOpen&&t.activeElement!==this.input&&this.close()},150)}),this.removeButton.addEventListener("click",()=>this.finish(""))}get isOpen(){return this.popup.isOpen}contains(e){return this.popup.el.contains(e)}open(e,t,n){this.done=n,this.input.type=t.type,this.input.value=t.value,this.input.placeholder=t.placeholder,this.input.setAttribute("aria-label",t.label),this.removeButton.textContent=t.removeLabel??"",this.removeButton.hidden=!t.removeLabel||t.value==="",this.popup.show(e,"below"),this.input.focus(),this.input.select()}close(){this.done=null,this.popup.hide()}finish(e){let t=this.done;this.done=null,this.popup.hide(),t?.(e)}destroy(){this.popup.destroy()}};var Yl=24,Yu=8,Jl=16,Bo=class{constructor(e){this.editor=e;let t=e.root,n=t.ownerDocument,i=e.labels;this.frame=n.createElement("div"),this.frame.className="mbe-ui mbe-image-frame",this.frame.hidden=!0;let r=l=>{let a=n.createElement("div");return a.className=`mbe-resize-handle mbe-resize-handle-${l}`,a.dataset.side=l,a.title=i.resizeImage,a.setAttribute("aria-hidden","true"),this.frame.appendChild(a),a};this.handles={left:r("left"),right:r("right")};let s=(l,a,d)=>{l.addEventListener(a,d),this.cleanup.push(()=>l.removeEventListener(a,d))};s(t,"mouseover",l=>this.onOver(l)),s(t,"mouseleave",()=>this.scheduleUnhover());for(let l of[this.frame,this.handles.left,this.handles.right])s(l,"mouseenter",()=>this.cancelUnhover()),s(l,"mouseleave",()=>this.scheduleUnhover());s(this.frame,"mousedown",l=>l.preventDefault());for(let l of["left","right"])s(this.handles[l],"pointerdown",a=>this.startDrag(a,l));s(this.frame,"pointermove",l=>this.moveDrag(l)),s(this.frame,"pointerup",l=>this.endDrag(l,!0)),s(this.frame,"pointercancel",l=>this.endDrag(l,!1)),s(this.frame,"lostpointercapture",l=>this.endDrag(l,!0))}frame;handles;hovered=null;selected=null;framed=null;drag=null;hideTimer=null;cleanup=[];contains(e){return this.frame.contains(e)}get isDragging(){return this.drag!==null}update(e){this.selected=e,this.drag||this.placeFrame()}reposition(){this.drag||(this.selected&&!this.selected.el.isConnected&&(this.selected=null),this.placeFrame())}hide(){this.drag||(this.hovered=null,this.selected=null,this.placeFrame())}destroy(){this.cancelUnhover();for(let e of this.cleanup)e();this.frame.remove()}onOver(e){if(this.drag||!this.editor.isEditable)return;let t=e.target,n=t instanceof HTMLImageElement&&t.classList.contains("mbe-image")?t:null;n?(this.cancelUnhover(),this.hovered=n,this.placeFrame()):this.hovered&&this.scheduleUnhover()}scheduleUnhover(){this.cancelUnhover(),this.hideTimer=setTimeout(()=>{this.hideTimer=null,this.hovered=null,this.placeFrame()},250)}cancelUnhover(){this.hideTimer!==null&&clearTimeout(this.hideTimer),this.hideTimer=null}placeFrame(){let e=this.editor.isEditable,t=this.hovered?.isConnected?this.hovered:null,n=this.drag?.el??(e?t??this.selected?.el??null:null),i=n instanceof HTMLImageElement&&n.isConnected?n:null,r=i?.getBoundingClientRect();if(!i||!r||r.width===0||r.height===0){this.frame.hidden=!0,this.framed=null;return}this.framed!==i&&(this.framed=i,i.complete||i.addEventListener("load",()=>this.framed===i&&this.placeFrame(),{once:!0}));let s=yr(i,r);if(!s||s.width<Math.min(r.width,Jl)||s.height<Math.min(r.height,Jl)){this.frame.hidden=!0;return}this.frame.hidden=!1;let l=$t(this.frame,this.editor.root);this.frame.style.left=`${Math.round(s.left-l.x)}px`,this.frame.style.top=`${Math.round(s.top-l.y)}px`,this.frame.style.width=`${Math.round(s.width)}px`,this.frame.style.height=`${Math.round(s.height)}px`,this.frame.classList.toggle("mbe-image-frame-small",s.width<64||s.height<40)}startDrag(e,t){let n=this.framed;if(!n||this.drag||e.button!==0||!this.editor.isEditable)return;let i=this.editor.imageTarget(n);if(!i)return;e.preventDefault(),this.handles[t].setPointerCapture?.(e.pointerId);let r=n.getBoundingClientRect().width,s=Array.from(this.editor.root.querySelectorAll("img.mbe-image")).filter(d=>d!==n).map(d=>d.getBoundingClientRect().width).filter(d=>d>0),l=Math.max(Yl,Ju(n)),a=n.closest(".mbe-image-block")?.getAttribute("data-align")==="center"?2:1;this.drag={el:n,side:t,pointer:e.pointerId,startX:e.clientX,startWidth:r,max:l,snaps:[...s,l],width:r,factor:a},this.frame.classList.add("mbe-image-resizing"),this.editor.selectImageAt(i.pos)}moveDrag(e){let t=this.drag;if(!t||e.pointerId!==t.pointer)return;e.preventDefault();let n=(e.clientX-t.startX)*(t.side==="right"?1:-1)*t.factor,i=Math.min(t.max,Math.max(Yl,t.startWidth+n)),r=t.snaps.find(s=>Math.abs(s-i)<=Yu);r!==void 0&&(i=Math.min(t.max,r)),t.width=Math.round(i),t.el.style.width=`${t.width}px`,t.el.style.height="auto",this.placeFrame()}endDrag(e,t){let n=this.drag;if(!n||e.pointerId!==n.pointer)return;this.drag=null,this.frame.classList.remove("mbe-image-resizing"),this.handles[n.side].releasePointerCapture?.(e.pointerId),n.el.style.width="",n.el.style.height="";let i=n.el.isConnected?this.editor.imageTarget(n.el):null;t&&i&&Math.round(n.width)!==Math.round(n.startWidth)?this.editor.updateImageAt(i.pos,{width:n.width}):this.editor.refreshImageUi()}};function yr(o,e){let t=o.ownerDocument,n=t.defaultView,i=e.left,r=e.top,s=e.right,l=e.bottom,a=(d,u,c,h)=>{i=Math.max(i,d),r=Math.max(r,u),s=Math.min(s,c),l=Math.min(l,h)};for(let d=o.parentElement;d&&d!==t.body&&d!==t.documentElement;d=d.parentElement){let u=n?.getComputedStyle(d),c=d.scrollHeight>d.clientHeight&&/auto|scroll|overlay/.test(u?.overflowY??""),h=d.scrollWidth>d.clientWidth&&/auto|scroll|overlay/.test(u?.overflowX??"");if(!c&&!h)continue;let f=d.getBoundingClientRect(),p=f.left+d.clientLeft,m=f.top+d.clientTop;a(p,m,p+d.clientWidth,m+d.clientHeight)}return a(0,0,t.documentElement.clientWidth,t.documentElement.clientHeight),s<=i||l<=r?null:{left:i,top:r,width:s-i,height:l-r}}function Ju(o){let e=o.parentElement,t=o.ownerDocument.defaultView;for(;e&&t.getComputedStyle(e).display.startsWith("inline");)e=e.parentElement;if(!e)return o.getBoundingClientRect().width;let n=t.getComputedStyle(e);return e.clientWidth-parseFloat(n.paddingLeft||"0")-parseFloat(n.paddingRight||"0")}var eh=[{align:"left",icon:"imageAlignLeft"},{align:"center",icon:"imageAlignCenter"},{align:"right",icon:"imageAlignRight"}],Ao=class{constructor(e,t){this.editor=e;let n=e.root.ownerDocument,i=e.labels;this.popup=new we(e.root,"mbe-image-toolbar");let r=this.popup.el;r.setAttribute("role","toolbar"),r.setAttribute("aria-label",i.imageSection);let s=(a,d,u)=>{let c=n.createElement("button");c.type="button",c.className=`mbe-tool ${d}`;let h=X(n,e.icon(a));return h&&c.appendChild(h),c.setAttribute("aria-label",i[a]),c.title=i[a],c.addEventListener("click",()=>{this.target&&u()}),c};for(let{align:a,icon:d}of eh){let u=s(d,`mbe-tool-${d}`,()=>t.align(this.target,a));u.setAttribute("aria-pressed","false"),this.alignButtons.push({align:a,button:u}),r.appendChild(u)}this.alignSep=n.createElement("span"),this.alignSep.className="mbe-tool-sep",r.appendChild(this.alignSep),r.appendChild(s("altText","mbe-tool-altText",()=>t.altText(this.target))),this.resetButton=s("resetImageSize","mbe-tool-resetImageSize",()=>t.resetSize(this.target)),r.appendChild(this.resetButton);let l=n.createElement("span");l.className="mbe-tool-sep",r.appendChild(l),r.appendChild(s("delete","mbe-tool-delete",()=>t.remove(this.target)))}popup;alignButtons=[];alignSep;resetButton;target=null;get isOpen(){return this.popup.isOpen}contains(e){return this.popup.el.contains(e)}show(e,t){let n=yr(e.el,e.el.getBoundingClientRect());if(!n)return this.hide();this.target=e;let i=e.image.align??"left";for(let{align:d,button:u}of this.alignButtons)u.hidden=!t,u.classList.toggle("mbe-active",t&&d===i),u.setAttribute("aria-pressed",String(t&&d===i));this.alignSep.hidden=!t,this.resetButton.hidden=!e.image.width;let{left:r,top:s,width:l,height:a}=n;this.popup.show({left:r,top:s,right:r+l,bottom:s+a},Ft()?"below":"above",8)}hide(){this.target=null,this.popup.hide()}destroy(){this.popup.destroy()}};var W="data-block-id";function Cr(o){return`code:${o.trim().toLowerCase()}`}var Kt="data-content",In="mbe-source-text",Ir="mbe-source-line",$o="data-mbe-fill",xr="data-mbe-pending",ia="data-atom",ra="data-list-start",th="data-quote-joined",nh="data-quote-continued",oh="data-section-hidden",ih="data-collapsed",rh="data-in-section",sh={bold:["strong"],italic:["em"],strike:["s"],code:["code"],html:["span","mbe-raw-html"]},lh={};function qe(...o){return o.filter(Boolean).join(" ")}function ah(o,e){let t=ch(o,e);return Wt(o)&&o.quote?qe(t,e.blockquote)||void 0:t}function ch(o,e){switch(o.type){case"paragraph":return e.paragraph;case"heading":return e.heading?.[o.level];case"quote":return e.blockquote;case"listItem":return e[`${o.listType}List`];case"code":return e.codeBlock;case"table":return e.table;case"raw":return e.raw;case"divider":return;case"details":return e.details;case"detailsEnd":return}}var Ln=o=>{let e=o.replace(/^[\u0000- ]+|[\u0000- ]+$/g,"").replace(/[\t\n\r]/g,""),t=/^([a-z][a-z0-9+.-]*):/i.exec(e);return t?["http","https","mailto","tel"].includes(t[1].toLowerCase())?e:null:e},Je="block";function Cn(o,e){return o?`${o.row}:${o.col}@${e}`:`@${e}`}function Lr(o,e){if(o===e)return!0;if(!o||!e||o.size!==e.size)return!1;for(let t of o)if(!e.has(t))return!1;return!0}function Br(o){return`callout${o[0].toUpperCase()}${o.slice(1)}`}function dh(o,e){let t=Y(e);if(t===null)return;o.classList.add("mbe-image-block");let n=C(e.content,t,t+1)[0]?.image?.align;n&&(o.dataset.align=n)}function uh(o,e,t,n){let i=t.doc,r=i.createElement("blockquote");r.dir="auto",r.dataset.callout=e;let s=i.createElement("div");return s.className="mbe-content",vt(s,o.content,t,o,n),r.append(No(e,t),s),r}function No(o,e){let t=Br(o),n=e.doc.createElement("div");n.className="mbe-callout-icon",n.contentEditable="false",n.setAttribute("role","img"),n.setAttribute("aria-label",e.labels[t]);let i=e.icon?.(t);return i&&n.appendChild(i),n}function sa(o,e,t){let{doc:n,labels:i}=e,r=(c,h)=>{let f=n.createElement(c);return h&&(f.className=h),f},s,l=t?.has(Je)||e.source?null:e.blockView?.(o);if(l)s=r("div","mbe-view-block mbe-block-view"),s.contentEditable="false",s.appendChild(l);else switch(o.type){case"paragraph":case"heading":case"quote":if(o.type==="quote"&&o.callout){s=uh(o,o.callout,e,t);break}s=r(o.type==="paragraph"?"p":o.type==="quote"?"blockquote":`h${o.level}`),s.dir="auto",vt(s,o.content,e,o,t),o.type==="paragraph"&&dh(s,o);break;case"listItem":{s=r(o.quote?"blockquote":"div"),o.callout&&(s.dataset.callout=o.callout,s.appendChild(No(o.callout,e))),s.dir="auto",s.dataset.listType=o.listType,s.dataset.indent=String(o.indent);let c=r("span","mbe-marker");if(c.contentEditable="false",o.listType==="task"){let f=r("input","mbe-checkbox");f.type="checkbox",f.tabIndex=-1,f.checked=o.checked,c.appendChild(f)}let h=r("div","mbe-content");vt(h,o.content,e,o,t),s.append(c,h);break}case"code":{if(e.source){s=r("div"),s.spellcheck=!1;let y=r("div",In);y.setAttribute(Kt,""),aa(y,o,e),s.appendChild(y);break}let c=t?.has(Je)?null:e.view?.(Cr(o.language),I(o.content),"block");if(c){s=c,s.classList.add("mbe-view-block"),s.contentEditable="false",s.dataset.language=o.language;break}s=r("div"),s.dataset.language=o.language;let h=r("div","mbe-code-bar");h.contentEditable="false";let f=r("input","mbe-code-lang");f.value=o.language,f.spellcheck=!1,f.setAttribute("aria-label",i.language),f.placeholder=i.plainText,h.appendChild(f),e.codeCopy&&h.appendChild(bh(e));let p=r("pre"),m=r("code");m.spellcheck=!1,vt(m,o.content,e,o,void 0,void 0,!1),p.appendChild(m),s.append(h,p);break}case"raw":{let c=I(o.content),h=t?.has(Je)===!0,f=o.syntax==="html"&&!h&&e.views?.has("html")?tl(c,e.markdown):null,p=o.syntax==="footnote"&&!h&&e.views?.has("footnote")?da(c):null,m=o.syntax==="markdown"&&!h&&e.views?.get("markdown")===Nr,y=h?null:m?wh(c,e):p?ua(p,e):e.view?.(o.syntax,f?f.html:c,"block");y?(s=y,s.classList.add("mbe-view-block"),o.syntax==="html"&&s.classList.add("mbe-view-html"),s.contentEditable="false",f&&Ar(s,f.slots.length,g=>yh(f.slots[g],e),e.doc)):(s=r("pre"),s.spellcheck=!1,vt(s,o.content,e,o,void 0,void 0,!1)),s.dataset.syntax=o.syntax;break}case"divider":s=r("div"),s.contentEditable="false",s.appendChild(r("hr",e.classes.divider));break;case"details":{s=r("div"),s.dir="auto";let c=r("span","mbe-details-marker");c.contentEditable="false";let h=r("button","mbe-details-toggle");h.type="button",h.tabIndex=-1;let f=e.icon?.("chevronRight");f&&h.appendChild(f),c.appendChild(h);let p=r("div","mbe-content");vt(p,o.content,e,o,void 0,void 0,!1),s.append(c,p);break}case"detailsEnd":s=r("div"),s.contentEditable="false";break;case"table":{s=r("div"),s.contentEditable="false";let c=r("table"),h=r("tbody");o.rows.forEach((f,p)=>{let m=r("tr");f.forEach((y,g)=>{let x=r(p===0?"th":"td");x.contentEditable="true",x.dir="auto",x.dataset.row=String(p),x.dataset.col=String(g);let b=o.align[g];b&&(x.style.textAlign=b),vt(x,y,e,o,t,{row:p,col:g}),m.appendChild(x)}),h.appendChild(m)}),c.appendChild(h),s.appendChild(c);break}}if(!l&&!e.source&&o.type!=="listItem"&&Wt(o)&&o.quote){let c=s;s=r("blockquote"),o.type==="heading"&&(s.dir="auto"),o.callout&&(s.dataset.callout=o.callout,s.appendChild(No(o.callout,e))),c.classList.add("mbe-content"),c.contentEditable==="false"&&(s.contentEditable="false"),c.classList.contains("mbe-view-block")&&s.classList.add("mbe-view-block");for(let h of["language","syntax"])c.dataset[h]!==void 0&&(s.dataset[h]=c.dataset[h]);s.appendChild(c)}s.setAttribute(W,o.id);let a=o.type==="listItem"?"list-item":o.type==="detailsEnd"?"details-end":o.type==="code"&&e.source?"source":o.type,d=`${s.className?s.className+" ":""}mbe-block mbe-${a}`;o.type==="listItem"&&o.checked&&(d+=" mbe-checked"),"content"in o&&B(o.content)===0&&(d+=" mbe-empty");let u=ah(o,e.classes);return u&&(d+=" "+u),s.className=d,s}var wr=new WeakMap,hh=[];function fh(o,e){let t=e.map((s,l)=>({d:s,index:l})).filter(s=>s.d.to>s.d.from).sort((s,l)=>s.d.from-l.d.from),n=0,i=[],r=[];for(let s=0;;){let l=o.indexOf(`
|
|
109
109
|
`,s),a=l===-1?o.length:l,d=i.filter(c=>c.d.to>s);for(;n<t.length&&t[n].d.from<a;){let c=t[n++];c.d.to>s&&d.push(c)}i=d.filter(c=>c.d.to>a),d.length>1&&d.sort((c,h)=>c.index-h.index);let u=d.length===0?hh:d.map(({d:c})=>({...c,from:Math.max(c.from,s)-s,to:Math.min(c.to,a)-s})).filter(c=>c.to>c.from);if(r.push({text:o.slice(s,a),decorations:u}),l===-1)return r;s=l+1}}function ea(o,e){return o.text===e.text&&$n(o.decorations,e.decorations)}function Ro(o){return o instanceof HTMLElement&&o.classList.contains(Ir)}function*la(o){let e=wr.get(o),t=e&&e.length===o.childElementCount?e:null,n=0;for(let i=o.firstChild;i;i=i.nextSibling)Ro(i)&&(yield[i,t?t[n].text.length:i.textContent.length],n++)}function aa(o,e,t){mh(o,I(e.content),t.decorateSource?.(e)??[],t)}function ph(o,e){let t=e.doc,n=t.createElement("div");if(n.className=Ir,n.dir="auto",o.text.length===0)n.appendChild(Tr(t));else if(o.decorations.length===0)n.appendChild(t.createTextNode(o.text));else for(let i of Yo([{text:o.text,marks:[]}],o.decorations))n.appendChild(fa(t.createTextNode(i.span.text),i.decorations,t));return n}function mh(o,e,t,n){let i=fh(e,t);o.classList.toggle("mbe-empty-content",e.length===0);let r=Array.from(o.childNodes).filter(Ro),s=wr.get(o)??[];r.length!==s.length&&(s=[],o.replaceChildren());let l=0;for(;l<s.length&&l<i.length&&ea(s[l],i[l]);)l++;let a=0;for(;a<s.length-l&&a<i.length-l&&ea(s[s.length-1-a],i[i.length-1-a]);)a++;let d=n.doc.createDocumentFragment();for(let c=l;c<i.length-a;c++)d.appendChild(ph(i[c],n));let u=a>0?r[s.length-a]:null;for(let c=l;c<s.length-a;c++)r[c].remove();if(o.insertBefore(d,u),o.childNodes.length!==i.length)for(let c of Array.from(o.childNodes))Ro(c)||c.remove();wr.set(o,i)}function gh(o){let e=[],t="";for(let n=o.firstChild;n;n=n.nextSibling)Ro(n)?(n!==o.firstChild&&e.push(t),t=Vt(n).map(Dr).join("")):t+=n.nodeType===3?n.data:n.hasAttribute($o)?"":n.textContent??"";return e.push(t),e.join(`
|
|
110
110
|
`)}function vt(o,e,t,n,i,r,s=!0){let l=t.doc;if(o.setAttribute(Kt,""),B(e)===0){o.classList.add("mbe-empty-content"),o.appendChild(Tr(l));return}let a=n.type==="code"?t.decorateCode?.(n)??[]:s&&t.decorate?t.decorate(n,e,r):[],d=e.length===1?e[0]:null;d&&a.length===0&&d.marks.length===0&&!d.link&&!d.token&&!d.image?o.appendChild(l.createTextNode(d.text)):Ch(e,o,t,a,i,r);let u=e[e.length-1];u&&u.text.endsWith(`
|
|
111
111
|
`)&&o.appendChild(Tr(l))}function bh(o){let e=o.doc.createElement("button");e.type="button",e.className="mbe-code-copy",e.tabIndex=-1,e.setAttribute("aria-label",o.labels.copyCode),e.title=o.labels.copyCode;let t=o.icon?.("copyCode");return t&&e.appendChild(t),e}function Ar(o,e,t,n){if(e===0)return;let i=[],r=n.createTreeWalker(o,4);for(let l=r.nextNode();l;l=r.nextNode())l.nodeValue.includes("\uE000")&&i.push(l);let s=new Set;for(let l of i){let a=l.nodeValue,d=n.createDocumentFragment(),u=0;for(let c of a.matchAll(el)){c.index>u&&d.append(a.slice(u,c.index));let h=Number(c[1]);h<e&&!s.has(h)&&(s.add(h),d.append(t(h))),u=c.index+c[0].length}u<a.length&&d.append(a.slice(u)),l.replaceWith(d)}if(!o.hasAttribute(xr))for(let l=0;l<e;l++)s.has(l)||o.appendChild(t(l))}function yh(o,e){let t=e.doc.createElement("div");return t.className="mbe-flow",Sr(t,o,e),Do(t),t}function Sr(o,e,t){let n=new Map,i=t.blockView?{...t,blockView:void 0}:t;for(let r of e){let s=sa(r,i);n.set(r.id,s),o.appendChild(s)}Er({blocks:e},r=>n.get(r),new WeakMap,new Set,t.labels)}var kh=/^(\[\^((?:[^\]\s\\]|\\.)+)\]:)[ \t]*(.*)$/,vh=/\[\^((?:[^\]\s\\]|\\.)+)\]/g,ca=o=>o.replace(/\\(.)/g,"$1");function da(o){let[e="",...t]=o.split(`
|
|
112
112
|
`),n=kh.exec(e);if(!n)return null;let i=[n[3],...t.map(r=>r.replace(/^(?: {1,4}|\t)/,""))].join(`
|
|
113
|
-
`);return{label:ca(n[2]),head:`${n[1]} `,body:i}}function ua(o,e){let t=e.view?.("footnote",o.head+ko(0),"block")??null;return t&&Ar(t,1,()=>xh(o.body,e),e.doc),t}function xh(o,e){let t=Fo(he(o,e.markdown).children,e),n=t.childNodes.length===1?t.firstElementChild:null;if(t.childNodes.length===0||n&&n.tagName==="P"){let i=e.doc.createElement("span");return n&&i.append(...Array.from(n.childNodes)),Do(i),i}return Do(t),t}var Nr=()=>!1;function wh(o,e){let t=e.doc.createElement("div");return t.classList.add("mbe-view-markdown"),t.appendChild(Fo(he(o,e.markdown).children,e)),Do(t),t}function Fo(o,e){let t=e.markdown??pe,n=e.doc.createElement("div");n.className="mbe-flow";let i=[];for(let r of o){let s=fn(r,t,i[i.length-1]);if(s){i.push(...s);continue}Sr(n,i,e),i=[],r.type==="blockQuote"?n.appendChild(Sh(r,e)):r.type==="list"&&r.items.forEach((l,a)=>n.appendChild(Th(r,l,a,e)))}return Sr(n,i,e),n}function Sh(o,e){let t=e.markdown?.callouts===!1?null:Ni(o.children),n=t?[...t.rest.length>0?[{type:"paragraph",children:[...t.rest]}]:[],...o.children.slice(1)]:o.children,i=e.doc.createElement("blockquote");return i.dir="auto",i.className=qe("mbe-block mbe-quote",e.classes.blockquote),t&&(i.dataset.callout=t.type,i.appendChild(No(t.type,e))),i.appendChild(Fo(n,e)),i}function Th(o,e,t,n){let i=e.task?"task":o.ordered?"ordered":"bullet",r=n.doc.createElement("div");r.dir="auto",r.className=qe("mbe-block mbe-list-item",n.classes[`${i}List`]),r.dataset.listType=i,r.dataset.indent="0",t===0&&r.setAttribute(ra,"");let s=n.doc.createElement("span");if(s.className="mbe-marker",e.task){let a=n.doc.createElement("input");a.className="mbe-checkbox",a.type="checkbox",a.checked=e.checked,s.appendChild(a)}else o.ordered&&(s.textContent=`${o.start+t}.`);let l=n.doc.createElement("div");return l.className="mbe-content",l.appendChild(Fo(e.children,n)),r.append(s,l),r}function Do(o){for(let e of Array.from(o.querySelectorAll(`[${W}], [${Kt}], [contenteditable], [data-row]`)))e.removeAttribute(W),e.removeAttribute(Kt),e.removeAttribute("contenteditable"),e.removeAttribute("data-row"),e.removeAttribute("data-col");for(let e of Array.from(o.querySelectorAll("input")))e.type==="checkbox"?e.disabled=!0:e.readOnly=!0,e.tabIndex=-1}function Tr(o){let e=o.createElement("br");return e.setAttribute($o,""),e}var Eh=new Set(["area","base","br","col","embed","hr","img","input","link","meta","source","track","wbr"]),Mh=/<!--[\s\S]*?-->|<[^<>]*>/g;function ha(o){if(!o.some(t=>t.marks.includes("html")))return o;let e=[];for(let t of G(o)){if(!t.marks.includes("html")){e.push(t);continue}let n=0;for(let i of t.text.matchAll(Mh))i.index>n&&e.push({...t,text:t.text.slice(n,i.index)}),e.push({...t,text:i[0]}),n=i.index+i[0].length;n<t.text.length&&e.push({...t,text:t.text.slice(n)})}return e}function Rr(o){let e=[],t=0;for(let r of ha(o))e.push({span:r,from:t,to:t+r.text.length}),t+=r.text.length;let n=r=>r.marks.includes("html"),i=[];for(let r=0;r<e.length;r++){let{span:s,from:l,to:a}=e[r];if(!n(s))continue;let d=/^<([A-Za-z][A-Za-z0-9-]*)\b[^>]*?(\/?)>$/.exec(s.text),u=r;if(d&&!d[2]&&!Eh.has(d[1].toLowerCase())){let c=d[1].toLowerCase(),h=0;for(let f=r+1;f<e.length;f++){let p=e[f].span;if(!n(p))continue;let m=/^<(\/?)([A-Za-z][A-Za-z0-9-]*)\b[^>]*?(\/?)>$/.exec(p.text);if(!(!m||m[2].toLowerCase()!==c||m[3]))if(!m[1])h++;else if(h>0)h--;else{u=f;break}}}i.push({from:l,to:u===r?a:e[u].to}),r=u}return i}function Ch(o,e,t,n,i,r){let s=t.doc,l=t.views,a=l?(v,T)=>!!v.token&&l.has(v.token)&&!i?.has(Cn(r,T)):void 0;l?.has("html")&&(o=ha(o));let d=l?.has("html")&&o.some(v=>v.marks.includes("html"))?Rr(o).filter(v=>!i?.has(Cn(r,v.from))):[],u=v=>d.some(T=>v>=T.from&&v<T.to),c=v=>v.marks.includes("html"),h=a||d.length>0?(v,T)=>!!a?.(v,T)||c(v)&&u(T):void 0,f=Yo(o,n,h),p=v=>fa(Ih(v.span,t,v.atomic&&!c(v.span)),v.decorations,s),m=new Map,y=0;for(let v of f)m.set(v,y),y+=v.span.text.length;let g=v=>{let T=s.createElement("a"),E=t.resolveUrl(v.link,"link");return E!==null&&T.setAttribute("href",E),T.dataset.href=v.link,v.linkTitle&&(T.title=v.linkTitle),T.className=qe("mbe-link",t.classes.link),T},x=v=>d.find(T=>T.from===m.get(f[v])),b=(v,T)=>{let E=v;for(;E<f.length&&m.get(f[E])<T.to;)E++;return E},w=(v,T)=>{let E=f[v].span;if(!E.link)return!1;for(let A=v+1,fe=b(v,T);A<fe;A++)if(!ae(f[A].span,E))return!1;return!0},k=(v,T,E,A)=>{let fe=[],me=[],Se=b(v,T);for(let le=v;le<Se;){let Vr=f[le];if(c(Vr.span)){fe.push(Vr.span.text),le++;continue}let Wo=s.createDocumentFragment(),Ur=zr=>zr>=Se||c(f[zr].span);if(A)for(;!Ur(le);le++)Wo.appendChild(p(f[le]));else le=S(le,Ur,Wo);me.push(Wo),fe.push(ko(me.length-1))}let Te=t.view?.("html",fe.join(""),"inline");if(!Te)return null;Ar(Te,me.length,le=>me[le],s);let Rn=I(C(o,T.from,T.to));return E.appendChild(Oo(Te,qe("mbe-view","mbe-view-html",t.classes.token?.html),Rn)),Se},S=(v,T,E)=>{for(;v<f.length&&!T(v);){let A=f[v].span,fe=x(v);if(fe&&!w(v,fe)){let Te=k(v,fe,E,!1);if(Te!==null){v=Te;continue}}if(!A.link){E.appendChild(p(f[v])),v++;continue}let me=g(A),Se=v;for(;v<f.length&&!T(v)&&ae(f[v].span,A);){let Te=x(v);if(Te){let Rn=w(v,Te);if(!Rn&&v>Se)break;let le=Rn?k(v,Te,me,!0):null;if(le!==null){v=le;continue}}me.appendChild(p(f[v])),v++}E.appendChild(me)}return v};S(0,()=>!1,e)}function fa(o,e,t){for(let n=e.length-1;n>=0;n--){let i=e[n],r=t.createElement("span");for(let[s,l]of Object.entries(i.attributes??{}))r.setAttribute(s,l);r.className=`mbe-deco ${i.class}`,r.appendChild(o),o=r}return o}function Oo(o,e,t){return o.classList.add(...e.split(/\s+/).filter(Boolean)),o.contentEditable="false",o.setAttribute(ia,""),t!==void 0&&(o.dataset.source=t),o}function Ih(o,e,t=!1){let n=e.doc,i=t?e.view?.(o.token,o.text,"inline"):null;if(i)return na(o,Oo(i,qe("mbe-view",`mbe-view-${o.token}`,e.classes.token?.[o.token]),o.text),e);if(o.image){let s=o.image,l=e.resolveUrl(s.src,"image");if(l===null)return ta(s,e);let a=e.loadedImage?.(l);if(!a){let d=n.createElement("img");d.setAttribute("loading","lazy"),d.setAttribute("decoding","async"),e.referrerPolicy&&d.setAttribute("referrerpolicy",e.referrerPolicy),d.src=l,d.addEventListener("error",()=>{d.parentNode&&(d.replaceWith(ta({src:l,alt:d.alt,title:d.title||void 0},e)),e.onImageError?.())}),a=d}return a.alt=s.alt,kr(a,"title",s.title),kr(a,"width",s.width?String(s.width):void 0),kr(a,"height",s.height?String(s.height):void 0),Oo(a,qe("mbe-image",e.classes.image))}let r=n.createTextNode(o.text);if(o.token){let s=n.createElement("span");s.className=qe("mbe-token",`mbe-token-${o.token}`,e.classes.token?.[o.token]),s.spellcheck=!1,s.appendChild(r),r=s}return na(o,r,e)}function kr(o,e,t){t===void 0?o.removeAttribute(e):o.setAttribute(e,t)}function ta(o,e){let t=e.doc,n=t.createElement("span"),i=o.alt||e.labels.imageUnavailable;n.setAttribute("role","img"),n.setAttribute("aria-label",i),o.title&&(n.title=o.title);let r=e.icon?.("imageUnavailable");r&&n.appendChild(r);let s=t.createElement("span");return s.className="mbe-image-alt",s.textContent=i,n.appendChild(s),Oo(n,qe("mbe-image","mbe-image-unavailable",e.classes.image))}function na(o,e,t){for(let n=et.length-1;n>=0;n--){let i=et[n];if(!o.marks.includes(i))continue;let[r,s]=sh[i],l=t.doc.createElement(r),a=i==="html"?s:t.classes[i];a&&(l.className=a),l.appendChild(e),e=l}return e}var Ho=class{constructor(e,t){this.root=e;this.labels=t}rendered=new Map;numbers=new WeakMap;resolveUrl=Ln;referrerPolicy;decorate;decorateCode;decorateSource;onViewSettled;widgetsAfter;views;collectFootnotes=!1;footnotes;viewContext;blockViews=[];hosted=new Map;markdown;classes=lh;icon;codeCopy=!1;source=!1;onImageError;collapsed=new Set;revealed=new Map;viewCache=new Map;pendingViews=new Map;failedViews=new Set;viewBlocks=new Map;renderingBlock="";loadedImages=new Map;context(){return{doc:this.root.ownerDocument,labels:this.labels,resolveUrl:this.resolveUrl,referrerPolicy:this.referrerPolicy,decorate:this.decorate,decorateCode:this.decorateCode,decorateSource:this.decorateSource,views:this.views,view:this.views&&((e,t,n)=>this.renderView(e,t,n)),blockView:this.blockViews.length>0?e=>this.renderBlockView(e):void 0,markdown:this.markdown,classes:this.classes,icon:this.icon,codeCopy:this.codeCopy,source:this.source,onImageError:this.onImageError,loadedImage:e=>this.loadedImages.get(e)?.shift()}}renderView(e,t,n){let i=this.views?.get(e);if(!i||!this.viewContext)return null;let r=`${n}\0${e}\0${t}`;if(this.failedViews.has(r))return this.noteViewBlock(r),null;let s=this.viewCache.get(r);if(s)this.viewCache.delete(r);else{s=this.root.ownerDocument.createElement(n==="inline"?"span":"div");let l;try{l=i(t,s,n,this.viewContext)}catch{l=!1}if(l===!1)return this.noteViewBlock(r),null;if(l&&typeof l.then=="function"&&(s.setAttribute(xr,""),this.awaitView(r,l)),this.viewCache.size>=500){let a=this.viewCache.keys().next().value;this.viewCache.delete(a),this.viewBlocks.delete(a)}}return this.pendingViews.get(r)?.add(this.renderingBlock),this.noteViewBlock(r),this.viewCache.set(r,s),s.cloneNode(!0)}isHosted(e){return this.blockViews.some(({view:t})=>t.match(e))}renderBlockView(e){let t=this.hosted.get(e.id);if(t&&t.block===e)return t.after?null:t.el;t&&this.disposeHosted(e.id);let n=this.viewContext;if(!n)return null;let i=null;for(let{name:r,view:s}of this.blockViews){if(!s.match(e))continue;i=r;let l=this.root.ownerDocument.createElement("div"),a;try{a=s.render(e,l,n)}catch{a=!1}if(a===!1)continue;let d=typeof a=="function"?a:void 0;if(s.placement!=="after")return this.hosted.set(e.id,{block:e,name:r,el:l,dispose:d}),l;let u=this.root.ownerDocument.createElement("div");return u.className="mbe-block-view mbe-block-after",u.contentEditable="false",u.appendChild(l),this.hosted.set(e.id,{block:e,name:r,el:l,after:u,dispose:d}),null}return i!==null&&this.hosted.set(e.id,{block:e,name:i,el:null}),null}afterView(e,t){if(t?.has(Je)||this.source)return;let n=this.hosted.get(e.id);return n?.block===e?n.after:void 0}disposeHosted(e){let t=this.hosted.get(e);if(t){this.hosted.delete(e);try{t.dispose?.()}catch{}}}destroy(){for(let e of[...this.hosted.keys()])this.disposeHosted(e)}noteViewBlock(e){let t=this.viewBlocks.get(e);t||(this.viewBlocks.size>=1e3&&this.viewBlocks.delete(this.viewBlocks.keys().next().value),this.viewBlocks.set(e,t=new Set)),t.add(this.renderingBlock)}awaitView(e,t){let n=new Set;this.pendingViews.set(e,n);let i=r=>{this.pendingViews.get(e)===n&&(this.pendingViews.delete(e),this.viewCache.get(e)?.removeAttribute(xr),r||(this.failedViews.add(e),this.viewCache.delete(e)),n.size>0&&this.onViewSettled?.([...n]))};t.then(r=>i(r!==!1),()=>i(!1))}dropViews(e){let t=i=>e===void 0||i.split("\0",3)[1]===e;for(let i of[...this.viewCache.keys()])t(i)&&this.viewCache.delete(i);for(let i of[...this.failedViews])t(i)&&this.failedViews.delete(i);for(let i of[...this.pendingViews.keys()])t(i)&&this.pendingViews.delete(i);let n=new Set;for(let[i,r]of[...this.viewBlocks])if(t(i)){this.viewBlocks.delete(i);for(let s of r)this.rendered.has(s)&&n.add(s)}for(let[i,r]of[...this.hosted])e!==void 0&&r.name!==e||(this.disposeHosted(i),this.rendered.has(i)&&n.add(i));return[...n]}render(e,t=!1,n=!1){let i=this.context(),r=new Set;for(let u of e.blocks){let c=this.rendered.get(u.id);!t&&c&&c.block===u&&c.el.parentNode===this.root&&Lr(c.reveal,this.revealed.get(u.id))&&r.add(c.el)}if(!t){for(let{el:u}of this.rendered.values())if(!r.has(u))for(let c of Array.from(u.querySelectorAll("img.mbe-image"))){let h=c.getAttribute("src");if(h===null||!c.complete||c.naturalWidth===0)continue;let f=this.loadedImages.get(h);f?f.push(c):this.loadedImages.set(h,[c])}}let s=new Map;for(let u of e.blocks){this.renderingBlock=u.id;let c=this.rendered.get(u.id),h=this.revealed.get(u.id),f=c&&r.has(c.el)?c.el:null;!f&&i.source&&u.type==="code"&&c?.block.type==="code"&&c.el.parentNode===this.root&&this.patchSource(c.el,u,i)&&(f=c.el),f||(f=sa(u,i,h),c&&u.type==="raw"&&Bh(c.el,f)),s.set(u.id,{block:u,el:f,reveal:h})}this.loadedImages.clear();for(let[u,c]of[...this.hosted])s.get(u)?.block!==c.block&&this.disposeHosted(u);let l=this.collectFootnotes&&this.views?.has("footnote")===!0;for(let{block:u,el:c}of s.values())u.type==="raw"&&u.syntax==="footnote"&&c.classList.toggle("mbe-footnote-collected",l);let a=l?this.footnoteSection(e,s,i,t):null;l||(this.footnotes=void 0);let d=[];for(let[u,{block:c,el:h,reveal:f}]of s){d.push(h);let p=this.afterView(c,f);p&&d.push(p);let m=this.widgetsAfter?.(u);m&&d.push(...m)}if(a&&d.push(a),t||this.rendered.size===0){let u=this.root.ownerDocument.createDocumentFragment();for(let c of d)n&&c.hasAttribute(W)&&c.classList.add("mbe-lazy"),u.appendChild(c);this.root.replaceChildren(u)}else{let u=this.root.firstChild,c=!1;for(let h of d)h===u?u=u.nextSibling:(this.root.insertBefore(h,u),c=!0);if(c||u!==null||this.root.childNodes.length!==d.length){let h=new Set(d);for(let f of Array.from(this.root.childNodes))h.has(f)||f.remove()}}this.rendered=s,Er(e,u=>s.get(u)?.el,this.numbers,this.collapsed,this.labels)}footnoteSection(e,t,n,i){let r=new Map;for(let u of e.blocks){if(u.type!=="raw"||u.syntax!=="footnote")continue;let c=da(I(u.content));c&&!r.has(c.label)&&r.set(c.label,c)}if(r.size===0)return null;let s=[],l=new Map;for(let u of e.blocks){let c=t.get(u.id)?.el;if(c)for(let h of Array.from(c.querySelectorAll(".mbe-view-footnote")))for(let f of(h.dataset.source??"").matchAll(vh)){let p=ca(f[1]);if(!r.has(p))continue;let m=(l.get(p)??0)+1;l.set(p,m),m===1&&s.push(p)}}if(s.length===0)return null;let a=JSON.stringify(s.map(u=>[u,r.get(u).body,l.get(u)]));if(!i&&this.footnotes?.key===a)return this.footnotes.el;let d=n.doc.createElement("section");d.className="mbe-footnotes",d.contentEditable="false",d.setAttribute("aria-label",this.labels.footnotes);for(let u of s){let c=ua(r.get(u),n);if(!c)continue;let h=c.lastElementChild??c,f=h.querySelector(":scope > .mbe-flow");if(f){let p=f.lastElementChild;p?.tagName==="P"?h=p:(h=n.doc.createElement("p"),h.className="mbe-block mbe-paragraph",f.appendChild(h))}for(let p=1;p<=l.get(u);p++){let m=n.doc.createElement("button");m.type="button",m.className="mbe-footnote-backref",m.dataset.label=u,m.dataset.ref=String(p),m.setAttribute("aria-label",`${this.labels.backToReference} ${u}${p>1?` ${p}`:""}`);let y=n.icon?.("backToReference");if(y?m.appendChild(y):m.textContent="\u2191",p>1){let g=n.doc.createElement("sup");g.textContent=String(p),m.appendChild(g)}h.appendChild(m)}d.appendChild(c)}return this.footnotes={key:a,el:d},d}updateSections(e){Er(e,t=>this.rendered.get(t)?.el,this.numbers,this.collapsed,this.labels)}elementFor(e){return this.rendered.get(e)?.el}adopt(e){let t=this.rendered.get(e.id);t&&(this.rendered.set(e.id,{...t,block:e}),"content"in e&&t.el.classList.toggle("mbe-empty",B(e.content)===0))}invalidate(e){this.rendered.delete(e)}patchSource(e,t,n=this.context()){let i=oe(e);return i?(aa(i,t,n),e.classList.toggle("mbe-empty",B(t.content)===0),!0):!1}refreshSource(e){let t=this.rendered.get(e.id);t&&t.el.parentNode===this.root&&this.patchSource(t.el,e)}};function Lh(o,e,t,n,i){let r=e>0?String(e):"";if(o.style.getPropertyValue("--mbe-section-depth")!==r&&(r?o.style.setProperty("--mbe-section-depth",r):o.style.removeProperty("--mbe-section-depth")),_t(o,rh,e>0),_t(o,oh,t),n===null)return;_t(o,ih,n);let s=o.querySelector(".mbe-details-toggle"),l=String(!n);s&&s.getAttribute("aria-expanded")!==l&&(s.setAttribute("aria-expanded",l),s.setAttribute("aria-label",n?i.expandSection:i.collapseSection))}function Er(o,e,t,n,i){let r=[],s,l=[];o.blocks.forEach((a,d)=>{let u=o.blocks[d-1],c=e(a.id);if(a.type==="detailsEnd"&&l.pop(),c&&Lh(c,l.length,l.includes(!0),a.type==="details"?n.has(a.id):null,i),a.type==="details"&&l.push(n.has(a.id)),M(a)!==null&&(_t(c,th,ie(u,a)),_t(c,nh,ie(a,o.blocks[d+1]))),(a.type!=="listItem"||u?.type!=="listItem"||M(u)!==M(a))&&(r.length=0,s=void 0),a.type!=="listItem")return;let h=a.listType==="ordered";if(_t(c,ra,r.length===0||a.indent===0&&s!==void 0&&h!==s),a.indent===0&&(s=h),r.length=a.indent+1,!h){r[a.indent]=void 0;return}let f=r[a.indent]===void 0?a.start??1:r[a.indent]+1;if(r[a.indent]=f,!c||t.get(c)===f)return;let p=c.querySelector(".mbe-marker");p&&(p.textContent=`${f}.`),t.set(c,f)})}function Bh(o,e){let t=o.querySelectorAll("details");if(t.length===0)return;let n=e.querySelectorAll("details");n.length===t.length&&t.forEach((i,r)=>{n[r].open=i.open})}function _t(o,e,t){o&&o.hasAttribute(e)!==t&&o.toggleAttribute(e,t)}function Ah(o,e){let t=e;for(;t&&t.parentNode!==o;)t=t.parentNode;return t instanceof HTMLElement&&t.hasAttribute(W)?t:null}function oe(o,e){return e?o.querySelector(`[data-row="${e.row}"][data-col="${e.col}"]`):o.hasAttribute(Kt)?o:o.querySelector(`[${Kt}]`)}function Vt(o){let e=[],t=n=>{for(let i=n.firstChild;i;i=i.nextSibling)i.nodeType===3?e.push(i):i instanceof HTMLElement&&(i.hasAttribute(ia)?e.push(i):i.hasAttribute($o)||t(i))};return t(o),e}function Dr(o){return o.nodeType===3?o.data:o.dataset.source??"\uFFFC"}function Po(o){return Dr(o).length}function xt(o){return Array.prototype.indexOf.call(o.parentNode.childNodes,o)}function Or(o){return o.classList.contains(In)?gh(o):Vt(o).map(Dr).join("")}function oa(o,e){let t=0;for(let[n,i]of la(o)){if(n===e)return t;t+=i+1}return e===null&&t>0?t-1:t}function Mr(o,e,t){if(!o.contains(e))return e.compareDocumentPosition(o)&Node.DOCUMENT_POSITION_FOLLOWING?0:Vt(o).reduce((l,a)=>l+Po(a),0);if(o.classList.contains(In)){if(e===o)return oa(o,o.childNodes[t]??null);let s=(e instanceof Element?e:e.parentElement)?.closest(`.${Ir}`);if(s?.parentNode===o)return oa(o,s)+Mr(s,e,t)}let n=o.ownerDocument,i=n.createRange();i.setStart(e,t);let r=0;for(let s of Vt(o)){let l=Po(s);if(s===e)return r+Math.min(t,l);if(s.contains(e))return r;let a=n.createRange();if(s.nodeType===3?a.setStart(s,l):a.setStartAfter(s),a.compareBoundaryPoints(Range.START_TO_START,i)<=0)r+=l;else return r}return r}function _e(o,e,t){let n=e,i=t;if(e===o){let a=o.childNodes[Math.min(t,o.childNodes.length-1)];if(!a)return null;let d=t>=o.childNodes.length;if(n=a,i=d?a.childNodes.length:0,d){let u=a,c=u.hasAttribute?.(W)?oe(u):null;if(c)return{blockId:u.getAttribute(W),offset:Or(c).length}}}let r=Ah(o,n);if(!r)return null;let s=r.getAttribute(W);if(r.classList.contains("mbe-divider"))return{blockId:s,offset:0};if(r.classList.contains("mbe-table")){let a=(n instanceof Element?n:n.parentElement)?.closest("[data-row]");if(!a||!r.contains(a))return{blockId:s,offset:0,cell:{row:0,col:0}};let d={row:Number(a.dataset.row),col:Number(a.dataset.col)};return{blockId:s,offset:Mr(a,n,i),cell:d}}let l=oe(r);return l?{blockId:s,offset:Mr(l,n,i)}:{blockId:s,offset:0}}function pa(o,e){let t=oe(o,e.cell);if(!t||t.classList.contains(In))return null;let n=0;for(let i of Vt(t)){if(n===e.offset&&i.nodeType!==3)return i;if(n+=Po(i),n>e.offset)return null}return null}function ma(o,e){let t=oe(o,e.cell);if(!t)return{node:o.parentNode,offset:xt(o)};if(t.classList.contains(In)){let n=0,i=null;for(let r of la(t)){let[s,l]=r;if(e.offset<=n+l)return vr(s,e.offset-n);n+=l+1,i=r}return i?vr(i[0],i[1]):{node:t,offset:0}}return vr(t,e.offset)}function vr(o,e){let t=0,n=Vt(o);for(let r of n){let s=Po(r);if(r.nodeType===3){let l=r;if(t+s>=e){let a=e-t;if(a===s&&r===n[n.length-1]&&l.data.endsWith(`
|
|
113
|
+
`);return{label:ca(n[2]),head:`${n[1]} `,body:i}}function ua(o,e){let t=e.view?.("footnote",o.head+ko(0),"block")??null;return t&&Ar(t,1,()=>xh(o.body,e),e.doc),t}function xh(o,e){let t=Fo(he(o,e.markdown).children,e),n=t.childNodes.length===1?t.firstElementChild:null;if(t.childNodes.length===0||n&&n.tagName==="P"){let i=e.doc.createElement("span");return n&&i.append(...Array.from(n.childNodes)),Do(i),i}return Do(t),t}var Nr=()=>!1;function wh(o,e){let t=e.doc.createElement("div");return t.classList.add("mbe-view-markdown"),t.appendChild(Fo(he(o,e.markdown).children,e)),Do(t),t}function Fo(o,e){let t=e.markdown??pe,n=e.doc.createElement("div");n.className="mbe-flow";let i=[];for(let r of o){let s=fn(r,t,i[i.length-1]);if(s){i.push(...s);continue}Sr(n,i,e),i=[],r.type==="blockQuote"?n.appendChild(Sh(r,e)):r.type==="list"&&r.items.forEach((l,a)=>n.appendChild(Th(r,l,a,e)))}return Sr(n,i,e),n}function Sh(o,e){let t=e.markdown?.callouts===!1?null:Ni(o.children),n=t?[...t.rest.length>0?[{type:"paragraph",children:[...t.rest]}]:[],...o.children.slice(1)]:o.children,i=e.doc.createElement("blockquote");return i.dir="auto",i.className=qe("mbe-block mbe-quote",e.classes.blockquote),t&&(i.dataset.callout=t.type,i.appendChild(No(t.type,e))),i.appendChild(Fo(n,e)),i}function Th(o,e,t,n){let i=e.task?"task":o.ordered?"ordered":"bullet",r=n.doc.createElement("div");r.dir="auto",r.className=qe("mbe-block mbe-list-item",n.classes[`${i}List`]),r.dataset.listType=i,r.dataset.indent="0",t===0&&r.setAttribute(ra,"");let s=n.doc.createElement("span");if(s.className="mbe-marker",e.task){let a=n.doc.createElement("input");a.className="mbe-checkbox",a.type="checkbox",a.checked=e.checked,s.appendChild(a)}else o.ordered&&(s.textContent=`${o.start+t}.`);let l=n.doc.createElement("div");return l.className="mbe-content",l.appendChild(Fo(e.children,n)),r.append(s,l),r}function Do(o){for(let e of Array.from(o.querySelectorAll(`[${W}], [${Kt}], [contenteditable], [data-row]`)))e.removeAttribute(W),e.removeAttribute(Kt),e.removeAttribute("contenteditable"),e.removeAttribute("data-row"),e.removeAttribute("data-col");for(let e of Array.from(o.querySelectorAll("input")))e.type==="checkbox"?e.disabled=!0:e.readOnly=!0,e.tabIndex=-1}function Tr(o){let e=o.createElement("br");return e.setAttribute($o,""),e}var Eh=new Set(["area","base","br","col","embed","hr","img","input","link","meta","source","track","wbr"]),Mh=/<!--[\s\S]*?-->|<[^<>]*>/g;function ha(o){if(!o.some(t=>t.marks.includes("html")))return o;let e=[];for(let t of G(o)){if(!t.marks.includes("html")){e.push(t);continue}let n=0;for(let i of t.text.matchAll(Mh))i.index>n&&e.push({...t,text:t.text.slice(n,i.index)}),e.push({...t,text:i[0]}),n=i.index+i[0].length;n<t.text.length&&e.push({...t,text:t.text.slice(n)})}return e}function Rr(o){let e=[],t=0;for(let r of ha(o))e.push({span:r,from:t,to:t+r.text.length}),t+=r.text.length;let n=r=>r.marks.includes("html"),i=[];for(let r=0;r<e.length;r++){let{span:s,from:l,to:a}=e[r];if(!n(s))continue;let d=/^<([A-Za-z][A-Za-z0-9-]*)\b[^>]*?(\/?)>$/.exec(s.text),u=r;if(d&&!d[2]&&!Eh.has(d[1].toLowerCase())){let c=d[1].toLowerCase(),h=0;for(let f=r+1;f<e.length;f++){let p=e[f].span;if(!n(p))continue;let m=/^<(\/?)([A-Za-z][A-Za-z0-9-]*)\b[^>]*?(\/?)>$/.exec(p.text);if(!(!m||m[2].toLowerCase()!==c||m[3]))if(!m[1])h++;else if(h>0)h--;else{u=f;break}}}i.push({from:l,to:u===r?a:e[u].to}),r=u}return i}function Ch(o,e,t,n,i,r){let s=t.doc,l=t.views,a=l?(v,T)=>!!v.token&&l.has(v.token)&&!i?.has(Cn(r,T)):void 0;l?.has("html")&&(o=ha(o));let d=l?.has("html")&&o.some(v=>v.marks.includes("html"))?Rr(o).filter(v=>!i?.has(Cn(r,v.from))):[],u=v=>d.some(T=>v>=T.from&&v<T.to),c=v=>v.marks.includes("html"),h=a||d.length>0?(v,T)=>!!a?.(v,T)||c(v)&&u(T):void 0,f=Yo(o,n,h),p=v=>fa(Ih(v.span,t,v.atomic&&!c(v.span)),v.decorations,s),m=new Map,y=0;for(let v of f)m.set(v,y),y+=v.span.text.length;let g=v=>{let T=s.createElement("a"),E=t.resolveUrl(v.link,"link");return E!==null&&T.setAttribute("href",E),T.dataset.href=v.link,v.linkTitle&&(T.title=v.linkTitle),T.className=qe("mbe-link",t.classes.link),T},x=v=>d.find(T=>T.from===m.get(f[v])),b=(v,T)=>{let E=v;for(;E<f.length&&m.get(f[E])<T.to;)E++;return E},w=(v,T)=>{let E=f[v].span;if(!E.link)return!1;for(let A=v+1,fe=b(v,T);A<fe;A++)if(!ae(f[A].span,E))return!1;return!0},k=(v,T,E,A)=>{let fe=[],me=[],Se=b(v,T);for(let le=v;le<Se;){let Vr=f[le];if(c(Vr.span)){fe.push(Vr.span.text),le++;continue}let Wo=s.createDocumentFragment(),Ur=zr=>zr>=Se||c(f[zr].span);if(A)for(;!Ur(le);le++)Wo.appendChild(p(f[le]));else le=S(le,Ur,Wo);me.push(Wo),fe.push(ko(me.length-1))}let Te=t.view?.("html",fe.join(""),"inline");if(!Te)return null;Ar(Te,me.length,le=>me[le],s);let Rn=I(C(o,T.from,T.to));return E.appendChild(Oo(Te,qe("mbe-view","mbe-view-html",t.classes.token?.html),Rn)),Se},S=(v,T,E)=>{for(;v<f.length&&!T(v);){let A=f[v].span,fe=x(v);if(fe&&!w(v,fe)){let Te=k(v,fe,E,!1);if(Te!==null){v=Te;continue}}if(!A.link){E.appendChild(p(f[v])),v++;continue}let me=g(A),Se=v;for(;v<f.length&&!T(v)&&ae(f[v].span,A);){let Te=x(v);if(Te){let Rn=w(v,Te);if(!Rn&&v>Se)break;let le=Rn?k(v,Te,me,!0):null;if(le!==null){v=le;continue}}me.appendChild(p(f[v])),v++}E.appendChild(me)}return v};S(0,()=>!1,e)}function fa(o,e,t){for(let n=e.length-1;n>=0;n--){let i=e[n],r=t.createElement("span");for(let[s,l]of Object.entries(i.attributes??{}))r.setAttribute(s,l);r.className=`mbe-deco ${i.class}`,r.appendChild(o),o=r}return o}function Oo(o,e,t){return o.classList.add(...e.split(/\s+/).filter(Boolean)),o.contentEditable="false",o.setAttribute(ia,""),t!==void 0&&(o.dataset.source=t),o}function Ih(o,e,t=!1){let n=e.doc,i=t?e.view?.(o.token,o.text,"inline"):null;if(i)return na(o,Oo(i,qe("mbe-view",`mbe-view-${o.token}`,e.classes.token?.[o.token]),o.text),e);if(o.image){let s=o.image,l=e.resolveUrl(s.src,"image");if(l===null)return ta(s,e);let a=e.loadedImage?.(l);if(!a){let d=n.createElement("img");d.setAttribute("loading","lazy"),d.setAttribute("decoding","async"),e.referrerPolicy&&d.setAttribute("referrerpolicy",e.referrerPolicy),d.src=l,d.addEventListener("error",()=>{d.parentNode&&(d.replaceWith(ta({src:l,alt:d.alt,title:d.title||void 0},e)),e.onImageError?.())}),a=d}return a.alt=s.alt,kr(a,"title",s.title),kr(a,"width",s.width?String(s.width):void 0),kr(a,"height",s.height?String(s.height):void 0),Oo(a,qe("mbe-image",e.classes.image))}let r=n.createTextNode(o.text);if(o.token){let s=n.createElement("span");s.className=qe("mbe-token",`mbe-token-${o.token}`,e.classes.token?.[o.token]),s.spellcheck=!1,s.appendChild(r),r=s}return na(o,r,e)}function kr(o,e,t){t===void 0?o.removeAttribute(e):o.setAttribute(e,t)}function ta(o,e){let t=e.doc,n=t.createElement("span"),i=o.alt||e.labels.imageUnavailable;n.setAttribute("role","img"),n.setAttribute("aria-label",i),o.title&&(n.title=o.title);let r=e.icon?.("imageUnavailable");r&&n.appendChild(r);let s=t.createElement("span");return s.className="mbe-image-alt",s.textContent=i,n.appendChild(s),Oo(n,qe("mbe-image","mbe-image-unavailable",e.classes.image))}function na(o,e,t){for(let n=et.length-1;n>=0;n--){let i=et[n];if(!o.marks.includes(i))continue;let[r,s]=sh[i],l=t.doc.createElement(r),a=i==="html"?s:t.classes[i];a&&(l.className=a),l.appendChild(e),e=l}return e}var Ho=class{constructor(e,t){this.root=e;this.labels=t}rendered=new Map;numbers=new WeakMap;resolveUrl=Ln;referrerPolicy;decorate;decorateCode;decorateSource;onViewSettled;widgetsAfter;views;collectFootnotes=!1;footnotes;viewContext;blockViews=[];hosted=new Map;markdown;classes=lh;icon;codeCopy=!1;source=!1;onImageError;collapsed=new Set;revealed=new Map;viewCache=new Map;pendingViews=new Map;failedViews=new Set;viewBlocks=new Map;renderingBlock="";loadedImages=new Map;context(){return{doc:this.root.ownerDocument,labels:this.labels,resolveUrl:this.resolveUrl,referrerPolicy:this.referrerPolicy,decorate:this.decorate,decorateCode:this.decorateCode,decorateSource:this.decorateSource,views:this.views,view:this.views&&((e,t,n)=>this.renderView(e,t,n)),blockView:this.blockViews.length>0?e=>this.renderBlockView(e):void 0,markdown:this.markdown,classes:this.classes,icon:this.icon,codeCopy:this.codeCopy,source:this.source,onImageError:this.onImageError,loadedImage:e=>this.loadedImages.get(e)?.shift()}}renderView(e,t,n){let i=this.views?.get(e);if(!i||!this.viewContext)return null;let r=`${n}\0${e}\0${t}`;if(this.failedViews.has(r))return this.noteViewBlock(r),null;let s=this.viewCache.get(r);if(s)this.viewCache.delete(r);else{s=this.root.ownerDocument.createElement(n==="inline"?"span":"div");let l;try{l=i(t,s,n,this.viewContext)}catch{l=!1}if(l===!1)return this.noteViewBlock(r),null;if(l&&typeof l.then=="function"&&(s.setAttribute(xr,""),this.awaitView(r,l)),this.viewCache.size>=500){let a=this.viewCache.keys().next().value;this.viewCache.delete(a),this.viewBlocks.delete(a)}}return this.pendingViews.get(r)?.add(this.renderingBlock),this.noteViewBlock(r),this.viewCache.set(r,s),s.cloneNode(!0)}isHosted(e){return this.blockViews.some(({view:t})=>t.match(e))}renderBlockView(e){let t=this.hosted.get(e.id);if(t&&t.block===e)return t.after?null:t.el;t&&this.disposeHosted(e.id);let n=this.viewContext;if(!n)return null;let i=null;for(let{name:r,view:s}of this.blockViews){if(!s.match(e))continue;i=r;let l=this.root.ownerDocument.createElement("div"),a;try{a=s.render(e,l,n)}catch{a=!1}if(a===!1)continue;let d=typeof a=="function"?a:void 0;if(s.placement!=="after")return this.hosted.set(e.id,{block:e,name:r,el:l,dispose:d}),l;let u=this.root.ownerDocument.createElement("div");return u.className="mbe-block-view mbe-block-after",u.contentEditable="false",u.appendChild(l),this.hosted.set(e.id,{block:e,name:r,el:l,after:u,dispose:d}),null}return i!==null&&this.hosted.set(e.id,{block:e,name:i,el:null}),null}afterView(e,t){if(t?.has(Je)||this.source)return;let n=this.hosted.get(e.id);return n?.block===e?n.after:void 0}disposeHosted(e){let t=this.hosted.get(e);if(t){this.hosted.delete(e);try{t.dispose?.()}catch{}}}destroy(){for(let e of[...this.hosted.keys()])this.disposeHosted(e)}noteViewBlock(e){let t=this.viewBlocks.get(e);t||(this.viewBlocks.size>=1e3&&this.viewBlocks.delete(this.viewBlocks.keys().next().value),this.viewBlocks.set(e,t=new Set)),t.add(this.renderingBlock)}awaitView(e,t){let n=new Set;this.pendingViews.set(e,n);let i=r=>{this.pendingViews.get(e)===n&&(this.pendingViews.delete(e),this.viewCache.get(e)?.removeAttribute(xr),r||(this.failedViews.add(e),this.viewCache.delete(e)),n.size>0&&this.onViewSettled?.([...n]))};t.then(r=>i(r!==!1),()=>i(!1))}dropViews(e){let t=i=>e===void 0||i.split("\0",3)[1]===e;for(let i of[...this.viewCache.keys()])t(i)&&this.viewCache.delete(i);for(let i of[...this.failedViews])t(i)&&this.failedViews.delete(i);for(let i of[...this.pendingViews.keys()])t(i)&&this.pendingViews.delete(i);let n=new Set;for(let[i,r]of[...this.viewBlocks])if(t(i)){this.viewBlocks.delete(i);for(let s of r)this.rendered.has(s)&&n.add(s)}for(let[i,r]of[...this.hosted])e!==void 0&&r.name!==e||(this.disposeHosted(i),this.rendered.has(i)&&n.add(i));return[...n]}render(e,t=!1,n=!1){let i=this.context(),r=new Set;for(let u of e.blocks){let c=this.rendered.get(u.id);!t&&c&&c.block===u&&c.el.parentNode===this.root&&Lr(c.reveal,this.revealed.get(u.id))&&r.add(c.el)}if(!t){for(let{el:u}of this.rendered.values())if(!r.has(u))for(let c of Array.from(u.querySelectorAll("img.mbe-image"))){let h=c.getAttribute("src");if(h===null||!c.complete||c.naturalWidth===0)continue;let f=this.loadedImages.get(h);f?f.push(c):this.loadedImages.set(h,[c])}}let s=new Map;for(let u of e.blocks){this.renderingBlock=u.id;let c=this.rendered.get(u.id),h=this.revealed.get(u.id),f=c&&r.has(c.el)?c.el:null;!f&&i.source&&u.type==="code"&&c?.block.type==="code"&&c.el.parentNode===this.root&&this.patchSource(c.el,u,i)&&(f=c.el),f||(f=sa(u,i,h),c&&u.type==="raw"&&Bh(c.el,f)),s.set(u.id,{block:u,el:f,reveal:h})}this.loadedImages.clear();for(let[u,c]of[...this.hosted])s.get(u)?.block!==c.block&&this.disposeHosted(u);let l=this.collectFootnotes&&this.views?.has("footnote")===!0;for(let{block:u,el:c}of s.values())u.type==="raw"&&u.syntax==="footnote"&&c.classList.toggle("mbe-footnote-collected",l);let a=l?this.footnoteSection(e,s,i,t):null;l||(this.footnotes=void 0);let d=[];for(let[u,{block:c,el:h,reveal:f}]of s){d.push(h);let p=this.afterView(c,f);p&&d.push(p);let m=this.widgetsAfter?.(u);m&&d.push(...m)}if(a&&d.push(a),t||this.rendered.size===0){let u=this.root.ownerDocument.createDocumentFragment();for(let c of d)n&&c.hasAttribute(W)&&c.classList.add("mbe-lazy"),u.appendChild(c);this.root.replaceChildren(u)}else{let u=this.root.firstChild,c=!1;for(let h of d)h===u?u=u.nextSibling:(this.root.insertBefore(h,u),c=!0);if(c||u!==null||this.root.childNodes.length!==d.length){let h=new Set(d);for(let f of Array.from(this.root.childNodes))h.has(f)||f.remove()}}this.rendered=s,Er(e,u=>s.get(u)?.el,this.numbers,this.collapsed,this.labels)}footnoteSection(e,t,n,i){let r=new Map;for(let u of e.blocks){if(u.type!=="raw"||u.syntax!=="footnote")continue;let c=da(I(u.content));c&&!r.has(c.label)&&r.set(c.label,c)}if(r.size===0)return null;let s=[],l=new Map;for(let u of e.blocks){let c=t.get(u.id)?.el;if(c)for(let h of Array.from(c.querySelectorAll(".mbe-view-footnote")))for(let f of(h.dataset.source??"").matchAll(vh)){let p=ca(f[1]);if(!r.has(p))continue;let m=(l.get(p)??0)+1;l.set(p,m),m===1&&s.push(p)}}if(s.length===0)return null;let a=JSON.stringify(s.map(u=>[u,r.get(u).body,l.get(u)]));if(!i&&this.footnotes?.key===a)return this.footnotes.el;let d=n.doc.createElement("section");d.className="mbe-footnotes",d.contentEditable="false",d.setAttribute("aria-label",this.labels.footnotes);for(let u of s){let c=ua(r.get(u),n);if(!c)continue;let h=c.lastElementChild??c,f=h.querySelector(":scope > .mbe-flow");if(f){let p=f.lastElementChild;p?.tagName==="P"?h=p:(h=n.doc.createElement("p"),h.className="mbe-block mbe-paragraph",f.appendChild(h))}for(let p=1;p<=l.get(u);p++){let m=n.doc.createElement("button");m.type="button",m.className="mbe-footnote-backref",m.dataset.label=u,m.dataset.ref=String(p),m.setAttribute("aria-label",`${this.labels.backToReference} ${u}${p>1?` ${p}`:""}`);let y=n.icon?.("backToReference");if(y?m.appendChild(y):m.textContent="\u2191",p>1){let g=n.doc.createElement("sup");g.textContent=String(p),m.appendChild(g)}h.appendChild(m)}d.appendChild(c)}return this.footnotes={key:a,el:d},d}updateSections(e){Er(e,t=>this.rendered.get(t)?.el,this.numbers,this.collapsed,this.labels)}elementFor(e){return this.rendered.get(e)?.el}adopt(e){let t=this.rendered.get(e.id);t&&(this.rendered.set(e.id,{...t,block:e}),"content"in e&&t.el.classList.toggle("mbe-empty",B(e.content)===0))}invalidate(e){this.rendered.delete(e)}patchSource(e,t,n=this.context()){let i=se(e);return i?(aa(i,t,n),e.classList.toggle("mbe-empty",B(t.content)===0),!0):!1}refreshSource(e){let t=this.rendered.get(e.id);t&&t.el.parentNode===this.root&&this.patchSource(t.el,e)}};function Lh(o,e,t,n,i){let r=e>0?String(e):"";if(o.style.getPropertyValue("--mbe-section-depth")!==r&&(r?o.style.setProperty("--mbe-section-depth",r):o.style.removeProperty("--mbe-section-depth")),_t(o,rh,e>0),_t(o,oh,t),n===null)return;_t(o,ih,n);let s=o.querySelector(".mbe-details-toggle"),l=String(!n);s&&s.getAttribute("aria-expanded")!==l&&(s.setAttribute("aria-expanded",l),s.setAttribute("aria-label",n?i.expandSection:i.collapseSection))}function Er(o,e,t,n,i){let r=[],s,l=[];o.blocks.forEach((a,d)=>{let u=o.blocks[d-1],c=e(a.id);if(a.type==="detailsEnd"&&l.pop(),c&&Lh(c,l.length,l.includes(!0),a.type==="details"?n.has(a.id):null,i),a.type==="details"&&l.push(n.has(a.id)),M(a)!==null&&(_t(c,th,oe(u,a)),_t(c,nh,oe(a,o.blocks[d+1]))),(a.type!=="listItem"||u?.type!=="listItem"||M(u)!==M(a))&&(r.length=0,s=void 0),a.type!=="listItem")return;let h=a.listType==="ordered";if(_t(c,ra,r.length===0||a.indent===0&&s!==void 0&&h!==s),a.indent===0&&(s=h),r.length=a.indent+1,!h){r[a.indent]=void 0;return}let f=r[a.indent]===void 0?a.start??1:r[a.indent]+1;if(r[a.indent]=f,!c||t.get(c)===f)return;let p=c.querySelector(".mbe-marker");p&&(p.textContent=`${f}.`),t.set(c,f)})}function Bh(o,e){let t=o.querySelectorAll("details");if(t.length===0)return;let n=e.querySelectorAll("details");n.length===t.length&&t.forEach((i,r)=>{n[r].open=i.open})}function _t(o,e,t){o&&o.hasAttribute(e)!==t&&o.toggleAttribute(e,t)}function Ah(o,e){let t=e;for(;t&&t.parentNode!==o;)t=t.parentNode;return t instanceof HTMLElement&&t.hasAttribute(W)?t:null}function se(o,e){return e?o.querySelector(`[data-row="${e.row}"][data-col="${e.col}"]`):o.hasAttribute(Kt)?o:o.querySelector(`[${Kt}]`)}function Vt(o){let e=[],t=n=>{for(let i=n.firstChild;i;i=i.nextSibling)i.nodeType===3?e.push(i):i instanceof HTMLElement&&(i.hasAttribute(ia)?e.push(i):i.hasAttribute($o)||t(i))};return t(o),e}function Dr(o){return o.nodeType===3?o.data:o.dataset.source??"\uFFFC"}function Po(o){return Dr(o).length}function xt(o){return Array.prototype.indexOf.call(o.parentNode.childNodes,o)}function Or(o){return o.classList.contains(In)?gh(o):Vt(o).map(Dr).join("")}function oa(o,e){let t=0;for(let[n,i]of la(o)){if(n===e)return t;t+=i+1}return e===null&&t>0?t-1:t}function Mr(o,e,t){if(!o.contains(e))return e.compareDocumentPosition(o)&Node.DOCUMENT_POSITION_FOLLOWING?0:Vt(o).reduce((l,a)=>l+Po(a),0);if(o.classList.contains(In)){if(e===o)return oa(o,o.childNodes[t]??null);let s=(e instanceof Element?e:e.parentElement)?.closest(`.${Ir}`);if(s?.parentNode===o)return oa(o,s)+Mr(s,e,t)}let n=o.ownerDocument,i=n.createRange();i.setStart(e,t);let r=0;for(let s of Vt(o)){let l=Po(s);if(s===e)return r+Math.min(t,l);if(s.contains(e))return r;let a=n.createRange();if(s.nodeType===3?a.setStart(s,l):a.setStartAfter(s),a.compareBoundaryPoints(Range.START_TO_START,i)<=0)r+=l;else return r}return r}function _e(o,e,t){let n=e,i=t;if(e===o){let a=o.childNodes[Math.min(t,o.childNodes.length-1)];if(!a)return null;let d=t>=o.childNodes.length;if(n=a,i=d?a.childNodes.length:0,d){let u=a,c=u.hasAttribute?.(W)?se(u):null;if(c)return{blockId:u.getAttribute(W),offset:Or(c).length}}}let r=Ah(o,n);if(!r)return null;let s=r.getAttribute(W);if(r.classList.contains("mbe-divider"))return{blockId:s,offset:0};if(r.classList.contains("mbe-table")){let a=(n instanceof Element?n:n.parentElement)?.closest("[data-row]");if(!a||!r.contains(a))return{blockId:s,offset:0,cell:{row:0,col:0}};let d={row:Number(a.dataset.row),col:Number(a.dataset.col)};return{blockId:s,offset:Mr(a,n,i),cell:d}}let l=se(r);return l?{blockId:s,offset:Mr(l,n,i)}:{blockId:s,offset:0}}function pa(o,e){let t=se(o,e.cell);if(!t||t.classList.contains(In))return null;let n=0;for(let i of Vt(t)){if(n===e.offset&&i.nodeType!==3)return i;if(n+=Po(i),n>e.offset)return null}return null}function ma(o,e){let t=se(o,e.cell);if(!t)return{node:o.parentNode,offset:xt(o)};if(t.classList.contains(In)){let n=0,i=null;for(let r of la(t)){let[s,l]=r;if(e.offset<=n+l)return vr(s,e.offset-n);n+=l+1,i=r}return i?vr(i[0],i[1]):{node:t,offset:0}}return vr(t,e.offset)}function vr(o,e){let t=0,n=Vt(o);for(let r of n){let s=Po(r);if(r.nodeType===3){let l=r;if(t+s>=e){let a=e-t;if(a===s&&r===n[n.length-1]&&l.data.endsWith(`
|
|
114
114
|
`)){let d=o.querySelector(`[${$o}]`);if(d)return{node:d.parentNode,offset:xt(d)}}return{node:l,offset:a}}}else if(e<t+s)return{node:r.parentNode,offset:xt(r)+(t===e?0:1)};t+=s}let i=n[n.length-1];return i?i.nodeType===3?{node:i,offset:i.length}:{node:i.parentNode,offset:xt(i)+1}:{node:o,offset:0}}var qo=class{constructor(e){this.editor=e;let t=e.root,n=t.ownerDocument;this.el=n.createElement("button"),this.el.type="button",this.el.className="mbe-ui mbe-block-handle";let i=X(n,e.icon("blockHandle"));i&&this.el.appendChild(i),this.el.setAttribute("aria-label",e.labels.blockHandle),this.el.setAttribute("aria-haspopup","menu"),this.el.tabIndex=-1,this.el.hidden=!0;let r=(s,l,a)=>{s.addEventListener(l,a),this.cleanup.push(()=>s.removeEventListener(l,a))};r(t,"mousemove",s=>this.onMove(s)),r(t,"mouseleave",()=>this.scheduleHide()),r(this.el,"mouseenter",()=>this.cancelHide()),r(this.el,"mouseleave",()=>this.scheduleHide()),r(this.el,"mousedown",s=>s.preventDefault()),r(this.el,"click",()=>{this.blockId&&e.openBlockMenu(this.blockId,this.el.getBoundingClientRect())})}el;blockId=null;hideTimer=null;cleanup=[];onMove(e){if(!this.editor.isEditable)return;let n=e.target;for(;n&&n.parentElement!==this.editor.root;)n=n.parentElement;if(!n||!n.hasAttribute(W))return;let i=this.editor.blockMenuOwner(n.getAttribute(W));i?this.showFor(i):this.blockId!==null&&this.scheduleHide()}showFor(e){this.cancelHide();let t=this.editor.blockElement(e);if(!t)return this.hide();let n=this.editor.root.ownerDocument;this.blockId=e,this.el.hidden=!1;let i=$t(this.el,this.editor.root),r=t.getBoundingClientRect(),l=t.querySelector(":scope > .mbe-callout-icon")?.getBoundingClientRect(),a=parseFloat(n.defaultView.getComputedStyle(t).lineHeight)||24,d=l?.height?l.top+l.height/2:r.top+Math.min(r.height,a)/2,u=this.el.offsetHeight||22;this.el.style.left=`${Math.round(r.left-(this.el.offsetWidth||20)-6-i.x)}px`,this.el.style.top=`${Math.round(d-u/2-i.y)}px`}contains(e){return this.el.contains(e)}hide(){this.cancelHide(),this.el.hidden=!0,this.blockId=null}scheduleHide(){this.cancelHide(),this.hideTimer=setTimeout(()=>this.hide(),250)}cancelHide(){this.hideTimer!==null&&clearTimeout(this.hideTimer),this.hideTimer=null}destroy(){this.cancelHide();for(let e of this.cleanup)e();this.el.remove()}};var Nh=300,Rh=250,_o=class{constructor(e,t){this.editor=e;this.actions=t;let n=e.root.ownerDocument,i=e.labels;this.popup=new we(e.root,"mbe-link-card");let r=this.popup.el;r.setAttribute("role","toolbar"),r.setAttribute("aria-label",i.link),this.url=n.createElement("a"),this.url.className="mbe-link-card-url",this.url.target="_blank",this.url.rel="noopener noreferrer",this.url.addEventListener("click",f=>{f.preventDefault(),this.target&&t.open(this.target,f)});let s=(f,p)=>{let m=n.createElement("button");return m.type="button",m.className=`mbe-tool mbe-link-card-${f}`,ga(m,e,f),m.setAttribute("aria-label",i[f]),m.title=i[f],m.addEventListener("click",y=>{this.target&&p(y)}),m},l=s("openLink",f=>t.open(this.target,f));this.copyButton=s("copyLink",()=>this.copy());let a=s("editLink",()=>{let f=this.target;this.hide(),t.edit(f)}),d=s("removeLink",()=>{let f=this.target;this.hide(),t.remove(f)}),u=n.createElement("span");u.className="mbe-tool-sep",r.append(this.url,u,l,this.copyButton,a,d);let c=e.root,h=(f,p,m)=>{f.addEventListener(p,m),this.cleanup.push(()=>f.removeEventListener(p,m))};h(c,"mouseover",f=>this.onOver(f)),h(c,"mouseout",f=>this.onOut(f)),h(r,"mouseenter",()=>this.cancelHide()),h(r,"mouseleave",()=>{this.hovered&&this.scheduleHide()})}popup;url;copyButton;target=null;hovered=null;caret=null;showTimer=null;hideTimer=null;copiedTimer=null;cleanup=[];get isOpen(){return this.popup.isOpen}contains(e){return this.popup.el.contains(e)}setCaret(e){this.caret=e,this.hovered&&!this.hovered.el.isConnected&&(this.hovered=null),this.render()}reposition(){if(this.target){if(!this.target.el.isConnected)return this.hide();this.place(this.target)}}hide(){this.clearTimers(),this.hovered=null,this.caret=null,this.target=null,this.popup.hide()}destroy(){this.clearTimers(),this.copiedTimer!==null&&clearTimeout(this.copiedTimer);for(let e of this.cleanup)e();this.popup.destroy()}onOver(e){if(!this.editor.isEditable||e.buttons!==0)return;let t=e.target?.closest?.("a.mbe-link");!t||!this.editor.root.contains(t)||(this.cancelHide(),this.hovered?.el!==t&&(this.showTimer!==null&&clearTimeout(this.showTimer),this.showTimer=setTimeout(()=>{this.showTimer=null;let n=this.actions.targetOf(t);!n||!this.editor.isEditable||(this.hovered=n,this.render())},this.popup.isOpen?0:Nh)))}onOut(e){let t=e.target?.closest?.("a.mbe-link");if(!t)return;let n=e.relatedTarget;n&&(t.contains(n)||this.popup.el.contains(n))||(this.showTimer!==null&&(clearTimeout(this.showTimer),this.showTimer=null),this.hovered&&this.scheduleHide())}render(){let e=this.hovered??this.caret;if(!e){this.target=null,this.popup.hide();return}if(this.target?.el!==e.el||this.target.href!==e.href){this.target=e,this.url.textContent=e.href,this.url.title=e.href;let t=e.el.getAttribute("href");t?this.url.href=t:this.url.removeAttribute("href");let n=this.popup.el.querySelector(".mbe-link-card-openLink");n&&(n.disabled=!t),this.showCopied(!1)}this.place(e)}place(e){let t=e.el.getClientRects(),n=t.length>0?t[t.length-1]:e.el.getBoundingClientRect();this.popup.show(n,"below",4)}copy(){let e=this.target.href,t=()=>this.showCopied(!0),n=this.editor.root.ownerDocument.defaultView?.navigator.clipboard;n&&n.writeText(e).then(t,()=>{})}showCopied(e){this.copiedTimer!==null&&clearTimeout(this.copiedTimer),this.copiedTimer=null;let t=this.copyButton,n=e?"copied":"copyLink";ga(t,this.editor,n),t.setAttribute("aria-label",this.editor.labels[n]),t.title=this.editor.labels[n],e&&(this.copiedTimer=setTimeout(()=>this.showCopied(!1),1500))}scheduleHide(){this.cancelHide(),this.hideTimer=setTimeout(()=>{this.hideTimer=null,this.hovered=null,this.render()},Rh)}cancelHide(){this.hideTimer!==null&&clearTimeout(this.hideTimer),this.hideTimer=null}clearTimers(){this.cancelHide(),this.showTimer!==null&&clearTimeout(this.showTimer),this.showTimer=null}};function ga(o,e,t){let n=X(o.ownerDocument,e.icon(t));o.replaceChildren(...n?[n]:[])}function Hr(o,e,t,n){let i=new Map;if(!t)return i;let{anchor:r,focus:s}=o.selection,l=N(o.doc,s.blockId),a=r.blockId===s.blockId?l:N(o.doc,r.blockId);for(let[d,u]of[[s,l],[r,a]]){let c=i.get(u.id)??new Set;if(n?.(u)&&c.add(Je),u.type==="raw")e.has(u.syntax)&&c.add(Je);else if(u.type==="code")e.has(Cr(u.language))&&c.add(Je);else{let h=F(u,d.cell)??[];for(let{token:f,from:p}of Xr(h,d.offset))e.has(f)&&c.add(Cn(d.cell,p));if(e.has("html")&&h.some(f=>f.marks.includes("html")))for(let f of Rr(h))f.from<=d.offset&&d.offset<=f.to&&c.add(Cn(d.cell,f.from))}c.size>0&&i.set(u.id,c)}return i}function ba(o,e){return o.size===e.size&&[...o].every(([t,n])=>Lr(n,e.get(t)))}function Bn(o,e){if(o.type==="table"){let i=e?0:o.rows.length-1;return{blockId:o.id,offset:e?0:B(o.rows[i][0]),cell:{row:i,col:0}}}if(o.type!=="raw"&&o.type!=="code")return e?V(o):P(o);let t=I(o.content);if(o.type==="code"||o.syntax==="markdown")return{blockId:o.id,offset:e?0:t.length};if(e){let i=t.indexOf(`
|
|
115
115
|
`);return{blockId:o.id,offset:i>=0&&i<t.length-1?i+1:0}}let n=t.lastIndexOf(`
|
|
116
|
-
`);return{blockId:o.id,offset:n>0?n:t.length}}var Dh=o=>typeof o?.then=="function";function ya(o){return(e,t,n,i)=>{let r=t.ownerDocument.createElement("template");r.innerHTML=e;let s=a=>{if(!a)return!1;t.replaceChildren(a)},l=o(r.content,{editor:i.editor,kind:n});return Dh(l)?l.then(s):s(l)}}function Pr(o,e){let t=o.ownerDocument,n=t.defaultView,i=e.top,r=e.bottom,s=e.left,l=e.right;for(let c=o;c&&c!==t.body&&c!==t.documentElement;c=c.parentElement){let h=n?.getComputedStyle(c),f=c.scrollHeight>c.clientHeight&&/auto|scroll|overlay/.test(h?.overflowY??""),p=c.scrollWidth>c.clientWidth&&/auto|scroll|overlay/.test(h?.overflowX??"");if(!f&&!p)continue;let m=c.getBoundingClientRect(),y=m.top+c.clientTop,g=m.left+c.clientLeft,x=f?Ko(i,r,y,y+c.clientHeight):0,b=p?Ko(s,l,g,g+c.clientWidth):0;if(!x&&!b)continue;let w=c.scrollTop,k=c.scrollLeft;c.scrollTop+=x,c.scrollLeft+=b,i-=c.scrollTop-w,r-=c.scrollTop-w,s-=c.scrollLeft-k,l-=c.scrollLeft-k}if(!n)return;let a=t.documentElement,d=Ko(i,r,0,a.clientHeight),u=Ko(s,l,0,a.clientWidth);(d||u)&&n.scrollBy(u,d)}function Ko(o,e,t,n){return o<t+8?o-t-8:e>n-8?Math.min(e-n+8,o-t-8):0}function ka(o){let e={};for(let t of o)for(let[n,i]of Object.entries(t??{}))i!==void 0&&(e[n]=typeof i=="object"?{...e[n],...i}:i);return e}var Oh={text:"--mbe-text",muted:"--mbe-muted",background:"--mbe-bg",accent:"--mbe-accent",link:"--mbe-link",codeBackground:"--mbe-code-bg",border:"--mbe-border",popupBackground:"--mbe-popup-bg",popupBorder:"--mbe-popup-border",hover:"--mbe-hover",danger:"--mbe-danger",highlight:"--mbe-highlight",calloutNote:"--mbe-callout-note",calloutTip:"--mbe-callout-tip",calloutImportant:"--mbe-callout-important",calloutWarning:"--mbe-callout-warning",calloutCaution:"--mbe-callout-caution"},An={font:"--mbe-font",mono:"--mbe-mono",sourceFont:"--mbe-source-font",fontSize:"--mbe-font-size",lineHeight:"--mbe-line-height",radius:"--mbe-radius",blockGap:"--mbe-block-gap"},va=[An.font,An.mono,An.fontSize,An.lineHeight];function $r(o){let e=new Map;if(!o)return e;for(let t of["light","dark"])for(let[n,i]of Object.entries(Oh)){let r=o[t]?.[n]??o.colors?.[n];r&&e.set(`${i}-${t}`,r)}for(let[t,n]of Object.entries(An)){let i=o[t];i!==void 0&&i!==""&&e.set(n,typeof i=="number"?`${i}px`:i)}return e}var xa=/^(?:[ \t]*>[ \t]?)*/,Ut=/[ \t]*(?:[-*+]|\d{1,9}[.)])(?=[ \t\n\r]|$)/y,Hh=/^(`{3,}|~{3,})/,Ph=/^\[[ \t\nxX]\](?=[ \t\n]|$)/;function wa(o,e){let t=he(o,e),n=pt(o),i=[],r=[],s=(g,x,b)=>{b>x&&i.push({from:x,to:b,kind:g})},l=g=>({from:n.starts[g-1]??o.length,to:n.ends[g-1]??o.length}),a=(g,x,b,w)=>{for(let k of bs(g,n,x,b))s(w,k.from,k.to)},d=(g,x)=>{for(let b=g;b<x;b++){let w=o[b];if(w===">")s("marker",b,b+1);else if(w==="-"||w==="*"||w==="+"||w>="0"&&w<="9"){Ut.lastIndex=b;let k=Ut.exec(o);k&&k.index===b&&(s("marker",b+k[0].length-k[0].trimStart().length,b+k[0].length),b+=k[0].length-1)}}},u=g=>{let x=xa.exec(o.slice(g.from,g.to))[0];d(g.from,g.from+x.length),Ut.lastIndex=g.from+x.length;let b=g.from+x.length;for(let w=Ut.exec(o);w&&w.index===b;w=Ut.exec(o))s("marker",b+w[0].length-w[0].trimStart().length,b+w[0].length),b+=w[0].length,Ut.lastIndex=b;return b},c=g=>{for(let x of g.lines){let b=n.starts[x.line-1]??o.length;d(b,b+x.offset)}},h=(g,x)=>{for(let b=g;b<x;b++)o[b]==="\\"&&b+1<x?(o[b+1]==="|"&&s("escape",b,b+2),b++):o[b]==="|"&&s("marker",b,b+1)},f=(g,x,b)=>{g&&(c(g),b&&x.length>0&&a(g,0,Math.max(...x.map(w=>w.pos?.to??0)),b),p(g,x))},p=(g,x)=>{let b=w=>{for(let k of w){let{pos:S}=k;if(S)switch(k.type){case"text":if(k.pieces){let v=S.from;for(let T of k.pieces)a(g,v,T.from,"escape"),v=T.to;a(g,v,S.to,"escape")}break;case"hardBreak":{let v=new xe(g,n).source(S.from);o[v]==="\\"&&s("marker",v,v+1);break}case"code":{for(let v of k.syntax??[])a(g,v.from,v.to,"marker");k.content&&a(g,k.content.from,k.content.to,"code");break}case"html":a(g,S.from,S.to,"html");break;case"token":a(g,S.from,S.to,"token");break;case"emphasis":case"strong":case"strike":{let v=k.syntax?.find(E=>E.kind==="open"),T=k.syntax?.find(E=>E.kind==="close");for(let E of k.syntax??[])a(g,E.from,E.to,"marker");a(g,v?.to??S.from,T?.from??S.to,k.type==="emphasis"?"em":k.type),b(k.children);break}case"link":case"image":{let v=k.syntax?.find(A=>A.kind==="open"),T=k.syntax?.find(A=>A.kind==="close"),E=k.syntax?.find(A=>A.kind==="dest");for(let A of k.syntax??[])A.kind==="dest"?a(g,A.from,A.to,"url"):A.kind==="tail"&&E&&E.from>=A.from&&E.to<=A.to?(a(g,A.from,E.from,"marker"),a(g,E.to,A.to,"marker")):A.kind!=="title"&&a(g,A.from,A.to,"marker");v&&T&&!(E&&E.from===v.to&&E.to===T.from)&&a(g,v.to,T.from,"link"),b(k.children);break}default:break}}};b(x)},m=g=>{switch(g.type){case"paragraph":case"heading":case"htmlBlock":case"raw":return g.source?.lines[0]?.line??0;case"codeBlock":case"thematicBreak":case"table":return g.lines?.start??0;case"blockQuote":case"listItem":return g.children[0]?m(g.children[0]):0;case"list":return g.items[0]?m(g.items[0]):0}},y=g=>{switch(g.type){case"paragraph":f(g.source,g.children);break;case"heading":{f(g.source,g.children,"heading");let x=g.source?.lines[0];if(!x)break;if(g.setext){let b=l(x.line+(g.source?.lines.length??1)),w=xa.exec(o.slice(b.from,b.to))[0];d(b.from,b.from+w.length),s("marker",b.from+w.length,b.to)}else{let b=l(x.line),w=o.slice(b.from,b.from+x.offset),k=/#{1,6}[ \t]*$/.exec(w);k&&s("marker",b.from+k.index,b.from+k.index+k[0].trimEnd().length);let S=o.slice(b.from+x.offset,b.to),v=/(?:^|[ \t]+)(#+)[ \t]*$/.exec(S);v&&s("marker",b.from+x.offset+v.index+v[0].indexOf("#"),b.from+x.offset+v.index+v[0].trimEnd().length)}break}case"thematicBreak":if(g.lines){let x=l(g.lines.start);s("marker",u(x),x.to)}break;case"blockQuote":g.children.forEach(y);break;case"list":for(let x of g.items){if(x.lines&&m(x)!==x.lines.start&&u(l(x.lines.start)),x.task){let b=x.children[0],k=(b?.type==="paragraph"?b.source:void 0)?.lines[0];if(k){let S=(n.starts[k.line-1]??0)+k.offset;Ph.test(o.slice(S,n.ends[k.line-1]))&&s("marker",S,S+3)}}x.children.forEach(y)}break;case"codeBlock":{if(!g.source||!g.lines)break;let x=g.source.lines;if(g.fenced){let b=l(g.lines.start),w=u(b),k=o.slice(w,b.to),S=k.trimStart(),v=w+k.length-S.length,T=Hh.exec(S);T&&(s("marker",v,v+T[1].length),s("lang",v+T[1].length,b.to));let E=x[x.length-1]?.line??g.lines.start;if(g.lines.end>E){let A=l(g.lines.end);s("marker",u(A),A.to)}}if(c(g.source),x.length>0){let b=g.value.replace(/\n$/,"").length;a(g.source,0,b,"code");let w=new xe(g.source,n);r.push({from:w.source(0),to:w.source(b),language:Bi(g.info)})}break}case"htmlBlock":case"raw":g.source&&(c(g.source),a(g.source,0,g.value.length,g.type==="raw"?"token":"html"));break;case"table":if(g.lines){for(let x=g.lines.start;x<=g.lines.end;x++){let b=l(x),w=u(b);x===g.lines.start+1?s("marker",w,b.to):h(w,b.to)}g.sources?.forEach((x,b)=>x.forEach((w,k)=>p(w,g.rows[b]?.[k]??[])))}break}};return t.children.forEach(y),i.sort((g,x)=>g.from-x.from||x.to-g.to),{ranges:i,code:r}}var Sa={marker:"mbe-src-marker",heading:"mbe-src-heading",em:"mbe-src-em",strong:"mbe-src-strong",strike:"mbe-src-strike",code:"mbe-src-code",link:"mbe-src-link",url:"mbe-src-url",html:"mbe-src-html",token:"mbe-src-token",escape:"mbe-src-escape",lang:"mbe-src-lang"},$h=new Set(["marker","code","url","html","token","escape","lang"]),Ta=2e4,Ea=1e3,Fh=80;function qh(o,e,t,n){let i=n.highlight?_h(t,n.classes):[];if(n.decorators.length>0){let r=Pn(o,La(e,t));for(let s of n.decorators)i.push(...s(r))}if(n.codeDecorators.length>0)for(let r of t.code){let s={block:o,text:e.slice(r.from,r.to),language:r.language};for(let l of n.codeDecorators.flatMap(a=>a(s))){let a=r.from+Math.max(0,l.from),d=r.from+Math.min(r.to-r.from,l.to);d>a&&i.push({...l,from:a,to:d})}}return i}var Vo=class{constructor(e,t,n){this.options=e;this.config=t;this.onReady=n}last=null;markupFor=null;spansFor=null;pending=null;markup(e){return this.markupFor?.text!==e&&(this.markupFor={text:e,markup:wa(e,this.options)}),this.markupFor.markup}spans(e){let t=this.markupFor?.text===e?this.markupFor.markup:null;if(this.spansFor?.text===e&&(!t||this.spansFor.markup===t))return this.spansFor.spans;let n=t;if(!n&&this.markupFor&&e.length>Ta){let r=Ma(this.markupFor.markup.ranges,this.markupFor.text,e);r&&(n={ranges:r,code:this.markupFor.markup.code})}n??=this.markup(e);let i=La(e,n);return this.spansFor={text:e,markup:n,spans:i},i}decorations(e){let t=I(e.content),n=this.last;if(n&&n.text===t)return n.decorations;if(n&&t.length>Ta){let i=Ma(n.decorations,n.text,t);if(i)return this.last={text:t,decorations:i},this.schedule(e),i}return this.read(e,t)}reset(){this.last=null}destroy(){this.pending!==null&&clearTimeout(this.pending),this.pending=null}read(e,t){this.destroy();let n=qh(e,t,this.markup(t),this.config());return this.last={text:t,decorations:n},n}schedule(e){this.destroy(),this.pending=setTimeout(()=>{this.pending=null;let t=this.last;if(!t)return;let n=this.read(e,t.text);$n(t.decorations,n)||this.onReady()},Fh)}};function Ma(o,e,t){let{from:n,toBefore:i,toAfter:r}=dr(e,t);if(i-n>Ea||r-n>Ea)return null;let s=t.length-e.length,l=[];for(let a of o){let d=a.from<n?a.from:a.from>=i?a.from+s:n,u=a.to<=n?a.to:a.to>=i?a.to+s:n;u>d&&l.push(d===a.from&&u===a.to?a:{...a,from:d,to:u})}return l}function _h(o,e){return o.ranges.map(t=>{let n=e?.[t.kind];return{from:t.from,to:t.to,class:n?`${Sa[t.kind]} ${n}`:Sa[t.kind]}})}var Ca=["code"],Ia=[];function La(o,e){let t=[],n=(s,l,a)=>{l>s&&t.push({text:o.slice(s,l),marks:a})},i=0,r=0;for(let s of e.ranges)$h.has(s.kind)&&(s.from>r&&(n(i,r,Ca),n(r,s.from,Ia),i=s.from),r=Math.max(r,s.to));return n(i,r,Ca),n(r,o.length,Ia),t}var Pa=["text","heading1","heading2","heading3","heading4","bulletList","numberedList","todoList","quote","callout","details","code","table","divider"],Kh=[{id:"text",aliases:["paragraph","plain"],spec:{type:"paragraph"}},{id:"heading1",aliases:["h1","title"],spec:{type:"heading",level:1}},{id:"heading2",aliases:["h2","subtitle"],spec:{type:"heading",level:2}},{id:"heading3",aliases:["h3"],spec:{type:"heading",level:3}},{id:"heading4",aliases:["h4"],spec:{type:"heading",level:4}},{id:"bulletList",aliases:["ul","unordered","bullet"],spec:{type:"listItem",listType:"bullet"}},{id:"numberedList",aliases:["ol","ordered"],spec:{type:"listItem",listType:"ordered"}},{id:"todoList",aliases:["task","checkbox","checklist"],spec:{type:"listItem",listType:"task"}},{id:"quote",aliases:["blockquote","citation"],spec:{type:"quote"}},{id:"callout",aliases:["alert","admonition","note","tip","important","warning","caution"],spec:{type:"quote",callout:"note"}},{id:"details",aliases:["toggle","collapse","collapsible","summary","spoiler","fold"],spec:{type:"details"}},{id:"code",aliases:["pre","snippet","fence"],spec:{type:"code"}},{id:"table",aliases:["grid"],spec:{type:"table"}},{id:"divider",aliases:["hr","separator","line","rule"],spec:{type:"divider"}}],Vh=[{id:"addRowAbove",action:{kind:"addRowAbove"}},{id:"addRowBelow",action:{kind:"addRowBelow"}},{id:"addColumnLeft",action:{kind:"addColumnLeft"}},{id:"addColumnRight",action:{kind:"addColumnRight"}},{id:"deleteRow",action:{kind:"deleteRow"}},{id:"deleteColumn",action:{kind:"deleteColumn"}}],Uh=[{id:"alignLeft",align:"left",image:"imageAlignLeft"},{id:"alignCenter",align:"center",image:"imageAlignCenter"},{id:"alignRight",align:"right",image:"imageAlignRight"}],zh=["/","\uFF0F"],Ba=".mbe-widget, .mbe-block-after",Aa=/^(Arrow(Up|Down|Left|Right)|Home|End|PageUp|PageDown)$/,Na=new Set(["insertCompositionText","deleteCompositionText","insertFromComposition","deleteByComposition"]),Fr=class{root;labels;commands;suggestion;current;source;markdown;history=new oo(()=>this.source);modeValue="rich";pendingMode=null;renderer;composing=!1;composition=null;compositionEndedAt=0;shortcut=null;unsettled=new Set;settleFrame=null;readOnly;wasReadOnly=!1;taskToggle;smartLists;sourceHighlighting;sourceHighlighter;options;extensions;bindings;runtime;classesKey="{}";tokenNames=[];listeners=new Map;cleanup=[];widgets=[];changeFrame=null;plainPasteNext=!1;destroyed=!1;focused=!1;pointerDown=!1;toolbarClosedFor=null;typing=0;caretKey=null;suggestions;blockMenu;toolbar;imageToolbar;inputPopover;blockHandle;collapsed=new Set;linkCard;resizeHandles;selectedImageEl=null;hintEl=null;blockSel=null;selectedEls=[];headingIdsFor=null;redecoratePending=!1;viewFrame=0;constructor(e){let t=e.element;this.root=t,this.options=e,this.runtime={},this.labels={...Mi,...e.labels},this.readOnly=e.editable===!1,this.taskToggle=e.taskToggle===!0,this.smartLists=e.smartLists!==!1,this.sourceHighlighting=e.sourceHighlighting!==!1;let n=e.extensions??[];this.extensions=n.some(c=>c.name==="defaultKeymap")?n:[...n,rr()],this.bindings=this.extensions.flatMap(c=>Object.entries(c.keymap??{}).map(([h,f])=>Wl(h,f,re)));let i=n.flatMap(c=>c.markdown?[c.markdown]:[]);this.markdown=Ze({...e.markdown,syntax:[...e.markdown?.syntax??[],...i]});let r=Le(e.content??"",this.markdown);this.source=r.source,this.current=Hn(r.doc),this.renderer=new Ho(t,this.labels),this.renderer.collapsed=this.collapsed,this.collapseClosedSections(this.current.doc),this.renderer.widgetsAfter=c=>{if(this.widgets.length===0)return;let h=this.widgets.filter(f=>f.blockId===c).map(f=>f.element);return h.length?h:void 0};let s=[["markdown",Nr]];e.sanitizeHtml&&s.push(["html",ya(e.sanitizeHtml)]),this.renderer.views=new Map([...s,...this.extensions.flatMap(c=>Object.entries(c.views??{}).filter(([h])=>h!=="html"))]),this.renderer.viewContext={editor:this},this.renderer.blockViews=this.extensions.flatMap(c=>c.blockView?[{name:c.name,view:c.blockView}]:[]),this.renderer.markdown=this.markdown,this.renderer.onViewSettled=c=>this.viewsSettled(c);let l=this.extensions.flatMap(c=>c.codeDecorations?[c.codeDecorations]:[]);l.length>0&&(this.renderer.decorateCode=c=>{let h={block:c,text:I(c.content),language:c.language};return l.flatMap(f=>f(h))});let a=this.extensions.flatMap(c=>c.decorations?[c.decorations]:[]);a.length>0&&(this.renderer.decorate=(c,h,f)=>{let p=Pn(c,h,f);return a.flatMap(m=>m(p))}),this.sourceHighlighter=new Vo(this.markdown,()=>({highlight:this.sourceHighlighting,classes:this.renderer.classes.source,decorators:a,codeDecorators:l}),()=>this.refreshSource()),this.renderer.decorateSource=c=>this.sourceHighlighter.decorations(c),this.commands=this.createCommands(),t.classList.add("mbe-root"),t.setAttribute("role","textbox"),t.setAttribute("aria-multiline","true"),t.spellcheck=!0,t.translate=!1,t.style.setProperty("--mbe-slash-hint",JSON.stringify(this.labels.slashHint)),this.applyRuntime({placeholder:e.placeholder,attributes:e.attributes}),this.runtime={...this.runtime,resolveUrl:e.resolveUrl,onLinkClick:e.onLinkClick},this.renderer.resolveUrl=e.resolveUrl??Ln,this.renderer.referrerPolicy=e.referrerPolicy,this.renderer.codeCopy=e.codeCopy!==!1,this.renderer.icon=c=>X(t.ownerDocument,this.icon(c)),this.applyTheme(e.theme),ir(t,e.popupContainer);let d=this.extensions.flatMap(c=>c.suggestion?[c.suggestion]:[]);e.slashMenu!==!1&&d.push(this.slashSuggestion()),this.suggestions=new Co({editor:this,root:t,syncActiveDescendant:()=>this.syncActiveDescendant(),refresh:()=>this.updateUi(),endComposition:()=>this.endComposition(),changed:c=>this.emit("suggestionChange",{editor:this,state:c})},d);let u=this.suggestions;this.suggestion={get state(){return u.state},open:c=>!this.destroyed&&(this.refreshSelection(),u.open(c)),close:()=>u.dismiss(),move:c=>u.move(c),accept:c=>!this.destroyed&&u.accept(c)},this.blockMenu=new Xe(t,"mbe-block-menu","menu"),this.blockMenu.onPicked=()=>this.blockMenu.hide(),this.blockMenu.onHighlight=()=>this.syncActiveDescendant(),this.blockMenu.checkIcon=this.icon("selected"),this.blockMenu.submenuIcon=this.icon("chevronRight"),this.inputPopover=new Lo(this),this.toolbar=e.toolbar===!1?null:new Io(this,typeof e.toolbar=="object"?e.toolbar.items:void 0),this.toolbar&&(this.toolbar.onMenuChange=()=>this.syncActiveDescendant()),this.imageToolbar=e.toolbar===!1?null:new Ao(this,{align:(c,h)=>this.updateImageAt(c.pos,{align:h==="left"?null:h}),altText:c=>this.editAltText(c.pos,c.image.alt),resetSize:c=>this.updateImageAt(c.pos,{width:null}),remove:c=>this.deleteImageAt(c.pos)}),this.blockHandle=new qo(this),this.linkCard=e.linkCard===!1?null:new _o(this,{open:(c,h)=>this.openHref(c.el.getAttribute("href"),h),edit:c=>this.editLink(c),remove:c=>this.removeLink(c),targetOf:c=>this.linkTargetOf(c)}),this.resizeHandles=new Bo(this),this.renderer.onImageError=()=>{this.destroyed||(this.composing||this.syncDomSelection(),this.updateUi())},this.listen(t,"beforeinput",this.typed(c=>this.onBeforeInput(c))),this.listen(t,"input",this.typed(c=>this.onInput(c))),this.listen(t,"compositionstart",c=>this.onCompositionStart(c)),this.listen(t,"compositionend",this.typed(c=>this.onCompositionEnd(c))),this.listen(t,"keydown",this.typed(c=>this.onKeyDown(c))),this.listen(t,"copy",c=>this.onCopy(c,!1)),this.listen(t,"cut",this.typed(c=>this.onCopy(c,!0))),this.listen(t,"paste",this.typed(c=>this.onPaste(c))),this.listen(t,"dragstart",c=>c.preventDefault()),this.listen(t,"dragover",c=>{this.readOnly||c.preventDefault()}),this.listen(t,"drop",this.typed(c=>this.onDrop(c))),this.listen(t,"mousedown",c=>this.onMouseDown(c)),this.listen(t,"click",c=>this.onClick(c)),this.listen(t,"change",c=>this.onControlChange(c)),this.listen(t,"focusin",c=>this.onFocusIn(c)),this.listen(t,"focusout",c=>this.onFocusOut(c)),this.listen(t.ownerDocument,"selectionchange",()=>this.onSelectionChange()),this.listen(t.ownerDocument,"mouseup",()=>this.onMouseUp()),this.listen(t.ownerDocument,"scroll",()=>this.onScroll(),!0),this.listen(t.ownerDocument,"pointerdown",c=>this.onDocumentPointerDown(c),!0),e.mode==="source"&&this.enterSource(),this.paint(!0,!0),this.afterRender();for(let c of this.extensions){let h=c.onCreate?.({editor:this});typeof h=="function"&&this.cleanup.push(h)}e.autofocus&&this.commands.focus(e.autofocus==="end"?"end":"start")}getMarkdown(){return this.modeValue==="source"?K(this.current.doc):Pt(this.current.doc,{...this.markdown,source:this.source})}setMarkdown(e,t={}){if(this.destroyed)return;if(t.history==="keep"?this.history.record(this.current,"other"):this.history.clear(),this.modeValue==="source"){this.suggestions.reset(),this.current=Fe(e,0);for(let r of this.widgets)r.offset=Math.min(r.offset,e.length);this.paint(!0,!0),this.afterRender(),this.emit("selectionChange",{editor:this});return}let n=Le(e,this.markdown);this.source=n.source,this.blockSel=null,this.suggestions.reset();let i=Hn(n.doc);this.collapsed.clear(),this.collapseClosedSections(i.doc),this.relocateWidgets(this.current.doc,i.doc,!0),this.current=i,this.paint(!0,!0),this.afterRender(),this.emit("selectionChange",{editor:this})}get isEmpty(){return this.modeValue==="source"?K(this.current.doc)==="":this.current.doc.blocks.every(e=>e.type==="paragraph"&&B(e.content)===0)}get state(){return this.current}get isComposing(){return this.composing}get mode(){return this.pendingMode??this.modeValue}get isEditable(){return!this.readOnly&&!this.destroyed}get isDestroyed(){return this.destroyed}isActive(e,t){if(this.modeValue==="source")return!1;let n=this.current;switch(e){case"bold":case"italic":case"strike":case"code":return Vn(n,e);case"link":return Qe(n)!==null;case"image":{let s=De(n);if(!s||!t)return s!==null;let l={...s.image,block:ke(n)!==null};return Object.entries(t).every(([a,d])=>l[a]===d)}}let i=N(n.doc,n.selection.focus.blockId);if(Da(i)!==e&&!(e==="blockquote"&&M(i)))return!1;if(!t)return!0;let r=this.getAttributes(e);return Object.entries(t).every(([s,l])=>r[s]===l)}getAttributes(e){if(this.modeValue==="source")return{};let t=this.current;if(e==="link"){let r=Qe(t);return r?{href:r.href}:{}}if(e==="image"){let r=De(t);return r?{...r.image,block:ke(t)!==null}:{}}let n=N(t.doc,t.selection.focus.blockId),i=M(n);if(e==="blockquote"&&i)return i==="quote"?{}:{callout:i};if(Da(n)!==e)return{};switch(n.type){case"heading":return{level:n.level};case"code":return{language:n.language};case"listItem":return n.listType==="task"?{checked:n.checked,indent:n.indent}:{indent:n.indent};case"raw":return{syntax:n.syntax};case"quote":return n.callout?{callout:n.callout}:{};case"details":return{open:n.open,collapsed:this.collapsed.has(n.id)};default:return{}}}getSelection(){return this.current.selection}setSelection(e){if(this.destroyed)return!1;let t=jh(e)?te(this.current.doc,e):Gh(this.current.doc,e);return t?(this.dropBlockSelection(),this.current={...this.current,selection:t,storedMarks:null},this.history.breakGroup(),this.refreshRevealed(),this.syncDomSelection(),this.updateUi(),this.emit("selectionChange",{editor:this}),!0):!1}selectionToJSON(){let e=this.current.doc,t=n=>{let i=L(e,n.blockId);return n.cell?{block:i,offset:n.offset,cell:{row:n.cell.row,col:n.cell.col}}:{block:i,offset:n.offset}};return{anchor:t(this.current.selection.anchor),focus:t(this.current.selection.focus)}}hasFocus(){let e=this.root.ownerDocument.activeElement;return!!e&&this.root.contains(e)&&!(e instanceof HTMLInputElement)&&!e.closest(Ba)}addWidget(e){if(this.destroyed){let l=new kt(this.root.ownerDocument.createElement("div"),"",()=>{},()=>{});return l.remove(),l}let t=this.current.doc.blocks,n=e.at==="end"?P(t[t.length-1]):e.at,i=te(this.current.doc,{anchor:n,focus:n}).focus.blockId,r=this.root.ownerDocument.createElement("div");r.className="mbe-widget",r.contentEditable="false";let s=new kt(r,i,e.render,l=>{this.widgets=this.widgets.filter(a=>a!==l)});return this.modeValue==="source"&&(s.offset=e.at==="end"?K(this.current.doc).length:n.offset),this.widgets.push(s),this.paint(),s}relocateWidgets(e,t,n){if(this.widgets.length===0)return;let i=new Set(t.blocks.map(r=>r.id));for(let r of this.widgets){if(i.has(r.blockId))continue;let s=e.blocks.findIndex(a=>a.id===r.blockId),l;if(n)l=t.blocks[Math.max(0,Math.min(s,t.blocks.length-1))].id;else{for(let a=s-1;a>=0&&!l;a--)i.has(e.blocks[a].id)&&(l=e.blocks[a].id);for(let a=s+1;a<e.blocks.length&&!l&&s>=0;a++)i.has(e.blocks[a].id)&&(l=e.blocks[a].id)}r.blockId=l??t.blocks[0].id}}focus(){if(this.destroyed)return;let e=this.current.selection.focus,t=this.renderer.elementFor(e.blockId);((e.cell&&t?oe(t,e.cell):this.root)??this.root).focus({preventScroll:!0}),this.syncDomSelection(!0)}blur(){this.hasFocus()&&this.root.ownerDocument.activeElement?.blur()}setOptions(e){if(this.destroyed)return;e.extensions&&this.suggestions.setConfigs(e.extensions.flatMap(n=>n.suggestion?[n.suggestion]:[]));let t=e.taskToggle!==void 0&&e.taskToggle!==this.taskToggle;t&&(this.taskToggle=e.taskToggle===!0),e.editable!==void 0&&e.editable===this.readOnly?(this.readOnly=!e.editable,this.renderer.views?.has("footnote")?this.paint():this.refreshRevealed(),this.applyReadOnly(),this.hideUi()):t&&this.applyReadOnly(),this.applyRuntime(e),e.mode!==void 0&&this.setMode(e.mode)}setMode(e){if(e!=="rich"&&e!=="source")return;if(this.composing||this.composition){this.pendingMode=e===this.modeValue?null:e;return}if(this.pendingMode=null,e===this.modeValue)return;let t=this.hasFocus(),n=this.caretRect()?.top;this.blockSel=null,this.shortcut=null,this.suggestions.reset(),this.hideUi(),this.closeBlockMenu(),this.history.breakGroup(),e==="source"?this.enterSource():this.leaveSource(),this.paint(!0,!1,t),this.afterRender();let i=this.caretRect()?.top;n!==void 0&&i!==void 0&&Math.abs(i-n)>1&&Xh(this.root,i-n),this.emit("selectionChange",{editor:this})}enterSource(){let{doc:e,selection:t}=this.current,n=this.getMarkdown(),i=Le(n,this.markdown),r=hr(e,t,i);for(let s of this.widgets)s.offset=Ol(e,s.blockId,i),s.blockId=xn;this.collapsed.clear(),this.current=Fe(n,r.anchor,r.focus),this.modeValue="source",this.renderer.source=!0,this.root.classList.add("mbe-source-mode")}leaveSource(){let{anchor:e,focus:t}=this.current.selection,n=this.loadSource(K(this.current.doc));for(let r of this.widgets)r.blockId=wo(r.offset,n).blockId;let i=fr(e.offset,t.offset,n);this.current={doc:n.doc,selection:te(n.doc,i),storedMarks:null},this.modeValue="rich",this.renderer.source=!1,this.root.classList.remove("mbe-source-mode")}loadSource(e){let t=Le(e,this.markdown),n=Hn(t.doc).doc;return this.source=t.source,this.collapsed.clear(),this.collapseClosedSections(n),{doc:n,source:t.source}}restore(e){let{state:t}=e,n=So(t.doc);if(this.modeValue==="source"){if(n)return t;let s=Pt(t.doc,{...this.markdown,source:e.source}),l=hr(t.doc,t.selection,Le(s,this.markdown));return Fe(s,l.anchor,l.focus)}if(!n)return this.source=e.source,t;let i=this.loadSource(K(t.doc)),r=fr(t.selection.anchor.offset,t.selection.focus.offset,i);return{doc:i.doc,selection:te(i.doc,r),storedMarks:null}}on(e,t){let n=this.listeners.get(e);return n||this.listeners.set(e,n=new Set),n.add(t),()=>void n.delete(t)}emit(e,t){let n=this.listeners.get(e);if(n)for(let i of[...n])i(t)}applyRuntime(e){let t=this.root;if("placeholder"in e&&(e.placeholder?(t.style.setProperty("--mbe-placeholder",JSON.stringify(e.placeholder)),t.setAttribute("aria-placeholder",e.placeholder)):(t.style.removeProperty("--mbe-placeholder"),t.removeAttribute("aria-placeholder")),this.runtime={...this.runtime,placeholder:e.placeholder}),"attributes"in e){for(let n of Object.keys(this.runtime.attributes??{}))t.removeAttribute(n);for(let[n,i]of Object.entries(e.attributes??{}))t.setAttribute(n,i);this.runtime={...this.runtime,attributes:e.attributes}}"resolveUrl"in e&&e.resolveUrl!==this.runtime.resolveUrl&&(this.runtime={...this.runtime,resolveUrl:e.resolveUrl},this.renderer.resolveUrl=e.resolveUrl??Ln,this.paint(!0)),"onLinkClick"in e&&(this.runtime={...this.runtime,onLinkClick:e.onLinkClick}),"theme"in e&&this.applyTheme(e.theme)&&this.paint(!0)}applyTheme(e){let t=$r(this.runtime.theme?.tokens);this.runtime={...this.runtime,theme:e};let n=this.root.style,i=$r(e?.tokens);for(let a of this.tokenNames)i.has(a)||n.removeProperty(a);for(let[a,d]of i)n.setProperty(a,d);this.tokenNames=[...i.keys()];let r=e?.colorScheme??"auto";n.colorScheme=r==="auto"?"":r,va.some(a=>t.get(a)!==i.get(a))&&this.renderBlocksAgain(this.renderer.dropViews());let s=ka([...this.extensions.map(a=>a.theme?.classes).reverse(),e?.classes]),l=JSON.stringify(s);return l===this.classesKey?!1:(this.classesKey=l,this.renderer.classes=s,!0)}run(e,t="other"){if(this.readOnly||this.destroyed)return!1;let n=e(this.current);return!n||n===this.current||(n=bi(n,this.current,this.markdown.inline),n=yi(n,this.current),n=li(n),n.doc!==this.current.doc&&Qh(n.doc,this.current.doc)&&(n={...n,doc:this.current.doc}),n.doc===this.current.doc&&n.selection===this.current.selection&&n.storedMarks===this.current.storedMarks)?!1:(this.apply(n,t),!0)}createCommands(){let e=l=>this.run(l),t=l=>e(a=>ht(a,l)),n={toggleBold:()=>e(l=>We(l,"bold")),toggleItalic:()=>e(l=>We(l,"italic")),toggleStrike:()=>e(l=>We(l,"strike")),toggleCode:()=>e(l=>We(l,"code")),setParagraph:()=>e(l=>Ge(l,{type:"paragraph"})),toggleHeading:({level:l})=>t({type:"heading",level:l}),toggleBulletList:()=>t({type:"listItem",listType:"bullet"}),toggleOrderedList:()=>t({type:"listItem",listType:"ordered"}),toggleTaskList:()=>t({type:"listItem",listType:"task"}),toggleBlockquote:()=>t({type:"quote"}),toggleCallout:(l={})=>this.markdown.callouts&&t({type:"quote",callout:l.type??"note"}),toggleDetails:()=>this.markdown.details&&t({type:"details"}),setCallout:({type:l})=>(this.markdown.callouts||l===null)&&e(a=>sn(a,a.selection.focus.blockId,l)),toggleCodeBlock:(l={})=>t({type:"code",language:l.language}),setLink:({href:l})=>e(a=>ut(a,l)),unsetLink:()=>e(l=>ut(l,null)),updateImage:l=>e(a=>{let d=De(a);return d?Un(a,d.pos,l):null}),insertText:(l,a={})=>e(d=>{let u=a.range;if(!u)return ue(d,l);let c=te(d.doc,{anchor:u.from,focus:u.to});return ue({...d,selection:c,storedMarks:null},l)}),insertToken:l=>!this.composing&&e(a=>ni(a,l)),insertMarkdown:(l,a={})=>{let d=bt(l,this.markdown).blocks,u=a.at;if(u instanceof kt){if(u.isRemoved)return!1;let c=N(this.current.doc,u.blockId);return e(h=>cn(h,d,{...a,at:P(c),block:!0}))}return e(c=>cn(c,d,a))},insertTable:(l={})=>e(a=>Rt(a,{type:"table",rows:l.rows,cols:l.cols})),insertDivider:()=>e(l=>Rt(l,{type:"divider"})),indent:()=>e(l=>Wn(l,1)),outdent:()=>e(l=>Wn(l,-1)),undo:()=>this.undo(),redo:()=>this.redo(),selectAll:()=>(this.setSelection(xi(this.current).selection),!0),selectBlocks:()=>{if(this.readOnly)return!1;this.hasFocus()||this.root.focus({preventScroll:!0});let{anchor:l,focus:a}=this.current.selection;return this.selectBlockRange(l.blockId,a.blockId),!0},focus:l=>{if(l!==void 0){let a=this.current.doc.blocks,d=l==="start"?V(a[0]):l==="end"?P(a[a.length-1]):l;this.current={...this.current,selection:te(this.current.doc,{anchor:d,focus:d})}}return this.focus(),this.emit("selectionChange",{editor:this}),!0},blur:()=>{let l=this.hasFocus();return this.blur(),l},scrollIntoView:()=>{let l=this.caretRect();return l?(Pr(this.root,l),!0):!1}},i={...n},r=(l,a)=>{let d=i[l];n[l]=(...u)=>this.modeValue==="source"?a?a(...u):!1:d(...u)},s=l=>e(a=>(this.smartLists?Rl(a,l):null)??ln(a,l));r("indent",()=>s(1)),r("outdent",()=>s(-1)),r("insertMarkdown",(l,a={})=>this.insertSourceMarkdown(l,a)),r("toggleBold",()=>e(l=>Tn(l,"**"))),r("toggleItalic",()=>e(l=>Tn(l,"*"))),r("toggleStrike",()=>e(l=>Tn(l,"~~"))),r("toggleCode",()=>e(l=>Tn(l,"`"))),r("setParagraph",()=>e(l=>pr(l,0))),r("toggleHeading",({level:l})=>e(a=>pr(a,l))),r("toggleBulletList",()=>e(l=>Eo(l,"bullet"))),r("toggleOrderedList",()=>e(l=>Eo(l,"ordered"))),r("toggleTaskList",()=>e(l=>Eo(l,"task"))),r("toggleBlockquote",()=>e(l=>Fl(l))),r("toggleCodeBlock",()=>e(l=>ql(l)));for(let l of["toggleCallout","setCallout","toggleDetails","setLink","unsetLink","updateImage","insertTable","insertDivider","selectBlocks"])r(l,null);for(let l of Object.keys(n)){let a=n[l];n[l]=(...d)=>!this.destroyed&&a(...d)}return n}insertSourceMarkdown(e,t){let n=t.at??"selection",i=t.select==="keep",r=t.block===!0;return n instanceof kt?n.isRemoved?!1:this.run(s=>To(s,n.offset,e,!0,i)):n==="selection"&&!r&&!i?this.run(s=>Ae(s,e)):this.run(s=>{let l=s,a;if(n==="selection"){let{from:d,to:u}=ne(s);d!==u&&(l=Ae(s,"")),a=d}else a=n==="end"?K(s.doc).length:n.offset;return To(l,a,e,r,i)})}undo(){if(this.readOnly)return!1;let e=this.history.undo(this.current);return e?(this.commit(this.restore(e)),!0):!1}redo(){if(this.readOnly)return!1;let e=this.history.redo(this.current);return e?(this.commit(this.restore(e)),!0):!1}selectionRect(){let e=this.root.ownerDocument.getSelection();if(!e||e.rangeCount===0)return null;let t=e.getRangeAt(0),n=t.getClientRects(),i=n.length?R(this.current.selection)?n[n.length-1]:t.getBoundingClientRect():null;if(i&&(i.width>0||i.height>0))return i;let r=this.renderer.elementFor(this.current.selection.focus.blockId);return((r?oe(r,this.current.selection.focus.cell):null)??r)?.getBoundingClientRect()??null}caretRect(){let e=this.current.selection.focus,t=this.toDom(e);if(t){let i=this.root.ownerDocument.createRange();i.setStart(t.node,t.offset);let r=i.getClientRects(),s=r[r.length-1];if(s&&s.height>0)return s}let n=this.renderer.elementFor(e.blockId);return((n&&oe(n,e.cell))??n)?.getBoundingClientRect()??null}textRect(e,t){let n=this.toDom(e),i=this.toDom(t);if(!n||!i)return null;let r=this.root.ownerDocument.createRange();r.setStart(n.node,n.offset),r.setEnd(i.node,i.offset);let s=r.getClientRects()[0];if(!s)return null;let l=n.node.nodeType===1?n.node:n.node.parentElement,a=!!l&&this.root.ownerDocument.defaultView.getComputedStyle(l).direction==="rtl";return{rect:s,rtl:a}}scrollCaretIntoView(){let e=this.caretRect();e&&Pr(this.root,e)}blockElement(e){return this.renderer.elementFor(e)}destroy(){if(!this.destroyed){this.destroyed=!0,this.changeFrame!==null&&cancelAnimationFrame(this.changeFrame),this.settleFrame!==null&&cancelAnimationFrame(this.settleFrame),this.sourceHighlighter.destroy();for(let e of this.cleanup)e();this.suggestions.destroy();for(let e of[...this.widgets])e.remove();this.blockMenu.destroy(),this.toolbar?.destroy(),this.imageToolbar?.destroy(),this.inputPopover.destroy(),this.blockHandle.destroy(),this.linkCard?.destroy(),this.resizeHandles.destroy(),this.renderer.destroy(),ir(this.root,void 0);for(let e of Object.keys(this.runtime.attributes??{}))this.root.removeAttribute(e);for(let e of this.tokenNames)this.root.style.removeProperty(e);this.root.style.colorScheme="",this.root.contentEditable="false",this.root.classList.remove("mbe-root"),this.root.replaceChildren(),this.listeners.clear()}}apply(e,t){e.doc!==this.state.doc&&this.history.record(this.state,t),this.commit(e)}commit(e){let t=e.doc!==this.state.doc,n=e.selection!==this.state.selection,i=this.hasFocus();if(t&&this.modeValue==="source"){let r=K(this.current.doc),s=K(e.doc);for(let l of this.widgets)l.offset=Dl(l.offset,r,s)}else t&&this.relocateWidgets(this.current.doc,e.doc,!1);if(this.current=e,this.blockSel){let r=this.blockRange();r?this.current={...e,selection:an(e.doc.blocks,r)}:this.blockSel=null}this.paint(!1,!1,i),this.afterRender(),n&&i&&this.typing>0&&this.scrollCaretIntoView(),t&&this.scheduleChange(),this.emit("selectionChange",{editor:this})}flushChange(){this.changeFrame!==null&&(cancelAnimationFrame(this.changeFrame),this.changeFrame=null,this.emitChange())}scheduleChange(){if(this.changeFrame!==null||!this.options.onChange&&!this.listeners.get("change")?.size)return;let e=()=>{this.changeFrame=null,this.destroyed||this.emitChange()};typeof requestAnimationFrame=="function"?this.changeFrame=requestAnimationFrame(e):e()}emitChange(){this.options.onChange&&this.options.onChange(this.getMarkdown(),this),this.emit("change",{editor:this})}submit(){return this.destroyed||!this.options.onSubmit&&!this.listeners.get("submit")?.size?!1:(this.flushChange(),this.options.onSubmit?.(this.getMarkdown(),this),this.emit("submit",{editor:this}),!0)}applyReadOnly(){let e=this.readOnly?"false":"true";if(this.root.contentEditable!==e&&(this.root.contentEditable=e),!this.readOnly&&!this.wasReadOnly)return;this.wasReadOnly=this.readOnly;let t=this.ticking;this.root.classList.toggle("mbe-readonly",this.readOnly),this.root.classList.toggle("mbe-tasks",t),this.root.setAttribute("aria-readonly",String(this.readOnly)),this.root.querySelectorAll("[data-row]").forEach(n=>{n.contentEditable=this.readOnly?"false":"true"}),this.root.querySelectorAll("input").forEach(n=>{let i=n.classList.contains("mbe-checkbox");n.disabled=this.readOnly&&!(i&&t),i&&(n.tabIndex=t?0:-1,t?n.setAttribute("aria-label",Zh(n,this.labels.task)):n.removeAttribute("aria-label"))})}get ticking(){return this.readOnly&&this.taskToggle}toggleTaskAt(e){let t=e.closest("[data-block-id]");if(!t)return;let n=t.dataset.blockId,i=e===this.root.ownerDocument.activeElement;if(!this.readOnly)this.run(r=>Qn(r,n));else if(this.ticking&&this.current.doc.blocks.some(r=>r.id===n))this.apply(Qn(this.current,n),"other");else return;i&&this.root.querySelector(`[data-block-id="${n}"] .mbe-checkbox`)?.focus({preventScroll:!0})}afterRender(){for(let e of this.state.doc.blocks){let t=this.renderer.elementFor(e.id);if(!t||!t.classList.contains("mbe-empty"))continue;let n="";e.type==="heading"?n=this.labels[`heading${Math.min(e.level,4)}`]:e.type==="listItem"?n=this.labels.listHint:e.type==="quote"&&!e.callout?n=this.labels.quoteHint:e.type==="details"&&(n=this.labels.detailsHint),n&&t.dataset.hint!==n&&(t.dataset.hint=n)}this.updateUi()}syncDomSelection(e=!1){let t=this.root.ownerDocument.getSelection();if(!t||!e&&!this.hasFocus())return;let n=this.parkedCaret(),i=n?{anchor:n,focus:n}:this.state.selection,r=this.toDom(i.anchor),s=R(i)?r:this.toDom(i.focus);if(!(!r||!s)&&!(t.anchorNode===r.node&&t.anchorOffset===r.offset&&t.focusNode===s.node&&t.focusOffset===s.offset)){if(i.focus.cell){let l=(s.node instanceof Element?s.node:s.node.parentElement)?.closest("[data-row]");l&&this.root.ownerDocument.activeElement!==l&&l.focus({preventScroll:!0})}else this.root.ownerDocument.activeElement!==this.root&&this.hasFocus()&&this.root.focus({preventScroll:!0});t.setBaseAndExtent(r.node,r.offset,s.node,s.offset)}}toDom(e){let t=this.renderer.elementFor(e.blockId);return t?ma(t,e):null}readDomSelection(){let e=this.root.ownerDocument.getSelection();if(!e||!e.anchorNode||!e.focusNode||!this.root.contains(e.anchorNode)||!this.root.contains(e.focusNode))return null;let t=_e(this.root,e.anchorNode,e.anchorOffset),i=e.anchorNode===e.focusNode&&e.anchorOffset===e.focusOffset?t:_e(this.root,e.focusNode,e.focusOffset);return!t||!i?null:{anchor:t,focus:i}}onSelectionChange(){if(this.composing||this.destroyed)return;let e=this.readDomSelection();if(!e){this.hasFocus()||this.updateUi();return}let t=this.caretKey;if(this.caretKey=null,this.blockSel){this.updateUi();return}let n=this.state.selection;if(be(e.anchor,n.anchor)&&be(e.focus,n.focus)){this.updateUi();return}this.shortcut=null;let i=t?.vertical?this.skippedBlock(t.from,t.vertical,t.x,e.focus):null,r=this.imageBlockAt(i?{anchor:i,focus:i}:e);r?!be(r.anchor,n.anchor)||!be(r.focus,n.focus)?this.setSelection(r):this.updateUi():i?this.setSelection({anchor:i,focus:i}):(this.current={...this.state,selection:e,storedMarks:null},this.history.breakGroup(),this.pointerDown||this.refreshRevealed(),this.updateUi(),this.emit("selectionChange",{editor:this})),t&&this.scrollCaretIntoView()}imageBlockAt(e){if(!R(e)||e.focus.cell)return null;let t=this.state.doc.blocks.find(i=>i.id===e.focus.blockId),n=t?Y(t):null;return n===null?null:{anchor:{blockId:t.id,offset:n},focus:{blockId:t.id,offset:n+1}}}skippedBlock(e,t,n,i){let r=this.state.doc.blocks,s=L(this.state.doc,i.blockId);for(let l=e+t;(s-l)*t>0;l+=t){let a=r[l];if(!this.skipsCaret(a))return this.blockEntry(a,t===1,n)}return null}blockEntry(e,t,n){let i=this.renderer.elementFor(e.id),r=this.viewBlock(e);if(r)return Bn(r,t);if(i){let s=i.ownerDocument.createRange();s.selectNodeContents(oe(i)??i);let l=Array.from(s.getClientRects()).filter(u=>u.height>0),a=t?l[0]:l[l.length-1],d=a?this.positionAtPoint(n,a.top+a.height/2):null;if(d?.blockId===e.id)return d}if(e.type==="table"){let s=t?0:e.rows.length-1;return{blockId:e.id,offset:t?0:B(e.rows[s][0]),cell:{row:s,col:0}}}return t?V(e):P(e)}paint(e=!1,t=!1,n=this.hasFocus()){let i=this.renderer.views;i&&!this.pointerDown&&!this.composing&&(this.renderer.revealed=Hr(this.state,i,!this.readOnly&&n,this.hosted)),this.renderer.collectFootnotes=this.readOnly,this.renderer.render(this.state.doc,e,t),this.applyHeadingIds(),this.applyReadOnly(),this.paintBlockSelection(),this.composing||this.syncDomSelection(n)}sourceSpans(e){return this.sourceHighlighter.spans(I(e.content))}refreshSource(){if(this.destroyed||this.modeValue!=="source"||this.composing||this.composition)return;let e=this.current.doc.blocks[0];if(e?.type!=="code")return;let t=this.hasFocus();this.renderer.refreshSource(e),this.pointerDown||this.syncDomSelection(t)}applyHeadingIds(){let e=this.options.headingIds;if(!e)return;let t=this.state.doc;if(this.headingIdsFor?.doc!==t){let n=t.blocks.filter(r=>r.type==="heading"),i=e(n.map(r=>({text:I(r.content),level:r.level})));this.headingIdsFor={doc:t,ids:new Map(n.map((r,s)=>[r.id,i[s]]))}}for(let[n,i]of this.headingIdsFor.ids){let r=this.renderer.elementFor(n);r&&(i?r.id!==i&&(r.id=i):r.id&&r.removeAttribute("id"))}}redecorate(){if(!this.destroyed){if(this.composing||this.composition){this.redecoratePending=!0;return}this.redecoratePending=!1,this.sourceHighlighter.reset(),this.paint(!0),this.afterRender()}}refreshViews(e){this.destroyed||this.renderBlocksAgain(this.renderer.dropViews(e))}renderBlocksAgain(e){if(e.length!==0){if(this.composing||this.composition){for(let t of e)this.unsettled.add(t);return}for(let t of e)this.renderer.invalidate(t);this.paint(),this.afterRender()}}viewsSettled(e){if(!this.destroyed){if(this.composing||this.composition){for(let t of e)this.unsettled.add(t);return}for(let t of e)this.renderer.invalidate(t);this.viewFrame||=requestAnimationFrame(()=>{this.viewFrame=0,!(this.destroyed||this.composing||this.composition)&&(this.paint(),this.afterRender())})}}viewBlock(e){return e&&this.renderer.elementFor(e.id)?.classList.contains("mbe-view-block")?e:null}refreshRevealed(){let e=this.renderer.views;if(!e||this.pointerDown)return;let t=Hr(this.state,e,!this.readOnly&&this.hasFocus(),this.hosted);ba(t,this.renderer.revealed)||this.paint()}hosted=e=>this.renderer.isHosted(e);onMouseUp(){this.pointerDown&&(this.pointerDown=!1,!this.destroyed&&!this.composing&&this.refreshRevealed())}blockPast(e){let t=this.state.selection;if(!R(t)||t.focus.cell)return null;let n=t.focus,i=this.state.doc.blocks,r=L(this.state.doc,n.blockId),s=e==="ArrowDown"||e==="ArrowRight",l=i[r+(s?1:-1)],a=this.viewBlock(l);if(!l||!a&&l.type!=="table")return null;let d=i[r];if(!q(d)||e==="ArrowRight"&&n.offset!==P(d).offset||e==="ArrowLeft"&&n.offset!==0)return null;if(e==="ArrowDown"||e==="ArrowUp"){let u=oe(this.renderer.elementFor(d.id));if(u&&!this.onEdgeLine(u,s))return null}return Bn(l,s)}imageBlockStep(e){let t=this.state.selection,n=this.state.doc.blocks,i=e==="ArrowDown"||e==="ArrowRight",r=ke(this.state);if(r){let u=L(this.state.doc,r.pos.blockId);for(let c=u+(i?1:-1);c>=0&&c<n.length;c+=i?1:-1){let h=n[c];if(this.skipsCaret(h))continue;let f=Y(h);if(f!==null)return{anchor:{blockId:h.id,offset:f},focus:{blockId:h.id,offset:f+1}};let m=e==="ArrowDown"||e==="ArrowUp"?this.blockEntry(h,i,this.selectionRect()?.left??0):i?V(h):P(h);return{anchor:m,focus:m}}return t}if(!R(t)||t.focus.cell)return null;let s=L(this.state.doc,t.focus.blockId),l=n[s],a=n[s+(i?1:-1)],d=a?Y(a):null;if(!a||d===null||!q(l)||e==="ArrowRight"&&t.focus.offset!==P(l).offset||e==="ArrowLeft"&&t.focus.offset!==0)return null;if(e==="ArrowDown"||e==="ArrowUp"){let u=oe(this.renderer.elementFor(l.id));if(u&&!this.onEdgeLine(u,i))return null}return{anchor:{blockId:a.id,offset:d},focus:{blockId:a.id,offset:d+1}}}tableCellPast(e){let t=this.state.selection,n=t.focus;if(!R(t)||!n.cell)return null;let i=this.state.doc.blocks,r=L(this.state.doc,n.blockId),s=i[r];if(s?.type!=="table")return null;let l=oe(this.renderer.elementFor(s.id),n.cell);if(l&&!this.onEdgeLine(l,e===1))return null;let{row:a,col:d}=n.cell,u=s.rows[a+e];if(u){let c=Math.min(n.offset,B(u[d]));return{blockId:s.id,offset:c,cell:{row:a+e,col:d}}}for(let c=r+e;c>=0&&c<i.length;c+=e){let h=i[c];if(this.skipsCaret(h))continue;if(h.type==="table"){let p=e===1?0:h.rows.length-1,m=Math.min(d,h.align.length-1);return{blockId:h.id,offset:e===1?0:B(h.rows[p][m]),cell:{row:p,col:m}}}let f=this.viewBlock(h);return f?Bn(f,e===1):e===1?V(h):P(h)}return null}onEdgeLine(e,t){let n=this.selectionRect();if(!n||n.height===0)return!0;let i=e.ownerDocument.createRange();i.selectNodeContents(e);let r=Array.from(i.getClientRects()).filter(l=>l.height>0);if(r.length===0)return!0;let s=n.height/2;return t?Math.max(...r.map(l=>l.bottom))-n.bottom<=s:n.top-Math.min(...r.map(l=>l.top))<=s}revealView(e){let t=e.hasAttribute(W)?N(this.state.doc,e.getAttribute(W)):null,n=t?Bn(t,!1):_e(this.root,e.parentNode,xt(e)+1);n&&(this.root.focus({preventScroll:!0}),this.setSelection({anchor:n,focus:n}))}refreshSelection(){if(this.blockSel)return;let e=this.readDomSelection();e&&(this.current={...this.state,selection:e})}targetRange(e){let t=e.getTargetRanges?.()[0];if(!t)return null;let n=_e(this.root,t.startContainer,t.startOffset),i=_e(this.root,t.endContainer,t.endOffset);return n&&i?{anchor:n,focus:i}:null}fromControl(e){return e.target instanceof HTMLInputElement||!!e.target?.closest?.(Ba)}onBeforeInput(e){if(this.fromControl(e)||this.composing||e.isComposing)return;let t=e.inputType;if(!Na.has(t)&&!(!e.cancelable&&this.browserApplies(e))&&(e.preventDefault(),!this.readOnly)){if(this.blockSel){t.startsWith("delete")?this.deleteSelectedBlocks():t==="historyUndo"?this.undo():t==="historyRedo"&&this.redo();return}if(this.refreshSelection(),!((t==="insertText"||t==="insertReplacementText")&&ke(this.state))&&!(this.modeValue==="source"&&this.onSourceInput(t)))switch(t){case"insertText":{let n=e.data??"",i=R(this.state.selection)?this.targetRange(e):null;i&&!R(i)&&(this.current={...this.state,selection:i}),n?this.typeText(n):i&&!R(i)&&this.run(J,"typing");return}case"insertReplacementText":{let n=e.dataTransfer?.getData("text/plain")??e.data??"",i=this.targetRange(e);this.run(r=>ue(i?{...r,selection:i}:r,n));return}case"insertParagraph":{if(this.suggestions.isOpen&&this.suggestions.accept())return;this.run(n=>this.collapsed.has(n.selection.focus.blockId)?ri(n):Kn(n));return}case"insertLineBreak":this.run(ii);return;case"deleteContentBackward":this.undoShortcut()||this.run(nn);return;case"deleteContentForward":this.run(_n);return;case"deleteWordBackward":case"deleteWordForward":case"deleteSoftLineBackward":case"deleteSoftLineForward":case"deleteHardLineBackward":case"deleteHardLineForward":case"deleteEntireSoftLine":{if(!R(this.state.selection)){this.run(J);return}let n=this.targetRange(e);n&&!R(n)&&n.anchor.blockId===n.focus.blockId?this.run(i=>J({...i,selection:n})):this.run(this.modeValue==="source"?t.endsWith("Backward")?i=>ar(i,en):i=>cr(i,tn):t.endsWith("Backward")?nn:_n);return}case"historyUndo":this.undo();return;case"historyRedo":this.redo();return;case"formatBold":this.commands.toggleBold();return;case"formatItalic":this.commands.toggleItalic();return;case"formatStrikeThrough":this.commands.toggleStrike();return;default:return}}}onSourceInput(e){switch(e){case"insertParagraph":return this.run(t=>this.smartLists?Nl(t):Ae(t,`
|
|
116
|
+
`);return{blockId:o.id,offset:n>0?n:t.length}}var Dh=o=>typeof o?.then=="function";function ya(o){return(e,t,n,i)=>{let r=t.ownerDocument.createElement("template");r.innerHTML=e;let s=a=>{if(!a)return!1;t.replaceChildren(a)},l=o(r.content,{editor:i.editor,kind:n});return Dh(l)?l.then(s):s(l)}}function Pr(o,e){let t=o.ownerDocument,n=t.defaultView,i=e.top,r=e.bottom,s=e.left,l=e.right;for(let c=o;c&&c!==t.body&&c!==t.documentElement;c=c.parentElement){let h=n?.getComputedStyle(c),f=c.scrollHeight>c.clientHeight&&/auto|scroll|overlay/.test(h?.overflowY??""),p=c.scrollWidth>c.clientWidth&&/auto|scroll|overlay/.test(h?.overflowX??"");if(!f&&!p)continue;let m=c.getBoundingClientRect(),y=m.top+c.clientTop,g=m.left+c.clientLeft,x=f?Ko(i,r,y,y+c.clientHeight):0,b=p?Ko(s,l,g,g+c.clientWidth):0;if(!x&&!b)continue;let w=c.scrollTop,k=c.scrollLeft;c.scrollTop+=x,c.scrollLeft+=b,i-=c.scrollTop-w,r-=c.scrollTop-w,s-=c.scrollLeft-k,l-=c.scrollLeft-k}if(!n)return;let a=t.documentElement,d=Ko(i,r,0,a.clientHeight),u=Ko(s,l,0,a.clientWidth);(d||u)&&n.scrollBy(u,d)}function Ko(o,e,t,n){return o<t+8?o-t-8:e>n-8?Math.min(e-n+8,o-t-8):0}function ka(o){let e={};for(let t of o)for(let[n,i]of Object.entries(t??{}))i!==void 0&&(e[n]=typeof i=="object"?{...e[n],...i}:i);return e}var Oh={text:"--mbe-text",muted:"--mbe-muted",background:"--mbe-bg",accent:"--mbe-accent",link:"--mbe-link",codeBackground:"--mbe-code-bg",border:"--mbe-border",popupBackground:"--mbe-popup-bg",popupBorder:"--mbe-popup-border",hover:"--mbe-hover",danger:"--mbe-danger",highlight:"--mbe-highlight",calloutNote:"--mbe-callout-note",calloutTip:"--mbe-callout-tip",calloutImportant:"--mbe-callout-important",calloutWarning:"--mbe-callout-warning",calloutCaution:"--mbe-callout-caution"},An={font:"--mbe-font",mono:"--mbe-mono",sourceFont:"--mbe-source-font",fontSize:"--mbe-font-size",lineHeight:"--mbe-line-height",radius:"--mbe-radius",blockGap:"--mbe-block-gap"},va=[An.font,An.mono,An.fontSize,An.lineHeight];function $r(o){let e=new Map;if(!o)return e;for(let t of["light","dark"])for(let[n,i]of Object.entries(Oh)){let r=o[t]?.[n]??o.colors?.[n];r&&e.set(`${i}-${t}`,r)}for(let[t,n]of Object.entries(An)){let i=o[t];i!==void 0&&i!==""&&e.set(n,typeof i=="number"?`${i}px`:i)}return e}var xa=/^(?:[ \t]*>[ \t]?)*/,Ut=/[ \t]*(?:[-*+]|\d{1,9}[.)])(?=[ \t\n\r]|$)/y,Hh=/^(`{3,}|~{3,})/,Ph=/^\[[ \t\nxX]\](?=[ \t\n]|$)/;function wa(o,e){let t=he(o,e),n=pt(o),i=[],r=[],s=(g,x,b)=>{b>x&&i.push({from:x,to:b,kind:g})},l=g=>({from:n.starts[g-1]??o.length,to:n.ends[g-1]??o.length}),a=(g,x,b,w)=>{for(let k of bs(g,n,x,b))s(w,k.from,k.to)},d=(g,x)=>{for(let b=g;b<x;b++){let w=o[b];if(w===">")s("marker",b,b+1);else if(w==="-"||w==="*"||w==="+"||w>="0"&&w<="9"){Ut.lastIndex=b;let k=Ut.exec(o);k&&k.index===b&&(s("marker",b+k[0].length-k[0].trimStart().length,b+k[0].length),b+=k[0].length-1)}}},u=g=>{let x=xa.exec(o.slice(g.from,g.to))[0];d(g.from,g.from+x.length),Ut.lastIndex=g.from+x.length;let b=g.from+x.length;for(let w=Ut.exec(o);w&&w.index===b;w=Ut.exec(o))s("marker",b+w[0].length-w[0].trimStart().length,b+w[0].length),b+=w[0].length,Ut.lastIndex=b;return b},c=g=>{for(let x of g.lines){let b=n.starts[x.line-1]??o.length;d(b,b+x.offset)}},h=(g,x)=>{for(let b=g;b<x;b++)o[b]==="\\"&&b+1<x?(o[b+1]==="|"&&s("escape",b,b+2),b++):o[b]==="|"&&s("marker",b,b+1)},f=(g,x,b)=>{g&&(c(g),b&&x.length>0&&a(g,0,Math.max(...x.map(w=>w.pos?.to??0)),b),p(g,x))},p=(g,x)=>{let b=w=>{for(let k of w){let{pos:S}=k;if(S)switch(k.type){case"text":if(k.pieces){let v=S.from;for(let T of k.pieces)a(g,v,T.from,"escape"),v=T.to;a(g,v,S.to,"escape")}break;case"hardBreak":{let v=new xe(g,n).source(S.from);o[v]==="\\"&&s("marker",v,v+1);break}case"code":{for(let v of k.syntax??[])a(g,v.from,v.to,"marker");k.content&&a(g,k.content.from,k.content.to,"code");break}case"html":a(g,S.from,S.to,"html");break;case"token":a(g,S.from,S.to,"token");break;case"emphasis":case"strong":case"strike":{let v=k.syntax?.find(E=>E.kind==="open"),T=k.syntax?.find(E=>E.kind==="close");for(let E of k.syntax??[])a(g,E.from,E.to,"marker");a(g,v?.to??S.from,T?.from??S.to,k.type==="emphasis"?"em":k.type),b(k.children);break}case"link":case"image":{let v=k.syntax?.find(A=>A.kind==="open"),T=k.syntax?.find(A=>A.kind==="close"),E=k.syntax?.find(A=>A.kind==="dest");for(let A of k.syntax??[])A.kind==="dest"?a(g,A.from,A.to,"url"):A.kind==="tail"&&E&&E.from>=A.from&&E.to<=A.to?(a(g,A.from,E.from,"marker"),a(g,E.to,A.to,"marker")):A.kind!=="title"&&a(g,A.from,A.to,"marker");v&&T&&!(E&&E.from===v.to&&E.to===T.from)&&a(g,v.to,T.from,"link"),b(k.children);break}default:break}}};b(x)},m=g=>{switch(g.type){case"paragraph":case"heading":case"htmlBlock":case"raw":return g.source?.lines[0]?.line??0;case"codeBlock":case"thematicBreak":case"table":return g.lines?.start??0;case"blockQuote":case"listItem":return g.children[0]?m(g.children[0]):0;case"list":return g.items[0]?m(g.items[0]):0}},y=g=>{switch(g.type){case"paragraph":f(g.source,g.children);break;case"heading":{f(g.source,g.children,"heading");let x=g.source?.lines[0];if(!x)break;if(g.setext){let b=l(x.line+(g.source?.lines.length??1)),w=xa.exec(o.slice(b.from,b.to))[0];d(b.from,b.from+w.length),s("marker",b.from+w.length,b.to)}else{let b=l(x.line),w=o.slice(b.from,b.from+x.offset),k=/#{1,6}[ \t]*$/.exec(w);k&&s("marker",b.from+k.index,b.from+k.index+k[0].trimEnd().length);let S=o.slice(b.from+x.offset,b.to),v=/(?:^|[ \t]+)(#+)[ \t]*$/.exec(S);v&&s("marker",b.from+x.offset+v.index+v[0].indexOf("#"),b.from+x.offset+v.index+v[0].trimEnd().length)}break}case"thematicBreak":if(g.lines){let x=l(g.lines.start);s("marker",u(x),x.to)}break;case"blockQuote":g.children.forEach(y);break;case"list":for(let x of g.items){if(x.lines&&m(x)!==x.lines.start&&u(l(x.lines.start)),x.task){let b=x.children[0],k=(b?.type==="paragraph"?b.source:void 0)?.lines[0];if(k){let S=(n.starts[k.line-1]??0)+k.offset;Ph.test(o.slice(S,n.ends[k.line-1]))&&s("marker",S,S+3)}}x.children.forEach(y)}break;case"codeBlock":{if(!g.source||!g.lines)break;let x=g.source.lines;if(g.fenced){let b=l(g.lines.start),w=u(b),k=o.slice(w,b.to),S=k.trimStart(),v=w+k.length-S.length,T=Hh.exec(S);T&&(s("marker",v,v+T[1].length),s("lang",v+T[1].length,b.to));let E=x[x.length-1]?.line??g.lines.start;if(g.lines.end>E){let A=l(g.lines.end);s("marker",u(A),A.to)}}if(c(g.source),x.length>0){let b=g.value.replace(/\n$/,"").length;a(g.source,0,b,"code");let w=new xe(g.source,n);r.push({from:w.source(0),to:w.source(b),language:Bi(g.info)})}break}case"htmlBlock":case"raw":g.source&&(c(g.source),a(g.source,0,g.value.length,g.type==="raw"?"token":"html"));break;case"table":if(g.lines){for(let x=g.lines.start;x<=g.lines.end;x++){let b=l(x),w=u(b);x===g.lines.start+1?s("marker",w,b.to):h(w,b.to)}g.sources?.forEach((x,b)=>x.forEach((w,k)=>p(w,g.rows[b]?.[k]??[])))}break}};return t.children.forEach(y),i.sort((g,x)=>g.from-x.from||x.to-g.to),{ranges:i,code:r}}var Sa={marker:"mbe-src-marker",heading:"mbe-src-heading",em:"mbe-src-em",strong:"mbe-src-strong",strike:"mbe-src-strike",code:"mbe-src-code",link:"mbe-src-link",url:"mbe-src-url",html:"mbe-src-html",token:"mbe-src-token",escape:"mbe-src-escape",lang:"mbe-src-lang"},$h=new Set(["marker","code","url","html","token","escape","lang"]),Ta=2e4,Ea=1e3,Fh=80;function qh(o,e,t,n){let i=n.highlight?_h(t,n.classes):[];if(n.decorators.length>0){let r=Pn(o,La(e,t));for(let s of n.decorators)i.push(...s(r))}if(n.codeDecorators.length>0)for(let r of t.code){let s={block:o,text:e.slice(r.from,r.to),language:r.language};for(let l of n.codeDecorators.flatMap(a=>a(s))){let a=r.from+Math.max(0,l.from),d=r.from+Math.min(r.to-r.from,l.to);d>a&&i.push({...l,from:a,to:d})}}return i}var Vo=class{constructor(e,t,n){this.options=e;this.config=t;this.onReady=n}last=null;markupFor=null;spansFor=null;pending=null;markup(e){return this.markupFor?.text!==e&&(this.markupFor={text:e,markup:wa(e,this.options)}),this.markupFor.markup}spans(e){let t=this.markupFor?.text===e?this.markupFor.markup:null;if(this.spansFor?.text===e&&(!t||this.spansFor.markup===t))return this.spansFor.spans;let n=t;if(!n&&this.markupFor&&e.length>Ta){let r=Ma(this.markupFor.markup.ranges,this.markupFor.text,e);r&&(n={ranges:r,code:this.markupFor.markup.code})}n??=this.markup(e);let i=La(e,n);return this.spansFor={text:e,markup:n,spans:i},i}decorations(e){let t=I(e.content),n=this.last;if(n&&n.text===t)return n.decorations;if(n&&t.length>Ta){let i=Ma(n.decorations,n.text,t);if(i)return this.last={text:t,decorations:i},this.schedule(e),i}return this.read(e,t)}reset(){this.last=null}destroy(){this.pending!==null&&clearTimeout(this.pending),this.pending=null}read(e,t){this.destroy();let n=qh(e,t,this.markup(t),this.config());return this.last={text:t,decorations:n},n}schedule(e){this.destroy(),this.pending=setTimeout(()=>{this.pending=null;let t=this.last;if(!t)return;let n=this.read(e,t.text);$n(t.decorations,n)||this.onReady()},Fh)}};function Ma(o,e,t){let{from:n,toBefore:i,toAfter:r}=dr(e,t);if(i-n>Ea||r-n>Ea)return null;let s=t.length-e.length,l=[];for(let a of o){let d=a.from<n?a.from:a.from>=i?a.from+s:n,u=a.to<=n?a.to:a.to>=i?a.to+s:n;u>d&&l.push(d===a.from&&u===a.to?a:{...a,from:d,to:u})}return l}function _h(o,e){return o.ranges.map(t=>{let n=e?.[t.kind];return{from:t.from,to:t.to,class:n?`${Sa[t.kind]} ${n}`:Sa[t.kind]}})}var Ca=["code"],Ia=[];function La(o,e){let t=[],n=(s,l,a)=>{l>s&&t.push({text:o.slice(s,l),marks:a})},i=0,r=0;for(let s of e.ranges)$h.has(s.kind)&&(s.from>r&&(n(i,r,Ca),n(r,s.from,Ia),i=s.from),r=Math.max(r,s.to));return n(i,r,Ca),n(r,o.length,Ia),t}var Pa=["text","heading1","heading2","heading3","heading4","bulletList","numberedList","todoList","quote","callout","details","code","table","divider"],Kh=[{id:"text",aliases:["paragraph","plain"],spec:{type:"paragraph"}},{id:"heading1",aliases:["h1","title"],spec:{type:"heading",level:1}},{id:"heading2",aliases:["h2","subtitle"],spec:{type:"heading",level:2}},{id:"heading3",aliases:["h3"],spec:{type:"heading",level:3}},{id:"heading4",aliases:["h4"],spec:{type:"heading",level:4}},{id:"bulletList",aliases:["ul","unordered","bullet"],spec:{type:"listItem",listType:"bullet"}},{id:"numberedList",aliases:["ol","ordered"],spec:{type:"listItem",listType:"ordered"}},{id:"todoList",aliases:["task","checkbox","checklist"],spec:{type:"listItem",listType:"task"}},{id:"quote",aliases:["blockquote","citation"],spec:{type:"quote"}},{id:"callout",aliases:["alert","admonition","note","tip","important","warning","caution"],spec:{type:"quote",callout:"note"}},{id:"details",aliases:["toggle","collapse","collapsible","summary","spoiler","fold"],spec:{type:"details"}},{id:"code",aliases:["pre","snippet","fence"],spec:{type:"code"}},{id:"table",aliases:["grid"],spec:{type:"table"}},{id:"divider",aliases:["hr","separator","line","rule"],spec:{type:"divider"}}],Vh=[{id:"addRowAbove",action:{kind:"addRowAbove"}},{id:"addRowBelow",action:{kind:"addRowBelow"}},{id:"addColumnLeft",action:{kind:"addColumnLeft"}},{id:"addColumnRight",action:{kind:"addColumnRight"}},{id:"deleteRow",action:{kind:"deleteRow"}},{id:"deleteColumn",action:{kind:"deleteColumn"}}],Uh=[{id:"alignLeft",align:"left",image:"imageAlignLeft"},{id:"alignCenter",align:"center",image:"imageAlignCenter"},{id:"alignRight",align:"right",image:"imageAlignRight"}],zh=["/","\uFF0F"],Ba=".mbe-widget, .mbe-block-after",Aa=/^(Arrow(Up|Down|Left|Right)|Home|End|PageUp|PageDown)$/,Na=new Set(["insertCompositionText","deleteCompositionText","insertFromComposition","deleteByComposition"]),Fr=class{root;labels;commands;suggestion;current;source;markdown;history=new oo(()=>this.source);modeValue="rich";pendingMode=null;renderer;composing=!1;composition=null;compositionEndedAt=0;shortcut=null;unsettled=new Set;settleFrame=null;readOnly;wasReadOnly=!1;taskToggle;smartLists;sourceHighlighting;sourceHighlighter;options;extensions;bindings;runtime;classesKey="{}";tokenNames=[];listeners=new Map;cleanup=[];widgets=[];changeFrame=null;plainPasteNext=!1;destroyed=!1;focused=!1;pointerDown=!1;toolbarClosedFor=null;typing=0;caretKey=null;suggestions;blockMenu;toolbar;imageToolbar;inputPopover;blockHandle;collapsed=new Set;linkCard;resizeHandles;selectedImageEl=null;hintEl=null;blockSel=null;selectedEls=[];headingIdsFor=null;redecoratePending=!1;viewFrame=0;constructor(e){let t=e.element;this.root=t,this.options=e,this.runtime={},this.labels={...Mi,...e.labels},this.readOnly=e.editable===!1,this.taskToggle=e.taskToggle===!0,this.smartLists=e.smartLists!==!1,this.sourceHighlighting=e.sourceHighlighting!==!1;let n=e.extensions??[];this.extensions=n.some(c=>c.name==="defaultKeymap")?n:[...n,rr()],this.bindings=this.extensions.flatMap(c=>Object.entries(c.keymap??{}).map(([h,f])=>Wl(h,f,ie)));let i=n.flatMap(c=>c.markdown?[c.markdown]:[]);this.markdown=Ze({...e.markdown,syntax:[...e.markdown?.syntax??[],...i]});let r=Le(e.content??"",this.markdown);this.source=r.source,this.current=Hn(r.doc),this.renderer=new Ho(t,this.labels),this.renderer.collapsed=this.collapsed,this.collapseClosedSections(this.current.doc),this.renderer.widgetsAfter=c=>{if(this.widgets.length===0)return;let h=this.widgets.filter(f=>f.blockId===c).map(f=>f.element);return h.length?h:void 0};let s=[["markdown",Nr]];e.sanitizeHtml&&s.push(["html",ya(e.sanitizeHtml)]),this.renderer.views=new Map([...s,...this.extensions.flatMap(c=>Object.entries(c.views??{}).filter(([h])=>h!=="html"))]),this.renderer.viewContext={editor:this},this.renderer.blockViews=this.extensions.flatMap(c=>c.blockView?[{name:c.name,view:c.blockView}]:[]),this.renderer.markdown=this.markdown,this.renderer.onViewSettled=c=>this.viewsSettled(c);let l=this.extensions.flatMap(c=>c.codeDecorations?[c.codeDecorations]:[]);l.length>0&&(this.renderer.decorateCode=c=>{let h={block:c,text:I(c.content),language:c.language};return l.flatMap(f=>f(h))});let a=this.extensions.flatMap(c=>c.decorations?[c.decorations]:[]);a.length>0&&(this.renderer.decorate=(c,h,f)=>{let p=Pn(c,h,f);return a.flatMap(m=>m(p))}),this.sourceHighlighter=new Vo(this.markdown,()=>({highlight:this.sourceHighlighting,classes:this.renderer.classes.source,decorators:a,codeDecorators:l}),()=>this.refreshSource()),this.renderer.decorateSource=c=>this.sourceHighlighter.decorations(c),this.commands=this.createCommands(),t.classList.add("mbe-root"),t.setAttribute("role","textbox"),t.setAttribute("aria-multiline","true"),t.spellcheck=!0,t.translate=!1,t.style.setProperty("--mbe-slash-hint",JSON.stringify(this.labels.slashHint)),this.applyRuntime({placeholder:e.placeholder,attributes:e.attributes}),this.runtime={...this.runtime,resolveUrl:e.resolveUrl,onLinkClick:e.onLinkClick},this.renderer.resolveUrl=e.resolveUrl??Ln,this.renderer.referrerPolicy=e.referrerPolicy,this.renderer.codeCopy=e.codeCopy!==!1,this.renderer.icon=c=>X(t.ownerDocument,this.icon(c)),this.applyTheme(e.theme),ir(t,e.popupContainer);let d=this.extensions.flatMap(c=>c.suggestion?[c.suggestion]:[]);e.slashMenu!==!1&&d.push(this.slashSuggestion()),this.suggestions=new Co({editor:this,root:t,syncActiveDescendant:()=>this.syncActiveDescendant(),refresh:()=>this.updateUi(),endComposition:()=>this.endComposition(),changed:c=>this.emit("suggestionChange",{editor:this,state:c})},d);let u=this.suggestions;this.suggestion={get state(){return u.state},open:c=>!this.destroyed&&(this.refreshSelection(),u.open(c)),close:()=>u.dismiss(),move:c=>u.move(c),accept:c=>!this.destroyed&&u.accept(c)},this.blockMenu=new Xe(t,"mbe-block-menu","menu"),this.blockMenu.onPicked=()=>this.blockMenu.hide(),this.blockMenu.onHighlight=()=>this.syncActiveDescendant(),this.blockMenu.checkIcon=this.icon("selected"),this.blockMenu.submenuIcon=this.icon("chevronRight"),this.inputPopover=new Lo(this),this.toolbar=e.toolbar===!1?null:new Io(this,typeof e.toolbar=="object"?e.toolbar.items:void 0),this.toolbar&&(this.toolbar.onMenuChange=()=>this.syncActiveDescendant()),this.imageToolbar=e.toolbar===!1?null:new Ao(this,{align:(c,h)=>this.updateImageAt(c.pos,{align:h==="left"?null:h}),altText:c=>this.editAltText(c.pos,c.image.alt),resetSize:c=>this.updateImageAt(c.pos,{width:null}),remove:c=>this.deleteImageAt(c.pos)}),this.blockHandle=new qo(this),this.linkCard=e.linkCard===!1?null:new _o(this,{open:(c,h)=>this.openHref(c.el.getAttribute("href"),h),edit:c=>this.editLink(c),remove:c=>this.removeLink(c),targetOf:c=>this.linkTargetOf(c)}),this.resizeHandles=new Bo(this),this.renderer.onImageError=()=>{this.destroyed||(this.composing||this.syncDomSelection(),this.updateUi())},this.listen(t,"beforeinput",this.typed(c=>this.onBeforeInput(c))),this.listen(t,"input",this.typed(c=>this.onInput(c))),this.listen(t,"compositionstart",c=>this.onCompositionStart(c)),this.listen(t,"compositionend",this.typed(c=>this.onCompositionEnd(c))),this.listen(t,"keydown",this.typed(c=>this.onKeyDown(c))),this.listen(t,"copy",c=>this.onCopy(c,!1)),this.listen(t,"cut",this.typed(c=>this.onCopy(c,!0))),this.listen(t,"paste",this.typed(c=>this.onPaste(c))),this.listen(t,"dragstart",c=>c.preventDefault()),this.listen(t,"dragover",c=>{this.readOnly||c.preventDefault()}),this.listen(t,"drop",this.typed(c=>this.onDrop(c))),this.listen(t,"mousedown",c=>this.onMouseDown(c)),this.listen(t,"click",c=>this.onClick(c)),this.listen(t,"change",c=>this.onControlChange(c)),this.listen(t,"focusin",c=>this.onFocusIn(c)),this.listen(t,"focusout",c=>this.onFocusOut(c)),this.listen(t.ownerDocument,"selectionchange",()=>this.onSelectionChange()),this.listen(t.ownerDocument,"mouseup",()=>this.onMouseUp()),this.listen(t.ownerDocument,"scroll",()=>this.onScroll(),!0),this.listen(t.ownerDocument,"pointerdown",c=>this.onDocumentPointerDown(c),!0),e.mode==="source"&&this.enterSource(),this.paint(!0,!0),this.afterRender();for(let c of this.extensions){let h=c.onCreate?.({editor:this});typeof h=="function"&&this.cleanup.push(h)}e.autofocus&&this.commands.focus(e.autofocus==="end"?"end":"start")}getMarkdown(){return this.modeValue==="source"?K(this.current.doc):Pt(this.current.doc,{...this.markdown,source:this.source})}setMarkdown(e,t={}){if(this.destroyed)return;if(t.history==="keep"?this.history.record(this.current,"other"):this.history.clear(),this.modeValue==="source"){this.suggestions.reset(),this.current=Fe(e,0);for(let r of this.widgets)r.offset=Math.min(r.offset,e.length);this.paint(!0,!0),this.afterRender(),this.emit("selectionChange",{editor:this});return}let n=Le(e,this.markdown);this.source=n.source,this.blockSel=null,this.suggestions.reset();let i=Hn(n.doc);this.collapsed.clear(),this.collapseClosedSections(i.doc),this.relocateWidgets(this.current.doc,i.doc,!0),this.current=i,this.paint(!0,!0),this.afterRender(),this.emit("selectionChange",{editor:this})}get isEmpty(){return this.modeValue==="source"?K(this.current.doc)==="":this.current.doc.blocks.every(e=>e.type==="paragraph"&&B(e.content)===0)}get state(){return this.current}get isComposing(){return this.composing}get mode(){return this.pendingMode??this.modeValue}get isEditable(){return!this.readOnly&&!this.destroyed}get isDestroyed(){return this.destroyed}isActive(e,t){if(this.modeValue==="source")return!1;let n=this.current;switch(e){case"bold":case"italic":case"strike":case"code":return Vn(n,e);case"link":return Qe(n)!==null;case"image":{let s=De(n);if(!s||!t)return s!==null;let l={...s.image,block:ke(n)!==null};return Object.entries(t).every(([a,d])=>l[a]===d)}}let i=N(n.doc,n.selection.focus.blockId);if(Da(i)!==e&&!(e==="blockquote"&&M(i)))return!1;if(!t)return!0;let r=this.getAttributes(e);return Object.entries(t).every(([s,l])=>r[s]===l)}getAttributes(e){if(this.modeValue==="source")return{};let t=this.current;if(e==="link"){let r=Qe(t);return r?{href:r.href}:{}}if(e==="image"){let r=De(t);return r?{...r.image,block:ke(t)!==null}:{}}let n=N(t.doc,t.selection.focus.blockId),i=M(n);if(e==="blockquote"&&i)return i==="quote"?{}:{callout:i};if(Da(n)!==e)return{};switch(n.type){case"heading":return{level:n.level};case"code":return{language:n.language};case"listItem":return n.listType==="task"?{checked:n.checked,indent:n.indent}:{indent:n.indent};case"raw":return{syntax:n.syntax};case"quote":return n.callout?{callout:n.callout}:{};case"details":return{open:n.open,collapsed:this.collapsed.has(n.id)};default:return{}}}getSelection(){return this.current.selection}setSelection(e){if(this.destroyed)return!1;let t=jh(e)?te(this.current.doc,e):Gh(this.current.doc,e);return t?(this.dropBlockSelection(),this.current={...this.current,selection:t,storedMarks:null},this.history.breakGroup(),this.refreshRevealed(),this.syncDomSelection(),this.updateUi(),this.emit("selectionChange",{editor:this}),!0):!1}selectionToJSON(){let e=this.current.doc,t=n=>{let i=L(e,n.blockId);return n.cell?{block:i,offset:n.offset,cell:{row:n.cell.row,col:n.cell.col}}:{block:i,offset:n.offset}};return{anchor:t(this.current.selection.anchor),focus:t(this.current.selection.focus)}}hasFocus(){let e=this.root.ownerDocument.activeElement;return!!e&&this.root.contains(e)&&!(e instanceof HTMLInputElement)&&!e.closest(Ba)}addWidget(e){if(this.destroyed){let l=new kt(this.root.ownerDocument.createElement("div"),"",()=>{},()=>{});return l.remove(),l}let t=this.current.doc.blocks,n=e.at==="end"?P(t[t.length-1]):e.at,i=te(this.current.doc,{anchor:n,focus:n}).focus.blockId,r=this.root.ownerDocument.createElement("div");r.className="mbe-widget",r.contentEditable="false";let s=new kt(r,i,e.render,l=>{this.widgets=this.widgets.filter(a=>a!==l)});return this.modeValue==="source"&&(s.offset=e.at==="end"?K(this.current.doc).length:n.offset),this.widgets.push(s),this.paint(),s}relocateWidgets(e,t,n){if(this.widgets.length===0)return;let i=new Set(t.blocks.map(r=>r.id));for(let r of this.widgets){if(i.has(r.blockId))continue;let s=e.blocks.findIndex(a=>a.id===r.blockId),l;if(n)l=t.blocks[Math.max(0,Math.min(s,t.blocks.length-1))].id;else{for(let a=s-1;a>=0&&!l;a--)i.has(e.blocks[a].id)&&(l=e.blocks[a].id);for(let a=s+1;a<e.blocks.length&&!l&&s>=0;a++)i.has(e.blocks[a].id)&&(l=e.blocks[a].id)}r.blockId=l??t.blocks[0].id}}focus(){if(this.destroyed)return;let e=this.current.selection.focus,t=this.renderer.elementFor(e.blockId);((e.cell&&t?se(t,e.cell):this.root)??this.root).focus({preventScroll:!0}),this.syncDomSelection(!0)}blur(){this.hasFocus()&&this.root.ownerDocument.activeElement?.blur()}setOptions(e){if(this.destroyed)return;e.extensions&&this.suggestions.setConfigs(e.extensions.flatMap(n=>n.suggestion?[n.suggestion]:[]));let t=e.taskToggle!==void 0&&e.taskToggle!==this.taskToggle;t&&(this.taskToggle=e.taskToggle===!0),e.editable!==void 0&&e.editable===this.readOnly?(this.readOnly=!e.editable,this.renderer.views?.has("footnote")?this.paint():this.refreshRevealed(),this.applyReadOnly(),this.hideUi()):t&&this.applyReadOnly(),this.applyRuntime(e),e.mode!==void 0&&this.setMode(e.mode)}setMode(e){if(e!=="rich"&&e!=="source")return;if(this.composing||this.composition){this.pendingMode=e===this.modeValue?null:e;return}if(this.pendingMode=null,e===this.modeValue)return;let t=this.hasFocus(),n=this.caretRect()?.top;this.blockSel=null,this.shortcut=null,this.suggestions.reset(),this.hideUi(),this.closeBlockMenu(),this.history.breakGroup(),e==="source"?this.enterSource():this.leaveSource(),this.paint(!0,!1,t),this.afterRender();let i=this.caretRect()?.top;n!==void 0&&i!==void 0&&Math.abs(i-n)>1&&Xh(this.root,i-n),this.emit("selectionChange",{editor:this})}enterSource(){let{doc:e,selection:t}=this.current,n=this.getMarkdown(),i=Le(n,this.markdown),r=hr(e,t,i);for(let s of this.widgets)s.offset=Ol(e,s.blockId,i),s.blockId=xn;this.collapsed.clear(),this.current=Fe(n,r.anchor,r.focus),this.modeValue="source",this.renderer.source=!0,this.root.classList.add("mbe-source-mode")}leaveSource(){let{anchor:e,focus:t}=this.current.selection,n=this.loadSource(K(this.current.doc));for(let r of this.widgets)r.blockId=wo(r.offset,n).blockId;let i=fr(e.offset,t.offset,n);this.current={doc:n.doc,selection:te(n.doc,i),storedMarks:null},this.modeValue="rich",this.renderer.source=!1,this.root.classList.remove("mbe-source-mode")}loadSource(e){let t=Le(e,this.markdown),n=Hn(t.doc).doc;return this.source=t.source,this.collapsed.clear(),this.collapseClosedSections(n),{doc:n,source:t.source}}restore(e){let{state:t}=e,n=So(t.doc);if(this.modeValue==="source"){if(n)return t;let s=Pt(t.doc,{...this.markdown,source:e.source}),l=hr(t.doc,t.selection,Le(s,this.markdown));return Fe(s,l.anchor,l.focus)}if(!n)return this.source=e.source,t;let i=this.loadSource(K(t.doc)),r=fr(t.selection.anchor.offset,t.selection.focus.offset,i);return{doc:i.doc,selection:te(i.doc,r),storedMarks:null}}on(e,t){let n=this.listeners.get(e);return n||this.listeners.set(e,n=new Set),n.add(t),()=>void n.delete(t)}emit(e,t){let n=this.listeners.get(e);if(n)for(let i of[...n])i(t)}applyRuntime(e){let t=this.root;if("placeholder"in e&&(e.placeholder?(t.style.setProperty("--mbe-placeholder",JSON.stringify(e.placeholder)),t.setAttribute("aria-placeholder",e.placeholder)):(t.style.removeProperty("--mbe-placeholder"),t.removeAttribute("aria-placeholder")),this.runtime={...this.runtime,placeholder:e.placeholder}),"attributes"in e){for(let n of Object.keys(this.runtime.attributes??{}))t.removeAttribute(n);for(let[n,i]of Object.entries(e.attributes??{}))t.setAttribute(n,i);this.runtime={...this.runtime,attributes:e.attributes}}"resolveUrl"in e&&e.resolveUrl!==this.runtime.resolveUrl&&(this.runtime={...this.runtime,resolveUrl:e.resolveUrl},this.renderer.resolveUrl=e.resolveUrl??Ln,this.paint(!0)),"onLinkClick"in e&&(this.runtime={...this.runtime,onLinkClick:e.onLinkClick}),"theme"in e&&this.applyTheme(e.theme)&&this.paint(!0)}applyTheme(e){let t=$r(this.runtime.theme?.tokens);this.runtime={...this.runtime,theme:e};let n=this.root.style,i=$r(e?.tokens);for(let a of this.tokenNames)i.has(a)||n.removeProperty(a);for(let[a,d]of i)n.setProperty(a,d);this.tokenNames=[...i.keys()];let r=e?.colorScheme??"auto";n.colorScheme=r==="auto"?"":r,va.some(a=>t.get(a)!==i.get(a))&&this.renderBlocksAgain(this.renderer.dropViews());let s=ka([...this.extensions.map(a=>a.theme?.classes).reverse(),e?.classes]),l=JSON.stringify(s);return l===this.classesKey?!1:(this.classesKey=l,this.renderer.classes=s,!0)}run(e,t="other"){if(this.readOnly||this.destroyed)return!1;let n=e(this.current);return!n||n===this.current||(n=bi(n,this.current,this.markdown.inline),n=yi(n,this.current),n=li(n),n.doc!==this.current.doc&&Qh(n.doc,this.current.doc)&&(n={...n,doc:this.current.doc}),n.doc===this.current.doc&&n.selection===this.current.selection&&n.storedMarks===this.current.storedMarks)?!1:(this.apply(n,t),!0)}createCommands(){let e=l=>this.run(l),t=l=>e(a=>ht(a,l)),n={toggleBold:()=>e(l=>We(l,"bold")),toggleItalic:()=>e(l=>We(l,"italic")),toggleStrike:()=>e(l=>We(l,"strike")),toggleCode:()=>e(l=>We(l,"code")),setParagraph:()=>e(l=>Ge(l,{type:"paragraph"})),toggleHeading:({level:l})=>t({type:"heading",level:l}),toggleBulletList:()=>t({type:"listItem",listType:"bullet"}),toggleOrderedList:()=>t({type:"listItem",listType:"ordered"}),toggleTaskList:()=>t({type:"listItem",listType:"task"}),toggleBlockquote:()=>t({type:"quote"}),toggleCallout:(l={})=>this.markdown.callouts&&t({type:"quote",callout:l.type??"note"}),toggleDetails:()=>this.markdown.details&&t({type:"details"}),setCallout:({type:l})=>(this.markdown.callouts||l===null)&&e(a=>sn(a,a.selection.focus.blockId,l)),toggleCodeBlock:(l={})=>t({type:"code",language:l.language}),setLink:({href:l})=>e(a=>ut(a,l)),unsetLink:()=>e(l=>ut(l,null)),updateImage:l=>e(a=>{let d=De(a);return d?Un(a,d.pos,l):null}),insertText:(l,a={})=>e(d=>{let u=a.range;if(!u)return ue(d,l);let c=te(d.doc,{anchor:u.from,focus:u.to});return ue({...d,selection:c,storedMarks:null},l)}),insertToken:l=>!this.composing&&e(a=>ni(a,l)),insertMarkdown:(l,a={})=>{let d=bt(l,this.markdown).blocks,u=a.at;if(u instanceof kt){if(u.isRemoved)return!1;let c=N(this.current.doc,u.blockId);return e(h=>cn(h,d,{...a,at:P(c),block:!0}))}return e(c=>cn(c,d,a))},insertTable:(l={})=>e(a=>Rt(a,{type:"table",rows:l.rows,cols:l.cols})),insertDivider:()=>e(l=>Rt(l,{type:"divider"})),indent:()=>e(l=>Wn(l,1)),outdent:()=>e(l=>Wn(l,-1)),undo:()=>this.undo(),redo:()=>this.redo(),selectAll:()=>(this.setSelection(xi(this.current).selection),!0),selectBlocks:()=>{if(this.readOnly)return!1;this.hasFocus()||this.root.focus({preventScroll:!0});let{anchor:l,focus:a}=this.current.selection;return this.selectBlockRange(l.blockId,a.blockId),!0},focus:l=>{if(l!==void 0){let a=this.current.doc.blocks,d=l==="start"?V(a[0]):l==="end"?P(a[a.length-1]):l;this.current={...this.current,selection:te(this.current.doc,{anchor:d,focus:d})}}return this.focus(),this.emit("selectionChange",{editor:this}),!0},blur:()=>{let l=this.hasFocus();return this.blur(),l},scrollIntoView:()=>{let l=this.caretRect();return l?(Pr(this.root,l),!0):!1}},i={...n},r=(l,a)=>{let d=i[l];n[l]=(...u)=>this.modeValue==="source"?a?a(...u):!1:d(...u)},s=l=>e(a=>(this.smartLists?Rl(a,l):null)??ln(a,l));r("indent",()=>s(1)),r("outdent",()=>s(-1)),r("insertMarkdown",(l,a={})=>this.insertSourceMarkdown(l,a)),r("toggleBold",()=>e(l=>Tn(l,"**"))),r("toggleItalic",()=>e(l=>Tn(l,"*"))),r("toggleStrike",()=>e(l=>Tn(l,"~~"))),r("toggleCode",()=>e(l=>Tn(l,"`"))),r("setParagraph",()=>e(l=>pr(l,0))),r("toggleHeading",({level:l})=>e(a=>pr(a,l))),r("toggleBulletList",()=>e(l=>Eo(l,"bullet"))),r("toggleOrderedList",()=>e(l=>Eo(l,"ordered"))),r("toggleTaskList",()=>e(l=>Eo(l,"task"))),r("toggleBlockquote",()=>e(l=>Fl(l))),r("toggleCodeBlock",()=>e(l=>ql(l)));for(let l of["toggleCallout","setCallout","toggleDetails","setLink","unsetLink","updateImage","insertTable","insertDivider","selectBlocks"])r(l,null);for(let l of Object.keys(n)){let a=n[l];n[l]=(...d)=>!this.destroyed&&a(...d)}return n}insertSourceMarkdown(e,t){let n=t.at??"selection",i=t.select==="keep",r=t.block===!0;return n instanceof kt?n.isRemoved?!1:this.run(s=>To(s,n.offset,e,!0,i)):n==="selection"&&!r&&!i?this.run(s=>Ae(s,e)):this.run(s=>{let l=s,a;if(n==="selection"){let{from:d,to:u}=ne(s);d!==u&&(l=Ae(s,"")),a=d}else a=n==="end"?K(s.doc).length:n.offset;return To(l,a,e,r,i)})}undo(){if(this.readOnly)return!1;let e=this.history.undo(this.current);return e?(this.commit(this.restore(e)),!0):!1}redo(){if(this.readOnly)return!1;let e=this.history.redo(this.current);return e?(this.commit(this.restore(e)),!0):!1}selectionRect(){let e=this.root.ownerDocument.getSelection();if(!e||e.rangeCount===0)return null;let t=e.getRangeAt(0),n=t.getClientRects(),i=n.length?R(this.current.selection)?n[n.length-1]:t.getBoundingClientRect():null;return i&&(i.width>0||i.height>0)?i:this.emptyRect(this.current.selection.focus,e.focusNode)}caretRect(){let e=this.current.selection.focus,t=this.toDom(e);if(t){let n=this.root.ownerDocument.createRange();n.setStart(t.node,t.offset);let i=n.getClientRects(),r=i[i.length-1];if(r&&r.height>0)return r}return this.emptyRect(e,t?.node??null)}emptyRect(e,t){let n=this.renderer.elementFor(e.blockId),i=(n&&se(n,e.cell))??n,r=t?.nodeType===1?t:t?.parentElement??null;if(i&&r&&r!==i&&i.contains(r)){let s=r.getBoundingClientRect();if(s.height>0)return s}return i?.getBoundingClientRect()??null}textRect(e,t){let n=this.toDom(e),i=this.toDom(t);if(!n||!i)return null;let r=this.root.ownerDocument.createRange();r.setStart(n.node,n.offset),r.setEnd(i.node,i.offset);let s=r.getClientRects()[0];if(!s)return null;let l=n.node.nodeType===1?n.node:n.node.parentElement,a=!!l&&this.root.ownerDocument.defaultView.getComputedStyle(l).direction==="rtl";return{rect:s,rtl:a}}scrollCaretIntoView(){let e=this.caretRect();e&&Pr(this.root,e)}blockElement(e){return this.renderer.elementFor(e)}destroy(){if(!this.destroyed){this.destroyed=!0,this.changeFrame!==null&&cancelAnimationFrame(this.changeFrame),this.settleFrame!==null&&cancelAnimationFrame(this.settleFrame),this.sourceHighlighter.destroy();for(let e of this.cleanup)e();this.suggestions.destroy();for(let e of[...this.widgets])e.remove();this.blockMenu.destroy(),this.toolbar?.destroy(),this.imageToolbar?.destroy(),this.inputPopover.destroy(),this.blockHandle.destroy(),this.linkCard?.destroy(),this.resizeHandles.destroy(),this.renderer.destroy(),ir(this.root,void 0);for(let e of Object.keys(this.runtime.attributes??{}))this.root.removeAttribute(e);for(let e of this.tokenNames)this.root.style.removeProperty(e);this.root.style.colorScheme="",this.root.contentEditable="false",this.root.classList.remove("mbe-root"),this.root.replaceChildren(),this.listeners.clear()}}apply(e,t){e.doc!==this.state.doc&&this.history.record(this.state,t),this.commit(e)}commit(e){let t=e.doc!==this.state.doc,n=e.selection!==this.state.selection,i=this.hasFocus();if(t&&this.modeValue==="source"){let r=K(this.current.doc),s=K(e.doc);for(let l of this.widgets)l.offset=Dl(l.offset,r,s)}else t&&this.relocateWidgets(this.current.doc,e.doc,!1);if(this.current=e,this.blockSel){let r=this.blockRange();r?this.current={...e,selection:an(e.doc.blocks,r)}:this.blockSel=null}this.paint(!1,!1,i),this.afterRender(),n&&i&&this.typing>0&&this.scrollCaretIntoView(),t&&this.scheduleChange(),this.emit("selectionChange",{editor:this})}flushChange(){this.changeFrame!==null&&(cancelAnimationFrame(this.changeFrame),this.changeFrame=null,this.emitChange())}scheduleChange(){if(this.changeFrame!==null||!this.options.onChange&&!this.listeners.get("change")?.size)return;let e=()=>{this.changeFrame=null,this.destroyed||this.emitChange()};typeof requestAnimationFrame=="function"?this.changeFrame=requestAnimationFrame(e):e()}emitChange(){this.options.onChange&&this.options.onChange(this.getMarkdown(),this),this.emit("change",{editor:this})}submit(){return this.destroyed||!this.options.onSubmit&&!this.listeners.get("submit")?.size?!1:(this.flushChange(),this.options.onSubmit?.(this.getMarkdown(),this),this.emit("submit",{editor:this}),!0)}applyReadOnly(){let e=this.readOnly?"false":"true";if(this.root.contentEditable!==e&&(this.root.contentEditable=e),!this.readOnly&&!this.wasReadOnly)return;this.wasReadOnly=this.readOnly;let t=this.ticking;this.root.classList.toggle("mbe-readonly",this.readOnly),this.root.classList.toggle("mbe-tasks",t),this.root.setAttribute("aria-readonly",String(this.readOnly)),this.root.querySelectorAll("[data-row]").forEach(n=>{n.contentEditable=this.readOnly?"false":"true"}),this.root.querySelectorAll("input").forEach(n=>{let i=n.classList.contains("mbe-checkbox");n.disabled=this.readOnly&&!(i&&t),i&&(n.tabIndex=t?0:-1,t?n.setAttribute("aria-label",Zh(n,this.labels.task)):n.removeAttribute("aria-label"))})}get ticking(){return this.readOnly&&this.taskToggle}toggleTaskAt(e){let t=e.closest("[data-block-id]");if(!t)return;let n=t.dataset.blockId,i=e===this.root.ownerDocument.activeElement;if(!this.readOnly)this.run(r=>Qn(r,n));else if(this.ticking&&this.current.doc.blocks.some(r=>r.id===n))this.apply(Qn(this.current,n),"other");else return;i&&this.root.querySelector(`[data-block-id="${n}"] .mbe-checkbox`)?.focus({preventScroll:!0})}afterRender(){for(let e of this.state.doc.blocks){let t=this.renderer.elementFor(e.id);if(!t||!t.classList.contains("mbe-empty"))continue;let n="";e.type==="heading"?n=this.labels[`heading${Math.min(e.level,4)}`]:e.type==="listItem"?n=this.labels.listHint:e.type==="quote"&&!e.callout?n=this.labels.quoteHint:e.type==="details"&&(n=this.labels.detailsHint),n&&t.dataset.hint!==n&&(t.dataset.hint=n)}this.updateUi()}syncDomSelection(e=!1){let t=this.root.ownerDocument.getSelection();if(!t||!e&&!this.hasFocus())return;let n=this.parkedCaret(),i=n?{anchor:n,focus:n}:this.state.selection,r=this.toDom(i.anchor),s=R(i)?r:this.toDom(i.focus);if(!(!r||!s)&&!(t.anchorNode===r.node&&t.anchorOffset===r.offset&&t.focusNode===s.node&&t.focusOffset===s.offset)){if(i.focus.cell){let l=(s.node instanceof Element?s.node:s.node.parentElement)?.closest("[data-row]");l&&this.root.ownerDocument.activeElement!==l&&l.focus({preventScroll:!0})}else this.root.ownerDocument.activeElement!==this.root&&this.hasFocus()&&this.root.focus({preventScroll:!0});t.setBaseAndExtent(r.node,r.offset,s.node,s.offset)}}toDom(e){let t=this.renderer.elementFor(e.blockId);return t?ma(t,e):null}readDomSelection(){let e=this.root.ownerDocument.getSelection();if(!e||!e.anchorNode||!e.focusNode||!this.root.contains(e.anchorNode)||!this.root.contains(e.focusNode))return null;let t=_e(this.root,e.anchorNode,e.anchorOffset),i=e.anchorNode===e.focusNode&&e.anchorOffset===e.focusOffset?t:_e(this.root,e.focusNode,e.focusOffset);return!t||!i?null:{anchor:t,focus:i}}onSelectionChange(){if(this.composing||this.destroyed)return;let e=this.readDomSelection();if(!e){this.hasFocus()||this.updateUi();return}let t=this.caretKey;if(this.caretKey=null,this.blockSel){this.updateUi();return}let n=this.state.selection;if(be(e.anchor,n.anchor)&&be(e.focus,n.focus)){this.updateUi();return}this.shortcut=null;let i=t?.vertical?this.skippedBlock(t.from,t.vertical,t.x,e.focus):null,r=this.imageBlockAt(i?{anchor:i,focus:i}:e);r?!be(r.anchor,n.anchor)||!be(r.focus,n.focus)?this.setSelection(r):this.updateUi():i?this.setSelection({anchor:i,focus:i}):(this.current={...this.state,selection:e,storedMarks:null},this.history.breakGroup(),this.pointerDown||this.refreshRevealed(),this.updateUi(),this.emit("selectionChange",{editor:this})),t&&this.scrollCaretIntoView()}imageBlockAt(e){if(!R(e)||e.focus.cell)return null;let t=this.state.doc.blocks.find(i=>i.id===e.focus.blockId),n=t?Y(t):null;return n===null?null:{anchor:{blockId:t.id,offset:n},focus:{blockId:t.id,offset:n+1}}}skippedBlock(e,t,n,i){let r=this.state.doc.blocks,s=L(this.state.doc,i.blockId);for(let l=e+t;(s-l)*t>0;l+=t){let a=r[l];if(!this.skipsCaret(a))return this.blockEntry(a,t===1,n)}return null}blockEntry(e,t,n){let i=this.renderer.elementFor(e.id),r=this.viewBlock(e);if(r)return Bn(r,t);if(i){let s=i.ownerDocument.createRange();s.selectNodeContents(se(i)??i);let l=Array.from(s.getClientRects()).filter(u=>u.height>0),a=t?l[0]:l[l.length-1],d=a?this.positionAtPoint(n,a.top+a.height/2):null;if(d?.blockId===e.id)return d}if(e.type==="table"){let s=t?0:e.rows.length-1;return{blockId:e.id,offset:t?0:B(e.rows[s][0]),cell:{row:s,col:0}}}return t?V(e):P(e)}paint(e=!1,t=!1,n=this.hasFocus()){let i=this.renderer.views;i&&!this.pointerDown&&!this.composing&&(this.renderer.revealed=Hr(this.state,i,!this.readOnly&&n,this.hosted)),this.renderer.collectFootnotes=this.readOnly,this.renderer.render(this.state.doc,e,t),this.applyHeadingIds(),this.applyReadOnly(),this.paintBlockSelection(),this.composing||this.syncDomSelection(n)}sourceSpans(e){return this.sourceHighlighter.spans(I(e.content))}refreshSource(){if(this.destroyed||this.modeValue!=="source"||this.composing||this.composition)return;let e=this.current.doc.blocks[0];if(e?.type!=="code")return;let t=this.hasFocus();this.renderer.refreshSource(e),this.pointerDown||this.syncDomSelection(t)}applyHeadingIds(){let e=this.options.headingIds;if(!e)return;let t=this.state.doc;if(this.headingIdsFor?.doc!==t){let n=t.blocks.filter(r=>r.type==="heading"),i=e(n.map(r=>({text:I(r.content),level:r.level})));this.headingIdsFor={doc:t,ids:new Map(n.map((r,s)=>[r.id,i[s]]))}}for(let[n,i]of this.headingIdsFor.ids){let r=this.renderer.elementFor(n);r&&(i?r.id!==i&&(r.id=i):r.id&&r.removeAttribute("id"))}}redecorate(){if(!this.destroyed){if(this.composing||this.composition){this.redecoratePending=!0;return}this.redecoratePending=!1,this.sourceHighlighter.reset(),this.paint(!0),this.afterRender()}}refreshViews(e){this.destroyed||this.renderBlocksAgain(this.renderer.dropViews(e))}renderBlocksAgain(e){if(e.length!==0){if(this.composing||this.composition){for(let t of e)this.unsettled.add(t);return}for(let t of e)this.renderer.invalidate(t);this.paint(),this.afterRender()}}viewsSettled(e){if(!this.destroyed){if(this.composing||this.composition){for(let t of e)this.unsettled.add(t);return}for(let t of e)this.renderer.invalidate(t);this.viewFrame||=requestAnimationFrame(()=>{this.viewFrame=0,!(this.destroyed||this.composing||this.composition)&&(this.paint(),this.afterRender())})}}viewBlock(e){return e&&this.renderer.elementFor(e.id)?.classList.contains("mbe-view-block")?e:null}refreshRevealed(){let e=this.renderer.views;if(!e||this.pointerDown)return;let t=Hr(this.state,e,!this.readOnly&&this.hasFocus(),this.hosted);ba(t,this.renderer.revealed)||this.paint()}hosted=e=>this.renderer.isHosted(e);onMouseUp(){this.pointerDown&&(this.pointerDown=!1,!this.destroyed&&!this.composing&&this.refreshRevealed())}blockPast(e){let t=this.state.selection;if(!R(t)||t.focus.cell)return null;let n=t.focus,i=this.state.doc.blocks,r=L(this.state.doc,n.blockId),s=e==="ArrowDown"||e==="ArrowRight",l=i[r+(s?1:-1)],a=this.viewBlock(l);if(!l||!a&&l.type!=="table")return null;let d=i[r];if(!q(d)||e==="ArrowRight"&&n.offset!==P(d).offset||e==="ArrowLeft"&&n.offset!==0)return null;if(e==="ArrowDown"||e==="ArrowUp"){let u=se(this.renderer.elementFor(d.id));if(u&&!this.onEdgeLine(u,s))return null}return Bn(l,s)}imageBlockStep(e){let t=this.state.selection,n=this.state.doc.blocks,i=e==="ArrowDown"||e==="ArrowRight",r=ke(this.state);if(r){let u=L(this.state.doc,r.pos.blockId);for(let c=u+(i?1:-1);c>=0&&c<n.length;c+=i?1:-1){let h=n[c];if(this.skipsCaret(h))continue;let f=Y(h);if(f!==null)return{anchor:{blockId:h.id,offset:f},focus:{blockId:h.id,offset:f+1}};let m=e==="ArrowDown"||e==="ArrowUp"?this.blockEntry(h,i,this.selectionRect()?.left??0):i?V(h):P(h);return{anchor:m,focus:m}}return t}if(!R(t)||t.focus.cell)return null;let s=L(this.state.doc,t.focus.blockId),l=n[s],a=n[s+(i?1:-1)],d=a?Y(a):null;if(!a||d===null||!q(l)||e==="ArrowRight"&&t.focus.offset!==P(l).offset||e==="ArrowLeft"&&t.focus.offset!==0)return null;if(e==="ArrowDown"||e==="ArrowUp"){let u=se(this.renderer.elementFor(l.id));if(u&&!this.onEdgeLine(u,i))return null}return{anchor:{blockId:a.id,offset:d},focus:{blockId:a.id,offset:d+1}}}tableCellPast(e){let t=this.state.selection,n=t.focus;if(!R(t)||!n.cell)return null;let i=this.state.doc.blocks,r=L(this.state.doc,n.blockId),s=i[r];if(s?.type!=="table")return null;let l=se(this.renderer.elementFor(s.id),n.cell);if(l&&!this.onEdgeLine(l,e===1))return null;let{row:a,col:d}=n.cell,u=s.rows[a+e];if(u){let c=Math.min(n.offset,B(u[d]));return{blockId:s.id,offset:c,cell:{row:a+e,col:d}}}for(let c=r+e;c>=0&&c<i.length;c+=e){let h=i[c];if(this.skipsCaret(h))continue;if(h.type==="table"){let p=e===1?0:h.rows.length-1,m=Math.min(d,h.align.length-1);return{blockId:h.id,offset:e===1?0:B(h.rows[p][m]),cell:{row:p,col:m}}}let f=this.viewBlock(h);return f?Bn(f,e===1):e===1?V(h):P(h)}return null}onEdgeLine(e,t){let n=this.selectionRect();if(!n||n.height===0)return!0;let i=e.ownerDocument.createRange();i.selectNodeContents(e);let r=Array.from(i.getClientRects()).filter(l=>l.height>0);if(r.length===0)return!0;let s=n.height/2;return t?Math.max(...r.map(l=>l.bottom))-n.bottom<=s:n.top-Math.min(...r.map(l=>l.top))<=s}revealView(e){let t=e.hasAttribute(W)?N(this.state.doc,e.getAttribute(W)):null,n=t?Bn(t,!1):_e(this.root,e.parentNode,xt(e)+1);n&&(this.root.focus({preventScroll:!0}),this.setSelection({anchor:n,focus:n}))}refreshSelection(){if(this.blockSel)return;let e=this.readDomSelection();e&&(this.current={...this.state,selection:e})}targetRange(e){let t=e.getTargetRanges?.()[0];if(!t)return null;let n=_e(this.root,t.startContainer,t.startOffset),i=_e(this.root,t.endContainer,t.endOffset);return n&&i?{anchor:n,focus:i}:null}fromControl(e){return e.target instanceof HTMLInputElement||!!e.target?.closest?.(Ba)}onBeforeInput(e){if(this.fromControl(e)||this.composing||e.isComposing)return;let t=e.inputType;if(!Na.has(t)&&!(!e.cancelable&&this.browserApplies(e))&&(e.preventDefault(),!this.readOnly)){if(this.blockSel){t.startsWith("delete")?this.deleteSelectedBlocks():t==="historyUndo"?this.undo():t==="historyRedo"&&this.redo();return}if(this.refreshSelection(),!((t==="insertText"||t==="insertReplacementText")&&ke(this.state))&&!(this.modeValue==="source"&&this.onSourceInput(t)))switch(t){case"insertText":{let n=e.data??"",i=R(this.state.selection)?this.targetRange(e):null;i&&!R(i)&&(this.current={...this.state,selection:i}),n?this.typeText(n):i&&!R(i)&&this.run(J,"typing");return}case"insertReplacementText":{let n=e.dataTransfer?.getData("text/plain")??e.data??"",i=this.targetRange(e);this.run(r=>ue(i?{...r,selection:i}:r,n));return}case"insertParagraph":{if(this.suggestions.isOpen&&this.suggestions.accept())return;this.run(n=>this.collapsed.has(n.selection.focus.blockId)?ri(n):Kn(n));return}case"insertLineBreak":this.run(ii);return;case"deleteContentBackward":this.undoShortcut()||this.run(nn);return;case"deleteContentForward":this.run(_n);return;case"deleteWordBackward":case"deleteWordForward":case"deleteSoftLineBackward":case"deleteSoftLineForward":case"deleteHardLineBackward":case"deleteHardLineForward":case"deleteEntireSoftLine":{if(!R(this.state.selection)){this.run(J);return}let n=this.targetRange(e);n&&!R(n)&&n.anchor.blockId===n.focus.blockId?this.run(i=>J({...i,selection:n})):this.run(this.modeValue==="source"?t.endsWith("Backward")?i=>ar(i,en):i=>cr(i,tn):t.endsWith("Backward")?nn:_n);return}case"historyUndo":this.undo();return;case"historyRedo":this.redo();return;case"formatBold":this.commands.toggleBold();return;case"formatItalic":this.commands.toggleItalic();return;case"formatStrikeThrough":this.commands.toggleStrike();return;default:return}}}onSourceInput(e){switch(e){case"insertParagraph":return this.run(t=>this.smartLists?Nl(t):Ae(t,`
|
|
117
117
|
`)),!0;case"insertLineBreak":return this.run(t=>Ae(t,`
|
|
118
118
|
`)),!0;case"deleteContentBackward":return this.run(t=>ar(t,en)),!0;case"deleteContentForward":return this.run(t=>cr(t,tn)),!0;default:return!1}}typeText(e){this.run(t=>ue(t,e),"typing"),this.afterTyping(e)}browserApplies(e){let t=e.inputType;if(t==="insertText"||t==="insertReplacementText")return!0;if(!t.startsWith("delete"))return!1;let n=this.targetRange(e);return!!n&&!R(n)&&n.anchor.blockId===n.focus.blockId&&n.anchor.cell?.row===n.focus.cell?.row&&n.anchor.cell?.col===n.focus.cell?.col}afterTyping(e){if(this.modeValue==="source")this.shortcut=null;else{let t=this.state;(e===" "||e==="-")&&this.run(n=>fi(n,this.markdown)),e.length===1&&"*_`~".includes(e)&&this.run(n=>mi(n)),this.markdown.inline.length>0&&this.run(n=>gi(n,this.markdown.inline)??n),e===" "&&this.markdown.autolinks&&this.run(n=>ki(n)??n),this.shortcut=this.state.doc!==t.doc?{typed:t,applied:this.state}:null}e.includes(`
|
|
119
|
-
`)||this.typedForSuggestion()}undoShortcut(){let e=this.shortcut;this.shortcut=null;let{doc:t,selection:n}=this.state;return!e||t!==e.applied.doc||!R(n)||!be(n.focus,e.applied.selection.focus)?!1:this.run(()=>e.typed)}onCompositionStart(e){this.fromControl(e)||(this.dropBlockSelection(),this.refreshSelection(),R(this.state.selection)||this.run(J),this.composing=!0,this.composition={blockId:this.state.selection.focus.blockId,marks:this.state.storedMarks,recorded:!1},this.toolbar?.hide(),this.imageToolbar?.hide())}onCompositionEnd(e){if(!this.fromControl(e)){if(!this.composition){this.composing=!1,this.reconcileFromDom("other");return}if(this.syncComposition(),this.composing=!1,bl&&(this.compositionEndedAt=Date.now()),this.unsettled.add(this.composition.blockId),this.settleFrame===null){let t=()=>{this.settleFrame=null,this.settleComposition()};typeof requestAnimationFrame=="function"?this.settleFrame=requestAnimationFrame(t):this.settleComposition()}}}settleComposition(){if(this.composing||this.destroyed)return;let e=this.composition?.recorded;this.composition=null;for(let t of this.unsettled)this.renderer.invalidate(t);this.unsettled.clear(),this.paint(this.redecoratePending),this.redecoratePending=!1,this.afterRender(),e&&this.typedForSuggestion(),this.pendingMode&&this.setMode(this.pendingMode)}onInput(e){if(this.fromControl(e))return;if(this.composing||this.composition&&Na.has(e.inputType)){let n=this.composedLength();this.syncComposition(),this.composing&&this.composedLength()>n&&this.typedForSuggestion();return}if(e.isComposing)return;let t=this.state;this.reconcileFromDom("typing"),e.inputType==="insertText"&&e.data&&this.state!==t&&this.afterTyping(e.data)}composedLength(){let e=this.state.selection.focus,t=this.state.doc.blocks.find(i=>i.id===this.composition?.blockId),n=t&&F(t,t.id===e.blockId?e.cell:void 0);return n?B(n):0}endComposition(){!this.composing||!this.composition||(this.syncComposition(),this.composing=!1,this.unsettled.add(this.composition.blockId),this.settleFrame!==null&&cancelAnimationFrame(this.settleFrame),this.settleFrame=null,this.settleComposition())}syncComposition(){let e=this.composition;if(!e||this.readOnly)return;let t=this.state,n=this.readDom(t,e.marks,e.blockId);if(!n)return;e.recorded||(this.history.record(t,"other"),e.recorded=!0);let i=n.doc.blocks.find(s=>s.id===e.blockId);i&&this.renderer.adopt(i);let r=this.readDomSelection();this.commit(r?{...n,selection:te(n.doc,r)}:n)}reconcileFromDom(e){let t=this.readDom(this.state,this.state.storedMarks);if(!t){this.paint(!0);return}for(let i of t.doc.blocks)i!==N(this.state.doc,i.id)&&this.renderer.invalidate(i.id);let n=this.readDomSelection()??t.selection;this.apply({...t,selection:te(t.doc,n)},e)}readDom(e,t,n){let i=e,r=!1;for(let s of e.doc.blocks){if(n!==void 0&&s.id!==n)continue;let l=this.renderer.elementFor(s.id);if(!l||!l.isConnected)continue;let a=s.type==="table"?s.rows.flatMap((d,u)=>d.map((c,h)=>({cell:{row:u,col:h}}))):Z(s)?[]:[{}];for(let{cell:d}of a){let u=oe(l,d),c=F(N(i.doc,s.id),d);if(!u||!c)continue;let h=I(c),f=Or(u);if(h===f)continue;let p=0;for(;p<h.length&&p<f.length&&h[p]===f[p];)p++;let m=h.length,y=f.length;for(;m>p&&y>p&&h[m-1]===f[y-1];)m--,y--;let g=f.slice(p,y),x=d?{blockId:s.id,offset:p,cell:d}:{blockId:s.id,offset:p};i=ye(i,x,{...x,offset:m}),g&&(i=ue({...i,storedMarks:t},g)),r=!0}}return r?i:null}onKeyDown(e){if(this.caretKey=null,this.fromControl(e)||e.isComposing||e.keyCode===229)return;Aa.test(e.key)&&(this.shortcut=null);let t=re?e.metaKey:e.ctrlKey,n=e.key.toLowerCase(),i=a=>{e.preventDefault(),this.refreshSelection(),a()},r=Date.now()-this.compositionEndedAt<100;if(this.compositionEndedAt=0,this.suggestions.isOpen&&!r&&(this.refreshSelection(),this.suggestions.keydown(e))){e.preventDefault();return}let s=this.blockMenu.isOpen?this.blockMenu:this.toolbar?.menu??null;if(s){let a=s.submenu;if(a){if(e.key==="ArrowLeft"||e.key==="Escape")return i(()=>s.closeSubmenu());if(e.key==="ArrowDown"||e.key==="Tab"&&!e.shiftKey)return i(()=>this.moveMenu(a,1));if(e.key==="ArrowUp"||e.key==="Tab"&&e.shiftKey)return i(()=>this.moveMenu(a,-1));if(e.key==="Enter"&&a.count>0)return i(()=>a.pick())}if(e.key==="ArrowRight"&&s.openSubmenu())return i(()=>this.syncActiveDescendant());if(e.key==="ArrowDown"||e.key==="Tab"&&!e.shiftKey)return i(()=>this.moveMenu(s,1));if(e.key==="ArrowUp"||e.key==="Tab"&&e.shiftKey)return i(()=>this.moveMenu(s,-1));if(e.key==="Enter"&&s.count>0)return i(()=>s.pick());if(e.key==="Escape")return i(()=>s===this.blockMenu?this.closeBlockMenu():this.toolbar.closeMenu())}if(e.key==="Escape"&&(this.toolbar?.isOpen||this.imageToolbar?.isOpen))return i(()=>{this.toolbarClosedFor=this.state.selection,this.toolbar?.hide(),this.imageToolbar?.hide()});if(this.readOnly)return;if(this.blockSel&&this.onBlockKey(e)){e.preventDefault();return}if(e.key==="Enter"&&!t&&!e.altKey){this.refreshSelection();let a=De(this.state);if(a){let d={...a.pos,offset:a.pos.offset+1};this.setSelection({anchor:d,focus:d})}}let l=!1;for(let a of this.bindings)if(Ql(a,e)&&(l||this.refreshSelection(),l=!0,a.handler({editor:this}))){e.preventDefault();return}if(e.key==="Backspace"&&!e.ctrlKey&&!e.metaKey&&!e.altKey){if(this.refreshSelection(),this.undoShortcut()){e.preventDefault();return}let{selection:a}=this.state;if(R(a)&&a.focus.offset===0){e.preventDefault(),this.modeValue!=="source"&&this.run(nn);return}}if(t&&!e.altKey){if(e.key==="/"&&!e.shiftKey&&this.refreshSelection(),e.key==="/"&&!e.shiftKey&&this.hasBlockMenu(this.state.selection.focus.blockId))return i(()=>this.openBlockMenu(this.state.selection.focus.blockId));if(n==="v"&&e.shiftKey){this.plainPasteNext=!0;return}}if(e.key.startsWith("Arrow")&&!t&&!e.shiftKey&&!e.altKey&&this.modeValue==="rich"){this.refreshSelection();let a=this.blockPast(e.key);if(a){e.preventDefault(),this.setSelection({anchor:a,focus:a});return}let d=this.imageBlockStep(e.key);if(d){e.preventDefault(),d!==this.state.selection?this.setSelection(d):(e.key==="ArrowDown"||e.key==="ArrowUp")&&this.run(f=>on(f,e.key==="ArrowDown"?1:-1));return}let u=e.key==="ArrowDown"?1:e.key==="ArrowUp"?-1:0,c=u?this.tableCellPast(u):null;if(c){e.preventDefault(),this.setSelection({anchor:c,focus:c});return}let h=u?on(this.state,u):this.state;if(h!==this.state){e.preventDefault(),this.run(()=>h);return}}if(Aa.test(e.key)){this.refreshSelection();let d=!t&&!e.shiftKey&&!e.altKey?e.key==="ArrowDown"?1:e.key==="ArrowUp"?-1:0:0,c={from:L(this.state.doc,this.state.selection.focus.blockId),x:this.selectionRect()?.left??0,vertical:d};this.caretKey=c,d&&this.unlessCaretMoves(c)}}unlessCaretMoves(e){let t=this.state.selection.focus;if(t.cell||!R(this.state.selection))return;let n=oe(this.renderer.elementFor(t.blockId)??this.root);!n||!this.onEdgeLine(n,e.vertical===1)||setTimeout(()=>{if(this.destroyed||this.caretKey!==e)return;let i=this.readDomSelection();if(!i||!be(i.focus,t))return;let r=this.state.doc.blocks;for(let s=e.from+e.vertical;s>=0&&s<r.length;s+=e.vertical){let l=r[s];if(this.skipsCaret(l))continue;this.caretKey=null;let a=e.vertical===1?0:l.type==="table"?l.rows.length-1:0,d=l.type==="table"?{blockId:l.id,offset:e.vertical===1?0:B(l.rows[a][0]),cell:{row:a,col:0}}:this.blockEntry(l,e.vertical===1,e.x);this.setSelection({anchor:d,focus:d}),this.scrollCaretIntoView();return}e.vertical===1&&r[r.length-1].type==="detailsEnd"&&(this.caretKey=null,this.run(s=>oi(s)))})}selectBlockText(e){let t=e.closest?.(`[${W}]`);if(!t||!this.root.contains(t))return!1;let n=N(this.state.doc,t.getAttribute(W)),i=e.closest("[data-row]"),r=n.type==="table"&&i?{row:Number(i.dataset.row),col:Number(i.dataset.col)}:void 0,s=F(n,r);if(!s||t.classList.contains("mbe-view-block"))return!1;let l=a=>r?{blockId:n.id,offset:a,cell:r}:{blockId:n.id,offset:a};return this.setSelection({anchor:l(0),focus:l(B(s))}),!0}moveMenu(e,t){e.move(t),this.syncActiveDescendant()}onMouseDown(e){this.caretKey=null;let t=e.target;if(t.closest?.(".mbe-code-copy")){e.preventDefault();return}let n=e.button===0?t.closest?.(".mbe-details-toggle"):null,i=n?.closest(`[${W}]`)?.getAttribute(W);if(i&&this.root.contains(n)){e.preventDefault(),this.setSectionCollapsed(i,!this.collapsed.has(i));return}if(this.dropBlockSelection(),t===this.root&&e.button===0&&!this.readOnly&&this.modeValue==="rich"&&this.clickBelowLastBlock(e))return;let r=e.button===0&&!e.shiftKey&&!this.readOnly?t.closest?.(".mbe-image"):null,s=r&&this.root.contains(r)?this.imageTarget(r):null;if(s){e.preventDefault(),this.selectImageAt(s.pos);return}let l=e.button===0&&!this.readOnly?t.closest?.(".mbe-view, .mbe-view-block"):null;if(l&&this.root.contains(l)){e.preventDefault(),this.revealView(l);return}if(e.button===0&&e.detail===3&&!this.readOnly&&this.modeValue==="rich"&&this.selectBlockText(t)){e.preventDefault();return}let a=e.button===0&&!this.readOnly?t.closest?.(".mbe-callout-icon"):null,d=a?.parentElement?.getAttribute(W);if(a&&d&&a.parentElement.parentElement===this.root){e.preventDefault(),this.openBlockMenu(d,a.getBoundingClientRect());return}e.button===0&&(this.pointerDown=!0),t.classList?.contains("mbe-checkbox")&&(e.preventDefault(),e.button===0&&this.toggleTaskAt(t))}clickBelowLastBlock(e){let t=this.state.doc.blocks,n=t[t.length-1],i=n?Y(n):null;if(!n||n.type!=="code"&&n.type!=="table"&&!Z(n)&&i===null)return!1;let r=this.root.querySelector(`:scope > [data-block-id="${n.id}"]`);if(!r||e.clientY<=r.getBoundingClientRect().bottom)return!1;e.preventDefault(),this.root.focus({preventScroll:!0});let s=P(n),l=i!==null?{anchor:{blockId:n.id,offset:i},focus:{blockId:n.id,offset:i+1}}:{anchor:s,focus:s};return this.run(a=>on({...a,selection:l},1)),!0}onClick(e){let t=e.target;if(t.classList?.contains("mbe-checkbox")){e.preventDefault(),e.detail===0&&this.toggleTaskAt(t);return}let n=t.closest?.(".mbe-code-copy");if(n){e.preventDefault(),this.copyCode(n);return}let i=t.closest?.("a.mbe-link");i&&(e.preventDefault(),(this.readOnly||(re?e.metaKey:e.ctrlKey))&&this.openHref(i.getAttribute("href"),e))}openHref(e,t){!e||this.runtime.onLinkClick?.({editor:this,href:e,event:t})||this.root.ownerDocument.defaultView?.open(e,"_blank","noopener,noreferrer")}copyCode(e){let t=e.closest(`[${W}]`),n=t&&this.state.doc.blocks.find(u=>u.id===t.getAttribute(W)),i=n?.type==="code"?I(n.content):e.closest(".mbe-flow .mbe-code")?.querySelector("code")?.textContent;if(i==null)return;let r=this.root.ownerDocument,s=u=>{let c=X(r,this.icon(u));e.replaceChildren(...c?[c]:[]),e.setAttribute("aria-label",this.labels[u]),e.title=this.labels[u],e.classList.toggle("mbe-copied",u==="copied")},l=()=>{s("copied"),setTimeout(()=>e.isConnected&&s("copyCode"),1500)},a=()=>{let u=r.createElement("textarea");u.value=i,u.style.cssText="position:fixed;top:0;left:0;opacity:0",r.body.appendChild(u),u.select();let c=r.execCommand("copy");u.remove(),this.syncDomSelection(this.hasFocus()),c&&l()},d=r.defaultView?.navigator.clipboard;d?.writeText?d.writeText(i).then(l,a):a()}onControlChange(e){let t=e.target;if(!(t instanceof HTMLInputElement)||!t.classList.contains("mbe-code-lang"))return;let n=t.closest("[data-block-id]");n&&this.run(i=>ci(i,n.dataset.blockId,t.value.trim()))}onFocusIn(e){this.focused||!this.hasFocus()||(this.focused=!0,this.root.classList.add("mbe-focused"),this.pointerDown||this.refreshRevealed(),this.options.onFocus?.(this),this.emit("focus",{editor:this,event:e}),this.updateUi())}onFocusOut(e){let t=e.relatedTarget;t&&(this.root.contains(t)||this.inputPopover.contains(t))||queueMicrotask(()=>{this.hasFocus()||this.inputPopover.isOpen||this.focused&&(this.focused=!1,this.root.classList.remove("mbe-focused"),this.refreshRevealed(),this.hideUi(),this.flushChange(),this.options.onBlur?.(this),this.emit("blur",{editor:this,event:e}))})}onScroll(){this.blockHandle.hide(),this.linkCard?.reposition(),this.resizeHandles.reposition(),(this.suggestions.isActive||this.toolbar?.isOpen||this.imageToolbar?.isOpen||this.blockMenu.isOpen)&&this.updateUi()}onCopy(e,t){if(this.fromControl(e))return;if(this.modeValue==="source"){if(this.refreshSelection(),R(this.state.selection)||!e.clipboardData)return;e.preventDefault();let{from:d,to:u}=ne(this.state);e.clipboardData.setData("text/plain",K(this.state.doc).slice(d,u)),t&&!this.readOnly&&this.run(c=>Ae(c,""));return}let n=this.blockRange();if(n){if(!e.clipboardData)return;e.preventDefault();let d={blocks:rn(this.state.doc.blocks.slice(n.from,n.to+1).map(dt))};Oa(e.clipboardData,d,Pe(d.blocks,this.markdown)),t&&!this.readOnly&&this.deleteSelectedBlocks();return}if(this.refreshSelection(),R(this.state.selection)||!e.clipboardData)return;e.preventDefault();let i=vi(this.state),r={blocks:rn(i)},s=Pt(r,this.markdown).replace(/\n$/,""),l=i.length===1&&i[0].type==="paragraph"?i[0]:null,a=l&&Q(N(this.state.doc,this.state.selection.focus.blockId));Oa(e.clipboardData,r,s,a?I(l.content):s),t&&!this.readOnly&&this.run(J)}onPaste(e){if(this.fromControl(e)||(e.preventDefault(),this.readOnly))return;this.refreshSelection();for(let c of this.extensions)if(c.handlePaste?.({editor:this,event:e}))return;let t=e.clipboardData;if(!t)return;let n=t.getData("text/plain"),i=t.getData("text/html"),r=t.types.includes("vscode-editor-data")||ds(i)?"":i,s=this.plainPasteNext;if(this.plainPasteNext=!1,this.modeValue==="source"){let c=this.pastedUrl(n,s);if(c!==null){this.run(f=>_l(f,c));return}let h=this.pastedMarkdown(n,r,s);h&&this.run(f=>Ae(f,h.replace(/\r\n?/g,`
|
|
119
|
+
`)||this.typedForSuggestion()}undoShortcut(){let e=this.shortcut;this.shortcut=null;let{doc:t,selection:n}=this.state;return!e||t!==e.applied.doc||!R(n)||!be(n.focus,e.applied.selection.focus)?!1:this.run(()=>e.typed)}onCompositionStart(e){this.fromControl(e)||(this.dropBlockSelection(),this.refreshSelection(),R(this.state.selection)||this.run(J),this.composing=!0,this.composition={blockId:this.state.selection.focus.blockId,marks:this.state.storedMarks,recorded:!1},this.toolbar?.hide(),this.imageToolbar?.hide())}onCompositionEnd(e){if(!this.fromControl(e)){if(!this.composition){this.composing=!1,this.reconcileFromDom("other");return}if(this.syncComposition(),this.composing=!1,bl&&(this.compositionEndedAt=Date.now()),this.unsettled.add(this.composition.blockId),this.settleFrame===null){let t=()=>{this.settleFrame=null,this.settleComposition()};typeof requestAnimationFrame=="function"?this.settleFrame=requestAnimationFrame(t):this.settleComposition()}}}settleComposition(){if(this.composing||this.destroyed)return;let e=this.composition?.recorded;this.composition=null;for(let t of this.unsettled)this.renderer.invalidate(t);this.unsettled.clear(),this.paint(this.redecoratePending),this.redecoratePending=!1,this.afterRender(),e&&this.typedForSuggestion(),this.pendingMode&&this.setMode(this.pendingMode)}onInput(e){if(this.fromControl(e))return;if(this.composing||this.composition&&Na.has(e.inputType)){let n=this.composedLength();this.syncComposition(),this.composing&&this.composedLength()>n&&this.typedForSuggestion();return}if(e.isComposing)return;let t=this.state;this.reconcileFromDom("typing"),e.inputType==="insertText"&&e.data&&this.state!==t&&this.afterTyping(e.data)}composedLength(){let e=this.state.selection.focus,t=this.state.doc.blocks.find(i=>i.id===this.composition?.blockId),n=t&&F(t,t.id===e.blockId?e.cell:void 0);return n?B(n):0}endComposition(){!this.composing||!this.composition||(this.syncComposition(),this.composing=!1,this.unsettled.add(this.composition.blockId),this.settleFrame!==null&&cancelAnimationFrame(this.settleFrame),this.settleFrame=null,this.settleComposition())}syncComposition(){let e=this.composition;if(!e||this.readOnly)return;let t=this.state,n=this.readDom(t,e.marks,e.blockId);if(!n)return;e.recorded||(this.history.record(t,"other"),e.recorded=!0);let i=n.doc.blocks.find(s=>s.id===e.blockId);i&&this.renderer.adopt(i);let r=this.readDomSelection();this.commit(r?{...n,selection:te(n.doc,r)}:n)}reconcileFromDom(e){let t=this.readDom(this.state,this.state.storedMarks);if(!t){this.paint(!0);return}for(let i of t.doc.blocks)i!==N(this.state.doc,i.id)&&this.renderer.invalidate(i.id);let n=this.readDomSelection()??t.selection;this.apply({...t,selection:te(t.doc,n)},e)}readDom(e,t,n){let i=e,r=!1;for(let s of e.doc.blocks){if(n!==void 0&&s.id!==n)continue;let l=this.renderer.elementFor(s.id);if(!l||!l.isConnected)continue;let a=s.type==="table"?s.rows.flatMap((d,u)=>d.map((c,h)=>({cell:{row:u,col:h}}))):Z(s)?[]:[{}];for(let{cell:d}of a){let u=se(l,d),c=F(N(i.doc,s.id),d);if(!u||!c)continue;let h=I(c),f=Or(u);if(h===f)continue;let p=0;for(;p<h.length&&p<f.length&&h[p]===f[p];)p++;let m=h.length,y=f.length;for(;m>p&&y>p&&h[m-1]===f[y-1];)m--,y--;let g=f.slice(p,y),x=d?{blockId:s.id,offset:p,cell:d}:{blockId:s.id,offset:p};i=ye(i,x,{...x,offset:m}),g&&(i=ue({...i,storedMarks:t},g)),r=!0}}return r?i:null}onKeyDown(e){if(this.caretKey=null,this.fromControl(e)||e.isComposing||e.keyCode===229)return;Aa.test(e.key)&&(this.shortcut=null);let t=ie?e.metaKey:e.ctrlKey,n=e.key.toLowerCase(),i=a=>{e.preventDefault(),this.refreshSelection(),a()},r=Date.now()-this.compositionEndedAt<100;if(this.compositionEndedAt=0,this.suggestions.isOpen&&!r&&(this.refreshSelection(),this.suggestions.keydown(e))){e.preventDefault();return}let s=this.blockMenu.isOpen?this.blockMenu:this.toolbar?.menu??null;if(s){let a=s.submenu;if(a){if(e.key==="ArrowLeft"||e.key==="Escape")return i(()=>s.closeSubmenu());if(e.key==="ArrowDown"||e.key==="Tab"&&!e.shiftKey)return i(()=>this.moveMenu(a,1));if(e.key==="ArrowUp"||e.key==="Tab"&&e.shiftKey)return i(()=>this.moveMenu(a,-1));if(e.key==="Enter"&&a.count>0)return i(()=>a.pick())}if(e.key==="ArrowRight"&&s.openSubmenu())return i(()=>this.syncActiveDescendant());if(e.key==="ArrowDown"||e.key==="Tab"&&!e.shiftKey)return i(()=>this.moveMenu(s,1));if(e.key==="ArrowUp"||e.key==="Tab"&&e.shiftKey)return i(()=>this.moveMenu(s,-1));if(e.key==="Enter"&&s.count>0)return i(()=>s.pick());if(e.key==="Escape")return i(()=>s===this.blockMenu?this.closeBlockMenu():this.toolbar.closeMenu())}if(e.key==="Escape"&&(this.toolbar?.isOpen||this.imageToolbar?.isOpen))return i(()=>{this.toolbarClosedFor=this.state.selection,this.toolbar?.hide(),this.imageToolbar?.hide()});if(this.readOnly)return;if(this.blockSel&&this.onBlockKey(e)){e.preventDefault();return}if(e.key==="Enter"&&!t&&!e.altKey){this.refreshSelection();let a=De(this.state);if(a){let d={...a.pos,offset:a.pos.offset+1};this.setSelection({anchor:d,focus:d})}}let l=!1;for(let a of this.bindings)if(Ql(a,e)&&(l||this.refreshSelection(),l=!0,a.handler({editor:this}))){e.preventDefault();return}if(e.key==="Backspace"&&!e.ctrlKey&&!e.metaKey&&!e.altKey){if(this.refreshSelection(),this.undoShortcut()){e.preventDefault();return}let{selection:a}=this.state;if(R(a)&&a.focus.offset===0){e.preventDefault(),this.modeValue!=="source"&&this.run(nn);return}}if(t&&!e.altKey){if(e.key==="/"&&!e.shiftKey&&this.refreshSelection(),e.key==="/"&&!e.shiftKey&&this.hasBlockMenu(this.state.selection.focus.blockId))return i(()=>this.openBlockMenu(this.state.selection.focus.blockId));if(n==="v"&&e.shiftKey){this.plainPasteNext=!0;return}}if(e.key.startsWith("Arrow")&&!t&&!e.shiftKey&&!e.altKey&&this.modeValue==="rich"){this.refreshSelection();let a=this.blockPast(e.key);if(a){e.preventDefault(),this.setSelection({anchor:a,focus:a});return}let d=this.imageBlockStep(e.key);if(d){e.preventDefault(),d!==this.state.selection?this.setSelection(d):(e.key==="ArrowDown"||e.key==="ArrowUp")&&this.run(f=>on(f,e.key==="ArrowDown"?1:-1));return}let u=e.key==="ArrowDown"?1:e.key==="ArrowUp"?-1:0,c=u?this.tableCellPast(u):null;if(c){e.preventDefault(),this.setSelection({anchor:c,focus:c});return}let h=u?on(this.state,u):this.state;if(h!==this.state){e.preventDefault(),this.run(()=>h);return}}if(Aa.test(e.key)){this.refreshSelection();let d=!t&&!e.shiftKey&&!e.altKey?e.key==="ArrowDown"?1:e.key==="ArrowUp"?-1:0:0,c={from:L(this.state.doc,this.state.selection.focus.blockId),x:this.selectionRect()?.left??0,vertical:d};this.caretKey=c,d&&this.unlessCaretMoves(c)}}unlessCaretMoves(e){let t=this.state.selection.focus;if(t.cell||!R(this.state.selection))return;let n=se(this.renderer.elementFor(t.blockId)??this.root);!n||!this.onEdgeLine(n,e.vertical===1)||setTimeout(()=>{if(this.destroyed||this.caretKey!==e)return;let i=this.readDomSelection();if(!i||!be(i.focus,t))return;let r=this.state.doc.blocks;for(let s=e.from+e.vertical;s>=0&&s<r.length;s+=e.vertical){let l=r[s];if(this.skipsCaret(l))continue;this.caretKey=null;let a=e.vertical===1?0:l.type==="table"?l.rows.length-1:0,d=l.type==="table"?{blockId:l.id,offset:e.vertical===1?0:B(l.rows[a][0]),cell:{row:a,col:0}}:this.blockEntry(l,e.vertical===1,e.x);this.setSelection({anchor:d,focus:d}),this.scrollCaretIntoView();return}e.vertical===1&&r[r.length-1].type==="detailsEnd"&&(this.caretKey=null,this.run(s=>oi(s)))})}selectBlockText(e){let t=e.closest?.(`[${W}]`);if(!t||!this.root.contains(t))return!1;let n=N(this.state.doc,t.getAttribute(W)),i=e.closest("[data-row]"),r=n.type==="table"&&i?{row:Number(i.dataset.row),col:Number(i.dataset.col)}:void 0,s=F(n,r);if(!s||t.classList.contains("mbe-view-block"))return!1;let l=a=>r?{blockId:n.id,offset:a,cell:r}:{blockId:n.id,offset:a};return this.setSelection({anchor:l(0),focus:l(B(s))}),!0}moveMenu(e,t){e.move(t),this.syncActiveDescendant()}onMouseDown(e){this.caretKey=null;let t=e.target;if(t.closest?.(".mbe-code-copy")){e.preventDefault();return}let n=e.button===0?t.closest?.(".mbe-details-toggle"):null,i=n?.closest(`[${W}]`)?.getAttribute(W);if(i&&this.root.contains(n)){e.preventDefault(),this.setSectionCollapsed(i,!this.collapsed.has(i));return}if(this.dropBlockSelection(),t===this.root&&e.button===0&&!this.readOnly&&this.modeValue==="rich"&&this.clickBelowLastBlock(e))return;let r=e.button===0&&!e.shiftKey&&!this.readOnly?t.closest?.(".mbe-image"):null,s=r&&this.root.contains(r)?this.imageTarget(r):null;if(s){e.preventDefault(),this.selectImageAt(s.pos);return}let l=e.button===0&&!this.readOnly?t.closest?.(".mbe-view, .mbe-view-block"):null;if(l&&this.root.contains(l)){e.preventDefault(),this.revealView(l);return}if(e.button===0&&e.detail===3&&!this.readOnly&&this.modeValue==="rich"&&this.selectBlockText(t)){e.preventDefault();return}let a=e.button===0&&!this.readOnly?t.closest?.(".mbe-callout-icon"):null,d=a?.parentElement?.getAttribute(W);if(a&&d&&a.parentElement.parentElement===this.root){e.preventDefault(),this.openBlockMenu(d,a.getBoundingClientRect());return}e.button===0&&(this.pointerDown=!0),t.classList?.contains("mbe-checkbox")&&(e.preventDefault(),e.button===0&&this.toggleTaskAt(t))}clickBelowLastBlock(e){let t=this.state.doc.blocks,n=t[t.length-1],i=n?Y(n):null;if(!n||n.type!=="code"&&n.type!=="table"&&!Z(n)&&i===null)return!1;let r=this.root.querySelector(`:scope > [data-block-id="${n.id}"]`);if(!r||e.clientY<=r.getBoundingClientRect().bottom)return!1;e.preventDefault(),this.root.focus({preventScroll:!0});let s=P(n),l=i!==null?{anchor:{blockId:n.id,offset:i},focus:{blockId:n.id,offset:i+1}}:{anchor:s,focus:s};return this.run(a=>on({...a,selection:l},1)),!0}onClick(e){let t=e.target;if(t.classList?.contains("mbe-checkbox")){e.preventDefault(),e.detail===0&&this.toggleTaskAt(t);return}let n=t.closest?.(".mbe-code-copy");if(n){e.preventDefault(),this.copyCode(n);return}let i=t.closest?.("a.mbe-link");i&&(e.preventDefault(),(this.readOnly||(ie?e.metaKey:e.ctrlKey))&&this.openHref(i.getAttribute("href"),e))}openHref(e,t){!e||this.runtime.onLinkClick?.({editor:this,href:e,event:t})||this.root.ownerDocument.defaultView?.open(e,"_blank","noopener,noreferrer")}copyCode(e){let t=e.closest(`[${W}]`),n=t&&this.state.doc.blocks.find(u=>u.id===t.getAttribute(W)),i=n?.type==="code"?I(n.content):e.closest(".mbe-flow .mbe-code")?.querySelector("code")?.textContent;if(i==null)return;let r=this.root.ownerDocument,s=u=>{let c=X(r,this.icon(u));e.replaceChildren(...c?[c]:[]),e.setAttribute("aria-label",this.labels[u]),e.title=this.labels[u],e.classList.toggle("mbe-copied",u==="copied")},l=()=>{s("copied"),setTimeout(()=>e.isConnected&&s("copyCode"),1500)},a=()=>{let u=r.createElement("textarea");u.value=i,u.style.cssText="position:fixed;top:0;left:0;opacity:0",r.body.appendChild(u),u.select();let c=r.execCommand("copy");u.remove(),this.syncDomSelection(this.hasFocus()),c&&l()},d=r.defaultView?.navigator.clipboard;d?.writeText?d.writeText(i).then(l,a):a()}onControlChange(e){let t=e.target;if(!(t instanceof HTMLInputElement)||!t.classList.contains("mbe-code-lang"))return;let n=t.closest("[data-block-id]");n&&this.run(i=>ci(i,n.dataset.blockId,t.value.trim()))}onFocusIn(e){this.focused||!this.hasFocus()||(this.focused=!0,this.root.classList.add("mbe-focused"),this.pointerDown||this.refreshRevealed(),this.options.onFocus?.(this),this.emit("focus",{editor:this,event:e}),this.updateUi())}onFocusOut(e){let t=e.relatedTarget;t&&(this.root.contains(t)||this.inputPopover.contains(t))||queueMicrotask(()=>{this.hasFocus()||this.inputPopover.isOpen||this.focused&&(this.focused=!1,this.root.classList.remove("mbe-focused"),this.refreshRevealed(),this.hideUi(),this.flushChange(),this.options.onBlur?.(this),this.emit("blur",{editor:this,event:e}))})}onScroll(){this.blockHandle.hide(),this.linkCard?.reposition(),this.resizeHandles.reposition(),(this.suggestions.isActive||this.toolbar?.isOpen||this.imageToolbar?.isOpen||this.blockMenu.isOpen)&&this.updateUi()}onCopy(e,t){if(this.fromControl(e))return;if(this.modeValue==="source"){if(this.refreshSelection(),R(this.state.selection)||!e.clipboardData)return;e.preventDefault();let{from:d,to:u}=ne(this.state);e.clipboardData.setData("text/plain",K(this.state.doc).slice(d,u)),t&&!this.readOnly&&this.run(c=>Ae(c,""));return}let n=this.blockRange();if(n){if(!e.clipboardData)return;e.preventDefault();let d={blocks:rn(this.state.doc.blocks.slice(n.from,n.to+1).map(dt))};Oa(e.clipboardData,d,Pe(d.blocks,this.markdown)),t&&!this.readOnly&&this.deleteSelectedBlocks();return}if(this.refreshSelection(),R(this.state.selection)||!e.clipboardData)return;e.preventDefault();let i=vi(this.state),r={blocks:rn(i)},s=Pt(r,this.markdown).replace(/\n$/,""),l=i.length===1&&i[0].type==="paragraph"?i[0]:null,a=l&&Q(N(this.state.doc,this.state.selection.focus.blockId));Oa(e.clipboardData,r,s,a?I(l.content):s),t&&!this.readOnly&&this.run(J)}onPaste(e){if(this.fromControl(e)||(e.preventDefault(),this.readOnly))return;this.refreshSelection();for(let c of this.extensions)if(c.handlePaste?.({editor:this,event:e}))return;let t=e.clipboardData;if(!t)return;let n=t.getData("text/plain"),i=t.getData("text/html"),r=t.types.includes("vscode-editor-data")||ds(i)?"":i,s=this.plainPasteNext;if(this.plainPasteNext=!1,this.modeValue==="source"){let c=this.pastedUrl(n,s);if(c!==null){this.run(f=>_l(f,c));return}let h=this.pastedMarkdown(n,r,s);h&&this.run(f=>Ae(f,h.replace(/\r\n?/g,`
|
|
120
120
|
`)));return}let l=this.blockRange();if(l){let c=this.pastedBlocks(n,r,s);this.blockSel=null,this.run(h=>Gn(h,l,c));return}let a=N(this.state.doc,this.state.selection.focus.blockId);if(Q(a)){this.run(c=>ue(c,n));return}let d=this.pastedUrl(n,s);if(d!==null){this.run(c=>ut(c,d));return}let u=this.pastedBlocks(n,r,s);this.run(c=>Xn(c,u))}pastedMarkdown(e,t,n){return n||!t||Kl(this.state)?e:Ha(t)??(Pt({blocks:no(t).blocks},this.markdown).replace(/\n$/,"")||e)}pastedUrl(e,t){let n=e.trim();return!t&&!R(this.state.selection)&&Wh.test(n)?n:null}pastedBlocks(e,t,n){if(n)return e.replace(/\r\n?/g,`
|
|
121
121
|
`).split(`
|
|
122
122
|
`).map(s=>O(ge(s)));let i,r=Ha(t);return r!==null?i=bt(r,this.markdown).blocks:t?i=no(t).blocks:i=bt(e,this.markdown).blocks,i.length===0&&e&&(i=bt(e,this.markdown).blocks),i}onDrop(e){if(e.preventDefault(),this.readOnly)return;let t=this.positionAtPoint(e.clientX,e.clientY);for(let r of this.extensions)if(r.handleDrop?.({editor:this,event:e,position:t}))return;let n=e.dataTransfer?.getData("text/plain");if(!n||!t)return;if(this.modeValue==="source"){this.focus(),this.run(r=>To(r,t.offset,n.replace(/\r\n?/g,`
|
|
123
|
-
`),!1,!1));return}let i=bt(n,this.markdown).blocks;this.focus(),this.run(r=>cn(r,i,{at:t}))}positionAtPoint(e,t){let n=this.root.ownerDocument,i=null,r=0,s=n.caretPositionFromPoint?.(e,t);if(s)i=s.offsetNode,r=s.offset;else{let l=n.caretRangeFromPoint?.(e,t);l&&(i=l.startContainer,r=l.startOffset)}return!i||!this.root.contains(i)?null:_e(this.root,i,r)}typedForSuggestion(){this.suggestions.typed()&&this.updateUi()}slashSuggestion(){let t=(this.options.slashMenu||Pa).map(n=>Kh.find(i=>i.id===n)).filter(n=>!!n&&(n.id!=="callout"||this.markdown.callouts)&&(n.id!=="details"||this.markdown.details)).map(n=>({id:n.id,label:this.labels[n.id],icon:this.icon(n.id),aliases:n.aliases,spec:n.spec}));return{name:"slash",trigger:zh,allowSpaces:!0,richOnly:!0,emptyLabel:this.labels.noResults,label:this.labels.commands,className:"mbe-slash-menu",items:({query:n})=>t.filter(i=>yl(i,n)),command:({range:n,item:i})=>{let r=n.from.blockId,s=N(this.current.doc,r),l=q(s)?I(s.content):"",a=n.to.offset,d=a===l.length&&l[n.from.offset-1]===" "?n.from.offset-1:n.from.offset;this.run(u=>{let c=ye(u,{blockId:r,offset:d},{blockId:r,offset:a});return Rt(c,i.spec)})}}}get selectedBlocks(){let e=this.blockRange();return e?this.current.doc.blocks.slice(e.from,e.to+1):[]}blockRange(){if(!this.blockSel)return null;let e=this.current.doc,t=L(e,this.blockSel.anchor),n=L(e,this.blockSel.head);return t<0||n<0?null:this.wholeSections({from:Math.min(t,n),to:Math.max(t,n)})}wholeSections(e){let t=this.current.doc.blocks,{from:n,to:i}=e;if(!t.some(r=>r.type==="details"))return e;for(let r=!0;r;){r=!1;for(let s=n;s<=i;s++){let l=t[s];if(l.type==="details"){let a=At(t,s)??s;a>i&&(i=a,r=!0)}else if(l.type==="detailsEnd"){let a=si(t,s);a!==null&&a<n&&(n=a,r=!0)}}}return{from:n,to:i}}nextSelectable(e,t){let n=this.current.doc.blocks;for(let i=e+t;i>=0&&i<n.length;i+=t){let r=n[i];if(r.type!=="detailsEnd"&&!this.renderer.elementFor(r.id)?.hasAttribute("data-section-hidden"))return r.id}return n[e].id}selectBlockRange(e,t){this.blockSel={anchor:e,head:t},this.current={...this.current,selection:an(this.current.doc.blocks,this.blockRange()),storedMarks:null},this.history.breakGroup(),this.suggestions.close(),this.toolbar?.hide(),this.imageToolbar?.hide(),this.blockMenu.hide(),this.refreshRevealed(),this.paintBlockSelection(),this.syncDomSelection(!0),this.updateUi(),this.emit("selectionChange",{editor:this})}dropBlockSelection(){let e=this.parkedCaret();e&&(this.blockSel=null,this.current={...this.current,selection:{anchor:e,focus:e},storedMarks:null},this.paintBlockSelection())}exitBlockSelection(e){this.blockSel=null,this.paintBlockSelection(),this.setSelection({anchor:e,focus:e})}parkedCaret(){if(!this.blockSel)return null;let e=this.current.doc.blocks.find(t=>t.id===this.blockSel.head);return e?V(e):null}paintBlockSelection(){let e=this.blockRange(),t=[];if(e)for(let n of this.current.doc.blocks.slice(e.from,e.to+1)){let i=this.renderer.elementFor(n.id);i&&t.push(i)}for(let n of this.selectedEls)t.includes(n)||n.classList.remove("mbe-block-selected");for(let n of t)n.classList.add("mbe-block-selected");this.selectedEls=t,this.root.classList.toggle("mbe-block-selecting",t.length>0)}deleteSelectedBlocks(){let e=this.blockRange();e&&(this.blockSel=null,this.run(t=>jn(t,e)))}onBlockKey(e){let t=this.blockRange();if(!t)return!1;let n=this.state.doc.blocks,i=!e.metaKey&&!e.ctrlKey&&!e.altKey,r=re?e.metaKey:e.ctrlKey;switch(e.key){case"Escape":return!i||e.shiftKey?!1:(this.dropBlockSelection(),this.blur(),!0);case"ArrowUp":case"ArrowDown":{if(!i)return!1;let s=e.key==="ArrowUp";if(e.shiftKey){let l=L(this.current.doc,this.blockSel.head);!s&&n[l]?.type==="details"&&(l=At(n,l)??l),this.selectBlockRange(this.blockSel.anchor,this.nextSelectable(l,s?-1:1))}else{let l=this.nextSelectable(s?t.from:t.to,s?-1:1);this.selectBlockRange(l,l)}return!0}case"ArrowLeft":case"ArrowRight":return!i||e.shiftKey?!1:(this.exitBlockSelection(e.key==="ArrowLeft"?V(n[t.from]):P(n[t.to])),!0);case"Enter":return!i||e.shiftKey?!1:(this.exitBlockSelection(P(n[t.to])),!0);case"Backspace":case"Delete":return i?(this.deleteSelectedBlocks(),!0):!1}if(r&&!e.altKey&&!e.shiftKey){let s=e.key.toLowerCase();if(s==="a")return this.selectBlockRange(n[0].id,n[n.length-1].id),!0;if(s==="d"){let l=t.to-t.from+1;if(this.run(a=>ui(a,t))){let a=this.current.doc.blocks;this.selectBlockRange(a[t.to+1].id,a[t.to+l].id)}return!0}}return!1}icon(e){return this.options.icons?.[e]??fs(this.root.ownerDocument,e)}blockMenuSections(e){let t=N(this.state.doc,e),n=[],i=this.state.selection.focus.cell;if(t.type==="table"&&this.state.selection.focus.blockId===e&&i){let l=Vh.map(({id:d,action:u})=>({id:d,label:this.labels[d],icon:this.icon(d),run:()=>this.run(c=>Zn(c,u))})),a=t.align[i.col]??"left";l.splice(4,0,this.alignMenuItem("align",a,d=>({icon:this.icon(d.id),run:()=>this.run(u=>Zn(u,{kind:"align",align:d.align}))}))),n.push({title:this.labels.tableSection,items:l})}let r=M(t);if(r&&r!=="quote"){let l=r,a=Ve.map(d=>{let u=Br(d);return{id:u,label:this.labels[u],icon:this.icon(u),checked:d===l,run:()=>this.run(c=>sn(c,e,d))}});return n.push({title:this.labels.callout,items:a}),n.push({items:[{id:"delete",label:this.labels.delete,icon:this.icon("delete"),danger:!0,run:()=>this.run(d=>{let u=Nt(d,e);return u?jn(d,{from:u.start,to:u.end}):null})}]}),n}let s=Y(t);if(s!==null){let l={blockId:e,offset:s},a=C(t.content,s,s+1)[0].image,d=[this.alignMenuItem("imageAlignLeft",a.align??"left",u=>({icon:this.icon(u.image),run:()=>this.updateImageAt(l,{align:u.align==="left"?null:u.align})})),{id:"altText",label:this.labels.altText,icon:this.icon("altText"),run:()=>this.editAltText(l,a.alt)}];a.width&&d.push({id:"resetImageSize",label:this.labels.resetImageSize,icon:this.icon("resetImageSize"),run:()=>this.updateImageAt(l,{width:null})}),n.push({title:this.labels.imageSection,items:d})}return n.push({items:[{id:"duplicate",label:this.labels.duplicate,icon:this.icon("duplicate"),run:()=>this.run(l=>di(l,e))},{id:"delete",label:this.labels.delete,icon:this.icon("delete"),danger:!0,run:()=>this.run(l=>Bt(l,e))}]}),n}alignMenuItem(e,t,n){return{id:"align",label:this.labels.align,icon:this.icon(e),run:()=>{},submenu:{title:this.labels.align,items:Uh.map(i=>({id:i.id,label:this.labels[i.id],checked:t===i.align,...n(i)}))}}}hasBlockMenu(e){if(this.modeValue==="source")return!1;let t=this.state.doc.blocks.find(n=>n.id===e);return t?.type==="table"||t?.type==="code"||Ra(t)||!!t&&jr(t)}blockMenuOwner(e){if(!this.hasBlockMenu(e))return null;let t=Nt(this.state,e);return t?this.state.doc.blocks[t.start].id:e}openBlockMenu(e,t){if(this.readOnly||!this.hasBlockMenu(e))return;this.dropBlockSelection(),this.suggestions.close(),this.toolbar?.hide(),this.imageToolbar?.hide();let n=Y(N(this.state.doc,e));if(n!==null)this.current=je(this.state,{blockId:e,offset:n}),this.hasFocus()||this.focus();else if(this.state.selection.focus.blockId!==e&&!this.inSameCallout(this.state.selection.focus.blockId,e)){let r=N(this.state.doc,e),s=V(r);this.current={...this.state,selection:{anchor:s,focus:s}},this.focus()}else this.hasFocus()||this.focus();this.blockMenu.setSections(this.blockMenuSections(e));let i=t??this.blockElement(e)?.getBoundingClientRect();i&&this.blockMenu.popup.show(i,"below"),this.syncActiveDescendant()}inSameCallout(e,t){let n=N(this.state.doc,t),i=Ra(n)?Nt(this.state,t):null;if(!i)return!1;let r=L(this.state.doc,e);return r>=i.start&&r<=i.end}closeBlockMenu(){this.blockMenu.hide(),this.updateUi()}onDocumentPointerDown(e){let t=e.target;this.toolbar?.menu&&!(t&&this.toolbar.contains(t))&&this.toolbar.closeMenu(),this.blockMenu.isOpen&&(t&&(this.blockMenu.contains(t)||this.blockHandle.contains(t))||this.closeBlockMenu())}openLinkPopover(){if(this.readOnly||this.modeValue==="source")return!1;let e=N(this.state.doc,this.state.selection.focus.blockId);if(Q(e)||R(this.state.selection)&&!Qe(this.state))return!1;let t=this.selectionRect();if(!t)return!1;this.toolbar?.hide();let n=this.state.selection,i=Qe(this.current)?.href??"",{labels:r}=this,s={value:i,label:r.link,placeholder:r.linkPlaceholder,type:"url",removeLabel:r.removeLink};return this.inputPopover.open(t,s,l=>{this.current={...this.state,selection:n},l!==null&&this.run(a=>ut(a,l===""?null:l)),this.focus()}),!0}updateUi(){if(this.destroyed)return;this.expandAroundSelection();let e=this.hasFocus(),t=this.state.selection.focus,n=e&&!this.blockSel&&R(this.state.selection)?this.renderer.elementFor(t.blockId):void 0,i=!!this.options.placeholder&&this.state.doc.blocks.length===1,l=!!this.options.slashHint&&this.options.slashMenu!==!1&&n&&n.classList.contains("mbe-paragraph")&&n.classList.contains("mbe-empty")&&!this.readOnly&&!i?n:null;this.hintEl!==l&&(this.hintEl?.classList.remove("mbe-show-hint"),l?.classList.add("mbe-show-hint"),this.hintEl=l),this.suggestions.update(e);let a=e&&!this.readOnly&&!this.blockSel?this.imageTargetAt(this.state):null;this.paintSelectedImage(a?.el??null),this.resizeHandles.update(a);let d=this.toolbarClosedFor;if(d&&!(be(d.anchor,this.state.selection.anchor)&&be(d.focus,t))&&(this.toolbarClosedFor=null),this.imageToolbar&&(a&&!this.toolbarClosedFor&&!this.composing&&!this.suggestions.isActive&&!this.blockMenu.isOpen&&!this.inputPopover.isOpen&&!this.resizeHandles.isDragging?this.imageToolbar.show(a,ke(this.state)!==null):this.imageToolbar.hide()),this.toolbar){let u=this.state.doc.blocks.find(h=>h.id===t.blockId);!this.toolbarClosedFor&&e&&!this.readOnly&&!this.composing&&!R(this.state.selection)&&!this.blockSel&&!a&&!this.suggestions.isActive&&!this.blockMenu.isOpen&&!!u&&!Q(u)&&u.type!=="divider"?this.toolbar.show():this.toolbar.hide()}if(e&&!this.readOnly&&Ft()){let u=this.blockMenuOwner(t.blockId);u?this.blockHandle.showFor(u):this.blockHandle.hide()}!e&&this.blockMenu.isOpen&&!this.root.ownerDocument.activeElement?.closest?.(".mbe-ui")&&this.blockMenu.hide(),this.linkCard&&(this.readOnly?this.linkCard.hide():this.linkCard.setCaret(e&&!this.composing&&!this.blockSel&&!this.suggestions.isActive&&!this.blockMenu.isOpen&&!this.inputPopover.isOpen?this.caretLink():null)),this.syncActiveDescendant()}setSectionCollapsed(e,t){if(this.destroyed)return!1;let n=this.state.doc,i=L(n,e);if(n.blocks[i]?.type!=="details")return!1;if(this.collapsed.has(e)===t)return!0;if(t?this.collapsed.add(e):this.collapsed.delete(e),t){let r=At(n.blocks,i)??i,s=d=>{let u=L(n,d.blockId);return u>i&&u<=r},{anchor:l,focus:a}=this.state.selection;if(s(l)||s(a)){let d=P(n.blocks[i]);this.dropBlockSelection(),this.current={...this.current,selection:{anchor:d,focus:d},storedMarks:null},this.syncDomSelection()}}return this.renderer.updateSections(n),this.updateUi(),!0}isSectionCollapsed(e){return this.collapsed.has(e)}collapseClosedSections(e){for(let t of e.blocks)t.type==="details"&&!t.open&&this.collapsed.add(t.id)}expandAroundSelection(){if(this.collapsed.size===0)return;let e=this.state.selection.focus.blockId;if(!this.renderer.elementFor(e)?.hasAttribute("data-section-hidden"))return;let t=[];for(let n of this.state.doc.blocks){if(n.id===e)break;n.type==="details"?t.push(n.id):n.type==="detailsEnd"&&t.pop()}for(let n of t)this.collapsed.delete(n);this.renderer.updateSections(this.state.doc)}skipsCaret(e){return Z(e)||!!this.renderer.elementFor(e.id)?.hasAttribute("data-section-hidden")}caretLink(){if(!R(this.state.selection))return null;let e=Qe(this.state);if(!e||e.pos.offset<=e.from||e.pos.offset>=e.to)return null;let t=this.renderer.elementFor(e.pos.blockId);if(!t)return null;let n={...e.pos,offset:e.from};for(let i of t.querySelectorAll("a.mbe-link")){let r=this.linkStart(i);if(r&&be(r,n))return this.linkTargetOf(i)}return null}linkStart(e){return _e(this.root,e,0)}linkTargetOf(e){if(e.closest(".mbe-flow"))return null;let t=this.linkStart(e);if(!t)return null;let n=this.state.doc.blocks.find(s=>s.id===t.blockId),i=n&&F(n,t.cell),r=i&&Xt(i,t.offset);return r&&r.from===t.offset?{el:e,href:r.href}:null}linkSelection(e){let t=this.linkStart(e.el);if(!t)return null;let n=N(this.state.doc,t.blockId),i=F(n,t.cell),r=i&&Xt(i,t.offset);return r?{anchor:t,focus:{...t,offset:r.to}}:null}editLink(e){let t=this.linkSelection(e);!t||this.readOnly||(this.focus(),this.setSelection(t),this.openLinkPopover())}removeLink(e){let t=this.linkSelection(e);!t||this.readOnly||this.run(n=>({...ut({...n,selection:t},null),selection:n.selection}))}paintSelectedImage(e){this.selectedImageEl!==e&&(this.selectedImageEl?.classList.remove("mbe-image-selected"),e?.classList.add("mbe-image-selected"),this.selectedImageEl=e),this.root.classList.toggle("mbe-image-selection",e!==null)}imageTargetAt(e){let t=De(e);if(!t)return null;let n=this.renderer.elementFor(t.pos.blockId),i=n?pa(n,t.pos):null;return i?.classList.contains("mbe-image")?{el:i,...t}:null}imageTarget(e){if(!e.parentNode||!this.root.contains(e))return null;let t=_e(this.root,e.parentNode,xt(e));if(!t)return null;let n=je(this.current,t),i=n===this.current?null:De(n);return i?{el:e,...i}:null}selectImageAt(e){if(this.readOnly||this.destroyed)return;let t=je(this.current,e);t!==this.current&&(this.hasFocus()||this.root.focus({preventScroll:!0}),this.setSelection(t.selection))}updateImageAt(e,t){this.run(n=>Un(n,e,t))}editAltText(e,t){let n=(this.imageTargetAt(this.state)?.el??this.blockElement(e.blockId))?.getBoundingClientRect();if(!n)return;this.imageToolbar?.hide();let{labels:i}=this,r={value:t,label:i.altText,placeholder:i.altTextPlaceholder,type:"text"};this.inputPopover.open(n,r,s=>{s!==null?this.updateImageAt(e,{alt:s}):this.current=je(this.state,e),this.focus()})}deleteImageAt(e){this.run(t=>{let n=je(t,e);return n===t?null:ke(n)?Bt(n,e.blockId):J(n)})}refreshImageUi(){this.updateUi()}syncActiveDescendant(){let e=this.blockMenu.isOpen?this.blockMenu:this.toolbar?.menu,t=this.suggestions.activeDescendant??e?.activeDescendant??null;t?this.root.setAttribute("aria-activedescendant",t):this.root.removeAttribute("aria-activedescendant")}hideUi(){this.dropBlockSelection(),this.suggestions.close(),this.blockMenu.hide(),this.toolbar?.hide(),this.imageToolbar?.hide(),this.blockHandle.hide(),this.linkCard?.setCaret(null),this.resizeHandles.hide(),this.paintSelectedImage(null),this.hintEl?.classList.remove("mbe-show-hint"),this.hintEl=null}typed(e){return t=>{this.typing++;try{e(t)}finally{this.typing--}}}listen(e,t,n,i=!1){e.addEventListener(t,n,i),this.cleanup.push(()=>e.removeEventListener(t,n,i))}};function Ra(o){let e=M(o);return e!==null&&e!=="quote"}function Da(o){switch(o.type){case"listItem":return o.listType==="bullet"?"bulletList":o.listType==="ordered"?"orderedList":"taskList";case"quote":return"blockquote";case"code":return"codeBlock";case"detailsEnd":return"details";default:return o.type}}function Oa(o,e,t,n=t){o.setData("text/plain",n),o.setData("text/html",`<meta charset="utf-8"><div data-mbe-markdown="${encodeURIComponent(t)}">${dn(e)}</div>`)}var Wh=/^(https?:\/\/|mailto:)\S+$/i;function Ha(o){let e=/data-mbe-markdown="([^"]*)"/.exec(o);return e?decodeURIComponent(e[1]):null}function Qh(o,e){return o.blocks.length===e.blocks.length&&o.blocks.every((t,n)=>t===e.blocks[n])}function jh(o){return typeof o.anchor?.blockId=="string"}function Gh(o,e){let t=r=>{if(!r||typeof r!="object")return null;let{block:s,offset:l,cell:a}=r;if(!Number.isInteger(s)||!Number.isInteger(l))return null;let d=Math.max(0,Math.min(s,o.blocks.length-1)),u=o.blocks[d];if(s>=o.blocks.length)return P(u);if(u.type==="table"){let c=a&&typeof a=="object"?a:{},h=Number.isInteger(c.row)?Math.max(0,Math.min(c.row,u.rows.length-1)):0,f=Number.isInteger(c.col)?Math.max(0,Math.min(c.col,u.rows[0].length-1)):0;return{blockId:u.id,offset:Math.max(0,l),cell:{row:h,col:f}}}return{blockId:u.id,offset:Math.max(0,l)}};if(!e||typeof e!="object")return null;let n=t(e.anchor),i=t(e.focus);return!n||!i?null:te(o,{anchor:n,focus:i})}function Zh(o,e){return o.closest("[data-block-id]")?.querySelector(".mbe-content")?.textContent?.trim()||e}function Xh(o,e){for(let t=o.parentElement;t;t=t.parentElement){let n=getComputedStyle(t).overflowY;if((n==="auto"||n==="scroll")&&t.scrollHeight>t.clientHeight){t.scrollTop+=e;return}}o.ownerDocument.defaultView?.scrollBy(0,e)}function $a(o){switch(o.type){case"root":case"blockQuote":case"listItem":case"paragraph":case"heading":case"emphasis":case"strong":case"strike":case"link":case"image":return o.children;case"list":return o.items;case"table":return o.rows.flatMap(e=>e.flat());default:return[]}}function Yh(o,e){let t=[],n=i=>{if(e(i,t)!==!1){t.push(i);for(let r of $a(i))n(r);t.pop()}};n(o)}function Jh(o,e,t){let n=typeof o=="string"?pt(o):o,i=new xe(e,n);return{from:i.source(t.from),to:i.source(t.to)}}function Fa(o){return o===void 0||/\s/.test(o)}function ef(o){return o!==void 0&&o>="0"&&o<="9"}function tf(o,e){let t=0;for(;o.charCodeAt(e-t-1)===92;)t++;return t%2===1}var nf={name:"math",triggers:"$",match(o,e){if(o.charCodeAt(e-1)===36&&!tf(o,e-1))return-1;let t=e;for(;o.charCodeAt(t)===36;)t++;let n=t-e;if(n===1&&Fa(o[t]))return-1;for(let i=t;i<o.length;){if(o.charCodeAt(i)!==36){i++;continue}let r=i+1;for(;o.charCodeAt(r)===36;)r++;if(r-i===n)return n===1&&(Fa(o[i-1])||ef(o[r]))?-1:r;i=r}return-1}};function of(o){let e=0;for(;o.charCodeAt(e)===36;)e++;return e>=2&&!o.includes("$",e)?e:0}var rf={name:"math",interruptsParagraph:!0,open(o){let e=of(o);return e?{next(t){let n=t.replace(/^[ \t]+/,""),i=0;for(;n.charCodeAt(i)===36;)i++;return i>=e&&/^[ \t]*$/.test(n.slice(i))?"close":"continue"}}:null}};function sf(o,e){if(e==="inline"){let i=0;for(;o.charCodeAt(i)===36;)i++;return o.slice(i,o.length-i)}let t=o.split(`
|
|
123
|
+
`),!1,!1));return}let i=bt(n,this.markdown).blocks;this.focus(),this.run(r=>cn(r,i,{at:t}))}positionAtPoint(e,t){let n=this.root.ownerDocument,i=null,r=0,s=n.caretPositionFromPoint?.(e,t);if(s)i=s.offsetNode,r=s.offset;else{let l=n.caretRangeFromPoint?.(e,t);l&&(i=l.startContainer,r=l.startOffset)}return!i||!this.root.contains(i)?null:_e(this.root,i,r)}typedForSuggestion(){this.suggestions.typed()&&this.updateUi()}slashSuggestion(){let t=(this.options.slashMenu||Pa).map(n=>Kh.find(i=>i.id===n)).filter(n=>!!n&&(n.id!=="callout"||this.markdown.callouts)&&(n.id!=="details"||this.markdown.details)).map(n=>({id:n.id,label:this.labels[n.id],icon:this.icon(n.id),aliases:n.aliases,spec:n.spec}));return{name:"slash",trigger:zh,allowSpaces:!0,richOnly:!0,emptyLabel:this.labels.noResults,label:this.labels.commands,className:"mbe-slash-menu",items:({query:n})=>t.filter(i=>yl(i,n)),command:({range:n,item:i})=>{let r=n.from.blockId,s=N(this.current.doc,r),l=q(s)?I(s.content):"",a=n.to.offset,d=a===l.length&&l[n.from.offset-1]===" "?n.from.offset-1:n.from.offset;this.run(u=>{let c=ye(u,{blockId:r,offset:d},{blockId:r,offset:a});return Rt(c,i.spec)})}}}get selectedBlocks(){let e=this.blockRange();return e?this.current.doc.blocks.slice(e.from,e.to+1):[]}blockRange(){if(!this.blockSel)return null;let e=this.current.doc,t=L(e,this.blockSel.anchor),n=L(e,this.blockSel.head);return t<0||n<0?null:this.wholeSections({from:Math.min(t,n),to:Math.max(t,n)})}wholeSections(e){let t=this.current.doc.blocks,{from:n,to:i}=e;if(!t.some(r=>r.type==="details"))return e;for(let r=!0;r;){r=!1;for(let s=n;s<=i;s++){let l=t[s];if(l.type==="details"){let a=At(t,s)??s;a>i&&(i=a,r=!0)}else if(l.type==="detailsEnd"){let a=si(t,s);a!==null&&a<n&&(n=a,r=!0)}}}return{from:n,to:i}}nextSelectable(e,t){let n=this.current.doc.blocks;for(let i=e+t;i>=0&&i<n.length;i+=t){let r=n[i];if(r.type!=="detailsEnd"&&!this.renderer.elementFor(r.id)?.hasAttribute("data-section-hidden"))return r.id}return n[e].id}selectBlockRange(e,t){this.blockSel={anchor:e,head:t},this.current={...this.current,selection:an(this.current.doc.blocks,this.blockRange()),storedMarks:null},this.history.breakGroup(),this.suggestions.close(),this.toolbar?.hide(),this.imageToolbar?.hide(),this.blockMenu.hide(),this.refreshRevealed(),this.paintBlockSelection(),this.syncDomSelection(!0),this.updateUi(),this.emit("selectionChange",{editor:this})}dropBlockSelection(){let e=this.parkedCaret();e&&(this.blockSel=null,this.current={...this.current,selection:{anchor:e,focus:e},storedMarks:null},this.paintBlockSelection())}exitBlockSelection(e){this.blockSel=null,this.paintBlockSelection(),this.setSelection({anchor:e,focus:e})}parkedCaret(){if(!this.blockSel)return null;let e=this.current.doc.blocks.find(t=>t.id===this.blockSel.head);return e?V(e):null}paintBlockSelection(){let e=this.blockRange(),t=[];if(e)for(let n of this.current.doc.blocks.slice(e.from,e.to+1)){let i=this.renderer.elementFor(n.id);i&&t.push(i)}for(let n of this.selectedEls)t.includes(n)||n.classList.remove("mbe-block-selected");for(let n of t)n.classList.add("mbe-block-selected");this.selectedEls=t,this.root.classList.toggle("mbe-block-selecting",t.length>0)}deleteSelectedBlocks(){let e=this.blockRange();e&&(this.blockSel=null,this.run(t=>jn(t,e)))}onBlockKey(e){let t=this.blockRange();if(!t)return!1;let n=this.state.doc.blocks,i=!e.metaKey&&!e.ctrlKey&&!e.altKey,r=ie?e.metaKey:e.ctrlKey;switch(e.key){case"Escape":return!i||e.shiftKey?!1:(this.dropBlockSelection(),this.blur(),!0);case"ArrowUp":case"ArrowDown":{if(!i)return!1;let s=e.key==="ArrowUp";if(e.shiftKey){let l=L(this.current.doc,this.blockSel.head);!s&&n[l]?.type==="details"&&(l=At(n,l)??l),this.selectBlockRange(this.blockSel.anchor,this.nextSelectable(l,s?-1:1))}else{let l=this.nextSelectable(s?t.from:t.to,s?-1:1);this.selectBlockRange(l,l)}return!0}case"ArrowLeft":case"ArrowRight":return!i||e.shiftKey?!1:(this.exitBlockSelection(e.key==="ArrowLeft"?V(n[t.from]):P(n[t.to])),!0);case"Enter":return!i||e.shiftKey?!1:(this.exitBlockSelection(P(n[t.to])),!0);case"Backspace":case"Delete":return i?(this.deleteSelectedBlocks(),!0):!1}if(r&&!e.altKey&&!e.shiftKey){let s=e.key.toLowerCase();if(s==="a")return this.selectBlockRange(n[0].id,n[n.length-1].id),!0;if(s==="d"){let l=t.to-t.from+1;if(this.run(a=>ui(a,t))){let a=this.current.doc.blocks;this.selectBlockRange(a[t.to+1].id,a[t.to+l].id)}return!0}}return!1}icon(e){return this.options.icons?.[e]??fs(this.root.ownerDocument,e)}blockMenuSections(e){let t=N(this.state.doc,e),n=[],i=this.state.selection.focus.cell;if(t.type==="table"&&this.state.selection.focus.blockId===e&&i){let l=Vh.map(({id:d,action:u})=>({id:d,label:this.labels[d],icon:this.icon(d),run:()=>this.run(c=>Zn(c,u))})),a=t.align[i.col]??"left";l.splice(4,0,this.alignMenuItem("align",a,d=>({icon:this.icon(d.id),run:()=>this.run(u=>Zn(u,{kind:"align",align:d.align}))}))),n.push({title:this.labels.tableSection,items:l})}let r=M(t);if(r&&r!=="quote"){let l=r,a=Ve.map(d=>{let u=Br(d);return{id:u,label:this.labels[u],icon:this.icon(u),checked:d===l,run:()=>this.run(c=>sn(c,e,d))}});return n.push({title:this.labels.callout,items:a}),n.push({items:[{id:"delete",label:this.labels.delete,icon:this.icon("delete"),danger:!0,run:()=>this.run(d=>{let u=Nt(d,e);return u?jn(d,{from:u.start,to:u.end}):null})}]}),n}let s=Y(t);if(s!==null){let l={blockId:e,offset:s},a=C(t.content,s,s+1)[0].image,d=[this.alignMenuItem("imageAlignLeft",a.align??"left",u=>({icon:this.icon(u.image),run:()=>this.updateImageAt(l,{align:u.align==="left"?null:u.align})})),{id:"altText",label:this.labels.altText,icon:this.icon("altText"),run:()=>this.editAltText(l,a.alt)}];a.width&&d.push({id:"resetImageSize",label:this.labels.resetImageSize,icon:this.icon("resetImageSize"),run:()=>this.updateImageAt(l,{width:null})}),n.push({title:this.labels.imageSection,items:d})}return n.push({items:[{id:"duplicate",label:this.labels.duplicate,icon:this.icon("duplicate"),run:()=>this.run(l=>di(l,e))},{id:"delete",label:this.labels.delete,icon:this.icon("delete"),danger:!0,run:()=>this.run(l=>Bt(l,e))}]}),n}alignMenuItem(e,t,n){return{id:"align",label:this.labels.align,icon:this.icon(e),run:()=>{},submenu:{title:this.labels.align,items:Uh.map(i=>({id:i.id,label:this.labels[i.id],checked:t===i.align,...n(i)}))}}}hasBlockMenu(e){if(this.modeValue==="source")return!1;let t=this.state.doc.blocks.find(n=>n.id===e);return t?.type==="table"||t?.type==="code"||Ra(t)||!!t&&jr(t)}blockMenuOwner(e){if(!this.hasBlockMenu(e))return null;let t=Nt(this.state,e);return t?this.state.doc.blocks[t.start].id:e}openBlockMenu(e,t){if(this.readOnly||!this.hasBlockMenu(e))return;this.dropBlockSelection(),this.suggestions.close(),this.toolbar?.hide(),this.imageToolbar?.hide();let n=Y(N(this.state.doc,e));if(n!==null)this.current=je(this.state,{blockId:e,offset:n}),this.hasFocus()||this.focus();else if(this.state.selection.focus.blockId!==e&&!this.inSameCallout(this.state.selection.focus.blockId,e)){let r=N(this.state.doc,e),s=V(r);this.current={...this.state,selection:{anchor:s,focus:s}},this.focus()}else this.hasFocus()||this.focus();this.blockMenu.setSections(this.blockMenuSections(e));let i=t??this.blockElement(e)?.getBoundingClientRect();i&&this.blockMenu.popup.show(i,"below"),this.syncActiveDescendant()}inSameCallout(e,t){let n=N(this.state.doc,t),i=Ra(n)?Nt(this.state,t):null;if(!i)return!1;let r=L(this.state.doc,e);return r>=i.start&&r<=i.end}closeBlockMenu(){this.blockMenu.hide(),this.updateUi()}onDocumentPointerDown(e){let t=e.target;this.toolbar?.menu&&!(t&&this.toolbar.contains(t))&&this.toolbar.closeMenu(),this.blockMenu.isOpen&&(t&&(this.blockMenu.contains(t)||this.blockHandle.contains(t))||this.closeBlockMenu())}openLinkPopover(){if(this.readOnly||this.modeValue==="source")return!1;let e=N(this.state.doc,this.state.selection.focus.blockId);if(Q(e)||R(this.state.selection)&&!Qe(this.state))return!1;let t=this.selectionRect();if(!t)return!1;this.toolbar?.hide();let n=this.state.selection,i=Qe(this.current)?.href??"",{labels:r}=this,s={value:i,label:r.link,placeholder:r.linkPlaceholder,type:"url",removeLabel:r.removeLink};return this.inputPopover.open(t,s,l=>{this.current={...this.state,selection:n},l!==null&&this.run(a=>ut(a,l===""?null:l)),this.focus()}),!0}updateUi(){if(this.destroyed)return;this.expandAroundSelection();let e=this.hasFocus(),t=this.state.selection.focus,n=e&&!this.blockSel&&R(this.state.selection)?this.renderer.elementFor(t.blockId):void 0,i=!!this.options.placeholder&&this.state.doc.blocks.length===1,l=!!this.options.slashHint&&this.options.slashMenu!==!1&&n&&n.classList.contains("mbe-paragraph")&&n.classList.contains("mbe-empty")&&!this.readOnly&&!i?n:null;this.hintEl!==l&&(this.hintEl?.classList.remove("mbe-show-hint"),l?.classList.add("mbe-show-hint"),this.hintEl=l),this.suggestions.update(e);let a=e&&!this.readOnly&&!this.blockSel?this.imageTargetAt(this.state):null;this.paintSelectedImage(a?.el??null),this.resizeHandles.update(a);let d=this.toolbarClosedFor;if(d&&!(be(d.anchor,this.state.selection.anchor)&&be(d.focus,t))&&(this.toolbarClosedFor=null),this.imageToolbar&&(a&&!this.toolbarClosedFor&&!this.composing&&!this.suggestions.isActive&&!this.blockMenu.isOpen&&!this.inputPopover.isOpen&&!this.resizeHandles.isDragging?this.imageToolbar.show(a,ke(this.state)!==null):this.imageToolbar.hide()),this.toolbar){let u=this.state.doc.blocks.find(h=>h.id===t.blockId);!this.toolbarClosedFor&&e&&!this.readOnly&&!this.composing&&!R(this.state.selection)&&!this.blockSel&&!a&&!this.suggestions.isActive&&!this.blockMenu.isOpen&&!!u&&!Q(u)&&u.type!=="divider"?this.toolbar.show():this.toolbar.hide()}if(e&&!this.readOnly&&Ft()){let u=this.blockMenuOwner(t.blockId);u?this.blockHandle.showFor(u):this.blockHandle.hide()}!e&&this.blockMenu.isOpen&&!this.root.ownerDocument.activeElement?.closest?.(".mbe-ui")&&this.blockMenu.hide(),this.linkCard&&(this.readOnly?this.linkCard.hide():this.linkCard.setCaret(e&&!this.composing&&!this.blockSel&&!this.suggestions.isActive&&!this.blockMenu.isOpen&&!this.inputPopover.isOpen?this.caretLink():null)),this.syncActiveDescendant()}setSectionCollapsed(e,t){if(this.destroyed)return!1;let n=this.state.doc,i=L(n,e);if(n.blocks[i]?.type!=="details")return!1;if(this.collapsed.has(e)===t)return!0;if(t?this.collapsed.add(e):this.collapsed.delete(e),t){let r=At(n.blocks,i)??i,s=d=>{let u=L(n,d.blockId);return u>i&&u<=r},{anchor:l,focus:a}=this.state.selection;if(s(l)||s(a)){let d=P(n.blocks[i]);this.dropBlockSelection(),this.current={...this.current,selection:{anchor:d,focus:d},storedMarks:null},this.syncDomSelection()}}return this.renderer.updateSections(n),this.updateUi(),!0}isSectionCollapsed(e){return this.collapsed.has(e)}collapseClosedSections(e){for(let t of e.blocks)t.type==="details"&&!t.open&&this.collapsed.add(t.id)}expandAroundSelection(){if(this.collapsed.size===0)return;let e=this.state.selection.focus.blockId;if(!this.renderer.elementFor(e)?.hasAttribute("data-section-hidden"))return;let t=[];for(let n of this.state.doc.blocks){if(n.id===e)break;n.type==="details"?t.push(n.id):n.type==="detailsEnd"&&t.pop()}for(let n of t)this.collapsed.delete(n);this.renderer.updateSections(this.state.doc)}skipsCaret(e){return Z(e)||!!this.renderer.elementFor(e.id)?.hasAttribute("data-section-hidden")}caretLink(){if(!R(this.state.selection))return null;let e=Qe(this.state);if(!e||e.pos.offset<=e.from||e.pos.offset>=e.to)return null;let t=this.renderer.elementFor(e.pos.blockId);if(!t)return null;let n={...e.pos,offset:e.from};for(let i of t.querySelectorAll("a.mbe-link")){let r=this.linkStart(i);if(r&&be(r,n))return this.linkTargetOf(i)}return null}linkStart(e){return _e(this.root,e,0)}linkTargetOf(e){if(e.closest(".mbe-flow"))return null;let t=this.linkStart(e);if(!t)return null;let n=this.state.doc.blocks.find(s=>s.id===t.blockId),i=n&&F(n,t.cell),r=i&&Xt(i,t.offset);return r&&r.from===t.offset?{el:e,href:r.href}:null}linkSelection(e){let t=this.linkStart(e.el);if(!t)return null;let n=N(this.state.doc,t.blockId),i=F(n,t.cell),r=i&&Xt(i,t.offset);return r?{anchor:t,focus:{...t,offset:r.to}}:null}editLink(e){let t=this.linkSelection(e);!t||this.readOnly||(this.focus(),this.setSelection(t),this.openLinkPopover())}removeLink(e){let t=this.linkSelection(e);!t||this.readOnly||this.run(n=>({...ut({...n,selection:t},null),selection:n.selection}))}paintSelectedImage(e){this.selectedImageEl!==e&&(this.selectedImageEl?.classList.remove("mbe-image-selected"),e?.classList.add("mbe-image-selected"),this.selectedImageEl=e),this.root.classList.toggle("mbe-image-selection",e!==null)}imageTargetAt(e){let t=De(e);if(!t)return null;let n=this.renderer.elementFor(t.pos.blockId),i=n?pa(n,t.pos):null;return i?.classList.contains("mbe-image")?{el:i,...t}:null}imageTarget(e){if(!e.parentNode||!this.root.contains(e))return null;let t=_e(this.root,e.parentNode,xt(e));if(!t)return null;let n=je(this.current,t),i=n===this.current?null:De(n);return i?{el:e,...i}:null}selectImageAt(e){if(this.readOnly||this.destroyed)return;let t=je(this.current,e);t!==this.current&&(this.hasFocus()||this.root.focus({preventScroll:!0}),this.setSelection(t.selection))}updateImageAt(e,t){this.run(n=>Un(n,e,t))}editAltText(e,t){let n=(this.imageTargetAt(this.state)?.el??this.blockElement(e.blockId))?.getBoundingClientRect();if(!n)return;this.imageToolbar?.hide();let{labels:i}=this,r={value:t,label:i.altText,placeholder:i.altTextPlaceholder,type:"text"};this.inputPopover.open(n,r,s=>{s!==null?this.updateImageAt(e,{alt:s}):this.current=je(this.state,e),this.focus()})}deleteImageAt(e){this.run(t=>{let n=je(t,e);return n===t?null:ke(n)?Bt(n,e.blockId):J(n)})}refreshImageUi(){this.updateUi()}syncActiveDescendant(){let e=this.blockMenu.isOpen?this.blockMenu:this.toolbar?.menu,t=this.suggestions.activeDescendant??e?.activeDescendant??null;t?this.root.setAttribute("aria-activedescendant",t):this.root.removeAttribute("aria-activedescendant")}hideUi(){this.dropBlockSelection(),this.suggestions.close(),this.blockMenu.hide(),this.toolbar?.hide(),this.imageToolbar?.hide(),this.blockHandle.hide(),this.linkCard?.setCaret(null),this.resizeHandles.hide(),this.paintSelectedImage(null),this.hintEl?.classList.remove("mbe-show-hint"),this.hintEl=null}typed(e){return t=>{this.typing++;try{e(t)}finally{this.typing--}}}listen(e,t,n,i=!1){e.addEventListener(t,n,i),this.cleanup.push(()=>e.removeEventListener(t,n,i))}};function Ra(o){let e=M(o);return e!==null&&e!=="quote"}function Da(o){switch(o.type){case"listItem":return o.listType==="bullet"?"bulletList":o.listType==="ordered"?"orderedList":"taskList";case"quote":return"blockquote";case"code":return"codeBlock";case"detailsEnd":return"details";default:return o.type}}function Oa(o,e,t,n=t){o.setData("text/plain",n),o.setData("text/html",`<meta charset="utf-8"><div data-mbe-markdown="${encodeURIComponent(t)}">${dn(e)}</div>`)}var Wh=/^(https?:\/\/|mailto:)\S+$/i;function Ha(o){let e=/data-mbe-markdown="([^"]*)"/.exec(o);return e?decodeURIComponent(e[1]):null}function Qh(o,e){return o.blocks.length===e.blocks.length&&o.blocks.every((t,n)=>t===e.blocks[n])}function jh(o){return typeof o.anchor?.blockId=="string"}function Gh(o,e){let t=r=>{if(!r||typeof r!="object")return null;let{block:s,offset:l,cell:a}=r;if(!Number.isInteger(s)||!Number.isInteger(l))return null;let d=Math.max(0,Math.min(s,o.blocks.length-1)),u=o.blocks[d];if(s>=o.blocks.length)return P(u);if(u.type==="table"){let c=a&&typeof a=="object"?a:{},h=Number.isInteger(c.row)?Math.max(0,Math.min(c.row,u.rows.length-1)):0,f=Number.isInteger(c.col)?Math.max(0,Math.min(c.col,u.rows[0].length-1)):0;return{blockId:u.id,offset:Math.max(0,l),cell:{row:h,col:f}}}return{blockId:u.id,offset:Math.max(0,l)}};if(!e||typeof e!="object")return null;let n=t(e.anchor),i=t(e.focus);return!n||!i?null:te(o,{anchor:n,focus:i})}function Zh(o,e){return o.closest("[data-block-id]")?.querySelector(".mbe-content")?.textContent?.trim()||e}function Xh(o,e){for(let t=o.parentElement;t;t=t.parentElement){let n=getComputedStyle(t).overflowY;if((n==="auto"||n==="scroll")&&t.scrollHeight>t.clientHeight){t.scrollTop+=e;return}}o.ownerDocument.defaultView?.scrollBy(0,e)}function $a(o){switch(o.type){case"root":case"blockQuote":case"listItem":case"paragraph":case"heading":case"emphasis":case"strong":case"strike":case"link":case"image":return o.children;case"list":return o.items;case"table":return o.rows.flatMap(e=>e.flat());default:return[]}}function Yh(o,e){let t=[],n=i=>{if(e(i,t)!==!1){t.push(i);for(let r of $a(i))n(r);t.pop()}};n(o)}function Jh(o,e,t){let n=typeof o=="string"?pt(o):o,i=new xe(e,n);return{from:i.source(t.from),to:i.source(t.to)}}function Fa(o){return o===void 0||/\s/.test(o)}function ef(o){return o!==void 0&&o>="0"&&o<="9"}function tf(o,e){let t=0;for(;o.charCodeAt(e-t-1)===92;)t++;return t%2===1}var nf={name:"math",triggers:"$",match(o,e){if(o.charCodeAt(e-1)===36&&!tf(o,e-1))return-1;let t=e;for(;o.charCodeAt(t)===36;)t++;let n=t-e;if(n===1&&Fa(o[t]))return-1;for(let i=t;i<o.length;){if(o.charCodeAt(i)!==36){i++;continue}let r=i+1;for(;o.charCodeAt(r)===36;)r++;if(r-i===n)return n===1&&(Fa(o[i-1])||ef(o[r]))?-1:r;i=r}return-1}};function of(o){let e=0;for(;o.charCodeAt(e)===36;)e++;return e>=2&&!o.includes("$",e)?e:0}var rf={name:"math",interruptsParagraph:!0,open(o){let e=of(o);return e?{next(t){let n=t.replace(/^[ \t]+/,""),i=0;for(;n.charCodeAt(i)===36;)i++;return i>=e&&/^[ \t]*$/.test(n.slice(i))?"close":"continue"}}:null}};function sf(o,e){if(e==="inline"){let i=0;for(;o.charCodeAt(i)===36;)i++;return o.slice(i,o.length-i)}let t=o.split(`
|
|
124
124
|
`),n=t.length>1&&/^[ \t]*\${2,}[ \t]*$/.test(t[t.length-1]);return t.slice(1,n?-1:void 0).join(`
|
|
125
125
|
`)}function lf(o={}){let{render:e}=o;return{name:"math",markdown:{inline:[nf],block:[rf],escape:"$"},views:e&&{math(t,n,i){let r=sf(t,i);if(!r.trim())return!1;let s=e(r,n,{displayMode:i==="block"});if(s)return n.hasChildNodes()||(n.textContent=t),s}}}}var qr=/\[\^(?:[^\]\s\\]|\\.)+\]/y,qa=/^\[\^(?:[^\]\s\\]|\\.)+\]:/,af=/^(?:#{1,6}(?:[ \t]|$)|>|[-*+][ \t]|\d{1,9}[.)][ \t]|`{3,}|~{3,}|\$\$|<[A-Za-z/!?])/,_a={name:"footnote",triggers:"[",match(o,e){return qr.lastIndex=e,qr.test(o)?qr.lastIndex:-1}},Ka={name:"footnote",open(o){if(!qa.test(o))return null;let e=!1;return{next(t){if(/^[ \t]*$/.test(t))return e=!0,"continue";if(e)return e=!1,/^(?: {4}|\t)/.test(t)?"continue":"end";let n=t.replace(/^[ \t]+/,"");return qa.test(n)||af.test(n)?"end":"continue"}}}},Va=o=>/^\[\^((?:[^\]\\]|\\.)+)\]/.exec(o)?.[1]?.replace(/\\(.)/g,"$1")??"",cf=(o,e)=>{let t=Va(o),n=e.ownerDocument.createElement("sup");n.className="mbe-footnote-ref",n.dataset.label=t,n.textContent=t,e.appendChild(n)},df=(o,e)=>{let t=Va(o),n=e.ownerDocument;e.classList.add("mbe-footnote"),e.dataset.label=t;let i=n.createElement("sup");i.className="mbe-footnote-label",i.textContent=t;let r=n.createElement("span");r.textContent=o.replace(/^\[\^(?:[^\]\\]|\\.)+\]:[ \t]*/,"").split(`
|
|
126
|
-
`).map(s=>s.trim()).filter(Boolean).join(" "),e.append(i,r)};function uf(o={}){return o.render===!1?{name:"footnotes",markdown:{inline:[_a],block:[Ka]}}:{name:"footnotes",markdown:{inline:[_a],block:[Ka]},views:{footnote:(e,t,n,i)=>(n==="inline"?cf:df)(e,t,n,i)},onCreate:({editor:e})=>{let t=n=>{if(e.isEditable)return;let i=n.target,r=i.closest?.(".mbe-footnote-ref"),s=r?null:i.closest?.(".mbe-footnote-backref");if(!r&&!s)return;let l=d=>d.closest(".mbe-footnotes")!==null,a;if(r){let d=[...e.root.querySelectorAll(".mbe-footnote")].filter(u=>u.dataset.label===r.dataset.label);a=d.find(l)??d[0]}else s&&(a=[...e.root.querySelectorAll(".mbe-footnote-ref")].filter(u=>u.dataset.label===s.dataset.label&&!l(u))[Number(s.dataset.ref)-1]);a&&(n.preventDefault(),a.scrollIntoView({block:"center",behavior:"smooth"}))};return e.root.addEventListener("click",t),()=>e.root.removeEventListener("click",t)}}}function hf(o){let e=o.pattern&&new RegExp(o.pattern.source,o.pattern.flags.includes("g")?o.pattern.flags:o.pattern.flags+"g");return{name:o.name??"textDecorator",decorations({text:t,excluded:n}){let i=[],r=(s,l,a=o.class,d=o.attributes)=>{l>s&&!es(s,l,n)&&i.push(d?{from:s,to:l,class:a,attributes:d}:{from:s,to:l,class:a})};if(e)for(let s of t.matchAll(e))r(s.index,s.index+s[0].length);if(o.scan)for(let s of o.scan(t))r(s.from,s.to,s.class,s.attributes??o.attributes);return i}}}var ja=/\p{XID_Continue}/u,_r=/\p{Default_Ignorable_Code_Point}/u,Kr=/[\p{Mn}\p{Mc}]/u,ff="\u3003\uFF5E\u301C\u05BE\u05F3\u05F4\u0F0B",Ua=String.raw`(?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}|(?![\p{Regional_Indicator}\p{Emoji_Modifier}])\p{Emoji_Presentation}\uFE0F?|(?![#*0-9])\p{Emoji}\uFE0F|(?!\p{Emoji})(?=\P{Assigned})\p{Extended_Pictographic})`,za=new RegExp(String.raw`[#*0-9]\uFE0F\u20E3|\p{Regional_Indicator}{2}|\u{1F3F4}[\u{E0020}-\u{E007E}]+\u{E007F}|${Ua}(?:\u200D${Ua})*`,"uy");function Uo(o,e){return String.fromCodePoint(o.codePointAt(e))}function zo(o,e,t){za.lastIndex=e;let n=za.exec(o);return n&&e+n[0].length<=t?n[0].length:0}var Nn=o=>ja.test(o)&&!_r.test(o),pf=o=>o==="-"||o==="+"||o==="&"||Nn(o)&&!Kr.test(o),mf=o=>o==="-"||o==="+"||o==="&"||ff.includes(o)||Nn(o);function Wa(o,e,t){let n=e;for(;;){if(n>=t)return-1;let r=zo(o,n,t);if(r>0){n+=r;break}let s=Uo(o,n);if(n+s.length>t)return-1;if(_r.test(s)||ja.test(s)&&Kr.test(s)){n+=s.length;continue}if(!pf(s))return-1;n+=s.length;break}let i=n>e&&Nn(bf(o,n));for(;n<t;){let r=zo(o,n,t);if(r>0){n+=r,i=!1;continue}let s=Uo(o,n);if(n+s.length>t)break;if(_r.test(s))n+=s.length,i=!1;else if((s==="'"||s==="\u2019")&&i&&gf(o,n+1,t))n+=1,i=!1;else if(mf(s))n+=s.length,i=Nn(s);else break}return n}function gf(o,e,t){if(e>=t||zo(o,e,t)>0)return!1;let n=Uo(o,e);return Nn(n)&&!Kr.test(n)}function bf(o,e){if(e<=0)return;let t=o.charCodeAt(e-1);return t>=56320&&t<=57343&&e>1?o.slice(e-2,e):o[e-1]}function*Ga(o,e=0,t=o.length){let n=e;for(;n<t;){let i=zo(o,n,t);if(i>0){n+=i;continue}if(o[n]!=="#"){n+=Uo(o,n).length;continue}let r=Wa(o,n+1,t);if(r<0){n++;continue}for(;o[r]==="/";){let s=Wa(o,r+1,t);if(s<0)break;r=s}yield{from:n,to:r},n=r}}var Qa=/[A-Za-z0-9-]/,yf=/[\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u024f\u0253\u0254\u0256\u0257\u0259\u025b\u0263\u0268\u026f\u0272\u0289\u028b\u02bb\u0300-\u036f\u1e00-\u1eff]/;function*Za(o,e=0,t=o.length){for(let n=o.indexOf("@",e);n>=0&&n<t;n=o.indexOf("@",n+1)){if(n>e&&Qa.test(o[n-1]))continue;let i=n+1;for(;i<t&&Qa.test(o[i]);)i++;let r=o.slice(n+1,i);/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,34}[A-Za-z0-9])?$/.test(r)&&!yf.test(o[i]??"")&&(yield{from:n,to:i}),i>n+1&&(n=i-1)}}function kf(o,e,t,n){return({text:i,content:r})=>{let s=[],l=0;for(let a of r){let d=l+a.text.length;if(!ct(a))for(let u of o(i,l,d)){let c=Math.max(u.from,l),h=Math.min(u.to,d);if(h<=c)continue;let f=i.slice(c,h),p=typeof e=="function"?e(f):e,m=typeof t=="function"?t(f):t,y=n?{...m,[n]:f}:m;s.push(y?{from:c,to:h,class:p,attributes:y}:{from:c,to:h,class:p})}l=d}return s}}function vf(o){return{name:"escape",triggers:"\\",match:(e,t)=>e[t+1]===o?t+2:-1}}function Xa(o,e,t,n,i,r){let s=r.onClick,l={name:o,markdown:{inline:[vf(e)]},decorations:kf(r.scan??t,r.class??n,r.attributes,s?i:void 0)};return s?{...l,onCreate:({editor:a})=>{let d=u=>{let c=u.target.closest?.(`[${i}]`);!c||!a.root.contains(c)||a.isEditable&&!(
|
|
126
|
+
`).map(s=>s.trim()).filter(Boolean).join(" "),e.append(i,r)};function uf(o={}){return o.render===!1?{name:"footnotes",markdown:{inline:[_a],block:[Ka]}}:{name:"footnotes",markdown:{inline:[_a],block:[Ka]},views:{footnote:(e,t,n,i)=>(n==="inline"?cf:df)(e,t,n,i)},onCreate:({editor:e})=>{let t=n=>{if(e.isEditable)return;let i=n.target,r=i.closest?.(".mbe-footnote-ref"),s=r?null:i.closest?.(".mbe-footnote-backref");if(!r&&!s)return;let l=d=>d.closest(".mbe-footnotes")!==null,a;if(r){let d=[...e.root.querySelectorAll(".mbe-footnote")].filter(u=>u.dataset.label===r.dataset.label);a=d.find(l)??d[0]}else s&&(a=[...e.root.querySelectorAll(".mbe-footnote-ref")].filter(u=>u.dataset.label===s.dataset.label&&!l(u))[Number(s.dataset.ref)-1]);a&&(n.preventDefault(),a.scrollIntoView({block:"center",behavior:"smooth"}))};return e.root.addEventListener("click",t),()=>e.root.removeEventListener("click",t)}}}function hf(o){let e=o.pattern&&new RegExp(o.pattern.source,o.pattern.flags.includes("g")?o.pattern.flags:o.pattern.flags+"g");return{name:o.name??"textDecorator",decorations({text:t,excluded:n}){let i=[],r=(s,l,a=o.class,d=o.attributes)=>{l>s&&!es(s,l,n)&&i.push(d?{from:s,to:l,class:a,attributes:d}:{from:s,to:l,class:a})};if(e)for(let s of t.matchAll(e))r(s.index,s.index+s[0].length);if(o.scan)for(let s of o.scan(t))r(s.from,s.to,s.class,s.attributes??o.attributes);return i}}}var ja=/\p{XID_Continue}/u,_r=/\p{Default_Ignorable_Code_Point}/u,Kr=/[\p{Mn}\p{Mc}]/u,ff="\u3003\uFF5E\u301C\u05BE\u05F3\u05F4\u0F0B",Ua=String.raw`(?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}|(?![\p{Regional_Indicator}\p{Emoji_Modifier}])\p{Emoji_Presentation}\uFE0F?|(?![#*0-9])\p{Emoji}\uFE0F|(?!\p{Emoji})(?=\P{Assigned})\p{Extended_Pictographic})`,za=new RegExp(String.raw`[#*0-9]\uFE0F\u20E3|\p{Regional_Indicator}{2}|\u{1F3F4}[\u{E0020}-\u{E007E}]+\u{E007F}|${Ua}(?:\u200D${Ua})*`,"uy");function Uo(o,e){return String.fromCodePoint(o.codePointAt(e))}function zo(o,e,t){za.lastIndex=e;let n=za.exec(o);return n&&e+n[0].length<=t?n[0].length:0}var Nn=o=>ja.test(o)&&!_r.test(o),pf=o=>o==="-"||o==="+"||o==="&"||Nn(o)&&!Kr.test(o),mf=o=>o==="-"||o==="+"||o==="&"||ff.includes(o)||Nn(o);function Wa(o,e,t){let n=e;for(;;){if(n>=t)return-1;let r=zo(o,n,t);if(r>0){n+=r;break}let s=Uo(o,n);if(n+s.length>t)return-1;if(_r.test(s)||ja.test(s)&&Kr.test(s)){n+=s.length;continue}if(!pf(s))return-1;n+=s.length;break}let i=n>e&&Nn(bf(o,n));for(;n<t;){let r=zo(o,n,t);if(r>0){n+=r,i=!1;continue}let s=Uo(o,n);if(n+s.length>t)break;if(_r.test(s))n+=s.length,i=!1;else if((s==="'"||s==="\u2019")&&i&&gf(o,n+1,t))n+=1,i=!1;else if(mf(s))n+=s.length,i=Nn(s);else break}return n}function gf(o,e,t){if(e>=t||zo(o,e,t)>0)return!1;let n=Uo(o,e);return Nn(n)&&!Kr.test(n)}function bf(o,e){if(e<=0)return;let t=o.charCodeAt(e-1);return t>=56320&&t<=57343&&e>1?o.slice(e-2,e):o[e-1]}function*Ga(o,e=0,t=o.length){let n=e;for(;n<t;){let i=zo(o,n,t);if(i>0){n+=i;continue}if(o[n]!=="#"){n+=Uo(o,n).length;continue}let r=Wa(o,n+1,t);if(r<0){n++;continue}for(;o[r]==="/";){let s=Wa(o,r+1,t);if(s<0)break;r=s}yield{from:n,to:r},n=r}}var Qa=/[A-Za-z0-9-]/,yf=/[\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u024f\u0253\u0254\u0256\u0257\u0259\u025b\u0263\u0268\u026f\u0272\u0289\u028b\u02bb\u0300-\u036f\u1e00-\u1eff]/;function*Za(o,e=0,t=o.length){for(let n=o.indexOf("@",e);n>=0&&n<t;n=o.indexOf("@",n+1)){if(n>e&&Qa.test(o[n-1]))continue;let i=n+1;for(;i<t&&Qa.test(o[i]);)i++;let r=o.slice(n+1,i);/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,34}[A-Za-z0-9])?$/.test(r)&&!yf.test(o[i]??"")&&(yield{from:n,to:i}),i>n+1&&(n=i-1)}}function kf(o,e,t,n){return({text:i,content:r})=>{let s=[],l=0;for(let a of r){let d=l+a.text.length;if(!ct(a))for(let u of o(i,l,d)){let c=Math.max(u.from,l),h=Math.min(u.to,d);if(h<=c)continue;let f=i.slice(c,h),p=typeof e=="function"?e(f):e,m=typeof t=="function"?t(f):t,y=n?{...m,[n]:f}:m;s.push(y?{from:c,to:h,class:p,attributes:y}:{from:c,to:h,class:p})}l=d}return s}}function vf(o){return{name:"escape",triggers:"\\",match:(e,t)=>e[t+1]===o?t+2:-1}}function Xa(o,e,t,n,i,r){let s=r.onClick,l={name:o,markdown:{inline:[vf(e)]},decorations:kf(r.scan??t,r.class??n,r.attributes,s?i:void 0)};return s?{...l,onCreate:({editor:a})=>{let d=u=>{let c=u.target.closest?.(`[${i}]`);!c||!a.root.contains(c)||a.isEditable&&!(ie?u.metaKey:u.ctrlKey)||s({editor:a,match:c.getAttribute(i),event:u})&&(u.preventDefault(),u.stopPropagation())};return a.root.addEventListener("click",d),()=>a.root.removeEventListener("click",d)}}:l}function xf(o={}){return Xa("tags","#",Ga,"mbe-tag","data-mbe-tag",o)}function wf(o={}){return Xa("mentions","@",Za,"mbe-mention","data-mbe-mention",o)}function Sf(o){return{name:o.name,suggestion:o}}function Tf(o){let e=n=>{let i=o.accept;return i?typeof i=="function"?i(n):i.some(r=>r.endsWith("/*")?n.type.startsWith(r.slice(0,-1)):n.type===r):!0},t=n=>Array.from(n?.files??[]).filter(e);return{name:"fileHandler",handlePaste:({editor:n,event:i})=>{if(!o.onPaste||i.clipboardData?.getData("text/plain"))return!1;let r=t(i.clipboardData);return r.length===0?!1:(o.onPaste({editor:n,files:r,event:i}),!0)},handleDrop:({editor:n,event:i,position:r})=>{if(!o.onDrop)return!1;let s=t(i.dataTransfer);return s.length===0?!1:(o.onDrop({editor:n,files:s,event:i,position:r}),!0)}}}export{Ke as ATOM_CHAR,Ve as CALLOUT_TYPES,Mi as DEFAULT_LABELS,Fr as Editor,et as MARK_ORDER,tt as MAX_INDENT,Pa as SLASH_ITEM_IDS,Xl as TOOLBAR_ITEMS,L as blockIndex,$ as caret,te as clampSelection,Ee as codeBlock,wi as commands,oc as comparePositions,ce as concatInline,F as containerContent,ee as createId,Hn as createState,rr as defaultKeymap,Ln as defaultResolveUrl,st as deleteInline,St as details,Tt as detailsEnd,it as divider,dn as docToHtml,Qr as emptyTable,P as endOf,Tf as fileHandler,uf as footnotes,Gt as formatAt,N as getBlock,nt as heading,Dn as htmlBlock,no as htmlToDoc,Y as imageBlockOffset,B as inlineLength,Mf as inlineLines,ro as inlineNodesText,I as inlineText,Gr as insertInline,R as isCollapsed,jr as isImageBlock,Q as isPlainTextBlock,Wt as isQuotable,q as isTextBlock,Z as isTextless,Re as keepQuoteStart,pt as lineTable,Xt as linkAt,Ue as listItem,Le as loadMarkdown,nc as marksAt,qu as matchToken,lf as math,wf as mentions,$a as nodeChildren,G as normalizeInline,Me as orderedRange,O as paragraph,he as parseAst,mn as parseInline,bt as parseMarkdown,ft as pieceEnd,ge as plain,Ne as quote,M as quoteKind,jo as rangeHasMark,ot as rawBlock,de as sameCell,ae as sameLink,jt as sameMarks,be as samePosition,oe as sameQuote,Za as scanMentions,Ga as scanTags,Xd as serializeInline,Pt as serializeMarkdown,Go as setLinkInRange,Zt as setMarkInRange,Zr as setTokenInRange,C as sliceInline,rt as sortMarks,Jh as sourceRange,V as startOf,Et as startsQuote,Sf as suggestion,zt as table,xf as tags,hf as textDecorator,Ct as toPlain,Xr as tokensAt,Mt as unquote,Yh as visit,ze as withContainerContent,z as withContent,U as withQuote,Qt as withQuoteStart,On as withoutQuoteStart};
|
|
127
127
|
/*! Icons from Lucide (lucide-static 1.55.0), https://lucide.dev
|
|
128
128
|
* ISC License. Copyright (c) 2026 Lucide Icons and Contributors.
|
|
129
129
|
* Permission to use, copy, modify, and/or distribute this software for any
|