@ashwin_droid/notion-stream 1.1.3 → 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 +28 -28
- package/dist/index.mjs +1532 -1476
- 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";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 a of wn(t))!vn.call(e,a)&&a!==n&&vt(e,a,{get:()=>t[a],enumerable:!(r=bn(t,a))||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 qt=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(a=>{r||n(a.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 Wt(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,a=t[t.length-1];if(a&&a.style===r.style&&(a.style!=="link"||a.href===r.href)){a.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 a=e.indexOf(t,r);if(a===-1)return-1;if(!Ft(e,a))return a;r=a+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,a="";const o=()=>{a&&(n.push({text:a,style:pt(t)}),a="")};for(;r<e.length;){if(e[r]==="\\"&&r+1<e.length){a+=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 f=e.slice(r+1,i),m=e.slice(i+2,h);n.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)),n.push({text:m,style:"code"}),r=f+i;continue}a+=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 l=[{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 l)if(e.startsWith(i,r)){const f=dt(e,i,r+i.length);if(f!==-1){o();const m=e.slice(r+i.length,f);n.push(...Ht(m,h(t))),r=f+i.length,g=!0}break}g||(a+=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=Wt(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:a,close:o}=En(r.style),l=n?r.text:jt(r.text);return a+l+o}).join("")}function mt(e){if(!e.length)return null;let t=0;return e.map((n,r)=>{const a=t,o=n.text.length;t+=o;const l=`${r}-${n.style}`,g=a,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})},l);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})},l);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})},l);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})})},l);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})})},l);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})})},l);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})})})},l);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})},l);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})},l);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})},l);default:return s.jsx("span",{"data-span-start":g,"data-span-len":i,"data-span-style":h,children:n.text},l)}})}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 a=0;for(const o of e){const l=a,g=a+o.text.length;g>t&&l<n&&r.push(o),a=g}return r}function An(e,t,n,r){const a=Xt(e,t,n);return a.length===0?!1:a.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,a={}){const o=Math.max(0,Math.min(t,n)),l=Math.max(0,Math.max(t,n));if(o===l)return e;const g=r==="math"?Rn(e,o,l,r):An(e,o,l,r);if(r==="math"&&g){const f=[];let m=0;for(const u of e){const M=m,T=m+u.text.length;T>o&&M<l&&u.style==="math"?f.push({...u,style:"plain"}):f.push({...u}),m=T}return Ie(f)}const i=[];let h=0;for(const f of e){const m=h,u=h+f.text.length;if(u<=o||m>=l){i.push({...f}),h=u;continue}const M=Math.max(0,o-m),T=Math.min(f.text.length,l-m);if(M>0){const[v,$]=St(f,M);i.push(v),f.text=$.text}if(T-M<f.text.length){const[v,$]=St(f,T-M),G=Ct(v,r,g,a);i.push(G),i.push($)}else{const v=Ct(f,r,g,a);i.push(v)}h=u}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=Wt(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+(.*)$/,qn=/^(\s*)[-*]\s+(.*)$/,Wn=/^(\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 a=0;a<e.length;a++){const o=e[a];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(a=>a.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],isComplete:!0};const r=e.match(
|
|
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
|
-
`;t.push(i)}t.push(g),o=l.type}return t.join("")}function Vt({markdown:e,isStreaming:t}){const n=x.useRef([]);return{blocks:x.useMemo(()=>{const a=Oe(e,t,n.current);return n.current=a,a},[e,t])}}const Gn={"# ":1,"## ":2,"### ":3,"#### ":4,"##### ":5,"###### ":6},Yn={"- ":"unordered","* ":"unordered","1. ":"ordered"},Et={"\\* ":"* ","\\- ":"- ","\\> ":"> "};function zt({onBlockTypeChange:e}={}){const t=x.useCallback(a=>{for(const[o,l]of Object.entries(Et))if(a===o)return l;return null},[]),n=x.useCallback(a=>{for(const l of Object.keys(Et))if(a===l)return null;for(const[l,g]of Object.entries(Gn))if(a.startsWith(l))return{newType:"heading",newContent:a.slice(l.length),level:g};for(const[l,g]of Object.entries(Yn))if(a.startsWith(l))return{newType:"list",newContent:a.slice(l.length),listType:g};if(a.startsWith("> "))return{newType:"blockquote",newContent:a.slice(2)};if(a==="---"||a==="***"||a==="___")return{newType:"divider",newContent:""};const o=a.match(/^```([\w-]*)\s$/);return o?{newType:"code",newContent:"",language:o[1]||"text"}:null},[]),r=x.useCallback((a,o,l,g)=>{if(a.key===" "){const i=l.slice(0,g)+" ",h=l.slice(g);if(t(i)&&o.type==="paragraph")return!1;const m=n(i);if(m&&o.type==="paragraph"){a.preventDefault();const u=m.newContent+h;return e?.(o.id,m.newType,u,{level:m.level,listType:m.listType,language:m.language}),!0}if(m&&m.newType==="list"&&o.type==="list"&&m.listType!==o.listType){a.preventDefault();const u=m.newContent+h;return e?.(o.id,"list",u,{listType:m.listType,depth:o.depth??0}),!0}}if(a.key==="Enter"){const i=l.slice(0,g)+" ",h=n(i);if(h?.newType==="code"&&o.type==="paragraph"){a.preventDefault();const f=l.slice(g);return e?.(o.id,h.newType,f,{language:h.language}),!0}}return a.key==="Backspace"&&g===0&&o.type!=="paragraph"&&l===""?(a.preventDefault(),e?.(o.id,"paragraph",""),!0):(a.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,a=!1,o=!1,l=e.parentNode;for(;l&&l.nodeType===Node.ELEMENT_NODE;){const g=l,i=g.tagName.toLowerCase();i==="code"&&(r=!0),i==="a"&&(a=!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),l=l.parentNode}return r?{style:"code"}:o?{style:"math"}:a?{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 a=t;if(n>0)return a.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 a=tr(e,t,n);if(a&&a.nodeType===Node.ELEMENT_NODE&&a.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 a=r.textContent||"";if(!a||gt(r))continue;const o=Zn(r);if(o.style==="link"){t.push({text:a,style:"link",href:o.href});continue}if(o.style==="math"){t.push({text:a,style:"math"});continue}t.push({text:a,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),a=At(e,n.endContainer,n.endOffset);return r===null||a===null?null:{start:r,end:a,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 a=r.cloneContents();return a.querySelectorAll?.('[data-math-delim="1"]').forEach(o=>o.remove()),a.textContent?.length??0}function Be(e,t,n,r={}){const a=Rt(e,t,r.startAffinity),o=Rt(e,n,r.endAffinity);if(!a||!o)return;const l=document.createRange();l.setStart(a.node,a.offset),l.setEnd(o.node,o.offset);const g=window.getSelection();g?.removeAllRanges(),g?.addRange(l)}function Rt(e,t,n){const r=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null);let a=0,o;for(;o=r.nextNode();){if(gt(o))continue;const l=o.textContent?.length||0;if(a+l>t)return{node:o,offset:Math.max(0,t-a)};if(a+l===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 f=Array.prototype.indexOf.call(h.childNodes,g);if(f!==-1)return{node:h,offset:f+1}}}}return{node:o,offset:l}}a+=l}return{node:e,offset:e.childNodes.length}}const nr=220,Lt=8;function rr({containerRef:e,onFormat:t,isMathSelection:n}){const[r,a]=x.useState(!1),[o,l]=x.useState({top:0,left:0}),[g,i]=x.useState(!1),h=x.useRef(null),f=x.useRef(!1),m=x.useCallback(()=>{const M=window.getSelection();if(!M||M.isCollapsed||!M.rangeCount){f.current||(a(!1),i(!1));return}const T=M.getRangeAt(0);if(!e.current?.contains(T.commonAncestorContainer)){a(!1),i(!1);return}i(!!n?.(T));const v=T.getBoundingClientRect(),$=e.current?.getBoundingClientRect();if(!$)return;const G=h.current?.offsetWidth||nr,te=h.current?.offsetHeight||40,ae=v.left+v.width/2-$.left,le=v.top-$.top,y=v.bottom-$.top;let w=ae-G/2;const E=Lt,_=$.width-G-Lt;w=Math.max(E,Math.min(_,w));let U=le-te-8;U<0&&(U=y+8),l({top:U,left:w}),a(!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)&&a(!1)};return document.addEventListener("mousedown",M),()=>{document.removeEventListener("mousedown",M)}},[e]);const u=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:()=>{f.current=!0},onMouseLeave:()=>{f.current=!1},children:[s.jsx(ze,{onClick:()=>u("bold"),disabled:g,children:s.jsx(sr,{})}),s.jsx(ze,{onClick:()=>u("italic"),disabled:g,children:s.jsx(or,{})}),s.jsx(ze,{onClick:()=>u("strikethrough"),disabled:g,children:s.jsx(ir,{})}),s.jsx(ze,{onClick:()=>u("code"),disabled:g,children:s.jsx(ar,{})}),s.jsx(ze,{onClick:()=>u("math"),disabled:!1,children:s.jsx(cr,{})}),s.jsx("div",{className:"w-px h-5 bg-notion-border mx-1"}),s.jsx(ze,{onClick:()=>u("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[a,o]=x.useState(r),l=x.useRef(null),g=x.useRef(null);x.useEffect(()=>{e&&(o(r),requestAnimationFrame(()=>{l.current?.focus(),l.current?.select()}))},[e,r]);const i=x.useCallback(f=>{f.preventDefault();const m=a.trim();if(m){const u=m.match(/^https?:\/\//)?m:`https://${m}`;t(u)}},[a,t]),h=x.useCallback(f=>{f.key==="Escape"&&(f.preventDefault(),f.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:l,type:"text",value:a,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: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:!a.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:a,onKeyDown:o,className:l="",editingClassName:g="",viewClassName:i="",placeholder:h="Type something...",renderWrapper:f,readonly:m=!1}){const u=x.useRef(null),M=x.useRef(null),T=x.useRef(null),v=x.useRef(e.spans),$=x.useRef(null),[G,te]=x.useState(!1),ae=x.useRef(null),le=x.useRef(null),y=x.useCallback(()=>t?u.current:M.current,[t]);x.useEffect(()=>()=>{$.current&&clearTimeout($.current)},[]),x.useEffect(()=>{if(t&&u.current){u.current.innerHTML=Qe(e.spans),v.current=e.spans,u.current.focus();const R=le.current;if(le.current=null,R)Be(u.current,R.start,R.end,{startAffinity:R.startAffinity,endAffinity:R.endAffinity});else{const X=document.createRange(),J=window.getSelection();X.selectNodeContents(u.current),X.collapse(!1),J?.removeAllRanges(),J?.addRange(X)}}},[t]),x.useEffect(()=>{if(!t||!u.current)return;const R=u.current,X=Qe(e.spans),J=R.innerHTML;if(X===J)return;const W=be(R);R.innerHTML=X,W&&Be(R,W.start,W.end,{startAffinity:W.startAffinity,endAffinity:W.endAffinity}),v.current=e.spans},[e.spans,t]);const w=x.useCallback(()=>{if(!u.current)return;const R=be(u.current),X=_e(u.current);v.current=X,n(X);const W=Array.from(u.current.querySelectorAll('[data-math="1"]')).every(de=>de.querySelectorAll('[data-math-delim="1"]').length===2);$.current&&clearTimeout($.current);const F=()=>{if(!u.current)return;const de=_e(u.current),ue=Qe(de),fe=u.current.innerHTML;ue!==fe&&(u.current.innerHTML=ue,R&&Be(u.current,R.start,R.end,{startAffinity:R.startAffinity,endAffinity:R.endAffinity}))};W?$.current=setTimeout(F,hr):F()},[n]),E=x.useCallback(R=>{if(!R.relatedTarget?.closest(".floating-toolbar")&&!R.relatedTarget?.closest(".link-modal")){if(u.current){const X=_e(u.current);n(X)}a()}},[n,a]),_=x.useCallback(()=>{!t&&!m&&r()},[t,r,m]),U=x.useCallback(R=>{if(!M.current)return;const X=M.current,J=(ce,pe)=>{const ie=document;if(typeof ie.caretRangeFromPoint=="function")return ie.caretRangeFromPoint(ce,pe)||null;if(typeof ie.caretPositionFromPoint=="function"){const H=ie.caretPositionFromPoint(ce,pe);if(!H)return null;const j=document.createRange();return j.setStart(H.offsetNode,H.offset),j.collapse(!0),j}return null},W=window.getSelection();let F=null;if(W&&W.rangeCount>0&&(F=W.getRangeAt(0)),(!F||!X.contains(F.startContainer)||!X.contains(F.endContainer))&&R){const ce=J(R.clientX,R.clientY);ce&&(F=ce)}if(!F||!X.contains(F.startContainer)||!X.contains(F.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 H=ce,j=H.childNodes[pe]||null,S=pe>0&&H.childNodes[pe-1]||null,Z=we=>we&&we.nodeType===Node.ELEMENT_NODE?we.closest("[data-span-start]"):null;return ie==="start"?Z(j)||Z(S):Z(S)||Z(j)},fe=(ce,pe,ie)=>{let H=de(ce);if(H||(H=ue(ce,pe,ie)),!H)return null;const j=H.getAttribute("data-span-start"),S=H.getAttribute("data-span-len");if(!j||!S)return null;const Z=Number(j),we=Number(S);if(H.getAttribute("data-span-style")==="math")return{pos:ie==="start"?Z:Z+we,affinity:"insideMath"};try{const Ce=document.createRange();Ce.selectNodeContents(H),Ce.setEnd(ce,pe);const Pe=Ce.toString().length;return{pos:Z+Pe}}catch{return{pos:ie==="start"?Z:Z+we}}},ke=de(F.startContainer)||ue(F.startContainer,F.startOffset,"start");if(F.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(F.startContainer,F.startOffset,"start"),se=fe(F.endContainer,F.endOffset,"end");!oe||!se||(le.current={start:oe.pos,end:se.pos,startAffinity:oe.affinity,endAffinity:se.affinity})},[]),O=x.useCallback((R,X)=>{const J=y();if(!J)return;const W=be(J);if(!(!W||W.start===W.end))if(t&&u.current){const F=_e(u.current),de=tt(F,W.start,W.end,R,X);u.current.innerHTML=Qe(de),Be(u.current,W.start,W.end,{startAffinity:W.startAffinity,endAffinity:W.endAffinity}),v.current=de,n(de)}else{const F=tt(e.spans,W.start,W.end,R,X);n(F),window.getSelection()?.removeAllRanges()}},[t,e.spans,n,y]),ne=x.useCallback(R=>{if((R.key==="ArrowRight"||R.key==="ArrowLeft")&&u.current){const X=u.current,J=window.getSelection();if(J&&J.rangeCount>0){const W=J.getRangeAt(0);if(W.collapsed&&X.contains(W.startContainer)){const F=H=>{let j=H.nodeType===Node.ELEMENT_NODE?H:H.parentElement;for(;j&&j!==X;){if(j.getAttribute("data-math")==="1")return j;j=j.parentElement}return null},de=H=>{let j=H.parentNode;for(;j&&j.nodeType===Node.ELEMENT_NODE;){if(j.getAttribute("data-math-delim")==="1")return!0;j=j.parentNode}return!1},ue=H=>{const j=document.createTreeWalker(H,NodeFilter.SHOW_TEXT,null);let S;for(;S=j.nextNode();)if(!de(S))return S;return null},fe=H=>{const j=document.createTreeWalker(H,NodeFilter.SHOW_TEXT,null);let S=null,Z;for(;Z=j.nextNode();)de(Z)||(S=Z);return S},ke=H=>{const j=H.parentNode;if(!j)return;const S=Array.prototype.indexOf.call(j.childNodes,H);if(S===-1)return;const Z=document.createRange();Z.setStart(j,S+1),Z.collapse(!0),J.removeAllRanges(),J.addRange(Z)},oe=H=>{const j=H.parentNode;if(!j)return;const S=Array.prototype.indexOf.call(j.childNodes,H);if(S===-1)return;const Z=document.createRange();Z.setStart(j,S),Z.collapse(!0),J.removeAllRanges(),J.addRange(Z)},se=H=>{const j=ue(H),S=document.createRange();j?S.setStart(j,0):S.setStart(H,1),S.collapse(!0),J.removeAllRanges(),J.addRange(S)},ce=H=>{const j=fe(H),S=document.createRange();j?S.setStart(j,j.textContent?.length??0):S.setStart(H,1),S.collapse(!0),J.removeAllRanges(),J.addRange(S)},pe=()=>{const H=W.startContainer,j=W.startOffset;if(H.nodeType===Node.ELEMENT_NODE){const Z=H;if(j>0)return Z.childNodes[j-1]||null}if(H.nodeType===Node.TEXT_NODE&&j>0)return null;let S=H;for(;S&&S!==X;){if(S.previousSibling)return S.previousSibling;S=S.parentNode}return null},ie=()=>{const H=W.startContainer,j=W.startOffset;if(H.nodeType===Node.ELEMENT_NODE)return H.childNodes[j]||null;if(H.nodeType===Node.TEXT_NODE){const Z=H.textContent||"";if(j<Z.length)return null}let S=H;for(;S&&S!==X;){if(S.nextSibling)return S.nextSibling;S=S.parentNode}return null};if(R.key==="ArrowRight"){const H=F(W.startContainer);if(H){const S=fe(H);if(S&&W.startContainer===S&&W.startOffset===(S.textContent?.length??0)){R.preventDefault(),ke(H);return}}const j=ie();if(j&&j.nodeType===Node.ELEMENT_NODE){const S=j;if(S.getAttribute("data-math")==="1"){R.preventDefault(),se(S);return}}}if(R.key==="ArrowLeft"){const H=pe();if(H&&H.nodeType===Node.ELEMENT_NODE){const S=H;if(S.getAttribute("data-math")==="1"){R.preventDefault(),ce(S);return}}const j=F(W.startContainer);if(j){const S=ue(j);if(S&&W.startContainer===S&&W.startOffset===0){R.preventDefault(),oe(j);return}}}}}}if((R.metaKey||R.ctrlKey)&&u.current)switch(R.key.toLowerCase()){case"b":R.preventDefault(),O("bold");return;case"i":R.preventDefault(),O("italic");return;case"e":R.preventDefault(),O("code");return;case"m":R.preventDefault(),O("math");return;case"k":R.preventDefault();const X=be(u.current);X&&X.start!==X.end&&(ae.current=X,te(!0));return}if(R.key==="Escape"){if(u.current){const X=_e(u.current);n(X)}a();return}o(R)},[O,n,a,o]),Q=x.useCallback(R=>{const X=y();if(X){if(R==="link"){const J=be(X);J&&J.start!==J.end&&(ae.current=J,te(!0));return}O(R)}},[O,y]),ge=x.useCallback(R=>{if(te(!1),!ae.current)return;const X=ae.current;if(ae.current=null,t&&u.current){const J=_e(u.current),W=tt(J,X.start,X.end,"link",{href:R});u.current.innerHTML=Qe(W),Be(u.current,X.start,X.end,{startAffinity:X.startAffinity,endAffinity:X.endAffinity}),v.current=W,n(W),u.current.focus()}else{const J=tt(e.spans,X.start,X.end,"link",{href:R});n(J),window.getSelection()?.removeAllRanges()}},[t,e.spans,n]),Ne=x.useCallback(()=>{te(!1),ae.current=null,t&&u.current?.focus()},[t]),Se=x.useCallback(R=>{if(t&&u.current){const X=be(u.current);if(!X||X.start===X.end)return!1;const J=_e(u.current);let W=0;for(const F of J){const de=W,ue=W+F.text.length;if(ue>X.start&&de<X.end&&F.style==="math")return!0;W=ue}return!1}if(!t&&M.current){const X=M.current,J=Array.from(X.querySelectorAll('[data-span-style="math"]'));for(const W of J)try{if(R.intersectsNode(W))return!0}catch{}}return!1},[t]),Me=x.useCallback(R=>{const J=R.nativeEvent.inputType,W=J==="deleteContentBackward"||J==="deleteContentForward",F=J==="insertText"||J==="insertCompositionText"||J==="insertFromPaste";if(!W&&!F)return;const de=u.current;if(!de)return;const ue=window.getSelection();if(!ue||ue.rangeCount===0)return;const fe=ue.getRangeAt(0),ke=j=>{let S=j.nodeType===Node.ELEMENT_NODE?j:j.parentElement;for(;S&&S!==de;){if(S.getAttribute("data-math")==="1")return S;S=S.parentElement}return null},oe=j=>{const S=j.parentNode;if(!S)return;const Z=Array.prototype.indexOf.call(S.childNodes,j);if(Z===-1)return;const we=document.createRange();we.setStart(S,Z+1),we.collapse(!0),ue.removeAllRanges(),ue.addRange(we)},se=j=>{if(!j)return!1;if(j.nodeType===Node.ELEMENT_NODE){const Z=j;return Z.getAttribute("data-math-delim")==="1"?!0:!!Z.querySelector('[data-math-delim="1"]')}const S=j.parentElement;return!!S&&S.getAttribute("data-math-delim")==="1"},ce=j=>{let S=j;for(;S&&S!==de;){if(S.previousSibling)return S.previousSibling;S=S.parentNode}return null},pe=j=>{let S=j;for(;S&&S!==de;){if(S.nextSibling)return S.nextSibling;S=S.parentNode}return null},ie=(j,S)=>{if(j.nodeType===Node.ELEMENT_NODE){const Z=j;return S>0?Z.childNodes[S-1]||null:ce(j)}return j.nodeType===Node.TEXT_NODE&&S>0?null:ce(j)},H=(j,S)=>{if(j.nodeType===Node.ELEMENT_NODE)return j.childNodes[S]||pe(j);if(j.nodeType===Node.TEXT_NODE){const Z=j.textContent||"";return S<Z.length?null:pe(j)}return pe(j)};if(!fe.collapsed){fe.cloneContents().querySelector?.('[data-math-delim="1"]')&&W&&R.preventDefault();return}if(F){const j=ke(fe.startContainer);if(j&&fe.startContainer===j&&fe.startOffset>=j.childNodes.length){oe(j);return}}if(W){const j=J==="deleteContentBackward"?ie(fe.startContainer,fe.startOffset):H(fe.startContainer,fe.startOffset);se(j)&&R.preventDefault()}},[]),Ye=e.spans.length===0||e.spans.length===1&&e.spans[0].text==="",Je=s.jsx("div",{ref:u,className:`outline-none cursor-text ${l} ${g}`,contentEditable:!0,suppressContentEditableWarning:!0,"data-placeholder":h,"data-empty":Ye?"1":void 0,onMouseDown:R=>{const J=R.target.closest?.('[data-math-delim="1"]');if(!J||!u.current)return;const W=J.closest?.('[data-math="1"]');if(!W)return;R.preventDefault();const F=window.getSelection();if(!F)return;const de=W.firstElementChild===J,ue=document.createTreeWalker(W,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(W,1):ke?se.setStart(ke,ke.textContent?.length??0):se.setStart(W,1),se.collapse(!0),F.removeAllRanges(),F.addRange(se)},onBeforeInput:Me,onInput:w,onBlur:E,onKeyDown:ne},"editing"),Te=s.jsxs("div",{ref:M,className:`outline-none ${m?"cursor-default":"cursor-text"} ${l} ${i}`,onMouseUp:m?void 0:U,onClick:m?void 0:_,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?f?f(Je,!0):Je:f?f(Te,!1):Te;return s.jsxs("div",{ref:T,className:"relative",children:[!m&&s.jsxs(s.Fragment,{children:[s.jsx(rr,{containerRef:T,onFormat:Q,isMathSelection:Se}),s.jsx(dr,{isOpen:G,onSubmit:ge,onCancel:Ne})]}),Ae]})}function Qt({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:a,onKeyDown:o,readonly:l=!1}){return s.jsx(nt,{block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:a,onKeyDown:o,className:"notion-paragraph text-notion-text leading-relaxed min-h-[1.5em]",placeholder:"Type '/' for commands...",readonly:l})}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:a,onKeyDown:o,readonly:l=!1}){const g=e.level||1,i=Dt[g]||Dt[1];return s.jsx(nt,{block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:a,onKeyDown:o,className:`notion-heading ${i} text-notion-text`,placeholder:`Heading ${g}`,readonly:l,renderWrapper:(h,f)=>s.jsx("div",{role:"heading","aria-level":g,children:h})})}function Yt(e){const{block:t,isEditing:n,onContentChange:r,onStartEdit:a,onEndEdit:o,onKeyDown:l,readonly:g=!1}=e,i=x.useRef(null),[h,f]=x.useState(null);x.useEffect(()=>{let v=!1;return import("@streamdown/code").then($=>{v||f($.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)},u=()=>{o()},M=v=>{if(v.key==="Tab"){v.preventDefault();const $=v.currentTarget,G=$.selectionStart,te=$.selectionEnd,ae=$.value,le=ae.substring(0,G)+" "+ae.substring(te);r(le),setTimeout(()=>{$.selectionStart=$.selectionEnd=G+2},0);return}if(v.key==="Escape"){o();return}l(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:
|
|
19
|
-
`).length),spellCheck:!1})]}):h?s.jsxs("div",{className:`notion-code-block ${g?"cursor-default":"cursor-text"}`,onClick:g?void 0:
|
|
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
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:
|
|
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:
|
|
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
|
|
25
|
-
`);w.insertNode(
|
|
26
|
-
`)}function tn({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:a,onKeyDown:o,readonly:l=!1}){const g=e.listType==="ordered",i=e.depth??0,h=e.metadata?.displayNumber,f=g?`${h??1}.`:"•";return s.jsx(nt,{block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:a,onKeyDown:o,className:"flex-1",placeholder:"Empty item",readonly:l,renderWrapper:(m,u)=>s.jsxs("div",{className:`notion-list-block flex items-start gap-2 py-0.5 ${l?"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 nn({block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:a,onKeyDown:o,readonly:l=!1}){return s.jsx(nt,{block:e,isEditing:t,onContentChange:n,onStartEdit:r,onEndEdit:a,onKeyDown:o,className:"text-notion-text italic leading-relaxed",placeholder:"Empty quote",readonly:l,renderWrapper:(g,i)=>s.jsx("div",{className:`notion-blockquote border-l-4 border-notion-quote-border pl-4 py-0.5 ${l?"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:a,onKeyDown:o,hooks:l,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:t,onStartEdit:n,onEndEdit:r,onContentChange:a,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:a,onKeyDown:o,onExitTable:g,readonly:h});const u={block:e,isEditing:t,onStartEdit:n,onEndEdit:r,onContentChange:a,onKeyDown:o,readonly:h};switch(e.type){case"code":return s.jsx(Yt,{...u,theme:i});case"image":return s.jsx(Jt,{...u});case"math":return s.jsx(Zt,{...u,theme:i});case"mermaid":return s.jsx(en,{...u,theme:i});case"divider":return s.jsx(rn,{...u});default:return null}},m=`notion-block relative py-1 px-1 -mx-1 rounded ${h?"":"hover:bg-notion-hover/50"} transition-colors`;if(l?.renderBlock){const u=l.renderBlock(e,f);if(u!==null)return s.jsx("div",{className:m,"data-block-id":e.id,"data-block-type":e.type,children:u})}return s.jsx("div",{className:m,"data-block-id":e.id,"data-block-type":e.type,children:f()})}function sn(...e){return e.filter(Boolean).join(" ")}const on=qt.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=qt.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:a,onClose:o,onSelect:l,onHoverIndex:g}){const i=x.useRef(null);x.useEffect(()=>{if(!e)return;const f=m=>{const u=i.current;u&&(u.contains(m.target)||o())};return document.addEventListener("mousedown",f,!0),()=>document.removeEventListener("mousedown",f,!0)},[e,o]);const h=x.useMemo(()=>r.length?wr(a,0,r.length-1):0,[a,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((f,m)=>{const u=m===h;return s.jsxs("button",{type:"button",onMouseEnter:()=>g?.(m),onMouseDown:M=>M.preventDefault(),onClick:()=>l(f),className:`w-full text-left px-2 py-2 rounded-md transition-colors flex items-start gap-2 ${u?"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 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(a=>{if(!t.has(a.id))return t.add(a.id),a;n=!0;let o=$e(10);for(;t.has(o);)o=$e(10);return t.add(o),{...a,id:o}});return n?r:e}const It=360,vr=x.forwardRef(function({markdown:t,isStreaming:n=!1,hooks:r,onMarkdownChange:a,className:o="",theme:l,readonly:g=!1},i){const h=()=>{if(typeof window>"u")return"light";const p=document.documentElement,c=document.body;return p&&p.classList.contains("dark")||c&&c.classList.contains("dark")||window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"},[f,m]=x.useState(()=>l==="dark"||l==="light"?l:l==="system"?typeof window<"u"&&window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light":h());x.useEffect(()=>{if(l==="dark"||l==="light"){m(l);return}if(typeof window>"u"){m("light");return}const p=()=>{if(l==="system"){if(!window.matchMedia){m("light");return}m(window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light");return}m(h())};p();const c=[];if(l!=="system"&&typeof MutationObserver<"u"){const b=N=>{if(!N)return;const V=new MutationObserver(()=>p());V.observe(N,{attributes:!0,attributeFilter:["class"]}),c.push(V)};b(document.documentElement),b(document.body)}const d=window.matchMedia?window.matchMedia("(prefers-color-scheme: dark)"):null,k=()=>p();return d&&d.addEventListener("change",k),()=>{for(const b of c)b.disconnect();d&&d.removeEventListener("change",k)}},[l]);const[u,M]=x.useState(null),[T,v]=x.useState(null),[$,G]=x.useState(null);x.useEffect(()=>{let p=!1;return Promise.all([import("@streamdown/code"),import("@streamdown/math"),import("@streamdown/mermaid")]).then(([c,d,k])=>{p||(M(c.code),v(d.math),G(k.mermaid))}),()=>{p=!0}},[]);const te=x.useRef(null),{blocks:ae}=Vt({markdown:t,isStreaming:n}),[le,y]=x.useState(ae),[w,E]=x.useState([]),[_,U]=x.useState(null),[O,ne]=x.useState(null),Q=x.useRef(le);x.useEffect(()=>{Q.current=le},[le]);const ge=x.useRef(_);x.useEffect(()=>{ge.current=_},[_]);const Ne=x.useRef([]);x.useEffect(()=>{Ne.current=w},[w]);const Se=x.useCallback(p=>{Ne.current=p,E(p)},[]),Me=x.useRef(null),Ye=x.useRef(!1),Je=x.useRef(!1),Te=x.useRef(0),Ae=x.useRef([]),R=x.useRef(new Set),X=x.useRef(null);x.useEffect(()=>{const p=c=>{const d=te.current;if(!d||!d.contains(c.target))return;c.target?.closest?.('[contenteditable="true"], textarea')&&(X.current=null)};return document.addEventListener("mousedown",p,!0),()=>document.removeEventListener("mousedown",p,!0)},[]);const J=x.useRef(null),W=x.useCallback(p=>Q.current.find(c=>c.id===p)||null,[]),F=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 c=te.current;if(!c)return;let d=0,k=0;const b=document.querySelector(`[data-block-id="${p}"]`),N=c.getBoundingClientRect(),V=window.getSelection(),P=V&&V.rangeCount>0?V.getRangeAt(0):null,K=P?P.getBoundingClientRect():null;if(K&&K.width+K.height>0)d=K.bottom-N.top+8,k=K.left-N.left;else if(b){const z=b.getBoundingClientRect();d=z.top-N.top+28,k=z.left-N.left+28}const C=8,D=Math.max(C,N.width-It-8);k=ht(k,C,D),ne({blockId:p,query:"",activeIndex:0,position:{top:d,left:k}})},[]);x.useEffect(()=>{const p=Je.current;if(Je.current=n,!Ye.current&&!n){R.current.clear();const d=Ue(ae);Q.current=d,y(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()===""&&(R.current.clear(),y([]))),ae.length===0){Se([]);return}let d=ae.length;for(let P=ae.length-1;P>=0&&ae[P].isStreaming;P--)d=P;const k=ae.slice(0,d),b=ae.slice(d),N=R.current,V=Te.current;if(N.size===0){const P=k.map(D=>de({...D,isStreaming:!1})),K=Q.current,C=P.length!==K.length||P.some((D,z)=>{const L=K[z];return!L||D.type!==L.type||D.id!==L.id});if(C){const D=Ue(P);Q.current=D,y(D)}Ae.current=(C?Q.current:K).map(D=>D.id),Te.current=k.length}else{const P=k.length,K=A=>{if(A.type==="paragraph"||A.type==="heading"||A.type==="list"||A.type==="blockquote"){const he=A.level??"",xe=A.listType??"",ye=A.depth??"";return`${A.type}|${he}|${xe}|${ye}|${je(A.spans||[],{forParsing:!0})}`}return A.type==="table"?`table|${me([A])}`:A.type==="code"?`code|${A.language||""}|${A.content||""}`:A.type==="mermaid"?`mermaid|${A.content||""}`:A.type==="math"?`math|${A.content||""}`:A.type==="image"?`image|${A.content||""}`:A.type==="divider"?"divider":`${A.type}|${A.content||""}`},C=A=>!!A?.metadata?.dirty||N.has(A.id),D=Q.current;let z=D.slice(),L=!1;const I=D.slice(0,V).map(A=>A.id);let q=Ae.current.length>0?Ae.current.slice(0,V):I;if(P<V){const A=q.slice(P,V);if(A.length>0){const he=new Set(A),xe=z.filter(ye=>he.has(ye.id)?C(ye):!0);xe.length!==z.length&&(z=xe,L=!0)}q=q.slice(0,P)}const Y=Math.min(P,q.length),re=Math.max(0,Y-64);for(let A=re;A<Y;A++){const he=q[A];if(!he)continue;const xe=z.findIndex(Ve=>Ve.id===he);if(xe===-1)continue;const ye=z[xe];if(C(ye))continue;const Ee=de({...k[A],isStreaming:!1});K(ye)!==K(Ee)&&(z[xe]=Ee,q[A]=Ee.id,L=!0)}if(P>V){const A=k.slice(V).map(he=>de({...he,isStreaming:!1}));z=z.concat(A),q=q.concat(A.map(he=>he.id)),L=!0}if(Te.current=P,Ae.current=q,L){const A=Ue(z);Q.current=A,y(A)}}b.length===0&&(Te.current=k.length),Se(b.map(P=>de({...P,isStreaming:!0})));return}if(p&&!n){y(d=>{const k=d.slice(),b=Ne.current;for(const V of b)k.push(de({...V,isStreaming:!1}));const N=Ue(k.length>0?k:[{id:$e(10),type:"paragraph",spans:[],isStreaming:!1}]);return Q.current=N,N}),Se([]),Te.current=0,Ae.current=[];return}if(Me.current!==null&&t===Me.current)return;R.current.clear();const c=Ue(ae.map(d=>de(d)));Q.current=c,y(c),Se([]),Te.current=0,Ae.current=[]},[t,n,ae,de]);const ke=x.useCallback(p=>{const c=w.length>0?p.concat(w.map(k=>({...k,isStreaming:!1}))):p,d=me(c);Me.current=d,a?.(d)},[a,w]),oe=x.useCallback(p=>{const c=Ue(p);Q.current=c,y(c),ke(c)},[ke]),se=p=>p.type==="paragraph"||p.type==="heading"||p.type==="list"||p.type==="blockquote",ce=p=>{if(se(p))return p.spans.every(c=>c.text.trim()==="");if(p.type==="divider")return!1;if(p.type==="table"){const c=p.header||[],d=p.rows||[],k=b=>!!ut(b||[]).trim();return!(c.some(k)||d.some(b=>b.some(k)))}return p.content?.trim?.()===""},pe=x.useCallback(p=>{const c=r?.getBlockPolicy?.(p);return c||(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,c,d)=>{J.current={blockId:p,start:c,end:d},ge.current=p,U(p)},[]);x.useEffect(()=>{const p=J.current;if(!p)return;J.current=null;const{blockId:c,start:d,end:k}=p;requestAnimationFrame(()=>{const b=document.querySelector(`[data-block-id="${c}"]`);if(!b)return;const N=b.querySelector('[contenteditable="true"]');if(N){N.focus(),Be(N,d,k);return}const V=b.querySelector("textarea");V&&(V.focus(),V.setSelectionRange(d,k))})},[le,_]);const H=x.useCallback((p,c,d="bottom")=>{ge.current=p,U(p);const k=(N,V)=>{const P=document;if(typeof P.caretRangeFromPoint=="function")return P.caretRangeFromPoint(N,V)||null;if(typeof P.caretPositionFromPoint=="function"){const K=P.caretPositionFromPoint(N,V);if(!K)return null;const C=document.createRange();return C.setStart(K.offsetNode,K.offset),C.collapse(!0),C}return null},b=N=>{requestAnimationFrame(()=>{const P=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');if(!P){N<3&&b(N+1);return}P.focus();const K=P.getBoundingClientRect(),C=Math.max(K.left+4,Math.min(c,K.right-4)),D=d==="top"?K.top+4:K.bottom-4,z=k(C,D);if(z&&P.contains(z.startContainer)){z.collapse(!0);const I=window.getSelection();I?.removeAllRanges(),I?.addRange(z);return}const L=W(p);if(L&&se(L)){const I=L.spans.reduce((ee,q)=>ee+q.text.length,0);Be(P,I,I)}})};b(0)},[W]),j=x.useCallback((p,c,d,k)=>{R.current.add(p),oe(Q.current.map(N=>N.id!==p?N:c==="paragraph"?F({id:N.id,type:"paragraph",spans:Re(d),isStreaming:!1}):c==="heading"?F({id:N.id,type:"heading",spans:Re(d),level:k?.level??1,isStreaming:!1}):c==="blockquote"?F({id:N.id,type:"blockquote",spans:Re(d),isStreaming:!1}):c==="list"?F({id:N.id,type:"list",spans:Re(d),listType:k?.listType??"unordered",depth:k?.depth??0,isStreaming:!1}):c==="code"?F({id:N.id,type:"code",content:"",language:k?.language||"text",isStreaming:!1}):c==="divider"?F({id:N.id,type:"divider",isStreaming:!1}):N));const b=W(p);b&&r?.onBlockChange&&r.onBlockChange(p,d,b)},[oe,r,W,F]),{handleKeyDown:S}=zt({onBlockTypeChange:j}),Z=x.useCallback((p,c)=>{R.current.add(p);const d=Q.current.map(b=>{if(b.id!==p)return b;if(se(b)){const N=typeof c=="string"?Re(c):c;return F({...b,spans:N})}if(b.type==="table"){if(typeof c!="string")return b;const N=Oe(c,!1,[b]).find(V=>V.type==="table");return!N||N.type!=="table"?b:F({...N,isStreaming:!1})}return"content"in b?F({...b,content:typeof c=="string"?c:je(c)}):b});oe(d);const k=d.find(b=>b.id===p);k&&r?.onBlockChange&&r.onBlockChange(p,c,k)},[r,oe,F]),we=x.useCallback((p,c)=>{const d=$e(10);R.current.add(d);const k=F((()=>{switch(c.type){case"paragraph":return{id:d,type:"paragraph",spans:c.spans??[],isStreaming:!1};case"heading":return{id:d,type:"heading",spans:c.spans??[],level:c.level??1,isStreaming:!1};case"blockquote":return{id:d,type:"blockquote",spans:c.spans??[],isStreaming:!1};case"list":return{id:d,type:"list",spans:c.spans??[],listType:c.listType??"unordered",depth:c.depth??0,isStreaming:!1};case"code":return{id:d,type:"code",content:c.content??"",language:c.language??"text",isStreaming:!1};case"math":return{id:d,type:"math",content:c.content??"",isStreaming:!1};case"mermaid":return{id:d,type:"mermaid",content:c.content??"",isStreaming:!1};case"image":return{id:d,type:"image",content:c.content??"",isStreaming:!1};case"table":{const K=Math.max(1,c.cols??3),C=Math.max(0,c.rows??2);return{id:d,type:"table",header:Array.from({length:K},()=>[]),rows:Array.from({length:C},()=>Array.from({length:K},()=>[])),align:Array.from({length:K},()=>"left"),isStreaming:!1}}case"divider":return{id:d,type:"divider",isStreaming:!1}}})()),b=Q.current,N=p?b.findIndex(K=>K.id===p):b.length-1,V=N===-1?b.length:N+1,P=b.slice(0,V).concat([k],b.slice(V));return oe(P),d},[oe,F]),Le=x.useCallback(p=>{R.current.delete(p);const c=Q.current,d=c.findIndex(K=>K.id===p);if(d===-1)return;const k=c.slice(0,d).concat(c.slice(d+1)),b=k.length===0?[{id:$e(10),type:"paragraph",spans:[],isStreaming:!1}]:k;oe(b);const N=Math.max(0,Math.min(d-1,b.length-1)),V=b[N],P=se(V)?V.spans.reduce((K,C)=>K+C.text.length,0):V.content?.length??0;ie(V.id,P,P)},[oe,ie]),Ce=x.useCallback((p,c,d)=>{R.current.add(p);const k=Q.current,b=k.find(P=>P.id===p);if(!b)return;const N=se(b)?b.spans:[],V=(()=>{if(c==="paragraph")return{id:b.id,type:"paragraph",spans:N,isStreaming:!1};if(c==="heading")return{id:b.id,type:"heading",spans:N,level:d?.level??1,isStreaming:!1};if(c==="blockquote")return{id:b.id,type:"blockquote",spans:N,isStreaming:!1};if(c==="list")return{id:b.id,type:"list",spans:N,listType:d?.listType??"unordered",depth:d?.depth??0,isStreaming:!1};if(c==="table"){const C=Array.from({length:3},(D,z)=>z===0?N:[]);return{id:b.id,type:"table",header:C,rows:Array.from({length:2},()=>Array.from({length:3},()=>[])),align:Array.from({length:3},()=>"left"),isStreaming:!1}}return b})();oe(k.map(P=>P.id===p?F(V):P))},[oe,F]),Pe=x.useCallback((p,c=!1)=>{R.current.add(p);const d=Q.current,k=d.findIndex(D=>D.id===p),b=d[k],N=k>0?d[k-1]:null;if(!b||b.type!=="list"||!N||N.type!=="list")return;const V=b.depth??0,P=(N.depth??0)+1,K=Math.min(V+1,P);if(K===V)return;const C=c?b.listType:N.listType;oe(d.map(D=>D.id===p?F({...b,depth:K,listType:C}):D))},[oe,F]),Ke=x.useCallback((p,c=!1)=>{R.current.add(p);const d=Q.current,k=d.findIndex(V=>V.id===p),b=d[k];if(!b||b.type!=="list")return;const N=b.depth??0;if(N>0){const V=N-1;let P=b.listType;if(!c)for(let K=k-1;K>=0;K--){const C=d[K];if(C.type!=="list")break;if((C.depth??0)===V){P=C.listType;break}}oe(d.map(K=>K.id===p?F({...b,depth:V,listType:P}):K))}else Ce(p,"paragraph")},[oe,Ce,F]),Xe=x.useMemo(()=>{const p=w.length>0?Q.current.concat(w):Q.current;return{updateBlock:(c,d)=>Z(c,d),getBlock:c=>W(c),getAllBlocks:()=>p,getMarkdown:()=>me(p),insertBlockAfter:(c,d)=>we(c,d),deleteBlock:c=>Le(c),setBlockType:(c,d,k)=>Ce(c,d,k),indentListItem:c=>Pe(c),outdentListItem:c=>Ke(c)}},[Z,W,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"})]}),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:"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"})]}),b=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"})]}),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 4h6l4 16h6"}),s.jsx("path",{d:"M4 12h16"})]}),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:"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"})]}),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("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"})]}),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("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=L=>({id:`heading-${L}`,title:`Heading ${L}`,description:`Section heading (H${L})`,group:"Text",aliases:[`h${L}`],keywords:["heading","title",`h${L}`],icon:c,block:{type:"heading",level:L,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:b,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:P,block:{type:"math",content:""}},{id:"image",title:"Image",description:"Image block (URL)",group:"Media",aliases:["img","photo","picture"],keywords:["media"],icon:C,block:{type:"image",content:""}},{id:"table",title:"Table",description:"Spreadsheet-style table",group:"Layout",aliases:["tbl","grid"],keywords:["rows","columns","sheet"],icon:D,block:{type:"table",rows:2,cols:3}},{id:"divider",title:"Divider",description:"Horizontal divider",group:"Layout",aliases:["hr","line","---"],keywords:["separator"],icon:K,block:{type:"divider"},insertParagraphAfter:!0}]},[]),un=x.useCallback(p=>{const c=Q.current,d=c.findIndex(N=>N.id===p);if(d===-1)return;const k=d+1<c.length?c[d+1]:null;if(k&&k.type==="paragraph"&&ut(k.spans).trim()===""&&!k.isStreaming){ie(k.id,0,0);return}const b=we(p,{type:"paragraph",spans:[]});ie(b,0,0)},[we,ie]),qe=x.useCallback(p=>{const c=r?.getSlashCommands?r.getSlashCommands({block:p}):[];return yt.concat(c||[])},[yt,r]),We=x.useCallback((p,c)=>{const d=c.trim().toLowerCase();return d?p.map(k=>{const b=[k.title,...k.aliases||[],...k.keywords||[]].join(" ").toLowerCase(),N=(k.aliases||[]).some(K=>K.toLowerCase().startsWith(d))||k.title.toLowerCase().startsWith(d),V=b.includes(d);return{cmd:k,score:N?3:V?1:0}}).filter(k=>k.score>0).sort((k,b)=>b.score-k.score||k.cmd.title.localeCompare(b.cmd.title)).map(k=>k.cmd):p},[]),rt=x.useMemo(()=>{if(!O)return[];const p=Q.current.find(c=>c.id===O.blockId);return p?We(qe(p),O.query):[]},[O,We,qe]),at=x.useCallback((p,c)=>{switch(c.type){case"paragraph":return{id:p,type:"paragraph",spans:c.spans??[],isStreaming:!1};case"heading":return{id:p,type:"heading",spans:c.spans??[],level:c.level??1,isStreaming:!1};case"blockquote":return{id:p,type:"blockquote",spans:c.spans??[],isStreaming:!1};case"list":return{id:p,type:"list",spans:c.spans??[],listType:c.listType??"unordered",depth:c.depth??0,isStreaming:!1};case"code":return{id:p,type:"code",content:c.content??"",language:c.language??"text",isStreaming:!1};case"math":return{id:p,type:"math",content:c.content??"",isStreaming:!1};case"mermaid":return{id:p,type:"mermaid",content:c.content??"",isStreaming:!1};case"image":return{id:p,type:"image",content:c.content??"",isStreaming:!1};case"table":{const d=Math.max(1,c.cols??3),k=Math.max(0,c.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,c,d)=>{const k=Q.current.find(C=>C.id===p);if(!k)return;if(c.execute){c.execute(Xe,{blockId:p,block:k,query:d});return}if(!c.block)return;const b=Q.current,N=b.findIndex(C=>C.id===p);if(N===-1)return;let V=c.block;c.block.type==="list"&&k.type==="list"&&(V={...c.block,depth:k.depth??0}),R.current.add(p);const P=F(at(p,V));let K=b.slice(0,N).concat([P],b.slice(N+1));if(c.insertParagraphAfter){const C=$e(10);R.current.add(C);const D=F(at(C,{type:"paragraph",spans:[]}));K=K.slice(0,N+1).concat([D],K.slice(N+1)),oe(K),ie(C,0,0);return}oe(K),ie(p,0,0)},[Xe,F,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=>{Q.current.find(d=>d.id===p)?.isStreaming||(ge.current=p,U(p))},[]),hn=x.useCallback(p=>{if(ge.current!==p)return;const c=Q.current.find(k=>k.id===p);if(!c){ge.current=null,U(null);return}if(pe(c).deleteOnBlurWhenEmpty&&ce(c)){Le(p);return}ge.current=null,U(null)},[Le,pe]),pn=x.useCallback((p,c)=>{Z(p,c)},[Z]),bt=(p,c)=>{const d=[],k=[];let b=0;for(const N of p){const V=b,P=b+N.text.length;if(P<=c)d.push({...N});else if(V>=c)k.push({...N});else{const K=c-V;K>0&&d.push({...N,text:N.text.slice(0,K)}),K<N.text.length&&k.push({...N,text:N.text.slice(K)})}b=P}return[Ie(d),Ie(k)]},wt=(p,c,d)=>{if(c===d)return p;const k=Math.min(c,d),b=Math.max(c,d),N=[];let V=0;for(const P of p){const K=V,C=V+P.text.length;if(C<=k||K>=b)N.push({...P});else{const D=Math.max(0,k-K),z=Math.max(0,b-K);D>0&&N.push({...P,text:P.text.slice(0,D)}),z<P.text.length&&N.push({...P,text:P.text.slice(z)})}V=C}return Ie(N)},ct=(p,c)=>{if(p.length===0)return c;if(c.length===0)return p;const d=p[p.length-1],k=c[0],N=d.text.length>0&&k.text.length>0&&!/\s$/.test(d.text)&&!/^\s/.test(k.text)?p.concat([{text:" ",style:"plain"}],c):p.concat(c);return Ie(N)},mn=x.useCallback((p,c)=>{const d=Q.current.find(C=>C.id===p);if(!d)return;c.key!=="ArrowUp"&&c.key!=="ArrowDown"&&(X.current=null);const k=C=>{if(!(d.type==="paragraph"||d.type==="heading"||d.type==="list"||d.type==="blockquote"))return;Z(p,C?[{text:C,style:"plain"}]:[]);const z=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');if(z){z.textContent=C;const L=C.length;Be(z,L,L);return}ie(p,C.length,C.length)};if(O&&O.blockId===p){c.preventDefault();const C=O.query,D=c.key;if(D==="Escape"){ue();const L="/"+C;k(L);return}const z=Math.max(0,rt.length-1);if(D==="ArrowDown"||D==="Tab"){ne({...O,activeIndex:Math.min(O.activeIndex+1,z)});return}if(D==="ArrowUp"){ne({...O,activeIndex:Math.max(O.activeIndex-1,0)});return}if(D==="Backspace"){if(C.length===0){ue(),k("");return}const L=C.slice(0,-1),I=We(qe(d),L);ne({...O,query:L,activeIndex:0}),k("/"+L),I.length===0&&ue();return}if(D==="Enter"){const L=rt[ht(O.activeIndex,0,z)];ue(),L&<(p,L,C);return}if(D.length===1&&!c.metaKey&&!c.ctrlKey&&!c.altKey){const L=C+D,I=We(qe(d),L);ne({...O,query:L,activeIndex:0}),k("/"+L),I.length===0&&ue();return}return}if(!O&&se(d)){const C=d.spans.map(D=>D.text).join("");if(C.startsWith("/")){const D=c.currentTarget,z=D?.isContentEditable?be(D):null;if(z&&z.start===z.end&&z.start===C.length){const L=(I,ee)=>{c.preventDefault();const q=te.current;if(!q)return;let Y=0,B=0;const re=document.querySelector(`[data-block-id="${p}"]`),A=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)Y=ye.bottom-A.top+8,B=ye.left-A.left;else if(re){const st=re.getBoundingClientRect();Y=st.top-A.top+28,B=st.left-A.left+28}const Ee=8,Ve=Math.max(Ee,A.width-It-8);B=ht(B,Ee,Ve),ne({blockId:p,query:ee,activeIndex:0,position:{top:Y,left:B}}),Z(p,[{text:I,style:"plain"}]);const De=re?.querySelector('[contenteditable="true"]');De&&(De.textContent=I,Be(De,I.length,I.length))};if(c.key==="Backspace"&&C.length>1){const I=C.slice(0,-1),ee=I.slice(1);if(We(qe(d),ee).length>0){L(I,ee);return}}else if(c.key.length===1&&!c.metaKey&&!c.ctrlKey&&!c.altKey){const I=C+c.key,ee=I.slice(1);if(We(qe(d),ee).length>0){L(I,ee);return}}}}}if((c.key==="Backspace"||c.key==="Delete")&&c.target?.isContentEditable){const C=window.getSelection();if(C&&C.rangeCount>0){const D=C.getRangeAt(0);if(!D.collapsed){const z=D.startContainer.parentElement?.closest?.("[data-block-id]"),L=D.endContainer.parentElement?.closest?.("[data-block-id]"),I=z?.getAttribute("data-block-id"),ee=L?.getAttribute("data-block-id");if(I&&ee&&I!==ee){c.preventDefault();const q=Q.current.findIndex(De=>De.id===I),Y=Q.current.findIndex(De=>De.id===ee);if(q===-1||Y===-1)return;const B=Math.min(q,Y),re=Math.max(q,Y),A=Q.current,he=A.slice(0,B).concat(A.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(B-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 b=c.currentTarget;let N=0;if(b instanceof HTMLTextAreaElement)N=b.selectionStart;else if(b?.isContentEditable){const C=be(b);C&&(N=C.start)}const V="spans"in d?je(d.spans):"content"in d?d.content:"";if(S(c,d,V,N))return;if(c.key==="/"&&N===0){const C=!!d.metadata?.dirty,D=d.type==="paragraph"||d.type==="heading"||d.type==="list"||d.type==="blockquote"?ut(d.spans).trim()==="":d.type==="divider"?!1:d.content?.trim?.()==="";if(C&&D&&!d.isStreaming){c.preventDefault(),fe(p),Z(p,[{text:"/",style:"plain"}]);const L=document.querySelector(`[data-block-id="${p}"]`)?.querySelector('[contenteditable="true"]');L?(L.textContent="/",Be(L,1,1)):ie(p,1,1);return}}if(c.key==="ArrowUp"&&b?.isContentEditable&&se(d)){const C=be(b),D=I=>I?.parentElement?.closest?.('[data-math-delim="1"]')!=null,z=(I,ee)=>{const q=document.createTreeWalker(I,NodeFilter.SHOW_TEXT,null);let Y=0,B;for(;B=q.nextNode();){if(D(B))continue;const re=B.textContent?.length??0;if(re!==0){if(Y+re>=ee)return{node:B,offset:Math.max(0,Math.min(re,ee-Y))};Y+=re}}return{node:I,offset:I.childNodes.length}},L=(I,ee)=>{if(I.textContent==null||I.textContent.length===0)return I.getBoundingClientRect();const q=z(I,ee),Y=document.createRange();try{if(Y.setStart(q.node,q.offset),q.node.nodeType===Node.TEXT_NODE){const B=q.node,re=B.textContent?.length??0;re>0&&(q.offset<re?Y.setEnd(B,q.offset+1):q.offset>0?(Y.setStart(B,q.offset-1),Y.setEnd(B,q.offset)):Y.setEnd(B,Math.min(1,re)))}}catch{return null}return Y.getClientRects()[0]||Y.getBoundingClientRect()};if(C&&C.start===C.end){const I=b.getBoundingClientRect();let ee=L(b,C.start);if(!ee){const q=window.getSelection();if(q&&q.rangeCount>0){const Y=q.getRangeAt(0);if(Y.collapsed&&b.contains(Y.startContainer))try{const B=Y.cloneRange();if(B.startContainer.nodeType===Node.TEXT_NODE){const re=B.startContainer,A=re.textContent?.length??0;A>0&&(B.startOffset<A?B.setEnd(re,B.startOffset+1):B.startOffset>0&&(B.setStart(re,B.startOffset-1),B.setEnd(re,B.startOffset)))}ee=B.getClientRects()[0]||B.getBoundingClientRect()}catch{}}}if(ee){X.current==null&&(X.current=ee.left+1);const q=X.current;if(ee.top<=I.top+6){const B=Q.current;let A=B.findIndex(he=>he.id===p)-1;for(;A>=0&&(!se(B[A])||B[A].isStreaming);)A--;if(A>=0){c.preventDefault(),H(B[A].id,q,"bottom");return}}}}}if(c.key==="ArrowDown"&&b?.isContentEditable&&se(d)){const C=be(b),D=I=>I?.parentElement?.closest?.('[data-math-delim="1"]')!=null,z=(I,ee)=>{const q=document.createTreeWalker(I,NodeFilter.SHOW_TEXT,null);let Y=0,B;for(;B=q.nextNode();){if(D(B))continue;const re=B.textContent?.length??0;if(re!==0){if(Y+re>=ee)return{node:B,offset:Math.max(0,Math.min(re,ee-Y))};Y+=re}}return{node:I,offset:I.childNodes.length}},L=(I,ee)=>{if(I.textContent==null||I.textContent.length===0)return I.getBoundingClientRect();const q=z(I,ee),Y=document.createRange();try{if(Y.setStart(q.node,q.offset),q.node.nodeType===Node.TEXT_NODE){const B=q.node,re=B.textContent?.length??0;re>0&&(q.offset<re?Y.setEnd(B,q.offset+1):q.offset>0?(Y.setStart(B,q.offset-1),Y.setEnd(B,q.offset)):Y.setEnd(B,Math.min(1,re)))}}catch{return null}return Y.getClientRects()[0]||Y.getBoundingClientRect()};if(C&&C.start===C.end){const I=b.getBoundingClientRect();let ee=L(b,C.start);if(!ee){const q=window.getSelection();if(q&&q.rangeCount>0){const Y=q.getRangeAt(0);if(Y.collapsed&&b.contains(Y.startContainer))try{const B=Y.cloneRange();if(B.startContainer.nodeType===Node.TEXT_NODE){const re=B.startContainer,A=re.textContent?.length??0;A>0&&(B.startOffset<A?B.setEnd(re,B.startOffset+1):B.startOffset>0&&(B.setStart(re,B.startOffset-1),B.setEnd(re,B.startOffset)))}ee=B.getClientRects()[0]||B.getBoundingClientRect()}catch{}}}if(ee){X.current==null&&(X.current=ee.left+1);const q=X.current;if(ee.bottom>=I.bottom-6){const B=Q.current;let A=B.findIndex(he=>he.id===p)+1;for(;A<B.length&&(!se(B[A])||B[A].isStreaming);)A++;if(A<B.length){c.preventDefault(),H(B[A].id,q,"top");return}}}}}if((c.key==="ArrowLeft"||c.key==="ArrowRight")&&b?.isContentEditable&&se(d)){const C=be(b);if(C&&C.start===C.end){const D=Q.current,z=D.findIndex(L=>L.id===p);if(c.key==="ArrowLeft"&&C.start===0){let L=z-1;for(;L>=0&&(!se(D[L])||D[L].isStreaming);)L--;if(L>=0){c.preventDefault(),H(D[L].id,Number.POSITIVE_INFINITY,"bottom");return}}if(c.key==="ArrowRight"){const L=d.spans.reduce((I,ee)=>I+ee.text.length,0);if(C.start>=L){let I=z+1;for(;I<D.length&&(!se(D[I])||D[I].isStreaming);)I++;if(I<D.length){c.preventDefault(),H(D[I].id,Number.NEGATIVE_INFINITY,"top");return}}}}}const K=pe(d);if(c.key==="Enter"&&K.enterBehavior==="newBlock"&&se(d)&&b?.isContentEditable){c.preventDefault();const C=be(b),D=C?.start??0,z=C?.end??D;if(d.type==="list"&&ce(d)){Ce(d.id,"paragraph"),ie(d.id,0,0);return}const L=wt(d.spans,D,z),[I,ee]=bt(L,D),q=Q.current,Y=q.findIndex(ye=>ye.id===d.id),B=$e(10);R.current.add(d.id),R.current.add(B);let re=d.type,A;(d.type==="heading"||d.type==="blockquote")&&(re="paragraph"),d.type==="list"&&(re="list");const he=F({...d,spans:I});re==="list"&&d.type==="list"?A={id:B,type:"list",spans:ee,listType:d.listType,depth:d.depth??0,isStreaming:!1}:A={id:B,type:"paragraph",spans:ee,isStreaming:!1},A=F(A);const xe=q.slice(0,Y).concat([he,A],q.slice(Y+1));oe(xe),ie(B,0,0);return}if(c.key==="Tab"&&se(d)&&d.type==="list"&&b?.isContentEditable){c.preventDefault(),c.shiftKey?Ke(d.id):Pe(d.id);return}if(c.altKey&&(c.key==="]"||c.key==="[")&&se(d)&&d.type==="list"&&b?.isContentEditable){c.preventDefault(),c.key==="["?Ke(d.id,!0):Pe(d.id,!0);return}if(c.key==="Backspace"){if(se(d)&&b?.isContentEditable){const C=be(b);if(!C||C.start!==C.end)return;if(C.start===0){if(d.type==="heading"){c.preventDefault(),Ce(d.id,"paragraph"),ie(d.id,0,0);return}if(d.type==="list"){if(c.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 D=Q.current,z=D.findIndex(I=>I.id===d.id),L=z>0?D[z-1]:null;if(L&&L.type==="divider"){c.preventDefault(),Le(L.id),ie(d.id,0,0);return}if(ce(d)){c.preventDefault(),Le(d.id);return}if(c.preventDefault(),L&&L.type==="list"&&(d.type==="paragraph"||d.type==="blockquote")){const I=L.spans.reduce((Y,B)=>Y+B.text.length,0),ee={...L,spans:ct(L.spans,d.spans)},q=D.slice(0,z-1).concat([F(ee)],D.slice(z+1));oe(q),ie(ee.id,I,I);return}if(L&&se(L)&&se(d)&&L.type!=="list"){const I=L.spans.reduce((Y,B)=>Y+B.text.length,0),ee={...L,spans:ct(L.spans,d.spans)},q=D.slice(0,z-1).concat([F(ee)],D.slice(z+1));oe(q),ie(ee.id,I,I);return}}return}if(b instanceof HTMLTextAreaElement&&N===0&&ce(d)){c.preventDefault(),Le(d.id);return}}},[S,O,ue,ne,rt,lt,fe,Z,We,qe,pe,ce,Le,oe,ie,H,bt,wt,ct,Ce,Pe,Ke]),gn=x.useMemo(()=>{const p=[],c=[];for(const d of le)if(d.type==="list"&&d.listType==="ordered"){const k=d.depth??0;for(;c.length<=k;)c.push(0);c[k]=(c[k]||0)+1;for(let b=k+1;b<c.length;b++)c[b]=0;p.push({...d,metadata:{...d.metadata||{},displayNumber:c[k]}})}else d.type==="list"||(c.length=0),p.push(d);return p},[le]),xn=x.useMemo(()=>{if(w.length===0)return"";const p=w[w.length-1],c=w.slice(0,-1),d=c.length>0?me(c)+`
|
|
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
|
-
`,N=p.content||"";return d+
|
|
30
|
-
`+
|
|
31
|
-
`)}function et(e,t,n,r){const
|
|
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:(t.push(n),n={...e[r]});return t.push(n),t}function Br(e){const{baseMarkdown:t,nextMarkdown:n,defaultDecision:r="unset",applyFallback:
|
|
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;
|