database-petani-mobile/node_modules/@base44/vite-plugin/dist/statics/index.mjs
2026-02-23 16:39:35 +07:00

2 lines
6.2 KiB
JavaScript

function f(d){if(!d)return[];let h=Array.from(document.querySelectorAll(`[data-source-location="${d}"]`));return h.length>0?h:Array.from(document.querySelectorAll(`[data-visual-selector-id="${d}"]`))}function T(d,h){d.forEach(g=>{g.setAttribute("class",h)})}function V(){let d=!1,h=!1,g=!1,E=[],c=[],u=[],l=null,L=(n=!1)=>{let e=document.createElement("div");return e.style.position="absolute",e.style.pointerEvents="none",e.style.transition="all 0.1s ease-in-out",e.style.zIndex="9999",n?e.style.border="2px solid #2563EB":(e.style.border="2px solid #95a5fc",e.style.backgroundColor="rgba(99, 102, 241, 0.05)"),e},p=(n,e,t=!1)=>{if(!e||!d)return;e.offsetWidth;let s=e.getBoundingClientRect();n.style.top=`${s.top+window.scrollY}px`,n.style.left=`${s.left+window.scrollX}px`,n.style.width=`${s.width}px`,n.style.height=`${s.height}px`;let o=n.querySelector("div");o||(o=document.createElement("div"),o.textContent=e.tagName.toLowerCase(),o.style.position="absolute",o.style.top="-27px",o.style.left="-2px",o.style.padding="2px 8px",o.style.fontSize="11px",o.style.fontWeight=t?"500":"400",o.style.color=t?"#ffffff":"#526cff",o.style.backgroundColor=t?"#526cff":"#DBEAFE",o.style.borderRadius="3px",o.style.minWidth="24px",o.style.textAlign="center",n.appendChild(o))},m=()=>{E.forEach(n=>{n&&n.parentNode&&n.remove()}),E=[],u=[]},M=n=>{if(!d||h)return;let e=n.target;if(g){m();return}if(e.tagName.toLowerCase()==="path"){m();return}let t=e.closest("[data-source-location], [data-visual-selector-id]");if(!t){m();return}let a=t,s=a.dataset.sourceLocation||a.dataset.visualSelectorId;if(l===s){m();return}let o=f(s||null);m(),o.forEach(i=>{let r=L(!1);document.body.appendChild(r),E.push(r),p(r,i)}),u=o},I=()=>{h||m()},C=n=>{if(!d)return;let e=n.target;if(g){n.preventDefault(),n.stopPropagation(),n.stopImmediatePropagation(),window.parent.postMessage({type:"close-dropdowns"},"*");return}if(e.tagName.toLowerCase()==="path")return;n.preventDefault(),n.stopPropagation(),n.stopImmediatePropagation();let t=e.closest("[data-source-location], [data-visual-selector-id]");if(!t)return;let a=t,s=a.dataset.sourceLocation||a.dataset.visualSelectorId;c.forEach(v=>{v&&v.parentNode&&v.remove()}),c=[],f(s||null).forEach(v=>{let y=L(!0);document.body.appendChild(y),c.push(y),p(y,v,!0)}),l=s||null,m();let i=t.getBoundingClientRect(),r={top:i.top,left:i.left,right:i.right,bottom:i.bottom,width:i.width,height:i.height,centerX:i.left+i.width/2,centerY:i.top+i.height/2},b=t,O={type:"element-selected",tagName:t.tagName,classes:b.className?.baseVal||t.className||"",visualSelectorId:s,content:t.innerText,dataSourceLocation:a.dataset.sourceLocation,isDynamicContent:a.dataset.dynamicContent==="true",linenumber:a.dataset.linenumber,filename:a.dataset.filename,position:r};window.parent.postMessage(O,"*")},H=()=>{c.forEach(n=>{n&&n.parentNode&&n.remove()}),c=[],l=null},x=(n,e)=>{let t=f(n);t.length!==0&&(T(t,e),setTimeout(()=>{l===n&&c.forEach((a,s)=>{s<t.length&&p(a,t[s])}),u.length>0&&u[0]?.dataset?.visualSelectorId===n&&E.forEach((o,i)=>{i<u.length&&p(o,u[i])})},50))},D=(n,e)=>{let t=f(n);t.length!==0&&(t.forEach(a=>{a.innerText=e}),setTimeout(()=>{l===n&&c.forEach((a,s)=>{s<t.length&&p(a,t[s])})},50))},S=n=>{d=n,n?(document.body.style.cursor="crosshair",document.addEventListener("mouseover",M),document.addEventListener("mouseout",I),document.addEventListener("click",C,!0)):(m(),c.forEach(e=>{e&&e.parentNode&&e.remove()}),c=[],u=[],l=null,document.body.style.cursor="default",document.removeEventListener("mouseover",M),document.removeEventListener("mouseout",I),document.removeEventListener("click",C,!0))},N=()=>{if(l){let n=f(l);if(n.length>0){let t=n[0].getBoundingClientRect(),a=window.innerHeight,s=window.innerWidth,o=t.top<a&&t.bottom>0&&t.left<s&&t.right>0,i={top:t.top,left:t.left,right:t.right,bottom:t.bottom,width:t.width,height:t.height,centerX:t.left+t.width/2,centerY:t.top+t.height/2};window.parent.postMessage({type:"element-position-update",position:i,isInViewport:o,visualSelectorId:l},"*")}}},k=n=>{let e=n.data;switch(e.type){case"toggle-visual-edit-mode":S(e.data.enabled);break;case"update-classes":e.data&&e.data.classes!==void 0?x(e.data.visualSelectorId,e.data.classes):console.warn("[VisualEditAgent] Invalid update-classes message:",e);break;case"unselect-element":H();break;case"refresh-page":window.location.reload();break;case"update-content":e.data&&e.data.content!==void 0?D(e.data.visualSelectorId,e.data.content):console.warn("[VisualEditAgent] Invalid update-content message:",e);break;case"request-element-position":if(l){let t=f(l);if(t.length>0){let s=t[0].getBoundingClientRect(),o=window.innerHeight,i=window.innerWidth,r=s.top<o&&s.bottom>0&&s.left<i&&s.right>0,b={top:s.top,left:s.left,right:s.right,bottom:s.bottom,width:s.width,height:s.height,centerX:s.left+s.width/2,centerY:s.top+s.height/2};window.parent.postMessage({type:"element-position-update",position:b,isInViewport:r,visualSelectorId:l},"*")}}break;case"popover-drag-state":e.data&&e.data.isDragging!==void 0&&(h=e.data.isDragging,e.data.isDragging&&m());break;case"dropdown-state":e.data&&e.data.isOpen!==void 0&&(g=e.data.isOpen,e.data.isOpen&&m());break;default:break}},w=()=>{if(l){let n=f(l);c.forEach((e,t)=>{t<n.length&&p(e,n[t])})}u.length>0&&E.forEach((n,e)=>{e<u.length&&p(n,u[e])})};document.querySelectorAll("[data-linenumber]:not([data-visual-selector-id])").forEach((n,e)=>{let t=n,a=`visual-id-${t.dataset.filename}-${t.dataset.linenumber}-${e}`;t.dataset.visualSelectorId=a});let A=new MutationObserver(n=>{n.some(t=>{let a=o=>{if(o.nodeType===Node.ELEMENT_NODE){let i=o;if(i.dataset&&i.dataset.visualSelectorId)return!0;for(let r=0;r<i.children.length;r++)if(a(i.children[r]))return!0}return!1};return t.type==="attributes"&&(t.attributeName==="style"||t.attributeName==="class"||t.attributeName==="width"||t.attributeName==="height")&&a(t.target)})&&setTimeout(w,50)});window.addEventListener("message",k),window.addEventListener("scroll",N,!0),document.addEventListener("scroll",N,!0),window.addEventListener("resize",w),window.addEventListener("scroll",w),A.observe(document.body,{attributes:!0,childList:!0,subtree:!0,attributeFilter:["style","class","width","height"]}),window.parent.postMessage({type:"visual-edit-agent-ready"},"*")}export{V as setupVisualEditAgent};
//# sourceMappingURL=index.mjs.map