diff --git a/index.html b/index.html index d24325e..514f948 100644 --- a/index.html +++ b/index.html @@ -175,6 +175,6 @@ - + \ No newline at end of file diff --git a/index.js b/index.js index 9a1f930..70ac8aa 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),h=t=>"TEXTAREA"===t.target.tagName.toUpperCase(),u=t=>"mouse"===t.pointerType;function p(t,n){const e=document.createElementNS("http://www.w3.org/2000/svg",t);return n&&(e.innerHTML=n),e}const f=t=>t.parentElement.appendChild(t),m=(t,n)=>{const e=document.createElement("div");return t&&(e.innerHTML=t),e.style.cssText=n,e},v=(...t)=>document.body.append(...t);function g(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}],y=async t=>k(await fetch(t,{credentials:"include"})),x=async(t,n)=>b("POST",t,n),b=async(t,n,e)=>k(await fetch(n,{credentials:"include",method:t,headers:{"Content-Type":"application/json;charset=utf-8"},body:e?N(e):null})),k=t=>{if(!t.ok)throw 401===t.status&&alert("You have to relogin."),new Error;return t},$=(t,n)=>{const e=new URL(window.location.href);return e.searchParams.set(t,n),e.toString()},M=(...t)=>{const n=new URL(window.location.href);t?.forEach((t=>n.searchParams.delete(t))),history.replaceState(null,null,n.toString())};function C(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)=>z(t.ownerSVGElement,t,n);function z(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,n)=>new CustomEvent(t,{detail:n,cancelable:!1}),L=(t,n)=>t.dispatchEvent(n),S=(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}function T(t,n){for(let e=t.length-1;e>=0;--e)n(t[e])}const E=t=>t.values().next().value;function D(t,n,e){const o=e??1;return t.x+=o*n.x,t.y+=o*n.y,t}const Z=(t,n)=>({x:t.x+n,y:t.y+n}),A=(t,n)=>U(t,n.x,n.y),U=(t,n,e)=>(t.x=n,t.y=e,t),P=t=>({x:t.x,y:t.y}),R=(t,n)=>t?.x===n?.x&&t?.y===n?.y;function O(){const t=new Uint8Array(4);return window.crypto.getRandomValues(t),Array.from(t,(t=>t.toString(16).padStart(2,"0"))).join("")}const j=t=>t?.substring(0,t.lastIndexOf("."))||t,I=t=>K(N(t)),F=t=>t&&!!Object.keys(t).length,N=t=>JSON.stringify(t),K=t=>JSON.parse(t),_=(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 Y(t,n,e){t.x+=e[X]/n,t.y+=e[G]/n}const X=Symbol("movementX"),G=Symbol("movementY"),J=Symbol("Canvas"),W=(t,n,e)=>({x:(n-t.position.x)/t.scale,y:(e-t.position.y)/t.scale}),q=(t,n)=>W(t,n.clientX,n.clientY);function Q(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 tt(t){return{x:t.clientX,y:t.clientY}}function nt(t,n){return{id:t.pointerId,pos:tt(t),shift:{x:n.position.x-t.clientX,y:n.position.y-t.clientY}}}let et;function ot(t){t&&!et?(et=m("","z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; background: #fff; opacity: 0"),et.classList.add("blnk"),v(et)):t||(et?.remove(),et=null)}function it(t,n,e){const o=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 i(t,n){o&&(o.style.left=`${t}px`,o.style.top=window.scrollY+n-o.getBoundingClientRect().height+"px")}return o.setAttribute("ap-modal",""),o.append(t),v(o),null!=n?i(n,e):o.style.transform="translate(-50%, -30%)",o[lt]={position:i,contains:t=>o?.contains(t),del:()=>{o?.remove()}},o}const at=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]");function st(t,n,e,o){const i=m(null,"z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; "+(o?"background-color: rgba(52,71,103,.05);":""));v(i);const a=it(t,n,e);i.append(a);const s=z(i,a,(t=>{"pointerdown"===t.type?c(i,"click",(t=>{t.stopImmediatePropagation(),l()}),!0):l()}),(t=>t.stopImmediatePropagation())),r=a[lt].del,l=()=>{r(),s(),i.remove()};return a[lt].del=l,a}function ct(t,n,e){const o=it(t,n,e),i=o[lt].del,a=z(document,o,(t=>s())),s=()=>{i(),a()};return o[lt].del=s,o}function rt(t,e,o){const i=m('
Close
',"display: flex; flex-flow: column; padding: 15px;");i.className=e??"mds",i.append(t);const a=o?st(i,null,null,!0):ct(i);return c(n(i,"ap-close"),"click",(t=>a[lt].del())),a}const lt=Symbol(0);function dt(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",v(n),setTimeout((t=>n.remove()),2800)}const ht="https://api.dgrm.net/api/",ut=`${ht}s/`;const pt=async(t,n)=>(async(t,n)=>b("PUT",t,n))(`${ut}a/u/${t}`,n),ft=async(t,n)=>(await(async(t,n)=>k(await fetch(t,{credentials:"include",method:"PUT",body:n})))(`${ut}a/n/${t}`,n)).text();function mt(){const t=new Date;return`${t.getUTCFullYear()}${(t.getUTCMonth()+1).toString().padStart(2,"0")}${O()}`}function vt(t){return wt(t).find((t=>!t.hasAttribute("data-evt-no")))}function gt(t){return wt(t)[0]}function wt(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 yt=Symbol("path"),xt=Symbol("shape"),bt="1.2",kt=t=>!F(t.s),$t=t=>Mt([...t.children],!1);function Mt(t,n,e){const o={v:bt,s:{}},i=new Map,a=t=>{let e=i.get(t);if(!e){const a=I(t[xt].data);n&&(a.id=O(),e=a.id),i.set(t,e),o.s[a.id]=a}return e},s=i=>{if(o.s[i[yt].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:I(o.data)}):o.shape?(e&&a(o.shape.shapeEl),{s:o.shape.shapeEl[xt].data.id,k:o.shape.connectorKey}):{p:I(o.data)};var i},c=i[yt].data,r={id:n?O():c.id,type:0,s:s(c.s),e:s(c.e)};c.styles&&(r.c=I(c.styles)),o.s[r.id]=r};for(const n of t)n[xt]?(a(n),e&&n[xt].paths?.forEach(s)):s(n);return o}function Ct(t){return{v:bt,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 Ht=(t,n)=>zt(t,"1.1"===n.v?function(t){const n={v:bt,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 zt(t,n){if(n.v!==bt)return alert("Wrong format"),null;const o=new Map;function i(n){let e=o.get(n);return e||(e=Lt(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)if(0===n.s[e].type)s.push(St(t,n.s[e],a));else i(n.s[e]);return[...o.values(),...s]}function Vt(t,n){const o=e(n.id);return o?((o[xt]||o[yt]).patch(n),o):t[J].shapeMap[n.type].create(n)}const Lt=(t,n)=>Vt(t,I(n)),St=(t,n,e)=>Vt(t,function(t,n){const e=t=>t.p?{data:{dir:t.p.dir,position:P(t.p.position)}}:{shape:{shapeEl:n(t.s),connectorKey:t.k}};return{type:0,id:t.id,styles:t.c,s:e(t.s),e:e(t.e)}}(n,e));const Bt=(t,n,e)=>L(t,Tt(n,e)),Tt=(t,n)=>V("dgrmc",{newIds:t,shapesToCreateData:n}),Et=(t,n)=>V("dgrmd",{delIds:t,shapesToDelData:n}),Dt=(t,n,e)=>L(t,Zt(n,e)),Zt=(t,n)=>V("dgrmu",{shapesBeforeUpdData:t,shapesToUpdData:n});function At(t,n,e,o){const i=D(((t,n,e)=>{const o=W(t,n,e);return Q(o,t.cell),o})(t[J].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 Ut(t,(t=>{t&&(n.x>t.x&&(n.x=t.x),n.y>t.y&&(n.y=t.y),e.x{n&&Q(D(n,i),t[J].data.cell)}))}function Ut(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 Pt=(t,n)=>t[J].select(Rt(t,Mt(n,!0),!0));function Rt(t,n,e,o,i){return At(t,n,null==o?window.innerWidth/2:o,null==i?window.innerHeight/2:i),e&&Bt(t,Object.keys(n.s),I(n)),t[J].select(null),zt(t,n)}function Ot(t,n){((t,n,e)=>{L(t,Et(n,e))})(t,n.map((t=>(t[xt]||t[yt]).data.id)),Mt(n,!1,!0)),n.forEach((t=>(t[xt]||t[yt]).del()))}function jt(n){for(const e in n.cons)o(t(n.el,e),n.cons[e].position)}function It(t,n){const e=t.getBoundingClientRect(),o=ct(n,e.left+10,e.top+10);return{del:o[lt].del,updPos:()=>{const n=t.getBoundingClientRect();o[lt].position(n.left+10,n.top+10)}}}const Ft=(t,n)=>({del:ct(t,n.clientX-66,n.clientY-10)[lt].del}),Nt=(t,n,e)=>{const o=Mt([n],!1,!1);(n[xt]??n[yt]).patch(e),Dt(t,o,Mt([n],!1,!1))};function Kt(t,n){!function(t,n){const e=t.el[xt].data;null!=n.title&&e.title!==n.title&&(e.title=n.title,t.drawTxt());n.styles&&(e.styles=n.styles,Yt(t.el))}(t,n);const e=n.position&&!R(t.el[xt].data.position,n.position);return e&&A(t.el[xt].data.position,n.position),e}const _t=t=>t.forEach((t=>{const n=e(t);n&&(n[xt]||n[yt]).del()}));function Yt(t){const n=(t[xt]??t[yt]).data;n.styles||(n.styles=[]),T(t.classList,(n=>{n?.startsWith("d-")&&t.classList.remove(n)})),i(t,...n.styles)}const Xt=(t,n,e)=>{const o=(n[xt]??n[yt]).data;o.styles||(o.styles=[]);const i={styles:I(o.styles)};Nt(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))};class Gt extends HTMLElement{constructor(t,n){super(),this.o=n,this.g=t}connectedCallback(){const t=t=>this.o[yt].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":Xt(this.g,this.o,t.detail.arg);break;case"del":Ot(this.g,[this.o]);break;case"copy":Pt(this.g,[this.o])}})),l(n,"[data-cmd]",(t=>{const n=d(t,"data-cmd-arg"),e={styles:this.o[yt].data.styles?I(this.o[yt].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")),Nt(this.g,this.o,e)}))}}customElements.define("ap-path-settings",Gt);const Jt='',Wt='',qt='';function Qt(t,n,e,o){const i=sn(t,n);i.shape={shapeEl:e,connectorKey:o},en(t,i)}function tn(t,n,e){const o=sn(t,n);o.shape.shapeEl!==cn(t,o)&&on(t,o),o.shape=null,o.data={dir:o.data.dir,position:e}}function nn(t,n){const e=t[yt].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&&tn(t,o,a.data.position):function(t,n,e,o){const i=sn(t,n);if(i.shape.shapeEl===e&&i.shape.connectorKey===o)return;i.shape.shapeEl!==e&&i.shape.shapeEl!==cn(t,i)&&on(t,i);i.shape={shapeEl:e,connectorKey:o},en(t,i)}(t,o,a.shape.shapeEl,a.shape.connectorKey):null!=a.shape?Qt(t,o,a.shape.shapeEl,a.shape.connectorKey):A(i.data.position,a.data.position)};o("s"),o("e")}function en(t,n){n.data=an(n.shape).pathAdd(n.shape.connectorKey,t)}const on=(t,n)=>an(n.shape)?.pathDel(t),an=t=>t?.shapeEl[xt],sn=(t,n)=>t[yt].data[n],cn=(t,n)=>(t[yt].data.e===n?t[yt].data.s:t[yt].data.e).shape?.shapeEl;function rn(n,e){const o=p("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=mn(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 vn(e,-.8,.8)?"left":vn(e,.8,2.4)?"top":vn(e,2.4,3.2)||vn(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=fn(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))),hn(e.s),hn(e.e)}let r,l;let d,h=0;function m(t){switch(h){case 0:h=1,i(o,"s"===l?"highlight-s":"e"===l?"highlight-e":"select"),un(e.s,2),un(e.e,2),f(o);break;case 1:h=2,d=H(o,(t=>v(!0))),function(t){r||(r=Ft(new Gt(n,o),t))}(t)}}function v(t){t||(l=null),h=0,a(o,"select","highlight-s","highlight-e"),un(e.s,1),un(e.e,1),r?.del(),r=null,d&&(d(),d=null)}return o[yt]={data:e,drawPosition:c,click:(t,n)=>{if(0===h||l){const o=n||t?gn(e,n??vt(t)):null;wn(l,o)||v(),l=o}m(t)},patch:t=>{e.styles&&(e.styles=t.styles,Yt(o)),t.s&&nn(o,t),c()},moveCapture:(t,i,a)=>{v(!0);const s=a?null:Mt([o],!1,!0),r=t=>{v(!0),s?Dt(n,s,Mt([o],!1,!0)):Bt(n,[e.id],Mt([o],!1,!0)),u(t)||m()},l=gn(e,i);l?dn(n,o,c,t,l,r):function(t,n,e,o,i){C(n,o,(o=>{!function(t,n,e,o){const i=n=>Y(n,t.scale,o);ln(n.s,i),n.s.shape?.shapeEl&&n.s.shape?.shapeEl===n.e.shape?.shapeEl||ln(n.e,i);n.s.shape||n.e.shape||e()}(t[J].data,n[yt].data,e,o)}),(o=>{!function(t,n,e){const o=n=>Q(n,t.cell);ln(n.s,o),ln(n.e,o),n.s.shape&&n.e.shape||e()}(t[J].data,n[yt].data,e),i(o)}))}(n,o,c,t,r)},unselect:v,del:function(){v(),on(o,e.s),on(o,e.e),o.remove()}},e.styles&&i(o,...e.styles),e.s.shape&&en(o,e.s),e.e.shape&&en(o,e.e),c(),o}function ln(t,n){t.shape?(n(an(t.shape).data.position),an(t.shape).drawPosition()):n(t.data.position)}const dn=(t,n,e,o,l,d)=>{n[yt].data[l].shape&&tn(n,l,q(t[J].data,o)),n.style.pointerEvents="none";const h=function(t){let n=null;function e(t){const e=gt(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);C(n[yt].data[l].el,o,(o=>{Y(n[yt].data[l].data.position,t[J].data.scale,o),e()}),(o=>{const i=gt(o),a=i?.getAttribute("data-connect");a?Qt(n,l,i.parentElement,a):Q(n[yt].data[l].data.position,t[J].data.cell),e(),h(),n.style.pointerEvents="unset",d(o)}))};function hn(t){t.el.style.transform=`translate(${t.data.position.x}px, ${t.data.position.y}px) rotate(${pn(t.data.dir)}deg)`}function un(t,n){t.el.firstElementChild.setAttribute("data-evt-index",n.toString())}const pn=t=>"right"===t?180:"left"===t?0:"bottom"===t?270:90,fn=t=>"left"===t?"right":"right"===t?"left":"top"===t?"bottom":"top";const mn=(n,...e)=>e.map((e=>t(n,e))),vn=(t,n,e)=>n<=t&&t<=e,gn=(t,n)=>t.e.el.contains(n)?"e":t.s.el.contains(n)?"s":null,wn=(t,n)=>!t||t===n;function yn(t,n,e){const o=function(t,n){let e=0;return{s:t.split("\n").map(((t,o)=>(e=o,`${0===t.length?".":function(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'")}(t).replaceAll(" "," ")}`))).join(""),c:e}}(e||"",t.x?.baseVal[0]?.value??0);t.innerHTML=o.s,null!=n&&t.y.baseVal[0].newValueSpecifiedUnits(t.y.baseVal[0].SVG_LENGTHTYPE_EMS,o.c>0?n-o.c/2:n)}const xn=(t,n)=>It(n,kn(t,n)),bn=(t,n,e)=>Ft(kn(t,n),e);function kn(t,n){const e=new $n;return c(e,"cmd",(e=>{switch(e.detail.cmd){case"style":Xt(t,n,e.detail.arg);break;case"del":Ot(t,[n]);break;case"copy":Pt(t,[n])}})),e}class $n extends HTMLElement{connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${Wt} ${Jt}
`;{const n=t.getElementById("pnl");let e;l(t,"[data-toggle]",(o=>{const i=t=>{const e=at(this);e.style.top=`${e.getBoundingClientRect().top+window.scrollY+t*n.getBoundingClientRect().height}px`};e&&(i(1),Mn(e,!1));const a=t.getElementById(d(o,"data-toggle"));e!==a?(Mn(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 Mn(t,n){t.style.display=n?"unset":"none"}function Cn(n,e,o,i,a,s,c,r,l=0){const d=Hn(e.id,o,i),h={el:t(d,"text"),vMid:l},u=()=>yn(h.el,h.vMid,e.title);u();const p=zn(n,d,e,i,(t=>{1===t&&f(d)}),h,r??xn,(()=>c(h.el)),a,s,null);return{el:d,cons:i,draw:p.draw,drawTxt:u}}customElements.define("ap-shape-edit",$n);const Hn=(t,n,e)=>{const o=p("g",`${n}\n\t${Object.entries(e).map((t=>``)).join()}`);return o.id=t,o};function zn(t,n,e,s,c,r,l,d,h,f,m){let v,g,w,y;e.styles??=[];const x=()=>Mt([n],!1,!1);function b(t){e.title=t,d()}const k=function(t,n,e,s,c,r,l,d,h){i(n,"hovertrack");const f=I(s),m=new Set;let v,g,w=0;function y(){v?.remove(),v=null}function x(){n.style.transform=`translate(${e.position.x}px, ${e.position.y}px)`;for(const t in s)f[t].position={x:s[t].position.x+e.position.x,y:s[t].position.y+e.position.y};for(const t of m)t[yt].drawPosition()}function b(){w=1,i(n,"select"),l&&(v=function(t,n){const e=p("circle");return e.setAttribute("data-evt-index","2"),i(e,"resizer"),o(e,n),t.append(e),e}(n,l()))}function k(t){r(),w=0,a(n,"select","highlight"),t||y(),g&&(g(),g=null)}const $=t=>{switch(w){case 0:b();break;case 1:w=2,a(n,"select"),i(n,"highlight"),y(),g=H(n,(t=>k()))}c(t,w)},M=(e,o,i)=>{if(o===v){k(!0);const o=Mt([n],!1,!1);return void C(v,e,d,(e=>{h&&h(),y(),b(),Dt(t,o,Mt([n],!1,!1))}))}k();const a=o.getAttribute("data-connect");if(a){const o=rn(t,{id:O(),type:0,s:{shape:{shapeEl:n,connectorKey:a}},e:{data:{dir:fn(f[a].dir),position:q(t[J].data,e)}}});return n.parentNode.append(o),t[J].select(u(e)?null:[o]),o[yt].moveCapture(e,o[yt].data.e.el,!0),void m.add(o)}z(e,i)},z=(o,i)=>{const a=i?null:Mt([n],!1,!1);C(n,o,(n=>{Y(e.position,t[J].data.scale,n),x()}),(o=>{Q(e.position,t[J].data.cell),x(),a?Dt(t,a,Mt([n],!1,!1)):Bt(t,[e.id],Mt([n],!1)),u(o)||b()}))};return n[xt]={data:e,drawPosition:x,click:$,moveCapture:M,unselect:k,del:function(){k();for(const t of m)t[yt].del();n.remove()},paths:m,pathAdd:function(t,n){return m.add(n),f[t]},pathDel:function(t){m.delete(t)}},x}(t,n,e,s,((o,i)=>{c&&c(i),2===i&&(r&&!v&&(w=e.title,g=x(),v=function(t,n,e,o,i){let a=p("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(){yn(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}}(r.el,r.vMid,e.title,b)),y||(y=l(t,n,o)))}),(function(){v?.dispose(),v=null,y?.del(),y=null,g&&w!==e.title&&Dt(t,g,x()),g=null,w=null}),h,f,m);return e.styles&&i(n,...e.styles),{draw:()=>{k(),y?.updPos&&y?.updPos(),v?.draw()}}}function Vn(n,e,o){t(n,e).r.baseVal.value=o}const Ln=(t,n)=>function(t,n,e){const o=g(t);return B(n,e,Math.sqrt(o.x**2+o.y**2))}(t,n,24);function Sn(n,e){e.w??=96,e.h??=48;const a={w:e.w,h:e.h};let s;const c=Cn(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=D(q(t[J].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??=En(t(c.el,"text")),l)))&&(Tn(a,e),r())}),(t=>{const n=En(t,a);Bn(e,n)||(Tn(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,jt(t),Dn(t.el,"main",n.w,n.h,e,o),Dn(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[xt].patch=t=>{const n=Kt(c,t),o=(null!=t.w||null!=t.h)&&!Bn(e,t);o&&Tn(e,t),o?r():n&&c.draw()},c.el}const Bn=(t,n)=>t.w===n.w&&t.h===n.h,Tn=(t,n)=>{t.h=n.h,t.w=n.w};function En(t,n){const e=t.getBBox();return{w:B(n?.w,48,e.width),h:B(n?.h,48,e.height)}}const Dn=(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 Zn(t,n){let e;n.w??=96,n.h??=96;const a=function(t,n,e,o,i,a,s){const c=Hn(n.id,e,o);return{el:c,cons:o,draw:zn(t,c,n,o,null,null,bn,null,i,a,s).draw}}(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=An(n);return e=t.tCanvas,t.bInner}),(i=>s(function(t,n,e,i){const a=D(q(t[J].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=An(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,jt(a),Dn(a.el,"main",n.w,n.h,t.x,t.y),Dn(a.el,"selected",n.w,n.h,t.x,t.y),Dn(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(An(n).tInner):a.draw(),a.el[xt].patch=t=>{const e=Kt(a,t),o=(null!=t.w||null!=t.h)&&!Bn(n,t);o&&Tn(n,t),o?s(An(n).tInner):e&&a.draw()},a.el}function An(t){const n=t=>24*Math.floor(t/2/24),e=n(t.w),o=n(t.h);return{tCanvas:{x:t.position.x-e,y:t.position.y-o},tInner:{x:-e,y:-o},bInner:{x:t.w-e,y:t.h-o}}}const Un=(t,n)=>It(n,new Pn(t,n));class Pn extends HTMLElement{constructor(t,n){super(),this.$=n,this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='
';const n=this.$[xt].data,e=t.getElementById("edit");c(e,"cmd",(t=>{switch(t.detail.cmd){case"style":Xt(this.g,this.$,t.detail.arg);break;case"del":Ot(this.g,[this.$]);break;case"copy":Pt(this.g,[this.$])}}));const o=t=>Nt(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?I(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 Rn(n,e){e.w??=48,e.h??=48,e.a??=1;const o=Cn(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=>s(t)),Un,null);i(o.el,"shtxt"),i(o.el,`ta-${e.a}`);const a=()=>t(o.el,"text"),s=t=>{const n=On(t);Bn(e,n)||(Tn(e,n),h())};let c,r,l;function d(){r=e.w,c=e.h,l=e.a}function h(t){if(function(t,n){const e=n.w/-2,o=n.h/-2;t.cons.left.position.y=o+24,t.cons.left.position.x=e,t.cons.top.position.y=o,t.cons.bottom.position.y=-o,t.cons.right.position.y=o+24,t.cons.right.position.x=-e,jt(t),Dn(t.el,"main",n.w,n.h,e,o),Dn(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").forEach((t=>{t.x.baseVal[0].value=n})),e.position.x+=i,l!==e.a&&(Fn(o.el,l,e.a),l=e.a),r=e.w}c!==e.h&&(a().y.baseVal[0].value=In(e.h),e.position.y+=(e.h-c)/2,c=e.h),o.draw()}return d(),48!==e.w||48!==e.h?h(!0):o.draw(),o.el[xt].patch=t=>{const n=t.position&&!R(e.position,t.position);n&&A(e.position,t.position);let i=!1;t.styles&&(e.styles=t.styles,Yt(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=On(a());Bn(e,t)||(Tn(e,t),s=!0)}null==t.w||Bn(e,t)||(Tn(e,t),d(),s=!0),null!=t.a&&e.a!==t.a&&(Fn(o.el,e.a,t.a),e.a=t.a,l=e.a,s=!0),s?(a().y.baseVal[0].value=In(e.h),h(!0)):n&&o.draw()},o.el}function On(t){const n=t.getBBox();return{w:B(0,48,n.width+12),h:B(0,48,n.height+14)}}customElements.define("ap-rect-txt-settings",Pn);const jn=t=>1===t.a?-16:2===t.a?0:16,In=t=>-t/2+24;function Fn(t,n,e){a(t,`ta-${n}`),i(t,`ta-${e}`)}function Nn(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 Kn(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 _n=(t,n)=>B(n,48,function(t){const n=g(t);return 2*(Math.abs(n.x)+Math.abs(n.y))}(t)-20);async function Yn(t,n,e){return function(t,n,e){let o,i;const a=Xn(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(),Gn(n),e)}function Xn(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 Gn(t){return new DataView((new TextEncoder).encode(t).buffer).getUint32(0)}function Jn(t,n,e,o){const i=t.ownerSVGElement.cloneNode(!0),a=i.querySelector("#canvas");let c;i.style.backgroundImage=null,e?(c=function(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((t,n)=>n.some((n=>s(t,n))))(t,Wn)?(Qn(t),!0):(t.remove(),!1)))):(c=t.getBoundingClientRect(),te(a,qn).forEach((t=>Qn(t))));const r=i.getElementsByTagName("foreignObject");for(;r[0];)r[0].parentNode.removeChild(r[0]);const l=t[J].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=new Image;i.width=n.width*e*window.devicePixelRatio,i.height=n.height*e*window.devicePixelRatio,i.onload=function(){const t=document.createElement("canvas");t.width=i.width,t.height=i.height,t.style.width=`${i.width}px`,t.style.height=`${i.height}px`;const e=t.getContext("2d");e.imageSmoothingEnabled=!1,e.drawImage(i,n.x,n.y,n.width,n.height,0,0,i.width,i.height),URL.revokeObjectURL(i.src),t.toBlob(o,"image/png")},t.width.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,i.width),t.height.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,i.height),i.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 Yn(t,"dgRm",(new TextEncoder).encode(n)):t)))}const Wn=["select","highlight"],qn=["select","highlight","highlight-e","highlight-s"],Qn=t=>{a(t,...qn),t.querySelector(".resizer")?.remove()},te=(t,n)=>t.querySelectorAll(`.${n.join(", .")}`);async function ne(t){const n=await async function(t,n){return Xn(await t.arrayBuffer(),Gn(n))}(t,"dgRm");return n?(new TextDecoder).decode(n):null}async function ee(t,n){const e=`web text/dgrm${N(Mt(n,!0))}`;await async function(t){try{await navigator.clipboard.writeText(t)}catch(t){alert(t)}}(e),window.ClipboardItem&&Jn(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 oe=async()=>ie(await async function(){try{return await navigator.clipboard.readText()}catch(t){return alert(t),null}}());function ie(t){if(null==t||""===t.trim())return null;const n={txt:t};if(t.startsWith("web text/dgrm"))try{n.data=K(t.slice("web text/dgrm".length)),n.txt=null}catch{}return n}function ae(t){return!!re(t)&&(t.c[t.i].u(),t.i-=1,!0)}function se(t){const n=ce(t);return null!==n&&(t.i=n,t.c[n].r(),!0)}const ce=t=>{if(!t)return null;const n=t.i+1;return n<=t.c.length-1?n:null},re=t=>t&&t.i>=0,le=(t,n,e)=>fe(t,(()=>{ue(t,Et(n,e)),zt(t,e),_t(n)}),(()=>{ue(t,Tt(n,e)),zt(t,e)})),de=(t,n,e)=>fe(t,(()=>{ue(t,Tt(n,e)),zt(t,e)}),(()=>{ue(t,Et(n,e)),_t(n)})),he=(t,n,e)=>fe(t,(()=>{ue(t,Zt(e,n)),zt(t,n)}),(()=>{ue(t,Zt(n,e)),zt(t,e)})),ue=(t,n)=>{n[pe]=!0,L(t,n)},pe=Symbol(0);function fe(t,n,e){t[xe]??={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[xe],n,e),we(t)}function me(t,n){n(t[xe])&&(t[J].select(null),we(t))}const ve=t=>me(t,ae),ge=t=>me(t,se);const we=t=>{return L(t,V(ye,(n=t[xe],{u:re(n),r:null!==ce(n)})));var n},ye="dgrmh",xe=Symbol("h");function be(t,n,e,o){if(!n?.data&&!n?.txt)return void dt("Clipboard is empty");t[J].select(Rt(t,function(t){if(!t)return null;const n=new Map,e=t=>{const e=O();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)??(()=>{const e=function(t,n){const e=p("text");yn(e,null,n),e.style.visibility="hidden",t.append(e);const o=On(e);return e.remove(),o}(t,n.txt);return Ct({id:O(),type:3,position:{x:0,y:0},title:n.txt,w:e.w,h:e.h})})(),!0,e,o))}class ke 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,ye,(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=>ve(this.g))),c(e,"click",(t=>ge(this.g)))}}function $e(t,n){Ht(t,n),function(t){const n=t.getBoundingClientRect(),e=window.innerWidth/2,o=window.innerHeight/2;t[J].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[J].scale(a,{x:e,y:o})}(t)}function Me(t,n){const e=function(t,n){async function e(e){if(document.activeElement===t.ownerSVGElement){const o=n();if(o?.length)return e.preventDefault(),ee(t,o),o}return null}async function o(n){const o=await e(n);o&&Ot(t,o)}return document.addEventListener("copy",e),document.addEventListener("cut",o),function(){r(document,"copy",e),r(document,"cut",o)}}(t,n);function o(e){if(!h(e)&&("Delete"===e.key||"Backspace"===e.key)){const e=n();e?.length&&Ot(t,e)}}return c(document,"keydown",o),function(){e(),r(document,"keydown",o)}}function Ce(t,n){return Ve?async function(t,n){try{const e=await window.showSaveFilePicker(Le(n)),o=await e.createWritable();return await o.write(t),await o.close(),e}catch(t){"AbortError"!==t.name&&Se()}}(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)}function He(){return ze?async function(){const[t]=await window.showOpenFilePicker(Le());return{h:t,f:await t.getFile()}}():new Promise(((t,n)=>{!function(t,n){const e=document.createElement("input");e.type="file",e.multiple=!1,e.accept=".png",c(e,"cancel",(t=>n(t)),!0),c(e,"change",(async()=>t(e.files?.length?e.files[0]:null)),!0),e.click(),e.remove()}((n=>{t({f:n})}),(t=>n(t)))}))}customElements.define("ap-history",ke);const ze="showOpenFilePicker"in window,Ve="showSaveFilePicker"in window,Le=t=>({suggestedName:t,types:[{description:"PNG Image",accept:{"image/png":[".png"]}}]}),Se=()=>alert("File not saved");class Be extends HTMLElement{constructor(t,n){super(),this.g=t,this.M=n}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${Wt}Create copy
Copy to clipboard
${qt}Save image
${Jt}Delete
`;const n=()=>at(this)[lt].del();function e(e,o){c(t.getElementById(e),"click",(t=>{n(),o()}))}e("clone",(()=>Pt(this.g,this.M()))),e("copy",(async()=>await ee(this.g,this.M()))),e("save",(()=>Jn(this.g,N(Mt(this.M(),!1)),!0,(async t=>Ce(t,"dgrm.png"))))),e("del",(()=>Ot(this.g,this.M())))}}customElements.define("ap-grp-settings",Be);const Te=Symbol("p");const Ee=(t,n,e)=>{if(e[Te])return;const o=vt(e),i=We(o);if(!i)return void Ie(n);const a=_e(n,o),s=Ge(n);if(s&&a)qe(i,e);else{if(e.shiftKey){if(s){const t=Xe(n);Qe(t),Re(n,t)}return a?Oe(n,i):Re(n,i),void Ne(n,e)}a?ct(new Be(t,(()=>Fe(n))),e.clientX-10,e.clientY-10):(Ie(n),Re(n,i),Ne(n,e))}},De=(t,n,e,o)=>{if(e[Te])return;if(o===t.ownerSVGElement)return void t[J].moveCapture();const i=_e(n,o);if(!i&&!u(e))return void t[J].moveCapture();if(!i&&u(e))return Ie(n),void to(We(o),e,o);const a=Ge(n);i&&a?to(We(o),e,o):i&&!a&&Ze(t,n,e)},Ze=(t,n,e)=>{const o=()=>Ae(n),i=t=>Ue(n,t),a=o();C(t.ownerSVGElement,e,(n=>i((e=>Y(e,t[J].data.scale,n)))),(n=>{i((n=>Q(n,t[J].data.cell))),Dt(t,a,o())}))},Ae=t=>Mt([...t.shapesToMove,...no(t.pathEndsToMove)],!1,!0),Ue=(t,n)=>{t.shapesToMove?.forEach((t=>{n(t[xt].data.position),t[xt].drawPosition()})),t.pathEndsToMove?.forEach((t=>{n(t.data.position),eo(t)[yt].drawPosition()}))},Pe=()=>({shapes:new Set,paths:new Set,pathEnds:new Set,shapesToMove:new Set,pathEndsToMove:new Set}),Re=(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[yt].data.s),t.pathEnds.delete(n.path[yt].data.e);else if(n.pathEnd){const e=eo(n.pathEnd);if(t.paths.has(e))return;t.pathEnds.add(n.pathEnd),i(e,ao(n.pathEnd))}},Oe=(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[yt].data.s),t.pathEnds.delete(n.path[yt].data.e),a(n.path,"highlight","highlight-s","highlight-e");else if(n.pathEnd){const e=eo(n.pathEnd);if(t.paths.delete(e)){a(e,"highlight","highlight-s","highlight-e");const o=io(n.pathEnd);t.pathEnds.add(o),i(e,ao(o))}else t.pathEnds.delete(n.pathEnd),a(e,ao(n.pathEnd))}},je=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(eo(t))))},Ie=t=>{je(t);const n=Xe(t);n&&Qe(n),t.pathEnds.clear(),t.pathEndsToMove.clear(),t.paths.clear(),t.shapes.clear(),t.shapesToMove.clear()},Fe=t=>[...t.shapes,...t.paths,...no(t.pathEnds)],Ne=(t,n)=>{Ke(t);const e=Ye(t);e&&(je(t),qe(e,n))},Ke=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[yt].data.s),n(t[yt].data.e)})),t.pathEnds?.forEach(n)},_e=(t,n)=>[...t.shapes,...[...t.pathEnds].map((t=>t.el)),...t.paths].some((t=>t.contains(n))),Ye=t=>Je(t)?Xe(t):{},Xe=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:E(t.pathEnds)}:{path:E(t.paths)}:{shape:E(t.shapes)},Ge=t=>!!Xe(t),Je=t=>t.shapes.size||t.pathEnds.size||t.paths.size,We=t=>{const n=t[xt]?{shape:t}:t.parentElement[xt]?{shape:t.parentElement}:t.parentElement.parentElement[xt]?{shape:t.parentElement.parentElement}:null;if(n)return n;const e=t[yt]?{path:t}:t.parentElement[yt]?{path:t.parentElement}:null;if(e)return e;const o=oo(t);return o?{pathEnd:o}:null},qe=(t,n)=>{t.shape?t.shape[xt].click(n):t.path?t.path[yt].click(n):t.pathEnd&&eo(t.pathEnd)[yt].click(n,t.pathEnd.el)},Qe=t=>{t.shape?t.shape[xt].unselect():t.path?t.path[yt].unselect():t.pathEnd&&eo(t.pathEnd)[yt].unselect()},to=(t,n,e)=>{t.shape?t.shape[xt].moveCapture(n,e):t.path?t.path[yt].moveCapture(n,e):t.pathEnd&&eo(t.pathEnd)[yt].moveCapture(n,e)},no=t=>[...t].map(eo),eo=t=>t.el.parentNode,oo=t=>{const n=t.hasAttribute("data-key")?t:t.parentElement.hasAttribute("data-key")?t.parentElement:null;if(!n)return null;const e=n.parentNode[yt].data;return e.s.el===n?e.s:e.e},io=t=>{const n=eo(t)[yt].data;return n.s===t?n.e:n.s},ao=t=>eo(t)[yt].data.s===t?"highlight-s":"highlight-e";const so=(t,n,e,o,i)=>t.x<=o&&o<=t.x+n&&t.y<=i&&i<=t.y+e,co={iceServers:[{urls:"stun:stun3.l.google.com:19302"}]},ro=`${ht}m/`;const lo=()=>{if(!document.cookie.includes("dgrma=1"))return null;const t=K(localStorage.getItem("dgrm"));return t&&new Date(t.e)>new Date?t:null},ho=()=>{const t=lo()?.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},uo=t=>document.getElementById("menu").bage(t),po=t=>{const n=fo(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)",v(e),e},fo=t=>`hsl(${[...t].reduce(((t,n)=>n.charCodeAt(0)+((t<<5)-t)),0)%360}, 97%, 47%)`,mo=(t,n,e,o)=>{const i=((t,n,e)=>({x:t.position.x+n*t.scale,y:t.position.y+e*t.scale}))(t[J].data,e,o);n.style.transform=`translate(${i.x}px, ${i.y}px)`},vo=(t,n,e)=>{const o=t=>e(N(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])));let l=!0;const d=c(t.ownerSVGElement,"pointermove",S((e=>{if(l){const i=W(t[J].data,e.clientX,e.clientY);o([6,n,i.x,i.y])}}),60));return()=>{l=!1,r(t,"dgrmc",i),r(t,"dgrmu",a),r(t,"dgrmd",s),r(t.ownerSVGElement,"pointermove",d)}};async function go(t,n,e,o){const i=ho();if(null==i)return e(),null;ot(!0);const a=O(),s=new Map,c=await async function(t,n,e,o,i){const a=await async function(t,n,e){const o=new RTCPeerConnection(co),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)=>x(`${ro}o`,{m:t,c:n,s:e}))(n,t,a.localDescription.sdp);const s=_(3e3,15e3,(async()=>{try{const e=await(async(t,n)=>(await y(`${ro}a?m=${t}&c=${n}`)).text())(n,t);e&&(s(),a.setRemoteDescription({sdp:e,type:"answer"}))}catch{c(),i()}}),i),c=()=>{s(),a.close(),ot(!1)};return c}(a,n,((t,n)=>t.send(N([0,a,i]))),((n,e)=>{const o=K(e.data);switch(o[0]){case 6:mo(t,s.get(o[1]),o[2],o[3]);break;case 4:zt(t,o[1]);break;case 5:_t(o[1]);break;case 0:s.set(o[1],po(o[2]));break;case 7:s.get(o[1]).remove(),s.delete(o[1]);break;case 2:$e(t,o[1]),l(n);break;case 3:t[J].move(o[1],o[2],1),l(n)}}),(()=>{d(),o()}));let r;const l=n=>{r=vo(t,a,(t=>n.send(t))),ot(!1)},d=()=>{s.forEach((t=>t.remove())),r&&r(),c(),ot(!1)};return d}async function wo(t,n,e){const o=ho();if(null==o)return n(),null;const i=O(),a=O(),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=O();await((t,n)=>x(`${ro}c`,{m:t,k:n}))(t,a);const s=new Set,c=_(5e3,72e5,(async()=>{try{const i=await(async(t,n)=>{const e=await(await y(`${ro}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(co);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)=>x(`${ro}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(N([0,a,o]));const c=$t(t);e.send(N(kt(c)?[3,t[J].data.position.x,t[J].data.position.y]:[2,c])),s.forEach((t=>e.send(N([0,t.i,t.n]))))}),((n,e)=>{const o=K(e.data);switch(o[0]){case 6:mo(t,s.get(n).c,o[2],o[3]),c(e.data,n);break;case 4:zt(t,o[1]),c(e.data,n);break;case 5:_t(o[1]),c(e.data,n);break;case 0:s.set(n,{i:o[1],n:o[2],c:po(o[2])}),c(e.data,n)}}),((t,n)=>{const e=s.get(t);c(N([7,e.i]),t),e.c.remove(),s.delete(t)}),(()=>{d(),e()})),l=vo(t,a,c),d=()=>{l(),s.forEach((t=>t.c.remove())),r()};return{dispose:d,id:i}}function yo(t,e,o){const i=m(`
Live collaboration in progress
`),a=rt(i),s=(t,e)=>c(n(i,t),"click",(t=>{a[lt].del(),e()}));s("mstop",e),s("mshare",(async n=>{await navigator.clipboard.writeText($("m",t)),dt("Link copied to clipboard")}))}const xo=t=>{t[ko]?.dispose(),t[ko]=void 0,uo(!1)},bo=t=>{xo(t),alert("Live collaboration disabled")},ko=Symbol(0),$o=async t=>{const n=Lo(t);n?.f?Eo(t,(async t=>{const e=await n.f.createWritable();await e.write(t),await e.close(),Do()})):n?.k?await To(t,n.k):Mo(t)},Mo=t=>{const e=Lo(t)?.n;lo()?function(t,e){const o=m(`
`),i=rt(o,"mdm"),a=(t,e)=>c(n(o,t),"click",(t=>{i[lt].del(),e()})),s=()=>Bo(n(o,"nm").value);a("dvc",(n=>Co(t,s()))),a("cl",(async n=>{try{const n=s(),e=Lo(t);if(e?.k&&e.n===n)return void await To(t,e.k);const o=mt();Ho(t,o,await async function(t,n,e){const o=new URL(`${ut}a/c`);return o.searchParams.set("k",t),o.searchParams.set("n",n),(await x(o.toString(),e)).text()}(o,n,$t(t))),Do()}catch{Se()}}))}(t,e):Co(t,e)},Co=(t,n)=>Eo(t,(async e=>{const o=Ce(e,n??"dgrm.png");if(o){const n=await o;n&&zo(t,n)}}));const Ho=(t,n,e)=>{t[Zo]={k:n,n:e},So(e)},zo=(t,n)=>{const e=j(n.name);t[Zo]={f:n,n:e},So(e)},Vo=t=>{t[Zo]=void 0,So()},Lo=t=>t[Zo],So=t=>{document.title=t??"DGRM - Flowchart editor"},Bo=t=>t?.replace(/[/\\?%*:|"<>]/g,"-"),To=async(t,n)=>{await pt(n,$t(t)),Do()},Eo=(t,n)=>Jn(t,N($t(t)),null,n),Do=()=>dt("Saved"),Zo=Symbol(0);function Ao(t){L(t,V("dgrmn")),function(t){for(t[J].select(null);t.firstChild;)(t.firstChild[xt]||t.firstChild[yt]).del();t[J].move(0,0,1)}(t),function(t){var n;(n=t[xe])&&(n.c=[],n.i=-1),we(t)}(t),Vo(t)}async function Uo(t){lo()?async function(t){let e;ot(!0);try{e=await(async()=>{const t=await y(`${ut}a/l`);let n;try{n=await t.json()}catch{}return n})()}catch{return alert("Error"),void ot(!1)}const o=rt(m(`
${F(e)?Object.entries(e).map((t=>``)).join(""):'
No cloud items
'}
`,"display: flex; flex-flow: column; height: 100%; overflow-y: overlay;"),"mdh");i(at(o),"mdhc"),c(n(o,"dvc"),"click",(async n=>{o[lt].del(),await Po(t)}));const a=t=>o.querySelector(`[dgr='${t}']`),s=async t=>{try{ot(!0),await t()}catch{alert("Error")}finally{ot(!1)}};l(o,"[edt]",(o=>{const i=o.currentTarget.getAttribute("edt");!function(t,e,o,i){const a=e.currentTarget.getBoundingClientRect(),s=st(m('Rename Delete'),a.x-30,a.y+70),r=(t,e)=>c(n(s,t),"click",(t=>{s[lt].del(),e()}));r("rn",(e=>function(t,e){const o=rt(m(`
`),"mdm",!0);c(n(o,"sv"),"click",(t=>{o[lt].del(),e(Bo(n(o,"nm").value))}))}(t.n,o))),r("del",(t=>i()))}(e[i],o,(t=>{e[i].n!==t&&s((async()=>{e[i].n=await ft(i,t),a(i).querySelector("[nm]").textContent=e[i].n}))}),(()=>{confirm("Delete?")&&s((async()=>{await(async t=>x(`${ut}a/d/${t}`))(i),delete e[i],a(i).remove(),i===Lo(t)?.k&&Vo(t)}))}))})),ot(!1)}(t):Po(t)}async function Po(t){try{let n;try{n=await He()}catch(t){if("AbortError"===t.name)return;throw t}await Ro(t,n.f),n.h?zo(t,n.h):Vo(t)}catch(t){console.log(t),Oo()}}async function Ro(t,n){const e=await ne(n);e?(Ao(t),$e(t,K(e))):Oo()}const Oo=()=>alert("File cannot be read. Use the exact image file you got from the application.");class jo extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=lo(),e=t||ze,o=this.attachShadow({mode:"closed"});o.innerHTML=`
New
Open
${e?'
Save
':""}
${qt}Save${Ve||e?" as":""}
Share link to diagram
Live collaboration
${t?t.n:"Log in"}
`;const i=t=>n(o,t);this.C=i;const a=i("options");function s(){a.style.visibility="visible"===a.style.visibility?"hidden":"visible"}function l(t,n){c(i(t),"click",(async t=>{s(),await n(t)}))}c(i("menu"),"click",(t=>{s(),c(document,"pointerdown",(t=>{this.contains(t.target)||(a.style.visibility="hidden")}),!0)})),c(i("menu2"),"click",(t=>s())),l("new",(()=>{Ao(this.g)})),e&&l("save",(async()=>await $o(this.g))),l("saveas",(()=>Mo(this.g))),l("open",(async()=>await Uo(this.g))),l("collab",(async t=>await async function(t){const n=()=>xo(t),e=()=>bo(t);try{switch(t[ko]?.clientType){case 1:yo(t[ko].id,n);break;case 2:yo(t[ko].id,n,!0);break;default:{const o=await wo(t,n,e);if(!o)return;c(t,"dgrmn",e,!0),t[ko]={clientType:1,id:o.id,dispose:()=>{r(t,"dgrmn",e),o.dispose()}},uo(!0),yo(t[ko].id,n);break}}}catch{e()}}(this.g))),l("login",(t=>function(){const t=lo();rt(m(t?`
${t.n}
${t.l}
`:'
Welcome Back
'))}())),l("link",(async t=>await Io(this.g)))}bage(t){const n=t?i:a;n(this.C("menu"),"bage"),n(this.C("collab"),"bage")}}customElements.define("ap-menu",jo);const Io=async t=>{ot(!0);const n=$t(t);if(kt(n))return alert("Diagram is empty"),void ot(!1);const e=mt();await navigator.clipboard.writeText($("k",e)),await((t,n)=>x(`${ut}${t}`,n))(e,n),dt("Link copied to clipboard"),ot(!1)};class Fo extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='';const n=t.getElementById("menu");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)}handleEvent(t){switch(t.type){case"pointermove":if(!this.H){const n=document.elementFromPoint(t.clientX,t.clientY);if(n===this.V)return;this.L===this.V&&this.g.ownerSVGElement.setPointerCapture(t.pointerId),this.V=n}break;case"pointerleave":if(this.H=!0,null!=this.S){const n=No(this.g,this.S,!1,t.clientX,t.clientY)[0];u(t)||this.g[J].select([n]),n[xt]?n[xt]?.moveCapture(t,n,!0):n[yt]?.moveCapture(t,n,!0)}this.B();break;case"pointerdown":this.S=parseInt(d(t,"data-cmd-arg")),this.L=document.elementFromPoint(t.clientX,t.clientY),this.V=this.L,this.H=null;break;case"pointerup":this.B();break;case"click":this.g.ownerSVGElement.focus(),this.g[J].select(No(this.g,parseInt(d(t,"data-cmd-arg")),!0))}}B(){this.S=null,this.L=null,this.V=null}}customElements.define("ap-menu-shape",Fo);const No=(t,n,e,o,i)=>Rt(t,Ct(0===n?{id:O(),type:0,s:{data:{dir:"right",position:{x:-24,y:0}}},e:{data:{dir:"right",position:{x:24,y:0}}}}:{id:O(),type:n,position:{x:0,y:0},title:"Title"}),e,o,i);const Ko=document.getElementById("canvas");var _o,Yo,Xo;Ko[J]={data:{position:{x:0,y:0},scale:1,cell:24},shapeMap:function(n){const e=t=>(n.append(t),t);return{0:{create:t=>e(rn(n,t))},1:{create:i=>e(function(n,e){e.r??=48;let i,a=e.r;const s=Cn(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=Z(e.position,-e.r),{x:e.r,y:e.r})),(r=>{let l=q(n[J].data,r).x-i.x;l<48&&(l=48);const d=48*Math.round(l/48)/2;e.r!==d&&Ln(t(s.el,"text"))<=d&&(e.r=d,e.position.x=i.x+d,e.position.y=i.y+d,c(),a=d);const h=l-e.r;o(r.target,{x:h,y:h})}),(t=>{const n=Ln(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,jt(s),Vn(s.el,"outer",e.r+24),Vn(s.el,"main",e.r),s.draw()}return 48!==e.r?c():s.draw(),s.el[xt].patch=t=>{const n=Kt(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:{create:t=>e(Sn(n,t))},3:{create:t=>e(Rn(n,t))},4:{create:a=>e(function(n,e){e.w??=96;let a,s=e.w;const c=Cn(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=Z(e.position,-t),{x:t,y:t}}),(i=>{let l=q(n[J].data,i).x-a.x;l<48&&(l=48);const d=48*Math.round(l/48);if(e.w!==d&&_n(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 h=l-e.w/2;o(i.target,{x:h,y:h})}),(t=>{const n=_n(t,s);n!==e.w&&(e.w=n,r())}));function r(){const t=Kn(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,jt(c);const n=Kn(e.w,9);Nn(c.el,"main",n),Nn(c.el,"border",n),Nn(c.el,"outer",Kn(e.w,-24)),c.draw()}return i(c.el,"shrhomb"),96!==e.w?r():c.draw(),c.el[xt].patch=t=>{const n=Kt(c,t),o=null!=t.w&&e.w!==t.w;o&&(e.w=t.w),o?r():n&&c.draw()},c.el}(n,a))},5:{create:t=>{return e=Zn(n,t),n.prepend(e),e;var e}}}}(Ko)},Ko.ownerSVGElement.addEventListener("contextmenu",(async t=>{h(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[X]=e?t.clientX-e:0,t[G]=o?t.clientY-o:0,e=t.clientX,o=t.clientY):(t[X]=t.movementX,t[G]=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=>{h(n)||be(t,ie(n.clipboardData.getData("text/plain")))})),c(document,"keydown",(n=>{!n.ctrlKey&&!n.metaKey||n.repeat||h(n)||("KeyZ"===n.code?ve(t):"KeyY"===n.code&&ge(t))}))}(Ko),function(t){const n=t.ownerSVGElement;let e,a,s,l,d;const h=()=>{d?.remove(),d=null};let u=!1;function f(t){if(t[Te]||!s)return void v();t[Te]=!0,u&&(h(),u=!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&&!u){n[Te]=!0;const e=n=>so(W(t[J].data,l.x,l.y),s.width.baseVal.value/t[J].data.scale,s.height.baseVal.value/t[J].data.scale,n.x,n.y);!function(t,n,e,o){const i=Pe(),a=t=>Re(i,t),s=t=>t.shape&&e(t.shape.shapeEl)||o(t);for(const t of n)if(t[xt])e(t)&&a({shape:t});else if(t[yt]){const n=s(t[yt].data.s),e=s(t[yt].data.e);n&&e?a({path:t}):n?a({pathEnd:t[yt].data.s}):e&&a({pathEnd:t[yt].data.e})}t[J].selectedSet(i)}(t,t.children,(t=>e(t[xt].data.position)),(t=>e(t.data.position)))}v()}function v(){clearTimeout(e),e=null,s?.remove(),s=null,h(),u=!1,r(n,"pointermove",f),r(n,"wheel",v),r(n,"pointercancel",v),r(n,"pointerup",m)}c(n,"pointerdown",(r=>{if(!r.isPrimary||vt(r)!==n)return void v();c(n,"pointermove",f),c(n,"wheel",v,!0),c(n,"pointercancel",v,!0),c(n,"pointerup",m,!0);const h=()=>{t[J].selectedSet(null),u=!0,n.setPointerCapture(r.pointerId),a={x:r.clientX,y:r.clientY},l={x:r.clientX,y:r.clientY},s=p("rect"),s.style.cssText="rx:8px; fill: rgb(108 187 247 / 51%)",o(s,l),n.append(s)};2!==r.button?e=setTimeout((t=>{h(),d=p("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):h()}))}(Ko),function(t){const n=t.ownerSVGElement;let e,o,i=Pe();c(n,"pointerdown",(a=>{if(!a.isPrimary)return r(n,"pointermove",e),r(n,"pointerup",o),void t[J].moveCapture();o=o=>{r(n,"pointermove",e),Ee(t,i,o)},c(n,"pointerup",o,!0),e=e=>{r(n,"pointerup",o),De(t,i,e,vt(a))},0===a.button&&c(n,"pointermove",e,!0)})),Me(t,(()=>Fe(i)));const a=t=>{Ie(i),i=t??Pe(),Ne(i,null)};t[J].selectedSet=a,t[J].select=t=>{const n=Pe();t?.forEach((t=>Re(n,We(t)))),a(n)}}(Ko),function(t){const n=t[J].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 h(t){s=null,l=null,i?.id===t.pointerId&&(i=null),a?.id===t.pointerId&&(a=null),i||a||(r(document,"pointermove",u),r(document,"pointercancel",h),r(document,"pointerup",h),d=!1)}function u(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=nt(t,n)),a?.id===t.pointerId&&(a=nt(t,n))}c(t.ownerSVGElement,"pointerdown",(t=>{!i&&!t.isPrimary||i&&a||(i||(c(document,"pointermove",u),c(document,"pointercancel",h),c(document,"pointerup",h),i=nt(t,n)),a||i?.id===t.pointerId||(a=nt(t,n)))})),t[J].moveCapture=()=>{d=!0}}(t,n,i,o),t.ownerSVGElement.addEventListener("wheel",(t=>{t.preventDefault();const e=t.deltaY||t.deltaX,o=Math.abs(e)<50?.05:.25;i(n.scale+(e<0?o:-o),tt(t))})),t[J].move=function(t,e,i){n.position.x=t,n.position.y=e,n.scale=i,o()},t[J].scale=i}(Ko),function(t){if(!navigator.clipboard.readText)return void t.ownerSVGElement.addEventListener("contextmenu",(async n=>{t.ownerSVGElement===vt(n)&&n.preventDefault()}));const n=t.ownerSVGElement,e=t.ownerDocument;let o,i,a,s;function l(n){r(e,"pointermove",u),r(e,"pointerup",d),a||(a=function(t,n,e,o){const i=m('
Paste
');c(i,"click",(async i=>{be(e,await oe(),t,n),o()}));return{del:ct(i,t-10,n-10)[lt].del,el:i}}(n.clientX,n.clientY,t,d),c(e,"pointerdown",h))}function d(){clearTimeout(o),o=null,a?.del(),a=null,r(e,"pointerdown",h),r(e,"pointerup",d),r(e,"wheel",d),r(e,"keydown",d),r(e,"pointerup",f),r(e,"pointercancel",f),r(e,"pointermove",p),i=!1,s=!1}function h(t){a.el.contains(t.target)||d()}function u(){d(),i=!0}function p(){s||d(),i=!0}function f(){s=!0}c(n,"pointerdown",(async function(t){t.isPrimary&&vt(t)===n?(d(),c(e,"pointermove",u,!0),c(e,"pointerup",d,!0),c(e,"wheel",d,!0),c(e,"keydown",d,!0),2!==t.button&&(o=setTimeout((n=>{l(t),c(e,"pointerup",f,!0),c(e,"pointercancel",f,!0),c(e,"pointermove",p,!0)}),500))):d()})),n.addEventListener("contextmenu",(async t=>{n===vt(t)&&(t.preventDefault(),i||l(t))}))}(Ko),(t=>{const n=(n,e)=>c(t,n,(t=>{t[pe]||e(t)}));n("dgrmc",(n=>le(t,n.detail.newIds,n.detail.shapesToCreateData))),n("dgrmd",(n=>de(t,n.detail.delIds,n.detail.shapesToDelData))),n("dgrmu",(n=>he(t,n.detail.shapesBeforeUpdData,n.detail.shapesToUpdData)))})(Ko),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&&"image/png"===n.dataTransfer.items[0].type)if("getAsFileSystemHandle"in n.dataTransfer.items[0]){const e=await n.dataTransfer.items[0].getAsFileSystemHandle();await Ro(t,await e.getFile()),zo(t,e)}else{const e=n.dataTransfer.items[0].getAsFile();await Ro(t,e),So(j(e.name))}else Oo()}))}(Ko),(t=>{var n,e;n=S((async()=>await $o(t)),500),e=()=>Mo(t),document.addEventListener("keydown",(t=>{!t.ctrlKey&&!t.metaKey||t.repeat||"KeyS"!==t.code||(t.preventDefault(),t.shiftKey?e():n())}))})(Ko);{const t=new URL(window.location.href),n=n=>t.searchParams.get(n);n("usr")&&(_o=n("usr"),Yo=n("usrn"),Xo=parseInt(n("expr")),localStorage.setItem("dgrm",N({l:_o,n:Yo,e:Xo})),M("usr","usrn","expr"))}(t=>{const n=new jo(t);n.id="menu",v(n)})(Ko),v(new ke(Ko)),v(new Fo(Ko));{const t=new URL(window.location.href),e=n=>t.searchParams.get(n);if(e("k")){ot(!0);try{const t=e("k"),n=await(async t=>{const n=await y(`${ut}${t}`);return{e:n.headers.has("X-Dgrm-edit"),n:decodeURIComponent(n.headers.get("X-Dgrm-name")),d:await n.json()}})(t);$e(Ko,n.d),n.e&&Ho(Ko,t,n.n)}catch{alert("The link is broken or you do not have permission. Try to relogin.")}M("k"),ot(!1)}else if(e("m")){const t=e("m");M("m"),await async function(t,n){const e=()=>bo(t);try{const o=await go(t,n,(()=>xo(t)),e);if(!o)return;c(t,"dgrmn",e,!0),t[ko]={clientType:2,id:n,dispose:()=>{r(t,"dgrmn",e),o()}},uo(!0)}catch{e()}}(Ko,t)}else lo()||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),v(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),h=t=>"TEXTAREA"===t.target.tagName.toUpperCase(),u=t=>"mouse"===t.pointerType;function p(t,n){const e=document.createElementNS("http://www.w3.org/2000/svg",t);return n&&(e.innerHTML=n),e}const f=t=>t.parentElement.appendChild(t),m=(t,n)=>{const e=document.createElement("div");return t&&(e.innerHTML=t),e.style.cssText=n,e},v=(...t)=>document.body.append(...t);function g(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}],y=async(t,n)=>{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},x=(t,n)=>b("POST",t,n),b=(t,n,e)=>y(n,{method:t,headers:{"Content-Type":"application/json;charset=utf-8"},body:e?N(e):null}),k=(t,n)=>{const e=new URL(window.location.href);return e.searchParams.set(t,n),e.toString()},$=(...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 C=(t,n)=>z(t.ownerSVGElement,t,n),H=(t,n,e,o)=>z(t,n,e,o,!0);function z(t,n,e,o,i){const a=()=>{r(t,"pointercancel",s),r(t,"wheel",s),r(t,"pointerdown",s),r(document,"keydown",l,!0),r(document,"paste",l,!0)},s=t=>{o&&o(t),n.contains(t.target)||(a(),e(t))};c(t,"pointercancel",s),c(t,"wheel",s),c(t,"pointerdown",s);const l=t=>{n.contains(t.target)||t.stopImmediatePropagation()};return i&&(document.addEventListener("keydown",l,{capture:!0,passive:!0}),document.addEventListener("paste",l,{capture:!0,passive:!0})),a}const L=(t,n)=>new CustomEvent(t,{detail:n,cancelable:!1}),V=(t,n)=>t.dispatchEvent(n),S=(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}function T(t,n){for(let e=t.length-1;e>=0;--e)n(t[e])}const E=t=>t.values().next().value;function D(t,n,e){const o=e??1;return t.x+=o*n.x,t.y+=o*n.y,t}const Z=(t,n)=>({x:t.x+n,y:t.y+n}),A=(t,n)=>U(t,n.x,n.y),U=(t,n,e)=>(t.x=n,t.y=e,t),P=t=>({x:t.x,y:t.y}),R=(t,n)=>t?.x===n?.x&&t?.y===n?.y;function O(){const t=new Uint8Array(4);return window.crypto.getRandomValues(t),Array.from(t,(t=>t.toString(16).padStart(2,"0"))).join("")}const j=t=>t?.substring(0,t.lastIndexOf("."))||t,I=t=>K(N(t)),F=t=>t&&!!Object.keys(t).length,N=t=>JSON.stringify(t),K=t=>JSON.parse(t),_=(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 Y(t,n,e){t.x+=e[X]/n,t.y+=e[G]/n}const X=Symbol("movementX"),G=Symbol("movementY"),J=Symbol("Canvas"),q=(t,n,e)=>({x:(n-t.position.x)/t.scale,y:(e-t.position.y)/t.scale}),W=(t,n)=>q(t,n.clientX,n.clientY);function Q(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 tt(t){return{x:t.clientX,y:t.clientY}}function nt(t,n){return{id:t.pointerId,pos:tt(t),shift:{x:n.position.x-t.clientX,y:n.position.y-t.clientY}}}let et;function ot(t){t&&!et?(et=m("","z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; background: #fff; opacity: 0"),et.classList.add("blnk"),v(et)):t||(et?.remove(),et=null)}function it(t,n,e){const o=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 i(t,n){o&&(o.style.left=`${t}px`,o.style.top=window.scrollY+n-o.getBoundingClientRect().height+"px")}return o.setAttribute("ap-modal",""),o.append(t),v(o),null!=n?i(n,e):o.style.transform="translate(-50%, -30%)",o[lt]={position:i,contains:t=>o?.contains(t),del:()=>{o?.remove()}},o}const at=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]");function st(t,n,e,o){const i=m(null,"z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; "+(o?"background-color: rgba(52,71,103,.05);":""));v(i);const a=it(t,n,e);i.append(a);const s=H(i,a,(t=>{"pointerdown"===t.type?c(i,"click",(t=>{t.stopImmediatePropagation(),l()}),!0):l()}),(t=>t.stopImmediatePropagation())),r=a[lt].del,l=()=>{r(),s(),i.remove()};return a[lt].del=l,a}function ct(t,n,e){const o=it(t,n,e),i=o[lt].del,a=H(document,o,(t=>s())),s=()=>{i(),a()};return o[lt].del=s,o}function rt(t,e,o){const i=m('',"display: flex; flex-flow: column; padding: 15px;");i.className=e??"mds",i.append(t);const a=o?st(i,null,null,!0):ct(i);return c(n(i,"ap-close"),"click",(t=>a[lt].del())),a}const lt=Symbol(0);function dt(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",v(n),setTimeout((t=>n.remove()),2800)}const ht="https://api.dgrm.net/api/",ut=`${ht}s/`;const pt=async(t,n)=>b("PUT",`${ut}a/u/${t}`,n),ft=async(t,n)=>{return(await(e=`${ut}a/n/${t}`,o=n,y(e,{method:"PUT",body:o}))).text();var e,o};function mt(){const t=new Date;return`${t.getUTCFullYear()}${(t.getUTCMonth()+1).toString().padStart(2,"0")}${O()}`}function vt(t){return wt(t).find((t=>!t.hasAttribute("data-evt-no")))}function gt(t){return wt(t)[0]}function wt(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 yt=Symbol("path"),xt=Symbol("shape"),bt="1.2",kt=t=>!F(t.s),$t=t=>Mt([...t.children],!1);function Mt(t,n,e){const o={v:bt,s:{}},i=new Map,a=t=>{let e=i.get(t);if(!e){const a=I(t[xt].data);n&&(a.id=O(),e=a.id),i.set(t,e),o.s[a.id]=a}return e},s=i=>{if(o.s[i[yt].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:I(o.data)}):o.shape?(e&&a(o.shape.shapeEl),{s:o.shape.shapeEl[xt].data.id,k:o.shape.connectorKey}):{p:I(o.data)};var i},c=i[yt].data,r={id:n?O():c.id,type:0,s:s(c.s),e:s(c.e)};c.styles&&(r.c=I(c.styles)),o.s[r.id]=r};for(const n of t)n[xt]?(a(n),e&&n[xt].paths?.forEach(s)):s(n);return o}function Ct(t){return{v:bt,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 Ht=(t,n)=>zt(t,"1.1"===n.v?function(t){const n={v:bt,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 zt(t,n){if(n.v!==bt)return alert("Wrong format"),null;const o=new Map;function i(n){let e=o.get(n);return e||(e=Vt(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)if(0===n.s[e].type)s.push(St(t,n.s[e],a));else i(n.s[e]);return[...o.values(),...s]}function Lt(t,n){const o=e(n.id);return o?((o[xt]||o[yt]).patch(n),o):t[J].shapeMap[n.type].create(n)}const Vt=(t,n)=>Lt(t,I(n)),St=(t,n,e)=>Lt(t,function(t,n){const e=t=>t.p?{data:{dir:t.p.dir,position:P(t.p.position)}}:{shape:{shapeEl:n(t.s),connectorKey:t.k}};return{type:0,id:t.id,styles:t.c,s:e(t.s),e:e(t.e)}}(n,e));const Bt=(t,n,e)=>V(t,Tt(n,e)),Tt=(t,n)=>L("dgrmc",{newIds:t,shapesToCreateData:n}),Et=(t,n)=>L("dgrmd",{delIds:t,shapesToDelData:n}),Dt=(t,n,e)=>V(t,Zt(n,e)),Zt=(t,n)=>L("dgrmu",{shapesBeforeUpdData:t,shapesToUpdData:n});function At(t,n,e,o){const i=D(((t,n,e)=>{const o=q(t,n,e);return Q(o,t.cell),o})(t[J].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 Ut(t,(t=>{t&&(n.x>t.x&&(n.x=t.x),n.y>t.y&&(n.y=t.y),e.x{n&&Q(D(n,i),t[J].data.cell)}))}function Ut(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 Pt=(t,n)=>t[J].select(Rt(t,Mt(n,!0),!0));function Rt(t,n,e,o,i){return At(t,n,null==o?window.innerWidth/2:o,null==i?window.innerHeight/2:i),e&&Bt(t,Object.keys(n.s),I(n)),t[J].select(null),zt(t,n)}function Ot(t,n){((t,n,e)=>{V(t,Et(n,e))})(t,n.map((t=>(t[xt]||t[yt]).data.id)),Mt(n,!1,!0)),n.forEach((t=>(t[xt]||t[yt]).del()))}function jt(n){for(const e in n.cons)o(t(n.el,e),n.cons[e].position)}function It(t,n){const e=t.getBoundingClientRect(),o=ct(n,e.left+10,e.top+10);return{del:o[lt].del,updPos:()=>{const n=t.getBoundingClientRect();o[lt].position(n.left+10,n.top+10)}}}const Ft=(t,n)=>({del:ct(t,n.clientX-66,n.clientY-10)[lt].del}),Nt=(t,n,e)=>{const o=Mt([n],!1,!1);(n[xt]??n[yt]).patch(e),Dt(t,o,Mt([n],!1,!1))};function Kt(t,n){!function(t,n){const e=t.el[xt].data;null!=n.title&&e.title!==n.title&&(e.title=n.title,t.drawTxt());n.styles&&(e.styles=n.styles,Yt(t.el))}(t,n);const e=n.position&&!R(t.el[xt].data.position,n.position);return e&&A(t.el[xt].data.position,n.position),e}const _t=t=>t.forEach((t=>{const n=e(t);n&&(n[xt]||n[yt]).del()}));function Yt(t){const n=(t[xt]??t[yt]).data;n.styles||(n.styles=[]),T(t.classList,(n=>{n?.startsWith("d-")&&t.classList.remove(n)})),i(t,...n.styles)}const Xt=(t,n,e)=>{const o=(n[xt]??n[yt]).data;o.styles||(o.styles=[]);const i={styles:I(o.styles)};Nt(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))};class Gt extends HTMLElement{constructor(t,n){super(),this.o=n,this.g=t}connectedCallback(){const t=t=>this.o[yt].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":Xt(this.g,this.o,t.detail.arg);break;case"del":Ot(this.g,[this.o]);break;case"copy":Pt(this.g,[this.o])}})),l(n,"[data-cmd]",(t=>{const n=d(t,"data-cmd-arg"),e={styles:this.o[yt].data.styles?I(this.o[yt].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")),Nt(this.g,this.o,e)}))}}customElements.define("ap-path-settings",Gt);const Jt='',qt='',Wt='';function Qt(t,n,e,o){const i=sn(t,n);i.shape={shapeEl:e,connectorKey:o},en(t,i)}function tn(t,n,e){const o=sn(t,n);o.shape.shapeEl!==cn(t,o)&&on(t,o),o.shape=null,o.data={dir:o.data.dir,position:e}}function nn(t,n){const e=t[yt].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&&tn(t,o,a.data.position):function(t,n,e,o){const i=sn(t,n);if(i.shape.shapeEl===e&&i.shape.connectorKey===o)return;i.shape.shapeEl!==e&&i.shape.shapeEl!==cn(t,i)&&on(t,i);i.shape={shapeEl:e,connectorKey:o},en(t,i)}(t,o,a.shape.shapeEl,a.shape.connectorKey):null!=a.shape?Qt(t,o,a.shape.shapeEl,a.shape.connectorKey):A(i.data.position,a.data.position)};o("s"),o("e")}function en(t,n){n.data=an(n.shape).pathAdd(n.shape.connectorKey,t)}const on=(t,n)=>an(n.shape)?.pathDel(t),an=t=>t?.shapeEl[xt],sn=(t,n)=>t[yt].data[n],cn=(t,n)=>(t[yt].data.e===n?t[yt].data.s:t[yt].data.e).shape?.shapeEl;function rn(n,e){const o=p("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=mn(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 vn(e,-.8,.8)?"left":vn(e,.8,2.4)?"top":vn(e,2.4,3.2)||vn(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=fn(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))),hn(e.s),hn(e.e)}let r,l;let d,h=0;function m(t){switch(h){case 0:h=1,i(o,"s"===l?"highlight-s":"e"===l?"highlight-e":"select"),un(e.s,2),un(e.e,2),f(o);break;case 1:h=2,d=C(o,(t=>v(!0))),function(t){r||(r=Ft(new Gt(n,o),t))}(t)}}function v(t){t||(l=null),h=0,a(o,"select","highlight-s","highlight-e"),un(e.s,1),un(e.e,1),r?.del(),r=null,d&&(d(),d=null)}return o[yt]={data:e,drawPosition:c,click:(t,n)=>{if(0===h||l){const o=n||t?gn(e,n??vt(t)):null;wn(l,o)||v(),l=o}m(t)},patch:t=>{e.styles&&(e.styles=t.styles,Yt(o)),t.s&&nn(o,t),c()},moveCapture:(t,i,a)=>{v(!0);const s=a?null:Mt([o],!1,!0),r=t=>{v(!0),s?Dt(n,s,Mt([o],!1,!0)):Bt(n,[e.id],Mt([o],!1,!0)),u(t)||m()},l=gn(e,i);l?dn(n,o,c,t,l,r):function(t,n,e,o,i){M(n,o,(o=>{!function(t,n,e,o){const i=n=>Y(n,t.scale,o);ln(n.s,i),n.s.shape?.shapeEl&&n.s.shape?.shapeEl===n.e.shape?.shapeEl||ln(n.e,i);n.s.shape||n.e.shape||e()}(t[J].data,n[yt].data,e,o)}),(o=>{!function(t,n,e){const o=n=>Q(n,t.cell);ln(n.s,o),ln(n.e,o),n.s.shape&&n.e.shape||e()}(t[J].data,n[yt].data,e),i(o)}))}(n,o,c,t,r)},unselect:v,del:function(){v(),on(o,e.s),on(o,e.e),o.remove()}},e.styles&&i(o,...e.styles),e.s.shape&&en(o,e.s),e.e.shape&&en(o,e.e),c(),o}function ln(t,n){t.shape?(n(an(t.shape).data.position),an(t.shape).drawPosition()):n(t.data.position)}const dn=(t,n,e,o,l,d)=>{n[yt].data[l].shape&&tn(n,l,W(t[J].data,o)),n.style.pointerEvents="none";const h=function(t){let n=null;function e(t){const e=gt(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[yt].data[l].el,o,(o=>{Y(n[yt].data[l].data.position,t[J].data.scale,o),e()}),(o=>{const i=gt(o),a=i?.getAttribute("data-connect");a?Qt(n,l,i.parentElement,a):Q(n[yt].data[l].data.position,t[J].data.cell),e(),h(),n.style.pointerEvents="unset",d(o)}))};function hn(t){t.el.style.transform=`translate(${t.data.position.x}px, ${t.data.position.y}px) rotate(${pn(t.data.dir)}deg)`}function un(t,n){t.el.firstElementChild.setAttribute("data-evt-index",n.toString())}const pn=t=>"right"===t?180:"left"===t?0:"bottom"===t?270:90,fn=t=>"left"===t?"right":"right"===t?"left":"top"===t?"bottom":"top";const mn=(n,...e)=>e.map((e=>t(n,e))),vn=(t,n,e)=>n<=t&&t<=e,gn=(t,n)=>t.e.el.contains(n)?"e":t.s.el.contains(n)?"s":null,wn=(t,n)=>!t||t===n;function yn(t,n,e){const o=function(t,n){let e=0;return{s:t.split("\n").map(((t,o)=>(e=o,`${0===t.length?".":function(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'")}(t).replaceAll(" "," ")}`))).join(""),c:e}}(e||"",t.x?.baseVal[0]?.value??0);t.innerHTML=o.s,null!=n&&t.y.baseVal[0].newValueSpecifiedUnits(t.y.baseVal[0].SVG_LENGTHTYPE_EMS,o.c>0?n-o.c/2:n)}const xn=(t,n)=>It(n,kn(t,n)),bn=(t,n,e)=>Ft(kn(t,n),e);function kn(t,n){const e=new $n;return c(e,"cmd",(e=>{switch(e.detail.cmd){case"style":Xt(t,n,e.detail.arg);break;case"del":Ot(t,[n]);break;case"copy":Pt(t,[n])}})),e}class $n extends HTMLElement{connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${qt} ${Jt}
`;{const n=t.getElementById("pnl");let e;l(t,"[data-toggle]",(o=>{const i=t=>{const e=at(this);e.style.top=`${e.getBoundingClientRect().top+window.scrollY+t*n.getBoundingClientRect().height}px`};e&&(i(1),Mn(e,!1));const a=t.getElementById(d(o,"data-toggle"));e!==a?(Mn(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 Mn(t,n){t.style.display=n?"unset":"none"}function Cn(n,e,o,i,a,s,c,r,l=0){const d=Hn(e.id,o,i),h={el:t(d,"text"),vMid:l},u=()=>yn(h.el,h.vMid,e.title);u();const p=zn(n,d,e,i,(t=>{1===t&&f(d)}),h,r??xn,(()=>c(h.el)),a,s,null);return{el:d,cons:i,draw:p.draw,drawTxt:u}}customElements.define("ap-shape-edit",$n);const Hn=(t,n,e)=>{const o=p("g",`${n}\n\t${Object.entries(e).map((t=>``)).join()}`);return o.id=t,o};function zn(t,n,e,s,c,r,l,d,h,f,m){let v,g,w,y;e.styles??=[];const x=()=>Mt([n],!1,!1);function b(t){e.title=t,d()}const k=function(t,n,e,s,c,r,l,d,h){i(n,"hovertrack");const f=I(s),m=new Set;let v,g,w=0;function y(){v?.remove(),v=null}function x(){n.style.transform=`translate(${e.position.x}px, ${e.position.y}px)`;for(const t in s)f[t].position={x:s[t].position.x+e.position.x,y:s[t].position.y+e.position.y};for(const t of m)t[yt].drawPosition()}function b(){w=1,i(n,"select"),l&&(v=function(t,n){const e=p("circle");return e.setAttribute("data-evt-index","2"),i(e,"resizer"),o(e,n),t.append(e),e}(n,l()))}function k(t){r(),w=0,a(n,"select","highlight"),t||y(),g&&(g(),g=null)}const $=t=>{switch(w){case 0:b();break;case 1:w=2,a(n,"select"),i(n,"highlight"),y(),g=C(n,(t=>k()))}c(t,w)},H=(e,o,i)=>{if(o===v){k(!0);const o=Mt([n],!1,!1);return void M(v,e,d,(e=>{h&&h(),y(),b(),Dt(t,o,Mt([n],!1,!1))}))}k();const a=o.getAttribute("data-connect");if(a){const o=rn(t,{id:O(),type:0,s:{shape:{shapeEl:n,connectorKey:a}},e:{data:{dir:fn(f[a].dir),position:W(t[J].data,e)}}});return n.parentNode.append(o),t[J].select(u(e)?null:[o]),o[yt].moveCapture(e,o[yt].data.e.el,!0),void m.add(o)}z(e,i)},z=(o,i)=>{const a=i?null:Mt([n],!1,!1);M(n,o,(n=>{Y(e.position,t[J].data.scale,n),x()}),(o=>{Q(e.position,t[J].data.cell),x(),a?Dt(t,a,Mt([n],!1,!1)):Bt(t,[e.id],Mt([n],!1)),u(o)||b()}))};return n[xt]={data:e,drawPosition:x,click:$,moveCapture:H,unselect:k,del:function(){k();for(const t of m)t[yt].del();n.remove()},paths:m,pathAdd:function(t,n){return m.add(n),f[t]},pathDel:function(t){m.delete(t)}},x}(t,n,e,s,((o,i)=>{c&&c(i),2===i&&(r&&!v&&(w=e.title,g=x(),v=function(t,n,e,o,i){let a=p("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(){yn(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}}(r.el,r.vMid,e.title,b)),y||(y=l(t,n,o)))}),(function(){v?.dispose(),v=null,y?.del(),y=null,g&&w!==e.title&&Dt(t,g,x()),g=null,w=null}),h,f,m);return e.styles&&i(n,...e.styles),{draw:()=>{k(),y?.updPos&&y?.updPos(),v?.draw()}}}function Ln(n,e,o){t(n,e).r.baseVal.value=o}const Vn=(t,n)=>function(t,n,e){const o=g(t);return B(n,e,Math.sqrt(o.x**2+o.y**2))}(t,n,24);function Sn(n,e){e.w??=96,e.h??=48;const a={w:e.w,h:e.h};let s;const c=Cn(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=D(W(t[J].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??=En(t(c.el,"text")),l)))&&(Tn(a,e),r())}),(t=>{const n=En(t,a);Bn(e,n)||(Tn(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,jt(t),Dn(t.el,"main",n.w,n.h,e,o),Dn(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[xt].patch=t=>{const n=Kt(c,t),o=(null!=t.w||null!=t.h)&&!Bn(e,t);o&&Tn(e,t),o?r():n&&c.draw()},c.el}const Bn=(t,n)=>t.w===n.w&&t.h===n.h,Tn=(t,n)=>{t.h=n.h,t.w=n.w};function En(t,n){const e=t.getBBox();return{w:B(n?.w,48,e.width),h:B(n?.h,48,e.height)}}const Dn=(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 Zn(t,n){let e;n.w??=96,n.h??=96;const a=function(t,n,e,o,i,a,s){const c=Hn(n.id,e,o);return{el:c,cons:o,draw:zn(t,c,n,o,null,null,bn,null,i,a,s).draw}}(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=An(n);return e=t.tCanvas,t.bInner}),(i=>s(function(t,n,e,i){const a=D(W(t[J].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=An(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,jt(a),Dn(a.el,"main",n.w,n.h,t.x,t.y),Dn(a.el,"selected",n.w,n.h,t.x,t.y),Dn(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(An(n).tInner):a.draw(),a.el[xt].patch=t=>{const e=Kt(a,t),o=(null!=t.w||null!=t.h)&&!Bn(n,t);o&&Tn(n,t),o?s(An(n).tInner):e&&a.draw()},a.el}function An(t){const n=t=>24*Math.floor(t/2/24),e=n(t.w),o=n(t.h);return{tCanvas:{x:t.position.x-e,y:t.position.y-o},tInner:{x:-e,y:-o},bInner:{x:t.w-e,y:t.h-o}}}const Un=(t,n)=>It(n,new Pn(t,n));class Pn extends HTMLElement{constructor(t,n){super(),this.$=n,this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='
';const n=this.$[xt].data,e=t.getElementById("edit");c(e,"cmd",(t=>{switch(t.detail.cmd){case"style":Xt(this.g,this.$,t.detail.arg);break;case"del":Ot(this.g,[this.$]);break;case"copy":Pt(this.g,[this.$])}}));const o=t=>Nt(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?I(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 Rn(n,e){e.w??=48,e.h??=48,e.a??=1;const o=Cn(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=>s(t)),Un,null);i(o.el,"shtxt"),i(o.el,`ta-${e.a}`);const a=()=>t(o.el,"text"),s=t=>{const n=On(t);Bn(e,n)||(Tn(e,n),h())};let c,r,l;function d(){r=e.w,c=e.h,l=e.a}function h(t){if(function(t,n){const e=n.w/-2,o=n.h/-2;t.cons.left.position.y=o+24,t.cons.left.position.x=e,t.cons.top.position.y=o,t.cons.bottom.position.y=-o,t.cons.right.position.y=o+24,t.cons.right.position.x=-e,jt(t),Dn(t.el,"main",n.w,n.h,e,o),Dn(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").forEach((t=>{t.x.baseVal[0].value=n})),e.position.x+=i,l!==e.a&&(Fn(o.el,l,e.a),l=e.a),r=e.w}c!==e.h&&(a().y.baseVal[0].value=In(e.h),e.position.y+=(e.h-c)/2,c=e.h),o.draw()}return d(),48!==e.w||48!==e.h?h(!0):o.draw(),o.el[xt].patch=t=>{const n=t.position&&!R(e.position,t.position);n&&A(e.position,t.position);let i=!1;t.styles&&(e.styles=t.styles,Yt(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=On(a());Bn(e,t)||(Tn(e,t),s=!0)}null==t.w||Bn(e,t)||(Tn(e,t),d(),s=!0),null!=t.a&&e.a!==t.a&&(Fn(o.el,e.a,t.a),e.a=t.a,l=e.a,s=!0),s?(a().y.baseVal[0].value=In(e.h),h(!0)):n&&o.draw()},o.el}function On(t){const n=t.getBBox();return{w:B(0,48,n.width+12),h:B(0,48,n.height+14)}}customElements.define("ap-rect-txt-settings",Pn);const jn=t=>1===t.a?-16:2===t.a?0:16,In=t=>-t/2+24;function Fn(t,n,e){a(t,`ta-${n}`),i(t,`ta-${e}`)}function Nn(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 Kn(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 _n=(t,n)=>B(n,48,function(t){const n=g(t);return 2*(Math.abs(n.x)+Math.abs(n.y))}(t)-20);async function Yn(t,n,e){return function(t,n,e){let o,i;const a=Xn(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(),Gn(n),e)}function Xn(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 Gn(t){return new DataView((new TextEncoder).encode(t).buffer).getUint32(0)}function Jn(t,n,e,o){const i=t.ownerSVGElement.cloneNode(!0),a=i.querySelector("#canvas");let c;i.style.backgroundImage=null,e?(c=function(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((t,n)=>n.some((n=>s(t,n))))(t,qn)?(Qn(t),!0):(t.remove(),!1)))):(c=t.getBoundingClientRect(),te(a,Wn).forEach((t=>Qn(t))));const r=i.getElementsByTagName("foreignObject");for(;r[0];)r[0].parentNode.removeChild(r[0]);const l=t[J].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=new Image;i.width=n.width*e*window.devicePixelRatio,i.height=n.height*e*window.devicePixelRatio,i.onload=function(){const t=document.createElement("canvas");t.width=i.width,t.height=i.height,t.style.width=`${i.width}px`,t.style.height=`${i.height}px`;const e=t.getContext("2d");e.imageSmoothingEnabled=!1,e.drawImage(i,n.x,n.y,n.width,n.height,0,0,i.width,i.height),URL.revokeObjectURL(i.src),t.toBlob(o,"image/png")},t.width.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,i.width),t.height.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,i.height),i.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 Yn(t,"dgRm",(new TextEncoder).encode(n)):t)))}const qn=["select","highlight"],Wn=["select","highlight","highlight-e","highlight-s"],Qn=t=>{a(t,...Wn),t.querySelector(".resizer")?.remove()},te=(t,n)=>t.querySelectorAll(`.${n.join(", .")}`);async function ne(t){const n=await async function(t,n){return Xn(await t.arrayBuffer(),Gn(n))}(t,"dgRm");return n?(new TextDecoder).decode(n):null}async function ee(t,n){const e=`web text/dgrm${N(Mt(n,!0))}`;await async function(t){try{await navigator.clipboard.writeText(t)}catch(t){alert(t)}}(e),window.ClipboardItem&&Jn(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 oe=async()=>ie(await async function(){try{return await navigator.clipboard.readText()}catch(t){return alert(t),null}}());function ie(t){if(null==t||""===t.trim())return null;const n={txt:t};if(t.startsWith("web text/dgrm"))try{n.data=K(t.slice("web text/dgrm".length)),n.txt=null}catch{}return n}function ae(t){return!!re(t)&&(t.c[t.i].u(),t.i-=1,!0)}function se(t){const n=ce(t);return null!==n&&(t.i=n,t.c[n].r(),!0)}const ce=t=>{if(!t)return null;const n=t.i+1;return n<=t.c.length-1?n:null},re=t=>t&&t.i>=0,le=(t,n,e)=>fe(t,(()=>{ue(t,Et(n,e)),zt(t,e),_t(n)}),(()=>{ue(t,Tt(n,e)),zt(t,e)})),de=(t,n,e)=>fe(t,(()=>{ue(t,Tt(n,e)),zt(t,e)}),(()=>{ue(t,Et(n,e)),_t(n)})),he=(t,n,e)=>fe(t,(()=>{ue(t,Zt(e,n)),zt(t,n)}),(()=>{ue(t,Zt(n,e)),zt(t,e)})),ue=(t,n)=>{n[pe]=!0,V(t,n)},pe=Symbol(0);function fe(t,n,e){t[xe]??={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[xe],n,e),we(t)}function me(t,n){n(t[xe])&&(t[J].select(null),we(t))}const ve=t=>me(t,ae),ge=t=>me(t,se);const we=t=>{return V(t,L(ye,(n=t[xe],{u:re(n),r:null!==ce(n)})));var n},ye="dgrmh",xe=Symbol("h");function be(t,n,e,o){if(!n?.data&&!n?.txt)return void dt("Clipboard is empty");t[J].select(Rt(t,function(t){if(!t)return null;const n=new Map,e=t=>{const e=O();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)??(()=>{const e=function(t,n){const e=p("text");yn(e,null,n),e.style.visibility="hidden",t.append(e);const o=On(e);return e.remove(),o}(t,n.txt);return Ct({id:O(),type:3,position:{x:0,y:0},title:n.txt,w:e.w,h:e.h})})(),!0,e,o))}class ke 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,ye,(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=>ve(this.g))),c(e,"click",(t=>ge(this.g)))}}function $e(t,n){Ht(t,n),function(t){const n=t.getBoundingClientRect(),e=window.innerWidth/2,o=window.innerHeight/2;t[J].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[J].scale(a,{x:e,y:o})}(t)}function Me(t,n){const e=function(t,n){async function e(e){if(document.activeElement===t.ownerSVGElement){const o=n();if(o?.length)return e.preventDefault(),ee(t,o),o}return null}async function o(n){const o=await e(n);o&&Ot(t,o)}return document.addEventListener("copy",e),document.addEventListener("cut",o),function(){r(document,"copy",e),r(document,"cut",o)}}(t,n);function o(e){if(!h(e)&&("Delete"===e.key||"Backspace"===e.key)){const e=n();e?.length&&Ot(t,e)}}return c(document,"keydown",o),function(){e(),r(document,"keydown",o)}}function Ce(t,n){return Le?async function(t,n){try{const e=await window.showSaveFilePicker(Ve(n)),o=await e.createWritable();return await o.write(t),await o.close(),e}catch(t){"AbortError"!==t.name&&Se()}}(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)}function He(){return ze?async function(){const[t]=await window.showOpenFilePicker(Ve());return{h:t,f:await t.getFile()}}():new Promise(((t,n)=>{!function(t,n){const e=document.createElement("input");e.type="file",e.multiple=!1,e.accept=".png",c(e,"cancel",(t=>n(t)),!0),c(e,"change",(async()=>t(e.files?.length?e.files[0]:null)),!0),e.click(),e.remove()}((n=>{t({f:n})}),(t=>n(t)))}))}customElements.define("ap-history",ke);const ze="showOpenFilePicker"in window,Le="showSaveFilePicker"in window,Ve=t=>({suggestedName:t,types:[{description:"PNG Image",accept:{"image/png":[".png"]}}]}),Se=()=>alert("File not saved");class Be extends HTMLElement{constructor(t,n){super(),this.g=t,this.M=n}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${qt}Create copy
Copy to clipboard
${Wt}Save image
${Jt}Delete
`;const n=()=>at(this)[lt].del();function e(e,o){c(t.getElementById(e),"click",(t=>{n(),o()}))}e("clone",(()=>Pt(this.g,this.M()))),e("copy",(async()=>await ee(this.g,this.M()))),e("save",(()=>Jn(this.g,N(Mt(this.M(),!1)),!0,(async t=>Ce(t,"dgrm.png"))))),e("del",(()=>Ot(this.g,this.M())))}}customElements.define("ap-grp-settings",Be);const Te=Symbol("p");const Ee=(t,n,e)=>{if(e[Te])return;const o=vt(e),i=qe(o);if(!i)return void Ie(n);const a=_e(n,o),s=Ge(n);if(s&&a)We(i,e);else{if(e.shiftKey){if(s){const t=Xe(n);Qe(t),Re(n,t)}return a?Oe(n,i):Re(n,i),void Ne(n,e)}a?ct(new Be(t,(()=>Fe(n))),e.clientX-10,e.clientY-10):(Ie(n),Re(n,i),Ne(n,e))}},De=(t,n,e,o)=>{if(e[Te])return;if(o===t.ownerSVGElement)return void t[J].moveCapture();const i=_e(n,o);if(!i&&!u(e))return void t[J].moveCapture();if(!i&&u(e))return Ie(n),void to(qe(o),e,o);const a=Ge(n);i&&a?to(qe(o),e,o):i&&!a&&Ze(t,n,e)},Ze=(t,n,e)=>{const o=()=>Ae(n),i=t=>Ue(n,t),a=o();M(t.ownerSVGElement,e,(n=>i((e=>Y(e,t[J].data.scale,n)))),(n=>{i((n=>Q(n,t[J].data.cell))),Dt(t,a,o())}))},Ae=t=>Mt([...t.shapesToMove,...no(t.pathEndsToMove)],!1,!0),Ue=(t,n)=>{t.shapesToMove?.forEach((t=>{n(t[xt].data.position),t[xt].drawPosition()})),t.pathEndsToMove?.forEach((t=>{n(t.data.position),eo(t)[yt].drawPosition()}))},Pe=()=>({shapes:new Set,paths:new Set,pathEnds:new Set,shapesToMove:new Set,pathEndsToMove:new Set}),Re=(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[yt].data.s),t.pathEnds.delete(n.path[yt].data.e);else if(n.pathEnd){const e=eo(n.pathEnd);if(t.paths.has(e))return;t.pathEnds.add(n.pathEnd),i(e,ao(n.pathEnd))}},Oe=(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[yt].data.s),t.pathEnds.delete(n.path[yt].data.e),a(n.path,"highlight","highlight-s","highlight-e");else if(n.pathEnd){const e=eo(n.pathEnd);if(t.paths.delete(e)){a(e,"highlight","highlight-s","highlight-e");const o=io(n.pathEnd);t.pathEnds.add(o),i(e,ao(o))}else t.pathEnds.delete(n.pathEnd),a(e,ao(n.pathEnd))}},je=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(eo(t))))},Ie=t=>{je(t);const n=Xe(t);n&&Qe(n),t.pathEnds.clear(),t.pathEndsToMove.clear(),t.paths.clear(),t.shapes.clear(),t.shapesToMove.clear()},Fe=t=>[...t.shapes,...t.paths,...no(t.pathEnds)],Ne=(t,n)=>{Ke(t);const e=Ye(t);e&&(je(t),We(e,n))},Ke=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[yt].data.s),n(t[yt].data.e)})),t.pathEnds?.forEach(n)},_e=(t,n)=>[...t.shapes,...[...t.pathEnds].map((t=>t.el)),...t.paths].some((t=>t.contains(n))),Ye=t=>Je(t)?Xe(t):{},Xe=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:E(t.pathEnds)}:{path:E(t.paths)}:{shape:E(t.shapes)},Ge=t=>!!Xe(t),Je=t=>t.shapes.size||t.pathEnds.size||t.paths.size,qe=t=>{const n=t[xt]?{shape:t}:t.parentElement[xt]?{shape:t.parentElement}:t.parentElement.parentElement[xt]?{shape:t.parentElement.parentElement}:null;if(n)return n;const e=t[yt]?{path:t}:t.parentElement[yt]?{path:t.parentElement}:null;if(e)return e;const o=oo(t);return o?{pathEnd:o}:null},We=(t,n)=>{t.shape?t.shape[xt].click(n):t.path?t.path[yt].click(n):t.pathEnd&&eo(t.pathEnd)[yt].click(n,t.pathEnd.el)},Qe=t=>{t.shape?t.shape[xt].unselect():t.path?t.path[yt].unselect():t.pathEnd&&eo(t.pathEnd)[yt].unselect()},to=(t,n,e)=>{t.shape?t.shape[xt].moveCapture(n,e):t.path?t.path[yt].moveCapture(n,e):t.pathEnd&&eo(t.pathEnd)[yt].moveCapture(n,e)},no=t=>[...t].map(eo),eo=t=>t.el.parentNode,oo=t=>{const n=t.hasAttribute("data-key")?t:t.parentElement.hasAttribute("data-key")?t.parentElement:null;if(!n)return null;const e=n.parentNode[yt].data;return e.s.el===n?e.s:e.e},io=t=>{const n=eo(t)[yt].data;return n.s===t?n.e:n.s},ao=t=>eo(t)[yt].data.s===t?"highlight-s":"highlight-e";const so=(t,n,e,o,i)=>t.x<=o&&o<=t.x+n&&t.y<=i&&i<=t.y+e,co={iceServers:[{urls:"stun:stun3.l.google.com:19302"}]},ro=`${ht}m/`;const lo=()=>{if(!document.cookie.includes("dgrma=1"))return null;const t=K(localStorage.getItem("dgrm"));return t&&new Date(t.e)>new Date?t:null},ho=()=>{const t=lo()?.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},uo=t=>document.getElementById("menu").bage(t),po=t=>{const n=fo(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)",v(e),e},fo=t=>`hsl(${[...t].reduce(((t,n)=>n.charCodeAt(0)+((t<<5)-t)),0)%360}, 97%, 47%)`,mo=(t,n,e,o)=>{const i=((t,n,e)=>({x:t.position.x+n*t.scale,y:t.position.y+e*t.scale}))(t[J].data,e,o);n.style.transform=`translate(${i.x}px, ${i.y}px)`},vo=(t,n,e)=>{const o=t=>e(N(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])));let l=!0;const d=c(t.ownerSVGElement,"pointermove",S((e=>{if(l){const i=q(t[J].data,e.clientX,e.clientY);o([6,n,i.x,i.y])}}),60));return()=>{l=!1,r(t,"dgrmc",i),r(t,"dgrmu",a),r(t,"dgrmd",s),r(t.ownerSVGElement,"pointermove",d)}};async function go(t,n,e,o){const i=ho();if(null==i)return e(),null;ot(!0);const a=O(),s=new Map,c=await async function(t,n,e,o,i){const a=await async function(t,n,e){const o=new RTCPeerConnection(co),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)=>x(`${ro}o`,{m:t,c:n,s:e}))(n,t,a.localDescription.sdp);const s=_(3e3,15e3,(async()=>{try{const e=await(async(t,n)=>(await y(`${ro}a?m=${t}&c=${n}`)).text())(n,t);e&&(s(),a.setRemoteDescription({sdp:e,type:"answer"}))}catch{c(),i()}}),i),c=()=>{s(),a.close(),ot(!1)};return c}(a,n,((t,n)=>t.send(N([0,a,i]))),((n,e)=>{const o=K(e.data);switch(o[0]){case 6:mo(t,s.get(o[1]),o[2],o[3]);break;case 4:zt(t,o[1]);break;case 5:_t(o[1]);break;case 0:s.set(o[1],po(o[2]));break;case 7:s.get(o[1]).remove(),s.delete(o[1]);break;case 2:$e(t,o[1]),l(n);break;case 3:t[J].move(o[1],o[2],1),l(n)}}),(()=>{d(),o()}));let r;const l=n=>{r=vo(t,a,(t=>n.send(t))),ot(!1)},d=()=>{s.forEach((t=>t.remove())),r&&r(),c(),ot(!1)};return d}async function wo(t,n,e){const o=ho();if(null==o)return n(),null;const i=O(),a=O(),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=O();await((t,n)=>x(`${ro}c`,{m:t,k:n}))(t,a);const s=new Set,c=_(5e3,72e5,(async()=>{try{const i=await(async(t,n)=>{const e=await(await y(`${ro}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(co);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)=>x(`${ro}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(N([0,a,o]));const c=$t(t);e.send(N(kt(c)?[3,t[J].data.position.x,t[J].data.position.y]:[2,c])),s.forEach((t=>e.send(N([0,t.i,t.n]))))}),((n,e)=>{const o=K(e.data);switch(o[0]){case 6:mo(t,s.get(n).c,o[2],o[3]),c(e.data,n);break;case 4:zt(t,o[1]),c(e.data,n);break;case 5:_t(o[1]),c(e.data,n);break;case 0:s.set(n,{i:o[1],n:o[2],c:po(o[2])}),c(e.data,n)}}),((t,n)=>{const e=s.get(t);c(N([7,e.i]),t),e.c.remove(),s.delete(t)}),(()=>{d(),e()})),l=vo(t,a,c),d=()=>{l(),s.forEach((t=>t.c.remove())),r()};return{dispose:d,id:i}}function yo(t,e,o){const i=m(`
Live collaboration in progress
`),a=rt(i),s=(t,e)=>c(n(i,t),"click",(t=>{a[lt].del(),e()}));s("mstop",e),s("mshare",(async n=>{await navigator.clipboard.writeText(k("m",t)),dt("Link copied to clipboard")}))}const xo=t=>{t[ko]?.dispose(),t[ko]=void 0,uo(!1)},bo=t=>{xo(t),alert("Live collaboration disabled")},ko=Symbol(0),$o=async t=>{const n=Vo(t);n?.f?Eo(t,(async t=>{const e=await n.f.createWritable();await e.write(t),await e.close(),Do()})):n?.k?await To(t,n.k):Mo(t)},Mo=t=>{const e=Vo(t)?.n;lo()?function(t,e){const o=m(`
`),i=rt(o,"mdm"),a=(t,e)=>c(n(o,t),"click",(t=>{i[lt].del(),e()})),s=()=>Bo(n(o,"nm").value);a("dvc",(n=>Co(t,s()))),a("cl",(async n=>{try{const n=s(),e=Vo(t);if(e?.k&&e.n===n)return void await To(t,e.k);const o=mt();Ho(t,o,await async function(t,n,e){const o=new URL(`${ut}a/c`);return o.searchParams.set("k",t),o.searchParams.set("n",n),(await x(o.toString(),e)).text()}(o,n,$t(t))),Do()}catch{Se()}}))}(t,e):Co(t,e)},Co=(t,n)=>Eo(t,(async e=>{const o=Ce(e,n??"dgrm.png");if(o){const n=await o;n&&zo(t,n)}}));const Ho=(t,n,e)=>{t[Zo]={k:n,n:e},So(e)},zo=(t,n)=>{const e=j(n.name);t[Zo]={f:n,n:e},So(e)},Lo=t=>{t[Zo]=void 0,So()},Vo=t=>t[Zo],So=t=>{document.title=t??"DGRM - Flowchart editor"},Bo=t=>t?.replace(/[/\\?%*:|"<>]/g,"-"),To=async(t,n)=>{await pt(n,$t(t)),Do()},Eo=(t,n)=>Jn(t,N($t(t)),null,n),Do=()=>dt("Saved"),Zo=Symbol(0);function Ao(t){V(t,L("dgrmn")),function(t){for(t[J].select(null);t.firstChild;)(t.firstChild[xt]||t.firstChild[yt]).del();t[J].move(0,0,1)}(t),function(t){var n;(n=t[xe])&&(n.c=[],n.i=-1),we(t)}(t),Lo(t)}async function Uo(t){lo()?async function(t){let e;ot(!0);try{e=await(async()=>{const t=await y(`${ut}a/l`);let n;try{n=await t.json()}catch{}return n})()}catch{return alert("Error"),void ot(!1)}const o=rt(m(`
${F(e)?Object.entries(e).map((t=>``)).join(""):'
No cloud items
'}
`,"display: flex; flex-flow: column; height: 100%; overflow-y: overlay;"),"mdh");i(at(o),"mdhc"),c(n(o,"dvc"),"click",(async n=>{o[lt].del(),await Po(t)}));const a=t=>o.querySelector(`[dgr='${t}']`),s=async t=>{try{ot(!0),await t()}catch{alert("Error")}finally{ot(!1)}};l(o,"[edt]",(o=>{const i=o.currentTarget.getAttribute("edt");!function(t,e,o,i){const a=e.currentTarget.getBoundingClientRect(),s=st(m('Rename Delete'),a.x-30,a.y+70),r=(t,e)=>c(n(s,t),"click",(t=>{s[lt].del(),e()}));r("rn",(e=>function(t,e){const o=rt(m(`
`),"mdm",!0);c(n(o,"sv"),"click",(t=>{o[lt].del(),e(Bo(n(o,"nm").value))}))}(t.n,o))),r("del",(t=>i()))}(e[i],o,(t=>{e[i].n!==t&&s((async()=>{e[i].n=await ft(i,t),a(i).querySelector("[nm]").textContent=e[i].n}))}),(()=>{confirm("Delete?")&&s((async()=>{await(async t=>x(`${ut}a/d/${t}`))(i),delete e[i],a(i).remove(),i===Vo(t)?.k&&Lo(t)}))}))})),ot(!1)}(t):Po(t)}async function Po(t){try{let n;try{n=await He()}catch(t){if("AbortError"===t.name)return;throw t}await Ro(t,n.f),n.h?zo(t,n.h):Lo(t)}catch(t){console.log(t),Oo()}}async function Ro(t,n){const e=await ne(n);e?(Ao(t),$e(t,K(e))):Oo()}const Oo=()=>alert("File cannot be read. Use the exact image file you got from the application.");class jo extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=lo(),e=t||ze,o=this.attachShadow({mode:"closed"});o.innerHTML=`
New
Open
${e?'
Save
':""}
${Wt}Save${Le||e?" as":""}
Share link to diagram
Live collaboration
${t?t.n:"Log in"}
`;const i=t=>n(o,t);this.C=i;const a=i("options");function s(){a.style.visibility="visible"===a.style.visibility?"hidden":"visible"}function l(t,n){c(i(t),"click",(async t=>{s(),await n(t)}))}c(i("menu"),"click",(t=>{s(),c(document,"pointerdown",(t=>{this.contains(t.target)||(a.style.visibility="hidden")}),!0)})),c(i("menu2"),"click",(t=>s())),l("new",(()=>{Ao(this.g)})),e&&l("save",(async()=>await $o(this.g))),l("saveas",(()=>Mo(this.g))),l("open",(async()=>await Uo(this.g))),l("collab",(async t=>await async function(t){const n=()=>xo(t),e=()=>bo(t);try{switch(t[ko]?.clientType){case 1:yo(t[ko].id,n);break;case 2:yo(t[ko].id,n,!0);break;default:{const o=await wo(t,n,e);if(!o)return;c(t,"dgrmn",e,!0),t[ko]={clientType:1,id:o.id,dispose:()=>{r(t,"dgrmn",e),o.dispose()}},uo(!0),yo(t[ko].id,n);break}}}catch{e()}}(this.g))),l("login",(t=>function(){const t=lo();rt(m(t?`
${t.n}
${t.l}
`:'
Log in to access
cloud storage
'))}())),l("link",(async t=>await Io(this.g)))}bage(t){const n=t?i:a;n(this.C("menu"),"bage"),n(this.C("collab"),"bage")}}customElements.define("ap-menu",jo);const Io=async t=>{ot(!0);const n=$t(t);if(kt(n))return alert("Diagram is empty"),void ot(!1);const e=mt();await navigator.clipboard.writeText(k("k",e)),await((t,n)=>x(`${ut}${t}`,n))(e,n),dt("Link copied to clipboard"),ot(!1)};class Fo extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='';const n=t.getElementById("menu");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)}handleEvent(t){switch(t.type){case"pointermove":if(!this.H){const n=document.elementFromPoint(t.clientX,t.clientY);if(n===this.L)return;this.V===this.L&&this.g.ownerSVGElement.setPointerCapture(t.pointerId),this.L=n}break;case"pointerleave":if(this.H=!0,null!=this.S){const n=No(this.g,this.S,!1,t.clientX,t.clientY)[0];u(t)||this.g[J].select([n]),n[xt]?n[xt]?.moveCapture(t,n,!0):n[yt]?.moveCapture(t,n,!0)}this.B();break;case"pointerdown":this.S=parseInt(d(t,"data-cmd-arg")),this.V=document.elementFromPoint(t.clientX,t.clientY),this.L=this.V,this.H=null;break;case"pointerup":this.B();break;case"click":this.g.ownerSVGElement.focus(),this.g[J].select(No(this.g,parseInt(d(t,"data-cmd-arg")),!0))}}B(){this.S=null,this.V=null,this.L=null}}customElements.define("ap-menu-shape",Fo);const No=(t,n,e,o,i)=>Rt(t,Ct(0===n?{id:O(),type:0,s:{data:{dir:"right",position:{x:-24,y:0}}},e:{data:{dir:"right",position:{x:24,y:0}}}}:{id:O(),type:n,position:{x:0,y:0},title:"Title"}),e,o,i);const Ko=document.getElementById("canvas");var _o,Yo,Xo;Ko[J]={data:{position:{x:0,y:0},scale:1,cell:24},shapeMap:function(n){const e=t=>(n.append(t),t);return{0:{create:t=>e(rn(n,t))},1:{create:i=>e(function(n,e){e.r??=48;let i,a=e.r;const s=Cn(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=Z(e.position,-e.r),{x:e.r,y:e.r})),(r=>{let l=W(n[J].data,r).x-i.x;l<48&&(l=48);const d=48*Math.round(l/48)/2;e.r!==d&&Vn(t(s.el,"text"))<=d&&(e.r=d,e.position.x=i.x+d,e.position.y=i.y+d,c(),a=d);const h=l-e.r;o(r.target,{x:h,y:h})}),(t=>{const n=Vn(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,jt(s),Ln(s.el,"outer",e.r+24),Ln(s.el,"main",e.r),s.draw()}return 48!==e.r?c():s.draw(),s.el[xt].patch=t=>{const n=Kt(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:{create:t=>e(Sn(n,t))},3:{create:t=>e(Rn(n,t))},4:{create:a=>e(function(n,e){e.w??=96;let a,s=e.w;const c=Cn(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=Z(e.position,-t),{x:t,y:t}}),(i=>{let l=W(n[J].data,i).x-a.x;l<48&&(l=48);const d=48*Math.round(l/48);if(e.w!==d&&_n(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 h=l-e.w/2;o(i.target,{x:h,y:h})}),(t=>{const n=_n(t,s);n!==e.w&&(e.w=n,r())}));function r(){const t=Kn(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,jt(c);const n=Kn(e.w,9);Nn(c.el,"main",n),Nn(c.el,"border",n),Nn(c.el,"outer",Kn(e.w,-24)),c.draw()}return i(c.el,"shrhomb"),96!==e.w?r():c.draw(),c.el[xt].patch=t=>{const n=Kt(c,t),o=null!=t.w&&e.w!==t.w;o&&(e.w=t.w),o?r():n&&c.draw()},c.el}(n,a))},5:{create:t=>{return e=Zn(n,t),n.prepend(e),e;var e}}}}(Ko)},Ko.ownerSVGElement.addEventListener("contextmenu",(async t=>{h(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[X]=e?t.clientX-e:0,t[G]=o?t.clientY-o:0,e=t.clientX,o=t.clientY):(t[X]=t.movementX,t[G]=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=>{h(n)||be(t,ie(n.clipboardData.getData("text/plain")))})),c(document,"keydown",(n=>{!n.ctrlKey&&!n.metaKey||n.repeat||h(n)||("KeyZ"===n.code?ve(t):"KeyY"===n.code&&ge(t))}))}(Ko),function(t){const n=t.ownerSVGElement;let e,a,s,l,d;const h=()=>{d?.remove(),d=null};let u=!1;function f(t){if(t[Te]||!s)return void v();t[Te]=!0,u&&(h(),u=!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&&!u){n[Te]=!0;const e=n=>so(q(t[J].data,l.x,l.y),s.width.baseVal.value/t[J].data.scale,s.height.baseVal.value/t[J].data.scale,n.x,n.y);!function(t,n,e,o){const i=Pe(),a=t=>Re(i,t),s=t=>t.shape&&e(t.shape.shapeEl)||o(t);for(const t of n)if(t[xt])e(t)&&a({shape:t});else if(t[yt]){const n=s(t[yt].data.s),e=s(t[yt].data.e);n&&e?a({path:t}):n?a({pathEnd:t[yt].data.s}):e&&a({pathEnd:t[yt].data.e})}t[J].selectedSet(i)}(t,t.children,(t=>e(t[xt].data.position)),(t=>e(t.data.position)))}v()}function v(){clearTimeout(e),e=null,s?.remove(),s=null,h(),u=!1,r(n,"pointermove",f),r(n,"wheel",v),r(n,"pointercancel",v),r(n,"pointerup",m)}c(n,"pointerdown",(r=>{if(!r.isPrimary||vt(r)!==n)return void v();c(n,"pointermove",f),c(n,"wheel",v,!0),c(n,"pointercancel",v,!0),c(n,"pointerup",m,!0);const h=()=>{t[J].selectedSet(null),u=!0,n.setPointerCapture(r.pointerId),a={x:r.clientX,y:r.clientY},l={x:r.clientX,y:r.clientY},s=p("rect"),s.style.cssText="rx:8px; fill: rgb(108 187 247 / 51%)",o(s,l),n.append(s)};2!==r.button?e=setTimeout((t=>{h(),d=p("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):h()}))}(Ko),function(t){const n=t.ownerSVGElement;let e,o,i=Pe();c(n,"pointerdown",(a=>{if(!a.isPrimary)return r(n,"pointermove",e),r(n,"pointerup",o),void t[J].moveCapture();o=o=>{r(n,"pointermove",e),Ee(t,i,o)},c(n,"pointerup",o,!0),e=e=>{r(n,"pointerup",o),De(t,i,e,vt(a))},0===a.button&&c(n,"pointermove",e,!0)})),Me(t,(()=>Fe(i)));const a=t=>{Ie(i),i=t??Pe(),Ne(i,null)};t[J].selectedSet=a,t[J].select=t=>{const n=Pe();t?.forEach((t=>Re(n,qe(t)))),a(n)}}(Ko),function(t){const n=t[J].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 h(t){s=null,l=null,i?.id===t.pointerId&&(i=null),a?.id===t.pointerId&&(a=null),i||a||(r(document,"pointermove",u),r(document,"pointercancel",h),r(document,"pointerup",h),d=!1)}function u(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=nt(t,n)),a?.id===t.pointerId&&(a=nt(t,n))}c(t.ownerSVGElement,"pointerdown",(t=>{!i&&!t.isPrimary||i&&a||(i||(c(document,"pointermove",u),c(document,"pointercancel",h),c(document,"pointerup",h),i=nt(t,n)),a||i?.id===t.pointerId||(a=nt(t,n)))})),t[J].moveCapture=()=>{d=!0}}(t,n,i,o),t.ownerSVGElement.addEventListener("wheel",(t=>{t.preventDefault();const e=t.deltaY||t.deltaX,o=Math.abs(e)<50?.05:.25;i(n.scale+(e<0?o:-o),tt(t))})),t[J].move=function(t,e,i){n.position.x=t,n.position.y=e,n.scale=i,o()},t[J].scale=i}(Ko),function(t){if(!navigator.clipboard.readText)return void t.ownerSVGElement.addEventListener("contextmenu",(async n=>{t.ownerSVGElement===vt(n)&&n.preventDefault()}));const n=t.ownerSVGElement,e=t.ownerDocument;let o,i,a,s;function l(n){r(e,"pointermove",u),r(e,"pointerup",d),a||(a=function(t,n,e,o){const i=m('
Paste
');c(i,"click",(async i=>{be(e,await oe(),t,n),o()}));return{del:ct(i,t-10,n-10)[lt].del,el:i}}(n.clientX,n.clientY,t,d),c(e,"pointerdown",h))}function d(){clearTimeout(o),o=null,a?.del(),a=null,r(e,"pointerdown",h),r(e,"pointerup",d),r(e,"wheel",d),r(e,"keydown",d),r(e,"pointerup",f),r(e,"pointercancel",f),r(e,"pointermove",p),i=!1,s=!1}function h(t){a.el.contains(t.target)||d()}function u(){d(),i=!0}function p(){s||d(),i=!0}function f(){s=!0}c(n,"pointerdown",(async function(t){t.isPrimary&&vt(t)===n?(d(),c(e,"pointermove",u,!0),c(e,"pointerup",d,!0),c(e,"wheel",d,!0),c(e,"keydown",d,!0),2!==t.button&&(o=setTimeout((n=>{l(t),c(e,"pointerup",f,!0),c(e,"pointercancel",f,!0),c(e,"pointermove",p,!0)}),500))):d()})),n.addEventListener("contextmenu",(async t=>{n===vt(t)&&(t.preventDefault(),i||l(t))}))}(Ko),(t=>{const n=(n,e)=>c(t,n,(t=>{t[pe]||e(t)}));n("dgrmc",(n=>le(t,n.detail.newIds,n.detail.shapesToCreateData))),n("dgrmd",(n=>de(t,n.detail.delIds,n.detail.shapesToDelData))),n("dgrmu",(n=>he(t,n.detail.shapesBeforeUpdData,n.detail.shapesToUpdData)))})(Ko),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&&"image/png"===n.dataTransfer.items[0].type)if("getAsFileSystemHandle"in n.dataTransfer.items[0]){const e=await n.dataTransfer.items[0].getAsFileSystemHandle();await Ro(t,await e.getFile()),zo(t,e)}else{const e=n.dataTransfer.items[0].getAsFile();await Ro(t,e),So(j(e.name))}else Oo()}))}(Ko),(t=>{var n,e;n=S((async()=>await $o(t)),500),e=()=>Mo(t),c(document,"keydown",(t=>{!t.ctrlKey&&!t.metaKey||t.repeat||"KeyS"!==t.code||(t.preventDefault(),t.shiftKey?e():n())}))})(Ko);{const t=new URL(window.location.href),n=n=>t.searchParams.get(n);n("usr")&&(_o=n("usr"),Yo=n("usrn"),Xo=parseInt(n("expr")),localStorage.setItem("dgrm",N({l:_o,n:Yo,e:Xo})),$("usr","usrn","expr"))}(t=>{const n=new jo(t);n.id="menu",v(n)})(Ko),v(new ke(Ko)),v(new Fo(Ko));{const t=new URL(window.location.href),e=n=>t.searchParams.get(n);if(e("k")){ot(!0);try{const t=e("k"),n=await(async t=>{const n=await y(`${ut}${t}`);return{e:n.headers.has("X-Dgrm-edit"),n:decodeURIComponent(n.headers.get("X-Dgrm-name")),d:await n.json()}})(t);$e(Ko,n.d),n.e&&Ho(Ko,t,n.n)}catch{alert("The link is broken or you do not have permission. Try to relogin.")}$("k"),ot(!1)}else if(e("m")){const t=e("m");$("m"),await async function(t,n){const e=()=>bo(t);try{const o=await go(t,n,(()=>xo(t)),e);if(!o)return;c(t,"dgrmn",e,!0),t[ko]={clientType:2,id:n,dispose:()=>{r(t,"dgrmn",e),o()}},uo(!0)}catch{e()}}(Ko,t)}else lo()||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),v(t)}()}