import{r as n,j as c,d as R}from"./chunks/vendor-react.js?v=1775123024591";import{I as T}from"./chunks/vendor-excalidraw.js?v=1775123024591";import"./chunks/_commonjsHelpers.js?v=1775123024591";import"./chunks/preload-helper.js?v=1775123024591";import"./chunks/vendor-common.js?v=1775123024591";import"./chunks/_commonjs-dynamic-modules.js?v=1775123024591";const N=1500;function S({canvasName:t,isDarkMode:e}){const[s,i]=n.useState(null),[l,v]=n.useState(null),[I,h]=n.useState(!0),[g,w]=n.useState(""),u=n.useRef(null),d=n.useRef(!1),C=n.useRef(t),f=n.useRef(!1);n.useEffect(()=>{C.current=t,f.current=!1,h(!0),w(""),v(null);let r=!1;return(async()=>{try{const a=await fetch(`/api/canvas/${encodeURIComponent(t)}`);if(r)return;if(!a.ok)throw new Error(`加载画布失败 (${a.status})`);const m=await a.json();if(r)return;v(m),f.current=!0}catch(a){if(r)return;w((a==null?void 0:a.message)||"加载画布失败")}finally{r||h(!1)}})(),()=>{r=!0}},[t]);const E=n.useCallback(async(r,o)=>{if(!d.current){d.current=!0;try{const a=(s==null?void 0:s.getFiles())||{},m={type:"excalidraw",version:2,source:"axhub-make",elements:r,appState:{gridSize:(o==null?void 0:o.gridSize)??null,viewBackgroundColor:(o==null?void 0:o.viewBackgroundColor)??"#ffffff"},files:a};await fetch(`/api/canvas/${encodeURIComponent(C.current)}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:JSON.stringify(m,null,2)})})}catch(a){console.warn("Failed to save canvas:",a)}finally{d.current=!1}}},[s]),j=n.useCallback((r,o)=>{f.current&&(u.current&&clearTimeout(u.current),u.current=setTimeout(()=>{E(r,o)},N))},[E]);return n.useEffect(()=>()=>{u.current&&clearTimeout(u.current)},[]),I?c.jsx("div",{className:"canvas-template-status",children:"加载中..."}):g?c.jsx("div",{className:"canvas-template-error",children:g}):c.jsx("div",{className:"canvas-template-canvas",children:c.jsx(T,{excalidrawAPI:r=>i(r),initialData:l,onChange:j,theme:e?"dark":"light",UIOptions:{canvasActions:{saveAsImage:!0,export:!1}}},t)})}const p="axhub-make-dark-mode";function x(t){var e;return typeof document>"u"?"":((e=document.querySelector(`meta[name="${t}"]`))==null?void 0:e.getAttribute("content"))||""}function y(t){const e=String(t||"").trim();return!e||e.startsWith("{{")?"":e}function b(t){const e=t.match(/^\/canvas\/(.+?)\/?$/);if(!(e!=null&&e[1]))return"";try{return decodeURIComponent(e[1])}catch{return e[1]}}function A(){const t=y(x("axhub-canvas-name"));return t||(typeof window>"u"?"":b(window.location.pathname))}function D(t){const e=y(x("axhub-canvas-title"));if(e)return e;const s=t.replace(/\.excalidraw$/i,"").trim();return s?`${s} - Canvas`:"Canvas"}function M(){try{return localStorage.getItem(p)==="true"}catch{return!1}}function $(){const[t]=n.useState(()=>A()),[e,s]=n.useState(()=>M());return n.useEffect(()=>{const i=l=>{l.key===p&&s(l.newValue==="true")};return window.addEventListener("storage",i),()=>{window.removeEventListener("storage",i)}},[]),n.useEffect(()=>{const i=D(t);document.title=i,document.documentElement.classList.toggle("dark",e),document.body.classList.toggle("dark",e)},[t,e]),c.jsx("div",{className:e?"canvas-template-shell dark":"canvas-template-shell",children:t?c.jsx(S,{canvasName:t,isDarkMode:e}):c.jsx("div",{className:"canvas-template-placeholder",children:"未指定画布"})})}const k=document.getElementById("canvas-root");if(!k)throw new Error("[Canvas Template] 找不到 #canvas-root 元素");const L=R.createRoot(k);L.render(c.jsx($,{}));