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