@ashwin_droid/notion-stream 1.1.2 → 1.2.0
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 +29 -29
- package/dist/index.mjs +2446 -2331
- package/dist/types/index.d.ts +6 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react/jsx-runtime"),x=require("react"),Xe=require("streamdown"),Lt=require("@streamdown/code"),Ze=require("@streamdown/math"),Dt=require("@streamdown/mermaid"),hn=require("@radix-ui/react-scroll-area");function $t(e){const n=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const t in e)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(n,t,r.get?r:{enumerable:!0,get:()=>e[t]})}}return n.default=e,Object.freeze(n)}const It=$t(x),We=$t(hn),pn="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let Ie=(e=21)=>{let n="",t=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)n+=pn[t[e]&63];return n};function ct(e){const{bold:n,italic:t,strike:r}=e;return n&&t&&r?"boldItalicStrikethrough":n&&t?"boldItalic":n&&r?"boldStrikethrough":t&&r?"italicStrikethrough":n?"bold":t?"italic":r?"strikethrough":"plain"}function Ot(e){switch(e){case"bold":return{bold:!0,italic:!1,strike:!1};case"italic":return{bold:!1,italic:!0,strike:!1};case"strikethrough":return{bold:!1,italic:!1,strike:!0};case"boldItalic":return{bold:!0,italic:!0,strike:!1};case"boldStrikethrough":return{bold:!0,italic:!1,strike:!0};case"italicStrikethrough":return{bold:!1,italic:!0,strike:!0};case"boldItalicStrikethrough":return{bold:!0,italic:!0,strike:!0};default:return{bold:!1,italic:!1,strike:!1}}}function Oe(e){const n=[];for(const t of e){if(!t.text)continue;const r=t.style!=="plain"&&/^\s*$/.test(t.text)?{...t,style:"plain",href:void 0}:t,c=n[n.length-1];if(c&&c.style===r.style&&(c.style!=="link"||c.href===r.href)){c.text+=r.text;continue}n.push({...r})}return n}function _t(e,n){let t=0;for(let r=n-1;r>=0&&e[r]==="\\";r--)t++;return t%2===1}function ot(e,n,t){let r=t;for(;r<=e.length-n.length;){const c=e.indexOf(n,r);if(c===-1)return-1;if(!_t(e,c))return c;r=c+1}return-1}function mn(e,n,t){let r=t;for(;r<e.length&&e[r]===n;)r++;return r-t}function qt(e,n){const t=[];let r=0,c="";const o=()=>{c&&(t.push({text:c,style:ct(n)}),c="")};for(;r<e.length;){if(e[r]==="\\"&&r+1<e.length){c+=e[r+1],r+=2;continue}if(e[r]==="["){const i=ot(e,"]",r+1);if(i!==-1&&e[i+1]==="("){const h=ot(e,")",i+2);if(h!==-1){o();const f=e.slice(r+1,i),m=e.slice(i+2,h);t.push({text:f,style:"link",href:m}),r=h+1;continue}}}if(e[r]==="`"){const i=mn(e,"`",r),h="`".repeat(i),f=e.indexOf(h,r+i);if(f!==-1){o();let m=e.slice(r+i,f);m=m.replace(/\r?\n/g," "),/[^ ]/.test(m)&&m.startsWith(" ")&&m.endsWith(" ")&&(m=m.slice(1,-1)),t.push({text:m,style:"code"}),r=f+i;continue}c+=h,r+=i;continue}if(e[r]==="$"&&e[r+1]!=="$"){const i=(()=>{for(let h=r+1;h<e.length;h++)if(e[h]==="$"&&!_t(e,h)&&e[h-1]!=="$")return h;return-1})();if(i!==-1){o(),t.push({text:e.slice(r+1,i),style:"math"}),r=i+1;continue}}const a=[{marker:"***",apply:i=>({...i,bold:!0,italic:!0})},{marker:"**",apply:i=>({...i,bold:!0})},{marker:"*",apply:i=>({...i,italic:!0})},{marker:"~~",apply:i=>({...i,strike:!0})}];let g=!1;for(const{marker:i,apply:h}of a)if(e.startsWith(i,r)){const f=ot(e,i,r+i.length);if(f!==-1){o();const m=e.slice(r+i.length,f);t.push(...qt(m,h(n))),r=f+i.length,g=!0}break}g||(c+=e[r],r++)}return o(),t}function Ae(e){return Oe(qt(e,{bold:!1,italic:!1,strike:!1}))}function it(e){return e.map(n=>n.text).join("")}function gn(e,n){if(e==="code"||e==="link"||e==="math")return e;const t=Ot(e),r={...t};return n==="bold"&&(r.bold=!t.bold),n==="italic"&&(r.italic=!t.italic),n==="strikethrough"&&(r.strike=!t.strike),ct(r)}function xn(e){return e==="plain"?{open:"",close:""}:e==="italic"?{open:"*",close:"*"}:e==="bold"?{open:"**",close:"**"}:e==="strikethrough"?{open:"~~",close:"~~"}:e==="boldItalic"?{open:"***",close:"***"}:e==="boldStrikethrough"?{open:"~~**",close:"**~~"}:e==="italicStrikethrough"?{open:"~~*",close:"*~~"}:e==="boldItalicStrikethrough"?{open:"~~***",close:"***~~"}:{open:"",close:""}}function xt(e){return e.replace(/\\/g,"\\\\").replace(/\n/g,"\\n")}function yn(e){let n=0,t=0;for(let r=0;r<e.length;r++)e[r]==="`"?(t++,n=Math.max(n,t)):t=0;return"`".repeat(Math.max(1,n+1))}function Me(e,n={}){const{forParsing:t=!1}=n;return e.map(r=>{if(r.style==="code"){const g=yn(r.text);return g+r.text+g}if(r.style==="math")return`$${t?r.text:r.text.replace(/\$/g,"\\$")}$`;if(r.style==="link"){const g=t?r.text:xt(r.text),i=r.href||"";return`[${g}](${i})`}const{open:c,close:o}=xn(r.style),a=t?r.text:xt(r.text);return c+a+o}).join("")}function dt(e){if(!e.length)return null;let n=0;return e.map((t,r)=>{const c=n,o=t.text.length;n+=o;const a=`${r}-${t.style}`,g=c,i=o,h=t.style;switch(t.style){case"bold":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("strong",{className:"font-semibold",children:t.text})},a);case"italic":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("em",{className:"italic",children:t.text})},a);case"strikethrough":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("s",{className:"line-through",children:t.text})},a);case"boldItalic":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("strong",{className:"font-semibold",children:s.jsx("em",{className:"italic",children:t.text})})},a);case"boldStrikethrough":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("strong",{className:"font-semibold",children:s.jsx("s",{className:"line-through",children:t.text})})},a);case"italicStrikethrough":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("em",{className:"italic",children:s.jsx("s",{className:"line-through",children:t.text})})},a);case"boldItalicStrikethrough":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("strong",{className:"font-semibold",children:s.jsx("em",{className:"italic",children:s.jsx("s",{className:"line-through",children:t.text})})})},a);case"code":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("code",{className:"not-prose px-1.5 py-0.5 bg-notion-code-bg text-red-600 rounded text-sm font-mono",children:t.text})},a);case"link":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("a",{href:t.href,className:"text-blue-600 underline hover:text-blue-800",target:"_blank",rel:"noopener noreferrer",children:t.text})},a);case"math":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,"data-math":"1",className:"inline-block align-middle [&_.katex-display]:!m-0 [&_.katex-display]:!inline",children:s.jsx(Xe.Streamdown,{plugins:{math:Ze.math},isAnimating:!1,children:`$$${t.text}$$`})},a);default:return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:t.text},a)}})}function yt(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function Ke(e,n){return e.map(t=>{const r=yt(t.text);switch(t.style){case"bold":return`<strong class="font-semibold">${r}</strong>`;case"italic":return`<em class="italic">${r}</em>`;case"strikethrough":return`<s class="line-through">${r}</s>`;case"boldItalic":return`<strong class="font-semibold"><em class="italic">${r}</em></strong>`;case"boldStrikethrough":return`<strong class="font-semibold"><s class="line-through">${r}</s></strong>`;case"italicStrikethrough":return`<em class="italic"><s class="line-through">${r}</s></em>`;case"boldItalicStrikethrough":return`<strong class="font-semibold"><em class="italic"><s class="line-through">${r}</s></em></strong>`;case"code":return`<code class="not-prose px-1.5 py-0.5 bg-notion-code-bg text-red-600 rounded text-sm font-mono">${r}</code>`;case"link":return`<a href="${yt(t.href||"")}" class="text-blue-600 underline hover:text-blue-800" target="_blank" rel="noopener noreferrer">${r}</a>`;case"math":return`<span data-math="1" class="font-mono text-notion-text bg-notion-code-bg px-1 rounded border border-notion-border"><span data-math-delim="1" class="text-notion-text-secondary select-none" contenteditable="false">$</span>${r}<span data-math-delim="1" class="text-notion-text-secondary select-none" contenteditable="false">$</span></span>`;default:return r}}).join("")}function bt(e,n){return[{...e,text:e.text.slice(0,n)},{...e,text:e.text.slice(n)}]}function Wt(e,n){return n==="bold"?e==="bold"||e==="boldItalic"||e==="boldStrikethrough"||e==="boldItalicStrikethrough":n==="italic"?e==="italic"||e==="boldItalic"||e==="italicStrikethrough"||e==="boldItalicStrikethrough":n==="strikethrough"?e==="strikethrough"||e==="boldStrikethrough"||e==="italicStrikethrough"||e==="boldItalicStrikethrough":n==="code"?e==="code":n==="link"?e==="link":n==="math"?e==="math":!1}function Ft(e,n,t){const r=[];let c=0;for(const o of e){const a=c,g=c+o.text.length;g>n&&a<t&&r.push(o),c=g}return r}function bn(e,n,t,r){const c=Ft(e,n,t);return c.length===0?!1:c.every(o=>Wt(o.style,r))}function wn(e,n,t,r){return Ft(e,n,t).some(o=>Wt(o.style,r))}function Qe(e,n,t,r,c={}){const o=Math.max(0,Math.min(n,t)),a=Math.max(0,Math.max(n,t));if(o===a)return e;const g=r==="math"?wn(e,o,a,r):bn(e,o,a,r);if(r==="math"&&g){const f=[];let m=0;for(const d of e){const v=m,T=m+d.text.length;T>o&&v<a&&d.style==="math"?f.push({...d,style:"plain"}):f.push({...d}),m=T}return Oe(f)}const i=[];let h=0;for(const f of e){const m=h,d=h+f.text.length;if(d<=o||m>=a){i.push({...f}),h=d;continue}const v=Math.max(0,o-m),T=Math.min(f.text.length,a-m);if(v>0){const[N,E]=bt(f,v);i.push(N),f.text=E.text}if(T-v<f.text.length){const[N,E]=bt(f,T-v),te=wt(N,r,g,c);i.push(te),i.push(E)}else{const N=wt(f,r,g,c);i.push(N)}h=d}return Oe(i)}function wt(e,n,t,r){return n==="bold"||n==="italic"||n==="strikethrough"?{...e,style:kn(e.style,n,!t)}:n==="code"?t?{...e,style:"plain"}:{...e,style:"code",href:void 0}:n==="link"?t?{...e,style:"plain",href:void 0}:{...e,style:"link",href:r.href||""}:n==="math"?t?{...e,style:"plain"}:{...e,style:"math"}:e}function kn(e,n,t){if(e==="code"||e==="link"||e==="math")return e;const r=Ot(e);return n==="bold"&&(r.bold=t),n==="italic"&&(r.italic=t),n==="strikethrough"&&(r.strike=t),ct(r)}const vn=()=>({inCodeBlock:!1,codeBlockLanguage:"",codeBlockContent:"",inMathBlock:!1,mathBlockContent:"",inMermaidBlock:!1,mermaidContent:""}),jn=/^```(\w*)?$/,Nn=/^```$/,Sn=/^\$\$$/,Cn=/^\$\$$/,Mn=/^(#{1,6})\s+(.*)$/,Tn=/^(\s*)[-*]\s+(.*)$/,En=/^(\s*)(\d+)\.\s+(.*)$/,Bn=/^>\s?(.*)$/,An=/^(?:---|\*\*\*|___)$/,Rn=/^!\[([^\]]*)\]\(([^)]+)\)$/;function Ln(e){const n=e.trim();return n?/^:?-{3,}:?$/.test(n):!1}function Dn(e){const n=e.trim(),t=n.startsWith(":"),r=n.endsWith(":");return t&&r?"center":r?"right":"left"}function at(e){const n=[];let t="",r=!1;for(let c=0;c<e.length;c++){const o=e[c];if(r){t+=o,r=!1;continue}if(o==="\\"){r=!0,t+=o;continue}if(o==="|"){n.push(t),t="";continue}t+=o}return n.push(t),e.trimStart().startsWith("|")&&n.shift(),e.trimEnd().endsWith("|")&&n.pop(),n.map(c=>c.trim())}function kt(e){return e.replace(/<br\s*\/?>/gi,`
|
|
2
|
-
`).replace(/\\\|/g,"|")}function
|
|
3
|
-
`),
|
|
4
|
-
`:"")+
|
|
5
|
-
`:"")+
|
|
6
|
-
`),{isStreaming:!0})),h=
|
|
7
|
-
`+
|
|
8
|
-
`+i}return"```"+(
|
|
9
|
-
`+
|
|
10
|
-
`+
|
|
11
|
-
`+i}return"```mermaid\n"+
|
|
12
|
-
`+
|
|
13
|
-
$$`;case"image":return r(),"}case"list":{const i=
|
|
1
|
+
"use strict";var yn=Object.create;var vt=Object.defineProperty;var bn=Object.getOwnPropertyDescriptor;var wn=Object.getOwnPropertyNames;var kn=Object.getPrototypeOf,vn=Object.prototype.hasOwnProperty;var jn=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let l of wn(t))!vn.call(e,l)&&l!==n&&vt(e,l,{get:()=>t[l],enumerable:!(r=bn(t,l))||r.enumerable});return e};var Fe=(e,t,n)=>(n=e!=null?yn(kn(e)):{},jn(t||!e||!e.__esModule?vt(n,"default",{value:e,enumerable:!0}):n,e));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react/jsx-runtime"),x=require("react"),Ge=require("streamdown"),Nn=require("@radix-ui/react-scroll-area");function Pt(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const Wt=Pt(x),He=Pt(Nn),Sn="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let $e=(e=21)=>{let t="",n=crypto.getRandomValues(new Uint8Array(e|=0));for(;e--;)t+=Sn[n[e]&63];return t};function Cn({content:e}){const[t,n]=x.useState(null);return x.useEffect(()=>{let r=!1;return import("@streamdown/math").then(l=>{r||n(l.math)}),()=>{r=!0}},[]),t?s.jsx(Ge.Streamdown,{plugins:{math:t},isAnimating:!1,children:`$$${e}$$`}):s.jsx("span",{className:"text-notion-text-secondary",children:e})}function pt(e){const{bold:t,italic:n,strike:r}=e;return t&&n&&r?"boldItalicStrikethrough":t&&n?"boldItalic":t&&r?"boldStrikethrough":n&&r?"italicStrikethrough":t?"bold":n?"italic":r?"strikethrough":"plain"}function qt(e){switch(e){case"bold":return{bold:!0,italic:!1,strike:!1};case"italic":return{bold:!1,italic:!0,strike:!1};case"strikethrough":return{bold:!1,italic:!1,strike:!0};case"boldItalic":return{bold:!0,italic:!0,strike:!1};case"boldStrikethrough":return{bold:!0,italic:!1,strike:!0};case"italicStrikethrough":return{bold:!1,italic:!0,strike:!0};case"boldItalicStrikethrough":return{bold:!0,italic:!0,strike:!0};default:return{bold:!1,italic:!1,strike:!1}}}function Ie(e){const t=[];for(const n of e){if(!n.text)continue;const r=n.style!=="plain"&&/^\s*$/.test(n.text)?{...n,style:"plain",href:void 0}:n,l=t[t.length-1];if(l&&l.style===r.style&&(l.style!=="link"||l.href===r.href)){l.text+=r.text;continue}t.push({...r})}return t}function Ft(e,t){let n=0;for(let r=t-1;r>=0&&e[r]==="\\";r--)n++;return n%2===1}function dt(e,t,n){let r=n;for(;r<=e.length-t.length;){const l=e.indexOf(t,r);if(l===-1)return-1;if(!Ft(e,l))return l;r=l+1}return-1}function Mn(e,t,n){let r=n;for(;r<e.length&&e[r]===t;)r++;return r-n}function Ht(e,t){const n=[];let r=0,l="";const o=()=>{l&&(n.push({text:l,style:pt(t)}),l="")};for(;r<e.length;){if(e[r]==="\\"&&r+1<e.length){l+=e[r+1],r+=2;continue}if(e[r]==="["){const i=dt(e,"]",r+1);if(i!==-1&&e[i+1]==="("){const h=dt(e,")",i+2);if(h!==-1){o();const u=e.slice(r+1,i),m=e.slice(i+2,h);n.push({text:u,style:"link",href:m}),r=h+1;continue}}}if(e[r]==="`"){const i=Mn(e,"`",r),h="`".repeat(i),u=e.indexOf(h,r+i);if(u!==-1){o();let m=e.slice(r+i,u);m=m.replace(/\r?\n/g," "),/[^ ]/.test(m)&&m.startsWith(" ")&&m.endsWith(" ")&&(m=m.slice(1,-1)),n.push({text:m,style:"code"}),r=u+i;continue}l+=h,r+=i;continue}if(e[r]==="$"&&e[r+1]!=="$"){const i=(()=>{for(let h=r+1;h<e.length;h++)if(e[h]==="$"&&!Ft(e,h)&&e[h-1]!=="$")return h;return-1})();if(i!==-1){o(),n.push({text:e.slice(r+1,i),style:"math"}),r=i+1;continue}}const c=[{marker:"***",apply:i=>({...i,bold:!0,italic:!0})},{marker:"**",apply:i=>({...i,bold:!0})},{marker:"*",apply:i=>({...i,italic:!0})},{marker:"~~",apply:i=>({...i,strike:!0})}];let g=!1;for(const{marker:i,apply:h}of c)if(e.startsWith(i,r)){const u=dt(e,i,r+i.length);if(u!==-1){o();const m=e.slice(r+i.length,u);n.push(...Ht(m,h(t))),r=u+i.length,g=!0}break}g||(l+=e[r],r++)}return o(),n}function Re(e){return Ie(Ht(e,{bold:!1,italic:!1,strike:!1}))}function ut(e){return e.map(t=>t.text).join("")}function Tn(e,t){if(e==="code"||e==="link"||e==="math")return e;const n=qt(e),r={...n};return t==="bold"&&(r.bold=!n.bold),t==="italic"&&(r.italic=!n.italic),t==="strikethrough"&&(r.strike=!n.strike),pt(r)}function En(e){return e==="plain"?{open:"",close:""}:e==="italic"?{open:"*",close:"*"}:e==="bold"?{open:"**",close:"**"}:e==="strikethrough"?{open:"~~",close:"~~"}:e==="boldItalic"?{open:"***",close:"***"}:e==="boldStrikethrough"?{open:"~~**",close:"**~~"}:e==="italicStrikethrough"?{open:"~~*",close:"*~~"}:e==="boldItalicStrikethrough"?{open:"~~***",close:"***~~"}:{open:"",close:""}}function jt(e){return e.replace(/\\/g,"\\\\").replace(/\n/g,"\\n")}function Bn(e){let t=0,n=0;for(let r=0;r<e.length;r++)e[r]==="`"?(n++,t=Math.max(t,n)):n=0;return"`".repeat(Math.max(1,t+1))}function je(e,t={}){const{forParsing:n=!1}=t;return e.map(r=>{if(r.style==="code"){const g=Bn(r.text);return g+r.text+g}if(r.style==="math")return`$${n?r.text:r.text.replace(/\$/g,"\\$")}$`;if(r.style==="link"){const g=n?r.text:jt(r.text),i=r.href||"";return`[${g}](${i})`}const{open:l,close:o}=En(r.style),c=n?r.text:jt(r.text);return l+c+o}).join("")}function mt(e){if(!e.length)return null;let t=0;return e.map((n,r)=>{const l=t,o=n.text.length;t+=o;const c=`${r}-${n.style}`,g=l,i=o,h=n.style;switch(n.style){case"bold":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("strong",{className:"font-semibold",children:n.text})},c);case"italic":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("em",{className:"italic",children:n.text})},c);case"strikethrough":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("s",{className:"line-through",children:n.text})},c);case"boldItalic":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("strong",{className:"font-semibold",children:s.jsx("em",{className:"italic",children:n.text})})},c);case"boldStrikethrough":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("strong",{className:"font-semibold",children:s.jsx("s",{className:"line-through",children:n.text})})},c);case"italicStrikethrough":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("em",{className:"italic",children:s.jsx("s",{className:"line-through",children:n.text})})},c);case"boldItalicStrikethrough":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("strong",{className:"font-semibold",children:s.jsx("em",{className:"italic",children:s.jsx("s",{className:"line-through",children:n.text})})})},c);case"code":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("code",{className:"not-prose px-1.5 py-0.5 bg-notion-code-bg text-red-600 rounded text-sm font-mono",children:n.text})},c);case"link":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:s.jsx("a",{href:n.href,className:"text-blue-600 underline hover:text-blue-800",target:"_blank",rel:"noopener noreferrer",children:n.text})},c);case"math":return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,"data-math":"1",className:"inline-block align-middle [&_.katex-display]:!m-0 [&_.katex-display]:!inline",children:s.jsx(Cn,{content:n.text})},c);default:return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:n.text},c)}})}function Nt(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function Qe(e,t){return e.map(n=>{const r=Nt(n.text);switch(n.style){case"bold":return`<strong class="font-semibold">${r}</strong>`;case"italic":return`<em class="italic">${r}</em>`;case"strikethrough":return`<s class="line-through">${r}</s>`;case"boldItalic":return`<strong class="font-semibold"><em class="italic">${r}</em></strong>`;case"boldStrikethrough":return`<strong class="font-semibold"><s class="line-through">${r}</s></strong>`;case"italicStrikethrough":return`<em class="italic"><s class="line-through">${r}</s></em>`;case"boldItalicStrikethrough":return`<strong class="font-semibold"><em class="italic"><s class="line-through">${r}</s></em></strong>`;case"code":return`<code class="not-prose px-1.5 py-0.5 bg-notion-code-bg text-red-600 rounded text-sm font-mono">${r}</code>`;case"link":return`<a href="${Nt(n.href||"")}" class="text-blue-600 underline hover:text-blue-800" target="_blank" rel="noopener noreferrer">${r}</a>`;case"math":return`<span data-math="1" class="font-mono text-notion-text bg-notion-code-bg px-1 rounded border border-notion-border"><span data-math-delim="1" class="text-notion-text-secondary select-none" contenteditable="false">$</span>${r}<span data-math-delim="1" class="text-notion-text-secondary select-none" contenteditable="false">$</span></span>`;default:return r}}).join("")}function St(e,t){return[{...e,text:e.text.slice(0,t)},{...e,text:e.text.slice(t)}]}function Kt(e,t){return t==="bold"?e==="bold"||e==="boldItalic"||e==="boldStrikethrough"||e==="boldItalicStrikethrough":t==="italic"?e==="italic"||e==="boldItalic"||e==="italicStrikethrough"||e==="boldItalicStrikethrough":t==="strikethrough"?e==="strikethrough"||e==="boldStrikethrough"||e==="italicStrikethrough"||e==="boldItalicStrikethrough":t==="code"?e==="code":t==="link"?e==="link":t==="math"?e==="math":!1}function Xt(e,t,n){const r=[];let l=0;for(const o of e){const c=l,g=l+o.text.length;g>t&&c<n&&r.push(o),l=g}return r}function An(e,t,n,r){const l=Xt(e,t,n);return l.length===0?!1:l.every(o=>Kt(o.style,r))}function Rn(e,t,n,r){return Xt(e,t,n).some(o=>Kt(o.style,r))}function tt(e,t,n,r,l={}){const o=Math.max(0,Math.min(t,n)),c=Math.max(0,Math.max(t,n));if(o===c)return e;const g=r==="math"?Rn(e,o,c,r):An(e,o,c,r);if(r==="math"&&g){const u=[];let m=0;for(const f of e){const M=m,T=m+f.text.length;T>o&&M<c&&f.style==="math"?u.push({...f,style:"plain"}):u.push({...f}),m=T}return Ie(u)}const i=[];let h=0;for(const u of e){const m=h,f=h+u.text.length;if(f<=o||m>=c){i.push({...u}),h=f;continue}const M=Math.max(0,o-m),T=Math.min(u.text.length,c-m);if(M>0){const[v,E]=St(u,M);i.push(v),u.text=E.text}if(T-M<u.text.length){const[v,E]=St(u,T-M),U=Ct(v,r,g,l);i.push(U),i.push(E)}else{const v=Ct(u,r,g,l);i.push(v)}h=f}return Ie(i)}function Ct(e,t,n,r){return t==="bold"||t==="italic"||t==="strikethrough"?{...e,style:Ln(e.style,t,!n)}:t==="code"?n?{...e,style:"plain"}:{...e,style:"code",href:void 0}:t==="link"?n?{...e,style:"plain",href:void 0}:{...e,style:"link",href:r.href||""}:t==="math"?n?{...e,style:"plain"}:{...e,style:"math"}:e}function Ln(e,t,n){if(e==="code"||e==="link"||e==="math")return e;const r=qt(e);return t==="bold"&&(r.bold=n),t==="italic"&&(r.italic=n),t==="strikethrough"&&(r.strike=n),pt(r)}const Dn=()=>({inCodeBlock:!1,codeBlockLanguage:"",codeBlockContent:"",inMathBlock:!1,mathBlockContent:"",inMermaidBlock:!1,mermaidContent:""}),$n=/^```(\w*)?$/,In=/^```$/,On=/^\$\$$/,_n=/^\$\$$/,Pn=/^(#{1,6})\s+(.*)$/,Wn=/^(\s*)[-*]\s+(.*)$/,qn=/^(\s*)(\d+)\.\s+(.*)$/,Fn=/^>\s?(.*)$/,Hn=/^(?:---|\*\*\*|___)$/,Kn=/^!\[([^\]]*)\]\(([^)]+)\)$/;function Xn(e){const t=e.trim();return t?/^:?-{3,}:?$/.test(t):!1}function Vn(e){const t=e.trim(),n=t.startsWith(":"),r=t.endsWith(":");return n&&r?"center":r?"right":"left"}function ft(e){const t=[];let n="",r=!1;for(let l=0;l<e.length;l++){const o=e[l];if(r){n+=o,r=!1;continue}if(o==="\\"){r=!0,n+=o;continue}if(o==="|"){t.push(n),n="";continue}n+=o}return t.push(n),e.trimStart().startsWith("|")&&t.shift(),e.trimEnd().endsWith("|")&&t.pop(),t.map(l=>l.trim())}function Mt(e){return e.replace(/<br\s*\/?>/gi,`
|
|
2
|
+
`).replace(/\\\|/g,"|")}function zn(e){return e.replace(/\n/g,"<br>").replace(/\|/g,"\\|")}function Un(e){const t=je(e,{forParsing:!0});return zn(t)}function Tt(e){let t=0,n=0;for(let r=0;r<e.length;r++)e[r]==="`"?(n++,n>t&&(t=n)):n=0;return"`".repeat(Math.max(3,t+1))}function Qn(e){if(Hn.test(e))return{type:"divider",content:"",isComplete:!0};const t=e.match(Pn);if(t)return{type:"heading",content:t[2],level:t[1].length,isComplete:!0};const n=e.match(Kn);if(n)return{type:"image",content:n[2],alt:n[1]||"",isComplete:!0};const r=e.match(Wn);if(r)return{type:"list",content:r[2],listType:"unordered",depth:r[1].replace(/\t/g," ").length,isComplete:!0};const l=e.match(qn);if(l)return{type:"list",content:l[3],listType:"ordered",depth:l[1].replace(/\t/g," ").length,isComplete:!0};const o=e.match(Fn);return o?{type:"blockquote",content:o[1],isComplete:!0}:null}function Oe(e,t=!1,n=[]){const r=e.split(`
|
|
3
|
+
`),l=[],o=Dn();let c=[],g=0;const i=(h,u,m={})=>{const f=n[g],M=f&&f.type===h?f.id:$e(10);if(g++,h==="paragraph"||h==="heading"||h==="list"||h==="blockquote")return{id:M,type:h,spans:Re(u),level:m.level,listType:m.listType,depth:m.depth,isStreaming:m.isStreaming??!1,metadata:{dirty:!1}};if(h==="divider")return{id:M,type:h,isStreaming:m.isStreaming??!1,metadata:{dirty:!1}};if(h==="table"){const v=m.table;return{id:M,type:"table",header:(v?.header||[]).map(E=>E),rows:(v?.rows||[]).map(E=>E.map(U=>U)),align:v?.align,isStreaming:m.isStreaming??!1,metadata:{dirty:!1}}}const T={id:M,type:h,content:u,level:m.level,language:m.language,listType:m.listType,isStreaming:m.isStreaming??!1,metadata:{dirty:!1}};return h==="image"&&typeof m.alt=="string"&&(T.alt=m.alt),T};for(let h=0;h<r.length;h++){const u=r[h],m=h===r.length-1;if(o.inCodeBlock){In.test(u)&&o.codeBlockContent.length>0?(o.codeBlockLanguage.toLowerCase()==="mermaid"?l.push(i("mermaid",o.codeBlockContent.trimEnd(),{isStreaming:!1})):l.push(i("code",o.codeBlockContent.trimEnd(),{language:o.codeBlockLanguage||"text",isStreaming:!1})),o.inCodeBlock=!1,o.codeBlockLanguage="",o.codeBlockContent=""):(o.codeBlockContent+=(o.codeBlockContent?`
|
|
4
|
+
`:"")+u,t&&m&&(o.codeBlockLanguage.toLowerCase()==="mermaid"?l.push(i("mermaid",o.codeBlockContent,{isStreaming:!0})):l.push(i("code",o.codeBlockContent,{language:o.codeBlockLanguage||"text",isStreaming:!0}))));continue}if(o.inMathBlock){_n.test(u)&&o.mathBlockContent.length>0?(l.push(i("math",o.mathBlockContent.trimEnd(),{isStreaming:!1})),o.inMathBlock=!1,o.mathBlockContent=""):(o.mathBlockContent+=(o.mathBlockContent?`
|
|
5
|
+
`:"")+u,t&&m&&l.push(i("math",o.mathBlockContent,{isStreaming:!0})));continue}const f=u.match($n);if(f){o.inCodeBlock=!0,o.codeBlockLanguage=f[1]||"",o.codeBlockContent="";continue}if(On.test(u)){o.inMathBlock=!0,o.mathBlockContent="";continue}if(u.trim()===""){c=[];continue}const T=u.trim().startsWith("|");if(h+1<r.length){const E=ft(u),U=ft(r[h+1]),G=E.length>=1&&E.some(le=>le.length>0),ae=U.length===E.length&&U.length>=1&&U.every(le=>Xn(le));if(G&&ae){c=[];const le=U.map(Vn),b=E.map(Q=>Re(Mt(Q))),w=[];let B=h+2;for(;B<r.length;B++){const Q=r[B];if(Q.trim()===""||!Q.includes("|"))break;const O=ft(Q);if(O.length!==E.length)break;w.push(O.map(ne=>Re(Mt(ne))))}const P=t;l.push(i("table","",{table:{header:b,rows:w,align:le},isStreaming:P})),h=B-1;continue}if(t&&T&&G){const b=r[h+1].trim(),w=b.startsWith("|");if(w&&(b.includes("-")||b==="|"||h+1===r.length-1)||w&&h+1>=r.length-2){c=[];let P=h;const Q=[];for(;P<r.length;P++){const O=r[P].trim();if(O===""&&P>h+1||!O.startsWith("|")&&O!=="")break;Q.push(r[P])}l.push(i("paragraph",Q.join(`
|
|
6
|
+
`),{isStreaming:!0})),h=P-1;continue}}}if(t&&m&&T){c=[],l.push(i("paragraph",u,{isStreaming:!0}));continue}const v=Qn(u);if(v){if(v.type==="list"){const E=v.depth??0;if(c.length===0)c=[E],v.depth=0;else{let U=-1;for(let G=c.length-1;G>=0;G--)if(c[G]<=E){U=G;break}U===-1?(c=[E],v.depth=0):c[U]===E?(c=c.slice(0,U+1),v.depth=U):(c=c.slice(0,U+1),c.push(E),v.depth=U+1)}}else c=[];l.push(i(v.type,v.content,{level:v.level,listType:v.listType,depth:v.depth,alt:v.alt,isStreaming:t&&m}))}else c=[],l.push(i("paragraph",u,{isStreaming:t&&m}))}return l}function me(e){const t=[];let n=[];const r=()=>{n=[]},l=c=>{const g=!!c.metadata?.dirty;switch(c.type){case"heading":return r(),"#".repeat(c.level||1)+" "+je(c.spans);case"code":if(r(),g){const i=Tt(c.content),h=c.language||"";return i+h+`
|
|
7
|
+
`+c.content+`
|
|
8
|
+
`+i}return"```"+(c.language||"")+`
|
|
9
|
+
`+c.content+"\n```";case"mermaid":if(r(),g){const i=Tt(c.content);return i+`mermaid
|
|
10
|
+
`+c.content+`
|
|
11
|
+
`+i}return"```mermaid\n"+c.content+"\n```";case"math":return r(),`$$
|
|
12
|
+
`+c.content+`
|
|
13
|
+
$$`;case"image":return r(),"";case"table":{r();const i=c.header||[],h=c.rows||[],u=Math.max(i.length,...h.map(E=>E.length)),m=(c.align||[]).slice(0,u),f=E=>E?Un(E):"",M="| "+Array.from({length:u},(E,U)=>f(i[U])).join(" | ")+" |",T="| "+Array.from({length:u},(E,U)=>{const G=m[U]||"left";return G==="center"?":---:":G==="right"?"---:":"---"}).join(" | ")+" |",v=h.map(E=>"| "+Array.from({length:u},(U,G)=>f(E[G])).join(" | ")+" |");return[M,T,...v].join(`
|
|
14
|
+
`)}case"list":{const i=c.depth||0,h=" ".repeat(i),u=je(c.spans);if(c.listType==="ordered"){for(;n.length<=i;)n.push(0);n[i]=(n[i]||0)+1;for(let m=i+1;m<n.length;m++)n[m]=0;return`${h}${n[i]}. ${u}`}return`${h}- ${u}`}case"blockquote":return r(),`> ${je(c.spans)}`;case"divider":return r(),"---";case"paragraph":return r(),je(c.spans);default:return r(),c.content??""}};let o=null;for(const c of e){const g=l(c);if(t.length>0){const i=o==="list"&&c.type==="list"?`
|
|
15
15
|
`:`
|
|
16
16
|
|
|
17
|
-
`;n.push(i)}n.push(g),o=a.type}return n.join("")}function Pt({markdown:e,isStreaming:n}){const t=x.useRef([]);return{blocks:x.useMemo(()=>{const c=_e(e,n,t.current);return t.current=c,c},[e,n])}}const _n={"# ":1,"## ":2,"### ":3,"#### ":4,"##### ":5,"###### ":6},qn={"- ":"unordered","* ":"unordered","1. ":"ordered"},jt={"\\* ":"* ","\\- ":"- ","\\> ":"> "};function Ht({onBlockTypeChange:e}={}){const n=x.useCallback(c=>{for(const[o,a]of Object.entries(jt))if(c===o)return a;return null},[]),t=x.useCallback(c=>{for(const a of Object.keys(jt))if(c===a)return null;for(const[a,g]of Object.entries(_n))if(c.startsWith(a))return{newType:"heading",newContent:c.slice(a.length),level:g};for(const[a,g]of Object.entries(qn))if(c.startsWith(a))return{newType:"list",newContent:c.slice(a.length),listType:g};if(c.startsWith("> "))return{newType:"blockquote",newContent:c.slice(2)};if(c==="---"||c==="***"||c==="___")return{newType:"divider",newContent:""};const o=c.match(/^```([\w-]*)\s$/);return o?{newType:"code",newContent:"",language:o[1]||"text"}:null},[]),r=x.useCallback((c,o,a,g)=>{if(c.key===" "){const i=a.slice(0,g)+" ",h=a.slice(g);if(n(i)&&o.type==="paragraph")return!1;const m=t(i);if(m&&o.type==="paragraph"){c.preventDefault();const d=m.newContent+h;return e?.(o.id,m.newType,d,{level:m.level,listType:m.listType,language:m.language}),!0}if(m&&m.newType==="list"&&o.type==="list"&&m.listType!==o.listType){c.preventDefault();const d=m.newContent+h;return e?.(o.id,"list",d,{listType:m.listType,depth:o.depth??0}),!0}}if(c.key==="Enter"){const i=a.slice(0,g)+" ",h=t(i);if(h?.newType==="code"&&o.type==="paragraph"){c.preventDefault();const f=a.slice(g);return e?.(o.id,h.newType,f,{language:h.language}),!0}}return c.key==="Backspace"&&g===0&&o.type!=="paragraph"&&a===""?(c.preventDefault(),e?.(o.id,"paragraph",""),!0):(c.key==="Enter"&&o.type==="code",!1)},[t,n,e]);return{detectShortcut:t,detectEscape:n,handleKeyDown:r}}function Wn(e){const{bold:n,italic:t,strike:r}=e;return n&&t&&r?"boldItalicStrikethrough":n&&t?"boldItalic":n&&r?"boldStrikethrough":t&&r?"italicStrikethrough":n?"bold":t?"italic":r?"strikethrough":"plain"}function Fn(e){const n={bold:!1,italic:!1,strike:!1};let t,r=!1,c=!1,o=!1,a=e.parentNode;for(;a&&a.nodeType===Node.ELEMENT_NODE;){const g=a,i=g.tagName.toLowerCase();i==="code"&&(r=!0),i==="a"&&(c=!0,t=g.getAttribute("href")||""),(i==="strong"||i==="b")&&(n.bold=!0),(i==="em"||i==="i")&&(n.italic=!0),(i==="s"||i==="del"||i==="strike")&&(n.strike=!0),i==="span"&&g.getAttribute("data-math")==="1"&&(o=!0),a=a.parentNode}return r?{style:"code"}:o?{style:"math"}:c?{style:"link",href:t}:{style:Wn(n)}}function ut(e){let n=e.parentNode;for(;n&&n.nodeType===Node.ELEMENT_NODE;){if(n.getAttribute("data-math-delim")==="1")return!0;n=n.parentNode}return!1}function Kt(e,n){let t=e.nodeType===Node.ELEMENT_NODE?e:e.parentElement;for(;t&&t!==n;){if(t.getAttribute("data-math")==="1")return t;t=t.parentElement}return null}function Pn(e){const n=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null);let t=null,r;for(;r=n.nextNode();)ut(r)||(t=r);return t}function Hn(e,n,t){if(n.nodeType===Node.ELEMENT_NODE){const c=n;if(t>0)return c.childNodes[t-1]||null}if(n.nodeType===Node.TEXT_NODE&&t>0)return null;let r=n;for(;r&&r!==e;){if(r.previousSibling)return r.previousSibling;r=r.parentNode}return null}function Nt(e,n,t){const r=Kt(n,e);if(r){if(n.nodeType===Node.ELEMENT_NODE&&n===r){const o=r.childNodes.length;if(t>=o)return"afterMath"}return"insideMath"}const c=Hn(e,n,t);if(c&&c.nodeType===Node.ELEMENT_NODE&&c.getAttribute("data-math")==="1")return"afterMath"}function qe(e){const n=[],t=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null);let r;for(;r=t.nextNode();){const c=r.textContent||"";if(!c||ut(r))continue;const o=Fn(r);if(o.style==="link"){n.push({text:c,style:"link",href:o.href});continue}if(o.style==="math"){n.push({text:c,style:"math"});continue}n.push({text:c,style:o.style})}return Oe(n)}function ve(e){const n=window.getSelection();if(!n||n.rangeCount===0)return null;const t=n.getRangeAt(0);if(!e.contains(t.startContainer)||!e.contains(t.endContainer))return null;const r=St(e,t.startContainer,t.startOffset),c=St(e,t.endContainer,t.endOffset);return r===null||c===null?null:{start:r,end:c,startAffinity:Nt(e,t.startContainer,t.startOffset),endAffinity:Nt(e,t.endContainer,t.endOffset)}}function St(e,n,t){const r=document.createRange();try{r.selectNodeContents(e),r.setEnd(n,t)}catch{return null}const c=r.cloneContents();return c.querySelectorAll?.('[data-math-delim="1"]').forEach(o=>o.remove()),c.textContent?.length??0}function Be(e,n,t,r={}){const c=Ct(e,n,r.startAffinity),o=Ct(e,t,r.endAffinity);if(!c||!o)return;const a=document.createRange();a.setStart(c.node,c.offset),a.setEnd(o.node,o.offset);const g=window.getSelection();g?.removeAllRanges(),g?.addRange(a)}function Ct(e,n,t){const r=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null);let c=0,o;for(;o=r.nextNode();){if(ut(o))continue;const a=o.textContent?.length||0;if(c+a>n)return{node:o,offset:Math.max(0,n-c)};if(c+a===n){const g=Kt(o,e);if(g){const i=Pn(g);if(i&&i===o&&t==="afterMath"){const h=g.parentNode;if(h){const f=Array.prototype.indexOf.call(h.childNodes,g);if(f!==-1)return{node:h,offset:f+1}}}}return{node:o,offset:a}}c+=a}return{node:e,offset:e.childNodes.length}}const Kn=220,Mt=8;function Xn({containerRef:e,onFormat:n,isMathSelection:t}){const[r,c]=x.useState(!1),[o,a]=x.useState({top:0,left:0}),[g,i]=x.useState(!1),h=x.useRef(null),f=x.useRef(!1),m=x.useCallback(()=>{const v=window.getSelection();if(!v||v.isCollapsed||!v.rangeCount){f.current||(c(!1),i(!1));return}const T=v.getRangeAt(0);if(!e.current?.contains(T.commonAncestorContainer)){c(!1),i(!1);return}i(!!t?.(T));const N=T.getBoundingClientRect(),E=e.current?.getBoundingClientRect();if(!E)return;const te=h.current?.offsetWidth||Kn,se=h.current?.offsetHeight||40,le=N.left+N.width/2-E.left,Y=N.top-E.top,b=N.bottom-E.top;let y=le-te/2;const B=Mt,F=E.width-te-Mt;y=Math.max(B,Math.min(F,y));let X=Y-se-8;X<0&&(X=b+8),a({top:X,left:y}),c(!0)},[e,t]);x.useEffect(()=>{const v=()=>{requestAnimationFrame(m)},T=()=>{r&&requestAnimationFrame(m)};return document.addEventListener("selectionchange",v),window.addEventListener("scroll",T,!0),()=>{document.removeEventListener("selectionchange",v),window.removeEventListener("scroll",T,!0)}},[m,r]),x.useEffect(()=>{const v=T=>{const N=T.target;!N.closest(".floating-toolbar")&&!e.current?.contains(N)&&c(!1)};return document.addEventListener("mousedown",v),()=>{document.removeEventListener("mousedown",v)}},[e]);const d=x.useCallback(v=>{g&&v!=="math"||n(v)},[n,g]);return r?s.jsxs("div",{ref:h,className:"floating-toolbar absolute z-50 flex items-center gap-1 px-2 py-1.5 bg-notion-bg-elevated rounded-lg shadow-lg border border-notion-border whitespace-nowrap",style:{top:o.top,left:o.left},onMouseDown:v=>v.preventDefault(),onMouseEnter:()=>{f.current=!0},onMouseLeave:()=>{f.current=!1},children:[s.jsx(Pe,{onClick:()=>d("bold"),disabled:g,children:s.jsx(Vn,{})}),s.jsx(Pe,{onClick:()=>d("italic"),disabled:g,children:s.jsx(zn,{})}),s.jsx(Pe,{onClick:()=>d("strikethrough"),disabled:g,children:s.jsx(Un,{})}),s.jsx(Pe,{onClick:()=>d("code"),disabled:g,children:s.jsx(Qn,{})}),s.jsx(Pe,{onClick:()=>d("math"),disabled:!1,children:s.jsx(Yn,{})}),s.jsx("div",{className:"w-px h-5 bg-notion-border mx-1"}),s.jsx(Pe,{onClick:()=>d("link"),disabled:g,children:s.jsx(Gn,{})})]}):null}function Pe({children:e,onClick:n,disabled:t}){return s.jsx("button",{type:"button",onClick:n,disabled:t,className:`p-1.5 rounded text-notion-text-secondary transition-colors ${t?"opacity-40 cursor-not-allowed":"hover:bg-notion-bg-hover hover:text-notion-text"}`,children:e})}function Vn(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2.5",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M6 4h8a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"}),s.jsx("path",{d:"M6 12h9a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"})]})}function zn(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("line",{x1:"19",y1:"4",x2:"10",y2:"4"}),s.jsx("line",{x1:"14",y1:"20",x2:"5",y2:"20"}),s.jsx("line",{x1:"15",y1:"4",x2:"9",y2:"20"})]})}function Un(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M16 4H9a3 3 0 0 0-3 3v1a3 3 0 0 0 3 3h6a3 3 0 0 1 3 3v1a3 3 0 0 1-3 3H8"}),s.jsx("line",{x1:"4",y1:"12",x2:"20",y2:"12"})]})}function Qn(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("polyline",{points:"16 18 22 12 16 6"}),s.jsx("polyline",{points:"8 6 2 12 8 18"})]})}function Gn(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"}),s.jsx("path",{d:"M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"})]})}function Yn(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M4 4h6l4 16h6"}),s.jsx("path",{d:"M4 12h16"})]})}function Jn({isOpen:e,onSubmit:n,onCancel:t,initialHref:r=""}){const[c,o]=x.useState(r),a=x.useRef(null),g=x.useRef(null);x.useEffect(()=>{e&&(o(r),requestAnimationFrame(()=>{a.current?.focus(),a.current?.select()}))},[e,r]);const i=x.useCallback(f=>{f.preventDefault();const m=c.trim();if(m){const d=m.match(/^https?:\/\//)?m:`https://${m}`;n(d)}},[c,n]),h=x.useCallback(f=>{f.key==="Escape"&&(f.preventDefault(),f.stopPropagation(),t())},[t]);return e?s.jsxs("div",{className:"link-modal fixed inset-0 z-[100] flex items-center justify-center",onKeyDown:h,children:[s.jsx("div",{className:"absolute inset-0 bg-black/20 backdrop-blur-[1px]",onClick:t}),s.jsxs("div",{ref:g,className:"relative bg-notion-bg-elevated rounded-xl shadow-2xl border border-notion-border/60 w-full max-w-md mx-4 overflow-hidden animate-in fade-in zoom-in-95 duration-150",style:{boxShadow:"0 25px 50px -12px rgba(0, 0, 0, 0.25), 0 0 0 1px rgba(0, 0, 0, 0.05)"},children:[s.jsx("div",{className:"px-5 pt-5 pb-3",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-9 h-9 rounded-lg bg-gradient-to-br from-blue-500 to-blue-600 flex items-center justify-center shadow-sm",children:s.jsx(Zn,{className:"w-4.5 h-4.5 text-white"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-[15px] font-semibold text-notion-text tracking-[-0.01em]",children:"Insert link"}),s.jsx("p",{className:"text-xs text-notion-text-secondary mt-0.5",children:"Enter a URL for the selected text"})]})]})}),s.jsxs("form",{onSubmit:i,className:"px-5 pb-5",children:[s.jsxs("div",{className:"relative",children:[s.jsx("div",{className:"absolute inset-y-0 left-0 pl-3.5 flex items-center pointer-events-none",children:s.jsx(er,{className:"w-4 h-4 text-notion-text-secondary"})}),s.jsx("input",{ref:a,type:"text",value:c,onChange:f=>o(f.target.value),placeholder:"https://example.com",className:"w-full pl-10 pr-4 py-2.5 text-sm bg-notion-hover/50 border border-notion-border/60 rounded-lg text-notion-text placeholder:text-notion-text-secondary/60 focus:outline-none focus:ring-2 focus:ring-blue-500/30 focus:border-blue-500/50 transition-all duration-150",autoComplete:"url",spellCheck:!1})]}),s.jsxs("div",{className:"flex items-center justify-end gap-2 mt-4",children:[s.jsx("button",{type:"button",onClick:t,className:"px-3.5 py-1.5 text-sm font-medium text-notion-text-secondary hover:text-notion-text hover:bg-notion-hover rounded-lg transition-colors duration-150",children:"Cancel"}),s.jsx("button",{type:"submit",disabled:!c.trim(),className:"px-4 py-1.5 text-sm font-medium text-white bg-blue-500 hover:bg-blue-600 disabled:opacity-40 disabled:cursor-not-allowed rounded-lg shadow-sm transition-all duration-150 hover:shadow",children:"Apply"})]})]}),s.jsx("div",{className:"px-5 py-2.5 bg-notion-hover/30 border-t border-notion-border/40",children:s.jsxs("div",{className:"flex items-center justify-between text-[11px] text-notion-text-secondary",children:[s.jsxs("span",{className:"flex items-center gap-1.5",children:[s.jsx("kbd",{className:"px-1.5 py-0.5 bg-notion-bg-elevated rounded border border-notion-border/60 font-mono text-[10px] shadow-sm",children:"Enter"}),s.jsx("span",{children:"to apply"})]}),s.jsxs("span",{className:"flex items-center gap-1.5",children:[s.jsx("kbd",{className:"px-1.5 py-0.5 bg-notion-bg-elevated rounded border border-notion-border/60 font-mono text-[10px] shadow-sm",children:"Esc"}),s.jsx("span",{children:"to cancel"})]})]})})]})]}):null}function Zn({className:e}){return s.jsxs("svg",{className:e,width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"}),s.jsx("path",{d:"M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"})]})}function er({className:e}){return s.jsxs("svg",{className:e,width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("circle",{cx:"12",cy:"12",r:"10"}),s.jsx("path",{d:"M2 12h20"}),s.jsx("path",{d:"M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"})]})}const tr=400;function Ge({block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,className:a="",editingClassName:g="",viewClassName:i="",placeholder:h="Type something...",renderWrapper:f,readonly:m=!1}){const d=x.useRef(null),v=x.useRef(null),T=x.useRef(null),N=x.useRef(e.spans),E=x.useRef(null),[te,se]=x.useState(!1),le=x.useRef(null),Y=x.useRef(null),b=x.useCallback(()=>n?d.current:v.current,[n]);x.useEffect(()=>()=>{E.current&&clearTimeout(E.current)},[]),x.useEffect(()=>{if(n&&d.current){d.current.innerHTML=Ke(e.spans),N.current=e.spans,d.current.focus();const O=Y.current;if(Y.current=null,O)Be(d.current,O.start,O.end,{startAffinity:O.startAffinity,endAffinity:O.endAffinity});else{const z=document.createRange(),Q=window.getSelection();z.selectNodeContents(d.current),z.collapse(!1),Q?.removeAllRanges(),Q?.addRange(z)}}},[n]),x.useEffect(()=>{if(!n||!d.current)return;const O=d.current,z=Ke(e.spans),Q=O.innerHTML;if(z===Q)return;const A=ve(O);O.innerHTML=z,A&&Be(O,A.start,A.end,{startAffinity:A.startAffinity,endAffinity:A.endAffinity}),N.current=e.spans},[e.spans,n]);const y=x.useCallback(()=>{if(!d.current)return;const O=ve(d.current),z=qe(d.current);N.current=z,t(z);const A=Array.from(d.current.querySelectorAll('[data-math="1"]')).every(ae=>ae.querySelectorAll('[data-math-delim="1"]').length===2);E.current&&clearTimeout(E.current);const V=()=>{if(!d.current)return;const ae=qe(d.current),ce=Ke(ae),ee=d.current.innerHTML;ce!==ee&&(d.current.innerHTML=ce,O&&Be(d.current,O.start,O.end,{startAffinity:O.startAffinity,endAffinity:O.endAffinity}))};A?E.current=setTimeout(V,tr):V()},[t]),B=x.useCallback(O=>{if(!O.relatedTarget?.closest(".floating-toolbar")&&!O.relatedTarget?.closest(".link-modal")){if(d.current){const z=qe(d.current);t(z)}c()}},[t,c]),F=x.useCallback(()=>{!n&&!m&&r()},[n,r,m]),X=x.useCallback(O=>{if(!v.current)return;const z=v.current,Q=(oe,he)=>{const de=document;if(typeof de.caretRangeFromPoint=="function")return de.caretRangeFromPoint(oe,he)||null;if(typeof de.caretPositionFromPoint=="function"){const _=de.caretPositionFromPoint(oe,he);if(!_)return null;const j=document.createRange();return j.setStart(_.offsetNode,_.offset),j.collapse(!0),j}return null},A=window.getSelection();let V=null;if(A&&A.rangeCount>0&&(V=A.getRangeAt(0)),(!V||!z.contains(V.startContainer)||!z.contains(V.endContainer))&&O){const oe=Q(O.clientX,O.clientY);oe&&(V=oe)}if(!V||!z.contains(V.startContainer)||!z.contains(V.endContainer))return;const ae=oe=>(oe.nodeType===Node.ELEMENT_NODE?oe:oe.parentElement)?.closest("[data-span-start]"),ce=(oe,he,de)=>{if(oe.nodeType!==Node.ELEMENT_NODE)return null;const _=oe,j=_.childNodes[he]||null,S=he>0&&_.childNodes[he-1]||null,J=Se=>Se&&Se.nodeType===Node.ELEMENT_NODE?Se.closest("[data-span-start]"):null;return de==="start"?J(j)||J(S):J(S)||J(j)},ee=(oe,he,de)=>{let _=ae(oe);if(_||(_=ce(oe,he,de)),!_)return null;const j=_.getAttribute("data-span-start"),S=_.getAttribute("data-span-len");if(!j||!S)return null;const J=Number(j),Se=Number(S);if(_.getAttribute("data-span-style")==="math")return{pos:de==="start"?J:J+Se,affinity:"insideMath"};try{const Te=document.createRange();Te.selectNodeContents(_),Te.setEnd(oe,he);const De=Te.toString().length;return{pos:J+De}}catch{return{pos:de==="start"?J:J+Se}}},ye=ae(V.startContainer)||ce(V.startContainer,V.startOffset,"start");if(V.collapsed&&ye?.getAttribute("data-span-style")==="math"){const oe=Number(ye.getAttribute("data-span-start")||"0");Y.current={start:oe,end:oe,startAffinity:"insideMath",endAffinity:"insideMath"};return}const Ne=ee(V.startContainer,V.startOffset,"start"),be=ee(V.endContainer,V.endOffset,"end");!Ne||!be||(Y.current={start:Ne.pos,end:be.pos,startAffinity:Ne.affinity,endAffinity:be.affinity})},[]),Z=x.useCallback((O,z)=>{const Q=b();if(!Q)return;const A=ve(Q);if(!(!A||A.start===A.end))if(n&&d.current){const V=qe(d.current),ae=Qe(V,A.start,A.end,O,z);d.current.innerHTML=Ke(ae),Be(d.current,A.start,A.end,{startAffinity:A.startAffinity,endAffinity:A.endAffinity}),N.current=ae,t(ae)}else{const V=Qe(e.spans,A.start,A.end,O,z);t(V),window.getSelection()?.removeAllRanges()}},[n,e.spans,t,b]),re=x.useCallback(O=>{if((O.key==="ArrowRight"||O.key==="ArrowLeft")&&d.current){const z=d.current,Q=window.getSelection();if(Q&&Q.rangeCount>0){const A=Q.getRangeAt(0);if(A.collapsed&&z.contains(A.startContainer)){const V=_=>{let j=_.nodeType===Node.ELEMENT_NODE?_:_.parentElement;for(;j&&j!==z;){if(j.getAttribute("data-math")==="1")return j;j=j.parentElement}return null},ae=_=>{let j=_.parentNode;for(;j&&j.nodeType===Node.ELEMENT_NODE;){if(j.getAttribute("data-math-delim")==="1")return!0;j=j.parentNode}return!1},ce=_=>{const j=document.createTreeWalker(_,NodeFilter.SHOW_TEXT,null);let S;for(;S=j.nextNode();)if(!ae(S))return S;return null},ee=_=>{const j=document.createTreeWalker(_,NodeFilter.SHOW_TEXT,null);let S=null,J;for(;J=j.nextNode();)ae(J)||(S=J);return S},ye=_=>{const j=_.parentNode;if(!j)return;const S=Array.prototype.indexOf.call(j.childNodes,_);if(S===-1)return;const J=document.createRange();J.setStart(j,S+1),J.collapse(!0),Q.removeAllRanges(),Q.addRange(J)},Ne=_=>{const j=_.parentNode;if(!j)return;const S=Array.prototype.indexOf.call(j.childNodes,_);if(S===-1)return;const J=document.createRange();J.setStart(j,S),J.collapse(!0),Q.removeAllRanges(),Q.addRange(J)},be=_=>{const j=ce(_),S=document.createRange();j?S.setStart(j,0):S.setStart(_,1),S.collapse(!0),Q.removeAllRanges(),Q.addRange(S)},oe=_=>{const j=ee(_),S=document.createRange();j?S.setStart(j,j.textContent?.length??0):S.setStart(_,1),S.collapse(!0),Q.removeAllRanges(),Q.addRange(S)},he=()=>{const _=A.startContainer,j=A.startOffset;if(_.nodeType===Node.ELEMENT_NODE){const J=_;if(j>0)return J.childNodes[j-1]||null}if(_.nodeType===Node.TEXT_NODE&&j>0)return null;let S=_;for(;S&&S!==z;){if(S.previousSibling)return S.previousSibling;S=S.parentNode}return null},de=()=>{const _=A.startContainer,j=A.startOffset;if(_.nodeType===Node.ELEMENT_NODE)return _.childNodes[j]||null;if(_.nodeType===Node.TEXT_NODE){const J=_.textContent||"";if(j<J.length)return null}let S=_;for(;S&&S!==z;){if(S.nextSibling)return S.nextSibling;S=S.parentNode}return null};if(O.key==="ArrowRight"){const _=V(A.startContainer);if(_){const S=ee(_);if(S&&A.startContainer===S&&A.startOffset===(S.textContent?.length??0)){O.preventDefault(),ye(_);return}}const j=de();if(j&&j.nodeType===Node.ELEMENT_NODE){const S=j;if(S.getAttribute("data-math")==="1"){O.preventDefault(),be(S);return}}}if(O.key==="ArrowLeft"){const _=he();if(_&&_.nodeType===Node.ELEMENT_NODE){const S=_;if(S.getAttribute("data-math")==="1"){O.preventDefault(),oe(S);return}}const j=V(A.startContainer);if(j){const S=ce(j);if(S&&A.startContainer===S&&A.startOffset===0){O.preventDefault(),Ne(j);return}}}}}}if((O.metaKey||O.ctrlKey)&&d.current)switch(O.key.toLowerCase()){case"b":O.preventDefault(),Z("bold");return;case"i":O.preventDefault(),Z("italic");return;case"e":O.preventDefault(),Z("code");return;case"m":O.preventDefault(),Z("math");return;case"k":O.preventDefault();const z=ve(d.current);z&&z.start!==z.end&&(le.current=z,se(!0));return}if(O.key==="Escape"){if(d.current){const z=qe(d.current);t(z)}c();return}o(O)},[Z,t,c,o]),pe=x.useCallback(O=>{const z=b();if(z){if(O==="link"){const Q=ve(z);Q&&Q.start!==Q.end&&(le.current=Q,se(!0));return}Z(O)}},[Z,b]),me=x.useCallback(O=>{if(se(!1),!le.current)return;const z=le.current;if(le.current=null,n&&d.current){const Q=qe(d.current),A=Qe(Q,z.start,z.end,"link",{href:O});d.current.innerHTML=Ke(A),Be(d.current,z.start,z.end,{startAffinity:z.startAffinity,endAffinity:z.endAffinity}),N.current=A,t(A),d.current.focus()}else{const Q=Qe(e.spans,z.start,z.end,"link",{href:O});t(Q),window.getSelection()?.removeAllRanges()}},[n,e.spans,t]),xe=x.useCallback(()=>{se(!1),le.current=null,n&&d.current?.focus()},[n]),fe=x.useCallback(O=>{if(n&&d.current){const z=ve(d.current);if(!z||z.start===z.end)return!1;const Q=qe(d.current);let A=0;for(const V of Q){const ae=A,ce=A+V.text.length;if(ce>z.start&&ae<z.end&&V.style==="math")return!0;A=ce}return!1}if(!n&&v.current){const z=v.current,Q=Array.from(z.querySelectorAll('[data-span-style="math"]'));for(const A of Q)try{if(O.intersectsNode(A))return!0}catch{}}return!1},[n]),je=x.useCallback(O=>{const Q=O.nativeEvent.inputType,A=Q==="deleteContentBackward"||Q==="deleteContentForward",V=Q==="insertText"||Q==="insertCompositionText"||Q==="insertFromPaste";if(!A&&!V)return;const ae=d.current;if(!ae)return;const ce=window.getSelection();if(!ce||ce.rangeCount===0)return;const ee=ce.getRangeAt(0),ye=j=>{let S=j.nodeType===Node.ELEMENT_NODE?j:j.parentElement;for(;S&&S!==ae;){if(S.getAttribute("data-math")==="1")return S;S=S.parentElement}return null},Ne=j=>{const S=j.parentNode;if(!S)return;const J=Array.prototype.indexOf.call(S.childNodes,j);if(J===-1)return;const Se=document.createRange();Se.setStart(S,J+1),Se.collapse(!0),ce.removeAllRanges(),ce.addRange(Se)},be=j=>{if(!j)return!1;if(j.nodeType===Node.ELEMENT_NODE){const J=j;return J.getAttribute("data-math-delim")==="1"?!0:!!J.querySelector('[data-math-delim="1"]')}const S=j.parentElement;return!!S&&S.getAttribute("data-math-delim")==="1"},oe=j=>{let S=j;for(;S&&S!==ae;){if(S.previousSibling)return S.previousSibling;S=S.parentNode}return null},he=j=>{let S=j;for(;S&&S!==ae;){if(S.nextSibling)return S.nextSibling;S=S.parentNode}return null},de=(j,S)=>{if(j.nodeType===Node.ELEMENT_NODE){const J=j;return S>0?J.childNodes[S-1]||null:oe(j)}return j.nodeType===Node.TEXT_NODE&&S>0?null:oe(j)},_=(j,S)=>{if(j.nodeType===Node.ELEMENT_NODE)return j.childNodes[S]||he(j);if(j.nodeType===Node.TEXT_NODE){const J=j.textContent||"";return S<J.length?null:he(j)}return he(j)};if(!ee.collapsed){ee.cloneContents().querySelector?.('[data-math-delim="1"]')&&A&&O.preventDefault();return}if(V){const j=ye(ee.startContainer);if(j&&ee.startContainer===j&&ee.startOffset>=j.childNodes.length){Ne(j);return}}if(A){const j=Q==="deleteContentBackward"?de(ee.startContainer,ee.startOffset):_(ee.startContainer,ee.startOffset);be(j)&&O.preventDefault()}},[]),Ve=e.spans.length===0||e.spans.length===1&&e.spans[0].text==="",Re=s.jsx("div",{ref:d,className:`outline-none cursor-text ${a} ${g}`,contentEditable:!0,suppressContentEditableWarning:!0,"data-placeholder":h,"data-empty":Ve?"1":void 0,onMouseDown:O=>{const Q=O.target.closest?.('[data-math-delim="1"]');if(!Q||!d.current)return;const A=Q.closest?.('[data-math="1"]');if(!A)return;O.preventDefault();const V=window.getSelection();if(!V)return;const ae=A.firstElementChild===Q,ce=document.createTreeWalker(A,NodeFilter.SHOW_TEXT,null);let ee=null,ye=null,Ne;for(;Ne=ce.nextNode();)Ne.parentElement?.getAttribute("data-math-delim")!=="1"&&(ee||(ee=Ne),ye=Ne);const be=document.createRange();ae?ee?be.setStart(ee,0):be.setStart(A,1):ye?be.setStart(ye,ye.textContent?.length??0):be.setStart(A,1),be.collapse(!0),V.removeAllRanges(),V.addRange(be)},onBeforeInput:je,onInput:y,onBlur:B,onKeyDown:re},"editing"),ie=s.jsxs("div",{ref:v,className:`outline-none ${m?"cursor-default":"cursor-text"} ${a} ${i}`,onMouseUp:m?void 0:X,onClick:m?void 0:F,children:[e.spans.length>0?dt(e.spans):!m&&s.jsx("span",{className:"text-notion-text-secondary notion-placeholder",children:h}),e.isStreaming&&s.jsx("span",{className:"inline-block w-0.5 h-4 bg-notion-text animate-pulse ml-0.5"})]},"viewing"),Le=n?f?f(Re,!0):Re:f?f(ie,!1):ie;return s.jsxs("div",{ref:T,className:"relative",children:[!m&&s.jsxs(s.Fragment,{children:[s.jsx(Xn,{containerRef:T,onFormat:pe,isMathSelection:fe}),s.jsx(Jn,{isOpen:te,onSubmit:me,onCancel:xe})]}),Le]})}function Xt({block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,readonly:a=!1}){return s.jsx(Ge,{block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,className:"notion-paragraph text-notion-text leading-relaxed min-h-[1.5em]",placeholder:"Type '/' for commands...",readonly:a})}const Tt={1:"text-3xl font-bold",2:"text-2xl font-bold",3:"text-xl font-semibold",4:"text-lg font-semibold",5:"text-base font-semibold",6:"text-sm font-semibold"};function Vt({block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,readonly:a=!1}){const g=e.level||1,i=Tt[g]||Tt[1];return s.jsx(Ge,{block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,className:`notion-heading ${i} text-notion-text`,placeholder:`Heading ${g}`,readonly:a,renderWrapper:(h,f)=>s.jsx("div",{role:"heading","aria-level":g,children:h})})}function zt(e){const{block:n,isEditing:t,onContentChange:r,onStartEdit:c,onEndEdit:o,onKeyDown:a,readonly:g=!1}=e,i=x.useRef(null);x.useEffect(()=>{t&&i.current&&(i.current.focus(),i.current.selectionStart=i.current.value.length,i.current.selectionEnd=i.current.value.length)},[t]);const h=v=>{r(v.target.value)},f=()=>{o()},m=v=>{if(v.key==="Tab"){v.preventDefault();const T=v.currentTarget,N=T.selectionStart,E=T.selectionEnd,te=T.value,se=te.substring(0,N)+" "+te.substring(E);r(se),setTimeout(()=>{T.selectionStart=T.selectionEnd=N+2},0);return}if(v.key==="Escape"){o();return}a(v)},d="```"+(n.language||"")+`
|
|
18
|
-
`+
|
|
19
|
-
`).length),spellCheck:!1})]}):s.jsxs("div",{className:`notion-code-block ${g?"cursor-default":"cursor-text"}`,onClick:g?void 0:
|
|
20
|
-
${
|
|
21
|
-
$$`;return
|
|
22
|
-
`).length),placeholder:"Enter LaTeX...",spellCheck:!1}),
|
|
23
|
-
`).length),placeholder:"Enter Mermaid diagram code...",spellCheck:!1})]});if(!e.content)return g?null:s.jsx("div",{className:"notion-mermaid-block border-2 border-dashed border-notion-border rounded-lg p-6 text-center cursor-pointer hover:bg-notion-hover transition-colors",onClick:r,children:s.jsxs("div",{className:"text-notion-text-secondary",children:[s.jsx("svg",{className:"w-12 h-12 mx-auto mb-2 opacity-50",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:s.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5,d:"M7 21a4 4 0 01-4-4V5a2 2 0 012-2h4a2 2 0 012 2v12a4 4 0 01-4 4zm0 0h12a2 2 0 002-2v-4a2 2 0 00-2-2h-2.343M11 7.343l1.657-1.657a2 2 0 012.828 0l2.829 2.829a2 2 0 010 2.828l-8.486 8.485M7 17h.01"})}),s.jsx("p",{className:"text-sm",children:"Click to add Mermaid diagram"})]})});if(e.isStreaming)return s.jsxs("div",{className:"notion-mermaid-block p-6 bg-notion-code-bg rounded-lg",children:[s.jsxs("div",{className:"flex items-center justify-center space-x-2 text-notion-text-secondary",children:[s.jsx("div",{className:"w-4 h-4 border-2 border-notion-text-secondary border-t-transparent rounded-full animate-spin"}),s.jsx("span",{className:"text-sm",children:"Receiving diagram..."})]}),s.jsx("pre",{className:"mt-3 text-xs text-notion-text-secondary font-mono whitespace-pre-wrap",children:e.content})]});const
|
|
24
|
-
`))return}catch{}const b=window.getSelection();if(!b||b.rangeCount===0)return;const
|
|
25
|
-
`);
|
|
26
|
-
`)}function Yt({block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,readonly:a=!1}){const g=e.listType==="ordered",i=e.depth??0,h=e.metadata?.displayNumber,f=g?`${h??1}.`:"•";return s.jsx(Ge,{block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,className:"flex-1",placeholder:"Empty item",readonly:a,renderWrapper:(m,d)=>s.jsxs("div",{className:`notion-list-block flex items-start gap-2 py-0.5 ${a?"cursor-default":"cursor-text"}`,style:{paddingLeft:i*24},children:[s.jsx("span",{className:"text-notion-text select-none flex-shrink-0 w-5",children:f}),m]})})}function Jt({block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,readonly:a=!1}){return s.jsx(Ge,{block:e,isEditing:n,onContentChange:t,onStartEdit:r,onEndEdit:c,onKeyDown:o,className:"text-notion-text italic leading-relaxed",placeholder:"Empty quote",readonly:a,renderWrapper:(g,i)=>s.jsx("div",{className:`notion-blockquote border-l-4 border-notion-quote-border pl-4 py-0.5 ${a?"cursor-default":"cursor-text"}`,children:g})})}function Zt({block:e}){return s.jsxs("div",{className:"notion-divider py-3",children:[s.jsx("hr",{className:"border-t border-notion-border"}),e.isStreaming&&s.jsx("div",{className:"flex justify-center mt-1",children:s.jsx("span",{className:"inline-block w-0.5 h-2 bg-notion-text animate-pulse"})})]})}function ar({block:e,isEditing:n,onStartEdit:t,onEndEdit:r,onContentChange:c,onKeyDown:o,hooks:a,onExitTable:g,theme:i="light",readonly:h=!1}){const f=()=>{if(e.type==="paragraph"||e.type==="heading"||e.type==="list"||e.type==="blockquote"){const T={block:e,isEditing:n,onStartEdit:t,onEndEdit:r,onContentChange:c,onKeyDown:o,readonly:h};switch(e.type){case"paragraph":return s.jsx(Xt,{...T});case"heading":return s.jsx(Vt,{...T});case"list":return s.jsx(Yt,{...T});case"blockquote":return s.jsx(Jt,{...T})}}if(e.type==="table")return s.jsx(ir,{block:e,isEditing:n,onStartEdit:t,onEndEdit:r,onContentChange:c,onKeyDown:o,onExitTable:g,readonly:h});const d={block:e,isEditing:n,onStartEdit:t,onEndEdit:r,onContentChange:c,onKeyDown:o,readonly:h};switch(e.type){case"code":return s.jsx(zt,{...d,theme:i});case"image":return s.jsx(Ut,{...d});case"math":return s.jsx(Qt,{...d,theme:i});case"mermaid":return s.jsx(Gt,{...d,theme:i});case"divider":return s.jsx(Zt,{...d});default:return null}},m=`notion-block relative py-1 px-1 -mx-1 rounded ${h?"":"hover:bg-notion-hover/50"} transition-colors`;if(a?.renderBlock){const d=a.renderBlock(e,f);if(d!==null)return s.jsx("div",{className:m,"data-block-id":e.id,"data-block-type":e.type,children:d})}return s.jsx("div",{className:m,"data-block-id":e.id,"data-block-type":e.type,children:f()})}function en(...e){return e.filter(Boolean).join(" ")}const tn=It.forwardRef(({className:e,children:n,...t},r)=>s.jsxs(We.Root,{ref:r,className:en("relative overflow-hidden",e),...t,children:[s.jsx(We.Viewport,{className:"h-full w-full rounded-[inherit]",children:n}),s.jsx(nn,{}),s.jsx(We.Corner,{})]}));tn.displayName=We.Root.displayName;const nn=It.forwardRef(({className:e,orientation:n="vertical",...t},r)=>s.jsx(We.ScrollAreaScrollbar,{ref:r,orientation:n,className:en("flex touch-none select-none transition-colors",n==="vertical"&&"h-full w-2.5 border-l border-l-transparent p-[1px]",n==="horizontal"&&"h-2.5 flex-col border-t border-t-transparent p-[1px]",e),...t,children:s.jsx(We.ScrollAreaThumb,{className:"relative flex-1 rounded-full bg-notion-text-secondary/40"})}));nn.displayName=We.ScrollAreaScrollbar.displayName;function lr(e,n,t){return Math.max(n,Math.min(t,e))}function cr({isOpen:e,position:n,query:t,commands:r,activeIndex:c,onClose:o,onSelect:a,onHoverIndex:g}){const i=x.useRef(null);x.useEffect(()=>{if(!e)return;const f=m=>{const d=i.current;d&&(d.contains(m.target)||o())};return document.addEventListener("mousedown",f,!0),()=>document.removeEventListener("mousedown",f,!0)},[e,o]);const h=x.useMemo(()=>r.length?lr(c,0,r.length-1):0,[c,r.length]);return e?s.jsx("div",{ref:i,className:"notion-slash-menu absolute z-[60] w-[360px] max-w-[calc(100vw-24px)] overflow-x-hidden",style:{top:n.top,left:n.left},role:"dialog","aria-label":"Slash commands",children:s.jsxs("div",{className:"rounded-xl border border-notion-border bg-notion-bg-elevated shadow-[0_16px_40px_rgba(15,23,42,0.14)] overflow-hidden",children:[s.jsxs("div",{className:"px-3 py-2 border-b border-notion-border bg-gradient-to-b from-notion-hover/70 to-transparent",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-[11px] uppercase tracking-[0.12em] text-notion-text-secondary select-none",children:"Insert"}),s.jsxs("span",{className:"text-xs font-mono text-notion-text-secondary",children:["/",t||""]})]}),s.jsx("div",{className:"mt-1 text-xs text-notion-text-secondary",children:r.length?"Type to filter, Enter to apply, Esc to close":"No matches"})]}),s.jsx(tn,{className:"h-[320px]",children:s.jsx("div",{className:"py-1 px-1",children:r.map((f,m)=>{const d=m===h;return s.jsxs("button",{type:"button",onMouseEnter:()=>g?.(m),onMouseDown:v=>v.preventDefault(),onClick:()=>a(f),className:`w-full text-left px-2 py-2 rounded-md transition-colors flex items-start gap-2 ${d?"bg-notion-hover text-notion-text":"hover:bg-notion-hover/70 text-notion-text"}`,children:[s.jsx("div",{className:"mt-0.5 w-8 h-8 rounded-lg border border-notion-border bg-notion-bg flex items-center justify-center flex-shrink-0",children:f.icon?s.jsx("div",{className:"text-notion-text-secondary",children:f.icon}):s.jsx("div",{className:"text-notion-text-secondary text-xs font-mono",children:f.title.slice(0,2)})}),s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsxs("div",{className:"flex items-baseline gap-2",children:[s.jsx("div",{className:"font-medium text-sm truncate",children:f.title}),f.group?s.jsx("div",{className:"text-[11px] text-notion-text-secondary truncate",children:f.group}):null]}),f.description?s.jsx("div",{className:"text-xs text-notion-text-secondary mt-0.5 whitespace-normal break-words",children:f.description}):null]})]},f.id)})})})]})}):null}function lt(e,n,t){return Math.max(n,Math.min(t,e))}function He(e){const n=new Set;let t=!1;const r=e.map(c=>{if(!n.has(c.id))return n.add(c.id),c;t=!0;let o=Ie(10);for(;n.has(o);)o=Ie(10);return n.add(o),{...c,id:o}});return t?r:e}const Bt=360,dr=x.forwardRef(function({markdown:n,isStreaming:t=!1,hooks:r,onMarkdownChange:c,className:o="",theme:a,readonly:g=!1},i){const h=()=>{if(typeof window>"u")return"light";const p=document.documentElement,l=document.body;return p&&p.classList.contains("dark")||l&&l.classList.contains("dark")||window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"},[f,m]=x.useState(()=>a==="dark"||a==="light"?a:a==="system"?typeof window<"u"&&window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light":h());x.useEffect(()=>{if(a==="dark"||a==="light"){m(a);return}if(typeof window>"u"){m("light");return}const p=()=>{if(a==="system"){if(!window.matchMedia){m("light");return}m(window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light");return}m(h())};p();const l=[];if(a!=="system"&&typeof MutationObserver<"u"){const w=C=>{if(!C)return;const H=new MutationObserver(()=>p());H.observe(C,{attributes:!0,attributeFilter:["class"]}),l.push(H)};w(document.documentElement),w(document.body)}const u=window.matchMedia?window.matchMedia("(prefers-color-scheme: dark)"):null,k=()=>p();return u&&u.addEventListener("change",k),()=>{for(const w of l)w.disconnect();u&&u.removeEventListener("change",k)}},[a]);const d=x.useRef(null),{blocks:v}=Pt({markdown:n,isStreaming:t}),[T,N]=x.useState(v),[E,te]=x.useState([]),[se,le]=x.useState(null),[Y,b]=x.useState(null),y=x.useRef(T);x.useEffect(()=>{y.current=T},[T]);const B=x.useRef(se);x.useEffect(()=>{B.current=se},[se]);const F=x.useRef([]);x.useEffect(()=>{F.current=E},[E]);const X=x.useCallback(p=>{F.current=p,te(p)},[]),Z=x.useRef(null),re=x.useRef(!1),pe=x.useRef(!1),me=x.useRef(0),xe=x.useRef([]),fe=x.useRef(new Set),je=x.useRef(null);x.useEffect(()=>{const p=l=>{const u=d.current;if(!u||!u.contains(l.target))return;l.target?.closest?.('[contenteditable="true"], textarea')&&(je.current=null)};return document.addEventListener("mousedown",p,!0),()=>document.removeEventListener("mousedown",p,!0)},[]);const Ve=x.useRef(null),Re=x.useCallback(p=>y.current.find(l=>l.id===p)||null,[]),ie=x.useCallback(p=>({...p,metadata:{...p.metadata||{},dirty:!0}}),[]),Le=x.useCallback(p=>({...p,metadata:{...p.metadata||{},dirty:!1}}),[]),O=x.useCallback(()=>{b(null)},[]),z=x.useCallback(p=>{const l=d.current;if(!l)return;let u=0,k=0;const w=document.querySelector(`[data-block-id="${p}"]`),C=l.getBoundingClientRect(),H=window.getSelection(),q=H&&H.rangeCount>0?H.getRangeAt(0):null,P=q?q.getBoundingClientRect():null;if(P&&P.width+P.height>0)u=P.bottom-C.top+8,k=P.left-C.left;else if(w){const K=w.getBoundingClientRect();u=K.top-C.top+28,k=K.left-C.left+28}const M=8,$=Math.max(M,C.width-Bt-8);k=lt(k,M,$),b({blockId:p,query:"",activeIndex:0,position:{top:u,left:k}})},[]);x.useEffect(()=>{const p=pe.current;if(pe.current=t,!re.current&&!t){fe.current.clear();const u=He(v);y.current=u,N(u),X([]),me.current=0,xe.current=[],re.current=!0;return}if(t){if(re.current=!0,p||(me.current=0,xe.current=[],X([]),n.trim()===""&&(fe.current.clear(),N([]))),v.length===0){X([]);return}let u=v.length;for(let q=v.length-1;q>=0&&v[q].isStreaming;q--)u=q;const k=v.slice(0,u),w=v.slice(u),C=fe.current,H=me.current;if(C.size===0){const q=k.map($=>Le({...$,isStreaming:!1})),P=y.current,M=q.length!==P.length||q.some(($,K)=>{const D=P[K];return!D||$.type!==D.type||$.id!==D.id});if(M){const $=He(q);y.current=$,N($)}xe.current=(M?y.current:P).map($=>$.id),me.current=k.length}else{const q=k.length,P=L=>{if(L.type==="paragraph"||L.type==="heading"||L.type==="list"||L.type==="blockquote"){const ue=L.level??"",we=L.listType??"",ke=L.depth??"";return`${L.type}|${ue}|${we}|${ke}|${Me(L.spans||[],{forParsing:!0})}`}return L.type==="table"?`table|${ge([L])}`:L.type==="code"?`code|${L.language||""}|${L.content||""}`:L.type==="mermaid"?`mermaid|${L.content||""}`:L.type==="math"?`math|${L.content||""}`:L.type==="image"?`image|${L.content||""}`:L.type==="divider"?"divider":`${L.type}|${L.content||""}`},M=L=>!!L?.metadata?.dirty||C.has(L.id),$=y.current;let K=$.slice(),D=!1;const I=$.slice(0,H).map(L=>L.id);let W=xe.current.length>0?xe.current.slice(0,H):I;if(q<H){const L=W.slice(q,H);if(L.length>0){const ue=new Set(L),we=K.filter(ke=>ue.has(ke.id)?M(ke):!0);we.length!==K.length&&(K=we,D=!0)}W=W.slice(0,q)}const U=Math.min(q,W.length),ne=Math.max(0,U-64);for(let L=ne;L<U;L++){const ue=W[L];if(!ue)continue;const we=K.findIndex(Fe=>Fe.id===ue);if(we===-1)continue;const ke=K[we];if(M(ke))continue;const Ee=Le({...k[L],isStreaming:!1});P(ke)!==P(Ee)&&(K[we]=Ee,W[L]=Ee.id,D=!0)}if(q>H){const L=k.slice(H).map(ue=>Le({...ue,isStreaming:!1}));K=K.concat(L),W=W.concat(L.map(ue=>ue.id)),D=!0}if(me.current=q,xe.current=W,D){const L=He(K);y.current=L,N(L)}}w.length===0&&(me.current=k.length),X(w.map(q=>Le({...q,isStreaming:!0})));return}if(p&&!t){N(u=>{const k=u.slice(),w=F.current;for(const H of w)k.push(Le({...H,isStreaming:!1}));const C=He(k.length>0?k:[{id:Ie(10),type:"paragraph",spans:[],isStreaming:!1}]);return y.current=C,C}),X([]),me.current=0,xe.current=[];return}if(Z.current!==null&&n===Z.current)return;fe.current.clear();const l=He(v.map(u=>Le(u)));y.current=l,N(l),X([]),me.current=0,xe.current=[]},[n,t,v,Le]);const Q=x.useCallback(p=>{const l=E.length>0?p.concat(E.map(k=>({...k,isStreaming:!1}))):p,u=ge(l);Z.current=u,c?.(u)},[c,E]),A=x.useCallback(p=>{const l=He(p);y.current=l,N(l),Q(l)},[Q]),V=p=>p.type==="paragraph"||p.type==="heading"||p.type==="list"||p.type==="blockquote",ae=p=>{if(V(p))return p.spans.every(l=>l.text.trim()==="");if(p.type==="divider")return!1;if(p.type==="table"){const l=p.header||[],u=p.rows||[],k=w=>!!it(w||[]).trim();return!(l.some(k)||u.some(w=>w.some(k)))}return p.content?.trim?.()===""},ce=x.useCallback(p=>{const l=r?.getBlockPolicy?.(p);return l||(V(p)?{atomic:!1,mergeable:!0,deleteOnBlurWhenEmpty:!1,enterBehavior:"newBlock"}:p.type==="table"?{atomic:!0,mergeable:!1,deleteOnBlurWhenEmpty:!1,enterBehavior:"stayInBlock"}:p.type==="image"?{atomic:!0,mergeable:!1,deleteOnBlurWhenEmpty:!0,enterBehavior:"ignore"}:{atomic:!0,mergeable:!1,deleteOnBlurWhenEmpty:!0,enterBehavior:"stayInBlock"})},[r]),ee=x.useCallback((p,l,u)=>{Ve.current={blockId:p,start:l,end:u},B.current=p,le(p)},[]);x.useEffect(()=>{const p=Ve.current;if(!p)return;Ve.current=null;const{blockId:l,start:u,end:k}=p;requestAnimationFrame(()=>{const w=document.querySelector(`[data-block-id="${l}"]`);if(!w)return;const C=w.querySelector('[contenteditable="true"]');if(C){C.focus(),Be(C,u,k);return}const H=w.querySelector("textarea");H&&(H.focus(),H.setSelectionRange(u,k))})},[T,se]);const ye=x.useCallback((p,l,u="bottom")=>{B.current=p,le(p);const k=(C,H)=>{const q=document;if(typeof q.caretRangeFromPoint=="function")return q.caretRangeFromPoint(C,H)||null;if(typeof q.caretPositionFromPoint=="function"){const P=q.caretPositionFromPoint(C,H);if(!P)return null;const M=document.createRange();return M.setStart(P.offsetNode,P.offset),M.collapse(!0),M}return null},w=C=>{requestAnimationFrame(()=>{const q=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');if(!q){C<3&&w(C+1);return}q.focus();const P=q.getBoundingClientRect(),M=Math.max(P.left+4,Math.min(l,P.right-4)),$=u==="top"?P.top+4:P.bottom-4,K=k(M,$);if(K&&q.contains(K.startContainer)){K.collapse(!0);const I=window.getSelection();I?.removeAllRanges(),I?.addRange(K);return}const D=Re(p);if(D&&V(D)){const I=D.spans.reduce((G,W)=>G+W.text.length,0);Be(q,I,I)}})};w(0)},[Re]),Ne=x.useCallback((p,l,u,k)=>{fe.current.add(p),A(y.current.map(C=>C.id!==p?C:l==="paragraph"?ie({id:C.id,type:"paragraph",spans:Ae(u),isStreaming:!1}):l==="heading"?ie({id:C.id,type:"heading",spans:Ae(u),level:k?.level??1,isStreaming:!1}):l==="blockquote"?ie({id:C.id,type:"blockquote",spans:Ae(u),isStreaming:!1}):l==="list"?ie({id:C.id,type:"list",spans:Ae(u),listType:k?.listType??"unordered",depth:k?.depth??0,isStreaming:!1}):l==="code"?ie({id:C.id,type:"code",content:"",language:k?.language||"text",isStreaming:!1}):l==="divider"?ie({id:C.id,type:"divider",isStreaming:!1}):C));const w=Re(p);w&&r?.onBlockChange&&r.onBlockChange(p,u,w)},[A,r,Re,ie]),{handleKeyDown:be}=Ht({onBlockTypeChange:Ne}),oe=x.useCallback((p,l)=>{fe.current.add(p);const u=y.current.map(w=>{if(w.id!==p)return w;if(V(w)){const C=typeof l=="string"?Ae(l):l;return ie({...w,spans:C})}if(w.type==="table"){if(typeof l!="string")return w;const C=_e(l,!1,[w]).find(H=>H.type==="table");return!C||C.type!=="table"?w:ie({...C,isStreaming:!1})}return"content"in w?ie({...w,content:typeof l=="string"?l:Me(l)}):w});A(u);const k=u.find(w=>w.id===p);k&&r?.onBlockChange&&r.onBlockChange(p,l,k)},[r,A,ie]),he=x.useCallback((p,l)=>{const u=Ie(10);fe.current.add(u);const k=ie((()=>{switch(l.type){case"paragraph":return{id:u,type:"paragraph",spans:l.spans??[],isStreaming:!1};case"heading":return{id:u,type:"heading",spans:l.spans??[],level:l.level??1,isStreaming:!1};case"blockquote":return{id:u,type:"blockquote",spans:l.spans??[],isStreaming:!1};case"list":return{id:u,type:"list",spans:l.spans??[],listType:l.listType??"unordered",depth:l.depth??0,isStreaming:!1};case"code":return{id:u,type:"code",content:l.content??"",language:l.language??"text",isStreaming:!1};case"math":return{id:u,type:"math",content:l.content??"",isStreaming:!1};case"mermaid":return{id:u,type:"mermaid",content:l.content??"",isStreaming:!1};case"image":return{id:u,type:"image",content:l.content??"",isStreaming:!1};case"table":{const P=Math.max(1,l.cols??3),M=Math.max(0,l.rows??2);return{id:u,type:"table",header:Array.from({length:P},()=>[]),rows:Array.from({length:M},()=>Array.from({length:P},()=>[])),align:Array.from({length:P},()=>"left"),isStreaming:!1}}case"divider":return{id:u,type:"divider",isStreaming:!1}}})()),w=y.current,C=p?w.findIndex(P=>P.id===p):w.length-1,H=C===-1?w.length:C+1,q=w.slice(0,H).concat([k],w.slice(H));return A(q),u},[A,ie]),de=x.useCallback(p=>{fe.current.delete(p);const l=y.current,u=l.findIndex(P=>P.id===p);if(u===-1)return;const k=l.slice(0,u).concat(l.slice(u+1)),w=k.length===0?[{id:Ie(10),type:"paragraph",spans:[],isStreaming:!1}]:k;A(w);const C=Math.max(0,Math.min(u-1,w.length-1)),H=w[C],q=V(H)?H.spans.reduce((P,M)=>P+M.text.length,0):H.content?.length??0;ee(H.id,q,q)},[A,ee]),_=x.useCallback((p,l,u)=>{fe.current.add(p);const k=y.current,w=k.find(q=>q.id===p);if(!w)return;const C=V(w)?w.spans:[],H=(()=>{if(l==="paragraph")return{id:w.id,type:"paragraph",spans:C,isStreaming:!1};if(l==="heading")return{id:w.id,type:"heading",spans:C,level:u?.level??1,isStreaming:!1};if(l==="blockquote")return{id:w.id,type:"blockquote",spans:C,isStreaming:!1};if(l==="list")return{id:w.id,type:"list",spans:C,listType:u?.listType??"unordered",depth:u?.depth??0,isStreaming:!1};if(l==="table"){const M=Array.from({length:3},($,K)=>K===0?C:[]);return{id:w.id,type:"table",header:M,rows:Array.from({length:2},()=>Array.from({length:3},()=>[])),align:Array.from({length:3},()=>"left"),isStreaming:!1}}return w})();A(k.map(q=>q.id===p?ie(H):q))},[A,ie]),j=x.useCallback((p,l=!1)=>{fe.current.add(p);const u=y.current,k=u.findIndex($=>$.id===p),w=u[k],C=k>0?u[k-1]:null;if(!w||w.type!=="list"||!C||C.type!=="list")return;const H=w.depth??0,q=(C.depth??0)+1,P=Math.min(H+1,q);if(P===H)return;const M=l?w.listType:C.listType;A(u.map($=>$.id===p?ie({...w,depth:P,listType:M}):$))},[A,ie]),S=x.useCallback((p,l=!1)=>{fe.current.add(p);const u=y.current,k=u.findIndex(H=>H.id===p),w=u[k];if(!w||w.type!=="list")return;const C=w.depth??0;if(C>0){const H=C-1;let q=w.listType;if(!l)for(let P=k-1;P>=0;P--){const M=u[P];if(M.type!=="list")break;if((M.depth??0)===H){q=M.listType;break}}A(u.map(P=>P.id===p?ie({...w,depth:H,listType:q}):P))}else _(p,"paragraph")},[A,_,ie]),J=x.useMemo(()=>{const p=E.length>0?y.current.concat(E):y.current;return{updateBlock:(l,u)=>oe(l,u),getBlock:l=>Re(l),getAllBlocks:()=>p,getMarkdown:()=>ge(p),insertBlockAfter:(l,u)=>he(l,u),deleteBlock:l=>de(l),setBlockType:(l,u,k)=>_(l,u,k),indentListItem:l=>j(l),outdentListItem:l=>S(l)}},[oe,Re,T,E,he,de,_,j,S]),Se=x.useMemo(()=>{const p=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M4 6h16"}),s.jsx("path",{d:"M4 12h10"}),s.jsx("path",{d:"M4 18h16"})]}),l=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M6 4v16"}),s.jsx("path",{d:"M18 4v16"}),s.jsx("path",{d:"M6 12h12"})]}),u=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M8 6h13"}),s.jsx("path",{d:"M8 12h13"}),s.jsx("path",{d:"M8 18h13"}),s.jsx("path",{d:"M3 6h.01"}),s.jsx("path",{d:"M3 12h.01"}),s.jsx("path",{d:"M3 18h.01"})]}),k=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M10 6h11"}),s.jsx("path",{d:"M10 12h11"}),s.jsx("path",{d:"M10 18h11"}),s.jsx("path",{d:"M3 7h1"}),s.jsx("path",{d:"M3 12h1"}),s.jsx("path",{d:"M3 18h1"})]}),w=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M10 11H6c-1.1 0-2 .9-2 2v5h6v-7z"}),s.jsx("path",{d:"M20 11h-4c-1.1 0-2 .9-2 2v5h6v-7z"}),s.jsx("path",{d:"M4 11V9a5 5 0 0 1 5-5"}),s.jsx("path",{d:"M14 11V9a5 5 0 0 1 5-5"})]}),C=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M16 18l6-6-6-6"}),s.jsx("path",{d:"M8 6l-6 6 6 6"})]}),H=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M7 7h10"}),s.jsx("path",{d:"M7 17h10"}),s.jsx("path",{d:"M7 7v10"}),s.jsx("path",{d:"M17 7v10"})]}),q=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M4 4h6l4 16h6"}),s.jsx("path",{d:"M4 12h16"})]}),P=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M3 12h18"}),s.jsx("path",{d:"M8 8h.01"}),s.jsx("path",{d:"M8 16h.01"}),s.jsx("path",{d:"M16 8h.01"}),s.jsx("path",{d:"M16 16h.01"})]}),M=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("rect",{x:"3",y:"3",width:"18",height:"18",rx:"2",ry:"2"}),s.jsx("path",{d:"M3 16l5-5a2 2 0 0 1 3 0l5 5"}),s.jsx("path",{d:"M14 14l2-2a2 2 0 0 1 3 0l2 2"}),s.jsx("path",{d:"M8 8h.01"})]}),$=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("rect",{x:"3",y:"4",width:"18",height:"16",rx:"2"}),s.jsx("path",{d:"M3 10h18"}),s.jsx("path",{d:"M9 4v16"}),s.jsx("path",{d:"M15 4v16"})]}),K=D=>({id:`heading-${D}`,title:`Heading ${D}`,description:`Section heading (H${D})`,group:"Text",aliases:[`h${D}`],keywords:["heading","title",`h${D}`],icon:l,block:{type:"heading",level:D,spans:[]}});return[{id:"paragraph",title:"Text",description:"Plain paragraph",group:"Text",aliases:["p","text"],keywords:["paragraph"],icon:p,block:{type:"paragraph",spans:[]}},K(1),K(2),K(3),{id:"blockquote",title:"Quote",description:"Blockquote",group:"Text",aliases:["quote","bq"],keywords:["blockquote"],icon:w,block:{type:"blockquote",spans:[]}},{id:"bulleted-list",title:"Bulleted list",description:"Unordered list item",group:"Lists",aliases:["ul","bullet","list"],keywords:["unordered"],icon:u,block:{type:"list",listType:"unordered",depth:0,spans:[]}},{id:"numbered-list",title:"Numbered list",description:"Ordered list item",group:"Lists",aliases:["ol","number","ordered"],keywords:["ordered"],icon:k,block:{type:"list",listType:"ordered",depth:0,spans:[]}},{id:"code",title:"Code",description:"Fenced code block",group:"Media",aliases:["codeblock","```"],keywords:["snippet"],icon:C,block:{type:"code",language:"text",content:""}},{id:"mermaid",title:"Mermaid",description:"Mermaid diagram block",group:"Media",aliases:["diagram","flow"],keywords:["graph","sequence"],icon:H,block:{type:"mermaid",content:""}},{id:"math",title:"Math (block)",description:"LaTeX block equation",group:"Media",aliases:["latex","equation"],keywords:["katex"],icon:q,block:{type:"math",content:""}},{id:"image",title:"Image",description:"Image block (URL)",group:"Media",aliases:["img","photo","picture"],keywords:["media"],icon:M,block:{type:"image",content:""}},{id:"table",title:"Table",description:"Spreadsheet-style table",group:"Layout",aliases:["tbl","grid"],keywords:["rows","columns","sheet"],icon:$,block:{type:"table",rows:2,cols:3}},{id:"divider",title:"Divider",description:"Horizontal divider",group:"Layout",aliases:["hr","line","---"],keywords:["separator"],icon:P,block:{type:"divider"},insertParagraphAfter:!0}]},[]),ht=x.useCallback(p=>{const l=y.current,u=l.findIndex(C=>C.id===p);if(u===-1)return;const k=u+1<l.length?l[u+1]:null;if(k&&k.type==="paragraph"&&it(k.spans).trim()===""&&!k.isStreaming){ee(k.id,0,0);return}const w=he(p,{type:"paragraph",spans:[]});ee(w,0,0)},[he,ee]),Te=x.useCallback(p=>{const l=r?.getSlashCommands?r.getSlashCommands({block:p}):[];return Se.concat(l||[])},[Se,r]),De=x.useCallback((p,l)=>{const u=l.trim().toLowerCase();return u?p.map(k=>{const w=[k.title,...k.aliases||[],...k.keywords||[]].join(" ").toLowerCase(),C=(k.aliases||[]).some(P=>P.toLowerCase().startsWith(u))||k.title.toLowerCase().startsWith(u),H=w.includes(u);return{cmd:k,score:C?3:H?1:0}}).filter(k=>k.score>0).sort((k,w)=>w.score-k.score||k.cmd.title.localeCompare(w.cmd.title)).map(k=>k.cmd):p},[]),Ye=x.useMemo(()=>{if(!Y)return[];const p=y.current.find(l=>l.id===Y.blockId);return p?De(Te(p),Y.query):[]},[Y,De,Te]),nt=x.useCallback((p,l)=>{switch(l.type){case"paragraph":return{id:p,type:"paragraph",spans:l.spans??[],isStreaming:!1};case"heading":return{id:p,type:"heading",spans:l.spans??[],level:l.level??1,isStreaming:!1};case"blockquote":return{id:p,type:"blockquote",spans:l.spans??[],isStreaming:!1};case"list":return{id:p,type:"list",spans:l.spans??[],listType:l.listType??"unordered",depth:l.depth??0,isStreaming:!1};case"code":return{id:p,type:"code",content:l.content??"",language:l.language??"text",isStreaming:!1};case"math":return{id:p,type:"math",content:l.content??"",isStreaming:!1};case"mermaid":return{id:p,type:"mermaid",content:l.content??"",isStreaming:!1};case"image":return{id:p,type:"image",content:l.content??"",isStreaming:!1};case"table":{const u=Math.max(1,l.cols??3),k=Math.max(0,l.rows??2);return{id:p,type:"table",header:Array.from({length:u},()=>[]),rows:Array.from({length:k},()=>Array.from({length:u},()=>[])),align:Array.from({length:u},()=>"left"),isStreaming:!1}}case"divider":return{id:p,type:"divider",isStreaming:!1}}},[]),rt=x.useCallback((p,l,u)=>{const k=y.current.find(M=>M.id===p);if(!k)return;if(l.execute){l.execute(J,{blockId:p,block:k,query:u});return}if(!l.block)return;const w=y.current,C=w.findIndex(M=>M.id===p);if(C===-1)return;let H=l.block;l.block.type==="list"&&k.type==="list"&&(H={...l.block,depth:k.depth??0}),fe.current.add(p);const q=ie(nt(p,H));let P=w.slice(0,C).concat([q],w.slice(C+1));if(l.insertParagraphAfter){const M=Ie(10);fe.current.add(M);const $=ie(nt(M,{type:"paragraph",spans:[]}));P=P.slice(0,C+1).concat([$],P.slice(C+1)),A(P),ee(M,0,0);return}A(P),ee(p,0,0)},[J,ie,nt,A,ee]);x.useImperativeHandle(i,()=>J,[J]),x.useEffect(()=>{r?.ref&&"current"in r.ref&&(r.ref.current=J)},[r?.ref,J]);const an=x.useCallback(p=>{y.current.find(u=>u.id===p)?.isStreaming||(B.current=p,le(p))},[]),ln=x.useCallback(p=>{if(B.current!==p)return;const l=y.current.find(k=>k.id===p);if(!l){B.current=null,le(null);return}if(ce(l).deleteOnBlurWhenEmpty&&ae(l)){de(p);return}B.current=null,le(null)},[de,ce]),cn=x.useCallback((p,l)=>{oe(p,l)},[oe]),pt=(p,l)=>{const u=[],k=[];let w=0;for(const C of p){const H=w,q=w+C.text.length;if(q<=l)u.push({...C});else if(H>=l)k.push({...C});else{const P=l-H;P>0&&u.push({...C,text:C.text.slice(0,P)}),P<C.text.length&&k.push({...C,text:C.text.slice(P)})}w=q}return[Oe(u),Oe(k)]},mt=(p,l,u)=>{if(l===u)return p;const k=Math.min(l,u),w=Math.max(l,u),C=[];let H=0;for(const q of p){const P=H,M=H+q.text.length;if(M<=k||P>=w)C.push({...q});else{const $=Math.max(0,k-P),K=Math.max(0,w-P);$>0&&C.push({...q,text:q.text.slice(0,$)}),K<q.text.length&&C.push({...q,text:q.text.slice(K)})}H=M}return Oe(C)},st=(p,l)=>{if(p.length===0)return l;if(l.length===0)return p;const u=p[p.length-1],k=l[0],C=u.text.length>0&&k.text.length>0&&!/\s$/.test(u.text)&&!/^\s/.test(k.text)?p.concat([{text:" ",style:"plain"}],l):p.concat(l);return Oe(C)},dn=x.useCallback((p,l)=>{const u=y.current.find(M=>M.id===p);if(!u)return;l.key!=="ArrowUp"&&l.key!=="ArrowDown"&&(je.current=null);const k=M=>{if(!(u.type==="paragraph"||u.type==="heading"||u.type==="list"||u.type==="blockquote"))return;oe(p,M?[{text:M,style:"plain"}]:[]);const K=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');if(K){K.textContent=M;const D=M.length;Be(K,D,D);return}ee(p,M.length,M.length)};if(Y&&Y.blockId===p){l.preventDefault();const M=Y.query,$=l.key;if($==="Escape"){O();const D="/"+M;k(D);return}const K=Math.max(0,Ye.length-1);if($==="ArrowDown"||$==="Tab"){b({...Y,activeIndex:Math.min(Y.activeIndex+1,K)});return}if($==="ArrowUp"){b({...Y,activeIndex:Math.max(Y.activeIndex-1,0)});return}if($==="Backspace"){if(M.length===0){O(),k("");return}const D=M.slice(0,-1),I=De(Te(u),D);b({...Y,query:D,activeIndex:0}),k("/"+D),I.length===0&&O();return}if($==="Enter"){const D=Ye[lt(Y.activeIndex,0,K)];O(),D&&rt(p,D,M);return}if($.length===1&&!l.metaKey&&!l.ctrlKey&&!l.altKey){const D=M+$,I=De(Te(u),D);b({...Y,query:D,activeIndex:0}),k("/"+D),I.length===0&&O();return}return}if(!Y&&V(u)){const M=u.spans.map($=>$.text).join("");if(M.startsWith("/")){const $=l.currentTarget,K=$?.isContentEditable?ve($):null;if(K&&K.start===K.end&&K.start===M.length){const D=(I,G)=>{l.preventDefault();const W=d.current;if(!W)return;let U=0,R=0;const ne=document.querySelector(`[data-block-id="${p}"]`),L=W.getBoundingClientRect(),ue=window.getSelection(),we=ue&&ue.rangeCount>0?ue.getRangeAt(0):null,ke=we?we.getBoundingClientRect():null;if(ke&&ke.width+ke.height>0)U=ke.bottom-L.top+8,R=ke.left-L.left;else if(ne){const Je=ne.getBoundingClientRect();U=Je.top-L.top+28,R=Je.left-L.left+28}const Ee=8,Fe=Math.max(Ee,L.width-Bt-8);R=lt(R,Ee,Fe),b({blockId:p,query:G,activeIndex:0,position:{top:U,left:R}}),oe(p,[{text:I,style:"plain"}]);const $e=ne?.querySelector('[contenteditable="true"]');$e&&($e.textContent=I,Be($e,I.length,I.length))};if(l.key==="Backspace"&&M.length>1){const I=M.slice(0,-1),G=I.slice(1);if(De(Te(u),G).length>0){D(I,G);return}}else if(l.key.length===1&&!l.metaKey&&!l.ctrlKey&&!l.altKey){const I=M+l.key,G=I.slice(1);if(De(Te(u),G).length>0){D(I,G);return}}}}}if((l.key==="Backspace"||l.key==="Delete")&&l.target?.isContentEditable){const M=window.getSelection();if(M&&M.rangeCount>0){const $=M.getRangeAt(0);if(!$.collapsed){const K=$.startContainer.parentElement?.closest?.("[data-block-id]"),D=$.endContainer.parentElement?.closest?.("[data-block-id]"),I=K?.getAttribute("data-block-id"),G=D?.getAttribute("data-block-id");if(I&&G&&I!==G){l.preventDefault();const W=y.current.findIndex($e=>$e.id===I),U=y.current.findIndex($e=>$e.id===G);if(W===-1||U===-1)return;const R=Math.min(W,U),ne=Math.max(W,U),L=y.current,ue=L.slice(0,R).concat(L.slice(ne+1)),we=ue.length===0?[{id:Ie(10),type:"paragraph",spans:[],isStreaming:!1}]:ue;A(we);const ke=Math.max(0,Math.min(R-1,we.length-1)),Ee=we[ke],Fe=V(Ee)?Ee.spans.reduce(($e,Je)=>$e+Je.text.length,0):Ee.content?.length??0;ee(Ee.id,Fe,Fe);return}}}}const w=l.currentTarget;let C=0;if(w instanceof HTMLTextAreaElement)C=w.selectionStart;else if(w?.isContentEditable){const M=ve(w);M&&(C=M.start)}const H="spans"in u?Me(u.spans):"content"in u?u.content:"";if(be(l,u,H,C))return;if(l.key==="/"&&C===0){const M=!!u.metadata?.dirty,$=u.type==="paragraph"||u.type==="heading"||u.type==="list"||u.type==="blockquote"?it(u.spans).trim()==="":u.type==="divider"?!1:u.content?.trim?.()==="";if(M&&$&&!u.isStreaming){l.preventDefault(),z(p),oe(p,[{text:"/",style:"plain"}]);const D=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');D?(D.textContent="/",Be(D,1,1)):ee(p,1,1);return}}if(l.key==="ArrowUp"&&w?.isContentEditable&&V(u)){const M=ve(w),$=I=>I?.parentElement?.closest?.('[data-math-delim="1"]')!=null,K=(I,G)=>{const W=document.createTreeWalker(I,NodeFilter.SHOW_TEXT,null);let U=0,R;for(;R=W.nextNode();){if($(R))continue;const ne=R.textContent?.length??0;if(ne!==0){if(U+ne>=G)return{node:R,offset:Math.max(0,Math.min(ne,G-U))};U+=ne}}return{node:I,offset:I.childNodes.length}},D=(I,G)=>{if(I.textContent==null||I.textContent.length===0)return I.getBoundingClientRect();const W=K(I,G),U=document.createRange();try{if(U.setStart(W.node,W.offset),W.node.nodeType===Node.TEXT_NODE){const R=W.node,ne=R.textContent?.length??0;ne>0&&(W.offset<ne?U.setEnd(R,W.offset+1):W.offset>0?(U.setStart(R,W.offset-1),U.setEnd(R,W.offset)):U.setEnd(R,Math.min(1,ne)))}}catch{return null}return U.getClientRects()[0]||U.getBoundingClientRect()};if(M&&M.start===M.end){const I=w.getBoundingClientRect();let G=D(w,M.start);if(!G){const W=window.getSelection();if(W&&W.rangeCount>0){const U=W.getRangeAt(0);if(U.collapsed&&w.contains(U.startContainer))try{const R=U.cloneRange();if(R.startContainer.nodeType===Node.TEXT_NODE){const ne=R.startContainer,L=ne.textContent?.length??0;L>0&&(R.startOffset<L?R.setEnd(ne,R.startOffset+1):R.startOffset>0&&(R.setStart(ne,R.startOffset-1),R.setEnd(ne,R.startOffset)))}G=R.getClientRects()[0]||R.getBoundingClientRect()}catch{}}}if(G){je.current==null&&(je.current=G.left+1);const W=je.current;if(G.top<=I.top+6){const R=y.current;let L=R.findIndex(ue=>ue.id===p)-1;for(;L>=0&&(!V(R[L])||R[L].isStreaming);)L--;if(L>=0){l.preventDefault(),ye(R[L].id,W,"bottom");return}}}}}if(l.key==="ArrowDown"&&w?.isContentEditable&&V(u)){const M=ve(w),$=I=>I?.parentElement?.closest?.('[data-math-delim="1"]')!=null,K=(I,G)=>{const W=document.createTreeWalker(I,NodeFilter.SHOW_TEXT,null);let U=0,R;for(;R=W.nextNode();){if($(R))continue;const ne=R.textContent?.length??0;if(ne!==0){if(U+ne>=G)return{node:R,offset:Math.max(0,Math.min(ne,G-U))};U+=ne}}return{node:I,offset:I.childNodes.length}},D=(I,G)=>{if(I.textContent==null||I.textContent.length===0)return I.getBoundingClientRect();const W=K(I,G),U=document.createRange();try{if(U.setStart(W.node,W.offset),W.node.nodeType===Node.TEXT_NODE){const R=W.node,ne=R.textContent?.length??0;ne>0&&(W.offset<ne?U.setEnd(R,W.offset+1):W.offset>0?(U.setStart(R,W.offset-1),U.setEnd(R,W.offset)):U.setEnd(R,Math.min(1,ne)))}}catch{return null}return U.getClientRects()[0]||U.getBoundingClientRect()};if(M&&M.start===M.end){const I=w.getBoundingClientRect();let G=D(w,M.start);if(!G){const W=window.getSelection();if(W&&W.rangeCount>0){const U=W.getRangeAt(0);if(U.collapsed&&w.contains(U.startContainer))try{const R=U.cloneRange();if(R.startContainer.nodeType===Node.TEXT_NODE){const ne=R.startContainer,L=ne.textContent?.length??0;L>0&&(R.startOffset<L?R.setEnd(ne,R.startOffset+1):R.startOffset>0&&(R.setStart(ne,R.startOffset-1),R.setEnd(ne,R.startOffset)))}G=R.getClientRects()[0]||R.getBoundingClientRect()}catch{}}}if(G){je.current==null&&(je.current=G.left+1);const W=je.current;if(G.bottom>=I.bottom-6){const R=y.current;let L=R.findIndex(ue=>ue.id===p)+1;for(;L<R.length&&(!V(R[L])||R[L].isStreaming);)L++;if(L<R.length){l.preventDefault(),ye(R[L].id,W,"top");return}}}}}if((l.key==="ArrowLeft"||l.key==="ArrowRight")&&w?.isContentEditable&&V(u)){const M=ve(w);if(M&&M.start===M.end){const $=y.current,K=$.findIndex(D=>D.id===p);if(l.key==="ArrowLeft"&&M.start===0){let D=K-1;for(;D>=0&&(!V($[D])||$[D].isStreaming);)D--;if(D>=0){l.preventDefault(),ye($[D].id,Number.POSITIVE_INFINITY,"bottom");return}}if(l.key==="ArrowRight"){const D=u.spans.reduce((I,G)=>I+G.text.length,0);if(M.start>=D){let I=K+1;for(;I<$.length&&(!V($[I])||$[I].isStreaming);)I++;if(I<$.length){l.preventDefault(),ye($[I].id,Number.NEGATIVE_INFINITY,"top");return}}}}}const P=ce(u);if(l.key==="Enter"&&P.enterBehavior==="newBlock"&&V(u)&&w?.isContentEditable){l.preventDefault();const M=ve(w),$=M?.start??0,K=M?.end??$;if(u.type==="list"&&ae(u)){_(u.id,"paragraph"),ee(u.id,0,0);return}const D=mt(u.spans,$,K),[I,G]=pt(D,$),W=y.current,U=W.findIndex(ke=>ke.id===u.id),R=Ie(10);fe.current.add(u.id),fe.current.add(R);let ne=u.type,L;(u.type==="heading"||u.type==="blockquote")&&(ne="paragraph"),u.type==="list"&&(ne="list");const ue=ie({...u,spans:I});ne==="list"&&u.type==="list"?L={id:R,type:"list",spans:G,listType:u.listType,depth:u.depth??0,isStreaming:!1}:L={id:R,type:"paragraph",spans:G,isStreaming:!1},L=ie(L);const we=W.slice(0,U).concat([ue,L],W.slice(U+1));A(we),ee(R,0,0);return}if(l.key==="Tab"&&V(u)&&u.type==="list"&&w?.isContentEditable){l.preventDefault(),l.shiftKey?S(u.id):j(u.id);return}if(l.altKey&&(l.key==="]"||l.key==="[")&&V(u)&&u.type==="list"&&w?.isContentEditable){l.preventDefault(),l.key==="["?S(u.id,!0):j(u.id,!0);return}if(l.key==="Backspace"){if(V(u)&&w?.isContentEditable){const M=ve(w);if(!M||M.start!==M.end)return;if(M.start===0){if(u.type==="heading"){l.preventDefault(),_(u.id,"paragraph"),ee(u.id,0,0);return}if(u.type==="list"){if(l.preventDefault(),(u.depth??0)>0){S(u.id),ee(u.id,0,0);return}_(u.id,"paragraph"),ee(u.id,0,0);return}const $=y.current,K=$.findIndex(I=>I.id===u.id),D=K>0?$[K-1]:null;if(D&&D.type==="divider"){l.preventDefault(),de(D.id),ee(u.id,0,0);return}if(ae(u)){l.preventDefault(),de(u.id);return}if(l.preventDefault(),D&&D.type==="list"&&(u.type==="paragraph"||u.type==="blockquote")){const I=D.spans.reduce((U,R)=>U+R.text.length,0),G={...D,spans:st(D.spans,u.spans)},W=$.slice(0,K-1).concat([ie(G)],$.slice(K+1));A(W),ee(G.id,I,I);return}if(D&&V(D)&&V(u)&&D.type!=="list"){const I=D.spans.reduce((U,R)=>U+R.text.length,0),G={...D,spans:st(D.spans,u.spans)},W=$.slice(0,K-1).concat([ie(G)],$.slice(K+1));A(W),ee(G.id,I,I);return}}return}if(w instanceof HTMLTextAreaElement&&C===0&&ae(u)){l.preventDefault(),de(u.id);return}}},[be,Y,O,b,Ye,rt,z,oe,De,Te,ce,ae,de,A,ee,ye,pt,mt,st,_,j,S]),un=x.useMemo(()=>{const p=[],l=[];for(const u of T)if(u.type==="list"&&u.listType==="ordered"){const k=u.depth??0;for(;l.length<=k;)l.push(0);l[k]=(l[k]||0)+1;for(let w=k+1;w<l.length;w++)l[w]=0;p.push({...u,metadata:{...u.metadata||{},displayNumber:l[k]}})}else u.type==="list"||(l.length=0),p.push(u);return p},[T]),fn=x.useMemo(()=>{if(E.length===0)return"";const p=E[E.length-1],l=E.slice(0,-1),u=l.length>0?ge(l)+`
|
|
17
|
+
`;t.push(i)}t.push(g),o=c.type}return t.join("")}function Vt({markdown:e,isStreaming:t}){const n=x.useRef([]);return{blocks:x.useMemo(()=>{const l=Oe(e,t,n.current);return n.current=l,l},[e,t])}}const Gn={"# ":1,"## ":2,"### ":3,"#### ":4,"##### ":5,"###### ":6},Yn={"- ":"unordered","* ":"unordered","1. ":"ordered"},Et={"\\* ":"* ","\\- ":"- ","\\> ":"> "};function zt({onBlockTypeChange:e}={}){const t=x.useCallback(l=>{for(const[o,c]of Object.entries(Et))if(l===o)return c;return null},[]),n=x.useCallback(l=>{for(const c of Object.keys(Et))if(l===c)return null;for(const[c,g]of Object.entries(Gn))if(l.startsWith(c))return{newType:"heading",newContent:l.slice(c.length),level:g};for(const[c,g]of Object.entries(Yn))if(l.startsWith(c))return{newType:"list",newContent:l.slice(c.length),listType:g};if(l.startsWith("> "))return{newType:"blockquote",newContent:l.slice(2)};if(l==="---"||l==="***"||l==="___")return{newType:"divider",newContent:""};const o=l.match(/^```([\w-]*)\s$/);return o?{newType:"code",newContent:"",language:o[1]||"text"}:null},[]),r=x.useCallback((l,o,c,g)=>{if(l.key===" "){const i=c.slice(0,g)+" ",h=c.slice(g);if(t(i)&&o.type==="paragraph")return!1;const m=n(i);if(m&&o.type==="paragraph"){l.preventDefault();const f=m.newContent+h;return e?.(o.id,m.newType,f,{level:m.level,listType:m.listType,language:m.language}),!0}if(m&&m.newType==="list"&&o.type==="list"&&m.listType!==o.listType){l.preventDefault();const f=m.newContent+h;return e?.(o.id,"list",f,{listType:m.listType,depth:o.depth??0}),!0}}if(l.key==="Enter"){const i=c.slice(0,g)+" ",h=n(i);if(h?.newType==="code"&&o.type==="paragraph"){l.preventDefault();const u=c.slice(g);return e?.(o.id,h.newType,u,{language:h.language}),!0}}return l.key==="Backspace"&&g===0&&o.type!=="paragraph"&&c===""?(l.preventDefault(),e?.(o.id,"paragraph",""),!0):(l.key==="Enter"&&o.type==="code",!1)},[n,t,e]);return{detectShortcut:n,detectEscape:t,handleKeyDown:r}}function Jn(e){const{bold:t,italic:n,strike:r}=e;return t&&n&&r?"boldItalicStrikethrough":t&&n?"boldItalic":t&&r?"boldStrikethrough":n&&r?"italicStrikethrough":t?"bold":n?"italic":r?"strikethrough":"plain"}function Zn(e){const t={bold:!1,italic:!1,strike:!1};let n,r=!1,l=!1,o=!1,c=e.parentNode;for(;c&&c.nodeType===Node.ELEMENT_NODE;){const g=c,i=g.tagName.toLowerCase();i==="code"&&(r=!0),i==="a"&&(l=!0,n=g.getAttribute("href")||""),(i==="strong"||i==="b")&&(t.bold=!0),(i==="em"||i==="i")&&(t.italic=!0),(i==="s"||i==="del"||i==="strike")&&(t.strike=!0),i==="span"&&g.getAttribute("data-math")==="1"&&(o=!0),c=c.parentNode}return r?{style:"code"}:o?{style:"math"}:l?{style:"link",href:n}:{style:Jn(t)}}function gt(e){let t=e.parentNode;for(;t&&t.nodeType===Node.ELEMENT_NODE;){if(t.getAttribute("data-math-delim")==="1")return!0;t=t.parentNode}return!1}function Ut(e,t){let n=e.nodeType===Node.ELEMENT_NODE?e:e.parentElement;for(;n&&n!==t;){if(n.getAttribute("data-math")==="1")return n;n=n.parentElement}return null}function er(e){const t=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null);let n=null,r;for(;r=t.nextNode();)gt(r)||(n=r);return n}function tr(e,t,n){if(t.nodeType===Node.ELEMENT_NODE){const l=t;if(n>0)return l.childNodes[n-1]||null}if(t.nodeType===Node.TEXT_NODE&&n>0)return null;let r=t;for(;r&&r!==e;){if(r.previousSibling)return r.previousSibling;r=r.parentNode}return null}function Bt(e,t,n){const r=Ut(t,e);if(r){if(t.nodeType===Node.ELEMENT_NODE&&t===r){const o=r.childNodes.length;if(n>=o)return"afterMath"}return"insideMath"}const l=tr(e,t,n);if(l&&l.nodeType===Node.ELEMENT_NODE&&l.getAttribute("data-math")==="1")return"afterMath"}function _e(e){const t=[],n=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null);let r;for(;r=n.nextNode();){const l=r.textContent||"";if(!l||gt(r))continue;const o=Zn(r);if(o.style==="link"){t.push({text:l,style:"link",href:o.href});continue}if(o.style==="math"){t.push({text:l,style:"math"});continue}t.push({text:l,style:o.style})}return Ie(t)}function be(e){const t=window.getSelection();if(!t||t.rangeCount===0)return null;const n=t.getRangeAt(0);if(!e.contains(n.startContainer)||!e.contains(n.endContainer))return null;const r=At(e,n.startContainer,n.startOffset),l=At(e,n.endContainer,n.endOffset);return r===null||l===null?null:{start:r,end:l,startAffinity:Bt(e,n.startContainer,n.startOffset),endAffinity:Bt(e,n.endContainer,n.endOffset)}}function At(e,t,n){const r=document.createRange();try{r.selectNodeContents(e),r.setEnd(t,n)}catch{return null}const l=r.cloneContents();return l.querySelectorAll?.('[data-math-delim="1"]').forEach(o=>o.remove()),l.textContent?.length??0}function Be(e,t,n,r={}){const l=Rt(e,t,r.startAffinity),o=Rt(e,n,r.endAffinity);if(!l||!o)return;const c=document.createRange();c.setStart(l.node,l.offset),c.setEnd(o.node,o.offset);const g=window.getSelection();g?.removeAllRanges(),g?.addRange(c)}function Rt(e,t,n){const r=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null);let l=0,o;for(;o=r.nextNode();){if(gt(o))continue;const c=o.textContent?.length||0;if(l+c>t)return{node:o,offset:Math.max(0,t-l)};if(l+c===t){const g=Ut(o,e);if(g){const i=er(g);if(i&&i===o&&n==="afterMath"){const h=g.parentNode;if(h){const u=Array.prototype.indexOf.call(h.childNodes,g);if(u!==-1)return{node:h,offset:u+1}}}}return{node:o,offset:c}}l+=c}return{node:e,offset:e.childNodes.length}}const nr=220,Lt=8;function rr({containerRef:e,onFormat:t,isMathSelection:n}){const[r,l]=x.useState(!1),[o,c]=x.useState({top:0,left:0}),[g,i]=x.useState(!1),h=x.useRef(null),u=x.useRef(!1),m=x.useCallback(()=>{const M=window.getSelection();if(!M||M.isCollapsed||!M.rangeCount){u.current||(l(!1),i(!1));return}const T=M.getRangeAt(0);if(!e.current?.contains(T.commonAncestorContainer)){l(!1),i(!1);return}i(!!n?.(T));const v=T.getBoundingClientRect(),E=e.current?.getBoundingClientRect();if(!E)return;const U=h.current?.offsetWidth||nr,G=h.current?.offsetHeight||40,ae=v.left+v.width/2-E.left,le=v.top-E.top,b=v.bottom-E.top;let w=ae-U/2;const B=Lt,P=E.width-U-Lt;w=Math.max(B,Math.min(P,w));let Q=le-G-8;Q<0&&(Q=b+8),c({top:Q,left:w}),l(!0)},[e,n]);x.useEffect(()=>{const M=()=>{requestAnimationFrame(m)},T=()=>{r&&requestAnimationFrame(m)};return document.addEventListener("selectionchange",M),window.addEventListener("scroll",T,!0),()=>{document.removeEventListener("selectionchange",M),window.removeEventListener("scroll",T,!0)}},[m,r]),x.useEffect(()=>{const M=T=>{const v=T.target;!v.closest(".floating-toolbar")&&!e.current?.contains(v)&&l(!1)};return document.addEventListener("mousedown",M),()=>{document.removeEventListener("mousedown",M)}},[e]);const f=x.useCallback(M=>{g&&M!=="math"||t(M)},[t,g]);return r?s.jsxs("div",{ref:h,className:"floating-toolbar absolute z-50 flex items-center gap-1 px-2 py-1.5 bg-notion-bg-elevated rounded-lg shadow-lg border border-notion-border whitespace-nowrap",style:{top:o.top,left:o.left},onMouseDown:M=>M.preventDefault(),onMouseEnter:()=>{u.current=!0},onMouseLeave:()=>{u.current=!1},children:[s.jsx(ze,{onClick:()=>f("bold"),disabled:g,children:s.jsx(sr,{})}),s.jsx(ze,{onClick:()=>f("italic"),disabled:g,children:s.jsx(or,{})}),s.jsx(ze,{onClick:()=>f("strikethrough"),disabled:g,children:s.jsx(ir,{})}),s.jsx(ze,{onClick:()=>f("code"),disabled:g,children:s.jsx(ar,{})}),s.jsx(ze,{onClick:()=>f("math"),disabled:!1,children:s.jsx(cr,{})}),s.jsx("div",{className:"w-px h-5 bg-notion-border mx-1"}),s.jsx(ze,{onClick:()=>f("link"),disabled:g,children:s.jsx(lr,{})})]}):null}function ze({children:e,onClick:t,disabled:n}){return s.jsx("button",{type:"button",onClick:t,disabled:n,className:`p-1.5 rounded text-notion-text-secondary transition-colors ${n?"opacity-40 cursor-not-allowed":"hover:bg-notion-bg-hover hover:text-notion-text"}`,children:e})}function sr(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2.5",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M6 4h8a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"}),s.jsx("path",{d:"M6 12h9a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"})]})}function or(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("line",{x1:"19",y1:"4",x2:"10",y2:"4"}),s.jsx("line",{x1:"14",y1:"20",x2:"5",y2:"20"}),s.jsx("line",{x1:"15",y1:"4",x2:"9",y2:"20"})]})}function ir(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M16 4H9a3 3 0 0 0-3 3v1a3 3 0 0 0 3 3h6a3 3 0 0 1 3 3v1a3 3 0 0 1-3 3H8"}),s.jsx("line",{x1:"4",y1:"12",x2:"20",y2:"12"})]})}function ar(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("polyline",{points:"16 18 22 12 16 6"}),s.jsx("polyline",{points:"8 6 2 12 8 18"})]})}function lr(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"}),s.jsx("path",{d:"M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"})]})}function cr(){return s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M4 4h6l4 16h6"}),s.jsx("path",{d:"M4 12h16"})]})}function dr({isOpen:e,onSubmit:t,onCancel:n,initialHref:r=""}){const[l,o]=x.useState(r),c=x.useRef(null),g=x.useRef(null);x.useEffect(()=>{e&&(o(r),requestAnimationFrame(()=>{c.current?.focus(),c.current?.select()}))},[e,r]);const i=x.useCallback(u=>{u.preventDefault();const m=l.trim();if(m){const f=m.match(/^https?:\/\//)?m:`https://${m}`;t(f)}},[l,t]),h=x.useCallback(u=>{u.key==="Escape"&&(u.preventDefault(),u.stopPropagation(),n())},[n]);return e?s.jsxs("div",{className:"link-modal fixed inset-0 z-[100] flex items-center justify-center",onKeyDown:h,children:[s.jsx("div",{className:"absolute inset-0 bg-black/20 backdrop-blur-[1px]",onClick:n}),s.jsxs("div",{ref:g,className:"relative bg-notion-bg-elevated rounded-xl shadow-2xl border border-notion-border/60 w-full max-w-md mx-4 overflow-hidden animate-in fade-in zoom-in-95 duration-150",style:{boxShadow:"0 25px 50px -12px rgba(0, 0, 0, 0.25), 0 0 0 1px rgba(0, 0, 0, 0.05)"},children:[s.jsx("div",{className:"px-5 pt-5 pb-3",children:s.jsxs("div",{className:"flex items-center gap-3",children:[s.jsx("div",{className:"w-9 h-9 rounded-lg bg-gradient-to-br from-blue-500 to-blue-600 flex items-center justify-center shadow-sm",children:s.jsx(ur,{className:"w-4.5 h-4.5 text-white"})}),s.jsxs("div",{children:[s.jsx("h3",{className:"text-[15px] font-semibold text-notion-text tracking-[-0.01em]",children:"Insert link"}),s.jsx("p",{className:"text-xs text-notion-text-secondary mt-0.5",children:"Enter a URL for the selected text"})]})]})}),s.jsxs("form",{onSubmit:i,className:"px-5 pb-5",children:[s.jsxs("div",{className:"relative",children:[s.jsx("div",{className:"absolute inset-y-0 left-0 pl-3.5 flex items-center pointer-events-none",children:s.jsx(fr,{className:"w-4 h-4 text-notion-text-secondary"})}),s.jsx("input",{ref:c,type:"text",value:l,onChange:u=>o(u.target.value),placeholder:"https://example.com",className:"w-full pl-10 pr-4 py-2.5 text-sm bg-notion-hover/50 border border-notion-border/60 rounded-lg text-notion-text placeholder:text-notion-text-secondary/60 focus:outline-none focus:ring-2 focus:ring-blue-500/30 focus:border-blue-500/50 transition-all duration-150",autoComplete:"url",spellCheck:!1})]}),s.jsxs("div",{className:"flex items-center justify-end gap-2 mt-4",children:[s.jsx("button",{type:"button",onClick:n,className:"px-3.5 py-1.5 text-sm font-medium text-notion-text-secondary hover:text-notion-text hover:bg-notion-hover rounded-lg transition-colors duration-150",children:"Cancel"}),s.jsx("button",{type:"submit",disabled:!l.trim(),className:"px-4 py-1.5 text-sm font-medium text-white bg-blue-500 hover:bg-blue-600 disabled:opacity-40 disabled:cursor-not-allowed rounded-lg shadow-sm transition-all duration-150 hover:shadow",children:"Apply"})]})]}),s.jsx("div",{className:"px-5 py-2.5 bg-notion-hover/30 border-t border-notion-border/40",children:s.jsxs("div",{className:"flex items-center justify-between text-[11px] text-notion-text-secondary",children:[s.jsxs("span",{className:"flex items-center gap-1.5",children:[s.jsx("kbd",{className:"px-1.5 py-0.5 bg-notion-bg-elevated rounded border border-notion-border/60 font-mono text-[10px] shadow-sm",children:"Enter"}),s.jsx("span",{children:"to apply"})]}),s.jsxs("span",{className:"flex items-center gap-1.5",children:[s.jsx("kbd",{className:"px-1.5 py-0.5 bg-notion-bg-elevated rounded border border-notion-border/60 font-mono text-[10px] shadow-sm",children:"Esc"}),s.jsx("span",{children:"to cancel"})]})]})})]})]}):null}function ur({className:e}){return s.jsxs("svg",{className:e,width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"}),s.jsx("path",{d:"M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"})]})}function fr({className:e}){return s.jsxs("svg",{className:e,width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("circle",{cx:"12",cy:"12",r:"10"}),s.jsx("path",{d:"M2 12h20"}),s.jsx("path",{d:"M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"})]})}const hr=400;function nt({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,className:c="",editingClassName:g="",viewClassName:i="",placeholder:h="Type something...",renderWrapper:u,readonly:m=!1}){const f=x.useRef(null),M=x.useRef(null),T=x.useRef(null),v=x.useRef(e.spans),E=x.useRef(null),[U,G]=x.useState(!1),ae=x.useRef(null),le=x.useRef(null),b=x.useCallback(()=>t?f.current:M.current,[t]);x.useEffect(()=>()=>{E.current&&clearTimeout(E.current)},[]),x.useEffect(()=>{if(t&&f.current){f.current.innerHTML=Qe(e.spans),v.current=e.spans,f.current.focus();const L=le.current;if(le.current=null,L)Be(f.current,L.start,L.end,{startAffinity:L.startAffinity,endAffinity:L.endAffinity});else{const X=document.createRange(),Z=window.getSelection();X.selectNodeContents(f.current),X.collapse(!1),Z?.removeAllRanges(),Z?.addRange(X)}}},[t]),x.useEffect(()=>{if(!t||!f.current)return;const L=f.current,X=Qe(e.spans),Z=L.innerHTML;if(X===Z)return;const F=be(L);L.innerHTML=X,F&&Be(L,F.start,F.end,{startAffinity:F.startAffinity,endAffinity:F.endAffinity}),v.current=e.spans},[e.spans,t]);const w=x.useCallback(()=>{if(!f.current)return;const L=be(f.current),X=_e(f.current);v.current=X,n(X);const F=Array.from(f.current.querySelectorAll('[data-math="1"]')).every(de=>de.querySelectorAll('[data-math-delim="1"]').length===2);E.current&&clearTimeout(E.current);const H=()=>{if(!f.current)return;const de=_e(f.current),ue=Qe(de),fe=f.current.innerHTML;ue!==fe&&(f.current.innerHTML=ue,L&&Be(f.current,L.start,L.end,{startAffinity:L.startAffinity,endAffinity:L.endAffinity}))};F?E.current=setTimeout(H,hr):H()},[n]),B=x.useCallback(L=>{if(!L.relatedTarget?.closest(".floating-toolbar")&&!L.relatedTarget?.closest(".link-modal")){if(f.current){const X=_e(f.current);n(X)}l()}},[n,l]),P=x.useCallback(()=>{!t&&!m&&r()},[t,r,m]),Q=x.useCallback(L=>{if(!M.current)return;const X=M.current,Z=(ce,pe)=>{const ie=document;if(typeof ie.caretRangeFromPoint=="function")return ie.caretRangeFromPoint(ce,pe)||null;if(typeof ie.caretPositionFromPoint=="function"){const K=ie.caretPositionFromPoint(ce,pe);if(!K)return null;const j=document.createRange();return j.setStart(K.offsetNode,K.offset),j.collapse(!0),j}return null},F=window.getSelection();let H=null;if(F&&F.rangeCount>0&&(H=F.getRangeAt(0)),(!H||!X.contains(H.startContainer)||!X.contains(H.endContainer))&&L){const ce=Z(L.clientX,L.clientY);ce&&(H=ce)}if(!H||!X.contains(H.startContainer)||!X.contains(H.endContainer))return;const de=ce=>(ce.nodeType===Node.ELEMENT_NODE?ce:ce.parentElement)?.closest("[data-span-start]"),ue=(ce,pe,ie)=>{if(ce.nodeType!==Node.ELEMENT_NODE)return null;const K=ce,j=K.childNodes[pe]||null,C=pe>0&&K.childNodes[pe-1]||null,ee=we=>we&&we.nodeType===Node.ELEMENT_NODE?we.closest("[data-span-start]"):null;return ie==="start"?ee(j)||ee(C):ee(C)||ee(j)},fe=(ce,pe,ie)=>{let K=de(ce);if(K||(K=ue(ce,pe,ie)),!K)return null;const j=K.getAttribute("data-span-start"),C=K.getAttribute("data-span-len");if(!j||!C)return null;const ee=Number(j),we=Number(C);if(K.getAttribute("data-span-style")==="math")return{pos:ie==="start"?ee:ee+we,affinity:"insideMath"};try{const Ce=document.createRange();Ce.selectNodeContents(K),Ce.setEnd(ce,pe);const Pe=Ce.toString().length;return{pos:ee+Pe}}catch{return{pos:ie==="start"?ee:ee+we}}},ke=de(H.startContainer)||ue(H.startContainer,H.startOffset,"start");if(H.collapsed&&ke?.getAttribute("data-span-style")==="math"){const ce=Number(ke.getAttribute("data-span-start")||"0");le.current={start:ce,end:ce,startAffinity:"insideMath",endAffinity:"insideMath"};return}const oe=fe(H.startContainer,H.startOffset,"start"),se=fe(H.endContainer,H.endOffset,"end");!oe||!se||(le.current={start:oe.pos,end:se.pos,startAffinity:oe.affinity,endAffinity:se.affinity})},[]),O=x.useCallback((L,X)=>{const Z=b();if(!Z)return;const F=be(Z);if(!(!F||F.start===F.end))if(t&&f.current){const H=_e(f.current),de=tt(H,F.start,F.end,L,X);f.current.innerHTML=Qe(de),Be(f.current,F.start,F.end,{startAffinity:F.startAffinity,endAffinity:F.endAffinity}),v.current=de,n(de)}else{const H=tt(e.spans,F.start,F.end,L,X);n(H),window.getSelection()?.removeAllRanges()}},[t,e.spans,n,b]),ne=x.useCallback(L=>{if((L.key==="ArrowRight"||L.key==="ArrowLeft")&&f.current){const X=f.current,Z=window.getSelection();if(Z&&Z.rangeCount>0){const F=Z.getRangeAt(0);if(F.collapsed&&X.contains(F.startContainer)){const H=K=>{let j=K.nodeType===Node.ELEMENT_NODE?K:K.parentElement;for(;j&&j!==X;){if(j.getAttribute("data-math")==="1")return j;j=j.parentElement}return null},de=K=>{let j=K.parentNode;for(;j&&j.nodeType===Node.ELEMENT_NODE;){if(j.getAttribute("data-math-delim")==="1")return!0;j=j.parentNode}return!1},ue=K=>{const j=document.createTreeWalker(K,NodeFilter.SHOW_TEXT,null);let C;for(;C=j.nextNode();)if(!de(C))return C;return null},fe=K=>{const j=document.createTreeWalker(K,NodeFilter.SHOW_TEXT,null);let C=null,ee;for(;ee=j.nextNode();)de(ee)||(C=ee);return C},ke=K=>{const j=K.parentNode;if(!j)return;const C=Array.prototype.indexOf.call(j.childNodes,K);if(C===-1)return;const ee=document.createRange();ee.setStart(j,C+1),ee.collapse(!0),Z.removeAllRanges(),Z.addRange(ee)},oe=K=>{const j=K.parentNode;if(!j)return;const C=Array.prototype.indexOf.call(j.childNodes,K);if(C===-1)return;const ee=document.createRange();ee.setStart(j,C),ee.collapse(!0),Z.removeAllRanges(),Z.addRange(ee)},se=K=>{const j=ue(K),C=document.createRange();j?C.setStart(j,0):C.setStart(K,1),C.collapse(!0),Z.removeAllRanges(),Z.addRange(C)},ce=K=>{const j=fe(K),C=document.createRange();j?C.setStart(j,j.textContent?.length??0):C.setStart(K,1),C.collapse(!0),Z.removeAllRanges(),Z.addRange(C)},pe=()=>{const K=F.startContainer,j=F.startOffset;if(K.nodeType===Node.ELEMENT_NODE){const ee=K;if(j>0)return ee.childNodes[j-1]||null}if(K.nodeType===Node.TEXT_NODE&&j>0)return null;let C=K;for(;C&&C!==X;){if(C.previousSibling)return C.previousSibling;C=C.parentNode}return null},ie=()=>{const K=F.startContainer,j=F.startOffset;if(K.nodeType===Node.ELEMENT_NODE)return K.childNodes[j]||null;if(K.nodeType===Node.TEXT_NODE){const ee=K.textContent||"";if(j<ee.length)return null}let C=K;for(;C&&C!==X;){if(C.nextSibling)return C.nextSibling;C=C.parentNode}return null};if(L.key==="ArrowRight"){const K=H(F.startContainer);if(K){const C=fe(K);if(C&&F.startContainer===C&&F.startOffset===(C.textContent?.length??0)){L.preventDefault(),ke(K);return}}const j=ie();if(j&&j.nodeType===Node.ELEMENT_NODE){const C=j;if(C.getAttribute("data-math")==="1"){L.preventDefault(),se(C);return}}}if(L.key==="ArrowLeft"){const K=pe();if(K&&K.nodeType===Node.ELEMENT_NODE){const C=K;if(C.getAttribute("data-math")==="1"){L.preventDefault(),ce(C);return}}const j=H(F.startContainer);if(j){const C=ue(j);if(C&&F.startContainer===C&&F.startOffset===0){L.preventDefault(),oe(j);return}}}}}}if((L.metaKey||L.ctrlKey)&&f.current)switch(L.key.toLowerCase()){case"b":L.preventDefault(),O("bold");return;case"i":L.preventDefault(),O("italic");return;case"e":L.preventDefault(),O("code");return;case"m":L.preventDefault(),O("math");return;case"k":L.preventDefault();const X=be(f.current);X&&X.start!==X.end&&(ae.current=X,G(!0));return}if(L.key==="Escape"){if(f.current){const X=_e(f.current);n(X)}l();return}o(L)},[O,n,l,o]),Y=x.useCallback(L=>{const X=b();if(X){if(L==="link"){const Z=be(X);Z&&Z.start!==Z.end&&(ae.current=Z,G(!0));return}O(L)}},[O,b]),ge=x.useCallback(L=>{if(G(!1),!ae.current)return;const X=ae.current;if(ae.current=null,t&&f.current){const Z=_e(f.current),F=tt(Z,X.start,X.end,"link",{href:L});f.current.innerHTML=Qe(F),Be(f.current,X.start,X.end,{startAffinity:X.startAffinity,endAffinity:X.endAffinity}),v.current=F,n(F),f.current.focus()}else{const Z=tt(e.spans,X.start,X.end,"link",{href:L});n(Z),window.getSelection()?.removeAllRanges()}},[t,e.spans,n]),Ne=x.useCallback(()=>{G(!1),ae.current=null,t&&f.current?.focus()},[t]),Se=x.useCallback(L=>{if(t&&f.current){const X=be(f.current);if(!X||X.start===X.end)return!1;const Z=_e(f.current);let F=0;for(const H of Z){const de=F,ue=F+H.text.length;if(ue>X.start&&de<X.end&&H.style==="math")return!0;F=ue}return!1}if(!t&&M.current){const X=M.current,Z=Array.from(X.querySelectorAll('[data-span-style="math"]'));for(const F of Z)try{if(L.intersectsNode(F))return!0}catch{}}return!1},[t]),Me=x.useCallback(L=>{const Z=L.nativeEvent.inputType,F=Z==="deleteContentBackward"||Z==="deleteContentForward",H=Z==="insertText"||Z==="insertCompositionText"||Z==="insertFromPaste";if(!F&&!H)return;const de=f.current;if(!de)return;const ue=window.getSelection();if(!ue||ue.rangeCount===0)return;const fe=ue.getRangeAt(0),ke=j=>{let C=j.nodeType===Node.ELEMENT_NODE?j:j.parentElement;for(;C&&C!==de;){if(C.getAttribute("data-math")==="1")return C;C=C.parentElement}return null},oe=j=>{const C=j.parentNode;if(!C)return;const ee=Array.prototype.indexOf.call(C.childNodes,j);if(ee===-1)return;const we=document.createRange();we.setStart(C,ee+1),we.collapse(!0),ue.removeAllRanges(),ue.addRange(we)},se=j=>{if(!j)return!1;if(j.nodeType===Node.ELEMENT_NODE){const ee=j;return ee.getAttribute("data-math-delim")==="1"?!0:!!ee.querySelector('[data-math-delim="1"]')}const C=j.parentElement;return!!C&&C.getAttribute("data-math-delim")==="1"},ce=j=>{let C=j;for(;C&&C!==de;){if(C.previousSibling)return C.previousSibling;C=C.parentNode}return null},pe=j=>{let C=j;for(;C&&C!==de;){if(C.nextSibling)return C.nextSibling;C=C.parentNode}return null},ie=(j,C)=>{if(j.nodeType===Node.ELEMENT_NODE){const ee=j;return C>0?ee.childNodes[C-1]||null:ce(j)}return j.nodeType===Node.TEXT_NODE&&C>0?null:ce(j)},K=(j,C)=>{if(j.nodeType===Node.ELEMENT_NODE)return j.childNodes[C]||pe(j);if(j.nodeType===Node.TEXT_NODE){const ee=j.textContent||"";return C<ee.length?null:pe(j)}return pe(j)};if(!fe.collapsed){fe.cloneContents().querySelector?.('[data-math-delim="1"]')&&F&&L.preventDefault();return}if(H){const j=ke(fe.startContainer);if(j&&fe.startContainer===j&&fe.startOffset>=j.childNodes.length){oe(j);return}}if(F){const j=Z==="deleteContentBackward"?ie(fe.startContainer,fe.startOffset):K(fe.startContainer,fe.startOffset);se(j)&&L.preventDefault()}},[]),Ye=e.spans.length===0||e.spans.length===1&&e.spans[0].text==="",Je=s.jsx("div",{ref:f,className:`outline-none cursor-text ${c} ${g}`,contentEditable:!0,suppressContentEditableWarning:!0,"data-placeholder":h,"data-empty":Ye?"1":void 0,onMouseDown:L=>{const Z=L.target.closest?.('[data-math-delim="1"]');if(!Z||!f.current)return;const F=Z.closest?.('[data-math="1"]');if(!F)return;L.preventDefault();const H=window.getSelection();if(!H)return;const de=F.firstElementChild===Z,ue=document.createTreeWalker(F,NodeFilter.SHOW_TEXT,null);let fe=null,ke=null,oe;for(;oe=ue.nextNode();)oe.parentElement?.getAttribute("data-math-delim")!=="1"&&(fe||(fe=oe),ke=oe);const se=document.createRange();de?fe?se.setStart(fe,0):se.setStart(F,1):ke?se.setStart(ke,ke.textContent?.length??0):se.setStart(F,1),se.collapse(!0),H.removeAllRanges(),H.addRange(se)},onBeforeInput:Me,onInput:w,onBlur:B,onKeyDown:ne},"editing"),Te=s.jsxs("div",{ref:M,className:`outline-none ${m?"cursor-default":"cursor-text"} ${c} ${i}`,onMouseUp:m?void 0:Q,onClick:m?void 0:P,children:[e.spans.length>0?mt(e.spans):!m&&s.jsx("span",{className:"text-notion-text-secondary notion-placeholder",children:h}),e.isStreaming&&s.jsx("span",{className:"inline-block w-0.5 h-4 bg-notion-text animate-pulse ml-0.5"})]},"viewing"),Ae=t?u?u(Je,!0):Je:u?u(Te,!1):Te;return s.jsxs("div",{ref:T,className:"relative",children:[!m&&s.jsxs(s.Fragment,{children:[s.jsx(rr,{containerRef:T,onFormat:Y,isMathSelection:Se}),s.jsx(dr,{isOpen:U,onSubmit:ge,onCancel:Ne})]}),Ae]})}function Qt({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,readonly:c=!1}){return s.jsx(nt,{block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,className:"notion-paragraph text-notion-text leading-relaxed min-h-[1.5em]",placeholder:"Type '/' for commands...",readonly:c})}const Dt={1:"text-3xl font-bold",2:"text-2xl font-bold",3:"text-xl font-semibold",4:"text-lg font-semibold",5:"text-base font-semibold",6:"text-sm font-semibold"};function Gt({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,readonly:c=!1}){const g=e.level||1,i=Dt[g]||Dt[1];return s.jsx(nt,{block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,className:`notion-heading ${i} text-notion-text`,placeholder:`Heading ${g}`,readonly:c,renderWrapper:(h,u)=>s.jsx("div",{role:"heading","aria-level":g,children:h})})}function Yt(e){const{block:t,isEditing:n,onContentChange:r,onStartEdit:l,onEndEdit:o,onKeyDown:c,readonly:g=!1}=e,i=x.useRef(null),[h,u]=x.useState(null);x.useEffect(()=>{let v=!1;return import("@streamdown/code").then(E=>{v||u(E.code)}),()=>{v=!0}},[]),x.useEffect(()=>{n&&i.current&&(i.current.focus(),i.current.selectionStart=i.current.value.length,i.current.selectionEnd=i.current.value.length)},[n]);const m=v=>{r(v.target.value)},f=()=>{o()},M=v=>{if(v.key==="Tab"){v.preventDefault();const E=v.currentTarget,U=E.selectionStart,G=E.selectionEnd,ae=E.value,le=ae.substring(0,U)+" "+ae.substring(G);r(le),setTimeout(()=>{E.selectionStart=E.selectionEnd=U+2},0);return}if(v.key==="Escape"){o();return}c(v)},T="```"+(t.language||"")+`
|
|
18
|
+
`+t.content+"\n```";return n?s.jsxs("div",{className:"notion-code-block rounded-md bg-notion-code-bg border border-notion-border overflow-hidden",children:[s.jsx("div",{className:"flex items-center justify-between px-3 py-1.5 border-b border-notion-border bg-notion-hover",children:s.jsx("span",{className:"text-xs text-notion-text-secondary font-mono",children:t.language||"text"})}),s.jsx("textarea",{ref:i,className:"w-full p-3 font-mono text-sm bg-transparent text-notion-text outline-none resize-none",value:t.content,onChange:m,onBlur:f,onKeyDown:M,rows:Math.max(3,t.content.split(`
|
|
19
|
+
`).length),spellCheck:!1})]}):h?s.jsxs("div",{className:`notion-code-block ${g?"cursor-default":"cursor-text"}`,onClick:g?void 0:l,children:[s.jsx(Ge.Streamdown,{plugins:{code:h},shikiTheme:["min-light","min-dark"],isAnimating:t.isStreaming,children:T}),t.isStreaming&&s.jsx("div",{className:"flex justify-end px-3 py-1",children:s.jsx("span",{className:"inline-block w-0.5 h-4 bg-notion-text animate-pulse"})})]}):s.jsxs("div",{className:"notion-code-block rounded-md bg-notion-code-bg border border-notion-border overflow-hidden",children:[s.jsx("div",{className:"flex items-center justify-between px-3 py-1.5 border-b border-notion-border bg-notion-hover",children:s.jsx("span",{className:"text-xs text-notion-text-secondary font-mono",children:t.language||"text"})}),s.jsx("pre",{className:"p-3 font-mono text-sm text-notion-text-secondary whitespace-pre-wrap",children:t.content})]})}function Jt({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,readonly:o=!1}){const[c,g]=x.useState(!1),[i,h]=x.useState(!0),u=e.content,m=e.alt||"",M=(U=>{const G=(U||"").trim();return G?!!(/^(https?:\/\/)/i.test(G)||/^(data:image\/)/i.test(G)||/^(blob:)/i.test(G)||G.startsWith("/")||G.startsWith("./")||G.startsWith("../")):!1})(u),T=()=>{h(!1),g(!1)},v=()=>{h(!1),g(!0)},E=U=>{n(U.target.value),g(!1),h(!0)};return t?s.jsxs("div",{className:"notion-image-block space-y-2",children:[s.jsx("input",{type:"text",className:"w-full px-3 py-2 text-sm border border-notion-border rounded bg-notion-bg-elevated text-notion-text outline-none focus:border-blue-500",value:u,onChange:E,onBlur:l,placeholder:"Enter image URL...",autoFocus:!0}),u&&!c&&M&&s.jsx("img",{src:u,alt:m||"Preview",className:"max-w-full h-auto rounded",onLoad:T,onError:v}),u&&!M&&s.jsxs("div",{className:"border border-notion-border rounded-lg p-4 bg-notion-hover/30",children:[s.jsx("p",{className:"text-xs text-notion-text-secondary",children:"Image placeholder"}),s.jsx("p",{className:"text-sm text-notion-text truncate",children:u})]})]}):e.content?M?c?s.jsxs("div",{className:`notion-image-block border border-red-200 bg-red-50 rounded-lg p-4 ${o?"cursor-default":"cursor-pointer"}`,onClick:o?void 0:r,children:[s.jsx("p",{className:"text-sm text-red-600",children:"Failed to load image"}),s.jsx("p",{className:"text-xs text-red-400 truncate",children:e.content})]}):s.jsxs("div",{className:`notion-image-block relative group ${o?"":"cursor-pointer"}`,onClick:o?void 0:r,children:[i&&s.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-notion-hover rounded",children:s.jsx("div",{className:"w-6 h-6 border-2 border-notion-text-secondary border-t-transparent rounded-full animate-spin"})}),s.jsx("img",{src:u,alt:m||"Image",className:`max-w-full h-auto rounded ${o?"cursor-default":"cursor-pointer"} transition-opacity ${i?"opacity-0":"opacity-100"}`,onLoad:T,onError:v}),e.isStreaming&&s.jsx("div",{className:"absolute bottom-2 right-2 px-2 py-1 bg-black/50 text-white text-xs rounded",children:"Loading..."})]}):s.jsx("div",{className:`notion-image-block border border-notion-border bg-notion-bg-elevated rounded-lg p-4 ${o?"":"cursor-pointer hover:bg-notion-hover"} transition-colors`,onClick:o?void 0:r,children:s.jsxs("div",{className:"flex items-start gap-3",children:[s.jsx("svg",{className:"w-5 h-5 mt-0.5 text-notion-text-secondary",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:s.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5,d:"M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"})}),s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsx("p",{className:"text-sm text-notion-text",children:m||"Image placeholder"}),s.jsx("p",{className:"text-xs text-notion-text-secondary truncate",children:u})]})]})}):o?null:s.jsx("div",{className:"notion-image-block border-2 border-dashed border-notion-border rounded-lg p-8 text-center cursor-pointer hover:bg-notion-hover transition-colors",onClick:r,children:s.jsxs("div",{className:"text-notion-text-secondary",children:[s.jsx("svg",{className:"w-12 h-12 mx-auto mb-2 opacity-50",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:s.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5,d:"M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"})}),s.jsx("p",{className:"text-sm",children:"Click to add image URL"})]})})}function Zt(e){const{block:t,isEditing:n,onContentChange:r,onStartEdit:l,onEndEdit:o,onKeyDown:c,readonly:g=!1}=e,i=x.useRef(null),[h,u]=x.useState(null);x.useEffect(()=>{let v=!1;return import("@streamdown/math").then(E=>{v||u(E.math)}),()=>{v=!0}},[]),x.useEffect(()=>{n&&i.current&&(i.current.focus(),i.current.selectionStart=i.current.value.length,i.current.selectionEnd=i.current.value.length)},[n]);const m=v=>{r(v.target.value)},f=()=>{o()},M=v=>{if(v.key==="Escape"){o();return}c(v)},T=`$$
|
|
20
|
+
${t.content}
|
|
21
|
+
$$`;return n?s.jsxs("div",{className:"notion-math-block space-y-2",children:[s.jsx("div",{className:"text-xs text-notion-text-secondary mb-1",children:"LaTeX"}),s.jsx("textarea",{ref:i,className:"w-full px-3 py-2 font-mono text-sm border border-notion-border rounded bg-notion-code-bg text-notion-text outline-none focus:border-blue-500 resize-none",value:t.content,onChange:m,onBlur:f,onKeyDown:M,rows:Math.max(2,t.content.split(`
|
|
22
|
+
`).length),placeholder:"Enter LaTeX...",spellCheck:!1}),t.content&&h&&s.jsxs("div",{className:"p-3 bg-notion-bg-elevated border border-notion-border rounded text-notion-text",children:[s.jsx("div",{className:"text-xs text-notion-text-secondary mb-1",children:"Preview"}),s.jsx("div",{className:"text-center",children:s.jsx(Ge.Streamdown,{plugins:{math:h},isAnimating:!1,children:T})})]})]}):t.content?h?s.jsxs("div",{className:`notion-math-block py-4 px-6 bg-notion-code-bg rounded-lg ${g?"cursor-default":"cursor-pointer hover:bg-notion-hover"} transition-colors`,onClick:g?void 0:l,children:[s.jsx("div",{className:"text-center",children:s.jsx(Ge.Streamdown,{plugins:{math:h},isAnimating:t.isStreaming,children:T})}),t.isStreaming&&s.jsx("div",{className:"flex justify-center mt-2",children:s.jsx("span",{className:"inline-block w-0.5 h-4 bg-notion-text animate-pulse"})})]}):s.jsx("div",{className:"notion-math-block py-4 px-6 bg-notion-code-bg rounded-lg",children:s.jsx("div",{className:"text-center font-mono text-sm text-notion-text-secondary",children:t.content})}):g?null:s.jsx("div",{className:"notion-math-block border-2 border-dashed border-notion-border rounded-lg p-6 text-center cursor-pointer hover:bg-notion-hover transition-colors",onClick:l,children:s.jsxs("div",{className:"text-notion-text-secondary",children:[s.jsx("span",{className:"text-2xl mb-2 block",children:"∑"}),s.jsx("p",{className:"text-sm",children:"Click to add LaTeX equation"})]})})}function en({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,theme:c="light",readonly:g=!1}){const i=c==="dark",h=x.useRef(null),[u,m]=x.useState(null);x.useEffect(()=>{let U=!1;return import("@streamdown/mermaid").then(G=>{U||m(G.mermaid)}),()=>{U=!0}},[]),x.useEffect(()=>{t&&h.current&&(h.current.focus(),h.current.selectionStart=h.current.value.length,h.current.selectionEnd=h.current.value.length)},[t]);const f=U=>{n(U.target.value)},M=()=>{l()},T=U=>{if(U.key==="Escape"){l();return}if(U.key==="Tab"){U.preventDefault();const G=U.currentTarget,ae=G.selectionStart,le=G.selectionEnd,b=G.value,w=b.substring(0,ae)+" "+b.substring(le);n(w),setTimeout(()=>{G.selectionStart=G.selectionEnd=ae+2},0);return}o(U)},v="```mermaid\n"+e.content+"\n```";if(t)return s.jsxs("div",{className:"notion-mermaid-block space-y-2",children:[s.jsx("div",{className:"text-xs text-notion-text-secondary mb-1",children:"Mermaid Diagram"}),s.jsx("textarea",{ref:h,className:"w-full px-3 py-2 font-mono text-sm border border-notion-border rounded bg-notion-code-bg text-notion-text outline-none focus:border-blue-500 resize-none",value:e.content,onChange:f,onBlur:M,onKeyDown:T,rows:Math.max(5,e.content.split(`
|
|
23
|
+
`).length),placeholder:"Enter Mermaid diagram code...",spellCheck:!1})]});if(!e.content)return g?null:s.jsx("div",{className:"notion-mermaid-block border-2 border-dashed border-notion-border rounded-lg p-6 text-center cursor-pointer hover:bg-notion-hover transition-colors",onClick:r,children:s.jsxs("div",{className:"text-notion-text-secondary",children:[s.jsx("svg",{className:"w-12 h-12 mx-auto mb-2 opacity-50",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",children:s.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:1.5,d:"M7 21a4 4 0 01-4-4V5a2 2 0 012-2h4a2 2 0 012 2v12a4 4 0 01-4 4zm0 0h12a2 2 0 002-2v-4a2 2 0 00-2-2h-2.343M11 7.343l1.657-1.657a2 2 0 012.828 0l2.829 2.829a2 2 0 010 2.828l-8.486 8.485M7 17h.01"})}),s.jsx("p",{className:"text-sm",children:"Click to add Mermaid diagram"})]})});if(e.isStreaming)return s.jsxs("div",{className:"notion-mermaid-block p-6 bg-notion-code-bg rounded-lg",children:[s.jsxs("div",{className:"flex items-center justify-center space-x-2 text-notion-text-secondary",children:[s.jsx("div",{className:"w-4 h-4 border-2 border-notion-text-secondary border-t-transparent rounded-full animate-spin"}),s.jsx("span",{className:"text-sm",children:"Receiving diagram..."})]}),s.jsx("pre",{className:"mt-3 text-xs text-notion-text-secondary font-mono whitespace-pre-wrap",children:e.content})]});const E=U=>{if(g)return;const G=U.target;G&&(G.closest("button")||G.closest("a")||r())};return u?s.jsx("div",{className:`notion-mermaid-block ${g?"cursor-default":"cursor-pointer"}`,onClick:E,children:s.jsx(Ge.Streamdown,{plugins:{mermaid:u},controls:{mermaid:{fullscreen:!1,download:!0,copy:!0}},mermaid:{config:{theme:i?"dark":"default",themeVariables:{background:"transparent"}}},isAnimating:!1,children:v},i?"dark":"light")}):s.jsx("div",{className:"notion-mermaid-block p-6 bg-notion-code-bg rounded-lg",children:s.jsxs("div",{className:"flex items-center justify-center space-x-2 text-notion-text-secondary",children:[s.jsx("div",{className:"w-4 h-4 border-2 border-notion-text-secondary border-t-transparent rounded-full animate-spin"}),s.jsx("span",{className:"text-sm",children:"Loading diagram renderer..."})]})})}function pr(e,t){return`${e}:${t}`}function mr(e){return e.reduce((t,n)=>t+n.text.length,0)}function gr(e){return e.replace(/\n/g,"<br>").replace(/\|/g,"\\|")}function $t(e){return!e||e.length===0?"":gr(je(e,{forParsing:!0}))}function xr(e,t){const n=Math.max(e.length,...t.map(o=>o.length),1),r=Array.from({length:n},(o,c)=>e[c]??[]),l=t.map(o=>Array.from({length:n},(c,g)=>o[g]??[]));return{header:r,rows:l,cols:n}}function yr({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onExitTable:o,readonly:c=!1}){const g=x.useRef(null),[i,h]=x.useState(null),[u,m]=x.useState(null);x.useEffect(()=>{t&&(i||h({r:0,c:0}))},[i,t]);const f=x.useMemo(()=>xr(e.header||[],e.rows||[]),[e.header,e.rows]),M=x.useMemo(()=>{const b=f.cols;return Array.from({length:b},(B,P)=>e.align?.[P]||"left")},[e.align,f.cols]),T=x.useCallback((b,w,B)=>{const P=f.header.map((ne,Y)=>Y===w&&b===0?B:ne),Q=f.rows.map((ne,Y)=>ne.map((ge,Ne)=>Y===b-1&&Ne===w?B:ge)),O={...e,header:P,rows:Q,align:M};n(ve(O))},[M,e,f.header,f.rows,n]),v=x.useCallback(()=>{try{if(document.execCommand("insertText",!1,`
|
|
24
|
+
`))return}catch{}const b=window.getSelection();if(!b||b.rangeCount===0)return;const w=b.getRangeAt(0);w.deleteContents();const B=document.createTextNode(`
|
|
25
|
+
`);w.insertNode(B),w.setStartAfter(B),w.setEndAfter(B),b.removeAllRanges(),b.addRange(w)},[]),E=x.useCallback((b,w,B,P)=>{const Q=f.cols,O=f.rows.length;let ne=b+B,Y=w+P;if(P!==0&&(Y<0&&(Y=Q-1,ne-=1),Y>=Q&&(Y=0,ne+=1)),ne<0){h({r:0,c:0});return}if(ne>O){o?.(e.id);return}h({r:ne,c:Y})},[e.id,f.cols,f.rows.length,o]),U=x.useCallback((b,w,B)=>{if(B.stopPropagation(),B.key==="Enter"&&B.shiftKey){B.preventDefault(),v();return}if(B.key==="Enter"&&!B.shiftKey){B.preventDefault();const P=f.rows.length,Q=f.cols,O=b===P,ne=w===Q-1;if(O&&ne){o?.(e.id);return}h({r:Math.min(b+1,P),c:w});return}if(B.key==="Tab"){B.preventDefault(),E(b,w,0,B.shiftKey?-1:1);return}if(B.key==="ArrowLeft"){const P=B.currentTarget,Q=be(P);Q&&Q.start===0&&Q.end===0&&(B.preventDefault(),E(b,w,0,-1));return}if(B.key==="ArrowRight"){const P=B.currentTarget,Q=b===0?f.header[w]:f.rows[b-1][w],O=mr(Q),ne=be(P);ne&&ne.start===O&&ne.end===O&&(B.preventDefault(),E(b,w,0,1));return}},[e.id,be,f.cols,f.header,f.rows,v,E,o]),G=(b,w,B,P)=>{const Q=pr(b,w),O=!!(!c&&t&&i?.r===b&&i?.c===w),ne=M[w]||"left",Y=`px-3 py-2 min-w-[140px] border border-notion-border/70 whitespace-pre-wrap break-words align-top ${P?"bg-notion-hover font-semibold":"bg-notion-bg"} ${O?"ring-2 ring-blue-200 ring-inset":!c&&t?"hover:bg-notion-hover/50":""} ${c?"cursor-default":"cursor-text"}`,ge={textAlign:ne,verticalAlign:"top"},Ne=P?"th":"td";if(!O)return s.jsx(Ne,{"data-cell-rc":Q,className:Y,style:ge,scope:P?"col":void 0,onMouseDown:c?void 0:Me=>{Me.preventDefault(),t||r(),h({r:b,c:w})},children:B.length?mt(B):s.jsx("span",{className:"text-notion-text-secondary",children:" "})},Q);const Se={id:`${e.id}:${Q}`,type:"paragraph",spans:B,isStreaming:!1};return s.jsx(Ne,{"data-cell-rc":Q,className:Y,style:ge,scope:P?"col":void 0,children:s.jsx(nt,{block:Se,isEditing:!0,onContentChange:Me=>T(b,w,Me),onStartEdit:()=>{},onEndEdit:()=>{},onKeyDown:Me=>U(b,w,Me),className:"w-full whitespace-pre-wrap break-words",placeholder:"",readonly:c})},Q)};x.useEffect(()=>{if(!u)return;const b=()=>m(null);return window.addEventListener("mousedown",b,!0),window.addEventListener("scroll",b,!0),()=>{window.removeEventListener("mousedown",b,!0),window.removeEventListener("scroll",b,!0)}},[u]);const ae=s.jsx("thead",{children:s.jsx("tr",{children:f.header.map((b,w)=>G(0,w,b,!0))})}),le=s.jsx("tbody",{children:f.rows.length?f.rows.map((b,w)=>s.jsx("tr",{children:b.map((B,P)=>G(w+1,P,B,!1))},`row-${w+1}`)):s.jsx("tr",{children:s.jsx("td",{colSpan:f.cols,className:"px-3 py-4 text-sm text-notion-text-secondary border border-notion-border/70",children:"No rows yet"})})});return s.jsxs("div",{ref:g,className:`notion-table-block relative ${!c&&t?"w-full":"inline-block max-w-full"}`,onClick:c?void 0:()=>{t||r()},onContextMenu:c?void 0:b=>{if(!t)return;const B=b.target?.closest?.("[data-cell-rc]");if(!B)return;b.preventDefault();const P=B.getAttribute("data-cell-rc")||"",[Q,O]=P.split(":"),ne=Number(Q),Y=Number(O);if(!Number.isFinite(ne)||!Number.isFinite(Y))return;const ge=g.current?.getBoundingClientRect(),Ne=ge?b.clientX-ge.left:b.clientX,Se=ge?b.clientY-ge.top:b.clientY;m({x:Ne,y:Se,r:ne,c:Y})},children:[!c&&t&&u&&s.jsx("div",{className:"absolute z-[70]",style:{top:u.y,left:u.x},onMouseDown:b=>b.preventDefault(),children:s.jsxs("div",{className:"rounded-xl border border-notion-border bg-notion-bg-elevated shadow-[0_16px_40px_rgba(15,23,42,0.14)] overflow-hidden w-[220px]",children:[s.jsx("div",{className:"px-3 py-2 border-b border-notion-border bg-gradient-to-b from-notion-hover/70 to-transparent",children:s.jsx("div",{className:"text-[11px] uppercase tracking-[0.12em] text-notion-text-secondary select-none",children:"Table"})}),s.jsxs("div",{className:"p-1",children:[[{id:"row-above",label:"Add row above",run:()=>{const b=u.r,w=Math.max(0,b===0?0:b-1),B=Array.from({length:f.cols},()=>[]),P=f.rows.slice(0,w).concat([B],f.rows.slice(w));n(ve({...e,header:f.header,rows:P,align:M})),h({r:Math.max(1,w+1),c:u.c})}},{id:"row-below",label:"Add row below",run:()=>{const b=Math.max(1,u.r),w=Math.min(f.rows.length,b),B=Array.from({length:f.cols},()=>[]),P=f.rows.slice(0,w).concat([B],f.rows.slice(w));n(ve({...e,header:f.header,rows:P,align:M})),h({r:w+1,c:u.c})}},{id:"col-left",label:"Add column left",run:()=>{const b=u.c,w=f.header.slice();w.splice(b,0,[]);const B=f.rows.map(Q=>{const O=Q.slice();return O.splice(b,0,[]),O}),P=M.slice();P.splice(b,0,"left"),n(ve({...e,header:w,rows:B,align:P})),h({r:u.r,c:b})}},{id:"col-right",label:"Add column right",run:()=>{const w=u.c+1,B=f.header.slice();B.splice(w,0,[]);const P=f.rows.map(O=>{const ne=O.slice();return ne.splice(w,0,[]),ne}),Q=M.slice();Q.splice(w,0,"left"),n(ve({...e,header:B,rows:P,align:Q})),h({r:u.r,c:w})}}].map(b=>s.jsx("button",{type:"button",className:"w-full text-left px-2 py-2 rounded-md hover:bg-notion-hover text-sm",onClick:()=>{b.run(),m(null)},children:b.label},b.id)),s.jsx("div",{className:"my-1 h-px bg-notion-border/70"}),s.jsx("button",{type:"button",className:`w-full text-left px-2 py-2 rounded-md text-sm ${u.r===0||f.rows.length===0?"opacity-50 cursor-not-allowed":"hover:bg-notion-hover"}`,disabled:u.r===0||f.rows.length===0,onClick:()=>{const b=u.r;if(b===0)return;const w=b-1;if(w<0||w>=f.rows.length)return;const B=f.rows.slice(0,w).concat(f.rows.slice(w+1));n(ve({...e,header:f.header,rows:B,align:M})),m(null)},children:"Delete row"}),s.jsx("button",{type:"button",className:`w-full text-left px-2 py-2 rounded-md text-sm ${f.cols<=1?"opacity-50 cursor-not-allowed":"hover:bg-notion-hover"}`,disabled:f.cols<=1,onClick:()=>{if(f.cols<=1)return;const b=u.c,w=f.header.slice(0,b).concat(f.header.slice(b+1)),B=f.rows.map(Q=>Q.slice(0,b).concat(Q.slice(b+1))),P=M.slice(0,b).concat(M.slice(b+1));n(ve({...e,header:w,rows:B,align:P})),m(null)},children:"Delete column"})]})]})}),s.jsxs("div",{className:`rounded-xl border border-notion-border bg-notion-bg overflow-hidden ${!c&&t?"w-full":"inline-block max-w-full"}`,children:[!c&&t&&s.jsxs("div",{className:"flex items-center justify-between px-3 py-2 border-b border-notion-border bg-notion-hover/40",children:[s.jsx("div",{className:"text-xs text-notion-text-secondary uppercase tracking-[0.12em]",children:"Table"}),s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("button",{type:"button",className:"text-xs px-2 py-1 rounded-md border border-notion-border bg-notion-bg hover:bg-notion-hover",onMouseDown:b=>b.preventDefault(),onClick:()=>{const b=Math.max(1,i?.r??1),w=Math.max(0,b-1),B=Array.from({length:f.cols},()=>[]),P=f.rows.slice(0,w).concat([B],f.rows.slice(w));n(ve({...e,header:f.header,rows:P,align:M})),h({r:b,c:i?.c??0})},children:"+ Row Above"}),s.jsx("button",{type:"button",className:"text-xs px-2 py-1 rounded-md border border-notion-border bg-notion-bg hover:bg-notion-hover",onMouseDown:b=>b.preventDefault(),onClick:()=>{const b=Math.max(1,i?.r??1),w=Math.max(1,b),B=Array.from({length:f.cols},()=>[]),P=f.rows.slice(0,w-1).concat([B],f.rows.slice(w-1));n(ve({...e,header:f.header,rows:P,align:M})),h({r:w,c:i?.c??0})},children:"+ Row Below"}),s.jsx("button",{type:"button",className:"text-xs px-2 py-1 rounded-md border border-notion-border bg-notion-bg hover:bg-notion-hover",onMouseDown:b=>b.preventDefault(),disabled:(i?.r??1)===0||f.rows.length===0,onClick:()=>{const b=i?.r??1;if(b===0)return;const w=b-1;if(w<0||w>=f.rows.length)return;const B=f.rows.slice(0,w).concat(f.rows.slice(w+1));n(ve({...e,header:f.header,rows:B,align:M}));const P=Math.min(b,B.length);h({r:Math.max(1,P),c:i?.c??0})},children:"- Row"}),s.jsx("div",{className:"w-px h-6 bg-notion-border/70 mx-0.5"}),s.jsx("button",{type:"button",className:"text-xs px-2 py-1 rounded-md border border-notion-border bg-notion-bg hover:bg-notion-hover",onMouseDown:b=>b.preventDefault(),onClick:()=>{const b=Math.max(0,i?.c??0),w=Math.min(f.cols,b),B=f.header.slice();B.splice(w,0,[]);const P=f.rows.map(O=>{const ne=O.slice();return ne.splice(w,0,[]),ne}),Q=M.slice();Q.splice(w,0,"left"),n(ve({...e,header:B,rows:P,align:Q})),h({r:i?.r??0,c:w})},children:"+ Col Left"}),s.jsx("button",{type:"button",className:"text-xs px-2 py-1 rounded-md border border-notion-border bg-notion-bg hover:bg-notion-hover",onMouseDown:b=>b.preventDefault(),onClick:()=>{const b=i?.c??f.cols,w=Math.min(f.cols,Math.max(0,b+1)),B=f.header.slice();B.splice(w,0,[]);const P=f.rows.map(O=>{const ne=O.slice();return ne.splice(w,0,[]),ne}),Q=M.slice();Q.splice(w,0,"left"),n(ve({...e,header:B,rows:P,align:Q})),h({r:i?.r??0,c:w})},children:"+ Col Right"}),s.jsx("button",{type:"button",className:"text-xs px-2 py-1 rounded-md border border-notion-border bg-notion-bg hover:bg-notion-hover",onMouseDown:b=>b.preventDefault(),disabled:f.cols<=1,onClick:()=>{const b=Math.max(0,Math.min(f.cols-1,i?.c??0));if(f.cols<=1)return;const w=f.header.slice(0,b).concat(f.header.slice(b+1)),B=f.rows.map(O=>O.slice(0,b).concat(O.slice(b+1))),P=M.slice(0,b).concat(M.slice(b+1));n(ve({...e,header:w,rows:B,align:P}));const Q=Math.max(0,Math.min(b,w.length-1));h({r:i?.r??0,c:Q})},children:"- Col"}),s.jsx("button",{type:"button",className:"text-xs px-2 py-1 rounded-md border border-notion-border bg-notion-bg hover:bg-notion-hover",onMouseDown:b=>b.preventDefault(),onClick:()=>l(),children:"Done"})]})]}),s.jsx("div",{className:"overflow-x-auto max-w-full",children:s.jsxs("table",{className:"min-w-max border-collapse",children:[ae,le]})})]})]})}function ve(e){const t=e.header||[],n=e.rows||[],r=Math.max(t.length,...n.map(i=>i.length),1),l=Array.from({length:r},(i,h)=>e.align?.[h]||"left"),o="| "+Array.from({length:r},(i,h)=>$t(t[h])).join(" | ")+" |",c="| "+Array.from({length:r},(i,h)=>{const u=l[h];return u==="center"?":---:":u==="right"?"---:":"---"}).join(" | ")+" |",g=n.map(i=>"| "+Array.from({length:r},(h,u)=>$t(i[u])).join(" | ")+" |");return[o,c,...g].join(`
|
|
26
|
+
`)}function tn({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,readonly:c=!1}){const g=e.listType==="ordered",i=e.depth??0,h=e.metadata?.displayNumber,u=g?`${h??1}.`:"•";return s.jsx(nt,{block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,className:"flex-1",placeholder:"Empty item",readonly:c,renderWrapper:(m,f)=>s.jsxs("div",{className:`notion-list-block flex items-start gap-2 py-0.5 ${c?"cursor-default":"cursor-text"}`,style:{paddingLeft:i*24},children:[s.jsx("span",{className:"text-notion-text select-none flex-shrink-0 w-5",children:u}),m]})})}function nn({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,readonly:c=!1}){return s.jsx(nt,{block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:l,onKeyDown:o,className:"text-notion-text italic leading-relaxed",placeholder:"Empty quote",readonly:c,renderWrapper:(g,i)=>s.jsx("div",{className:`notion-blockquote border-l-4 border-notion-quote-border pl-4 py-0.5 ${c?"cursor-default":"cursor-text"}`,children:g})})}function rn({block:e}){return s.jsxs("div",{className:"notion-divider py-3",children:[s.jsx("hr",{className:"border-t border-notion-border"}),e.isStreaming&&s.jsx("div",{className:"flex justify-center mt-1",children:s.jsx("span",{className:"inline-block w-0.5 h-2 bg-notion-text animate-pulse"})})]})}function br({block:e,isEditing:t,onStartEdit:n,onEndEdit:r,onContentChange:l,onKeyDown:o,hooks:c,onExitTable:g,theme:i="light",readonly:h=!1}){const u=()=>{if(e.type==="paragraph"||e.type==="heading"||e.type==="list"||e.type==="blockquote"){const T={block:e,isEditing:t,onStartEdit:n,onEndEdit:r,onContentChange:l,onKeyDown:o,readonly:h};switch(e.type){case"paragraph":return s.jsx(Qt,{...T});case"heading":return s.jsx(Gt,{...T});case"list":return s.jsx(tn,{...T});case"blockquote":return s.jsx(nn,{...T})}}if(e.type==="table")return s.jsx(yr,{block:e,isEditing:t,onStartEdit:n,onEndEdit:r,onContentChange:l,onKeyDown:o,onExitTable:g,readonly:h});const f={block:e,isEditing:t,onStartEdit:n,onEndEdit:r,onContentChange:l,onKeyDown:o,readonly:h};switch(e.type){case"code":return s.jsx(Yt,{...f,theme:i});case"image":return s.jsx(Jt,{...f});case"math":return s.jsx(Zt,{...f,theme:i});case"mermaid":return s.jsx(en,{...f,theme:i});case"divider":return s.jsx(rn,{...f});default:return null}},m=`notion-block relative py-1 px-1 -mx-1 rounded ${h?"":"hover:bg-notion-hover/50"} transition-colors`;if(c?.renderBlock){const f=c.renderBlock(e,u);if(f!==null)return s.jsx("div",{className:m,"data-block-id":e.id,"data-block-type":e.type,children:f})}return s.jsx("div",{className:m,"data-block-id":e.id,"data-block-type":e.type,children:u()})}function sn(...e){return e.filter(Boolean).join(" ")}const on=Wt.forwardRef(({className:e,children:t,...n},r)=>s.jsxs(He.Root,{ref:r,className:sn("relative overflow-hidden",e),...n,children:[s.jsx(He.Viewport,{className:"h-full w-full rounded-[inherit]",children:t}),s.jsx(an,{}),s.jsx(He.Corner,{})]}));on.displayName=He.Root.displayName;const an=Wt.forwardRef(({className:e,orientation:t="vertical",...n},r)=>s.jsx(He.ScrollAreaScrollbar,{ref:r,orientation:t,className:sn("flex touch-none select-none transition-colors",t==="vertical"&&"h-full w-2.5 border-l border-l-transparent p-[1px]",t==="horizontal"&&"h-2.5 flex-col border-t border-t-transparent p-[1px]",e),...n,children:s.jsx(He.ScrollAreaThumb,{className:"relative flex-1 rounded-full bg-notion-text-secondary/40"})}));an.displayName=He.ScrollAreaScrollbar.displayName;function wr(e,t,n){return Math.max(t,Math.min(n,e))}function kr({isOpen:e,position:t,query:n,commands:r,activeIndex:l,onClose:o,onSelect:c,onHoverIndex:g}){const i=x.useRef(null);x.useEffect(()=>{if(!e)return;const u=m=>{const f=i.current;f&&(f.contains(m.target)||o())};return document.addEventListener("mousedown",u,!0),()=>document.removeEventListener("mousedown",u,!0)},[e,o]);const h=x.useMemo(()=>r.length?wr(l,0,r.length-1):0,[l,r.length]);return e?s.jsx("div",{ref:i,className:"notion-slash-menu absolute z-[60] w-[360px] max-w-[calc(100vw-24px)] overflow-x-hidden",style:{top:t.top,left:t.left},role:"dialog","aria-label":"Slash commands",children:s.jsxs("div",{className:"rounded-xl border border-notion-border bg-notion-bg-elevated shadow-[0_16px_40px_rgba(15,23,42,0.14)] overflow-hidden",children:[s.jsxs("div",{className:"px-3 py-2 border-b border-notion-border bg-gradient-to-b from-notion-hover/70 to-transparent",children:[s.jsxs("div",{className:"flex items-center gap-2",children:[s.jsx("span",{className:"text-[11px] uppercase tracking-[0.12em] text-notion-text-secondary select-none",children:"Insert"}),s.jsxs("span",{className:"text-xs font-mono text-notion-text-secondary",children:["/",n||""]})]}),s.jsx("div",{className:"mt-1 text-xs text-notion-text-secondary",children:r.length?"Type to filter, Enter to apply, Esc to close":"No matches"})]}),s.jsx(on,{className:"h-[320px]",children:s.jsx("div",{className:"py-1 px-1",children:r.map((u,m)=>{const f=m===h;return s.jsxs("button",{type:"button",onMouseEnter:()=>g?.(m),onMouseDown:M=>M.preventDefault(),onClick:()=>c(u),className:`w-full text-left px-2 py-2 rounded-md transition-colors flex items-start gap-2 ${f?"bg-notion-hover text-notion-text":"hover:bg-notion-hover/70 text-notion-text"}`,children:[s.jsx("div",{className:"mt-0.5 w-8 h-8 rounded-lg border border-notion-border bg-notion-bg flex items-center justify-center flex-shrink-0",children:u.icon?s.jsx("div",{className:"text-notion-text-secondary",children:u.icon}):s.jsx("div",{className:"text-notion-text-secondary text-xs font-mono",children:u.title.slice(0,2)})}),s.jsxs("div",{className:"min-w-0 flex-1",children:[s.jsxs("div",{className:"flex items-baseline gap-2",children:[s.jsx("div",{className:"font-medium text-sm truncate",children:u.title}),u.group?s.jsx("div",{className:"text-[11px] text-notion-text-secondary truncate",children:u.group}):null]}),u.description?s.jsx("div",{className:"text-xs text-notion-text-secondary mt-0.5 whitespace-normal break-words",children:u.description}):null]})]},u.id)})})})]})}):null}function ht(e,t,n){return Math.max(t,Math.min(n,e))}function Ue(e){const t=new Set;let n=!1;const r=e.map(l=>{if(!t.has(l.id))return t.add(l.id),l;n=!0;let o=$e(10);for(;t.has(o);)o=$e(10);return t.add(o),{...l,id:o}});return n?r:e}const It=360,vr=x.forwardRef(function({markdown:t,isStreaming:n=!1,hooks:r,onMarkdownChange:l,className:o="",theme:c,readonly:g=!1},i){const h=()=>{if(typeof window>"u")return"light";const p=document.documentElement,a=document.body;return p&&p.classList.contains("dark")||a&&a.classList.contains("dark")||window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"},[u,m]=x.useState(()=>c==="dark"||c==="light"?c:c==="system"?typeof window<"u"&&window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light":h());x.useEffect(()=>{if(c==="dark"||c==="light"){m(c);return}if(typeof window>"u"){m("light");return}const p=()=>{if(c==="system"){if(!window.matchMedia){m("light");return}m(window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light");return}m(h())};p();const a=[];if(c!=="system"&&typeof MutationObserver<"u"){const y=N=>{if(!N)return;const V=new MutationObserver(()=>p());V.observe(N,{attributes:!0,attributeFilter:["class"]}),a.push(V)};y(document.documentElement),y(document.body)}const d=window.matchMedia?window.matchMedia("(prefers-color-scheme: dark)"):null,k=()=>p();return d&&d.addEventListener("change",k),()=>{for(const y of a)y.disconnect();d&&d.removeEventListener("change",k)}},[c]);const[f,M]=x.useState(null),[T,v]=x.useState(null),[E,U]=x.useState(null);x.useEffect(()=>{let p=!1;return Promise.all([import("@streamdown/code"),import("@streamdown/math"),import("@streamdown/mermaid")]).then(([a,d,k])=>{p||(M(a.code),v(d.math),U(k.mermaid))}),()=>{p=!0}},[]);const G=x.useRef(null),{blocks:ae}=Vt({markdown:t,isStreaming:n}),[le,b]=x.useState(ae),[w,B]=x.useState([]),[P,Q]=x.useState(null),[O,ne]=x.useState(null),Y=x.useRef(le);x.useEffect(()=>{Y.current=le},[le]);const ge=x.useRef(P);x.useEffect(()=>{ge.current=P},[P]);const Ne=x.useRef([]);x.useEffect(()=>{Ne.current=w},[w]);const Se=x.useCallback(p=>{Ne.current=p,B(p)},[]),Me=x.useRef(null),Ye=x.useRef(!1),Je=x.useRef(!1),Te=x.useRef(0),Ae=x.useRef([]),L=x.useRef(new Set),X=x.useRef(null);x.useEffect(()=>{const p=a=>{const d=G.current;if(!d||!d.contains(a.target))return;a.target?.closest?.('[contenteditable="true"], textarea')&&(X.current=null)};return document.addEventListener("mousedown",p,!0),()=>document.removeEventListener("mousedown",p,!0)},[]);const Z=x.useRef(null),F=x.useCallback(p=>Y.current.find(a=>a.id===p)||null,[]),H=x.useCallback(p=>({...p,metadata:{...p.metadata||{},dirty:!0}}),[]),de=x.useCallback(p=>({...p,metadata:{...p.metadata||{},dirty:!1}}),[]),ue=x.useCallback(()=>{ne(null)},[]),fe=x.useCallback(p=>{const a=G.current;if(!a)return;let d=0,k=0;const y=document.querySelector(`[data-block-id="${p}"]`),N=a.getBoundingClientRect(),V=window.getSelection(),_=V&&V.rangeCount>0?V.getRangeAt(0):null,W=_?_.getBoundingClientRect():null;if(W&&W.width+W.height>0)d=W.bottom-N.top+8,k=W.left-N.left;else if(y){const z=y.getBoundingClientRect();d=z.top-N.top+28,k=z.left-N.left+28}const S=8,$=Math.max(S,N.width-It-8);k=ht(k,S,$),ne({blockId:p,query:"",activeIndex:0,position:{top:d,left:k}})},[]);x.useEffect(()=>{const p=Je.current;if(Je.current=n,!Ye.current&&!n){L.current.clear();const d=Ue(ae);Y.current=d,b(d),Se([]),Te.current=0,Ae.current=[],Ye.current=!0;return}if(n){if(Ye.current=!0,p||(Te.current=0,Ae.current=[],Se([]),t.trim()===""&&(L.current.clear(),b([]))),ae.length===0){Se([]);return}let d=ae.length;for(let _=ae.length-1;_>=0&&ae[_].isStreaming;_--)d=_;const k=ae.slice(0,d),y=ae.slice(d),N=L.current,V=Te.current;if(N.size===0){const _=k.map($=>de({...$,isStreaming:!1})),W=Y.current,S=_.length!==W.length||_.some(($,z)=>{const D=W[z];return!D||$.type!==D.type||$.id!==D.id});if(S){const $=Ue(_);Y.current=$,b($)}Ae.current=(S?Y.current:W).map($=>$.id),Te.current=k.length}else{const _=k.length,W=R=>{if(R.type==="paragraph"||R.type==="heading"||R.type==="list"||R.type==="blockquote"){const he=R.level??"",xe=R.listType??"",ye=R.depth??"";return`${R.type}|${he}|${xe}|${ye}|${je(R.spans||[],{forParsing:!0})}`}return R.type==="table"?`table|${me([R])}`:R.type==="code"?`code|${R.language||""}|${R.content||""}`:R.type==="mermaid"?`mermaid|${R.content||""}`:R.type==="math"?`math|${R.content||""}`:R.type==="image"?`image|${R.content||""}`:R.type==="divider"?"divider":`${R.type}|${R.content||""}`},S=R=>!!R?.metadata?.dirty||N.has(R.id),$=Y.current;let z=$.slice(),D=!1;const I=$.slice(0,V).map(R=>R.id);let q=Ae.current.length>0?Ae.current.slice(0,V):I;if(_<V){const R=q.slice(_,V);if(R.length>0){const he=new Set(R),xe=z.filter(ye=>he.has(ye.id)?S(ye):!0);xe.length!==z.length&&(z=xe,D=!0)}q=q.slice(0,_)}const J=Math.min(_,q.length),re=Math.max(0,J-64);for(let R=re;R<J;R++){const he=q[R];if(!he)continue;const xe=z.findIndex(Ve=>Ve.id===he);if(xe===-1)continue;const ye=z[xe];if(S(ye))continue;const Ee=de({...k[R],isStreaming:!1});W(ye)!==W(Ee)&&(z[xe]=Ee,q[R]=Ee.id,D=!0)}if(_>V){const R=k.slice(V).map(he=>de({...he,isStreaming:!1}));z=z.concat(R),q=q.concat(R.map(he=>he.id)),D=!0}if(Te.current=_,Ae.current=q,D){const R=Ue(z);Y.current=R,b(R)}}y.length===0&&(Te.current=k.length),Se(y.map(_=>de({..._,isStreaming:!0})));return}if(p&&!n){b(d=>{const k=d.slice(),y=Ne.current;for(const V of y)k.push(de({...V,isStreaming:!1}));const N=Ue(k.length>0?k:[{id:$e(10),type:"paragraph",spans:[],isStreaming:!1}]);return Y.current=N,N}),Se([]),Te.current=0,Ae.current=[];return}if(Me.current!==null&&t===Me.current)return;L.current.clear();const a=Ue(ae.map(d=>de(d)));Y.current=a,b(a),Se([]),Te.current=0,Ae.current=[]},[t,n,ae,de]);const ke=x.useCallback(p=>{const a=w.length>0?p.concat(w.map(k=>({...k,isStreaming:!1}))):p,d=me(a);Me.current=d,l?.(d)},[l,w]),oe=x.useCallback(p=>{const a=Ue(p);Y.current=a,b(a),ke(a)},[ke]),se=p=>p.type==="paragraph"||p.type==="heading"||p.type==="list"||p.type==="blockquote",ce=p=>{if(se(p))return p.spans.every(a=>a.text.trim()==="");if(p.type==="divider")return!1;if(p.type==="table"){const a=p.header||[],d=p.rows||[],k=y=>!!ut(y||[]).trim();return!(a.some(k)||d.some(y=>y.some(k)))}return p.content?.trim?.()===""},pe=x.useCallback(p=>{const a=r?.getBlockPolicy?.(p);return a||(se(p)?{atomic:!1,mergeable:!0,deleteOnBlurWhenEmpty:!1,enterBehavior:"newBlock"}:p.type==="table"?{atomic:!0,mergeable:!1,deleteOnBlurWhenEmpty:!1,enterBehavior:"stayInBlock"}:p.type==="image"?{atomic:!0,mergeable:!1,deleteOnBlurWhenEmpty:!0,enterBehavior:"ignore"}:{atomic:!0,mergeable:!1,deleteOnBlurWhenEmpty:!0,enterBehavior:"stayInBlock"})},[r]),ie=x.useCallback((p,a,d)=>{Z.current={blockId:p,start:a,end:d},ge.current=p,Q(p)},[]);x.useEffect(()=>{const p=Z.current;if(!p)return;Z.current=null;const{blockId:a,start:d,end:k}=p;requestAnimationFrame(()=>{const y=document.querySelector(`[data-block-id="${a}"]`);if(!y)return;const N=y.querySelector('[contenteditable="true"]');if(N){N.focus(),Be(N,d,k);return}const V=y.querySelector("textarea");V&&(V.focus(),V.setSelectionRange(d,k))})},[le,P]);const K=x.useCallback((p,a,d="bottom")=>{ge.current=p,Q(p);const k=(N,V)=>{const _=document;if(typeof _.caretRangeFromPoint=="function")return _.caretRangeFromPoint(N,V)||null;if(typeof _.caretPositionFromPoint=="function"){const W=_.caretPositionFromPoint(N,V);if(!W)return null;const S=document.createRange();return S.setStart(W.offsetNode,W.offset),S.collapse(!0),S}return null},y=N=>{requestAnimationFrame(()=>{const _=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');if(!_){N<3&&y(N+1);return}_.focus();const W=_.getBoundingClientRect(),S=Math.max(W.left+4,Math.min(a,W.right-4)),$=d==="top"?W.top+4:W.bottom-4,z=k(S,$);if(z&&_.contains(z.startContainer)){z.collapse(!0);const I=window.getSelection();I?.removeAllRanges(),I?.addRange(z);return}const D=F(p);if(D&&se(D)){const I=D.spans.reduce((te,q)=>te+q.text.length,0);Be(_,I,I)}})};y(0)},[F]),j=x.useCallback((p,a,d,k)=>{L.current.add(p),oe(Y.current.map(N=>N.id!==p?N:a==="paragraph"?H({id:N.id,type:"paragraph",spans:Re(d),isStreaming:!1}):a==="heading"?H({id:N.id,type:"heading",spans:Re(d),level:k?.level??1,isStreaming:!1}):a==="blockquote"?H({id:N.id,type:"blockquote",spans:Re(d),isStreaming:!1}):a==="list"?H({id:N.id,type:"list",spans:Re(d),listType:k?.listType??"unordered",depth:k?.depth??0,isStreaming:!1}):a==="code"?H({id:N.id,type:"code",content:"",language:k?.language||"text",isStreaming:!1}):a==="divider"?H({id:N.id,type:"divider",isStreaming:!1}):N));const y=F(p);y&&r?.onBlockChange&&r.onBlockChange(p,d,y)},[oe,r,F,H]),{handleKeyDown:C}=zt({onBlockTypeChange:j}),ee=x.useCallback((p,a)=>{L.current.add(p);const d=Y.current.map(y=>{if(y.id!==p)return y;if(se(y)){const N=typeof a=="string"?Re(a):a;return H({...y,spans:N})}if(y.type==="table"){if(typeof a!="string")return y;const N=Oe(a,!1,[y]).find(V=>V.type==="table");return!N||N.type!=="table"?y:H({...N,isStreaming:!1})}return"content"in y?H({...y,content:typeof a=="string"?a:je(a)}):y});oe(d);const k=d.find(y=>y.id===p);k&&r?.onBlockChange&&r.onBlockChange(p,a,k)},[r,oe,H]),we=x.useCallback((p,a)=>{const d=$e(10);L.current.add(d);const k=H((()=>{switch(a.type){case"paragraph":return{id:d,type:"paragraph",spans:a.spans??[],isStreaming:!1};case"heading":return{id:d,type:"heading",spans:a.spans??[],level:a.level??1,isStreaming:!1};case"blockquote":return{id:d,type:"blockquote",spans:a.spans??[],isStreaming:!1};case"list":return{id:d,type:"list",spans:a.spans??[],listType:a.listType??"unordered",depth:a.depth??0,isStreaming:!1};case"code":return{id:d,type:"code",content:a.content??"",language:a.language??"text",isStreaming:!1};case"math":return{id:d,type:"math",content:a.content??"",isStreaming:!1};case"mermaid":return{id:d,type:"mermaid",content:a.content??"",isStreaming:!1};case"image":return{id:d,type:"image",content:a.src??a.content??"",alt:a.alt,isStreaming:!1};case"table":{const W=Math.max(1,a.cols??3),S=Math.max(0,a.rows??2);return{id:d,type:"table",header:Array.from({length:W},()=>[]),rows:Array.from({length:S},()=>Array.from({length:W},()=>[])),align:Array.from({length:W},()=>"left"),isStreaming:!1}}case"divider":return{id:d,type:"divider",isStreaming:!1}}})()),y=Y.current,N=p?y.findIndex(W=>W.id===p):y.length-1,V=N===-1?y.length:N+1,_=y.slice(0,V).concat([k],y.slice(V));return oe(_),d},[oe,H]),Le=x.useCallback(p=>{L.current.delete(p);const a=Y.current,d=a.findIndex(W=>W.id===p);if(d===-1)return;const k=a.slice(0,d).concat(a.slice(d+1)),y=k.length===0?[{id:$e(10),type:"paragraph",spans:[],isStreaming:!1}]:k;oe(y);const N=Math.max(0,Math.min(d-1,y.length-1)),V=y[N],_=se(V)?V.spans.reduce((W,S)=>W+S.text.length,0):V.content?.length??0;ie(V.id,_,_)},[oe,ie]),Ce=x.useCallback((p,a,d)=>{L.current.add(p);const k=Y.current,y=k.find(_=>_.id===p);if(!y)return;const N=se(y)?y.spans:[],V=(()=>{if(a==="paragraph")return{id:y.id,type:"paragraph",spans:N,isStreaming:!1};if(a==="heading")return{id:y.id,type:"heading",spans:N,level:d?.level??1,isStreaming:!1};if(a==="blockquote")return{id:y.id,type:"blockquote",spans:N,isStreaming:!1};if(a==="list")return{id:y.id,type:"list",spans:N,listType:d?.listType??"unordered",depth:d?.depth??0,isStreaming:!1};if(a==="table"){const S=Array.from({length:3},($,z)=>z===0?N:[]);return{id:y.id,type:"table",header:S,rows:Array.from({length:2},()=>Array.from({length:3},()=>[])),align:Array.from({length:3},()=>"left"),isStreaming:!1}}if(a==="image"){const _="content"in y?y.content??"":"",W=d?.src??_,S=d?.alt??(y.type==="image"?y.alt:void 0);return{id:y.id,type:"image",content:W,alt:S,isStreaming:!1}}return y})();oe(k.map(_=>_.id===p?H(V):_))},[oe,H]),Pe=x.useCallback((p,a=!1)=>{L.current.add(p);const d=Y.current,k=d.findIndex($=>$.id===p),y=d[k],N=k>0?d[k-1]:null;if(!y||y.type!=="list"||!N||N.type!=="list")return;const V=y.depth??0,_=(N.depth??0)+1,W=Math.min(V+1,_);if(W===V)return;const S=a?y.listType:N.listType;oe(d.map($=>$.id===p?H({...y,depth:W,listType:S}):$))},[oe,H]),Ke=x.useCallback((p,a=!1)=>{L.current.add(p);const d=Y.current,k=d.findIndex(V=>V.id===p),y=d[k];if(!y||y.type!=="list")return;const N=y.depth??0;if(N>0){const V=N-1;let _=y.listType;if(!a)for(let W=k-1;W>=0;W--){const S=d[W];if(S.type!=="list")break;if((S.depth??0)===V){_=S.listType;break}}oe(d.map(W=>W.id===p?H({...y,depth:V,listType:_}):W))}else Ce(p,"paragraph")},[oe,Ce,H]),Xe=x.useMemo(()=>{const p=w.length>0?Y.current.concat(w):Y.current;return{updateBlock:(a,d)=>ee(a,d),getBlock:a=>F(a),getAllBlocks:()=>p,getMarkdown:()=>me(p),insertBlockAfter:(a,d)=>we(a,d),deleteBlock:a=>Le(a),setBlockType:(a,d,k)=>Ce(a,d,k),indentListItem:a=>Pe(a),outdentListItem:a=>Ke(a)}},[ee,F,le,w,we,Le,Ce,Pe,Ke]),yt=x.useMemo(()=>{const p=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M4 6h16"}),s.jsx("path",{d:"M4 12h10"}),s.jsx("path",{d:"M4 18h16"})]}),a=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M6 4v16"}),s.jsx("path",{d:"M18 4v16"}),s.jsx("path",{d:"M6 12h12"})]}),d=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M8 6h13"}),s.jsx("path",{d:"M8 12h13"}),s.jsx("path",{d:"M8 18h13"}),s.jsx("path",{d:"M3 6h.01"}),s.jsx("path",{d:"M3 12h.01"}),s.jsx("path",{d:"M3 18h.01"})]}),k=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M10 6h11"}),s.jsx("path",{d:"M10 12h11"}),s.jsx("path",{d:"M10 18h11"}),s.jsx("path",{d:"M3 7h1"}),s.jsx("path",{d:"M3 12h1"}),s.jsx("path",{d:"M3 18h1"})]}),y=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M10 11H6c-1.1 0-2 .9-2 2v5h6v-7z"}),s.jsx("path",{d:"M20 11h-4c-1.1 0-2 .9-2 2v5h6v-7z"}),s.jsx("path",{d:"M4 11V9a5 5 0 0 1 5-5"}),s.jsx("path",{d:"M14 11V9a5 5 0 0 1 5-5"})]}),N=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M16 18l6-6-6-6"}),s.jsx("path",{d:"M8 6l-6 6 6 6"})]}),V=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M7 7h10"}),s.jsx("path",{d:"M7 17h10"}),s.jsx("path",{d:"M7 7v10"}),s.jsx("path",{d:"M17 7v10"})]}),_=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M4 4h6l4 16h6"}),s.jsx("path",{d:"M4 12h16"})]}),W=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("path",{d:"M3 12h18"}),s.jsx("path",{d:"M8 8h.01"}),s.jsx("path",{d:"M8 16h.01"}),s.jsx("path",{d:"M16 8h.01"}),s.jsx("path",{d:"M16 16h.01"})]}),S=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("rect",{x:"3",y:"3",width:"18",height:"18",rx:"2",ry:"2"}),s.jsx("path",{d:"M3 16l5-5a2 2 0 0 1 3 0l5 5"}),s.jsx("path",{d:"M14 14l2-2a2 2 0 0 1 3 0l2 2"}),s.jsx("path",{d:"M8 8h.01"})]}),$=s.jsxs("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[s.jsx("rect",{x:"3",y:"4",width:"18",height:"16",rx:"2"}),s.jsx("path",{d:"M3 10h18"}),s.jsx("path",{d:"M9 4v16"}),s.jsx("path",{d:"M15 4v16"})]}),z=D=>({id:`heading-${D}`,title:`Heading ${D}`,description:`Section heading (H${D})`,group:"Text",aliases:[`h${D}`],keywords:["heading","title",`h${D}`],icon:a,block:{type:"heading",level:D,spans:[]}});return[{id:"paragraph",title:"Text",description:"Plain paragraph",group:"Text",aliases:["p","text"],keywords:["paragraph"],icon:p,block:{type:"paragraph",spans:[]}},z(1),z(2),z(3),{id:"blockquote",title:"Quote",description:"Blockquote",group:"Text",aliases:["quote","bq"],keywords:["blockquote"],icon:y,block:{type:"blockquote",spans:[]}},{id:"bulleted-list",title:"Bulleted list",description:"Unordered list item",group:"Lists",aliases:["ul","bullet","list"],keywords:["unordered"],icon:d,block:{type:"list",listType:"unordered",depth:0,spans:[]}},{id:"numbered-list",title:"Numbered list",description:"Ordered list item",group:"Lists",aliases:["ol","number","ordered"],keywords:["ordered"],icon:k,block:{type:"list",listType:"ordered",depth:0,spans:[]}},{id:"code",title:"Code",description:"Fenced code block",group:"Media",aliases:["codeblock","```"],keywords:["snippet"],icon:N,block:{type:"code",language:"text",content:""}},{id:"mermaid",title:"Mermaid",description:"Mermaid diagram block",group:"Media",aliases:["diagram","flow"],keywords:["graph","sequence"],icon:V,block:{type:"mermaid",content:""}},{id:"math",title:"Math (block)",description:"LaTeX block equation",group:"Media",aliases:["latex","equation"],keywords:["katex"],icon:_,block:{type:"math",content:""}},{id:"image",title:"Image",description:"Image block (URL)",group:"Media",aliases:["img","photo","picture"],keywords:["media"],icon:S,block:{type:"image",content:""}},{id:"table",title:"Table",description:"Spreadsheet-style table",group:"Layout",aliases:["tbl","grid"],keywords:["rows","columns","sheet"],icon:$,block:{type:"table",rows:2,cols:3}},{id:"divider",title:"Divider",description:"Horizontal divider",group:"Layout",aliases:["hr","line","---"],keywords:["separator"],icon:W,block:{type:"divider"},insertParagraphAfter:!0}]},[]),un=x.useCallback(p=>{const a=Y.current,d=a.findIndex(N=>N.id===p);if(d===-1)return;const k=d+1<a.length?a[d+1]:null;if(k&&k.type==="paragraph"&&ut(k.spans).trim()===""&&!k.isStreaming){ie(k.id,0,0);return}const y=we(p,{type:"paragraph",spans:[]});ie(y,0,0)},[we,ie]),We=x.useCallback(p=>{const a=r?.getSlashCommands?r.getSlashCommands({block:p}):[];return yt.concat(a||[])},[yt,r]),qe=x.useCallback((p,a)=>{const d=a.trim().toLowerCase();return d?p.map(k=>{const y=[k.title,...k.aliases||[],...k.keywords||[]].join(" ").toLowerCase(),N=(k.aliases||[]).some(W=>W.toLowerCase().startsWith(d))||k.title.toLowerCase().startsWith(d),V=y.includes(d);return{cmd:k,score:N?3:V?1:0}}).filter(k=>k.score>0).sort((k,y)=>y.score-k.score||k.cmd.title.localeCompare(y.cmd.title)).map(k=>k.cmd):p},[]),rt=x.useMemo(()=>{if(!O)return[];const p=Y.current.find(a=>a.id===O.blockId);return p?qe(We(p),O.query):[]},[O,qe,We]),at=x.useCallback((p,a)=>{switch(a.type){case"paragraph":return{id:p,type:"paragraph",spans:a.spans??[],isStreaming:!1};case"heading":return{id:p,type:"heading",spans:a.spans??[],level:a.level??1,isStreaming:!1};case"blockquote":return{id:p,type:"blockquote",spans:a.spans??[],isStreaming:!1};case"list":return{id:p,type:"list",spans:a.spans??[],listType:a.listType??"unordered",depth:a.depth??0,isStreaming:!1};case"code":return{id:p,type:"code",content:a.content??"",language:a.language??"text",isStreaming:!1};case"math":return{id:p,type:"math",content:a.content??"",isStreaming:!1};case"mermaid":return{id:p,type:"mermaid",content:a.content??"",isStreaming:!1};case"image":return{id:p,type:"image",content:a.src??a.content??"",alt:a.alt,isStreaming:!1};case"table":{const d=Math.max(1,a.cols??3),k=Math.max(0,a.rows??2);return{id:p,type:"table",header:Array.from({length:d},()=>[]),rows:Array.from({length:k},()=>Array.from({length:d},()=>[])),align:Array.from({length:d},()=>"left"),isStreaming:!1}}case"divider":return{id:p,type:"divider",isStreaming:!1}}},[]),lt=x.useCallback((p,a,d)=>{const k=Y.current.find(S=>S.id===p);if(!k)return;if(a.execute){a.execute(Xe,{blockId:p,block:k,query:d});return}if(!a.block)return;const y=Y.current,N=y.findIndex(S=>S.id===p);if(N===-1)return;let V=a.block;a.block.type==="list"&&k.type==="list"&&(V={...a.block,depth:k.depth??0}),L.current.add(p);const _=H(at(p,V));let W=y.slice(0,N).concat([_],y.slice(N+1));if(a.insertParagraphAfter){const S=$e(10);L.current.add(S);const $=H(at(S,{type:"paragraph",spans:[]}));W=W.slice(0,N+1).concat([$],W.slice(N+1)),oe(W),ie(S,0,0);return}oe(W),ie(p,0,0)},[Xe,H,at,oe,ie]);x.useImperativeHandle(i,()=>Xe,[Xe]),x.useEffect(()=>{r?.ref&&"current"in r.ref&&(r.ref.current=Xe)},[r?.ref,Xe]);const fn=x.useCallback(p=>{Y.current.find(d=>d.id===p)?.isStreaming||(ge.current=p,Q(p))},[]),hn=x.useCallback(p=>{if(ge.current!==p)return;const a=Y.current.find(k=>k.id===p);if(!a){ge.current=null,Q(null);return}if(pe(a).deleteOnBlurWhenEmpty&&ce(a)){Le(p);return}ge.current=null,Q(null)},[Le,pe]),pn=x.useCallback((p,a)=>{ee(p,a)},[ee]),bt=(p,a)=>{const d=[],k=[];let y=0;for(const N of p){const V=y,_=y+N.text.length;if(_<=a)d.push({...N});else if(V>=a)k.push({...N});else{const W=a-V;W>0&&d.push({...N,text:N.text.slice(0,W)}),W<N.text.length&&k.push({...N,text:N.text.slice(W)})}y=_}return[Ie(d),Ie(k)]},wt=(p,a,d)=>{if(a===d)return p;const k=Math.min(a,d),y=Math.max(a,d),N=[];let V=0;for(const _ of p){const W=V,S=V+_.text.length;if(S<=k||W>=y)N.push({..._});else{const $=Math.max(0,k-W),z=Math.max(0,y-W);$>0&&N.push({..._,text:_.text.slice(0,$)}),z<_.text.length&&N.push({..._,text:_.text.slice(z)})}V=S}return Ie(N)},ct=(p,a)=>{if(p.length===0)return a;if(a.length===0)return p;const d=p[p.length-1],k=a[0],N=d.text.length>0&&k.text.length>0&&!/\s$/.test(d.text)&&!/^\s/.test(k.text)?p.concat([{text:" ",style:"plain"}],a):p.concat(a);return Ie(N)},mn=x.useCallback((p,a)=>{const d=Y.current.find(S=>S.id===p);if(!d)return;a.key!=="ArrowUp"&&a.key!=="ArrowDown"&&(X.current=null);const k=S=>{if(!(d.type==="paragraph"||d.type==="heading"||d.type==="list"||d.type==="blockquote"))return;ee(p,S?[{text:S,style:"plain"}]:[]);const z=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');if(z){z.textContent=S;const D=S.length;Be(z,D,D);return}ie(p,S.length,S.length)};if(O&&O.blockId===p){a.preventDefault();const S=O.query,$=a.key;if($==="Escape"){ue();const D="/"+S;k(D);return}const z=Math.max(0,rt.length-1);if($==="ArrowDown"||$==="Tab"){ne({...O,activeIndex:Math.min(O.activeIndex+1,z)});return}if($==="ArrowUp"){ne({...O,activeIndex:Math.max(O.activeIndex-1,0)});return}if($==="Backspace"){if(S.length===0){ue(),k("");return}const D=S.slice(0,-1),I=qe(We(d),D);ne({...O,query:D,activeIndex:0}),k("/"+D),I.length===0&&ue();return}if($==="Enter"){const D=rt[ht(O.activeIndex,0,z)];ue(),D&<(p,D,S);return}if($.length===1&&!a.metaKey&&!a.ctrlKey&&!a.altKey){const D=S+$,I=qe(We(d),D);ne({...O,query:D,activeIndex:0}),k("/"+D),I.length===0&&ue();return}return}if(!O&&se(d)){const S=d.spans.map($=>$.text).join("");if(S.startsWith("/")){const $=a.currentTarget,z=$?.isContentEditable?be($):null;if(z&&z.start===z.end&&z.start===S.length){const D=(I,te)=>{a.preventDefault();const q=G.current;if(!q)return;let J=0,A=0;const re=document.querySelector(`[data-block-id="${p}"]`),R=q.getBoundingClientRect(),he=window.getSelection(),xe=he&&he.rangeCount>0?he.getRangeAt(0):null,ye=xe?xe.getBoundingClientRect():null;if(ye&&ye.width+ye.height>0)J=ye.bottom-R.top+8,A=ye.left-R.left;else if(re){const st=re.getBoundingClientRect();J=st.top-R.top+28,A=st.left-R.left+28}const Ee=8,Ve=Math.max(Ee,R.width-It-8);A=ht(A,Ee,Ve),ne({blockId:p,query:te,activeIndex:0,position:{top:J,left:A}}),ee(p,[{text:I,style:"plain"}]);const De=re?.querySelector('[contenteditable="true"]');De&&(De.textContent=I,Be(De,I.length,I.length))};if(a.key==="Backspace"&&S.length>1){const I=S.slice(0,-1),te=I.slice(1);if(qe(We(d),te).length>0){D(I,te);return}}else if(a.key.length===1&&!a.metaKey&&!a.ctrlKey&&!a.altKey){const I=S+a.key,te=I.slice(1);if(qe(We(d),te).length>0){D(I,te);return}}}}}if((a.key==="Backspace"||a.key==="Delete")&&a.target?.isContentEditable){const S=window.getSelection();if(S&&S.rangeCount>0){const $=S.getRangeAt(0);if(!$.collapsed){const z=$.startContainer.parentElement?.closest?.("[data-block-id]"),D=$.endContainer.parentElement?.closest?.("[data-block-id]"),I=z?.getAttribute("data-block-id"),te=D?.getAttribute("data-block-id");if(I&&te&&I!==te){a.preventDefault();const q=Y.current.findIndex(De=>De.id===I),J=Y.current.findIndex(De=>De.id===te);if(q===-1||J===-1)return;const A=Math.min(q,J),re=Math.max(q,J),R=Y.current,he=R.slice(0,A).concat(R.slice(re+1)),xe=he.length===0?[{id:$e(10),type:"paragraph",spans:[],isStreaming:!1}]:he;oe(xe);const ye=Math.max(0,Math.min(A-1,xe.length-1)),Ee=xe[ye],Ve=se(Ee)?Ee.spans.reduce((De,st)=>De+st.text.length,0):Ee.content?.length??0;ie(Ee.id,Ve,Ve);return}}}}const y=a.currentTarget;let N=0;if(y instanceof HTMLTextAreaElement)N=y.selectionStart;else if(y?.isContentEditable){const S=be(y);S&&(N=S.start)}const V="spans"in d?je(d.spans):"content"in d?d.content:"";if(C(a,d,V,N))return;if(a.key==="/"&&N===0){const S=!!d.metadata?.dirty,$=d.type==="paragraph"||d.type==="heading"||d.type==="list"||d.type==="blockquote"?ut(d.spans).trim()==="":d.type==="divider"?!1:d.content?.trim?.()==="";if(S&&$&&!d.isStreaming){a.preventDefault(),fe(p),ee(p,[{text:"/",style:"plain"}]);const D=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');D?(D.textContent="/",Be(D,1,1)):ie(p,1,1);return}}if(a.key==="ArrowUp"&&y?.isContentEditable&&se(d)){const S=be(y),$=I=>I?.parentElement?.closest?.('[data-math-delim="1"]')!=null,z=(I,te)=>{const q=document.createTreeWalker(I,NodeFilter.SHOW_TEXT,null);let J=0,A;for(;A=q.nextNode();){if($(A))continue;const re=A.textContent?.length??0;if(re!==0){if(J+re>=te)return{node:A,offset:Math.max(0,Math.min(re,te-J))};J+=re}}return{node:I,offset:I.childNodes.length}},D=(I,te)=>{if(I.textContent==null||I.textContent.length===0)return I.getBoundingClientRect();const q=z(I,te),J=document.createRange();try{if(J.setStart(q.node,q.offset),q.node.nodeType===Node.TEXT_NODE){const A=q.node,re=A.textContent?.length??0;re>0&&(q.offset<re?J.setEnd(A,q.offset+1):q.offset>0?(J.setStart(A,q.offset-1),J.setEnd(A,q.offset)):J.setEnd(A,Math.min(1,re)))}}catch{return null}return J.getClientRects()[0]||J.getBoundingClientRect()};if(S&&S.start===S.end){const I=y.getBoundingClientRect();let te=D(y,S.start);if(!te){const q=window.getSelection();if(q&&q.rangeCount>0){const J=q.getRangeAt(0);if(J.collapsed&&y.contains(J.startContainer))try{const A=J.cloneRange();if(A.startContainer.nodeType===Node.TEXT_NODE){const re=A.startContainer,R=re.textContent?.length??0;R>0&&(A.startOffset<R?A.setEnd(re,A.startOffset+1):A.startOffset>0&&(A.setStart(re,A.startOffset-1),A.setEnd(re,A.startOffset)))}te=A.getClientRects()[0]||A.getBoundingClientRect()}catch{}}}if(te){X.current==null&&(X.current=te.left+1);const q=X.current;if(te.top<=I.top+6){const A=Y.current;let R=A.findIndex(he=>he.id===p)-1;for(;R>=0&&(!se(A[R])||A[R].isStreaming);)R--;if(R>=0){a.preventDefault(),K(A[R].id,q,"bottom");return}}}}}if(a.key==="ArrowDown"&&y?.isContentEditable&&se(d)){const S=be(y),$=I=>I?.parentElement?.closest?.('[data-math-delim="1"]')!=null,z=(I,te)=>{const q=document.createTreeWalker(I,NodeFilter.SHOW_TEXT,null);let J=0,A;for(;A=q.nextNode();){if($(A))continue;const re=A.textContent?.length??0;if(re!==0){if(J+re>=te)return{node:A,offset:Math.max(0,Math.min(re,te-J))};J+=re}}return{node:I,offset:I.childNodes.length}},D=(I,te)=>{if(I.textContent==null||I.textContent.length===0)return I.getBoundingClientRect();const q=z(I,te),J=document.createRange();try{if(J.setStart(q.node,q.offset),q.node.nodeType===Node.TEXT_NODE){const A=q.node,re=A.textContent?.length??0;re>0&&(q.offset<re?J.setEnd(A,q.offset+1):q.offset>0?(J.setStart(A,q.offset-1),J.setEnd(A,q.offset)):J.setEnd(A,Math.min(1,re)))}}catch{return null}return J.getClientRects()[0]||J.getBoundingClientRect()};if(S&&S.start===S.end){const I=y.getBoundingClientRect();let te=D(y,S.start);if(!te){const q=window.getSelection();if(q&&q.rangeCount>0){const J=q.getRangeAt(0);if(J.collapsed&&y.contains(J.startContainer))try{const A=J.cloneRange();if(A.startContainer.nodeType===Node.TEXT_NODE){const re=A.startContainer,R=re.textContent?.length??0;R>0&&(A.startOffset<R?A.setEnd(re,A.startOffset+1):A.startOffset>0&&(A.setStart(re,A.startOffset-1),A.setEnd(re,A.startOffset)))}te=A.getClientRects()[0]||A.getBoundingClientRect()}catch{}}}if(te){X.current==null&&(X.current=te.left+1);const q=X.current;if(te.bottom>=I.bottom-6){const A=Y.current;let R=A.findIndex(he=>he.id===p)+1;for(;R<A.length&&(!se(A[R])||A[R].isStreaming);)R++;if(R<A.length){a.preventDefault(),K(A[R].id,q,"top");return}}}}}if((a.key==="ArrowLeft"||a.key==="ArrowRight")&&y?.isContentEditable&&se(d)){const S=be(y);if(S&&S.start===S.end){const $=Y.current,z=$.findIndex(D=>D.id===p);if(a.key==="ArrowLeft"&&S.start===0){let D=z-1;for(;D>=0&&(!se($[D])||$[D].isStreaming);)D--;if(D>=0){a.preventDefault(),K($[D].id,Number.POSITIVE_INFINITY,"bottom");return}}if(a.key==="ArrowRight"){const D=d.spans.reduce((I,te)=>I+te.text.length,0);if(S.start>=D){let I=z+1;for(;I<$.length&&(!se($[I])||$[I].isStreaming);)I++;if(I<$.length){a.preventDefault(),K($[I].id,Number.NEGATIVE_INFINITY,"top");return}}}}}const W=pe(d);if(a.key==="Enter"&&W.enterBehavior==="newBlock"&&se(d)&&y?.isContentEditable){a.preventDefault();const S=be(y),$=S?.start??0,z=S?.end??$;if(d.type==="list"&&ce(d)){Ce(d.id,"paragraph"),ie(d.id,0,0);return}const D=wt(d.spans,$,z),[I,te]=bt(D,$),q=Y.current,J=q.findIndex(ye=>ye.id===d.id),A=$e(10);L.current.add(d.id),L.current.add(A);let re=d.type,R;(d.type==="heading"||d.type==="blockquote")&&(re="paragraph"),d.type==="list"&&(re="list");const he=H({...d,spans:I});re==="list"&&d.type==="list"?R={id:A,type:"list",spans:te,listType:d.listType,depth:d.depth??0,isStreaming:!1}:R={id:A,type:"paragraph",spans:te,isStreaming:!1},R=H(R);const xe=q.slice(0,J).concat([he,R],q.slice(J+1));oe(xe),ie(A,0,0);return}if(a.key==="Tab"&&se(d)&&d.type==="list"&&y?.isContentEditable){a.preventDefault(),a.shiftKey?Ke(d.id):Pe(d.id);return}if(a.altKey&&(a.key==="]"||a.key==="[")&&se(d)&&d.type==="list"&&y?.isContentEditable){a.preventDefault(),a.key==="["?Ke(d.id,!0):Pe(d.id,!0);return}if(a.key==="Backspace"){if(se(d)&&y?.isContentEditable){const S=be(y);if(!S||S.start!==S.end)return;if(S.start===0){if(d.type==="heading"){a.preventDefault(),Ce(d.id,"paragraph"),ie(d.id,0,0);return}if(d.type==="list"){if(a.preventDefault(),(d.depth??0)>0){Ke(d.id),ie(d.id,0,0);return}Ce(d.id,"paragraph"),ie(d.id,0,0);return}const $=Y.current,z=$.findIndex(I=>I.id===d.id),D=z>0?$[z-1]:null;if(D&&D.type==="divider"){a.preventDefault(),Le(D.id),ie(d.id,0,0);return}if(ce(d)){a.preventDefault(),Le(d.id);return}if(a.preventDefault(),D&&D.type==="list"&&(d.type==="paragraph"||d.type==="blockquote")){const I=D.spans.reduce((J,A)=>J+A.text.length,0),te={...D,spans:ct(D.spans,d.spans)},q=$.slice(0,z-1).concat([H(te)],$.slice(z+1));oe(q),ie(te.id,I,I);return}if(D&&se(D)&&se(d)&&D.type!=="list"){const I=D.spans.reduce((J,A)=>J+A.text.length,0),te={...D,spans:ct(D.spans,d.spans)},q=$.slice(0,z-1).concat([H(te)],$.slice(z+1));oe(q),ie(te.id,I,I);return}}return}if(y instanceof HTMLTextAreaElement&&N===0&&ce(d)){a.preventDefault(),Le(d.id);return}}},[C,O,ue,ne,rt,lt,fe,ee,qe,We,pe,ce,Le,oe,ie,K,bt,wt,ct,Ce,Pe,Ke]),gn=x.useMemo(()=>{const p=[],a=[];for(const d of le)if(d.type==="list"&&d.listType==="ordered"){const k=d.depth??0;for(;a.length<=k;)a.push(0);a[k]=(a[k]||0)+1;for(let y=k+1;y<a.length;y++)a[y]=0;p.push({...d,metadata:{...d.metadata||{},displayNumber:a[k]}})}else d.type==="list"||(a.length=0),p.push(d);return p},[le]),xn=x.useMemo(()=>{if(w.length===0)return"";const p=w[w.length-1],a=w.slice(0,-1),d=a.length>0?me(a)+`
|
|
27
27
|
|
|
28
|
-
`:"";if(p.type==="code"){const
|
|
29
|
-
`,
|
|
30
|
-
`+
|
|
31
|
-
`)}function
|
|
28
|
+
`:"";if(p.type==="code"){const y="```"+(p.language||"")+`
|
|
29
|
+
`,N=p.content||"";return d+y+N}if(p.type==="mermaid"){const k="```mermaid\n",y=p.content||"";return d+k+y}if(p.type==="math"){const y=p.content||"";return d+`$$
|
|
30
|
+
`+y}return me(w)},[w]),kt=u==="dark";return s.jsxs("div",{ref:G,className:`notion-stream ${kt?"dark":""} ${o} relative overflow-x-hidden bg-notion-bg text-notion-text`,children:[s.jsx(kr,{isOpen:!!O,position:O?.position||{top:0,left:0},query:O?.query||"",commands:rt,activeIndex:O?.activeIndex||0,onClose:ue,onHoverIndex:p=>{O&&ne({...O,activeIndex:p})},onSelect:p=>{O&&(ue(),lt(O.blockId,p,O.query))}}),s.jsxs("div",{className:"notion-stream-content space-y-1 relative overflow-x-hidden",children:[gn.map(p=>s.jsx("div",{"data-block-id":p.id,className:"relative",children:s.jsx(br,{block:p,isEditing:!g&&P===p.id,onStartEdit:()=>fn(p.id),onEndEdit:()=>hn(p.id),onContentChange:a=>pn(p.id,a),onKeyDown:a=>mn(p.id,a),hooks:r,onExitTable:un,theme:u,readonly:g})},p.id)),n&&w.length>0&&s.jsx("div",{className:"relative notion-stream-virtual-block",children:s.jsx(Ge.Streamdown,{plugins:{...f?{code:f}:{},...T?{math:T}:{},...E?{mermaid:E}:{}},shikiTheme:["min-light","min-dark"],mermaid:{config:{theme:kt?"dark":"default",themeVariables:{background:"transparent"}}},isAnimating:!0,controls:!1,children:xn})})]}),le.length===0&&!n&&s.jsx("div",{className:"text-notion-text-secondary text-center py-8",children:"No content yet"}),le.length===0&&n&&s.jsxs("div",{className:"flex items-center space-x-2 text-notion-text-secondary py-4",children:[s.jsx("span",{className:"inline-block w-0.5 h-4 bg-notion-text animate-pulse"}),s.jsx("span",{className:"text-sm",children:"Receiving content..."})]})]})});function Ze(e,t){if(e.type==="paragraph"||e.type==="heading"||e.type==="list"||e.type==="blockquote"){const n=typeof t=="string"?Re(t):t;return{...e,spans:n}}if("content"in e){const n=typeof t=="string"?t:me([{...e,...e,spans:t}]);return{...e,content:n}}return e}function jr({blocks:e,onMarkdownChange:t}){const[n,r]=x.useState(null),[l,o]=x.useState(new Map),c=x.useRef(e);c.current=e;const g=x.useMemo(()=>e.map(u=>{const m=l.get(u.id);return m===void 0?u:Ze(u,m)}),[e,l]),i=x.useCallback((u,m)=>{if(o(f=>{const M=new Map(f);return M.set(u,m),M}),t){const f=c.current.map(M=>{const T=M.id===u?m:l.get(M.id);return T===void 0?M:Ze(M,T)});t(me(f))}},[t,l]),h=x.useCallback(()=>({updateBlock:(u,m)=>{i(u,m)},getBlock:u=>{const m=c.current.find(M=>M.id===u);if(!m)return null;const f=l.get(u);return f===void 0?m:Ze(m,f)},getAllBlocks:()=>c.current.map(u=>{const m=l.get(u.id);return m===void 0?u:Ze(u,m)}),getMarkdown:()=>{const u=c.current.map(m=>{const f=l.get(m.id);return f===void 0?m:Ze(m,f)});return me(u)},insertBlockAfter:()=>{throw new Error("insertBlockAfter is only available on NotionStream ref")},deleteBlock:()=>{throw new Error("deleteBlock is only available on NotionStream ref")},setBlockType:()=>{throw new Error("setBlockType is only available on NotionStream ref")},indentListItem:()=>{throw new Error("indentListItem is only available on NotionStream ref")},outdentListItem:()=>{throw new Error("outdentListItem is only available on NotionStream ref")}}),[i,l]);return{mergedBlocks:g,editingBlockId:n,setEditingBlockId:r,updateBlockValue:i,getRef:h}}function ln(e,t){const n=Oe(e,!1),r=Oe(t,!1),l=xt(n,r);if(l.length===0)return[];const o=[];let c=[],g=[],i=0;const h=T=>T.type==="code",u=()=>{if(!(c.length===0&&g.length===0)){if(c.length===1&&g.length===1&&h(c[0])&&h(g[0])){const T=[...c],v=[...g];o.push({id:et(i,"modified",T,v),type:"modified",beforeBlocks:T,afterBlocks:v,beforeMarkdown:me(T),afterMarkdown:me(v),codeDiff:{language:(g[0].language||c[0].language||"text").toString(),lines:ot(c[0].content||"",g[0].content||"")}})}else if(c.length>0&&g.length>0){const T=[...c],v=[...g];o.push({id:et(i,"modified",T,v),type:"modified",beforeBlocks:T,afterBlocks:v,beforeMarkdown:me(T),afterMarkdown:me(v)})}else if(c.length>0){const T=[...c],v=me(T);v.trim()&&o.push({id:et(i,"removed",T,[]),type:"removed",blocks:T,markdown:v})}else if(g.length>0){const T=[...g],v=me(T);v.trim()&&o.push({id:et(i,"added",T,[]),type:"added",blocks:T,markdown:v})}c=[],g=[]}};let m=[],f=0;const M=()=>{if(m.length===0)return;const T=me(m);T.trim()&&o.push({id:et(f,"unchanged",[...m],[]),type:"unchanged",blocks:[...m],markdown:T}),m=[]};for(let T=0;T<l.length;T++){const v=l[T];v.type==="unchanged"?(u(),m.length===0&&(f=T),m.push(v.block)):v.type==="removed"?(M(),c.length===0&&g.length===0&&(i=T),c.push(v.block)):v.type==="added"&&(M(),c.length===0&&g.length===0&&(i=T),g.push(v.block))}return M(),u(),o}function cn(e,t,n={}){const r=n.defaultDecision??"reject",l=[];for(const o of e){if(o.type==="unchanged"){l.push(...o.blocks);continue}const c=t[o.id]??r,g=c==="unset"?r:c;if(o.type==="added"){g==="accept"&&l.push(...o.blocks);continue}if(o.type==="removed"){g==="reject"&&l.push(...o.blocks);continue}if(o.type==="modified")if(g==="accept"){const i=o.afterBlocks?.length?o.afterBlocks:o.beforeBlocks;l.push(...i)}else l.push(...o.beforeBlocks)}return me(l)}function dn(e,t){const n=Ot(e),r=Ot(t);if(n.length===0&&r.length===0)return[];if(n.length===0)return r.map(u=>({text:u,type:"added"}));if(r.length===0)return n.map(u=>({text:u,type:"removed"}));const l=n.length,o=r.length,c=Array(l+1).fill(null).map(()=>Array(o+1).fill(0));for(let u=1;u<=l;u++)for(let m=1;m<=o;m++)n[u-1]===r[m-1]?c[u][m]=c[u-1][m-1]+1:c[u][m]=Math.max(c[u-1][m],c[u][m-1]);const g=[];let i=l,h=o;for(;i>0||h>0;)i>0&&h>0&&n[i-1]===r[h-1]?(g.unshift({text:n[i-1],type:"unchanged"}),i--,h--):h>0&&(i===0||c[i][h-1]>=c[i-1][h])?(g.unshift({text:r[h-1],type:"added"}),h--):(g.unshift({text:n[i-1],type:"removed"}),i--);return Tr(g)}function Nr(e,t,n={}){const{mergeAdjacent:r=!0}=n,l=Oe(e,!1),o=Oe(t,!1),c=xt(l,o);if(c.length===0)return[];const i=(r?Mr(c):c.map(h=>({type:h.type,blocks:[h.block]}))).map(h=>({type:h.type,markdown:me(h.blocks)})).filter(h=>h.markdown.trim()!=="");return r?Er(i):i}function Sr(e,t){const n=Oe(e,!1),r=Oe(t,!1),l=xt(n,r);if(l.length===0)return[];const o=[];let c=null,g=[];const i=()=>{if(!c||g.length===0)return;const u=me(g);u.trim()&&o.push({type:c,markdown:u}),c=null,g=[]},h=u=>u.type==="code";for(let u=0;u<l.length;u++){const m=l[u],f=l[u+1];if(m.type==="removed"&&f?.type==="added"&&h(m.block)&&h(f.block)){i(),o.push({type:"modified",codeDiff:{language:(f.block.language||m.block.language||"text").toString(),lines:ot(m.block.content||"",f.block.content||"")}}),u++;continue}if(m.type==="added"&&f?.type==="removed"&&h(m.block)&&h(f.block)){i(),o.push({type:"modified",codeDiff:{language:(m.block.language||f.block.language||"text").toString(),lines:ot(f.block.content||"",m.block.content||"")}}),u++;continue}c===m.type?g.push(m.block):(i(),c=m.type,g=[m.block])}return i(),o}function ot(e,t){const n=_t(e),r=_t(t),l=n.length,o=r.length,c=Array(l+1).fill(null).map(()=>Array(o+1).fill(0));for(let u=1;u<=l;u++)for(let m=1;m<=o;m++)n[u-1]===r[m-1]?c[u][m]=c[u-1][m-1]+1:c[u][m]=Math.max(c[u-1][m],c[u][m-1]);const g=[];let i=l,h=o;for(;i>0||h>0;)i>0&&h>0&&n[i-1]===r[h-1]?(g.unshift({type:"unchanged",text:n[i-1]}),i--,h--):h>0&&(i===0||c[i][h-1]>=c[i-1][h])?(g.unshift({type:"added",text:r[h-1]}),h--):(g.unshift({type:"removed",text:n[i-1]}),i--);for(let u=0;u<g.length-1;u++){const m=g[u],f=g[u+1];if(m.type==="removed"&&f.type==="added"){const M=dn(m.text,f.text);m.inline=M.filter(T=>T.type!=="added"),f.inline=M.filter(T=>T.type!=="removed"),u++}}return g}function Ot(e){const t=[];let n="",r=!1;for(const l of e){const o=/\s/.test(l);o!==r&&n&&(t.push(n),n=""),n+=l,r=o}return n&&t.push(n),t}function _t(e){return e.split(`
|
|
31
|
+
`)}function et(e,t,n,r){const l=n.map(it).join("||"),o=r.map(it).join("||");return"d_"+Cr(`${e}|${t}|${l}|->|${o}`).toString(36)}function Cr(e){let t=2166136261;for(let n=0;n<e.length;n++)t^=e.charCodeAt(n),t=t+((t<<1)+(t<<4)+(t<<7)+(t<<8)+(t<<24))>>>0;return t>>>0}function it(e){return e.type==="paragraph"||e.type==="blockquote"?`${e.type}|${je(e.spans,{forParsing:!0})}`:e.type==="heading"?`heading|${e.level||1}|${je(e.spans,{forParsing:!0})}`:e.type==="list"?`list|${e.listType||"unordered"}|${e.depth||0}|${je(e.spans,{forParsing:!0})}`:e.type==="table"?`table|${me([e])}`:e.type==="code"?`code|${e.language||""}|${e.content||""}`:e.type==="mermaid"?`mermaid|${e.content||""}`:e.type==="math"?`math|${e.content||""}`:e.type==="image"?`image|${e.content||""}`:e.type==="divider"?"divider":`${e.type}|${e.content||""}`}function xt(e,t){if(e.length===0&&t.length===0)return[];const n=e.map(it),r=t.map(it),l=n.length,o=r.length,c=Array(l+1).fill(null).map(()=>Array(o+1).fill(0));for(let u=1;u<=l;u++)for(let m=1;m<=o;m++)n[u-1]===r[m-1]?c[u][m]=c[u-1][m-1]+1:c[u][m]=Math.max(c[u-1][m],c[u][m-1]);const g=[];let i=l,h=o;for(;i>0||h>0;)i>0&&h>0&&n[i-1]===r[h-1]?(g.unshift({type:"unchanged",block:e[i-1]}),i--,h--):h>0&&(i===0||c[i][h-1]>=c[i-1][h])?(g.unshift({type:"added",block:t[h-1]}),h--):(g.unshift({type:"removed",block:e[i-1]}),i--);return g}function Mr(e){if(e.length===0)return[];const t=[];let n=e[0].type,r=[e[0].block];for(let l=1;l<e.length;l++){const o=e[l];o.type===n?r.push(o.block):(t.push({type:n,blocks:r}),n=o.type,r=[o.block])}return t.push({type:n,blocks:r}),t}function Tr(e){if(e.length===0)return[];const t=[];let n={...e[0]};for(let r=1;r<e.length;r++)e[r].type===n.type?n.text+=e[r].text:(t.push(n),n={...e[r]});return t.push(n),t}function Er(e){if(e.length===0)return[];const t=[];let n={...e[0]};for(let r=1;r<e.length;r++)e[r].type===n.type?n.markdown+=`
|
|
32
32
|
|
|
33
|
-
`+e[r].markdown:(
|
|
33
|
+
`+e[r].markdown:(t.push(n),n={...e[r]});return t.push(n),t}function Br(e){const{baseMarkdown:t,nextMarkdown:n,defaultDecision:r="unset",applyFallback:l="reject"}=e,o=x.useMemo(()=>ln(t,n),[t,n]),c=x.useMemo(()=>o.map(v=>v.id).join("|"),[o]),[g,i]=x.useState({});x.useEffect(()=>{i(v=>{const E={};for(const U of o)U.type!=="unchanged"&&(E[U.id]=v[U.id]??r);return E})},[c,r,o]);const h=x.useCallback(v=>{i(E=>({...E,[v]:"accept"}))},[]),u=x.useCallback(v=>{i(E=>({...E,[v]:"reject"}))},[]),m=x.useCallback(v=>{i(E=>({...E,[v]:"unset"}))},[]),f=x.useCallback(()=>{i(()=>{const v={};for(const E of o)E.type!=="unchanged"&&(v[E.id]=r);return v})},[o,r]),M=x.useMemo(()=>cn(o,g,{defaultDecision:l}),[o,g,l]),T=x.useMemo(()=>{let v=0,E=0,U=0,G=0,ae=0,le=0,b=0;for(const w of o){if(w.type==="unchanged"){v++;continue}w.type==="added"?E++:w.type==="removed"?U++:w.type==="modified"&&G++;const B=g[w.id]??r;B==="unset"?b++:B==="accept"?ae++:le++}return{unchangedItems:v,addedItems:E,removedItems:U,modifiedItems:G,totalChangeItems:E+U+G,accepted:ae,rejected:le,pending:b}},[o,g,r]);return{items:o,decisions:g,accept:h,reject:u,unset:m,resetAll:f,resultMarkdown:M,stats:T}}exports.BlockquoteBlock=nn;exports.CodeBlock=Yt;exports.DividerBlock=rn;exports.HeadingBlock=Gt;exports.ImageBlock=Jt;exports.ListBlock=tn;exports.MathBlock=Zt;exports.MermaidBlock=en;exports.NotionStream=vr;exports.ParagraphBlock=Qt;exports.applyMarkdownDiffReview=cn;exports.applyStyleToRange=tt;exports.blocksToMarkdown=me;exports.computeCodeDiff=ot;exports.computeDiff=dn;exports.computeMarkdownBlockDiff=Nr;exports.computeMarkdownDiffReviewItems=ln;exports.computeMarkdownRenderDiff=Sr;exports.domToSpans=_e;exports.getSelectionTextOffsets=be;exports.mergeAdjacentSpans=Ie;exports.parseMarkdown=Oe;exports.parseMarkdownToSpans=Re;exports.renderSpans=mt;exports.setSelectionTextOffsets=Be;exports.spansToHtml=Qe;exports.spansToMarkdown=je;exports.toggleStyle=Tn;exports.useBlockState=jr;exports.useDiffReview=Br;exports.useMarkdownShortcuts=zt;exports.useStreamParser=Vt;
|