import{r as s,u as G,j as h,U as B}from"./index-26FXPGkN.js";import{f as Y,L as J,j as Q,g as L,k as X,u as Z,_ as C,n as N,m as ee,o as te,I as ie,p as se,q as oe,r as ae,s as re,c as P,d as de}from"./TaskPanel-1n1xV0vy.js";import"./tasks-BMVBejH2.js";import"./index-DlWqcwmX.js";import"./IconUser-CkQmhwMi.js";import"./Textarea-C8aNW3Hx.js";import"./get-env-uyVen0u2.js";import"./IconChevronLeft-FAloe5QL.js";import"./use-resize-observer-6Qm_8hWl.js";import"./useTaskTypesData-CFzDcAWE.js";import"./ColorSwatch-CtoYH4Cn.js";import"./Typography-PKUzf06x.js";import"./clamp-CUTWWzXs.js";import"./Select-C1WMqk5R.js";import"./SegmentedControl-BeEuUkOb.js";import"./IconGripVertical-COCN8Vqa.js";import"./IconAt-Bca7mi74.js";import"./IconTableOff-DmQNoB2C.js";import"./Accordion-z2XcZ0M6.js";const U=t=>{const{width:m,height:r,isLoading:l,isError:x,src:u,id:g,altText:p,imageRef:f,maxWidth:c}=t;return h.jsxs("div",{className:"media-wrapper",style:{width:m,height:r,display:"inline-flex",position:"relative"},children:[h.jsx(B,{visible:l,zIndex:1e3,overlayProps:{backgroundOpacity:.5},loaderProps:{size:24,color:"black"}}),h.jsx(ne,{className:null,src:u,altText:p,imageRef:f,width:"inherit",height:"inherit",maxWidth:c||"auto",id:g})]})};function ne({altText:t,className:m,imageRef:r,src:l,width:x,height:u,maxWidth:g,id:p}){const[f,c]=s.useState(!1);return h.jsx("img",{className:`${m||void 0} ${f?"":"loading"}`,src:l,alt:t,ref:r,style:{height:u,maxWidth:g,width:x},onLoad:()=>{l.includes("splash")?setTimeout(()=>{c(!0)},100):c(!0)},draggable:"false","data-id":p})}function Le({src:t,altText:m,nodeKey:r,width:l,height:x,maxWidth:u,showCaption:g,caption:p,id:f,create:c,alignment:V}){var R,A;const D=s.useRef(null),[v,E]=s.useState(!0),{isCollabActive:k}=Y(),[n]=J(),[w,T]=s.useState(!0),[d,b]=s.useState({width:0,height:0}),{fetchUploadMedia:I,fetchDeleteMedia:z,maxFileSize:he}=s.useContext(Q),j=L.has(t)&&(((R=L.get(t))==null?void 0:R.mediaNodeKeys.length)===0||((A=L.get(t))==null?void 0:A.mediaNodeKeys.length)===void 0||t.trim()===""),{t:S}=G(),[le,_]=s.useState(0);s.useEffect(()=>n.registerDecoratorListener(()=>{_(e=>e+1)}),[j]),s.useEffect(()=>{if(!t||t.startsWith("data:image/")||!t.trim()){E(!1);return}const e=new Image;e.src=t,e.onload=()=>E(!1),e.onerror=()=>{E(!1)}},[t,j]);const O=(e,i)=>{n.update(()=>{de(null);const o=C(r);N(o)&&o.setWidthAndHeight(e,i)})},{historyState:K}=X(),{settings:{showNestedEditorTreeView:M}}=Z(),[W,y]=s.useState(!1);function $(e){return e.endsWith(".svg")||e.includes("image/svg+xml")}function q(e){return new Promise((i,o)=>{if($(e)){i(0);return}const a=new Image;a.src=e,a.onload=()=>{i(a.width)},a.onerror=()=>{o(new Error("Failed to load image"))}})}s.useEffect(()=>{c&&y(!0)},[c]),s.useEffect(()=>{let e=l,i=x,o=100;(t.includes("unsplash")?t.trim():!j&&t.trim()||t.includes("upload-src-editor"))&&t!=="upload"&&q(t).then(a=>{typeof e=="number"&&e<o&&a<o?(T(!0),b({width:e,height:i})):a<o?(T(!0),b({width:"auto",height:"auto"})):(b({width:a,height:a}),T(!1))}).catch(a=>{console.warn("Ошибка при загрузке изображения:",a.message)})},[t,j]),s.useEffect(()=>{d.width&&d.height&&w&&n.update(()=>{const e=C(r);N(e)&&e.setWidthAndHeight(d.width,d.height)})},[d.width,d.height,w]),s.useEffect(()=>{const e=document.documentElement;ee(W,e)},[W]),s.useEffect(()=>n.registerCommand(te,e=>(t==="upload"&&H(e),!1),ie),[t,v]);const F=(e,i)=>{E(!1),n.update(()=>{const o=C(r);N(o)&&(o.setSrc(e),o.setId(i))})},H=e=>{E(!0),I&&I(e[0],(i,o)=>{F(i,o)},i=>{i&&se({message:S("editor.gallery.errorFile")}),F("","")})};return!t||t==="upload"?h.jsx(oe,{uploading:!1,onUpload:e=>{!I||!e.length||(H(e),y(!1))},popoverProps:{opened:W,position:"bottom",onChange:y},onChange:e=>{y(!1),n.update(()=>{const i=C(r);N(i)&&i.setSrc(e)})},children:h.jsx(ae,{title:S("editor.image.add_photo"),icon:"photo",onClick:()=>y(!W),isLoading:v})}):w?h.jsx(U,{width:d.width,height:d.height,isLoading:v,isError:!1,isIcon:w,src:t,id:f,editor:n,nodeKey:r,fetchDeleteMedia:z,altText:m,imageRef:D,maxWidth:u,showCaption:g,caption:p,isCollabActive:k,historyState:K,showNestedEditorTreeView:M,t:S}):h.jsx(re,{editor:n,isIcon:w,width:P(l),height:P(x),initialWidth:d.width||"",initialHeight:d.height||"",onResizeEnd:O,alignment:V,children:h.jsx(U,{width:"inherit",height:"inherit",isLoading:v,isError:!1,isIcon:w,src:t,id:f,editor:n,nodeKey:r,fetchDeleteMedia:z,altText:m,imageRef:D,maxWidth:u,showCaption:g,caption:p,isCollabActive:k,historyState:K,showNestedEditorTreeView:M,t:S})})}export{Le as default};
