diff --git a/index.html b/index.html index 5f60ebc..b84ddeb 100644 --- a/index.html +++ b/index.html @@ -184,6 +184,6 @@ - + \ No newline at end of file diff --git a/index.js b/index.js index ccd8820..6143a3a 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -const t=(t,n)=>t.querySelector(`[data-key="${n}"]`),n=(t,n)=>t.querySelector(`[${n}]`),e=t=>document.getElementById(t);function o(t,n){t.style.transform=`translate(${n.x}px, ${n.y}px)`}const i=(t,...n)=>t?.classList.add(...n),a=(t,...n)=>t?.classList.remove(...n),s=(t,n)=>t?.classList.contains(n),c=(t,n,e,o)=>(t.addEventListener(n,e,{passive:!0,once:o}),e),r=(t,n,e,o)=>t?.removeEventListener(n,e,{capture:o}),l=(t,n,e)=>t.querySelectorAll(n).forEach((t=>c(t,"click",e))),d=(t,n)=>t.currentTarget.getAttribute(n),u=t=>"TEXTAREA"===t.target.tagName.toUpperCase()||"INPUT"===t.target.tagName.toUpperCase(),h=t=>"mouse"===t.pointerType,p=t=>({x:t.clientX,y:t.clientY});function f(t,n){const e=document.createElementNS("http://www.w3.org/2000/svg",t);return n&&(e.innerHTML=n),e}const m=(t,n)=>{const e=document.createElement("div");return t&&(e.innerHTML=t),e.style.cssText=n,e},g=(...t)=>document.body.append(...t);function v(t){let n,e=0;for(const o of t.getElementsByTagName("tspan"))for(const t of w(o.getBBox())){const o=Math.abs(t.x)+Math.abs(t.y);e[{x:t.x,y:t.y},{x:t.right,y:t.y},{x:t.x,y:t.bottom},{x:t.right,y:t.bottom}];function y(t){const n={x:1/0,y:1/0},e={x:-1/0,y:-1/0};for(const o of t){const t=o.getBoundingClientRect();n.x>t.x&&(n.x=t.x),n.y>t.y&&(n.y=t.y),e.x{let e;(n=n||{}).credentials="include";try{e=await fetch(t,n)}catch(t){throw console.log(t),t}if(!e.ok)throw 401===e.status&&alert("You have to relogin."),new Error;return e},b=(t,n)=>x(t,{method:"POST",body:n}),k=(t,n)=>x(t,{method:"POST",body:n?Y(n):null}),$=(t,n)=>{const e=new URL(window.location.href);return e.searchParams.set(t,n),e.toString()},C=(...t)=>{const n=new URL(window.location.href);t?.forEach((t=>n.searchParams.delete(t))),history.replaceState(null,null,n.toString())};function M(t,n,e,o){function i(n){r(t,"pointercancel",i),r(t,"pointerup",i),r(t,"wheel",i),r(document,"pointerdown",i),r(t,"pointermove",e),o(n)}c(t,"pointercancel",i,!0),c(t,"pointerup",i,!0),c(t,"wheel",i,!0),c(document,"pointerdown",i,!0),c(t,"pointermove",e),t.setPointerCapture(n.pointerId)}const H=(t,n)=>L(t.ownerSVGElement,t,n);function L(t,n,e,o){const i=()=>{r(t,"pointercancel",a),r(t,"wheel",a),r(t,"pointerdown",a)},a=t=>{o&&o(t),n.contains(t.target)||(i(),e(t))};return c(t,"pointercancel",a),c(t,"wheel",a),c(t,"pointerdown",a),i}const V=t=>{const n=n=>{"Escape"===n.key&&t()};return c(document,"keydown",n),()=>r(document,"keydown",n)},z=(t,n,e)=>new CustomEvent(t,{detail:n,cancelable:e}),S=(t,n)=>t.dispatchEvent(n),Z=(t,n)=>{let e,o,i=!1;return function a(){if(i)return e=arguments,void(o=this);t.apply(this,arguments),i=!0,setTimeout((function(){i=!1,e&&(a.apply(o,e),e=o=null)}),n)}};function B(t,n,e){return t??=0,e<=t?t:t+Math.ceil((e-t)/n)*n}const D=t=>Math.round(100*(t+Number.EPSILON))/100,E=(t,n,e)=>{let o=!1;return n>e?n>t&&(o=!0,e*=t/n,n=t):e>t&&(o=!0,n*=t/e,e=t),{w:n,h:e,c:o}};function T(t,n){for(let e=t.length-1;e>=0;--e)n(t[e])}const A=t=>t.values().next().value;function R(t,n,e){const o=e??1;return t.x+=o*n.x,t.y+=o*n.y,t}const P=(t,n)=>({x:t.x+n,y:t.y+n}),U=(t,n)=>O(t,n.x,n.y),O=(t,n,e)=>(t.x=n,t.y=e,t),N=t=>({x:t.x,y:t.y}),j=(t,n)=>t?.x===n?.x&&t?.y===n?.y;function I(){const t=new Uint8Array(4);return window.crypto.getRandomValues(t),Array.from(t,(t=>t.toString(16).padStart(2,"0"))).join("")}const F=t=>t?.substring(0,t.lastIndexOf("."))||t,K=t=>_(Y(t)),Y=t=>JSON.stringify(t),_=t=>JSON.parse(t),X=(t,n,e,o)=>{let i,a;const s=()=>{a=setTimeout((async t=>{await e(),i||s()}),t)};s();const c=setTimeout((t=>{r(),o&&o()}),n),r=()=>{i=!0,clearTimeout(a),clearTimeout(c)};return r};function G(t,n,e){t.x+=e[q]/n,t.y+=e[J]/n}const q=Symbol("movementX"),J=Symbol("movementY"),W=Symbol("Canvas"),Q=(t,n,e)=>({x:(n-t.position.x)/t.scale,y:(e-t.position.y)/t.scale}),tt=(t,n)=>Q(t,n.clientX,n.clientY);function nt(t,n){const e=n/2;function o(t){const o=Math.round(t/n)*n;return t-o>=0?o+e:o-e}return t.x=o(t.x),t.y=o(t.y),t}function et(t,n){return{id:t.pointerId,pos:p(t),shift:{x:n.position.x-t.clientX,y:n.position.y-t.clientY}}}let ot;function it(t){t&&!ot?(ot=m("","z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; background: #fff; opacity: 0"),ot.classList.add("blnk"),g(ot)):t||(ot?.remove(),ot=null)}const at=async t=>{try{return it(!0),await t()}catch(t){console.log(t),alert("Error")}finally{it(!1)}};function st(t,n,e,o,i){const a=m(null,"position: fixed; left: 50%; top: 30%; box-shadow: 0 4px 24px rgba(0,0,0,.12); border-radius: 8px; background-color: rgb(255,255,255);");function s(t,n){a&&(o?(a.style.right="0",a.style.left="unset",a.style.marginRight=`${t}px`):a.style.left=`${t}px`,a.style.top=i?`${window.scrollY+n}px`:window.scrollY+n-a.getBoundingClientRect().height+"px")}return a.setAttribute("ap-modal",""),a.append(t),g(a),null!=n?s(n,e):a.style.transform="translate(-50%, -30%)",a[pt]={position:s,del:()=>{a?.remove()}},a}const ct=t=>((t,n)=>{const e=t=>t&&t!==document&&t!==window?t.closest(n)||e(t.getRootNode().host):null;return e(t)})(t,"[ap-modal]"),rt=()=>{for(;;){const t=document.querySelector("[ap-modal]");if(!t)return;t[pt]?.del()}};function lt(t,n,e,o,i,a){const s=m(null,"z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; "+(o?"background-color: rgba(52,71,103,.05);":""));g(s);const r=st(t,n,e,i,a);s.append(r);const l=ut(s,r,(t=>{"pointerdown"===t?.type?c(s,"click",(t=>{t.stopImmediatePropagation(),u()}),!0):u()}),(t=>t.stopImmediatePropagation())),d=r[pt].del,u=()=>{d(),l(),s.remove()};return r[pt].del=u,r}function dt(t,n,e){const o=st(t,n,e),i=o[pt].del,a=ut(document,o,(t=>s())),s=()=>{i(),a()};return o[pt].del=s,o}function ut(t,n,e,o){const i=((t,n,e,o)=>L(t,n,e,o))(t,n,e,o),a=V((()=>{((t,n)=>{const e=t.querySelectorAll(n);return e?.length?e[e?.length-1]:null})(document,"[ap-modal]")===n&&(e(),s())})),s=()=>{i(),a()};return s}function ht(t,n,e,o){const i=m(""+(o?"":''),"display: flex; flex-flow: column; padding: 15px;");i.className=n??"mds",o?i.append(o):l(i,"[ap-close]",(t=>a[pt].del())),i.append(t);const a=e?lt(i,null,null,!0):dt(i);return a}const pt=Symbol(0);function ft(t){const n=m(`${t}`,"min-width: 150px;\n\t\tbackground-color: rgb(52,71,103);\n\t\tcolor: #fff;\n\t\ttext-align: center;\n\t\tborder-radius: 8px;\n\t\tpadding: 10px;\n\t\tposition: fixed;\n\t\tz-index: 1;\n\t\tleft: 50%;\n\t\ttop: 30px;\n\t\ttransform: translate(-50%, -30%)");n.className="toast",g(n),setTimeout((t=>n.remove()),2800)}const mt="https://api.dgrm.net/api/",gt=`${mt}s/`,vt=async t=>{const n=await x(`${gt}${t}`),e=n.headers.get("X-Dgrm-loc");return{e:n.headers.has("X-Dgrm-edit"),m:n.headers.has("X-Dgrm-manage"),n:decodeURIComponent(n.headers.get("X-Dgrm-name")),l:e?decodeURIComponent(e):null,d:await n.json()}},wt=async t=>{const n=await x(`${gt}a/l/${t??""}`);let e;try{e=await n.json()}catch{}return e};async function yt(t,n,e,o){const i=new URL(`${gt}a/c`);return bt(i,"k",t),n&&bt(i,"f",n.toString()),bt(i,"n",e),(await k(i.toString(),o)).text()}function xt(){const t=new Date;return`${t.getUTCFullYear()}${(t.getUTCMonth()+1).toString().padStart(2,"0")}${I()}`}const bt=(t,n,e)=>t.searchParams.set(n,e);function kt(t){return Ct(t).find((t=>!t.hasAttribute("data-evt-no")))}function $t(t){return Ct(t)[0]}function Ct(t){return document.elementsFromPoint(t.clientX,t.clientY).sort(((t,n)=>{const e=t.getAttribute("data-evt-index"),o=n.getAttribute("data-evt-index");return e===o?0:e>o?-1:1}))}const Mt=Symbol("path"),Ht=Symbol("shape"),Lt="1.2",Vt=()=>({v:Lt,s:{}}),zt=t=>{return!((n=t.s)&&Object.keys(n).length);var n},St=t=>Zt([...t.children],!1);function Zt(t,n,e){const o=Vt();if(!t?.length)return o;const i=new Map,a=t=>{let e=i.get(t);if(!e){const a=K(t[Ht].data);n&&(a.id=I(),e=a.id),i.set(t,e),o.s[a.id]=a}return e},s=i=>{if(o.s[i[Mt].data.id])return;const s=o=>{return n?(i=o.shape?.shapeEl,-1!==t.indexOf(i)?{s:a(o.shape.shapeEl),k:o.shape.connectorKey}:{p:K(o.data)}):o.shape?(e&&a(o.shape.shapeEl),{s:o.shape.shapeEl[Ht].data.id,k:o.shape.connectorKey}):{p:K(o.data)};var i},c=i[Mt].data,r={id:n?I():c.id,type:0,s:s(c.s),e:s(c.e)};c.styles&&(r.c=K(c.styles)),o.s[r.id]=r};for(const n of t)n[Ht]?(a(n),e&&n[Ht].paths?.forEach(s)):s(n);return o}function Bt(t){return{v:Lt,s:{[t.id]:0!==t.type?t:(n=t,{id:n.id,type:0,s:{p:n.s.data},e:{p:n.e.data},c:n.styles})}};var n}const Dt=(t,n)=>Et(t,"1.1"===n.v?function(t){const n={v:Lt,s:{}};return t.s.forEach(((t,e)=>{switch(t.type){case 0:{const o=t,i={id:e.toString(),type:0,c:o.c?.map((t=>"d-"+t)),s:{s:o.s.s?.toString(),k:o.s.k,p:o.s.p},e:{s:o.e.s?.toString(),k:o.e.k,p:o.e.p}};n.s[i.id]=i;break}default:{const o=t;o.id=e.toString(),o.styles=o.styles?.map((t=>"d-"+t)),n.s[o.id]=o;break}}})),n}(n):n);function Et(t,n){if(n.v!==Lt)return alert("Wrong format"),null;const o=new Map;function i(n){let e=o.get(n);return e||(e=At(t,n),o.set(n,e)),e}const a=t=>n.s[t]?i(n.s[t]):e(t),s=[];for(const e in n.s)switch(n.s[e].type){case 0:{const o=Rt(t,n.s[e],a);o&&s.push(o);break}default:i(n.s[e])}return[...o.values(),...s]}function Tt(t,n){const o=e(n.id);return o?((o[Ht]||o[Mt]).patch(n),o):t[W].shapeMap[n.type].create(n)}const At=(t,n)=>Tt(t,K(n)),Rt=(t,n,e)=>{const o=function(t,n){const e=t=>{const e=n(t.s);return e?{shape:{shapeEl:e,connectorKey:t.k}}:null},o=t=>t.p?{data:{dir:t.p.dir,position:N(t.p.position)}}:e(t),i={type:0,id:t.id,styles:t.c,s:o(t.s),e:o(t.e)};return i.e&&i.s?i:null}(n,e);return o?Tt(t,o):null};function Pt(t){for(const n in t.s)Ut(t.s[n]);return t}function Ut(t){switch(t.type){case 6:delete t.d,delete t.r;break;case 7:delete t.d}}const Ot=(t,n,e)=>S(t,Nt(n,e)),Nt=(t,n)=>z("dgrmc",{newIds:t,shapesToCreateData:n}),jt=(t,n)=>z("dgrmd",{delIds:t,shapesToDelData:n}),It=(t,n,e)=>S(t,Ft(Pt(n),Pt(e))),Ft=(t,n)=>z("dgrmu",{shapesBeforeUpdData:t,shapesToUpdData:n}),Kt=(t,n,e,o)=>z("dgrmdc",{delIds:t,shapesToDelData:n,newIds:e,shapesToCreateData:o});function Yt(t,n,e,o){const i=R(((t,n,e)=>{const o=Q(t,n,e);return nt(o,t.cell),o})(t[W].data,e,o),function(t){const n=function(t){const n={x:1/0,y:1/0},e={x:-1/0,y:-1/0};return _t(t,(t=>{t&&(n.x>t.x&&(n.x=t.x),n.y>t.y&&(n.y=t.y),e.x{n&&nt(R(n,i),t[W].data.cell)}))}function _t(t,n){for(const e in t.s)0===t.s[e].type?(n(t.s[e].s.p?.position),n(t.s[e].e.p?.position)):n(t.s[e].position)}const Xt=(t,n)=>t[W].select(Gt(t,Zt(n,!0),!0));function Gt(t,n,e,o,i){return Yt(t,n,null==o?window.innerWidth/2:o,null==i?window.innerHeight/2:i),qt(t,n,e)}function qt(t,n,e){return e&&Ot(t,nn(n),K(n)),Jt(t,n)}function Jt(t,n){return t[W].select(null),Et(t,n)}function Wt(t,n){((t,n,e)=>{S(t,jt(n,e))})(t,tn(n),Zt(n,!1,!0)),en(n)}function Qt(t,n,e){((t,n,e,o,i)=>{S(t,Kt(n,e,o,i))})(t,tn(n),Zt(n,!1,!0),nn(e),K(e)),en(n),Jt(t,e)}const tn=t=>t.map((t=>(t[Ht]||t[Mt]).data.id)),nn=t=>Object.keys(t.s),en=t=>t.forEach((t=>(t[Ht]||t[Mt]).del())),on='',an='',sn='',cn='';function rn(n){for(const e in n.cons)o(t(n.el,e),n.cons[e].position)}function ln(t,n){const e=t.getBoundingClientRect(),o=st(n,e.left+10,e.top+10);return{del:o[pt].del,updPos:()=>{const n=t.getBoundingClientRect();o[pt].position(n.left+10,n.top+10)}}}const dn=(t,n)=>({del:st(t,n.clientX-66,n.clientY-10)[pt].del}),un=(t,n,e)=>{const o=Zt([n],!1,!1);(n[Ht]??n[Mt]).patch(e),It(t,o,Zt([n],!1,!1))};function hn(t,n){return function(t,n){const e=t.el[Ht].data;null!=n.title&&e.title!==n.title&&(e.title=n.title,t.drawTxt());n.styles&&(e.styles=n.styles,mn(t.el))}(t,n),pn(t,n)}function pn(t,n){const e=n.position&&!j(t.el[Ht].data.position,n.position);return e&&U(t.el[Ht].data.position,n.position),e}const fn=t=>t.forEach((t=>{const n=e(t);n&&(n[Ht]||n[Mt]).del()}));function mn(t){const n=(t[Ht]??t[Mt]).data;n.styles||(n.styles=[]),T(t.classList,(n=>{n?.startsWith("d-")&&t.classList.remove(n)})),i(t,...n.styles)}const gn=(t,n,e)=>{const o=(n[Ht]??n[Mt]).data;o.styles||(o.styles=[]);const i={styles:K(o.styles)};un(t,n,function(t,n){const e=t.styles.findIndex((t=>t.startsWith("d-cl-")));e>-1&&t.styles.splice(e,1);n&&t.styles.push(n);return t}(i,e))};function vn(t,n){const e=Number(n.getAttribute("bg"));if(3===e)return void t.append(n);const o=t.querySelector(wn(e+1,3).map((t=>`[bg="${t}"]`)).join());o?o.before(n):t.append(n)}const wn=(t,n)=>[...Array(n-t+1).keys()].map((n=>n+t));class yn extends HTMLElement{constructor(t,n){super(),this.o=n,this.g=t}connectedCallback(){const t=t=>this.o[Mt].data.styles?.includes(t)?'class="actv"':"",n=this.attachShadow({mode:"closed"});n.innerHTML=`
`,c(n.getElementById("edit"),"cmd",(t=>{switch(t.detail.cmd){case"style":gn(this.g,this.o,t.detail.arg);break;case"del":Wt(this.g,[this.o]);break;case"copy":Xt(this.g,[this.o])}})),l(n,"[data-cmd]",(t=>{const n=d(t,"data-cmd-arg"),e={styles:this.o[Mt].data.styles?K(this.o[Mt].data.styles):[]},o=e.styles.indexOf(n);o>-1?(e.styles.splice(o,1),a(t.currentTarget,"actv")):(e.styles.push(n),i(t.currentTarget,"actv")),un(this.g,this.o,e)}))}}function xn(t,n,e,o){const i=Hn(t,n);i.shape={shapeEl:e,connectorKey:o},$n(t,i)}function bn(t,n,e){const o=Hn(t,n);o.shape.shapeEl!==Ln(t,o)&&Cn(t,o),o.shape=null,o.data={dir:o.data.dir,position:e}}function kn(t,n){const e=t[Mt].data,o=o=>{const i=e[o],a=n[o];i.shape?null==a.shape||i.shape.shapeEl===a.shape.shapeEl&&i.shape.connectorKey===a.shape.connectorKey?a.data&&bn(t,o,a.data.position):function(t,n,e,o){const i=Hn(t,n);if(i.shape.shapeEl===e&&i.shape.connectorKey===o)return;i.shape.shapeEl!==e&&i.shape.shapeEl!==Ln(t,i)&&Cn(t,i);i.shape={shapeEl:e,connectorKey:o},$n(t,i)}(t,o,a.shape.shapeEl,a.shape.connectorKey):null!=a.shape?xn(t,o,a.shape.shapeEl,a.shape.connectorKey):U(i.data.position,a.data.position)};o("s"),o("e")}function $n(t,n){n.data=Mn(n.shape).pathAdd(n.shape.connectorKey,t)}customElements.define("ap-path-settings",yn);const Cn=(t,n)=>Mn(n.shape)?.pathDel(t),Mn=t=>t?.shapeEl[Ht],Hn=(t,n)=>t[Mt].data[n],Ln=(t,n)=>(t[Mt].data.e===n?t[Mt].data.s:t[Mt].data.e).shape?.shapeEl;function Vn(n,e){const o=f("g",'');o.id=e.id,i(o,"shpath"),e.s.el=t(o,"start"),e.e.el=t(o,"end"),e.styles=e.styles??["d-arw-e"];const s=Tn(o,"path","outer","selected");function c(){if(!e.s.shape||!e.e.shape){const t=function(t,n){const e=Math.atan2(n.y-t.y,n.x-t.x);return An(e,-.8,.8)?"left":An(e,.8,2.4)?"top":An(e,2.4,3.2)||An(e,-3.2,-2.4)?"right":"bottom"}(e.s.data.position,e.e.data.position);e.e.shape||(e.e.data.dir=t),e.s.shape||(e.s.data.dir=En(t))}const t=function(t){let n=.5*Math.hypot(t.s.data.position.x-t.e.data.position.x,t.s.data.position.y-t.e.data.position.y);function e(t){return"right"===t.dir||"left"===t.dir?"right"===t.dir?t.position.x+n:t.position.x-n:t.position.x}function o(t){return"right"===t.dir||"left"===t.dir?t.position.y:"bottom"===t.dir?t.position.y+n:t.position.y-n}n=n>70?70:n<15?15:n;const i=(n,e,o,i)=>t.styles.includes(o)?n.position[e]+(i[n.dir]??0):n.position[e],a=(t,n)=>i(t,"x",n,{left:-6,right:6}),s=(t,n)=>i(t,"y",n,{top:-6,bottom:6});return`M ${a(t.s.data,"d-arw-s")} ${s(t.s.data,"d-arw-s")} C ${e(t.s.data)} ${o(t.s.data)}, ${e(t.e.data)} ${o(t.e.data)}, ${a(t.e.data,"d-arw-e")} ${s(t.e.data,"d-arw-e")}`}(e);s.forEach((n=>n.setAttribute("d",t))),Zn(e.s),Zn(e.e)}let r,l;let d,u,p=0;const m=()=>{u&&(u(),u=null)},g=t=>{m(),u=V(t)};function v(t){switch(p){case 0:y();break;case 1:p=2,d=H(o,(t=>w(!0))),g((()=>{w(!0),y()})),function(t){r||(r=dn(new yn(n,o),t))}(t)}}function w(t){t||(l=null),p=0,a(o,"select","highlight-s","highlight-e"),Bn(e.s,1),Bn(e.e,1),r?.del(),r=null,d&&(d(),d=null),m()}function y(){p=1,i(o,"s"===l?"highlight-s":"e"===l?"highlight-e":"select"),Bn(e.s,2),Bn(e.e,2),vn(n,o),g((()=>{w(!0)}))}return o[Mt]={data:e,drawPosition:c,click:(t,n)=>{if(0===p||l){const o=n||t?Rn(e,n??kt(t)):null;Pn(l,o)||w(),l=o}v(t)},patch:t=>{e.styles&&(e.styles=t.styles,mn(o)),t.s&&kn(o,t),c()},moveCapture:(t,i,a)=>{w(!0);const s=a?null:Zt([o],!1,!0),r=t=>{w(!0),s?It(n,s,Zt([o],!1,!0)):Ot(n,[e.id],Zt([o],!1,!0)),h(t)||v()},l=Rn(e,i);l?Sn(n,o,c,t,l,r):function(t,n,e,o,i){M(n,o,(o=>{!function(t,n,e,o){const i=n=>G(n,t.scale,o);zn(n.s,i),n.s.shape?.shapeEl&&n.s.shape?.shapeEl===n.e.shape?.shapeEl||zn(n.e,i);n.s.shape||n.e.shape||e()}(t[W].data,n[Mt].data,e,o)}),(o=>{!function(t,n,e){const o=n=>nt(n,t.cell);zn(n.s,o),zn(n.e,o),n.s.shape&&n.e.shape||e()}(t[W].data,n[Mt].data,e),i(o)}))}(n,o,c,t,r)},unselect:w,del:function(){w(),Cn(o,e.s),Cn(o,e.e),o.remove()}},e.styles&&i(o,...e.styles),e.s.shape&&$n(o,e.s),e.e.shape&&$n(o,e.e),c(),o}function zn(t,n){t.shape?(n(Mn(t.shape).data.position),Mn(t.shape).drawPosition()):n(t.data.position)}const Sn=(t,n,e,o,l,d)=>{n[Mt].data[l].shape&&bn(n,l,tt(t[W].data,o)),n.style.pointerEvents="none";const u=function(t){let n=null;function e(t){const e=$t(t);if(n!==e){s(e,"hovertrack")&&i(e,"hover");let t=!1;s(e?.parentElement,"hovertrack")&&(i(e.parentElement,"hover"),t=!0),a(n,"hover"),n?.parentElement===e?.parentElement&&t||a(n?.parentElement,"hover"),n=e}}return c(t,"pointermove",e),function(){r(t,"pointermove",e),a(n,"hover"),a(n?.parentElement,"hover"),n=null}}(n.parentElement);M(n[Mt].data[l].el,o,(o=>{G(n[Mt].data[l].data.position,t[W].data.scale,o),e()}),(o=>{const i=$t(o),a=i?.getAttribute("data-connect");a?xn(n,l,i.parentElement,a):nt(n[Mt].data[l].data.position,t[W].data.cell),e(),u(),n.style.pointerEvents="unset",d(o)}))};function Zn(t){t.el.style.transform=`translate(${t.data.position.x}px, ${t.data.position.y}px) rotate(${Dn(t.data.dir)}deg)`}function Bn(t,n){t.el.firstElementChild.setAttribute("data-evt-index",n.toString())}const Dn=t=>"right"===t?180:"left"===t?0:"bottom"===t?270:90,En=t=>"left"===t?"right":"right"===t?"left":"top"===t?"bottom":"top";const Tn=(n,...e)=>e.map((e=>t(n,e))),An=(t,n,e)=>n<=t&&t<=e,Rn=(t,n)=>t.e.el.contains(n)?"e":t.s.el.contains(n)?"s":null,Pn=(t,n)=>!t||t===n,Un=(t,n,e)=>Nn(t,n,e,(t=>function(t){return t.split(" ")?.forEach((n=>{(n.startsWith("http://")||n.startsWith("https://"))&&(t=t.replace(n,`${new URL(n).hostname}`))})),t}(jn(t)))),On=(t,n,e)=>Nn(t,n,e,jn);function Nn(t,n,e,o){const i=function(t,n,e){let o=0;return{s:t.split("\n").map(((t,i)=>(o=i,`${0===t.length?".":e(t)}`))).join(""),c:o}}(e||"",t.x?.baseVal[0]?.value??0,o);t.innerHTML=i.s,null!=n&&t.y.baseVal[0].newValueSpecifiedUnits(t.y.baseVal[0].SVG_LENGTHTYPE_EMS,i.c>0?n-i.c/2:n)}function jn(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'").replaceAll(" "," ")}const In=(t,n)=>ln(n,Kn(t,n)),Fn=(t,n,e)=>dn(Kn(t,n),e);function Kn(t,n){const e=new Yn;return c(e,"cmd",(e=>{switch(e.detail.cmd){case"style":gn(t,n,e.detail.arg);break;case"del":Wt(t,[n]);break;case"copy":Xt(t,[n])}})),e}class Yn extends HTMLElement{connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${an} ${on}
`;{const n=t.getElementById("pnl");let e;l(t,"[data-toggle]",(o=>{const i=t=>{const e=ct(this);e.style.top=`${e.getBoundingClientRect().top+window.scrollY+t*n.getBoundingClientRect().height}px`};e&&(i(1),_n(e,!1));const a=t.getElementById(d(o,"data-toggle"));e!==a?(_n(a,!0),i(-1),e=a):e=null}))}l(t,"[data-cmd]",(t=>{this.dispatchEvent(new CustomEvent("cmd",{detail:{cmd:d(t,"data-cmd"),arg:d(t,"data-cmd-arg")}}))}))}}function _n(t,n){t.style.display=n?"unset":"none"}function Xn(n,e,o,i,a,s,c,r,l=0,d){const u=qn(e.id,o,i),h={el:t(u,"text"),vMid:l};d??=Un;const p=()=>d(h.el,h.vMid,e.title);p();return{el:u,cons:i,draw:Jn(n,u,e,i,h,r??In,(t=>c(h.el,t)),a,s,null).draw,drawTxt:p}}function Gn(t,n,e,o,i,a,s,c,r){const l=qn(n.id,e,o);return{el:l,cons:o,draw:Jn(t,l,n,o,null,c??Fn,null,i,a,s,r).draw}}customElements.define("ap-shape-edit",Yn);const qn=(t,n,e)=>{const o=f("g",`${n}\n\t${Object.entries(e).map((t=>``)).join()}`);return o.id=t,o};function Jn(t,n,e,s,c,r,l,d,u,p,m){let g,v,w,y;e.styles??=[];const x=()=>Zt([n],!1,!1);function b(t){e.title=t,l()}const k=function(t,n,e,s,c,r,l,d,u,p){i(n,"hovertrack");const m=K(s),g=new Set;let v,w,y,x=0;function b(){v?.remove(),v=null}const k=()=>{y&&(y(),y=null)},$=t=>{k(),y=V(t)};function C(){n.style.transform=`translate(${e.position.x}px, ${e.position.y}px)`;for(const t in s)m[t].position={x:s[t].position.x+e.position.x,y:s[t].position.y+e.position.y};for(const t of g)t[Mt].drawPosition()}function L(){x=1,i(n,"select"),l&&(v=function(t,n){const e=f("circle");return e.setAttribute("data-evt-index","2"),i(e,"resizer"),o(e,n),t.append(e),e}(n,l())),$((()=>z()))}function z(t){r(),x=0,a(n,"select","highlight"),t||b(),w&&(w(),w=null),k()}const S=t=>{switch(x){case 0:L();break;case 1:x=2,a(n,"select"),i(n,"highlight"),b(),w=H(n,(t=>z())),$((()=>{z(),L()}))}c(t,x)},Z=(e,o,i)=>{if(o===v){z(!0);const o=Zt([n],!1,!1);return void M(v,e,d,(e=>{u&&u(),b(),L(),It(t,o,Zt([n],!1,!1))}))}z();const a=o.getAttribute("data-connect");if(a){const o=Vn(t,{id:I(),type:0,s:{shape:{shapeEl:n,connectorKey:a}},e:{data:{dir:En(m[a].dir),position:tt(t[W].data,e)}}});return n.parentNode.append(o),t[W].select(h(e)?null:[o]),o[Mt].moveCapture(e,o[Mt].data.e.el,!0),void g.add(o)}B(e,i)};p??=()=>nt(e.position,t[W].data.cell);const B=(o,i)=>{const a=i?null:Zt([n],!1,!1);M(n,o,(n=>{G(e.position,t[W].data.scale,n),C()}),(o=>{p(),C(),a?It(t,a,Zt([n],!1,!1)):Ot(t,[e.id],Zt([n],!1)),h(o)||L()}))};return n[Ht]={data:e,drawPosition:C,click:S,moveCapture:Z,unselect:z,del:function(){z();for(const t of g)t[Mt].del();n.remove()},paths:g,pathAdd:function(t,n){return g.add(n),m[t]},pathDel:function(t){g.delete(t)}},C}(t,n,e,s,((o,i)=>{1===i&&vn(t,n),2===i&&(c&&!g&&(w=e.title,v=x(),On(c.el,c.vMid,e.title),l(),g=function(t,n,e,o,i){let a=f("foreignObject");const s=document.createElement("textarea"),c=()=>function(t,n,e,o,i){const a=t.getBBox(),s=a.width+20;n.width.baseVal.value=s+2*o+2,n.x.baseVal.value=a.x-o-("center"===i?10:"right"===i?20:0),n.height.baseVal.value=a.height+2*o+3,n.y.baseVal.value=a.y-o,e.style.width=`${s}px`,e.style.height=`${a.height}px`}(t,a,s,l,r.textAlign);s.value=e||"",s.oninput=function(){On(t,n,s.value),o(s.value),c()},i&&(s.onblur=function(){i(s.value)}),s.onpointerdown=function(t){t.stopImmediatePropagation()},a.appendChild(s),t.parentElement.appendChild(a);const r=getComputedStyle(s),l=parseInt(r.paddingLeft)+parseInt(r.borderWidth);return c(),s.focus(),{dispose:()=>{a.remove(),a=null},draw:c}}(c.el,c.vMid,e.title,b)),y||(y=r(t,n,o)))}),(function(){g&&(Un(c.el,c.vMid,e.title),l(!0),g?.dispose(),g=null),y?.del(),y=null,v&&w!==e.title&&It(t,v,x()),v=null,w=null}),d,u,p,m);return e.styles&&i(n,...e.styles),{draw:()=>{k(),y?.updPos&&y?.updPos(),g?.draw()}}}function Wn(n,e,o){t(n,e).r.baseVal.value=o}const Qn=(t,n)=>function(t,n,e){const o=v(t);return B(n,e,Math.sqrt(o.x**2+o.y**2))}(t,n,24);function te(n,e){e.w??=96,e.h??=48;const a={w:e.w,h:e.h};let s;const c=Xn(n,e,'\n\t\t \n\t\t  ',{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:24}},top:{dir:"top",position:{x:0,y:-24}}},(()=>{const t=function(t){const n=t.w/2,e=t.h/2;return{t:{x:t.position.x-n,y:t.position.y-e},b:{x:n,y:e}}}(e);return s=t.t,t.b}),(i=>{let l;(function(t,n,e,i,a){const s=R(tt(t[W].data,i),n,-1),c={};let r=!1;function l(t){s[t]<48&&(s[t]=48);const o=48*Math.round(s[t]/48),i="x"===t?"w":"h";e[i]!==o&&(!a||a()[i]<=o)&&(e[i]=o,e.position[t]=n[t]+o/2,r=!0),c[t]=s[t]-e[i]/2}return l("x"),l("y"),o(i.target,c),r})(n,s,e,i,(()=>(l??=oe(t(c.el,"text")),l)))&&(ee(a,e),r())}),(t=>{const n=oe(t,a);ne(e,n)||(ee(e,n),r())}));function r(){!function(t,n){const e=n.w/-2,o=n.h/-2;t.cons.right.position.x=-e,t.cons.left.position.x=e,t.cons.bottom.position.y=-o,t.cons.top.position.y=o,rn(t),ie(t.el,"main",n.w,n.h,e,o),ie(t.el,"outer",n.w+48,n.h+48,e-24,o-24)}(c,e),c.draw()}return i(c.el,"shrect"),96!==e.w||48!==e.h?r():c.draw(),c.el[Ht].patch=t=>{const n=hn(c,t),o=(null!=t.w||null!=t.h)&&!ne(e,t);o&&ee(e,t),o?r():n&&c.draw()},c.el}const ne=(t,n)=>t.w===n.w&&t.h===n.h,ee=(t,n)=>{t.h=n.h,t.w=n.w};function oe(t,n){const e=t.getBBox();return{w:B(n?.w,48,e.width),h:B(n?.h,48,e.height)}}const ie=(n,e,o,i,a,s)=>{const c=t(n,e);c.width.baseVal.value=o,c.height.baseVal.value=i,c.x.baseVal.value=a,c.y.baseVal.value=s};function ae(t,n){let e;n.w??=96,n.h??=96;const a=Gn(t,n,'\n\t\t \n\t\t ',{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:48}},top:{dir:"top",position:{x:0,y:-48}}},(()=>{const t=se(n);return e=t.tCanvas,t.bInner}),(i=>s(function(t,n,e,i){const a=R(tt(t[W].data,i),n,-1);e.w=a.x<48?48:a.x;const s=e.w/2;e.position.x=n.x+s,e.h=a.y<48?48:a.y;const c=e.h/2;return e.position.y=n.y+c,o(i.target,{x:s,y:c}),{x:-s,y:-c}}(t,e,n,i))),(t=>s(function(t,n){const e=t=>24*Math.round(t/24);t.w=e(t.w),t.h=e(t.h);const o=se(t);return t.position.x=n.x-o.tInner.x,t.position.y=n.y-o.tInner.y,o.tInner}(n,e))));i(a.el,"shbox");const s=t=>{a.cons.right.position.x=n.w+t.x,a.cons.left.position.x=t.x,a.cons.bottom.position.y=n.h+t.y,a.cons.top.position.y=t.y,rn(a),ie(a.el,"main",n.w,n.h,t.x,t.y),ie(a.el,"selected",n.w,n.h,t.x,t.y),ie(a.el,"outer",n.w+48,n.h+48,t.x-24,t.y-24),a.draw()};return 96!==n.w||96!==n.h?s(se(n).tInner):a.draw(),a.el[Ht].patch=t=>{const e=hn(a,t),o=(null!=t.w||null!=t.h)&&!ne(n,t);o&&ee(n,t),o?s(se(n).tInner):e&&a.draw()},a.el}function se(t){const n=ce(t.w),e=ce(t.h);return{tCanvas:{x:t.position.x-n,y:t.position.y-e},tInner:{x:-n,y:-e},bInner:{x:t.w-n,y:t.h-e}}}const ce=t=>24*Math.floor(t/2/24);function re(t,n){return ue?async function(t,n){try{const e=await window.showSaveFilePicker({suggestedName:n,types:[he]}),o=await e.createWritable();return await o.write(t),await o.close(),e}catch(t){"AbortError"!==t.name&&alert("File not saved")}}(t,n):function(t,n){const e=document.createElement("a");e.download=n,e.href=URL.createObjectURL(t),e.click(),URL.revokeObjectURL(e.href),e.remove()}(t,n)}async function le(t){const n="AbortError";try{return de?await async function(t){const[n]=await window.showOpenFilePicker({types:[t]});return{h:n,f:await n.getFile()}}(t):await new Promise(((e,o)=>{!function(t,n,e){const o=document.createElement("input");o.type="file",o.multiple=!1,o.accept=t,c(o,"cancel",(t=>e(t)),!0),c(o,"change",(async()=>n(o.files?.length?o.files[0]:null)),!0),o.click(),o.remove()}(t.inputAccept,(t=>{e({f:t})}),(t=>o(Error(n))))}))}catch(t){if(t.name===n||t.message===n)return null;throw t}}const de="showOpenFilePicker"in window,ue="showSaveFilePicker"in window,he={accept:{"image/png":[".png"]},inputAccept:"image/png"},pe={accept:{"image/*":[".png",".gif",".jpeg",".jpg"]},inputAccept:"image/*"};function fe(t,n,e){const o=m(`
${an} ${on}
`);return l(o,"[data-cmd]",(e=>{switch(d(e,"data-cmd")){case"del":Wt(t,[n]);break;case"copy":Xt(t,[n])}})),dn(o,e)}function me(t,n,e,o){ge(n,(n=>t[W].select(Gt(t,Bt(n),!0,e,o))))}function ge(t,n){!function(t,n,e){const o=new Image;o.onload=function(){const i=E(n,o.width,o.height);if(!i.c){URL.revokeObjectURL(o.src);const n=new FileReader;return n.onloadend=function(){e(n.result,i.w,i.h)},void n.readAsDataURL(t)}const a=document.createElement("canvas");a.width=i.w,a.height=i.h;const s=a.getContext("2d");s.imageSmoothingEnabled=!1,s.imageSmoothingQuality="high",s.drawImage(o,0,0,i.w,i.h),URL.revokeObjectURL(o.src),e(a.toDataURL(),i.w,i.h)},o.src=URL.createObjectURL(t)}(t,4096,((t,e,o)=>{const i=E(Math.min(document.body.clientHeight/2,264),e,o),a={id:I(),type:6,position:{x:0,y:0},w:i.w,h:i.h,r:D(o/e),d:t};we(a),n(a)}))}function ve(t,n){let e;const a=Gn(t,n,`\n\t\t \n\t\t `,{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:48}},top:{dir:"top",position:{x:0,y:-24}}},(()=>{const t=se(n);return e=t.tCanvas,t.bInner}),(i=>s(function(t,n,e,i){const a=R(tt(t[W].data,i),n,-1);e.w=a.x<48?48:a.x;const s=e.w/2;e.position.x=n.x+s,e.h=e.r*e.w;const c=e.h/2;return e.position.y=n.y+c,o(i.target,{x:s,y:c}),{x:-s,y:-c}}(t,e,n,i))),(t=>s(function(t,n){we(t);const e=se(t);return t.position.x=n.x-e.tInner.x,t.position.y=n.y-e.tInner.y,e.tInner}(n,e))),fe);i(a.el,"shimg");const s=t=>{a.cons.right.position.x=n.w+t.x,a.cons.left.position.x=t.x,a.cons.bottom.position.y=n.h+t.y,a.cons.top.position.y=t.y,rn(a),ie(a.el,"main",n.w,n.h,t.x,t.y),ie(a.el,"selected",n.w,n.h,t.x,t.y),ie(a.el,"outer",n.w+48,n.h+48,t.x-24,t.y-24),a.draw()};return s(se(n).tInner),a.el[Ht].patch=t=>{const e=pn(a,t),o=(null!=t.w||null!=t.h)&&!ne(n,t);o&&ee(n,t),o?s(se(n).tInner):e&&a.draw()},a.el}function we(t){t.w=24*Math.round(t.w/24),t.h=D(t.r*t.w)}const ye=(t,n)=>ln(n,new xe(t,n));class xe extends HTMLElement{constructor(t,n){super(),this.$=n,this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='
';const n=this.$[Ht].data,e=t.getElementById("edit");c(e,"cmd",(t=>{switch(t.detail.cmd){case"style":gn(this.g,this.$,t.detail.arg);break;case"del":Wt(this.g,[this.$]);break;case"copy":Xt(this.g,[this.$])}}));const o=t=>un(this.g,this.$,t);i(e,`ta-${n.a}`),l(t,'[data-cmd="a"]',(t=>{const s=Number.parseInt(d(t,"data-cmd-arg"));s!==n.a&&(a(e,`ta-${n.a}`),i(e,`ta-${s}`),o({a:s}))})),["d-h","d-b"].forEach((t=>{n.styles?.includes(t)&&i(e,t)})),l(t,'[data-cmd="s"]',(t=>{const s=d(t,"data-cmd-arg"),c={styles:n.styles?K(n.styles):[]};c.styles?.includes(s)?(!function(t,n){const e=t.indexOf(n);e>-1&&t.splice(e,1)}(c.styles,s),a(e,s)):(c.styles??=[],c.styles.push(s),i(e,s)),o(c)}))}}function be(n,e){e.w??=48,e.h??=48,e.a??=1;const o=Xn(n,e,`\n\t\t \n\t\t  `,{right:{dir:"right",position:{x:24,y:0}},left:{dir:"left",position:{x:-24,y:0}},bottom:{dir:"bottom",position:{x:0,y:24}},top:{dir:"top",position:{x:0,y:-24}}},null,null,((t,n)=>s(t,n)),ye,null,Ve);i(o.el,"shtxt"),i(o.el,`ta-${e.a}`);const a=()=>t(o.el,"text"),s=(t,n)=>{n&&He(t)&&t.append(Le());const o=ke(t);ne(e,o)||(ee(e,o),u())};let c,r,l;function d(){r=e.w,c=e.h,l=e.a}function u(t){if(function(t,n){const e=n.w/-2,o=-ce(n.h);t.cons.left.position.y=o+24,t.cons.left.position.x=e,t.cons.top.position.y=o,t.cons.bottom.position.y=n.h+o,t.cons.right.position.y=o+24,t.cons.right.position.x=-e,rn(t),ie(t.el,"main",n.w,n.h,e,o),ie(t.el,"outer",n.w+48,n.h+48,e-24,o-24)}(o,e),t||l!==e.a||r!==e.w){const t=e.w/-2;let n,i;switch(e.a){case 1:n=t+8,i=(e.w-r)/2;break;case 2:n=0,i=0;break;case 3:n=-t-8,i=(e.w-r)/-2}const s=a();s.x.baseVal[0].value=n,s.querySelectorAll("tspan[x]").forEach((t=>{t.x.baseVal[0].value=n})),e.position.x+=i,l!==e.a&&(Me(o.el,l,e.a),l=e.a),r=e.w}c!==e.h&&(a().y.baseVal[0].value=Ce(e.h),e.position.y+=ce(e.h)-ce(c),c=e.h),o.draw()}return d(),48!==e.w||48!==e.h?u(!0):o.draw(),o.el[Ht].patch=t=>{const n=t.position&&!j(e.position,t.position);n&&U(e.position,t.position);let i=!1;t.styles&&(e.styles=t.styles,mn(o.el),i=!0),null!=t.title&&e.title!==t.title&&(e.title=t.title,o.drawTxt(),i=!0);let s=!1;if(null==t.w&&i){const t=ke(a());ne(e,t)||(ee(e,t),s=!0)}null==t.w||ne(e,t)||(ee(e,t),d(),s=!0),null!=t.a&&e.a!==t.a&&(Me(o.el,e.a,t.a),e.a=t.a,l=e.a,s=!0),s?(a().y.baseVal[0].value=Ce(e.h),u(!0)):n&&o.draw()},o.el}function ke(t){const n=t.getBBox();return{w:B(0,48,n.width+12),h:B(0,24,n.height+14)}}customElements.define("ap-rect-txt-settings",xe);const $e=t=>1===t.a?-16:2===t.a?0:16,Ce=t=>24-ce(t);function Me(t,n,e){a(t,`ta-${n}`),i(t,`ta-${e}`)}const He=t=>[...t.childNodes].every((t=>![...t.childNodes].some((t=>t.nodeType===Node.TEXT_NODE&&t.textContent?.trim())))),Le=()=>{const t=f("tspan"," ➜");return t.setAttribute("fill","rgb(13, 110, 253)"),t},Ve=(t,n,e)=>{Un(t,n,e),He(t)&&t.append(Le())};function ze(n,e,o){t(n,e).setAttribute("d",`M${o.l.x} ${o.l.y} L${o.t.x} ${o.t.y} L${o.r.x} ${o.r.y} L${o.b.x} ${o.b.y} Z`)}function Se(t,n){const e=t/2,o=n-e,i=e-n;return{l:{x:o,y:0},t:{x:0,y:o},r:{x:i,y:0},b:{x:0,y:i}}}const Ze=(t,n)=>B(n,48,function(t){const n=v(t);return 2*(Math.abs(n.x)+Math.abs(n.y))}(t)-20);async function Be(t,n,e){return function(t,n,e){let o,i;const a=De(t,n);if(a)o=new DataView(t,0,a.byteOffset-8),i=new DataView(t,a.byteOffset+a.byteLength+4);else{const n=t.byteLength-12;o=new DataView(t,0,n),i=new DataView(t,n)}const s=new DataView(new ArrayBuffer(8));return s.setUint32(0,e.length),s.setUint32(4,n),new Blob([o,s,e,new Uint32Array([0]),i],{type:"image/png"})}(await t.arrayBuffer(),Ee(n),e)}function De(t,n){const e=new DataView(t,8);let o,i=0,a=e.getUint32(4);for(;1229278788!==a;){if(o=e.getUint32(i),a===n)return new DataView(t,i+16,o);i=i+12+o,a=e.getUint32(i+4)}return null}function Ee(t){return new DataView((new TextEncoder).encode(t).buffer).getUint32(0)}const Te=()=>"undefined"!=typeof navigator&&(/iPad|iPhone|iPod/.test(navigator.userAgent||"")||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1),Ae=(t,n)=>Re(t,Y(St(t)),null,n);function Re(t,n,e,o){const i=t.ownerSVGElement.cloneNode(!0),a=i.querySelector("#canvas");let c;i.style.backgroundImage=null,e?(c=y(Ne(t,Pe)),T(a.children,(t=>((t,n)=>n.some((n=>s(t,n))))(t,Pe)?(Oe(t),!0):(t.remove(),!1)))):(c=t.getBoundingClientRect(),Ne(a,Ue).forEach((t=>Oe(t))));const r=i.getElementsByTagName("foreignObject");for(;r[0];)r[0].parentNode.removeChild(r[0]);const l=t[W].data,d=1/l.scale;a.style.transform=`matrix(1, 0, 0, 1, ${d*(l.position.x+15*l.scale-c.x)}, ${d*(l.position.y+15*l.scale-c.y)})`,function(t,n,e,o){const i=E(Te()?4096:10836,n.width*e*window.devicePixelRatio,n.height*e*window.devicePixelRatio),a=new Image;a.width=n.width,a.height=n.height,a.onload=function(){const t=document.createElement("canvas");t.width=i.w,t.height=i.h,t.style.width=`${i.w}px`,t.style.height=`${i.h}px`;const e=t.getContext("2d");e.imageSmoothingEnabled=!1,e.drawImage(a,n.x,n.y,n.width,n.height,0,0,i.w,i.h),URL.revokeObjectURL(a.src),t.toBlob(o,"image/png")},t.width.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,a.width),t.height.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,a.height),a.src=URL.createObjectURL(new Blob([(new XMLSerializer).serializeToString(t)],{type:"image/svg+xml;charset=utf-8"}))}(i,{x:0,y:0,height:c.height/l.scale+30,width:c.width/l.scale+30},3,(async t=>o(n?await Be(t,"dgRm",(new TextEncoder).encode(n)):t)))}const Pe=["select","highlight"],Ue=["select","highlight","highlight-e","highlight-s"],Oe=t=>{a(t,...Ue),t.querySelector(".resizer")?.remove()},Ne=(t,n)=>t.querySelectorAll(`.${n.join(", .")}`);async function je(t){const n=await async function(t,n){return De(await t.arrayBuffer(),Ee(n))}(t,"dgRm");return n?(new TextDecoder).decode(n):null}const Ie=navigator.clipboard.readText;async function Fe(t,n){const e=`web text/dgrm${Y(Zt(n,!0))}`;await async function(t){try{await navigator.clipboard.writeText(t)}catch(t){alert(t)}}(e),window.ClipboardItem&&Re(t,null,!0,(async t=>{try{await navigator.clipboard.write([new window.ClipboardItem({[t.type]:Promise.resolve(t),"text/plain":Promise.resolve(new Blob([e],{type:"text/plain"}))})])}catch{}}))}const Ke=t=>{if(!t?.startsWith("web text/dgrm"))return null;try{return _(t.slice("web text/dgrm".length))}catch{return null}};function Ye(t,n,e){const o=function(t,n){async function e(e){if(document.activeElement===t.ownerSVGElement){const o=n();if(o?.length)return e.preventDefault(),Fe(t,o),o}return null}async function o(n){const o=await e(n);o&&_e(t,o)}return document.addEventListener("copy",e),document.addEventListener("cut",o),function(){r(document,"copy",e),r(document,"cut",o)}}(t,n);function i(o){if(u(o)||"Delete"!==o.key&&"Backspace"!==o.key)"Escape"===o.key&&e();else{const e=n();e?.length&&_e(t,e)}}return c(document,"keydown",i),function(){o(),r(document,"keydown",i)}}function _e(t,n){t[W].select(null),Wt(t,n)}const Xe=()=>{if(!document.cookie.includes("dgrma=1"))return null;const t=_(localStorage.getItem("dgrm"));return t&&new Date(t.e)>new Date?t:null},Ge=(t,n,e,o)=>{n&&We(qe(),e,o,n.n,0===e?n.k:n.n,n.k,new Date,n.k?null:St(t))},qe=()=>`dgrm${Xe()?.l}`,Je=(t,n)=>oo(t,(t=>Qe(t.transaction("r","readonly").objectStore("r"),n))),We=(t,n,e,o,i,a,s,c)=>oo(t,(t=>{const r=t.transaction("r","readwrite").objectStore("r");Qe(r,(l=>{const d=l.find((t=>t.k===i));l.length>=5&&!d&&to(t,r,l[l.length-1]);const u=d?.i??a??xt();r.put({t:n,i:u,k:i,n:o,l:e,d:s}),c&&t.transaction("d","readwrite").objectStore("d").put({i:u,d:c})}))})),Qe=(t,n)=>{t.getAll().onsuccess=t=>{n((t.target.result??[]).sort(((t,n)=>n.d.getTime()-t.d.getTime())))}},to=(t,n,e,o)=>{let i=0;const a=()=>{1!==i?i++:o&&o()};n.delete(e.i).onsuccess=a,1===e.t?t.transaction("d","readwrite").objectStore("d").delete(e.i).onsuccess=a:a()},no=(t,n,e)=>oo(t,(t=>{let o=null,i=null;const a=(t,n)=>{t&&(o=t),n&&(i=n),o&&i&&(o.s=i.d,e(o))};t.transaction("r","readonly").objectStore("r").get(n).onsuccess=t=>{a(t.target.result,null)},t.transaction("d","readonly").objectStore("d").get(n).onsuccess=t=>{a(null,t.target.result)}})),eo=(t,n,e)=>oo(t,(t=>{const o=t.transaction("r","readwrite").objectStore("r");Qe(o,(i=>{const a=i.find((t=>t.k===n));a?to(t,o,a,e):e()}))})),oo=(t,n)=>{const e=indexedDB.open(t);e.onerror=t=>console.error(e.error),e.onupgradeneeded=t=>{const n=e.result;n.createObjectStore("d",{keyPath:"i"}),n.createObjectStore("r",{keyPath:"i"})},e.onsuccess=t=>{e.result.onerror=t=>console.error("indexedDB error",t),n(e.result)}},io=(t,n,e,o,i,a)=>{t[po]={k:n,n:e,e:o,m:i},lo(t,0,a)},ao=(t,n)=>{const e=F(n.name);t[po]={f:n,n:e},lo(t,1)},so=(t,n)=>{t[po]={n:n},lo(t,1)},co=t=>{t[po]=void 0,lo(t)},ro=t=>t[po],lo=(t,n=null,e=null)=>{const o=t[po];uo(o?.n),Ge(t,o,n,e)},uo=t=>{document.title=t??"DGRM - Flowchart editor"},ho=t=>t?.replace(/[/\\?%*:|"<>]/g,"-"),po=Symbol(0),fo=async(t,n)=>{const e=`${ro(t)?.n??"dgrm"}.png`;if(mo())return void await re(n,e);const o={files:[new File([n],e,{type:n.type})]};navigator.canShare&&navigator.canShare(o)?await navigator.share(o):await re(n,e)},mo=()=>screen.width>768;class go extends HTMLElement{constructor(t,n){super(),this.g=t,this.C=n}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${an}Create copy
Copy to clipboard
${mo()?sn+"Download":cn+"Send"} image
${on}Delete
`;const n=()=>ct(this)[pt].del();function e(e,o){c(t.getElementById(e),"click",(t=>{n(),o()}))}e("clone",(()=>Xt(this.g,this.C()))),e("copy",(async()=>await Fe(this.g,this.C()))),e("save",(()=>Re(this.g,Y(Zt(this.C(),!1)),!0,(async t=>fo(this.g,t))))),e("del",(()=>Wt(this.g,this.C())))}}customElements.define("ap-grp-settings",go);const vo=Symbol("p"),wo=Symbol(0);const yo=(t,n,e)=>{if(e[vo])return null;if("a"===e?.target?.tagName)return null;const o=kt(e),i=Po(o);if(!i)return zo(n),null;const a=Do(n,o),s=Ao(n);if(s&&a)return Uo(i,e),null;if(e.shiftKey||e.ctrlKey){if(s){const t=To(n);Oo(t),Ho(n,t)}return a?Lo(n,i):Ho(n,i),Zo(n,e),null}return a?dt(new go(t,(()=>So(n))),e.clientX-10,e.clientY-10):(zo(n),Ho(n,i),Zo(n,e),null)},xo=(t,n,e,o)=>{if(e[vo])return;if(t[wo]?.size)for(const n of t[wo])if(n(e,o))return;if(o===t.ownerSVGElement)return void t[W].moveCapture();const i=Do(n,o);if(!i&&!h(e))return void t[W].moveCapture();if(!i&&h(e))return zo(n),void No(Po(o),e,o);const a=Ao(n);i&&a?No(Po(o),e,o):i&&!a&&bo(t,n,e)},bo=(t,n,e)=>{const o=()=>ko(n),i=t=>$o(n,t),a=o();M(t.ownerSVGElement,e,(n=>i((e=>G(e,t[W].data.scale,n)))),(n=>{i((n=>nt(n,t[W].data.cell))),It(t,a,o())}))},ko=t=>Zt([...t.shapesToMove,...jo(t.pathEndsToMove)],!1,!0),$o=(t,n)=>{t.shapesToMove?.forEach((t=>{n(t[Ht].data.position),t[Ht].drawPosition()})),t.pathEndsToMove?.forEach((t=>{n(t.data.position),Io(t)[Mt].drawPosition()}))},Co=t=>{t[W].select(null),t[W].select(t.children)},Mo=()=>({shapes:new Set,paths:new Set,pathEnds:new Set,shapesToMove:new Set,pathEndsToMove:new Set}),Ho=(t,n)=>{if(n.shape)t.shapes.add(n.shape),i(n.shape,"highlight");else if(n.path)t.paths.add(n.path),i(n.path,"highlight"),a(n.path,"highlight-s","highlight-e"),t.pathEnds.delete(n.path[Mt].data.s),t.pathEnds.delete(n.path[Mt].data.e);else if(n.pathEnd){const e=Io(n.pathEnd);if(t.paths.has(e))return;t.pathEnds.add(n.pathEnd),i(e,Yo(n.pathEnd))}},Lo=(t,n)=>{if(n.shape)t.shapes.delete(n.shape),a(n.shape,"highlight");else if(n.path)t.paths.delete(n.path),t.pathEnds.delete(n.path[Mt].data.s),t.pathEnds.delete(n.path[Mt].data.e),a(n.path,"highlight","highlight-s","highlight-e");else if(n.pathEnd){const e=Io(n.pathEnd);if(t.paths.delete(e)){a(e,"highlight","highlight-s","highlight-e");const o=Ko(n.pathEnd);t.pathEnds.add(o),i(e,Yo(o))}else t.pathEnds.delete(n.pathEnd),a(e,Yo(n.pathEnd))}},Vo=t=>{const n=t=>a(t,"highlight","highlight-s","highlight-e");t.shapes.forEach((t=>n(t))),t.paths.forEach((t=>n(t))),t.pathEnds.forEach((t=>n(Io(t))))},zo=t=>{Vo(t);const n=To(t);n&&Oo(n),t.pathEnds.clear(),t.pathEndsToMove.clear(),t.paths.clear(),t.shapes.clear(),t.shapesToMove.clear()},So=t=>[...t.shapes,...t.paths,...jo(t.pathEnds)],Zo=(t,n)=>{Bo(t);const e=Eo(t);e&&(Vo(t),Uo(e,n))},Bo=t=>{t.shapesToMove=new Set(t.shapes),t.pathEndsToMove=new Set;const n=n=>{n.shape?t.shapesToMove.add(n.shape.shapeEl):t.pathEndsToMove.add(n)};t.paths?.forEach((t=>{n(t[Mt].data.s),n(t[Mt].data.e)})),t.pathEnds?.forEach(n)},Do=(t,n)=>[...t.shapes,...[...t.pathEnds].map((t=>t.el)),...t.paths].some((t=>t.contains(n))),Eo=t=>Ro(t)?To(t):{},To=t=>1!==t.shapes.size||t.paths.size||t.pathEnds.size?t.shapes.size||1!==t.paths.size||t.pathEnds.size?t.shapes.size||t.paths.size||1!==t.pathEnds.size?null:{pathEnd:A(t.pathEnds)}:{path:A(t.paths)}:{shape:A(t.shapes)},Ao=t=>!!To(t),Ro=t=>t.shapes.size||t.pathEnds.size||t.paths.size,Po=t=>{const n=t[Ht]?{shape:t}:t.parentElement[Ht]?{shape:t.parentElement}:t.parentElement.parentElement[Ht]?{shape:t.parentElement.parentElement}:null;if(n)return n;const e=t[Mt]?{path:t}:t.parentElement[Mt]?{path:t.parentElement}:null;if(e)return e;const o=Fo(t);return o?{pathEnd:o}:null},Uo=(t,n)=>{t.shape?t.shape[Ht].click(n):t.path?t.path[Mt].click(n):t.pathEnd&&Io(t.pathEnd)[Mt].click(n,t.pathEnd.el)},Oo=t=>{t.shape?t.shape[Ht].unselect():t.path?t.path[Mt].unselect():t.pathEnd&&Io(t.pathEnd)[Mt].unselect()},No=(t,n,e)=>{t.shape?t.shape[Ht].moveCapture(n,e):t.path?t.path[Mt].moveCapture(n,e):t.pathEnd&&Io(t.pathEnd)[Mt].moveCapture(n,e)},jo=t=>[...t].map(Io),Io=t=>t.el.parentNode,Fo=t=>{const n=t.hasAttribute("data-key")?t:t.parentElement.hasAttribute("data-key")?t.parentElement:null;if(!n)return null;const e=n.parentNode[Mt].data;return e.s.el===n?e.s:e.e},Ko=t=>{const n=Io(t)[Mt].data;return n.s===t?n.e:n.s},Yo=t=>Io(t)[Mt].data.s===t?"highlight-s":"highlight-e";function _o(t){const n=(n,e)=>(function(t,n,e){const o=f("path");o.style.pointerEvents="none",i(o,"mrkp"),t.append(o);const a=[],c=n=>a.push(Q(t[W].data,n.clientX,n.clientY)),r=new Set,l=t=>{s(t,"mrkt")&&r.add(t)};l(e),c(n),M(o,n,(t=>{c(t),o.setAttribute("d",Qo(a)),l(document.elementFromPoint(t.clientX,t.clientY))}),(n=>{let e=ti(y([o,...r]));o.remove(),e=ni(Q(t[W].data,e.x,e.y));const i={id:I(),type:7,position:e,d:""},s=[{position:{x:0,y:0},d:Qo(a)}],c=[];r.forEach((t=>{const n=t.parentNode;c.push(n),s.push(n[Ht].data)}));for(const t of s)Go(i,t);c.length?Qt(t,c,Bt(i)):qt(t,Bt(i),!0)}))}(t,n,e),!0);return((t,n)=>{t[wo]??=new Set,t[wo].add(n)})(t,n),()=>{((t,n)=>{t[wo]?.delete(n)})(t,n)}}const Xo=t=>``;function Go(t,n){const e=qo(n.d),o=N(n.position);R(o,t.position,-1);let i=n.d;for(const t of e){const n=N(t);ni(R(t,o)),i=Wo(i,n,t)}t.d+=i}const qo=t=>t.match(/M(.*?)l/g).map((t=>{const n=t.split(" ");return{x:Number.parseFloat(n[0].substring(1)),y:Number.parseFloat(n[1].substring(0,n[1].length-1))}})),Jo=t=>`M${t.x} ${t.y}l`,Wo=(t,n,e)=>t.replaceAll(Jo(n),Jo(e));function Qo(t,n=5,e=2){const o=t.length,i=o-n-1==0;let a="M",s=[t[0].x,t[0].y];const[c,r]=s.map((t=>+t.toFixed(e)));let l=`${a}${c} ${r}`,d=c,u=r;const[h,p]=[t[1].x,t[1].y],[f,m]=[(c+h)/2,(r+p)/2],[g,v]=[f-d,m-u].map((t=>+t.toFixed(e)));l+=`l${g} ${v}`,d+=g,u+=v;for(let i=1;i1&&(a="t",s=[w,y]),l+=`${a}${s.map((t=>+t.toFixed(e))).join(" ")} `,d+=w,u+=y}return i||(s=[t[o-1].x-d,t[o-1].y-u],l+=`l${s.map((t=>+t.toFixed(e))).join(" ")}`),l}const ti=t=>({x:Math.round(t.x+t.width/2),y:Math.round(t.y+t.height/2)}),ni=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t);const ei=()=>{const t=Xe()?.n;if(t)return t;const n=`User${Math.floor(100*Math.random())}`,e=prompt("Your name",n);return null==e?null:e?.substring(0,25)??n},oi=t=>document.getElementById("menu").bage(t),ii=t=>{const n=ai(t),e=document.createElement("div");return e.innerHTML=` ${t}`,e.style.cssText=`position: fixed; top:0; color:${n}; pointer-events: none;`,e.style.transform="translate(-9999px, 0px)",g(e),e},ai=t=>`hsl(${[...t].reduce(((t,n)=>n.charCodeAt(0)+((t<<5)-t)),0)%360}, 97%, 47%)`,si=(t,n,e,o)=>{const i=((t,n,e)=>({x:t.position.x+n*t.scale,y:t.position.y+e*t.scale}))(t[W].data,e,o);n.style.transform=`translate(${i.x}px, ${i.y}px)`},ci=(t,n,e)=>{const o=t=>e(Y(t)),i=c(t,"dgrmc",(t=>o([4,t.detail.shapesToCreateData]))),a=c(t,"dgrmu",(t=>o([4,t.detail.shapesToUpdData]))),s=c(t,"dgrmd",(t=>o([5,t.detail.delIds]))),l=c(t,"dgrmdc",(t=>{o([5,t.detail.delIds]),o([4,t.detail.shapesToCreateData])}));let d=!0;const u=c(t.ownerSVGElement,"pointermove",Z((e=>{if(d){const i=Q(t[W].data,e.clientX,e.clientY);o([6,n,i.x,i.y])}}),60));return()=>{d=!1,r(t,"dgrmc",i),r(t,"dgrmu",a),r(t,"dgrmd",s),r(t,"dgrmdc",l),r(t.ownerSVGElement,"pointermove",u)}},ri=(t,n)=>{Et(t,n),S(t,new CustomEvent(di,{detail:n,cancelable:!1}))},li=(t,n)=>{fn(n),S(t,new CustomEvent(ui,{detail:n,cancelable:!1}))},di="dgrmcu",ui="dgrmcd";function hi(t,n){Dt(t,n),function(t){const n=t.getBoundingClientRect(),e=window.innerWidth/2,o=window.innerHeight/2;t[W].move(e-n.width/2-n.x,o-n.height/2-n.y,1);const i=t=>t>1?1:Math.max(t,.25),a=Math.min(i(window.innerWidth/n.width),i(window.innerHeight/n.height));a<1&&t[W].scale(a,{x:e,y:o})}(t)}function pi(t){return!!gi(t)&&(t.c[t.i].u(),t.i-=1,!0)}function fi(t){const n=mi(t);return null!==n&&(t.i=n,t.c[n].r(),!0)}const mi=t=>{if(!t)return null;const n=t.i+1;return n<=t.c.length-1?n:null},gi=t=>t&&t.i>=0,vi=(t,n,e)=>$i(t,(()=>{bi(t,jt(n,e)),Et(t,e),fn(n)}),(()=>{bi(t,Nt(n,e)),Et(t,e)})),wi=(t,n,e)=>$i(t,(()=>{bi(t,Nt(n,e)),Et(t,e)}),(()=>{bi(t,jt(n,e)),fn(n)})),yi=(t,n,e)=>$i(t,(()=>{bi(t,Ft(e,n)),Et(t,n)}),(()=>{bi(t,Ft(n,e)),Et(t,e)})),xi=(t,n,e,o,i)=>$i(t,(()=>{bi(t,Nt(n,e)),Et(t,e),bi(t,jt(o,i)),Et(t,i),fn(o)}),(()=>{bi(t,jt(n,e)),fn(n),bi(t,Nt(o,i)),Et(t,i)})),bi=(t,n)=>{n[ki]=!0,S(t,n)},ki=Symbol(0);function $i(t,n,e){t[zi]??={c:[],i:-1},function(t,n,e){15===t.c.length&&(t.c.splice(0,1),t.i--),t.c.splice(t.i+1),t.i=t.c.push({u:n,r:e})-1}(t[zi],n,e),Li(t)}function Ci(t,n){n(t[zi])&&(t[W].select(null),Li(t))}const Mi=t=>Ci(t,pi),Hi=t=>Ci(t,fi);const Li=t=>{return S(t,z(Vi,(n=t[zi],{u:gi(n),r:null!==mi(n)})));var n},Vi="dgrmh",zi=Symbol("h");function Si(t){S(t,z("dgrmn",null,!0))&&(!function(t){for(t[W].select(null);t.firstChild;)(t.firstChild[Ht]||t.firstChild[Mt]).del();t[W].move(0,0,1)}(t),function(t){var n;(n=t[zi])&&(n.c=[],n.i=-1),Li(t)}(t),co(t))}const Zi=async t=>{const e=Bi(`
${Xe()?'':'
Log in to access cloud storage.
'}
`);e.s();const o=t=>{n(e.m,"ftr").style.display=t?"unset":"none"},i=async()=>{await Di(e.h,e.b,(()=>e.m[pt].del())),o(!0)};await i(),l(e.m,"[cld]",(()=>at((async()=>{o(!1),await Ei(t,e.h,e.b,!0,(async t=>await i()))})))),l(e.m,"[dvc]",(async()=>{e.m[pt].del(),await async function(t){try{const n=await le(he);if(!n)return;if(!await Ii(t,n.f))return void Fi();n.h?ao(t,n.h):so(t,F(n.f.name))}catch(t){console.log(t),Fi()}}(t)}))};const Bi=t=>{const e=ht(m(`
${t}`,"display: flex; flex-flow: column; height: 100%; overflow-y: overlay;"),"mdh",!1,m('
'));i(ct(e),"mdhc");const o=t=>n(e,t);return{m:e,h:o("hdr"),b:o("cnt"),s:()=>{ct(e).style.display="flex"}}},Di=async(t,n,e)=>{t.innerHTML='CloseRecent',l(t,"[c]",e);const o=await new Promise(((t,n)=>Je(qe(),t)));n.innerHTML=o?.length?o.map((t=>``)).join(" "):_i},Ei=async(t,n,e,o,i)=>{let a=await wt(),s={i:null,n:"Cloud"};const c=[],r=()=>Ai(e,a,((n,e)=>at((async()=>{a.d[n].n=await(async(t,n)=>(await b(`${gt}a/n/${t}`,n)).text())(n,e),n===ro(t)?.k&&((t,n,e)=>{t[po].n=n,lo(t,0,e)})(t,a.d[n].n,s?.n),r()}))),(t=>at((async()=>{if(Xi(a))return;const n=await(async t=>(await k(`${gt}a/cp/${t}`)).json())(t);a.d[n.i]={n:n.n,d:n.d},r()}))),(n=>at((async()=>{var e;await(e=n,k(`${gt}a/d/${e}`)),delete a.d[n],n===ro(t)?.k&&co(t),r()}))),(t=>at((async()=>{c.push(s),s={i:t,n:a.f[t]},l(),a=await wt(t),r()}))),((t,n)=>at((async()=>{a.f[t]=await(async(t,n)=>(await b(`${gt}f/n/${t}`,n)).text())(t,n),r()}))),(n=>at((async()=>{await(async(t,n)=>{const e=new URL(`${gt}f/d/${t}`);return bt(e,"k",n),(await k(e.toString())).json()})(n,ro(t)?.k)&&co(t),delete a.f[n],r()})))),l=()=>Ti(n,s,o,i,(()=>at((async()=>{const t=c.pop();s=t,l(),a=await wt(t.i),r()}))),(t=>at((async()=>{const n=await(async t=>(await b(`${gt}f/a`,t)).json())(t);a.f[n.i]=n.n,r()}))),((t,n)=>at((async()=>{const e=xt();a.d[e]={n:await yt(e,t,n,Vt()),d:Date.now()},r()}))));return r(),l(),()=>({f:s,d:a})},Ti=(t,n,e,o,i,a,s)=>{t.innerHTML=`\n\t\t${n.i||e?`${Yi}`:"Close"}\n\t\t${n.n??""}\n\t\t${Ki}`,l(t,"[c]",(t=>n.i?i():o()));const c=Oi("Add diagram",(()=>Ni("New diagram",(t=>s(n.i,t)))));l(t,"[af]",(t=>Ri(t,n.i?[c]:[c,Oi("Add folder",(()=>Ni("New folder",a)))])))},Ai=(t,n,e,o,i,a,s,c)=>{const r=Object.entries(n.f).sort(((t,n)=>t[1].localeCompare(n[1]))),d=Object.entries(n.d).sort(((t,n)=>(n[1].d??0)-(t[1].d??0)));t.innerHTML=`${r.map((t=>``)).join(" ")}\n\t\t${d.map((t=>``)).join(" ")}\n\t${r.length||d.length?"":_i}`,l(t,"[fld]",(t=>a(Number(t.currentTarget.getAttribute("fld"))))),l(t,"[fedt]",(t=>{const e=Number(t.currentTarget.getAttribute("fedt"));Ri(t,[Pi(n.f[e],(t=>s(e,t))),Ui((()=>c(e)))])})),l(t,"[edt]",(t=>{const a=t.currentTarget.getAttribute("edt");Ri(t,[Pi(n.d[a].n,(t=>e(a,t))),Oi("Copy",(()=>o(a))),Ui((()=>i(a)))])}))};function Ri(t,e){const o=n(t.currentTarget,"dts").getBoundingClientRect(),i=window.innerHeight-o.bottom<40*e.length;!function(t,n,e,o){const i=lt(m(`
${t.map((t=>t[0])).join(" ")}
`),n,e,!1,!0,!o);l(i,"a",(n=>{i[pt].del(),t[a(n.currentTarget)][1]()}));const a=t=>Array.prototype.indexOf.call(t.parentNode.children,t)}(e,window.innerWidth-o.right-5,i?o.top:o.bottom,i)}const Pi=(t,n)=>Oi("Rename",(()=>Ni(t,(e=>{t!==e&&n(e)})))),Ui=t=>['Delete',()=>{confirm("Delete?")&&t()}],Oi=(t,n)=>[`${t}`,n];function Ni(t,e){const o=ht(m(`
`),"mdm",!0);l(o,"[sv]",(t=>{o[pt].del(),e(ho(n(o,"nm").value))}))}async function ji(t,n,e){return!!await Ii(t,n)||(me(t,n,e.clientX,e.clientY),!1)}async function Ii(t,n){if("image/png"!==n.type)return!1;const e=await je(n);return!!e&&(Si(t),hi(t,_(e)),!0)}const Fi=()=>alert("File cannot be read. Use the exact image file you got from the application."),Ki='',Yi='',_i='
No items
',Xi=t=>Object.keys(t.d).length>29&&(alert("Maximum number of diagrams in folder exceeded"),!0),Gi=async t=>{const n=ro(t);n?.f?Ae(t,(async e=>{const o=await n.f.createWritable();await o.write(e),await o.close(),Ge(t,n,1),ia(t)})):n?.k?n.e?at((async()=>await ta(t,n.k))):alert('You do not have access to edit this diagram. Use "Save As" to make your own copy.'):qi(t)},qi=async t=>Xe()?Ji(t):Wi(t),Ji=async t=>{const e=await at((()=>(async(t,n)=>{const e=Bi(n),o=await Ei(t,e.h,e.b,!1,(()=>e.m[pt].del()));return e.s(),{m:e.m,d:()=>o()}})(t,`
`))),o=()=>e.m[pt].del(),i=()=>ho(n(e.m,"snm").value);l(e.m,"[dvc]",(n=>{o(),Qi(t,i())})),l(e.m,"[cld]",(async()=>{const n=e.d();if(Xi(n.d))return;const a=async t=>{at(t),o()},s=(e,o)=>{io(t,e,o,!0,!0,n?.f?.n),ia(t)},c=i(),r=Object.entries(n.d.d).find((t=>t[1].n===c));if(r){const n=ro(t);return r[0]===n?.k&&n.m?void a((async()=>await ta(t,n.k))):void(confirm("Overwrite existing file?")&&a((async()=>s(r[0],await(async(t,n)=>(await k(`${gt}a/o/${t}`,n)).text())(r[0],St(t))))))}a((async()=>{const e=xt();s(e,await yt(e,n.f.i,c,St(t)))}))}))},Wi=t=>{const e=ht(m(`
Log in to access cloud storage.
`),"mdm");l(e,"[dvc]",(o=>{Qi(t,ho(n(e,"nm").value)),e[pt].del()}))},Qi=(t,n)=>Ae(t,(async e=>{const o=re(e,n?.length?n:aa());if(o){const n=await o;n&&(ao(t,n),ia(t))}else so(t,n),ia(t)})),ta=async(t,n)=>{let e;if(na(t)){const o=[...t[oa].c,...t[oa].u],i=function(t,n){for(const e in t.s)n.has(e)&&Ut(t.s[e]);return t}(Zt(o.length?[...document.querySelectorAll(o.map((t=>`[id="${t}"]`)).join(","))]:null,!1),t[oa].u);t[oa].d.forEach((t=>{i.s[t]=null})),e=await(async(t,n)=>(await k(`${gt}u/${t}`,n)).json())(n,i)}else e=(await vt(n)).d;Et(t,e),ia(t)},na=t=>t[oa].c.size||t[oa].u.size||t[oa].d.size,ea=t=>{t[oa].c.clear(),t[oa].u.clear(),t[oa].d.clear()},oa=Symbol(0),ia=t=>{ea(t),ft("Saved")},aa=()=>{const t=t=>t<10?`0${t}`:t.toString(),n=new Date;return`${n.getFullYear()}-${t(n.getMonth()+1)}-${t(n.getDate())}.${t(n.getHours())}${t(n.getMinutes())}${t(n.getSeconds())}.png`};function sa(t,n,e,o){const i=n=>t[W].select(Gt(t,n,!0,e,o));if(n?.data)i(function(t){if(!t)return null;const n=new Map,e=t=>{const e=I();return n.set(t,e),e},o=[];for(const n in t.s){const i=e(t.s[n].id);t.s[i]=t.s[n],t.s[i].id=i,delete t.s[n],0===t.s[i].type&&o.push(t.s[i])}return o.forEach((t=>{t.s.s=n.get(t.s.s),t.e.s=n.get(t.e.s)})),t}(n.data));else if(n?.img)ge(n.img,(t=>i(Bt(t))));else if(n?.txt){const e=function(t,n){const e=f("text");Ve(e,null,n),e.style.visibility="hidden",t.append(e);const o=ke(e);return e.remove(),o}(t,n.txt);i(Bt({id:I(),type:3,position:{x:0,y:0},title:n.txt,w:e.w,h:e.h}))}else ft("Clipboard is empty")}class ca extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='';const n=t.getElementById("u"),e=t.getElementById("r");c(this.g,Vi,(t=>{const o=(t,n)=>n?i(t,"actv"):a(t,"actv");o(n,t.detail.u),o(e,t.detail.r)})),c(n,"click",(t=>Mi(this.g))),c(e,"click",(t=>Hi(this.g)))}}customElements.define("ap-history",ca);const ra=-1!==window.navigator.userAgent.indexOf(" OPR"),la=t=>ra&&0!==t.button,da=(t,n,e,o,i)=>t.x<=o&&o<=t.x+n&&t.y<=i&&i<=t.y+e,ua={iceServers:[{urls:"stun:stun3.l.google.com:19302"}]},ha=`${mt}m/`;async function pa(t,n,e,o){const i=ei();if(null==i)return e(),null;it(!0);const a=I(),s=new Map,c=await async function(t,n,e,o,i){const a=await async function(t,n,e){const o=new RTCPeerConnection(ua),i=o.createDataChannel("dgrm");return i.onopen=n=>t(i,n),i.onmessage=t=>n(i,t),i.onerror=t=>{o.close(),e()},await o.setLocalDescription(),new Promise(((t,n)=>{o.onicecandidate=n=>{n.candidate||t(o)}}))}(e,o,i);await((t,n,e)=>k(`${ha}o`,{m:t,c:n,s:e}))(n,t,a.localDescription.sdp);const s=X(3e3,15e3,(async()=>{try{const e=await(async(t,n)=>(await x(`${ha}a?m=${t}&c=${n}`)).text())(n,t);e&&(s(),a.setRemoteDescription({sdp:e,type:"answer"}))}catch{c(),i()}}),i),c=()=>{s(),a.close(),it(!1)};return c}(a,n,((t,n)=>t.send(Y([0,a,i]))),((n,e)=>{const o=_(e.data);switch(o[0]){case 6:si(t,s.get(o[1]),o[2],o[3]);break;case 4:ri(t,o[1]);break;case 5:li(t,o[1]);break;case 0:s.set(o[1],ii(o[2]));break;case 7:s.get(o[1]).remove(),s.delete(o[1]);break;case 2:hi(t,o[1]),l(n);break;case 3:t[W].move(o[1],o[2],1),l(n)}}),(()=>{d(),o()}));let r;const l=n=>{r=ci(t,a,(t=>n.send(t))),it(!1)},d=()=>{s.forEach((t=>t.remove())),r&&r(),c(),it(!1)};return d}async function fa(t,n,e){const o=ei();if(null==o)return n(),null;const i=I(),a=I(),s=new Map,c=(t,n)=>s.forEach(((e,o)=>o!==n?o.send(t):null)),r=await async function(t,n,e,o,i){const a=I();await((t,n)=>k(`${ha}c`,{m:t,k:n}))(t,a);const s=new Set,c=X(5e3,72e5,(async()=>{try{const i=await(async(t,n)=>{const e=await(await x(`${ha}o?m=${t}&k=${n}`)).json();return e?Object.entries(e):void 0})(t,a);if(!i?.length)return;i.forEach((async i=>{const c=await async function(t,n,e,o){const i=new RTCPeerConnection(ua);return i.ondatachannel=t=>{const a=t.channel;a.onopen=t=>n(a,t),a.onmessage=t=>e(a,t),a.onerror=t=>{i.close(),o(i,a,t)}},await i.setRemoteDescription({sdp:t,type:"offer"}),await i.setLocalDescription(),new Promise(((t,n)=>{i.onicecandidate=n=>{n.candidate||t(i)}}))}(i[1],((t,e)=>n(i[0],t,e)),e,((t,n,e)=>{s.delete(t),o(n,e)}));s.add(c),await((t,n,e,o)=>k(`${ha}a`,{m:t,k:n,c:e,s:o}))(t,a,i[0],c.localDescription.sdp)}))}catch{r(),i()}})),r=()=>{c(),s?.forEach((t=>t.close()))};return r}(i,((n,e,i)=>{e.send(Y([0,a,o]));const c=St(t);e.send(Y(zt(c)?[3,t[W].data.position.x,t[W].data.position.y]:[2,c])),s.forEach((t=>e.send(Y([0,t.i,t.n]))))}),((n,e)=>{const o=_(e.data);switch(o[0]){case 6:si(t,s.get(n).c,o[2],o[3]),c(e.data,n);break;case 4:ri(t,o[1]),c(e.data,n);break;case 5:li(t,o[1]),c(e.data,n);break;case 0:s.set(n,{i:o[1],n:o[2],c:ii(o[2])}),c(e.data,n)}}),((t,n)=>{const e=s.get(t);c(Y([7,e.i]),t),e.c.remove(),s.delete(t)}),(()=>{d(),e()})),l=ci(t,a,c),d=()=>{l(),s.forEach((t=>t.c.remove())),r()};return{dispose:d,id:i}}function ma(t,n,e){const o=m(`
Live collaboration in progress
`),i=ht(o),a=(t,n)=>l(o,t,(t=>{i[pt].del(),n()}));a("[mstop]",n),a("[mshare]",(async n=>{await navigator.clipboard.writeText($("m",t)),ft("Link copied to clipboard")}))}const ga=t=>{t[wa]?.dispose(),t[wa]=void 0,oi(!1)},va=t=>{ga(t),alert("Live collaboration disabled")},wa=Symbol(0);async function ya(t){let e=!0,o={v:!0};const i=ro(t),a=i?.k;a&&(o={},Xe()&&(o=await(async t=>(await x(`${gt}p/${t}`)).json())(i.k),e=!o.m));const s=m(`
Anyone with a link can:
${a&&e?'
You do not have access to manage permissions.
':""}
${a?"":'
To share, save diagram to the cloud.
'}
`),r=ht(s),d=n(s,"v"),u=n(s,"e"),h=async()=>{try{it(!0),await(t=i.k,n={v:d.checked,e:u.checked},k(`${gt}p/s/${t}`,n))}catch{alert("Error")}finally{it(!1)}var t,n};c(d,"change",(async t=>{d.checked||(u.checked=!1),await h()})),c(u,"change",(async t=>{u.checked&&(d.checked=!0),await h()})),l(s,"[s]",(async t=>{await navigator.clipboard.writeText($("k",i.k)),ft("Link copied to clipboard"),r[pt].del()}))}class xa extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='',this.M=!1;const e=e=>n(t,e);this.H=e;const o=t=>{this.contains(t.target)||s()};let i;let a=null;const s=()=>{a.remove(),a=null,r(document,"pointerdown",o),i&&i()},d=()=>{c(document,"pointerdown",o,!0),i=V(s),a=function(t,n,e){const o=Xe(),i=o||de,a=ro(t)?.k?"Save and sync with cloud":"Save",s=m(`
New
Open
${i?'
'+a+"
":""}
Save${ue||i?" as":""}
${mo()?sn+"Download":cn+"Send"} image
Share link
Live collaboration
${o?o.n:"Log in"}
`);s.setAttribute("options","");const d=(t,n)=>l(s,t,(async t=>{e(),await n(t)}));l(s,"[menu2]",(t=>e())),d("[new]",(()=>{Si(t)})),i&&d("[save]",(async()=>await Gi(t)));return d("[saveas]",(()=>qi(t))),d("[open]",(async()=>await Zi(t))),d("[collab]",(async n=>await async function(t){const n=()=>ga(t),e=()=>va(t);try{switch(t[wa]?.clientType){case 1:ma(t[wa].id,n);break;case 2:ma(t[wa].id,n,!0);break;default:{const o=await fa(t,n,e);if(!o)return;c(t,"dgrmn",e,!0),t[wa]={clientType:1,id:o.id,dispose:()=>{r(t,"dgrmn",e),o.dispose()}},oi(!0),ma(t[wa].id,n);break}}}catch{e()}}(t))),d("[login]",(t=>function(){const t=Xe();ht(m(t?`
${t.n}
${t.l}
Log out Delete account Delete account and all data. Disable auto payment for subscription.
`:'
Log in to access
cloud storage
'))}())),d("[link]",(n=>ya(t))),d("[down]",(n=>(t=>Ae(t,(async n=>await fo(t,n))))(t))),s}(this.g,this.M,(()=>s())),t.append(a)};c(e("menu"),"click",(t=>d()))}bage(t){(t?i:a)(this.H("menu"),"bage"),this.M=t}}customElements.define("ap-menu",xa);class ba extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='';const n=t.getElementById("smenu");n.querySelectorAll('[data-cmd="shapeAdd"]').forEach((t=>{c(t,"pointerdown",this),c(t,"click",this)})),c(n,"pointerleave",this),c(n,"pointerup",this),c(n,"pointermove",this);const e=t.getElementById("m");let o;c(e,"click",(t=>{o?(o(),o=null,e.style.opacity="0.4"):(o=_o(this.g),e.style.opacity="1")}))}handleEvent(t){switch(t.type){case"pointermove":if(!this.L){const n=document.elementFromPoint(t.clientX,t.clientY);if(n===this.V)return;this.S===this.V&&this.g.ownerSVGElement.setPointerCapture(t.pointerId),this.V=n}break;case"pointerleave":if(this.L=!0,null!=this.Z){const n=ka(this.g,this.Z,!1,t.clientX,t.clientY)[0];h(t)||this.g[W].select([n]),n[Ht]?n[Ht]?.moveCapture(t,n,!0):n[Mt]?.moveCapture(t,n,!0)}this.B();break;case"pointerdown":this.Z=parseInt(d(t,"data-cmd-arg")),this.S=document.elementFromPoint(t.clientX,t.clientY),this.V=this.S,this.L=null;break;case"pointerup":this.B();break;case"click":this.g.ownerSVGElement.focus(),this.g[W].select(ka(this.g,parseInt(d(t,"data-cmd-arg")),!0))}}B(){this.Z=null,this.S=null,this.V=null}}customElements.define("ap-menu-shape",ba);const ka=(t,n,e,o,i)=>Gt(t,Bt(0===n?{id:I(),type:0,s:{data:{dir:"right",position:{x:-24,y:0}}},e:{data:{dir:"right",position:{x:24,y:0}}}}:{id:I(),type:n,position:{x:0,y:0},title:"Title"}),e,o,i);const $a=document.getElementById("canvas");var Ca,Ma,Ha,La,Va;$a[W]={data:{position:{x:0,y:0},scale:1,cell:24},shapeMap:function(n){const e=(t,e)=>(t.setAttribute("bg",e.toString()),vn(n,t),t);return{0:{create:t=>e(Vn(n,t),2)},1:{create:i=>e(function(n,e){e.r??=48;let i,a=e.r;const s=Xn(n,e,' ',{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:48}},top:{dir:"top",position:{x:0,y:-48}}},(()=>(i=P(e.position,-e.r),{x:e.r,y:e.r})),(r=>{let l=tt(n[W].data,r).x-i.x;l<48&&(l=48);const d=48*Math.round(l/48)/2;e.r!==d&&Qn(t(s.el,"text"))<=d&&(e.r=d,e.position.x=i.x+d,e.position.y=i.y+d,c(),a=d);const u=l-e.r;o(r.target,{x:u,y:u})}),(t=>{const n=Qn(t,a);n!==e.r&&(e.r=n,c())}));function c(){s.cons.right.position.x=e.r,s.cons.left.position.x=-e.r,s.cons.bottom.position.y=e.r,s.cons.top.position.y=-e.r,rn(s),Wn(s.el,"outer",e.r+24),Wn(s.el,"main",e.r),s.draw()}return 48!==e.r?c():s.draw(),s.el[Ht].patch=t=>{const n=hn(s,t),o=null!=t.r&&e.r!==t.r;o&&(e.r=t.r),o?c():n&&s.draw()},s.el}(n,i),2)},2:{create:t=>e(te(n,t),2)},3:{create:t=>e(be(n,t),2)},4:{create:a=>e(function(n,e){e.w??=96;let a,s=e.w;const c=Xn(n,e,' ',{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:48}},top:{dir:"top",position:{x:0,y:-48}}},(()=>{const t=e.w/2;return a=P(e.position,-t),{x:t,y:t}}),(i=>{let l=tt(n[W].data,i).x-a.x;l<48&&(l=48);const d=48*Math.round(l/48);if(e.w!==d&&Ze(t(c.el,"text"))<=d){e.w=d;const t=d/2;e.position.x=a.x+t,e.position.y=a.y+t,r(),s=d}const u=l-e.w/2;o(i.target,{x:u,y:u})}),(t=>{const n=Ze(t,s);n!==e.w&&(e.w=n,r())}));function r(){const t=Se(e.w,0);c.cons.right.position.x=t.r.x,c.cons.left.position.x=t.l.x,c.cons.bottom.position.y=t.b.y,c.cons.top.position.y=t.t.y,rn(c);const n=Se(e.w,9);ze(c.el,"main",n),ze(c.el,"border",n),ze(c.el,"outer",Se(e.w,-24)),c.draw()}return i(c.el,"shrhomb"),96!==e.w?r():c.draw(),c.el[Ht].patch=t=>{const n=hn(c,t),o=null!=t.w&&e.w!==t.w;o&&(e.w=t.w),o?r():n&&c.draw()},c.el}(n,a),2)},5:{create:t=>e(ae(n,t),1)},6:{create:t=>e(ve(n,t),0)},7:{create:t=>e(function(t,n){const e=Gn(t,n,Xo(n),{},null,null,null,fe,(()=>{ni(n.position)}));return i(e.el,"shmrk"),e.draw(),e.el[Ht].patch=t=>{pn(e,t)&&e.draw()},e.el}(n,t),3)}}}($a)},$a.ownerSVGElement.addEventListener("contextmenu",(async t=>{u(t)||t.preventDefault()})),function(t){let n,e,o;function i(t){t.isPrimary&&t.isTrusted&&(n&&Math.abs(n.x-t.clientX)<3&&Math.abs(n.y-t.clientY)<3?t.stopImmediatePropagation():(n=null,void 0===t.movementX?(t[q]=e?t.clientX-e:0,t[J]=o?t.clientY-o:0,e=t.clientX,o=t.clientY):(t[q]=t.movementX,t[J]=t.movementY)))}t.addEventListener("pointerdown",(a=>{n={x:a.clientX,y:a.clientY},e=null,o=null,t.addEventListener("pointermove",i,{capture:!0,passive:!0}),t.addEventListener("pointerup",(n=>{r(t,"pointermove",i,!0)}),{capture:!0,once:!0,passive:!0})}),{capture:!0,passive:!0})}(document),function(t){c(document,"paste",(async n=>{u(n)||sa(t,(t=>{const n=t.getData("text/plain"),e=Ke(n);return e?{data:e}:t.files?.length&&t.files.item(0).type?.startsWith("image/")?{img:t.files.item(0)}:n?{txt:n}:null})(n.clipboardData))}));const n=Z((async()=>await Gi(t)),500);document.addEventListener("keydown",(async e=>{if((e.ctrlKey||e.metaKey)&&"KeyS"===e.code&&e.preventDefault(),(e.ctrlKey||e.metaKey)&&!e.repeat&&!u(e))switch(e.code){case"KeyZ":rt(),Mi(t);break;case"KeyY":rt(),Hi(t);break;case"KeyS":rt(),e.shiftKey?qi(t):n();break;case"KeyA":rt(),Co(t)}}))}($a),function(t){const n=t.ownerSVGElement;let e,a,s,l,d;const u=()=>{d?.remove(),d=null};let h=!1;function p(t){if(t[vo]||!s)return void g();t[vo]=!0,h&&(u(),h=!1);const n=t.clientX-a.x,e=t.clientY-a.y;s.width.baseVal.value=Math.abs(n),s.height.baseVal.value=Math.abs(e),n<0&&(l.x=t.clientX),e<0&&(l.y=t.clientY),s.style.transform=`translate(${l.x}px, ${l.y}px)`}function m(n){if(s&&!h){n[vo]=!0;const e=n=>da(Q(t[W].data,l.x,l.y),s.width.baseVal.value/t[W].data.scale,s.height.baseVal.value/t[W].data.scale,n.x,n.y);!function(t,n,e,o){const i=Mo(),a=t=>Ho(i,t),s=t=>t.shape&&e(t.shape.shapeEl)||o(t);for(const t of n)if(t[Ht])e(t)&&a({shape:t});else if(t[Mt]){const n=s(t[Mt].data.s),e=s(t[Mt].data.e);n&&e?a({path:t}):n?a({pathEnd:t[Mt].data.s}):e&&a({pathEnd:t[Mt].data.e})}t[W].selectedSet(i)}(t,t.children,(t=>e(t[Ht].data.position)),(t=>e(t.data.position)))}g()}function g(){clearTimeout(e),e=null,s?.remove(),s=null,u(),h=!1,r(n,"pointermove",p),r(n,"wheel",g),r(n,"pointercancel",g),r(n,"pointerup",m)}c(n,"pointerdown",(r=>{if(!r.isPrimary||kt(r)!==n||la(r))return void g();c(n,"pointermove",p),c(n,"wheel",g,!0),c(n,"pointercancel",g,!0),c(n,"pointerup",m,!0);const u=()=>{t[W].selectedSet(null),h=!0,n.setPointerCapture(r.pointerId),a={x:r.clientX,y:r.clientY},l={x:r.clientX,y:r.clientY},s=f("rect"),s.style.cssText="rx:8px; fill: rgb(108 187 247 / 51%)",o(s,l),n.append(s)};2!==r.button?e=setTimeout((t=>{u(),d=f("circle"),i(d,"ative-elem"),d.style.cssText="r:10px; fill: rgb(108 187 247 / 51%)",o(d,{x:r.clientX,y:r.clientY}),n.append(d)}),500):u()}))}($a),function(t){const n=t.ownerSVGElement;let e,o=Mo();const i=()=>{e&&(e[pt]?.del(),e=null)};let a,s;c(n,"pointerdown",(l=>{if(i(),!l.isPrimary)return r(n,"pointermove",a),r(n,"pointerup",s),void t[W].moveCapture();s=s=>{var c;r(n,"pointermove",a),c=yo(t,o,s),i(),e=c},c(n,"pointerup",s,!0),a=e=>{r(n,"pointerup",s),xo(t,o,e,kt(l))},0===l.button&&c(n,"pointermove",a,!0)})),Ye(t,(()=>So(o)),(()=>{e?i():Ro(o)&&!To(o)&&t[W].select(null)}));const l=t=>{zo(o),o=t??Mo(),Zo(o,null)};t[W].selectedSet=l,t[W].select=t=>{i();const n=Mo();if(t?.length)for(const e of t)Ho(n,Po(e));l(n)}}($a),function(t){const n=t[W].data,e=function(t,n){let e;function o(n){e!==n&&(e=n,t.style.backgroundImage=`radial-gradient(rgb(73 80 87 / ${n}) 1px, transparent 0)`)}return o(.6),t.style.backgroundSize=`${n.cell}px ${n.cell}px`,function(){const e=n.cell*n.scale;n.scale<.5?o(0):n.scale<=.9?o(.2):o(.6),t.style.backgroundSize=`${e}px ${e}px`,t.style.backgroundPosition=`${n.position.x}px ${n.position.y}px`}}(t.ownerSVGElement,n);function o(){t.style.transform=`matrix(${n.scale}, 0, 0, ${n.scale}, ${n.position.x}, ${n.position.y})`,e()}function i(t,e){if(t<.25||t>4)return;const i=t/n.scale;n.scale=t,n.position.x=i*(n.position.x-e.x)+e.x,n.position.y=i*(n.position.y-e.y)+e.y,o()}!function(t,n,e,o){let i,a,s,l,d=!1;function u(t){s=null,l=null,i?.id===t.pointerId&&(i=null),a?.id===t.pointerId&&(a=null),i||a||(r(document,"pointermove",h),r(document,"pointercancel",u),r(document,"pointerup",u),d=!1)}function h(t){if(d){if(i&&!a||!i&&a)return n.position.x=t.clientX+(i||a).shift.x,n.position.y=t.clientY+(i||a).shift.y,void o();if(!a||!i||a?.id!==t.pointerId&&i?.id!==t.pointerId)return;const c=Math.hypot(i.pos.x-a.pos.x,i.pos.y-a.pos.y),r={x:(i.pos.x+a.pos.x)/2,y:(i.pos.y+a.pos.y)/2};s&&(n.position.x=n.position.x+r.x-l.x,n.position.y=n.position.y+r.y-l.y,e(n.scale/s*c,r)),s=c,l=r}i?.id===t.pointerId&&(i=et(t,n)),a?.id===t.pointerId&&(a=et(t,n))}c(t.ownerSVGElement,"pointerdown",(t=>{!i&&!t.isPrimary||i&&a||(i||(c(document,"pointermove",h),c(document,"pointercancel",u),c(document,"pointerup",u),i=et(t,n)),a||i?.id===t.pointerId||(a=et(t,n)))})),t[W].moveCapture=()=>{d=!0}}(t,n,i,o),t.ownerSVGElement.addEventListener("wheel",(t=>{t.preventDefault();const e=()=>{n.position.x-=t.deltaX,n.position.y-=t.deltaY,o()},a=e=>i(n.scale+e,p(t));Math.abs(t.deltaY)>50?t.ctrlKey?e():a(t.deltaY<0?.25:-.25):t.ctrlKey?a(-.01*t.deltaY):e()})),t[W].move=function(t,e,i){n.position.x=t,n.position.y=e,n.scale=i,o()},t[W].scale=i}($a),function(t){const n=t.ownerSVGElement,e=t.ownerDocument;let o,i,a,s;function d(n){r(e,"pointermove",p),r(e,"pointerup",u),a||(a=function(t,n,e,o){const i=m(`${Ie?'
Paste
':""}
Select all
Add image
`),a=(t,n)=>l(i,t,(async t=>{await n(),o()}));Ie&&a("[p]",(async()=>sa(e,await(async()=>{let t;try{t=await(navigator.clipboard?.read())}catch{}if(!t?.length)return null;const n=t[0],e=n.types.includes("text/plain")?await(await n.getType("text/plain")).text():null,o=Ke(e);if(o)return{data:o};const i=n.types.find((t=>t.startsWith("image/")));return i?{img:await n.getType(i)}:e?{txt:e}:null})(),t,n)));a("[s]",(async()=>Co(e))),a("[i]",(async()=>async function(t,n,e){const o=await le(pe);o&&me(t,o.f,n,e)}(e,t,n)));return{del:dt(i,t-10,n-10)[pt].del,el:i}}(n.clientX,n.clientY,t,u),c(e,"pointerdown",h))}function u(){clearTimeout(o),o=null,a?.del(),a=null,r(e,"pointerdown",h),r(e,"pointerup",u),r(e,"wheel",u),r(e,"keydown",u),r(e,"pointerup",g),r(e,"pointercancel",g),r(e,"pointermove",f),i=!1,s=!1}function h(t){a.el.contains(t.target)||u()}function p(){u(),i=!0}function f(){s||u(),i=!0}function g(){s=!0}c(n,"pointerdown",(async function(t){t.isPrimary&&kt(t)===n?(u(),c(e,"pointermove",p,!0),c(e,"pointerup",u,!0),c(e,"wheel",u,!0),c(e,"keydown",u,!0),2!==t.button&&(o=setTimeout((n=>{d(t),c(e,"pointerup",g,!0),c(e,"pointercancel",g,!0),c(e,"pointermove",f,!0)}),500))):u()})),n.addEventListener("contextmenu",(async t=>{n===kt(t)&&(t.preventDefault(),i||d(t))}))}($a),(t=>{const n=(n,e)=>c(t,n,(t=>{t[ki]||e(t)}));n("dgrmc",(n=>vi(t,n.detail.newIds,n.detail.shapesToCreateData))),n("dgrmd",(n=>wi(t,n.detail.delIds,n.detail.shapesToDelData))),n("dgrmu",(n=>yi(t,n.detail.shapesBeforeUpdData,n.detail.shapesToUpdData))),n("dgrmdc",(n=>xi(t,n.detail.delIds,n.detail.shapesToDelData,n.detail.newIds,n.detail.shapesToCreateData)))})($a),function(t){document.body.addEventListener("dragover",(t=>{t.preventDefault()})),document.body.addEventListener("drop",(async n=>{if(n.preventDefault(),1===n.dataTransfer?.items?.length&&"file"===n.dataTransfer.items[0].kind&&n.dataTransfer.items[0].type.startsWith("image/"))if("getAsFileSystemHandle"in n.dataTransfer.items[0]){const e=await n.dataTransfer.items[0].getAsFileSystemHandle();await ji(t,await e.getFile(),n)&&ao(t,e)}else{const e=n.dataTransfer.items[0].getAsFile();await ji(t,e,n)&&so(t,F(e.name))}else Fi()}))}($a),(t=>{t[oa]={c:new Set,u:new Set,d:new Set};const n=n=>{t[oa].c.add(n),t[oa].u.delete(n),t[oa].d.delete(n)},e=n=>{t[oa].c.has(n)||t[oa].u.add(n),t[oa].d.delete(n)},o=n=>n?.forEach((n=>{t[oa].d.add(n),t[oa].c.delete(n),t[oa].u.delete(n)})),i=t=>Object.keys(t.s).forEach(e);c(t,"dgrmc",(t=>{t.detail.newIds?.forEach(n)})),c(t,"dgrmu",(t=>i(t.detail.shapesToUpdData))),c(t,"dgrmd",(t=>o(t.detail.delIds))),c(t,"dgrmdc",(t=>{o(t.detail.delIds),t.detail.newIds?.forEach(n)})),c(t,di,(t=>i(t.detail))),c(t,ui,(t=>o(t.detail))),t.addEventListener("dgrmn",(n=>{!na(t)||window.confirm("Changes you made may not be saved. Continue?")?ea(t):n.preventDefault()})),window.addEventListener("beforeunload",(n=>{na(t)&&n.preventDefault()}))})($a);{const t=new URL(window.location.href),n=n=>t.searchParams.get(n);n("usr")&&(Ca=n("usr"),Ma=n("usrn"),Ha=parseInt(n("expr")),localStorage.setItem("dgrm",Y({l:Ca,n:Ma,e:Ha})),C("usr","usrn","expr"))}(t=>{const n=new xa(t);n.id="menu",g(n)})($a),g(new ca($a)),g(new ba($a));{const t=new URL(window.location.href),e=n=>t.searchParams.get(n);if(e("k")){it(!0);const t=e("k");try{const n=await vt(t);hi($a,n.d),io($a,t,n.n,n.e,n.m,n.l)}catch{alert("The link is broken or you do not have permission. Try to relogin."),await(Va=t,new Promise(((t,n)=>eo(qe(),Va,(()=>t())))))}C("k"),it(!1)}else if(e("l")){try{const t=e("l"),n=await(La=t,new Promise(((t,n)=>no(qe(),La,t))));n&&(hi($a,n.s),so($a,n.n))}catch{alert("The link is broken.")}C("l")}else if(e("m")){const t=e("m");C("m"),await async function(t,n){const e=()=>va(t);try{const o=await pa(t,n,(()=>ga(t)),e);if(!o)return;c(t,"dgrmn",e,!0),t[wa]={clientType:2,id:n,dispose:()=>{r(t,"dgrmn",e),o()}},oi(!0)}catch{e()}}($a,t)}else Xe()||function(){const t=m('');function e(){t.remove(),r(document,"pointerdown",o),r(document,"wheel",e),r(document,"keydown",e),r(document,"dragover",e)}function o(n){t.contains(n.target)||e()}t.style.display="unset",c(document,"pointerdown",o),c(document,"wheel",e,!0),c(document,"keydown",e,!0),c(document,"dragover",e,!0),c(n(t,"cl"),"click",(t=>e()),!0),g(t)}()} +const t=(t,n)=>t.querySelector(`[data-key="${n}"]`),n=(t,n)=>t.querySelector(`[${n}]`),e=t=>document.getElementById(t);function o(t,n){t.style.transform=`translate(${n.x}px, ${n.y}px)`}const i=(t,...n)=>t?.classList.add(...n),a=(t,...n)=>t?.classList.remove(...n),s=(t,n)=>t?.classList.contains(n),c=(t,n,e,o)=>(t.addEventListener(n,e,{passive:!0,once:o}),e),r=(t,n,e,o)=>t?.removeEventListener(n,e,{capture:o}),l=(t,n,e)=>t.querySelectorAll(n).forEach((t=>c(t,"click",e))),d=(t,n)=>t.currentTarget.getAttribute(n),u=t=>"TEXTAREA"===t.target.tagName.toUpperCase()||"INPUT"===t.target.tagName.toUpperCase(),h=t=>"mouse"===t.pointerType,p=t=>({x:t.clientX,y:t.clientY});function f(t,n){const e=document.createElementNS("http://www.w3.org/2000/svg",t);return n&&(e.innerHTML=n),e}const m=(t,n)=>{const e=document.createElement("div");return t&&(e.innerHTML=t),e.style.cssText=n,e},g=(...t)=>document.body.append(...t);function v(t){let n,e=0;for(const o of t.getElementsByTagName("tspan"))for(const t of w(o.getBBox())){const o=Math.abs(t.x)+Math.abs(t.y);e[{x:t.x,y:t.y},{x:t.right,y:t.y},{x:t.x,y:t.bottom},{x:t.right,y:t.bottom}];function y(t){const n={x:1/0,y:1/0},e={x:-1/0,y:-1/0};for(const o of t){const t=o.getBoundingClientRect();n.x>t.x&&(n.x=t.x),n.y>t.y&&(n.y=t.y),e.x{let e;(n=n||{}).credentials="include";try{e=await fetch(t,n)}catch(t){throw console.log(t),t}if(!e.ok)throw 401===e.status&&alert("You have to relogin."),new Error;return e},b=(t,n)=>x(t,{method:"POST",body:n}),k=(t,n)=>x(t,{method:"POST",body:n?Y(n):null}),$=(t,n)=>{const e=new URL(window.location.href);return e.searchParams.set(t,n),e.toString()},C=(...t)=>{const n=new URL(window.location.href);t?.forEach((t=>n.searchParams.delete(t))),history.replaceState(null,null,n.toString())};function M(t,n,e,o){function i(n){r(t,"pointercancel",i),r(t,"pointerup",i),r(t,"wheel",i),r(document,"pointerdown",i),r(t,"pointermove",e),o(n)}c(t,"pointercancel",i,!0),c(t,"pointerup",i,!0),c(t,"wheel",i,!0),c(document,"pointerdown",i,!0),c(t,"pointermove",e),t.setPointerCapture(n.pointerId)}const H=(t,n)=>L(t.ownerSVGElement,t,n);function L(t,n,e,o){const i=()=>{r(t,"pointercancel",a),r(t,"wheel",a),r(t,"pointerdown",a)},a=t=>{o&&o(t),n.contains(t.target)||(i(),e(t))};return c(t,"pointercancel",a),c(t,"wheel",a),c(t,"pointerdown",a),i}const V=t=>{const n=n=>{"Escape"===n.key&&t()};return c(document,"keydown",n),()=>r(document,"keydown",n)},z=(t,n,e)=>new CustomEvent(t,{detail:n,cancelable:e}),S=(t,n)=>t.dispatchEvent(n),Z=(t,n)=>{let e,o,i=!1;return function a(){if(i)return e=arguments,void(o=this);t.apply(this,arguments),i=!0,setTimeout((function(){i=!1,e&&(a.apply(o,e),e=o=null)}),n)}};function B(t,n,e){return t??=0,e<=t?t:t+Math.ceil((e-t)/n)*n}const D=t=>Math.round(100*(t+Number.EPSILON))/100,E=(t,n,e)=>{let o=!1;return n>e?n>t&&(o=!0,e*=t/n,n=t):e>t&&(o=!0,n*=t/e,e=t),{w:n,h:e,c:o}};function T(t,n){for(let e=t.length-1;e>=0;--e)n(t[e])}const A=t=>t.values().next().value;function R(t,n,e){const o=e??1;return t.x+=o*n.x,t.y+=o*n.y,t}const P=(t,n)=>({x:t.x+n,y:t.y+n}),U=(t,n)=>O(t,n.x,n.y),O=(t,n,e)=>(t.x=n,t.y=e,t),N=t=>({x:t.x,y:t.y}),j=(t,n)=>t?.x===n?.x&&t?.y===n?.y;function I(){const t=new Uint8Array(4);return window.crypto.getRandomValues(t),Array.from(t,(t=>t.toString(16).padStart(2,"0"))).join("")}const F=t=>t?.substring(0,t.lastIndexOf("."))||t,K=t=>_(Y(t)),Y=t=>JSON.stringify(t),_=t=>JSON.parse(t),X=(t,n,e,o)=>{let i,a;const s=()=>{a=setTimeout((async t=>{await e(),i||s()}),t)};s();const c=setTimeout((t=>{r(),o&&o()}),n),r=()=>{i=!0,clearTimeout(a),clearTimeout(c)};return r};function G(t,n,e){t.x+=e[q]/n,t.y+=e[J]/n}const q=Symbol("movementX"),J=Symbol("movementY"),W=Symbol("Canvas"),Q=(t,n,e)=>({x:(n-t.position.x)/t.scale,y:(e-t.position.y)/t.scale}),tt=(t,n)=>Q(t,n.clientX,n.clientY);function nt(t,n){const e=n/2;function o(t){const o=Math.round(t/n)*n;return t-o>=0?o+e:o-e}return t.x=o(t.x),t.y=o(t.y),t}function et(t,n){return{id:t.pointerId,pos:p(t),shift:{x:n.position.x-t.clientX,y:n.position.y-t.clientY}}}let ot;function it(t){t&&!ot?(ot=m("","z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; background: #fff; opacity: 0"),ot.classList.add("blnk"),g(ot)):t||(ot?.remove(),ot=null)}const at=async t=>{try{return it(!0),await t()}catch(t){console.log(t),alert("Error")}finally{it(!1)}};function st(t,n,e,o,i){const a=m(null,"position: fixed; left: 50%; top: 30%; box-shadow: 0 4px 24px rgba(0,0,0,.12); border-radius: 8px; background-color: rgb(255,255,255);");function s(t,n){a&&(o?(a.style.right="0",a.style.left="unset",a.style.marginRight=`${t}px`):a.style.left=`${t}px`,a.style.top=i?`${window.scrollY+n}px`:window.scrollY+n-a.getBoundingClientRect().height+"px")}return a.setAttribute("ap-modal",""),a.append(t),g(a),null!=n?s(n,e):a.style.transform="translate(-50%, -30%)",a[pt]={position:s,del:()=>{a?.remove()}},a}const ct=t=>((t,n)=>{const e=t=>t&&t!==document&&t!==window?t.closest(n)||e(t.getRootNode().host):null;return e(t)})(t,"[ap-modal]"),rt=()=>{for(;;){const t=document.querySelector("[ap-modal]");if(!t)return;t[pt]?.del()}};function lt(t,n,e,o,i,a){const s=m(null,"z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; "+(o?"background-color: rgba(52,71,103,.05);":""));g(s);const r=st(t,n,e,i,a);s.append(r);const l=ut(s,r,(t=>{"pointerdown"===t?.type?c(s,"click",(t=>{t.stopImmediatePropagation(),u()}),!0):u()}),(t=>t.stopImmediatePropagation())),d=r[pt].del,u=()=>{d(),l(),s.remove()};return r[pt].del=u,r}function dt(t,n,e){const o=st(t,n,e),i=o[pt].del,a=ut(document,o,(t=>s())),s=()=>{i(),a()};return o[pt].del=s,o}function ut(t,n,e,o){const i=((t,n,e,o)=>L(t,n,e,o))(t,n,e,o),a=V((()=>{((t,n)=>{const e=t.querySelectorAll(n);return e?.length?e[e?.length-1]:null})(document,"[ap-modal]")===n&&(e(),s())})),s=()=>{i(),a()};return s}function ht(t,n,e,o){const i=m(""+(o?"":''),"display: flex; flex-flow: column; padding: 15px;");i.className=n??"mds",o?i.append(o):l(i,"[ap-close]",(t=>a[pt].del())),i.append(t);const a=e?lt(i,null,null,!0):dt(i);return a}const pt=Symbol(0);function ft(t){const n=m(`${t}`,"min-width: 150px;\n\t\tbackground-color: rgb(52,71,103);\n\t\tcolor: #fff;\n\t\ttext-align: center;\n\t\tborder-radius: 8px;\n\t\tpadding: 10px;\n\t\tposition: fixed;\n\t\tz-index: 1;\n\t\tleft: 50%;\n\t\ttop: 30px;\n\t\ttransform: translate(-50%, -30%)");n.className="toast",g(n),setTimeout((t=>n.remove()),2800)}const mt="https://api.dgrm.net/api/",gt=`${mt}s/`,vt=async t=>{const n=await x(`${gt}${t}`),e=n.headers.get("X-Dgrm-loc");return{e:n.headers.has("X-Dgrm-edit"),m:n.headers.has("X-Dgrm-manage"),n:decodeURIComponent(n.headers.get("X-Dgrm-name")),l:e?decodeURIComponent(e):null,d:await n.json()}},wt=async t=>{const n=await x(`${gt}a/l/${t??""}`);let e;try{e=await n.json()}catch{}return e};async function yt(t,n,e,o){const i=new URL(`${gt}a/c`);return bt(i,"k",t),n&&bt(i,"f",n.toString()),bt(i,"n",e),(await k(i.toString(),o)).text()}function xt(){const t=new Date;return`${t.getUTCFullYear()}${(t.getUTCMonth()+1).toString().padStart(2,"0")}${I()}`}const bt=(t,n,e)=>t.searchParams.set(n,e);function kt(t){return Ct(t).find((t=>!t.hasAttribute("data-evt-no")))}function $t(t){return Ct(t)[0]}function Ct(t){return document.elementsFromPoint(t.clientX,t.clientY).sort(((t,n)=>{const e=t.getAttribute("data-evt-index"),o=n.getAttribute("data-evt-index");return e===o?0:e>o?-1:1}))}const Mt=Symbol("path"),Ht=Symbol("shape"),Lt="1.2",Vt=()=>({v:Lt,s:{}}),zt=t=>{return!((n=t.s)&&Object.keys(n).length);var n},St=t=>Zt([...t.children],!1);function Zt(t,n,e){const o=Vt();if(!t?.length)return o;const i=new Map,a=t=>{let e=i.get(t);if(!e){const a=K(t[Ht].data);n&&(a.id=I(),e=a.id),i.set(t,e),o.s[a.id]=a}return e},s=i=>{if(o.s[i[Mt].data.id])return;const s=o=>{return n?(i=o.shape?.shapeEl,-1!==t.indexOf(i)?{s:a(o.shape.shapeEl),k:o.shape.connectorKey}:{p:K(o.data)}):o.shape?(e&&a(o.shape.shapeEl),{s:o.shape.shapeEl[Ht].data.id,k:o.shape.connectorKey}):{p:K(o.data)};var i},c=i[Mt].data,r={id:n?I():c.id,type:0,s:s(c.s),e:s(c.e)};c.styles&&(r.c=K(c.styles)),o.s[r.id]=r};for(const n of t)n[Ht]?(a(n),e&&n[Ht].paths?.forEach(s)):s(n);return o}function Bt(t){return{v:Lt,s:{[t.id]:0!==t.type?t:(n=t,{id:n.id,type:0,s:{p:n.s.data},e:{p:n.e.data},c:n.styles})}};var n}const Dt=(t,n)=>Et(t,"1.1"===n.v?function(t){const n={v:Lt,s:{}};return t.s.forEach(((t,e)=>{switch(t.type){case 0:{const o=t,i={id:e.toString(),type:0,c:o.c?.map((t=>"d-"+t)),s:{s:o.s.s?.toString(),k:o.s.k,p:o.s.p},e:{s:o.e.s?.toString(),k:o.e.k,p:o.e.p}};n.s[i.id]=i;break}default:{const o=t;o.id=e.toString(),o.styles=o.styles?.map((t=>"d-"+t)),n.s[o.id]=o;break}}})),n}(n):n);function Et(t,n){if(n.v!==Lt)return alert("Wrong format"),null;const o=new Map;function i(n){let e=o.get(n);return e||(e=At(t,n),o.set(n,e)),e}const a=t=>n.s[t]?i(n.s[t]):e(t),s=[];for(const e in n.s)switch(n.s[e].type){case 0:{const o=Rt(t,n.s[e],a);o&&s.push(o);break}default:i(n.s[e])}return[...o.values(),...s]}function Tt(t,n){const o=e(n.id);return o?((o[Ht]||o[Mt]).patch(n),o):t[W].shapeMap[n.type].create(n)}const At=(t,n)=>Tt(t,K(n)),Rt=(t,n,e)=>{const o=function(t,n){const e=t=>{const e=n(t.s);return e?{shape:{shapeEl:e,connectorKey:t.k}}:null},o=t=>t.p?{data:{dir:t.p.dir,position:N(t.p.position)}}:e(t),i={type:0,id:t.id,styles:t.c,s:o(t.s),e:o(t.e)};return i.e&&i.s?i:null}(n,e);return o?Tt(t,o):null};function Pt(t){for(const n in t.s)Ut(t.s[n]);return t}function Ut(t){switch(t.type){case 6:delete t.d,delete t.r;break;case 7:delete t.d}}const Ot=(t,n,e)=>S(t,Nt(n,e)),Nt=(t,n)=>z("dgrmc",{newIds:t,shapesToCreateData:n}),jt=(t,n)=>z("dgrmd",{delIds:t,shapesToDelData:n}),It=(t,n,e)=>S(t,Ft(Pt(n),Pt(e))),Ft=(t,n)=>z("dgrmu",{shapesBeforeUpdData:t,shapesToUpdData:n}),Kt=(t,n,e,o)=>z("dgrmdc",{delIds:t,shapesToDelData:n,newIds:e,shapesToCreateData:o});function Yt(t,n,e,o){const i=R(((t,n,e)=>{const o=Q(t,n,e);return nt(o,t.cell),o})(t[W].data,e,o),function(t){const n=function(t){const n={x:1/0,y:1/0},e={x:-1/0,y:-1/0};return _t(t,(t=>{t&&(n.x>t.x&&(n.x=t.x),n.y>t.y&&(n.y=t.y),e.x{n&&nt(R(n,i),t[W].data.cell)}))}function _t(t,n){for(const e in t.s)0===t.s[e].type?(n(t.s[e].s.p?.position),n(t.s[e].e.p?.position)):n(t.s[e].position)}const Xt=(t,n)=>t[W].select(Gt(t,Zt(n,!0),!0));function Gt(t,n,e,o,i){return Yt(t,n,null==o?window.innerWidth/2:o,null==i?window.innerHeight/2:i),qt(t,n,e)}function qt(t,n,e){return e&&Ot(t,nn(n),K(n)),Jt(t,n)}function Jt(t,n){return t[W].select(null),Et(t,n)}function Wt(t,n){((t,n,e)=>{S(t,jt(n,e))})(t,tn(n),Zt(n,!1,!0)),en(n)}function Qt(t,n,e){((t,n,e,o,i)=>{S(t,Kt(n,e,o,i))})(t,tn(n),Zt(n,!1,!0),nn(e),K(e)),en(n),Jt(t,e)}const tn=t=>t.map((t=>(t[Ht]||t[Mt]).data.id)),nn=t=>Object.keys(t.s),en=t=>t.forEach((t=>(t[Ht]||t[Mt]).del())),on='',an='',sn='',cn='';function rn(n){for(const e in n.cons)o(t(n.el,e),n.cons[e].position)}function ln(t,n){const e=t.getBoundingClientRect(),o=st(n,e.left+10,e.top+10);return{del:o[pt].del,updPos:()=>{const n=t.getBoundingClientRect();o[pt].position(n.left+10,n.top+10)}}}const dn=(t,n)=>({del:st(t,n.clientX-66,n.clientY-10)[pt].del}),un=(t,n,e)=>{const o=Zt([n],!1,!1);(n[Ht]??n[Mt]).patch(e),It(t,o,Zt([n],!1,!1))};function hn(t,n){return function(t,n){const e=t.el[Ht].data;null!=n.title&&e.title!==n.title&&(e.title=n.title,t.drawTxt());n.styles&&(e.styles=n.styles,mn(t.el))}(t,n),pn(t,n)}function pn(t,n){const e=n.position&&!j(t.el[Ht].data.position,n.position);return e&&U(t.el[Ht].data.position,n.position),e}const fn=t=>t.forEach((t=>{const n=e(t);n&&(n[Ht]||n[Mt]).del()}));function mn(t){const n=(t[Ht]??t[Mt]).data;n.styles||(n.styles=[]),T(t.classList,(n=>{n?.startsWith("d-")&&t.classList.remove(n)})),i(t,...n.styles)}const gn=(t,n,e)=>{const o=(n[Ht]??n[Mt]).data;o.styles||(o.styles=[]);const i={styles:K(o.styles)};un(t,n,function(t,n){const e=t.styles.findIndex((t=>t.startsWith("d-cl-")));e>-1&&t.styles.splice(e,1);n&&t.styles.push(n);return t}(i,e))};function vn(t,n){const e=Number(n.getAttribute("bg"));if(3===e)return void t.append(n);const o=t.querySelector(wn(e+1,3).map((t=>`[bg="${t}"]`)).join());o?o.before(n):t.append(n)}const wn=(t,n)=>[...Array(n-t+1).keys()].map((n=>n+t));class yn extends HTMLElement{constructor(t,n){super(),this.o=n,this.g=t}connectedCallback(){const t=t=>this.o[Mt].data.styles?.includes(t)?'class="actv"':"",n=this.attachShadow({mode:"closed"});n.innerHTML=`
`,c(n.getElementById("edit"),"cmd",(t=>{switch(t.detail.cmd){case"style":gn(this.g,this.o,t.detail.arg);break;case"del":Wt(this.g,[this.o]);break;case"copy":Xt(this.g,[this.o])}})),l(n,"[data-cmd]",(t=>{const n=d(t,"data-cmd-arg"),e={styles:this.o[Mt].data.styles?K(this.o[Mt].data.styles):[]},o=e.styles.indexOf(n);o>-1?(e.styles.splice(o,1),a(t.currentTarget,"actv")):(e.styles.push(n),i(t.currentTarget,"actv")),un(this.g,this.o,e)}))}}function xn(t,n,e,o){const i=Hn(t,n);i.shape={shapeEl:e,connectorKey:o},$n(t,i)}function bn(t,n,e){const o=Hn(t,n);o.shape.shapeEl!==Ln(t,o)&&Cn(t,o),o.shape=null,o.data={dir:o.data.dir,position:e}}function kn(t,n){const e=t[Mt].data,o=o=>{const i=e[o],a=n[o];i.shape?null==a.shape||i.shape.shapeEl===a.shape.shapeEl&&i.shape.connectorKey===a.shape.connectorKey?a.data&&bn(t,o,a.data.position):function(t,n,e,o){const i=Hn(t,n);if(i.shape.shapeEl===e&&i.shape.connectorKey===o)return;i.shape.shapeEl!==e&&i.shape.shapeEl!==Ln(t,i)&&Cn(t,i);i.shape={shapeEl:e,connectorKey:o},$n(t,i)}(t,o,a.shape.shapeEl,a.shape.connectorKey):null!=a.shape?xn(t,o,a.shape.shapeEl,a.shape.connectorKey):U(i.data.position,a.data.position)};o("s"),o("e")}function $n(t,n){n.data=Mn(n.shape).pathAdd(n.shape.connectorKey,t)}customElements.define("ap-path-settings",yn);const Cn=(t,n)=>Mn(n.shape)?.pathDel(t),Mn=t=>t?.shapeEl[Ht],Hn=(t,n)=>t[Mt].data[n],Ln=(t,n)=>(t[Mt].data.e===n?t[Mt].data.s:t[Mt].data.e).shape?.shapeEl;function Vn(n,e){const o=f("g",'');o.id=e.id,i(o,"shpath"),e.s.el=t(o,"start"),e.e.el=t(o,"end"),e.styles=e.styles??["d-arw-e"];const s=Tn(o,"path","outer","selected");function c(){if(!e.s.shape||!e.e.shape){const t=function(t,n){const e=Math.atan2(n.y-t.y,n.x-t.x);return An(e,-.8,.8)?"left":An(e,.8,2.4)?"top":An(e,2.4,3.2)||An(e,-3.2,-2.4)?"right":"bottom"}(e.s.data.position,e.e.data.position);e.e.shape||(e.e.data.dir=t),e.s.shape||(e.s.data.dir=En(t))}const t=function(t){let n=.5*Math.hypot(t.s.data.position.x-t.e.data.position.x,t.s.data.position.y-t.e.data.position.y);function e(t){return"right"===t.dir||"left"===t.dir?"right"===t.dir?t.position.x+n:t.position.x-n:t.position.x}function o(t){return"right"===t.dir||"left"===t.dir?t.position.y:"bottom"===t.dir?t.position.y+n:t.position.y-n}n=n>70?70:n<15?15:n;const i=(n,e,o,i)=>t.styles.includes(o)?n.position[e]+(i[n.dir]??0):n.position[e],a=(t,n)=>i(t,"x",n,{left:-6,right:6}),s=(t,n)=>i(t,"y",n,{top:-6,bottom:6});return`M ${a(t.s.data,"d-arw-s")} ${s(t.s.data,"d-arw-s")} C ${e(t.s.data)} ${o(t.s.data)}, ${e(t.e.data)} ${o(t.e.data)}, ${a(t.e.data,"d-arw-e")} ${s(t.e.data,"d-arw-e")}`}(e);s.forEach((n=>n.setAttribute("d",t))),Zn(e.s),Zn(e.e)}let r,l;let d,u,p=0;const m=()=>{u&&(u(),u=null)},g=t=>{m(),u=V(t)};function v(t){switch(p){case 0:y();break;case 1:p=2,d=H(o,(t=>w(!0))),g((()=>{w(!0),y()})),function(t){r||(r=dn(new yn(n,o),t))}(t)}}function w(t){t||(l=null),p=0,a(o,"select","highlight-s","highlight-e"),Bn(e.s,1),Bn(e.e,1),r?.del(),r=null,d&&(d(),d=null),m()}function y(){p=1,i(o,"s"===l?"highlight-s":"e"===l?"highlight-e":"select"),Bn(e.s,2),Bn(e.e,2),vn(n,o),g((()=>{w(!0)}))}return o[Mt]={data:e,drawPosition:c,click:(t,n)=>{if(0===p||l){const o=n||t?Rn(e,n??kt(t)):null;Pn(l,o)||w(),l=o}v(t)},patch:t=>{e.styles&&(e.styles=t.styles,mn(o)),t.s&&kn(o,t),c()},moveCapture:(t,i,a)=>{w(!0);const s=a?null:Zt([o],!1,!0),r=t=>{w(!0),s?It(n,s,Zt([o],!1,!0)):Ot(n,[e.id],Zt([o],!1,!0)),h(t)||v()},l=Rn(e,i);l?Sn(n,o,c,t,l,r):function(t,n,e,o,i){M(n,o,(o=>{!function(t,n,e,o){const i=n=>G(n,t.scale,o);zn(n.s,i),n.s.shape?.shapeEl&&n.s.shape?.shapeEl===n.e.shape?.shapeEl||zn(n.e,i);n.s.shape||n.e.shape||e()}(t[W].data,n[Mt].data,e,o)}),(o=>{!function(t,n,e){const o=n=>nt(n,t.cell);zn(n.s,o),zn(n.e,o),n.s.shape&&n.e.shape||e()}(t[W].data,n[Mt].data,e),i(o)}))}(n,o,c,t,r)},unselect:w,del:function(){w(),Cn(o,e.s),Cn(o,e.e),o.remove()}},e.styles&&i(o,...e.styles),e.s.shape&&$n(o,e.s),e.e.shape&&$n(o,e.e),c(),o}function zn(t,n){t.shape?(n(Mn(t.shape).data.position),Mn(t.shape).drawPosition()):n(t.data.position)}const Sn=(t,n,e,o,l,d)=>{n[Mt].data[l].shape&&bn(n,l,tt(t[W].data,o)),n.style.pointerEvents="none";const u=function(t){let n=null;function e(t){const e=$t(t);if(n!==e){s(e,"hovertrack")&&i(e,"hover");let t=!1;s(e?.parentElement,"hovertrack")&&(i(e.parentElement,"hover"),t=!0),a(n,"hover"),n?.parentElement===e?.parentElement&&t||a(n?.parentElement,"hover"),n=e}}return c(t,"pointermove",e),function(){r(t,"pointermove",e),a(n,"hover"),a(n?.parentElement,"hover"),n=null}}(n.parentElement);M(n[Mt].data[l].el,o,(o=>{G(n[Mt].data[l].data.position,t[W].data.scale,o),e()}),(o=>{const i=$t(o),a=i?.getAttribute("data-connect");a?xn(n,l,i.parentElement,a):nt(n[Mt].data[l].data.position,t[W].data.cell),e(),u(),n.style.pointerEvents="unset",d(o)}))};function Zn(t){t.el.style.transform=`translate(${t.data.position.x}px, ${t.data.position.y}px) rotate(${Dn(t.data.dir)}deg)`}function Bn(t,n){t.el.firstElementChild.setAttribute("data-evt-index",n.toString())}const Dn=t=>"right"===t?180:"left"===t?0:"bottom"===t?270:90,En=t=>"left"===t?"right":"right"===t?"left":"top"===t?"bottom":"top";const Tn=(n,...e)=>e.map((e=>t(n,e))),An=(t,n,e)=>n<=t&&t<=e,Rn=(t,n)=>t.e.el.contains(n)?"e":t.s.el.contains(n)?"s":null,Pn=(t,n)=>!t||t===n,Un=(t,n,e)=>Nn(t,n,e,(t=>function(t){return t.split(" ")?.forEach((n=>{(n.startsWith("http://")||n.startsWith("https://"))&&(t=t.replace(n,`${new URL(n).hostname}`))})),t}(jn(t)))),On=(t,n,e)=>Nn(t,n,e,jn);function Nn(t,n,e,o){const i=function(t,n,e){let o=0;return{s:t.split("\n").map(((t,i)=>(o=i,`${0===t.length?".":e(t)}`))).join(""),c:o}}(e||"",t.x?.baseVal[0]?.value??0,o);t.innerHTML=i.s,null!=n&&t.y.baseVal[0].newValueSpecifiedUnits(t.y.baseVal[0].SVG_LENGTHTYPE_EMS,i.c>0?n-i.c/2:n)}function jn(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'").replaceAll(" "," ")}const In=(t,n)=>ln(n,Kn(t,n)),Fn=(t,n,e)=>dn(Kn(t,n),e);function Kn(t,n){const e=new Yn;return c(e,"cmd",(e=>{switch(e.detail.cmd){case"style":gn(t,n,e.detail.arg);break;case"del":Wt(t,[n]);break;case"copy":Xt(t,[n])}})),e}class Yn extends HTMLElement{connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${an} ${on}
`;{const n=t.getElementById("pnl");let e;l(t,"[data-toggle]",(o=>{const i=t=>{const e=ct(this);e.style.top=`${e.getBoundingClientRect().top+window.scrollY+t*n.getBoundingClientRect().height}px`};e&&(i(1),_n(e,!1));const a=t.getElementById(d(o,"data-toggle"));e!==a?(_n(a,!0),i(-1),e=a):e=null}))}l(t,"[data-cmd]",(t=>{this.dispatchEvent(new CustomEvent("cmd",{detail:{cmd:d(t,"data-cmd"),arg:d(t,"data-cmd-arg")}}))}))}}function _n(t,n){t.style.display=n?"unset":"none"}function Xn(n,e,o,i,a,s,c,r,l=0,d){const u=qn(e.id,o,i),h={el:t(u,"text"),vMid:l};d??=Un;const p=()=>d(h.el,h.vMid,e.title);p();return{el:u,cons:i,draw:Jn(n,u,e,i,h,r??In,(t=>c(h.el,t)),a,s,null).draw,drawTxt:p}}function Gn(t,n,e,o,i,a,s,c,r){const l=qn(n.id,e,o);return{el:l,cons:o,draw:Jn(t,l,n,o,null,c??Fn,null,i,a,s,r).draw}}customElements.define("ap-shape-edit",Yn);const qn=(t,n,e)=>{const o=f("g",`${n}\n\t${Object.entries(e).map((t=>``)).join()}`);return o.id=t,o};function Jn(t,n,e,s,c,r,l,d,u,p,m){let g,v,w,y;e.styles??=[];const x=()=>Zt([n],!1,!1);function b(t){e.title=t,l()}const k=function(t,n,e,s,c,r,l,d,u,p){i(n,"hovertrack");const m=K(s),g=new Set;let v,w,y,x=0;function b(){v?.remove(),v=null}const k=()=>{y&&(y(),y=null)},$=t=>{k(),y=V(t)};function C(){n.style.transform=`translate(${e.position.x}px, ${e.position.y}px)`;for(const t in s)m[t].position={x:s[t].position.x+e.position.x,y:s[t].position.y+e.position.y};for(const t of g)t[Mt].drawPosition()}function L(){x=1,i(n,"select"),l&&(v=function(t,n){const e=f("circle");return e.setAttribute("data-evt-index","2"),i(e,"resizer"),o(e,n),t.append(e),e}(n,l())),$((()=>z()))}function z(t){r(),x=0,a(n,"select","highlight"),t||b(),w&&(w(),w=null),k()}const S=t=>{switch(x){case 0:L();break;case 1:x=2,a(n,"select"),i(n,"highlight"),b(),w=H(n,(t=>z())),$((()=>{z(),L()}))}c(t,x)},Z=(e,o,i)=>{if(o===v){z(!0);const o=Zt([n],!1,!1);return void M(v,e,d,(e=>{u&&u(),b(),L(),It(t,o,Zt([n],!1,!1))}))}z();const a=o.getAttribute("data-connect");if(a){const o=Vn(t,{id:I(),type:0,s:{shape:{shapeEl:n,connectorKey:a}},e:{data:{dir:En(m[a].dir),position:tt(t[W].data,e)}}});return n.parentNode.append(o),t[W].select(h(e)?null:[o]),o[Mt].moveCapture(e,o[Mt].data.e.el,!0),void g.add(o)}B(e,i)};p??=()=>nt(e.position,t[W].data.cell);const B=(o,i)=>{const a=i?null:Zt([n],!1,!1);M(n,o,(n=>{G(e.position,t[W].data.scale,n),C()}),(o=>{p(),C(),a?It(t,a,Zt([n],!1,!1)):Ot(t,[e.id],Zt([n],!1)),h(o)||L()}))};return n[Ht]={data:e,drawPosition:C,click:S,moveCapture:Z,unselect:z,del:function(){z();for(const t of g)t[Mt].del();n.remove()},paths:g,pathAdd:function(t,n){return g.add(n),m[t]},pathDel:function(t){g.delete(t)}},C}(t,n,e,s,((o,i)=>{1===i&&vn(t,n),2===i&&(c&&!g&&(w=e.title,v=x(),On(c.el,c.vMid,e.title),l(),g=function(t,n,e,o,i){let a=f("foreignObject");const s=document.createElement("textarea"),c=()=>function(t,n,e,o,i){const a=t.getBBox(),s=a.width+20;n.width.baseVal.value=s+2*o+2,n.x.baseVal.value=a.x-o-("center"===i?10:"right"===i?20:0),n.height.baseVal.value=a.height+2*o+3,n.y.baseVal.value=a.y-o,e.style.width=`${s}px`,e.style.height=`${a.height}px`}(t,a,s,l,r.textAlign);s.value=e||"",s.oninput=function(){On(t,n,s.value),o(s.value),c()},i&&(s.onblur=function(){i(s.value)}),s.onpointerdown=function(t){t.stopImmediatePropagation()},a.appendChild(s),t.parentElement.appendChild(a);const r=getComputedStyle(s),l=parseInt(r.paddingLeft)+parseInt(r.borderWidth);return c(),s.focus(),{dispose:()=>{a.remove(),a=null},draw:c}}(c.el,c.vMid,e.title,b)),y||(y=r(t,n,o)))}),(function(){g&&(Un(c.el,c.vMid,e.title),l(!0),g?.dispose(),g=null),y?.del(),y=null,v&&w!==e.title&&It(t,v,x()),v=null,w=null}),d,u,p,m);return e.styles&&i(n,...e.styles),{draw:()=>{k(),y?.updPos&&y?.updPos(),g?.draw()}}}function Wn(n,e,o){t(n,e).r.baseVal.value=o}const Qn=(t,n)=>function(t,n,e){const o=v(t);return B(n,e,Math.sqrt(o.x**2+o.y**2))}(t,n,24);function te(n,e){e.w??=96,e.h??=48;const a={w:e.w,h:e.h};let s;const c=Xn(n,e,'\n\t\t \n\t\t  ',{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:24}},top:{dir:"top",position:{x:0,y:-24}}},(()=>{const t=function(t){const n=t.w/2,e=t.h/2;return{t:{x:t.position.x-n,y:t.position.y-e},b:{x:n,y:e}}}(e);return s=t.t,t.b}),(i=>{let l;(function(t,n,e,i,a){const s=R(tt(t[W].data,i),n,-1),c={};let r=!1;function l(t){s[t]<48&&(s[t]=48);const o=48*Math.round(s[t]/48),i="x"===t?"w":"h";e[i]!==o&&(!a||a()[i]<=o)&&(e[i]=o,e.position[t]=n[t]+o/2,r=!0),c[t]=s[t]-e[i]/2}return l("x"),l("y"),o(i.target,c),r})(n,s,e,i,(()=>(l??=oe(t(c.el,"text")),l)))&&(ee(a,e),r())}),(t=>{const n=oe(t,a);ne(e,n)||(ee(e,n),r())}));function r(){!function(t,n){const e=n.w/-2,o=n.h/-2;t.cons.right.position.x=-e,t.cons.left.position.x=e,t.cons.bottom.position.y=-o,t.cons.top.position.y=o,rn(t),ie(t.el,"main",n.w,n.h,e,o),ie(t.el,"outer",n.w+48,n.h+48,e-24,o-24)}(c,e),c.draw()}return i(c.el,"shrect"),96!==e.w||48!==e.h?r():c.draw(),c.el[Ht].patch=t=>{const n=hn(c,t),o=(null!=t.w||null!=t.h)&&!ne(e,t);o&&ee(e,t),o?r():n&&c.draw()},c.el}const ne=(t,n)=>t.w===n.w&&t.h===n.h,ee=(t,n)=>{t.h=n.h,t.w=n.w};function oe(t,n){const e=t.getBBox();return{w:B(n?.w,48,e.width),h:B(n?.h,48,e.height)}}const ie=(n,e,o,i,a,s)=>{const c=t(n,e);c.width.baseVal.value=o,c.height.baseVal.value=i,c.x.baseVal.value=a,c.y.baseVal.value=s};function ae(t,n){let e;n.w??=96,n.h??=96;const a=Gn(t,n,'\n\t\t \n\t\t ',{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:48}},top:{dir:"top",position:{x:0,y:-48}}},(()=>{const t=se(n);return e=t.tCanvas,t.bInner}),(i=>s(function(t,n,e,i){const a=R(tt(t[W].data,i),n,-1);e.w=a.x<48?48:a.x;const s=e.w/2;e.position.x=n.x+s,e.h=a.y<48?48:a.y;const c=e.h/2;return e.position.y=n.y+c,o(i.target,{x:s,y:c}),{x:-s,y:-c}}(t,e,n,i))),(t=>s(function(t,n){const e=t=>24*Math.round(t/24);t.w=e(t.w),t.h=e(t.h);const o=se(t);return t.position.x=n.x-o.tInner.x,t.position.y=n.y-o.tInner.y,o.tInner}(n,e))));i(a.el,"shbox");const s=t=>{a.cons.right.position.x=n.w+t.x,a.cons.left.position.x=t.x,a.cons.bottom.position.y=n.h+t.y,a.cons.top.position.y=t.y,rn(a),ie(a.el,"main",n.w,n.h,t.x,t.y),ie(a.el,"selected",n.w,n.h,t.x,t.y),ie(a.el,"outer",n.w+48,n.h+48,t.x-24,t.y-24),a.draw()};return 96!==n.w||96!==n.h?s(se(n).tInner):a.draw(),a.el[Ht].patch=t=>{const e=hn(a,t),o=(null!=t.w||null!=t.h)&&!ne(n,t);o&&ee(n,t),o?s(se(n).tInner):e&&a.draw()},a.el}function se(t){const n=ce(t.w),e=ce(t.h);return{tCanvas:{x:t.position.x-n,y:t.position.y-e},tInner:{x:-n,y:-e},bInner:{x:t.w-n,y:t.h-e}}}const ce=t=>24*Math.floor(t/2/24);function re(t,n){return ue?async function(t,n){try{const e=await window.showSaveFilePicker({suggestedName:n,types:[he]}),o=await e.createWritable();return await o.write(t),await o.close(),e}catch(t){"AbortError"!==t.name&&alert("File not saved")}}(t,n):function(t,n){const e=document.createElement("a");e.download=n,e.href=URL.createObjectURL(t),e.click(),URL.revokeObjectURL(e.href),e.remove()}(t,n)}async function le(t){const n="AbortError";try{return de?await async function(t){const[n]=await window.showOpenFilePicker({types:[t]});return{h:n,f:await n.getFile()}}(t):await new Promise(((e,o)=>{!function(t,n,e){const o=document.createElement("input");o.type="file",o.multiple=!1,o.accept=t,c(o,"cancel",(t=>e(t)),!0),c(o,"change",(async()=>n(o.files?.length?o.files[0]:null)),!0),o.click(),o.remove()}(t.inputAccept,(t=>{e({f:t})}),(t=>o(Error(n))))}))}catch(t){if(t.name===n||t.message===n)return null;throw t}}const de="showOpenFilePicker"in window,ue="showSaveFilePicker"in window,he={accept:{"image/png":[".png"]},inputAccept:"image/png"},pe={accept:{"image/*":[".png",".gif",".jpeg",".jpg"]},inputAccept:"image/*"};function fe(t,n,e){const o=m(`
${an} ${on}
`);return l(o,"[data-cmd]",(e=>{switch(d(e,"data-cmd")){case"del":Wt(t,[n]);break;case"copy":Xt(t,[n])}})),dn(o,e)}function me(t,n,e,o){ge(n,(n=>t[W].select(Gt(t,Bt(n),!0,e,o))))}function ge(t,n){!function(t,n,e){const o=new Image;o.onload=function(){const i=E(n,o.width,o.height);if(!i.c){URL.revokeObjectURL(o.src);const n=new FileReader;return n.onloadend=function(){e(n.result,i.w,i.h)},void n.readAsDataURL(t)}const a=document.createElement("canvas");a.width=i.w,a.height=i.h;const s=a.getContext("2d");s.imageSmoothingEnabled=!1,s.imageSmoothingQuality="high",s.drawImage(o,0,0,i.w,i.h),URL.revokeObjectURL(o.src),e(a.toDataURL(),i.w,i.h)},o.src=URL.createObjectURL(t)}(t,4096,((t,e,o)=>{const i=E(Math.min(document.body.clientHeight/2,264),e,o),a={id:I(),type:6,position:{x:0,y:0},w:i.w,h:i.h,r:D(o/e),d:t};we(a),n(a)}))}function ve(t,n){let e;const a=Gn(t,n,`\n\t\t \n\t\t `,{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:48}},top:{dir:"top",position:{x:0,y:-24}}},(()=>{const t=se(n);return e=t.tCanvas,t.bInner}),(i=>s(function(t,n,e,i){const a=R(tt(t[W].data,i),n,-1);e.w=a.x<48?48:a.x;const s=e.w/2;e.position.x=n.x+s,e.h=e.r*e.w;const c=e.h/2;return e.position.y=n.y+c,o(i.target,{x:s,y:c}),{x:-s,y:-c}}(t,e,n,i))),(t=>s(function(t,n){we(t);const e=se(t);return t.position.x=n.x-e.tInner.x,t.position.y=n.y-e.tInner.y,e.tInner}(n,e))),fe);i(a.el,"shimg");const s=t=>{a.cons.right.position.x=n.w+t.x,a.cons.left.position.x=t.x,a.cons.bottom.position.y=n.h+t.y,a.cons.top.position.y=t.y,rn(a),ie(a.el,"main",n.w,n.h,t.x,t.y),ie(a.el,"selected",n.w,n.h,t.x,t.y),ie(a.el,"outer",n.w+48,n.h+48,t.x-24,t.y-24),a.draw()};return s(se(n).tInner),a.el[Ht].patch=t=>{const e=pn(a,t),o=(null!=t.w||null!=t.h)&&!ne(n,t);o&&ee(n,t),o?s(se(n).tInner):e&&a.draw()},a.el}function we(t){t.w=24*Math.round(t.w/24),t.h=D(t.r*t.w)}const ye=(t,n)=>ln(n,new xe(t,n));class xe extends HTMLElement{constructor(t,n){super(),this.$=n,this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='
';const n=this.$[Ht].data,e=t.getElementById("edit");c(e,"cmd",(t=>{switch(t.detail.cmd){case"style":gn(this.g,this.$,t.detail.arg);break;case"del":Wt(this.g,[this.$]);break;case"copy":Xt(this.g,[this.$])}}));const o=t=>un(this.g,this.$,t);i(e,`ta-${n.a}`),l(t,'[data-cmd="a"]',(t=>{const s=Number.parseInt(d(t,"data-cmd-arg"));s!==n.a&&(a(e,`ta-${n.a}`),i(e,`ta-${s}`),o({a:s}))})),["d-h","d-b"].forEach((t=>{n.styles?.includes(t)&&i(e,t)})),l(t,'[data-cmd="s"]',(t=>{const s=d(t,"data-cmd-arg"),c={styles:n.styles?K(n.styles):[]};c.styles?.includes(s)?(!function(t,n){const e=t.indexOf(n);e>-1&&t.splice(e,1)}(c.styles,s),a(e,s)):(c.styles??=[],c.styles.push(s),i(e,s)),o(c)}))}}function be(n,e){e.w??=48,e.h??=48,e.a??=1;const o=Xn(n,e,`\n\t\t \n\t\t  `,{right:{dir:"right",position:{x:24,y:0}},left:{dir:"left",position:{x:-24,y:0}},bottom:{dir:"bottom",position:{x:0,y:24}},top:{dir:"top",position:{x:0,y:-24}}},null,null,((t,n)=>s(t,n)),ye,null,Ve);i(o.el,"shtxt"),i(o.el,`ta-${e.a}`);const a=()=>t(o.el,"text"),s=(t,n)=>{n&&He(t)&&t.append(Le());const o=ke(t);ne(e,o)||(ee(e,o),u())};let c,r,l;function d(){r=e.w,c=e.h,l=e.a}function u(t){if(function(t,n){const e=n.w/-2,o=-ce(n.h);t.cons.left.position.y=o+24,t.cons.left.position.x=e,t.cons.top.position.y=o,t.cons.bottom.position.y=n.h+o,t.cons.right.position.y=o+24,t.cons.right.position.x=-e,rn(t),ie(t.el,"main",n.w,n.h,e,o),ie(t.el,"outer",n.w+48,n.h+48,e-24,o-24)}(o,e),t||l!==e.a||r!==e.w){const t=e.w/-2;let n,i;switch(e.a){case 1:n=t+8,i=(e.w-r)/2;break;case 2:n=0,i=0;break;case 3:n=-t-8,i=(e.w-r)/-2}const s=a();s.x.baseVal[0].value=n,s.querySelectorAll("tspan[x]").forEach((t=>{t.x.baseVal[0].value=n})),e.position.x+=i,l!==e.a&&(Me(o.el,l,e.a),l=e.a),r=e.w}c!==e.h&&(a().y.baseVal[0].value=Ce(e.h),e.position.y+=ce(e.h)-ce(c),c=e.h),o.draw()}return d(),48!==e.w||48!==e.h?u(!0):o.draw(),o.el[Ht].patch=t=>{const n=t.position&&!j(e.position,t.position);n&&U(e.position,t.position);let i=!1;t.styles&&(e.styles=t.styles,mn(o.el),i=!0),null!=t.title&&e.title!==t.title&&(e.title=t.title,o.drawTxt(),i=!0);let s=!1;if(null==t.w&&i){const t=ke(a());ne(e,t)||(ee(e,t),s=!0)}null==t.w||ne(e,t)||(ee(e,t),d(),s=!0),null!=t.a&&e.a!==t.a&&(Me(o.el,e.a,t.a),e.a=t.a,l=e.a,s=!0),s?(a().y.baseVal[0].value=Ce(e.h),u(!0)):n&&o.draw()},o.el}function ke(t){const n=t.getBBox();return{w:B(0,48,n.width+12),h:B(0,24,n.height+14)}}customElements.define("ap-rect-txt-settings",xe);const $e=t=>1===t.a?-16:2===t.a?0:16,Ce=t=>24-ce(t);function Me(t,n,e){a(t,`ta-${n}`),i(t,`ta-${e}`)}const He=t=>[...t.childNodes].every((t=>![...t.childNodes].some((t=>t.nodeType===Node.TEXT_NODE&&t.textContent?.trim())))),Le=()=>{const t=f("tspan"," ➜");return t.setAttribute("fill","rgb(13, 110, 253)"),t},Ve=(t,n,e)=>{Un(t,n,e),He(t)&&t.append(Le())};function ze(n,e,o){t(n,e).setAttribute("d",`M${o.l.x} ${o.l.y} L${o.t.x} ${o.t.y} L${o.r.x} ${o.r.y} L${o.b.x} ${o.b.y} Z`)}function Se(t,n){const e=t/2,o=n-e,i=e-n;return{l:{x:o,y:0},t:{x:0,y:o},r:{x:i,y:0},b:{x:0,y:i}}}const Ze=(t,n)=>B(n,48,function(t){const n=v(t);return 2*(Math.abs(n.x)+Math.abs(n.y))}(t)-20);async function Be(t,n,e){return function(t,n,e){let o,i;const a=De(t,n);if(a)o=new DataView(t,0,a.byteOffset-8),i=new DataView(t,a.byteOffset+a.byteLength+4);else{const n=t.byteLength-12;o=new DataView(t,0,n),i=new DataView(t,n)}const s=new DataView(new ArrayBuffer(8));return s.setUint32(0,e.length),s.setUint32(4,n),new Blob([o,s,e,new Uint32Array([0]),i],{type:"image/png"})}(await t.arrayBuffer(),Ee(n),e)}function De(t,n){const e=new DataView(t,8);let o,i=0,a=e.getUint32(4);for(;1229278788!==a;){if(o=e.getUint32(i),a===n)return new DataView(t,i+16,o);i=i+12+o,a=e.getUint32(i+4)}return null}function Ee(t){return new DataView((new TextEncoder).encode(t).buffer).getUint32(0)}const Te=()=>"undefined"!=typeof navigator&&(/iPad|iPhone|iPod/.test(navigator.userAgent||"")||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1),Ae=(t,n,e)=>Re(t,Y(n),null,e);function Re(t,n,e,o){const i=t.ownerSVGElement.cloneNode(!0),a=i.querySelector("#canvas");let c;i.style.backgroundImage=null,e?(c=y(Ne(t,Pe)),T(a.children,(t=>((t,n)=>n.some((n=>s(t,n))))(t,Pe)?(Oe(t),!0):(t.remove(),!1)))):(c=t.getBoundingClientRect(),Ne(a,Ue).forEach((t=>Oe(t))));const r=i.getElementsByTagName("foreignObject");for(;r[0];)r[0].parentNode.removeChild(r[0]);const l=t[W].data,d=1/l.scale;a.style.transform=`matrix(1, 0, 0, 1, ${d*(l.position.x+15*l.scale-c.x)}, ${d*(l.position.y+15*l.scale-c.y)})`,function(t,n,e,o){const i=E(Te()?4096:10836,n.width*e*window.devicePixelRatio,n.height*e*window.devicePixelRatio),a=new Image;a.width=n.width,a.height=n.height,a.onload=function(){const t=document.createElement("canvas");t.width=i.w,t.height=i.h,t.style.width=`${i.w}px`,t.style.height=`${i.h}px`;const e=t.getContext("2d");e.imageSmoothingEnabled=!1,e.drawImage(a,n.x,n.y,n.width,n.height,0,0,i.w,i.h),URL.revokeObjectURL(a.src),t.toBlob(o,"image/png")},t.width.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,a.width),t.height.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,a.height),a.src=URL.createObjectURL(new Blob([(new XMLSerializer).serializeToString(t)],{type:"image/svg+xml;charset=utf-8"}))}(i,{x:0,y:0,height:c.height/l.scale+30,width:c.width/l.scale+30},3,(async t=>o(n?await Be(t,"dgRm",(new TextEncoder).encode(n)):t)))}const Pe=["select","highlight"],Ue=["select","highlight","highlight-e","highlight-s"],Oe=t=>{a(t,...Ue),t.querySelector(".resizer")?.remove()},Ne=(t,n)=>t.querySelectorAll(`.${n.join(", .")}`);async function je(t){const n=await async function(t,n){return De(await t.arrayBuffer(),Ee(n))}(t,"dgRm");return n?(new TextDecoder).decode(n):null}const Ie=navigator.clipboard.readText;async function Fe(t,n){const e=`web text/dgrm${Y(Zt(n,!0))}`;await async function(t){try{await navigator.clipboard.writeText(t)}catch(t){alert(t)}}(e),window.ClipboardItem&&Re(t,null,!0,(async t=>{try{await navigator.clipboard.write([new window.ClipboardItem({[t.type]:Promise.resolve(t),"text/plain":Promise.resolve(new Blob([e],{type:"text/plain"}))})])}catch{}}))}const Ke=t=>{if(!t?.startsWith("web text/dgrm"))return null;try{return _(t.slice("web text/dgrm".length))}catch{return null}};function Ye(t,n,e){const o=function(t,n){async function e(e){if(document.activeElement===t.ownerSVGElement){const o=n();if(o?.length)return e.preventDefault(),Fe(t,o),o}return null}async function o(n){const o=await e(n);o&&_e(t,o)}return document.addEventListener("copy",e),document.addEventListener("cut",o),function(){r(document,"copy",e),r(document,"cut",o)}}(t,n);function i(o){if(u(o)||"Delete"!==o.key&&"Backspace"!==o.key)"Escape"===o.key&&e();else{const e=n();e?.length&&_e(t,e)}}return c(document,"keydown",i),function(){o(),r(document,"keydown",i)}}function _e(t,n){t[W].select(null),Wt(t,n)}const Xe=()=>{if(!document.cookie.includes("dgrma=1"))return null;const t=_(localStorage.getItem("dgrm"));return t&&new Date(t.e)>new Date?t:null},Ge=(t,n,e,o,i)=>{n&&We(qe(),e,o,n.n,0===e?n.k:n.n,n.k,new Date,i)},qe=()=>`dgrm${Xe()?.l}`,Je=(t,n)=>oo(t,(t=>Qe(t.transaction("r","readonly").objectStore("r"),n))),We=(t,n,e,o,i,a,s,c)=>oo(t,(t=>{const r=t.transaction("r","readwrite").objectStore("r");Qe(r,(l=>{const d=l.find((t=>t.k===i));l.length>=5&&!d&&to(t,r,l[l.length-1]);const u=d?.i??a??xt();r.put({t:n,i:u,k:i,n:o,l:e,d:s}),c&&t.transaction("d","readwrite").objectStore("d").put({i:u,d:c})}))})),Qe=(t,n)=>{t.getAll().onsuccess=t=>{n((t.target.result??[]).sort(((t,n)=>n.d.getTime()-t.d.getTime())))}},to=(t,n,e,o)=>{let i=0;const a=()=>{1!==i?i++:o&&o()};n.delete(e.i).onsuccess=a,1===e.t?t.transaction("d","readwrite").objectStore("d").delete(e.i).onsuccess=a:a()},no=(t,n,e)=>oo(t,(t=>{let o=null,i=null;const a=(t,n)=>{t&&(o=t),n&&(i=n),o&&i&&(o.s=i.d,e(o))};t.transaction("r","readonly").objectStore("r").get(n).onsuccess=t=>{a(t.target.result,null)},t.transaction("d","readonly").objectStore("d").get(n).onsuccess=t=>{a(null,t.target.result)}})),eo=(t,n,e)=>oo(t,(t=>{const o=t.transaction("r","readwrite").objectStore("r");Qe(o,(i=>{const a=i.find((t=>t.k===n));a?to(t,o,a,e):e()}))})),oo=(t,n)=>{const e=indexedDB.open(t);e.onerror=t=>console.error(e.error),e.onupgradeneeded=t=>{const n=e.result;n.createObjectStore("d",{keyPath:"i"}),n.createObjectStore("r",{keyPath:"i"})},e.onsuccess=t=>{e.result.onerror=t=>console.error("indexedDB error",t),n(e.result)}},io=(t,n,e,o,i,a)=>{t[po]={k:n,n:e,e:o,m:i},lo(t,0,a,null)},ao=(t,n,e)=>{const o=F(n.name);t[po]={f:n,n:o},lo(t,1,null,e)},so=(t,n,e)=>{t[po]={n:n},lo(t,1,null,e)},co=t=>{t[po]=void 0,lo(t,null,null,null)},ro=t=>t[po],lo=(t,n,e,o)=>{const i=t[po];uo(i?.n),Ge(0,i,n,e,o)},uo=t=>{document.title=t??"DGRM - Flowchart editor"},ho=t=>t?.replace(/[/\\?%*:|"<>]/g,"-"),po=Symbol(0),fo=async(t,n)=>{const e=`${ro(t)?.n??"dgrm"}.png`;if(mo())return void await re(n,e);const o={files:[new File([n],e,{type:n.type})]};navigator.canShare&&navigator.canShare(o)?await navigator.share(o):await re(n,e)},mo=()=>screen.width>768;class go extends HTMLElement{constructor(t,n){super(),this.g=t,this.C=n}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${an}Create copy
Copy to clipboard
${mo()?sn+"Download":cn+"Send"} image
${on}Delete
`;const n=()=>ct(this)[pt].del();function e(e,o){c(t.getElementById(e),"click",(t=>{n(),o()}))}e("clone",(()=>Xt(this.g,this.C()))),e("copy",(async()=>await Fe(this.g,this.C()))),e("save",(()=>Re(this.g,Y(Zt(this.C(),!1)),!0,(async t=>fo(this.g,t))))),e("del",(()=>Wt(this.g,this.C())))}}customElements.define("ap-grp-settings",go);const vo=Symbol("p"),wo=Symbol(0);const yo=(t,n,e)=>{if(e[vo])return null;if("a"===e?.target?.tagName)return null;const o=kt(e),i=Po(o);if(!i)return zo(n),null;const a=Do(n,o),s=Ao(n);if(s&&a)return Uo(i,e),null;if(e.shiftKey||e.ctrlKey){if(s){const t=To(n);Oo(t),Ho(n,t)}return a?Lo(n,i):Ho(n,i),Zo(n,e),null}return a?dt(new go(t,(()=>So(n))),e.clientX-10,e.clientY-10):(zo(n),Ho(n,i),Zo(n,e),null)},xo=(t,n,e,o)=>{if(e[vo])return;if(t[wo]?.size)for(const n of t[wo])if(n(e,o))return;if(o===t.ownerSVGElement)return void t[W].moveCapture();const i=Do(n,o);if(!i&&!h(e))return void t[W].moveCapture();if(!i&&h(e))return zo(n),void No(Po(o),e,o);const a=Ao(n);i&&a?No(Po(o),e,o):i&&!a&&bo(t,n,e)},bo=(t,n,e)=>{const o=()=>ko(n),i=t=>$o(n,t),a=o();M(t.ownerSVGElement,e,(n=>i((e=>G(e,t[W].data.scale,n)))),(n=>{i((n=>nt(n,t[W].data.cell))),It(t,a,o())}))},ko=t=>Zt([...t.shapesToMove,...jo(t.pathEndsToMove)],!1,!0),$o=(t,n)=>{t.shapesToMove?.forEach((t=>{n(t[Ht].data.position),t[Ht].drawPosition()})),t.pathEndsToMove?.forEach((t=>{n(t.data.position),Io(t)[Mt].drawPosition()}))},Co=t=>{t[W].select(null),t[W].select(t.children)},Mo=()=>({shapes:new Set,paths:new Set,pathEnds:new Set,shapesToMove:new Set,pathEndsToMove:new Set}),Ho=(t,n)=>{if(n.shape)t.shapes.add(n.shape),i(n.shape,"highlight");else if(n.path)t.paths.add(n.path),i(n.path,"highlight"),a(n.path,"highlight-s","highlight-e"),t.pathEnds.delete(n.path[Mt].data.s),t.pathEnds.delete(n.path[Mt].data.e);else if(n.pathEnd){const e=Io(n.pathEnd);if(t.paths.has(e))return;t.pathEnds.add(n.pathEnd),i(e,Yo(n.pathEnd))}},Lo=(t,n)=>{if(n.shape)t.shapes.delete(n.shape),a(n.shape,"highlight");else if(n.path)t.paths.delete(n.path),t.pathEnds.delete(n.path[Mt].data.s),t.pathEnds.delete(n.path[Mt].data.e),a(n.path,"highlight","highlight-s","highlight-e");else if(n.pathEnd){const e=Io(n.pathEnd);if(t.paths.delete(e)){a(e,"highlight","highlight-s","highlight-e");const o=Ko(n.pathEnd);t.pathEnds.add(o),i(e,Yo(o))}else t.pathEnds.delete(n.pathEnd),a(e,Yo(n.pathEnd))}},Vo=t=>{const n=t=>a(t,"highlight","highlight-s","highlight-e");t.shapes.forEach((t=>n(t))),t.paths.forEach((t=>n(t))),t.pathEnds.forEach((t=>n(Io(t))))},zo=t=>{Vo(t);const n=To(t);n&&Oo(n),t.pathEnds.clear(),t.pathEndsToMove.clear(),t.paths.clear(),t.shapes.clear(),t.shapesToMove.clear()},So=t=>[...t.shapes,...t.paths,...jo(t.pathEnds)],Zo=(t,n)=>{Bo(t);const e=Eo(t);e&&(Vo(t),Uo(e,n))},Bo=t=>{t.shapesToMove=new Set(t.shapes),t.pathEndsToMove=new Set;const n=n=>{n.shape?t.shapesToMove.add(n.shape.shapeEl):t.pathEndsToMove.add(n)};t.paths?.forEach((t=>{n(t[Mt].data.s),n(t[Mt].data.e)})),t.pathEnds?.forEach(n)},Do=(t,n)=>[...t.shapes,...[...t.pathEnds].map((t=>t.el)),...t.paths].some((t=>t.contains(n))),Eo=t=>Ro(t)?To(t):{},To=t=>1!==t.shapes.size||t.paths.size||t.pathEnds.size?t.shapes.size||1!==t.paths.size||t.pathEnds.size?t.shapes.size||t.paths.size||1!==t.pathEnds.size?null:{pathEnd:A(t.pathEnds)}:{path:A(t.paths)}:{shape:A(t.shapes)},Ao=t=>!!To(t),Ro=t=>t.shapes.size||t.pathEnds.size||t.paths.size,Po=t=>{const n=t[Ht]?{shape:t}:t.parentElement[Ht]?{shape:t.parentElement}:t.parentElement.parentElement[Ht]?{shape:t.parentElement.parentElement}:null;if(n)return n;const e=t[Mt]?{path:t}:t.parentElement[Mt]?{path:t.parentElement}:null;if(e)return e;const o=Fo(t);return o?{pathEnd:o}:null},Uo=(t,n)=>{t.shape?t.shape[Ht].click(n):t.path?t.path[Mt].click(n):t.pathEnd&&Io(t.pathEnd)[Mt].click(n,t.pathEnd.el)},Oo=t=>{t.shape?t.shape[Ht].unselect():t.path?t.path[Mt].unselect():t.pathEnd&&Io(t.pathEnd)[Mt].unselect()},No=(t,n,e)=>{t.shape?t.shape[Ht].moveCapture(n,e):t.path?t.path[Mt].moveCapture(n,e):t.pathEnd&&Io(t.pathEnd)[Mt].moveCapture(n,e)},jo=t=>[...t].map(Io),Io=t=>t.el.parentNode,Fo=t=>{const n=t.hasAttribute("data-key")?t:t.parentElement.hasAttribute("data-key")?t.parentElement:null;if(!n)return null;const e=n.parentNode[Mt].data;return e.s.el===n?e.s:e.e},Ko=t=>{const n=Io(t)[Mt].data;return n.s===t?n.e:n.s},Yo=t=>Io(t)[Mt].data.s===t?"highlight-s":"highlight-e";function _o(t){const n=(n,e)=>(function(t,n,e){const o=f("path");o.style.pointerEvents="none",i(o,"mrkp"),t.append(o);const a=[],c=n=>a.push(Q(t[W].data,n.clientX,n.clientY)),r=new Set,l=t=>{s(t,"mrkt")&&r.add(t)};l(e),c(n),M(o,n,(t=>{c(t),o.setAttribute("d",Qo(a)),l(document.elementFromPoint(t.clientX,t.clientY))}),(n=>{let e=ti(y([o,...r]));o.remove(),e=ni(Q(t[W].data,e.x,e.y));const i={id:I(),type:7,position:e,d:""},s=[{position:{x:0,y:0},d:Qo(a)}],c=[];r.forEach((t=>{const n=t.parentNode;c.push(n),s.push(n[Ht].data)}));for(const t of s)Go(i,t);c.length?Qt(t,c,Bt(i)):qt(t,Bt(i),!0)}))}(t,n,e),!0);return((t,n)=>{t[wo]??=new Set,t[wo].add(n)})(t,n),()=>{((t,n)=>{t[wo]?.delete(n)})(t,n)}}const Xo=t=>``;function Go(t,n){const e=qo(n.d),o=N(n.position);R(o,t.position,-1);let i=n.d;for(const t of e){const n=N(t);ni(R(t,o)),i=Wo(i,n,t)}t.d+=i}const qo=t=>t.match(/M(.*?)l/g).map((t=>{const n=t.split(" ");return{x:Number.parseFloat(n[0].substring(1)),y:Number.parseFloat(n[1].substring(0,n[1].length-1))}})),Jo=t=>`M${t.x} ${t.y}l`,Wo=(t,n,e)=>t.replaceAll(Jo(n),Jo(e));function Qo(t,n=5,e=2){const o=t.length,i=o-n-1==0;let a="M",s=[t[0].x,t[0].y];const[c,r]=s.map((t=>+t.toFixed(e)));let l=`${a}${c} ${r}`,d=c,u=r;const[h,p]=[t[1].x,t[1].y],[f,m]=[(c+h)/2,(r+p)/2],[g,v]=[f-d,m-u].map((t=>+t.toFixed(e)));l+=`l${g} ${v}`,d+=g,u+=v;for(let i=1;i1&&(a="t",s=[w,y]),l+=`${a}${s.map((t=>+t.toFixed(e))).join(" ")} `,d+=w,u+=y}return i||(s=[t[o-1].x-d,t[o-1].y-u],l+=`l${s.map((t=>+t.toFixed(e))).join(" ")}`),l}const ti=t=>({x:Math.round(t.x+t.width/2),y:Math.round(t.y+t.height/2)}),ni=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t);const ei=()=>{const t=Xe()?.n;if(t)return t;const n=`User${Math.floor(100*Math.random())}`,e=prompt("Your name",n);return null==e?null:e?.substring(0,25)??n},oi=t=>document.getElementById("menu").bage(t),ii=t=>{const n=ai(t),e=document.createElement("div");return e.innerHTML=` ${t}`,e.style.cssText=`position: fixed; top:0; color:${n}; pointer-events: none;`,e.style.transform="translate(-9999px, 0px)",g(e),e},ai=t=>`hsl(${[...t].reduce(((t,n)=>n.charCodeAt(0)+((t<<5)-t)),0)%360}, 97%, 47%)`,si=(t,n,e,o)=>{const i=((t,n,e)=>({x:t.position.x+n*t.scale,y:t.position.y+e*t.scale}))(t[W].data,e,o);n.style.transform=`translate(${i.x}px, ${i.y}px)`},ci=(t,n,e)=>{const o=t=>e(Y(t)),i=c(t,"dgrmc",(t=>o([4,t.detail.shapesToCreateData]))),a=c(t,"dgrmu",(t=>o([4,t.detail.shapesToUpdData]))),s=c(t,"dgrmd",(t=>o([5,t.detail.delIds]))),l=c(t,"dgrmdc",(t=>{o([5,t.detail.delIds]),o([4,t.detail.shapesToCreateData])}));let d=!0;const u=c(t.ownerSVGElement,"pointermove",Z((e=>{if(d){const i=Q(t[W].data,e.clientX,e.clientY);o([6,n,i.x,i.y])}}),60));return()=>{d=!1,r(t,"dgrmc",i),r(t,"dgrmu",a),r(t,"dgrmd",s),r(t,"dgrmdc",l),r(t.ownerSVGElement,"pointermove",u)}},ri=(t,n)=>{Et(t,n),S(t,new CustomEvent(di,{detail:n,cancelable:!1}))},li=(t,n)=>{fn(n),S(t,new CustomEvent(ui,{detail:n,cancelable:!1}))},di="dgrmcu",ui="dgrmcd";function hi(t,n){Dt(t,n),function(t){const n=t.getBoundingClientRect(),e=window.innerWidth/2,o=window.innerHeight/2;t[W].move(e-n.width/2-n.x,o-n.height/2-n.y,1);const i=t=>t>1?1:Math.max(t,.25),a=Math.min(i(window.innerWidth/n.width),i(window.innerHeight/n.height));a<1&&t[W].scale(a,{x:e,y:o})}(t)}function pi(t){return!!gi(t)&&(t.c[t.i].u(),t.i-=1,!0)}function fi(t){const n=mi(t);return null!==n&&(t.i=n,t.c[n].r(),!0)}const mi=t=>{if(!t)return null;const n=t.i+1;return n<=t.c.length-1?n:null},gi=t=>t&&t.i>=0,vi=(t,n,e)=>$i(t,(()=>{bi(t,jt(n,e)),Et(t,e),fn(n)}),(()=>{bi(t,Nt(n,e)),Et(t,e)})),wi=(t,n,e)=>$i(t,(()=>{bi(t,Nt(n,e)),Et(t,e)}),(()=>{bi(t,jt(n,e)),fn(n)})),yi=(t,n,e)=>$i(t,(()=>{bi(t,Ft(e,n)),Et(t,n)}),(()=>{bi(t,Ft(n,e)),Et(t,e)})),xi=(t,n,e,o,i)=>$i(t,(()=>{bi(t,Nt(n,e)),Et(t,e),bi(t,jt(o,i)),Et(t,i),fn(o)}),(()=>{bi(t,jt(n,e)),fn(n),bi(t,Nt(o,i)),Et(t,i)})),bi=(t,n)=>{n[ki]=!0,S(t,n)},ki=Symbol(0);function $i(t,n,e){t[zi]??={c:[],i:-1},function(t,n,e){15===t.c.length&&(t.c.splice(0,1),t.i--),t.c.splice(t.i+1),t.i=t.c.push({u:n,r:e})-1}(t[zi],n,e),Li(t)}function Ci(t,n){n(t[zi])&&(t[W].select(null),Li(t))}const Mi=t=>Ci(t,pi),Hi=t=>Ci(t,fi);const Li=t=>{return S(t,z(Vi,(n=t[zi],{u:gi(n),r:null!==mi(n)})));var n},Vi="dgrmh",zi=Symbol("h");function Si(t){S(t,z("dgrmn",null,!0))&&(!function(t){for(t[W].select(null);t.firstChild;)(t.firstChild[Ht]||t.firstChild[Mt]).del();t[W].move(0,0,1)}(t),function(t){var n;(n=t[zi])&&(n.c=[],n.i=-1),Li(t)}(t),co(t))}const Zi=async t=>{const e=Bi(`
${Xe()?'':'
Log in to access cloud storage.
'}
`);e.s();const o=t=>{n(e.m,"ftr").style.display=t?"unset":"none"},i=async()=>{await Di(e.h,e.b,(()=>e.m[pt].del())),o(!0)};await i(),l(e.m,"[cld]",(()=>at((async()=>{o(!1),await Ei(t,e.h,e.b,!0,(async t=>await i()))})))),l(e.m,"[dvc]",(async()=>{e.m[pt].del(),await async function(t){try{const n=await le(he);if(!n)return;const e=await Ii(t,n.f);if(!e)return void Fi();n.h?ao(t,n.h,e):so(t,F(n.f.name),e)}catch(t){console.log(t),Fi()}}(t)}))};const Bi=t=>{const e=ht(m(`
${t}`,"display: flex; flex-flow: column; height: 100%; overflow-y: overlay;"),"mdh",!1,m('
'));i(ct(e),"mdhc");const o=t=>n(e,t);return{m:e,h:o("hdr"),b:o("cnt"),s:()=>{ct(e).style.display="flex"}}},Di=async(t,n,e)=>{t.innerHTML='CloseRecent',l(t,"[c]",e);const o=await new Promise(((t,n)=>Je(qe(),t)));n.innerHTML=o?.length?o.map((t=>``)).join(" "):_i},Ei=async(t,n,e,o,i)=>{let a=await wt(),s={i:null,n:"Cloud"};const c=[],r=()=>Ai(e,a,((n,e)=>at((async()=>{a.d[n].n=await(async(t,n)=>(await b(`${gt}a/n/${t}`,n)).text())(n,e),n===ro(t)?.k&&((t,n,e)=>{t[po].n=n,lo(t,0,e,null)})(t,a.d[n].n,s?.n),r()}))),(t=>at((async()=>{if(Xi(a))return;const n=await(async t=>(await k(`${gt}a/cp/${t}`)).json())(t);a.d[n.i]={n:n.n,d:n.d},r()}))),(n=>at((async()=>{var e;await(e=n,k(`${gt}a/d/${e}`)),delete a.d[n],n===ro(t)?.k&&co(t),r()}))),(t=>at((async()=>{c.push(s),s={i:t,n:a.f[t]},l(),a=await wt(t),r()}))),((t,n)=>at((async()=>{a.f[t]=await(async(t,n)=>(await b(`${gt}f/n/${t}`,n)).text())(t,n),r()}))),(n=>at((async()=>{await(async(t,n)=>{const e=new URL(`${gt}f/d/${t}`);return bt(e,"k",n),(await k(e.toString())).json()})(n,ro(t)?.k)&&co(t),delete a.f[n],r()})))),l=()=>Ti(n,s,o,i,(()=>at((async()=>{const t=c.pop();s=t,l(),a=await wt(t.i),r()}))),(t=>at((async()=>{const n=await(async t=>(await b(`${gt}f/a`,t)).json())(t);a.f[n.i]=n.n,r()}))),((t,n)=>at((async()=>{const e=xt();a.d[e]={n:await yt(e,t,n,Vt()),d:Date.now()},r()}))));return r(),l(),()=>({f:s,d:a})},Ti=(t,n,e,o,i,a,s)=>{t.innerHTML=`\n\t\t${n.i||e?`${Yi}`:"Close"}\n\t\t${n.n??""}\n\t\t${Ki}`,l(t,"[c]",(t=>n.i?i():o()));const c=Oi("Add diagram",(()=>Ni("New diagram",(t=>s(n.i,t)))));l(t,"[af]",(t=>Ri(t,n.i?[c]:[c,Oi("Add folder",(()=>Ni("New folder",a)))])))},Ai=(t,n,e,o,i,a,s,c)=>{const r=Object.entries(n.f).sort(((t,n)=>t[1].localeCompare(n[1]))),d=Object.entries(n.d).sort(((t,n)=>(n[1].d??0)-(t[1].d??0)));t.innerHTML=`${r.map((t=>``)).join(" ")}\n\t\t${d.map((t=>``)).join(" ")}\n\t${r.length||d.length?"":_i}`,l(t,"[fld]",(t=>a(Number(t.currentTarget.getAttribute("fld"))))),l(t,"[fedt]",(t=>{const e=Number(t.currentTarget.getAttribute("fedt"));Ri(t,[Pi(n.f[e],(t=>s(e,t))),Ui((()=>c(e)))])})),l(t,"[edt]",(t=>{const a=t.currentTarget.getAttribute("edt");Ri(t,[Pi(n.d[a].n,(t=>e(a,t))),Oi("Copy",(()=>o(a))),Ui((()=>i(a)))])}))};function Ri(t,e){const o=n(t.currentTarget,"dts").getBoundingClientRect(),i=window.innerHeight-o.bottom<40*e.length;!function(t,n,e,o){const i=lt(m(`
${t.map((t=>t[0])).join(" ")}
`),n,e,!1,!0,!o);l(i,"a",(n=>{i[pt].del(),t[a(n.currentTarget)][1]()}));const a=t=>Array.prototype.indexOf.call(t.parentNode.children,t)}(e,window.innerWidth-o.right-5,i?o.top:o.bottom,i)}const Pi=(t,n)=>Oi("Rename",(()=>Ni(t,(e=>{t!==e&&n(e)})))),Ui=t=>['Delete',()=>{confirm("Delete?")&&t()}],Oi=(t,n)=>[`${t}`,n];function Ni(t,e){const o=ht(m(`
`),"mdm",!0);l(o,"[sv]",(t=>{o[pt].del(),e(ho(n(o,"nm").value))}))}async function ji(t,n,e){const o=await Ii(t,n);return o||(me(t,n,e.clientX,e.clientY),null)}async function Ii(t,n){if("image/png"!==n.type)return null;const e=await je(n);if(e){Si(t);const n=_(e);return hi(t,n),n}return null}const Fi=()=>alert("File cannot be read. Use the exact image file you got from the application."),Ki='',Yi='',_i='
No items
',Xi=t=>Object.keys(t.d).length>29&&(alert("Maximum number of diagrams in folder exceeded"),!0),Gi=async t=>{const n=ro(t);if(n?.f){const e=St(t);Ae(t,e,(async o=>{const i=await n.f.createWritable();await i.write(o),await i.close(),Ge(0,n,1,null,e),ia(t)}))}else n?.k?n.e?at((async()=>await ta(t,n.k))):alert('You do not have access to edit this diagram. Use "Save As" to make your own copy.'):qi(t)},qi=async t=>Xe()?Ji(t):Wi(t),Ji=async t=>{const e=await at((()=>(async(t,n)=>{const e=Bi(n),o=await Ei(t,e.h,e.b,!1,(()=>e.m[pt].del()));return e.s(),{m:e.m,d:()=>o()}})(t,`
`))),o=()=>e.m[pt].del(),i=()=>ho(n(e.m,"snm").value);l(e.m,"[dvc]",(n=>{o(),Qi(t,i())})),l(e.m,"[cld]",(async()=>{const n=e.d();if(Xi(n.d))return;const a=async t=>{at(t),o()},s=(e,o)=>{io(t,e,o,!0,!0,n?.f?.n),ia(t)},c=i(),r=Object.entries(n.d.d).find((t=>t[1].n===c));if(r){const n=ro(t);return r[0]===n?.k&&n.m?void a((async()=>await ta(t,n.k))):void(confirm("Overwrite existing file?")&&a((async()=>s(r[0],await(async(t,n)=>(await k(`${gt}a/o/${t}`,n)).text())(r[0],St(t))))))}a((async()=>{const e=xt();s(e,await yt(e,n.f.i,c,St(t)))}))}))},Wi=t=>{const e=ht(m(`
Log in to access cloud storage.
`),"mdm");l(e,"[dvc]",(o=>{Qi(t,ho(n(e,"nm").value)),e[pt].del()}))},Qi=(t,n)=>{const e=St(t);Ae(t,e,(async o=>{const i=re(o,n?.length?n:aa());if(i){const n=await i;n&&(ao(t,n,e),ia(t))}else so(t,n,e),ia(t)}))},ta=async(t,n)=>{let e;if(na(t)){const o=[...t[oa].c,...t[oa].u],i=function(t,n){for(const e in t.s)n.has(e)&&Ut(t.s[e]);return t}(Zt(o.length?[...document.querySelectorAll(o.map((t=>`[id="${t}"]`)).join(","))]:null,!1),t[oa].u);t[oa].d.forEach((t=>{i.s[t]=null})),e=await(async(t,n)=>(await k(`${gt}u/${t}`,n)).json())(n,i)}else e=(await vt(n)).d;Et(t,e),ia(t)},na=t=>t[oa].c.size||t[oa].u.size||t[oa].d.size,ea=t=>{t[oa].c.clear(),t[oa].u.clear(),t[oa].d.clear()},oa=Symbol(0),ia=t=>{ea(t),ft("Saved")},aa=()=>{const t=t=>t<10?`0${t}`:t.toString(),n=new Date;return`${n.getFullYear()}-${t(n.getMonth()+1)}-${t(n.getDate())}.${t(n.getHours())}${t(n.getMinutes())}${t(n.getSeconds())}.png`};function sa(t,n,e,o){const i=n=>t[W].select(Gt(t,n,!0,e,o));if(n?.data)i(function(t){if(!t)return null;const n=new Map,e=t=>{const e=I();return n.set(t,e),e},o=[];for(const n in t.s){const i=e(t.s[n].id);t.s[i]=t.s[n],t.s[i].id=i,delete t.s[n],0===t.s[i].type&&o.push(t.s[i])}return o.forEach((t=>{t.s.s=n.get(t.s.s),t.e.s=n.get(t.e.s)})),t}(n.data));else if(n?.img)ge(n.img,(t=>i(Bt(t))));else if(n?.txt){const e=function(t,n){const e=f("text");Ve(e,null,n),e.style.visibility="hidden",t.append(e);const o=ke(e);return e.remove(),o}(t,n.txt);i(Bt({id:I(),type:3,position:{x:0,y:0},title:n.txt,w:e.w,h:e.h}))}else ft("Clipboard is empty")}class ca extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='';const n=t.getElementById("u"),e=t.getElementById("r");c(this.g,Vi,(t=>{const o=(t,n)=>n?i(t,"actv"):a(t,"actv");o(n,t.detail.u),o(e,t.detail.r)})),c(n,"click",(t=>Mi(this.g))),c(e,"click",(t=>Hi(this.g)))}}customElements.define("ap-history",ca);const ra=-1!==window.navigator.userAgent.indexOf(" OPR"),la=t=>ra&&0!==t.button,da=(t,n,e,o,i)=>t.x<=o&&o<=t.x+n&&t.y<=i&&i<=t.y+e,ua={iceServers:[{urls:"stun:stun3.l.google.com:19302"}]},ha=`${mt}m/`;async function pa(t,n,e,o){const i=ei();if(null==i)return e(),null;it(!0);const a=I(),s=new Map,c=await async function(t,n,e,o,i){const a=await async function(t,n,e){const o=new RTCPeerConnection(ua),i=o.createDataChannel("dgrm");return i.onopen=n=>t(i,n),i.onmessage=t=>n(i,t),i.onerror=t=>{o.close(),e()},await o.setLocalDescription(),new Promise(((t,n)=>{o.onicecandidate=n=>{n.candidate||t(o)}}))}(e,o,i);await((t,n,e)=>k(`${ha}o`,{m:t,c:n,s:e}))(n,t,a.localDescription.sdp);const s=X(3e3,15e3,(async()=>{try{const e=await(async(t,n)=>(await x(`${ha}a?m=${t}&c=${n}`)).text())(n,t);e&&(s(),a.setRemoteDescription({sdp:e,type:"answer"}))}catch{c(),i()}}),i),c=()=>{s(),a.close(),it(!1)};return c}(a,n,((t,n)=>t.send(Y([0,a,i]))),((n,e)=>{const o=_(e.data);switch(o[0]){case 6:si(t,s.get(o[1]),o[2],o[3]);break;case 4:ri(t,o[1]);break;case 5:li(t,o[1]);break;case 0:s.set(o[1],ii(o[2]));break;case 7:s.get(o[1]).remove(),s.delete(o[1]);break;case 2:hi(t,o[1]),l(n);break;case 3:t[W].move(o[1],o[2],1),l(n)}}),(()=>{d(),o()}));let r;const l=n=>{r=ci(t,a,(t=>n.send(t))),it(!1)},d=()=>{s.forEach((t=>t.remove())),r&&r(),c(),it(!1)};return d}async function fa(t,n,e){const o=ei();if(null==o)return n(),null;const i=I(),a=I(),s=new Map,c=(t,n)=>s.forEach(((e,o)=>o!==n?o.send(t):null)),r=await async function(t,n,e,o,i){const a=I();await((t,n)=>k(`${ha}c`,{m:t,k:n}))(t,a);const s=new Set,c=X(5e3,72e5,(async()=>{try{const i=await(async(t,n)=>{const e=await(await x(`${ha}o?m=${t}&k=${n}`)).json();return e?Object.entries(e):void 0})(t,a);if(!i?.length)return;i.forEach((async i=>{const c=await async function(t,n,e,o){const i=new RTCPeerConnection(ua);return i.ondatachannel=t=>{const a=t.channel;a.onopen=t=>n(a,t),a.onmessage=t=>e(a,t),a.onerror=t=>{i.close(),o(i,a,t)}},await i.setRemoteDescription({sdp:t,type:"offer"}),await i.setLocalDescription(),new Promise(((t,n)=>{i.onicecandidate=n=>{n.candidate||t(i)}}))}(i[1],((t,e)=>n(i[0],t,e)),e,((t,n,e)=>{s.delete(t),o(n,e)}));s.add(c),await((t,n,e,o)=>k(`${ha}a`,{m:t,k:n,c:e,s:o}))(t,a,i[0],c.localDescription.sdp)}))}catch{r(),i()}})),r=()=>{c(),s?.forEach((t=>t.close()))};return r}(i,((n,e,i)=>{e.send(Y([0,a,o]));const c=St(t);e.send(Y(zt(c)?[3,t[W].data.position.x,t[W].data.position.y]:[2,c])),s.forEach((t=>e.send(Y([0,t.i,t.n]))))}),((n,e)=>{const o=_(e.data);switch(o[0]){case 6:si(t,s.get(n).c,o[2],o[3]),c(e.data,n);break;case 4:ri(t,o[1]),c(e.data,n);break;case 5:li(t,o[1]),c(e.data,n);break;case 0:s.set(n,{i:o[1],n:o[2],c:ii(o[2])}),c(e.data,n)}}),((t,n)=>{const e=s.get(t);c(Y([7,e.i]),t),e.c.remove(),s.delete(t)}),(()=>{d(),e()})),l=ci(t,a,c),d=()=>{l(),s.forEach((t=>t.c.remove())),r()};return{dispose:d,id:i}}function ma(t,n,e){const o=m(`
Live collaboration in progress
`),i=ht(o),a=(t,n)=>l(o,t,(t=>{i[pt].del(),n()}));a("[mstop]",n),a("[mshare]",(async n=>{await navigator.clipboard.writeText($("m",t)),ft("Link copied to clipboard")}))}const ga=t=>{t[wa]?.dispose(),t[wa]=void 0,oi(!1)},va=t=>{ga(t),alert("Live collaboration disabled")},wa=Symbol(0);async function ya(t){let e=!0,o={v:!0};const i=ro(t),a=i?.k;a&&(o={},Xe()&&(o=await(async t=>(await x(`${gt}p/${t}`)).json())(i.k),e=!o.m));const s=m(`
Anyone with a link can:
${a&&e?'
You do not have access to manage permissions.
':""}
${a?"":'
To share, save diagram to the cloud.
'}
`),r=ht(s),d=n(s,"v"),u=n(s,"e"),h=async()=>{try{it(!0),await(t=i.k,n={v:d.checked,e:u.checked},k(`${gt}p/s/${t}`,n))}catch{alert("Error")}finally{it(!1)}var t,n};c(d,"change",(async t=>{d.checked||(u.checked=!1),await h()})),c(u,"change",(async t=>{u.checked&&(d.checked=!0),await h()})),l(s,"[s]",(async t=>{await navigator.clipboard.writeText($("k",i.k)),ft("Link copied to clipboard"),r[pt].del()}))}class xa extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='',this.M=!1;const e=e=>n(t,e);this.H=e;const o=t=>{this.contains(t.target)||s()};let i;let a=null;const s=()=>{a.remove(),a=null,r(document,"pointerdown",o),i&&i()},d=()=>{c(document,"pointerdown",o,!0),i=V(s),a=function(t,n,e){const o=Xe(),i=o||de,a=ro(t)?.k?"Save and sync with cloud":"Save",s=m(`
New
Open
${i?'
'+a+"
":""}
Save${ue||i?" as":""}
${mo()?sn+"Download":cn+"Send"} image
Share link
Live collaboration
${o?o.n:"Log in"}
`);s.setAttribute("options","");const d=(t,n)=>l(s,t,(async t=>{e(),await n(t)}));l(s,"[menu2]",(t=>e())),d("[new]",(()=>{Si(t)})),i&&d("[save]",(async()=>await Gi(t)));return d("[saveas]",(()=>qi(t))),d("[open]",(async()=>await Zi(t))),d("[collab]",(async n=>await async function(t){const n=()=>ga(t),e=()=>va(t);try{switch(t[wa]?.clientType){case 1:ma(t[wa].id,n);break;case 2:ma(t[wa].id,n,!0);break;default:{const o=await fa(t,n,e);if(!o)return;c(t,"dgrmn",e,!0),t[wa]={clientType:1,id:o.id,dispose:()=>{r(t,"dgrmn",e),o.dispose()}},oi(!0),ma(t[wa].id,n);break}}}catch{e()}}(t))),d("[login]",(t=>function(){const t=Xe();ht(m(t?`
${t.n}
${t.l}
Log out Delete account Delete account and all data. Disable auto payment for subscription.
`:'
Log in to access
cloud storage
'))}())),d("[link]",(n=>ya(t))),d("[down]",(n=>(t=>Ae(t,St(t),(async n=>await fo(t,n))))(t))),s}(this.g,this.M,(()=>s())),t.append(a)};c(e("menu"),"click",(t=>d()))}bage(t){(t?i:a)(this.H("menu"),"bage"),this.M=t}}customElements.define("ap-menu",xa);class ba extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='';const n=t.getElementById("smenu");n.querySelectorAll('[data-cmd="shapeAdd"]').forEach((t=>{c(t,"pointerdown",this),c(t,"click",this)})),c(n,"pointerleave",this),c(n,"pointerup",this),c(n,"pointermove",this);const e=t.getElementById("m");let o;c(e,"click",(t=>{o?(o(),o=null,e.style.opacity="0.4"):(o=_o(this.g),e.style.opacity="1")}))}handleEvent(t){switch(t.type){case"pointermove":if(!this.L){const n=document.elementFromPoint(t.clientX,t.clientY);if(n===this.V)return;this.S===this.V&&this.g.ownerSVGElement.setPointerCapture(t.pointerId),this.V=n}break;case"pointerleave":if(this.L=!0,null!=this.Z){const n=ka(this.g,this.Z,!1,t.clientX,t.clientY)[0];h(t)||this.g[W].select([n]),n[Ht]?n[Ht]?.moveCapture(t,n,!0):n[Mt]?.moveCapture(t,n,!0)}this.B();break;case"pointerdown":this.Z=parseInt(d(t,"data-cmd-arg")),this.S=document.elementFromPoint(t.clientX,t.clientY),this.V=this.S,this.L=null;break;case"pointerup":this.B();break;case"click":this.g.ownerSVGElement.focus(),this.g[W].select(ka(this.g,parseInt(d(t,"data-cmd-arg")),!0))}}B(){this.Z=null,this.S=null,this.V=null}}customElements.define("ap-menu-shape",ba);const ka=(t,n,e,o,i)=>Gt(t,Bt(0===n?{id:I(),type:0,s:{data:{dir:"right",position:{x:-24,y:0}}},e:{data:{dir:"right",position:{x:24,y:0}}}}:{id:I(),type:n,position:{x:0,y:0},title:"Title"}),e,o,i);const $a=document.getElementById("canvas");var Ca,Ma,Ha,La,Va;$a[W]={data:{position:{x:0,y:0},scale:1,cell:24},shapeMap:function(n){const e=(t,e)=>(t.setAttribute("bg",e.toString()),vn(n,t),t);return{0:{create:t=>e(Vn(n,t),2)},1:{create:i=>e(function(n,e){e.r??=48;let i,a=e.r;const s=Xn(n,e,' ',{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:48}},top:{dir:"top",position:{x:0,y:-48}}},(()=>(i=P(e.position,-e.r),{x:e.r,y:e.r})),(r=>{let l=tt(n[W].data,r).x-i.x;l<48&&(l=48);const d=48*Math.round(l/48)/2;e.r!==d&&Qn(t(s.el,"text"))<=d&&(e.r=d,e.position.x=i.x+d,e.position.y=i.y+d,c(),a=d);const u=l-e.r;o(r.target,{x:u,y:u})}),(t=>{const n=Qn(t,a);n!==e.r&&(e.r=n,c())}));function c(){s.cons.right.position.x=e.r,s.cons.left.position.x=-e.r,s.cons.bottom.position.y=e.r,s.cons.top.position.y=-e.r,rn(s),Wn(s.el,"outer",e.r+24),Wn(s.el,"main",e.r),s.draw()}return 48!==e.r?c():s.draw(),s.el[Ht].patch=t=>{const n=hn(s,t),o=null!=t.r&&e.r!==t.r;o&&(e.r=t.r),o?c():n&&s.draw()},s.el}(n,i),2)},2:{create:t=>e(te(n,t),2)},3:{create:t=>e(be(n,t),2)},4:{create:a=>e(function(n,e){e.w??=96;let a,s=e.w;const c=Xn(n,e,' ',{right:{dir:"right",position:{x:48,y:0}},left:{dir:"left",position:{x:-48,y:0}},bottom:{dir:"bottom",position:{x:0,y:48}},top:{dir:"top",position:{x:0,y:-48}}},(()=>{const t=e.w/2;return a=P(e.position,-t),{x:t,y:t}}),(i=>{let l=tt(n[W].data,i).x-a.x;l<48&&(l=48);const d=48*Math.round(l/48);if(e.w!==d&&Ze(t(c.el,"text"))<=d){e.w=d;const t=d/2;e.position.x=a.x+t,e.position.y=a.y+t,r(),s=d}const u=l-e.w/2;o(i.target,{x:u,y:u})}),(t=>{const n=Ze(t,s);n!==e.w&&(e.w=n,r())}));function r(){const t=Se(e.w,0);c.cons.right.position.x=t.r.x,c.cons.left.position.x=t.l.x,c.cons.bottom.position.y=t.b.y,c.cons.top.position.y=t.t.y,rn(c);const n=Se(e.w,9);ze(c.el,"main",n),ze(c.el,"border",n),ze(c.el,"outer",Se(e.w,-24)),c.draw()}return i(c.el,"shrhomb"),96!==e.w?r():c.draw(),c.el[Ht].patch=t=>{const n=hn(c,t),o=null!=t.w&&e.w!==t.w;o&&(e.w=t.w),o?r():n&&c.draw()},c.el}(n,a),2)},5:{create:t=>e(ae(n,t),1)},6:{create:t=>e(ve(n,t),0)},7:{create:t=>e(function(t,n){const e=Gn(t,n,Xo(n),{},null,null,null,fe,(()=>{ni(n.position)}));return i(e.el,"shmrk"),e.draw(),e.el[Ht].patch=t=>{pn(e,t)&&e.draw()},e.el}(n,t),3)}}}($a)},$a.ownerSVGElement.addEventListener("contextmenu",(async t=>{u(t)||t.preventDefault()})),function(t){let n,e,o;function i(t){t.isPrimary&&t.isTrusted&&(n&&Math.abs(n.x-t.clientX)<3&&Math.abs(n.y-t.clientY)<3?t.stopImmediatePropagation():(n=null,void 0===t.movementX?(t[q]=e?t.clientX-e:0,t[J]=o?t.clientY-o:0,e=t.clientX,o=t.clientY):(t[q]=t.movementX,t[J]=t.movementY)))}t.addEventListener("pointerdown",(a=>{n={x:a.clientX,y:a.clientY},e=null,o=null,t.addEventListener("pointermove",i,{capture:!0,passive:!0}),t.addEventListener("pointerup",(n=>{r(t,"pointermove",i,!0)}),{capture:!0,once:!0,passive:!0})}),{capture:!0,passive:!0})}(document),function(t){c(document,"paste",(async n=>{u(n)||sa(t,(t=>{const n=t.getData("text/plain"),e=Ke(n);return e?{data:e}:t.files?.length&&t.files.item(0).type?.startsWith("image/")?{img:t.files.item(0)}:n?{txt:n}:null})(n.clipboardData))}));const n=Z((async()=>await Gi(t)),500);document.addEventListener("keydown",(async e=>{if((e.ctrlKey||e.metaKey)&&"KeyS"===e.code&&e.preventDefault(),(e.ctrlKey||e.metaKey)&&!e.repeat&&!u(e))switch(e.code){case"KeyZ":rt(),Mi(t);break;case"KeyY":rt(),Hi(t);break;case"KeyS":rt(),e.shiftKey?qi(t):n();break;case"KeyA":rt(),Co(t)}}))}($a),function(t){const n=t.ownerSVGElement;let e,a,s,l,d;const u=()=>{d?.remove(),d=null};let h=!1;function p(t){if(t[vo]||!s)return void g();t[vo]=!0,h&&(u(),h=!1);const n=t.clientX-a.x,e=t.clientY-a.y;s.width.baseVal.value=Math.abs(n),s.height.baseVal.value=Math.abs(e),n<0&&(l.x=t.clientX),e<0&&(l.y=t.clientY),s.style.transform=`translate(${l.x}px, ${l.y}px)`}function m(n){if(s&&!h){n[vo]=!0;const e=n=>da(Q(t[W].data,l.x,l.y),s.width.baseVal.value/t[W].data.scale,s.height.baseVal.value/t[W].data.scale,n.x,n.y);!function(t,n,e,o){const i=Mo(),a=t=>Ho(i,t),s=t=>t.shape&&e(t.shape.shapeEl)||o(t);for(const t of n)if(t[Ht])e(t)&&a({shape:t});else if(t[Mt]){const n=s(t[Mt].data.s),e=s(t[Mt].data.e);n&&e?a({path:t}):n?a({pathEnd:t[Mt].data.s}):e&&a({pathEnd:t[Mt].data.e})}t[W].selectedSet(i)}(t,t.children,(t=>e(t[Ht].data.position)),(t=>e(t.data.position)))}g()}function g(){clearTimeout(e),e=null,s?.remove(),s=null,u(),h=!1,r(n,"pointermove",p),r(n,"wheel",g),r(n,"pointercancel",g),r(n,"pointerup",m)}c(n,"pointerdown",(r=>{if(!r.isPrimary||kt(r)!==n||la(r))return void g();c(n,"pointermove",p),c(n,"wheel",g,!0),c(n,"pointercancel",g,!0),c(n,"pointerup",m,!0);const u=()=>{t[W].selectedSet(null),h=!0,n.setPointerCapture(r.pointerId),a={x:r.clientX,y:r.clientY},l={x:r.clientX,y:r.clientY},s=f("rect"),s.style.cssText="rx:8px; fill: rgb(108 187 247 / 51%)",o(s,l),n.append(s)};2!==r.button?e=setTimeout((t=>{u(),d=f("circle"),i(d,"ative-elem"),d.style.cssText="r:10px; fill: rgb(108 187 247 / 51%)",o(d,{x:r.clientX,y:r.clientY}),n.append(d)}),500):u()}))}($a),function(t){const n=t.ownerSVGElement;let e,o=Mo();const i=()=>{e&&(e[pt]?.del(),e=null)};let a,s;c(n,"pointerdown",(l=>{if(i(),!l.isPrimary)return r(n,"pointermove",a),r(n,"pointerup",s),void t[W].moveCapture();s=s=>{var c;r(n,"pointermove",a),c=yo(t,o,s),i(),e=c},c(n,"pointerup",s,!0),a=e=>{r(n,"pointerup",s),xo(t,o,e,kt(l))},0===l.button&&c(n,"pointermove",a,!0)})),Ye(t,(()=>So(o)),(()=>{e?i():Ro(o)&&!To(o)&&t[W].select(null)}));const l=t=>{zo(o),o=t??Mo(),Zo(o,null)};t[W].selectedSet=l,t[W].select=t=>{i();const n=Mo();if(t?.length)for(const e of t)Ho(n,Po(e));l(n)}}($a),function(t){const n=t[W].data,e=function(t,n){let e;function o(n){e!==n&&(e=n,t.style.backgroundImage=`radial-gradient(rgb(73 80 87 / ${n}) 1px, transparent 0)`)}return o(.6),t.style.backgroundSize=`${n.cell}px ${n.cell}px`,function(){const e=n.cell*n.scale;n.scale<.5?o(0):n.scale<=.9?o(.2):o(.6),t.style.backgroundSize=`${e}px ${e}px`,t.style.backgroundPosition=`${n.position.x}px ${n.position.y}px`}}(t.ownerSVGElement,n);function o(){t.style.transform=`matrix(${n.scale}, 0, 0, ${n.scale}, ${n.position.x}, ${n.position.y})`,e()}function i(t,e){if(t<.25||t>4)return;const i=t/n.scale;n.scale=t,n.position.x=i*(n.position.x-e.x)+e.x,n.position.y=i*(n.position.y-e.y)+e.y,o()}!function(t,n,e,o){let i,a,s,l,d=!1;function u(t){s=null,l=null,i?.id===t.pointerId&&(i=null),a?.id===t.pointerId&&(a=null),i||a||(r(document,"pointermove",h),r(document,"pointercancel",u),r(document,"pointerup",u),d=!1)}function h(t){if(d){if(i&&!a||!i&&a)return n.position.x=t.clientX+(i||a).shift.x,n.position.y=t.clientY+(i||a).shift.y,void o();if(!a||!i||a?.id!==t.pointerId&&i?.id!==t.pointerId)return;const c=Math.hypot(i.pos.x-a.pos.x,i.pos.y-a.pos.y),r={x:(i.pos.x+a.pos.x)/2,y:(i.pos.y+a.pos.y)/2};s&&(n.position.x=n.position.x+r.x-l.x,n.position.y=n.position.y+r.y-l.y,e(n.scale/s*c,r)),s=c,l=r}i?.id===t.pointerId&&(i=et(t,n)),a?.id===t.pointerId&&(a=et(t,n))}c(t.ownerSVGElement,"pointerdown",(t=>{!i&&!t.isPrimary||i&&a||(i||(c(document,"pointermove",h),c(document,"pointercancel",u),c(document,"pointerup",u),i=et(t,n)),a||i?.id===t.pointerId||(a=et(t,n)))})),t[W].moveCapture=()=>{d=!0}}(t,n,i,o),t.ownerSVGElement.addEventListener("wheel",(t=>{t.preventDefault();const e=()=>{n.position.x-=t.deltaX,n.position.y-=t.deltaY,o()},a=e=>i(n.scale+e,p(t));Math.abs(t.deltaY)>50?t.ctrlKey?e():a(t.deltaY<0?.25:-.25):t.ctrlKey?a(-.01*t.deltaY):e()})),t[W].move=function(t,e,i){n.position.x=t,n.position.y=e,n.scale=i,o()},t[W].scale=i}($a),function(t){const n=t.ownerSVGElement,e=t.ownerDocument;let o,i,a,s;function d(n){r(e,"pointermove",p),r(e,"pointerup",u),a||(a=function(t,n,e,o){const i=m(`${Ie?'
Paste
':""}
Select all
Add image
`),a=(t,n)=>l(i,t,(async t=>{await n(),o()}));Ie&&a("[p]",(async()=>sa(e,await(async()=>{let t;try{t=await(navigator.clipboard?.read())}catch{}if(!t?.length)return null;const n=t[0],e=n.types.includes("text/plain")?await(await n.getType("text/plain")).text():null,o=Ke(e);if(o)return{data:o};const i=n.types.find((t=>t.startsWith("image/")));return i?{img:await n.getType(i)}:e?{txt:e}:null})(),t,n)));a("[s]",(async()=>Co(e))),a("[i]",(async()=>async function(t,n,e){const o=await le(pe);o&&me(t,o.f,n,e)}(e,t,n)));return{del:dt(i,t-10,n-10)[pt].del,el:i}}(n.clientX,n.clientY,t,u),c(e,"pointerdown",h))}function u(){clearTimeout(o),o=null,a?.del(),a=null,r(e,"pointerdown",h),r(e,"pointerup",u),r(e,"wheel",u),r(e,"keydown",u),r(e,"pointerup",g),r(e,"pointercancel",g),r(e,"pointermove",f),i=!1,s=!1}function h(t){a.el.contains(t.target)||u()}function p(){u(),i=!0}function f(){s||u(),i=!0}function g(){s=!0}c(n,"pointerdown",(async function(t){t.isPrimary&&kt(t)===n?(u(),c(e,"pointermove",p,!0),c(e,"pointerup",u,!0),c(e,"wheel",u,!0),c(e,"keydown",u,!0),2!==t.button&&(o=setTimeout((n=>{d(t),c(e,"pointerup",g,!0),c(e,"pointercancel",g,!0),c(e,"pointermove",f,!0)}),500))):u()})),n.addEventListener("contextmenu",(async t=>{n===kt(t)&&(t.preventDefault(),i||d(t))}))}($a),(t=>{const n=(n,e)=>c(t,n,(t=>{t[ki]||e(t)}));n("dgrmc",(n=>vi(t,n.detail.newIds,n.detail.shapesToCreateData))),n("dgrmd",(n=>wi(t,n.detail.delIds,n.detail.shapesToDelData))),n("dgrmu",(n=>yi(t,n.detail.shapesBeforeUpdData,n.detail.shapesToUpdData))),n("dgrmdc",(n=>xi(t,n.detail.delIds,n.detail.shapesToDelData,n.detail.newIds,n.detail.shapesToCreateData)))})($a),function(t){document.body.addEventListener("dragover",(t=>{t.preventDefault()})),document.body.addEventListener("drop",(async n=>{if(n.preventDefault(),1===n.dataTransfer?.items?.length&&"file"===n.dataTransfer.items[0].kind&&n.dataTransfer.items[0].type.startsWith("image/"))if("getAsFileSystemHandle"in n.dataTransfer.items[0]){const e=await n.dataTransfer.items[0].getAsFileSystemHandle(),o=await ji(t,await e.getFile(),n);o&&ao(t,e,o)}else{const e=n.dataTransfer.items[0].getAsFile(),o=await ji(t,e,n);o&&so(t,F(e.name),o)}else Fi()}))}($a),(t=>{t[oa]={c:new Set,u:new Set,d:new Set};const n=n=>{t[oa].c.add(n),t[oa].u.delete(n),t[oa].d.delete(n)},e=n=>{t[oa].c.has(n)||t[oa].u.add(n),t[oa].d.delete(n)},o=n=>n?.forEach((n=>{t[oa].d.add(n),t[oa].c.delete(n),t[oa].u.delete(n)})),i=t=>Object.keys(t.s).forEach(e);c(t,"dgrmc",(t=>{t.detail.newIds?.forEach(n)})),c(t,"dgrmu",(t=>i(t.detail.shapesToUpdData))),c(t,"dgrmd",(t=>o(t.detail.delIds))),c(t,"dgrmdc",(t=>{o(t.detail.delIds),t.detail.newIds?.forEach(n)})),c(t,di,(t=>i(t.detail))),c(t,ui,(t=>o(t.detail))),t.addEventListener("dgrmn",(n=>{!na(t)||window.confirm("Changes you made may not be saved. Continue?")?ea(t):n.preventDefault()})),window.addEventListener("beforeunload",(n=>{na(t)&&n.preventDefault()}))})($a);{const t=new URL(window.location.href),n=n=>t.searchParams.get(n);n("usr")&&(Ca=n("usr"),Ma=n("usrn"),Ha=parseInt(n("expr")),localStorage.setItem("dgrm",Y({l:Ca,n:Ma,e:Ha})),C("usr","usrn","expr"))}(t=>{const n=new xa(t);n.id="menu",g(n)})($a),g(new ca($a)),g(new ba($a));{const t=new URL(window.location.href),e=n=>t.searchParams.get(n);if(e("k")){it(!0);const t=e("k");try{const n=await vt(t);hi($a,n.d),io($a,t,n.n,n.e,n.m,n.l)}catch{alert("The link is broken or you do not have permission. Try to relogin."),await(Va=t,new Promise(((t,n)=>eo(qe(),Va,(()=>t())))))}C("k"),it(!1)}else if(e("l")){try{const t=e("l"),n=await(La=t,new Promise(((t,n)=>no(qe(),La,t))));n&&(hi($a,n.s),so($a,n.n,n.s))}catch{alert("The link is broken.")}C("l")}else if(e("m")){const t=e("m");C("m"),await async function(t,n){const e=()=>va(t);try{const o=await pa(t,n,(()=>ga(t)),e);if(!o)return;c(t,"dgrmn",e,!0),t[wa]={clientType:2,id:n,dispose:()=>{r(t,"dgrmn",e),o()}},oi(!0)}catch{e()}}($a,t)}else Xe()||function(){const t=m('');function e(){t.remove(),r(document,"pointerdown",o),r(document,"wheel",e),r(document,"keydown",e),r(document,"dragover",e)}function o(n){t.contains(n.target)||e()}t.style.display="unset",c(document,"pointerdown",o),c(document,"wheel",e,!0),c(document,"keydown",e,!0),c(document,"dragover",e,!0),c(n(t,"cl"),"click",(t=>e()),!0),g(t)}()}