From 91e6627e5b3c505bba7d434bff79dbd226dbfd24 Mon Sep 17 00:00:00 2001 From: Alexey Boyko Date: Wed, 27 Mar 2024 18:01:19 +0300 Subject: [PATCH] marker --- index.html | 6 +++++- index.js | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index ea5bb3b..18a0107 100644 --- a/index.html +++ b/index.html @@ -137,6 +137,10 @@ .shrhomb.highlight [data-key="border"] { stroke-width: 28px; stroke: rgb(108 187 247 / 51%); } .shrhomb.highlight [data-key="main"] { stroke-width:18px; stroke:rgb(29, 128, 159); } + /* marker */ + .mrkp, .shmrk [data-key="main"] { fill: none; stroke: rgb(213 178 255); mix-blend-mode: multiply; stroke-width: 10px; stroke-linecap: round; stroke-linejoin: round; } + .shmrk.select [data-key="selected"], .shmrk.highlight [data-key="selected"] { stroke: rgba(108 187 247 / 51%); stroke-linecap: round; stroke-linejoin: round; fill: none; } + /* shape settings styles */ .d-cl-red [data-key="main"], .d-cl-red .arr { fill: rgb(231, 76, 60); } .d-cl-red .path { stroke: rgb(231, 76, 60);} .d-cl-orange [data-key="main"], .d-cl-orange .arr { fill: rgb(255, 102, 0);} .d-cl-orange .path { stroke: rgb(255, 102, 0);} @@ -176,6 +180,6 @@ - + \ No newline at end of file diff --git a/index.js b/index.js index 4585aea..8787329 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?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 C=(t,n)=>M(t.ownerSVGElement,t,n);function M(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)},V=(t,n,e)=>new CustomEvent(t,{detail:n,cancelable:e}),L=(t,n)=>t.dispatchEvent(n),z=(t,n)=>{let e,o,i=!1;return function a(){if(i)return e=arguments,void(o=this);t.apply(this,arguments),i=!0,setTimeout((function(){i=!1,e&&(a.apply(o,e),e=o=null)}),n)}};function S(t,n,e){return t??=0,e<=t?t:t+Math.ceil((e-t)/n)*n}const B=t=>Math.round(100*(t+Number.EPSILON))/100,E=(t,n,e)=>{let o=!1;return n>e?n>t&&(o=!0,e*=t/n,n=t):e>t&&(o=!0,n*=t/e,e=t),{w:n,h:e,c:o}};function Z(t,n){for(let e=t.length-1;e>=0;--e)n(t[e])}const D=t=>t.values().next().value;function T(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)=>M(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",Ct=t=>Mt([...t.children],!1);function Mt(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 Vt=(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=Bt(t,n.s[e],a);o&&s.push(o);break}default:i(n.s[e])}return[...o.values(),...s]}function zt(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)=>zt(t,F(n)),Bt=(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?zt(t,o):null};function Et(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)=>L(t,Dt(n,e)),Dt=(t,n)=>V("dgrmc",{newIds:t,shapesToCreateData:n}),Tt=(t,n)=>V("dgrmd",{delIds:t,shapesToDelData:n}),At=(t,n,e)=>L(t,Rt(Et(n),Et(e))),Rt=(t,n)=>V("dgrmu",{shapesBeforeUpdData:t,shapesToUpdData:n});function Ut(t,n,e,o){const i=T(((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(T(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,Mt(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),Lt(t,n)}function It(t,n){((t,n,e)=>{L(t,Tt(n,e))})(t,n.map((t=>(t[kt]||t[bt]).data.id)),Mt(n,!1,!0)),n.forEach((t=>(t[kt]||t[bt]).del()))}const Ft='',Kt='',Nt='',Yt='';function Xt(n){for(const e in n.cons)o(t(n.el,e),n.cons[e].position)}function _t(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 Gt=(t,n)=>({del:at(t,n.clientX-66,n.clientY-10)[ht].del}),Jt=(t,n,e)=>{const o=Mt([n],!1,!1);(n[kt]??n[bt]).patch(e),At(t,o,Mt([n],!1,!1))};function qt(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,tn(t.el))}(t,n),Wt(t,n)}function Wt(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 Qt=t=>t.forEach((t=>{const n=e(t);n&&(n[kt]||n[bt]).del()}));function tn(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 nn=(t,n,e)=>{const o=(n[kt]??n[bt]).data;o.styles||(o.styles=[]);const i={styles:F(o.styles)};Jt(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 en(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 on 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":nn(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")),Jt(this.g,this.o,e)}))}}function an(t,n,e,o){const i=un(t,n);i.shape={shapeEl:e,connectorKey:o},rn(t,i)}function sn(t,n,e){const o=un(t,n);o.shape.shapeEl!==hn(t,o)&&ln(t,o),o.shape=null,o.data={dir:o.data.dir,position:e}}function cn(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&&sn(t,o,a.data.position):function(t,n,e,o){const i=un(t,n);if(i.shape.shapeEl===e&&i.shape.connectorKey===o)return;i.shape.shapeEl!==e&&i.shape.shapeEl!==hn(t,i)&&ln(t,i);i.shape={shapeEl:e,connectorKey:o},rn(t,i)}(t,o,a.shape.shapeEl,a.shape.connectorKey):null!=a.shape?an(t,o,a.shape.shapeEl,a.shape.connectorKey):R(i.data.position,a.data.position)};o("s"),o("e")}function rn(t,n){n.data=dn(n.shape).pathAdd(n.shape.connectorKey,t)}customElements.define("ap-path-settings",on);const ln=(t,n)=>dn(n.shape)?.pathDel(t),dn=t=>t?.shapeEl[kt],un=(t,n)=>t[bt].data[n],hn=(t,n)=>(t[bt].data.e===n?t[bt].data.s:t[bt].data.e).shape?.shapeEl;function pn(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=xn(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 bn(e,-.8,.8)?"left":bn(e,.8,2.4)?"top":bn(e,2.4,3.2)||bn(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=yn(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))),gn(e.s),gn(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=C(o,(t=>y(!0))),v((()=>{y(!0),x()})),function(t){r||(r=Gt(new on(n,o),t))}(t)}}function y(t){t||(l=null),m=0,a(o,"select","highlight-s","highlight-e"),vn(e.s,1),vn(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"),vn(e.s,2),vn(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?kn(e,n??wt(t)):null;$n(l,o)||y(),l=o}w(t)},patch:t=>{e.styles&&(e.styles=t.styles,tn(o)),t.s&&cn(o,t),c()},moveCapture:(t,i,a)=>{y(!0);const s=a?null:Mt([o],!1,!0),r=t=>{y(!0),s?At(n,s,Mt([o],!1,!0)):Zt(n,[e.id],Mt([o],!1,!0)),h(t)||w()},l=kn(e,i);l?mn(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);fn(n.s,i),n.s.shape?.shapeEl&&n.s.shape?.shapeEl===n.e.shape?.shapeEl||fn(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);fn(n.s,o),fn(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(),ln(o,e.s),ln(o,e.e),o.remove()}},e.styles&&i(o,...e.styles),e.s.shape&&rn(o,e.s),e.e.shape&&rn(o,e.e),c(),o}function fn(t,n){t.shape?(n(dn(t.shape).data.position),dn(t.shape).drawPosition()):n(t.data.position)}const mn=(t,n,e,o,l,d)=>{n[bt].data[l].shape&&sn(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?an(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 gn(t){t.el.style.transform=`translate(${t.data.position.x}px, ${t.data.position.y}px) rotate(${wn(t.data.dir)}deg)`}function vn(t,n){t.el.firstElementChild.setAttribute("data-evt-index",n.toString())}const wn=t=>"right"===t?180:"left"===t?0:"bottom"===t?270:90,yn=t=>"left"===t?"right":"right"===t?"left":"top"===t?"bottom":"top";const xn=(n,...e)=>e.map((e=>t(n,e))),bn=(t,n,e)=>n<=t&&t<=e,kn=(t,n)=>t.e.el.contains(n)?"e":t.s.el.contains(n)?"s":null,$n=(t,n)=>!t||t===n;function Cn(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)=>_t(n,Vn(t,n)),Hn=(t,n,e)=>Gt(Vn(t,n),e);function Vn(t,n){const e=new Ln;return c(e,"cmd",(e=>{switch(e.detail.cmd){case"style":nn(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 Sn(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=()=>Cn(u.el,u.vMid,e.title);h();const p=Zn(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 Bn(t,n,e,o,i,a,s,c){const r=En(n.id,e,o),l=Zn(t,r,n,o,(n=>{1===n&&en(t,r)}),null,c??Hn,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 Zn(t,n,e,s,c,r,l,d,u,f,m){let g,v,w,y;e.styles??=[];const x=()=>Mt([n],!1,!1);function b(t){e.title=t,d()}const k=function(t,n,e,s,c,r,l,d,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 M(){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 V(){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 z=t=>{switch(y){case 0:V();break;case 1:y=2,a(n,"select"),i(n,"highlight"),x(),v=C(n,(t=>L())),k((()=>{L(),V()}))}c(t,y)},S=(e,o,i)=>{if(o===g){L(!0);const o=Mt([n],!1,!1);return void $(g,e,d,(e=>{u&&u(),x(),V(),At(t,o,Mt([n],!1,!1))}))}L();const a=o.getAttribute("data-connect");if(a){const o=pn(t,{id:j(),type:0,s:{shape:{shapeEl:n,connectorKey:a}},e:{data:{dir:yn(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)}B(e,i)},B=(o,i)=>{const a=i?null:Mt([n],!1,!1);$(n,o,(n=>{_(e.position,t[q].data.scale,n),M()}),(o=>{tt(e.position,t[q].data.cell),M(),a?At(t,a,Mt([n],!1,!1)):Zt(t,[e.id],Mt([n],!1)),h(o)||V()}))};return n[kt]={data:e,drawPosition:M,click:z,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)}},M}(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(){Cn(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 Dn(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 An(n,e){e.w??=96,e.h??=48;const a={w:e.w,h:e.h};let s;const c=Sn(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=T(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??=Pn(t(c.el,"text")),l)))&&(Un(a,e),r())}),(t=>{const n=Pn(t,a);Rn(e,n)||(Un(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,Xt(t),On(t.el,"main",n.w,n.h,e,o),On(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=qt(c,t),o=(null!=t.w||null!=t.h)&&!Rn(e,t);o&&Un(e,t),o?r():n&&c.draw()},c.el}const Rn=(t,n)=>t.w===n.w&&t.h===n.h,Un=(t,n)=>{t.h=n.h,t.w=n.w};function Pn(t,n){const e=t.getBBox();return{w:S(n?.w,48,e.width),h:S(n?.h,48,e.height)}}const On=(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 jn(t,n){let e;n.w??=96,n.h??=96;const a=Bn(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=In(n);return e=t.tCanvas,t.bInner}),(i=>s(function(t,n,e,i){const a=T(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=In(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,Xt(a),On(a.el,"main",n.w,n.h,t.x,t.y),On(a.el,"selected",n.w,n.h,t.x,t.y),On(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(In(n).tInner):a.draw(),a.el[kt].patch=t=>{const e=qt(a,t),o=(null!=t.w||null!=t.h)&&!Rn(n,t);o&&Un(n,t),o?s(In(n).tInner):e&&a.draw()},a.el}function In(t){const n=Fn(t.w),e=Fn(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 Fn=t=>24*Math.floor(t/2/24);function Kn(t,n){return Xn?async function(t,n){try{const e=await window.showSaveFilePicker({suggestedName:n,types:[Gn]}),o=await e.createWritable();return await o.write(t),await o.close(),e}catch(t){"AbortError"!==t.name&&_n()}}(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 Yn?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 Yn="showOpenFilePicker"in window,Xn="showSaveFilePicker"in window,_n=()=>alert("File not saved"),Gn={accept:{"image/png":[".png"]},inputAccept:"image/png"},Jn={accept:{"image/*":[".png",".gif",".jpeg",".jpg"]},inputAccept:"image/*"};function qn(t,n,e,o){Wn(n,(n=>t[q].select(jt(t,Ht(n),!0,e,o))))}function Wn(t,n){!function(t,n,e){const o=new Image;o.onload=function(){const i=E(n,o.width,o.height);if(!i.c){URL.revokeObjectURL(o.src);const n=new FileReader;return n.onloadend=function(){e(n.result,i.w,i.h)},void n.readAsDataURL(t)}const a=document.createElement("canvas");a.width=i.w,a.height=i.h;const s=a.getContext("2d");s.imageSmoothingEnabled=!1,s.imageSmoothingQuality="high",s.drawImage(o,0,0,i.w,i.h),URL.revokeObjectURL(o.src),e(a.toDataURL(),i.w,i.h)},o.src=URL.createObjectURL(t)}(t,2040,((t,e,o)=>{const i=E(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:B(o/e),d:t};te(a),n(a)}))}function Qn(t,n){let e;const a=Bn(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=In(n);return e=t.tCanvas,t.bInner}),(i=>s(function(t,n,e,i){const a=T(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){te(t);const e=In(t);return t.position.x=n.x-e.tInner.x,t.position.y=n.y-e.tInner.y,e.tInner}(n,e))),ne);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,Xt(a),On(a.el,"main",n.w,n.h,t.x,t.y),On(a.el,"selected",n.w,n.h,t.x,t.y),On(a.el,"outer",n.w+48,n.h+48,t.x-24,t.y-24),a.draw()};return s(In(n).tInner),a.el[kt].patch=t=>{const e=Wt(a,t),o=(null!=t.w||null!=t.h)&&!Rn(n,t);o&&Un(n,t),o?s(In(n).tInner):e&&a.draw()},a.el}function te(t){t.w=24*Math.round(t.w/24),t.h=B(t.r*t.w)}function ne(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])}})),Gt(o,e)}const ee=(t,n)=>_t(n,new oe(t,n));class oe 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":nn(this.g,this.$,t.detail.arg);break;case"del":It(this.g,[this.$]);break;case"copy":Ot(this.g,[this.$])}}));const o=t=>Jt(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 ie(n,e){e.w??=48,e.h??=48,e.a??=1;const o=Sn(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)),ee,null);i(o.el,"shtxt"),i(o.el,`ta-${e.a}`);const a=()=>t(o.el,"text"),s=t=>{const n=ae(t);Rn(e,n)||(Un(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=-Fn(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,Xt(t),On(t.el,"main",n.w,n.h,e,o),On(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&&(re(o.el,l,e.a),l=e.a),r=e.w}c!==e.h&&(a().y.baseVal[0].value=ce(e.h),e.position.y+=Fn(e.h)-Fn(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,tn(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=ae(a());Rn(e,t)||(Un(e,t),s=!0)}null==t.w||Rn(e,t)||(Un(e,t),d(),s=!0),null!=t.a&&e.a!==t.a&&(re(o.el,e.a,t.a),e.a=t.a,l=e.a,s=!0),s?(a().y.baseVal[0].value=ce(e.h),u(!0)):n&&o.draw()},o.el}function ae(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",oe);const se=t=>1===t.a?-16:2===t.a?0:16,ce=t=>24-Fn(t);function re(t,n,e){a(t,`ta-${n}`),i(t,`ta-${e}`)}function le(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 de(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 ue=(t,n)=>S(n,48,function(t){const n=v(t);return 2*(Math.abs(n.x)+Math.abs(n.y))}(t)-20);const he=()=>{if(!document.cookie.includes("dgrma=1"))return null;const t=Y(localStorage.getItem("dgrm"));return t&&new Date(t.e)>new Date?t:null},pe=()=>{const t=he()?.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},fe=t=>document.getElementById("menu").bage(t),me=t=>{const n=ge(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},ge=t=>`hsl(${[...t].reduce(((t,n)=>n.charCodeAt(0)+((t<<5)-t)),0)%360}, 97%, 47%)`,ve=(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)`},we=(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",z((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)}},ye=(t,n)=>{Lt(t,n),L(t,new CustomEvent(be,{detail:n,cancelable:!1}))},xe=(t,n)=>{Qt(n),L(t,new CustomEvent(ke,{detail:n,cancelable:!1}))},be="dgrmcu",ke="dgrmcd";async function $e(t,n,e){return function(t,n,e){let o,i;const a=Ce(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 Ce(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 He=()=>"undefined"!=typeof navigator&&(/iPad|iPhone|iPod/.test(navigator.userAgent||"")||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1),Ve=(t,n)=>Le(t,N(Ct(t)),null,n);function Le(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)?(Be(t),!0):(t.remove(),!1)))):(c=t.getBoundingClientRect(),Ee(a,Se).forEach((t=>Be(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=E(He()?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 $e(t,"dgRm",(new TextEncoder).encode(n)):t)))}const ze=["select","highlight"],Se=["select","highlight","highlight-e","highlight-s"],Be=t=>{a(t,...Se),t.querySelector(".resizer")?.remove()},Ee=(t,n)=>t.querySelectorAll(`.${n.join(", .")}`);async function Ze(t){const n=await async function(t,n){return Ce(await t.arrayBuffer(),Me(n))}(t,"dgRm");return n?(new TextDecoder).decode(n):null}const De=(t,n,e,o,i)=>{t[Oe]={k:n,n:e,e:o,m:i},Ue(e)},Te=(t,n)=>{const e=I(n.name);t[Oe]={f:n,n:e},Ue(e)},Ae=t=>{t[Oe]=void 0,Ue()},Re=t=>t[Oe],Ue=t=>{document.title=t??"DGRM - Flowchart editor"},Pe=t=>t?.replace(/[/\\?%*:|"<>]/g,"-"),Oe=Symbol(0),je=async t=>{const n=Re(t);n?.f?Ve(t,(async e=>{const o=await n.f.createWritable();await o.write(e),await o.close(),_e(t)})):n?.k?n.e?Ge((async()=>await Ke(t,n.k))):alert('You do not have access to edit this diagram. Use "Save As" to make your own copy.'):Ie(t)};function Ie(t,e){const o=he(),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=()=>Pe(n(i,"nm").value);s("dvc",(n=>Fe(t,r()))),s("cl",(async n=>{Ge((async()=>{const n=r(),e=Re(t);if(e?.k&&e.n===n&&e.m)return void await Ke(t,e.k);const o=function(){const t=new Date;return`${t.getUTCFullYear()}${(t.getUTCMonth()+1).toString().padStart(2,"0")}${j()}`}();De(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,Ct(t)),!0,!0),_e(t)}))}))}const Fe=(t,n)=>Ve(t,(async e=>{const o=Kn(e,n??"dgrm.png");if(o){const n=await o;n&&(Te(t,n),_e(t))}else _e(t)})),Ke=async(t,n)=>{let e;if(Ne(t)){const o=[...t[Xe].c,...t[Xe].u],i=Et(Mt(o.length?[...document.querySelectorAll(o.map((t=>`[id="${t}"]`)).join(","))]:null,!1),t[Xe].u);t[Xe].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),_e(t)},Ne=t=>t[Xe].c.size||t[Xe].u.size||t[Xe].d.size,Ye=t=>{t[Xe].c.clear(),t[Xe].u.clear(),t[Xe].d.clear()},Xe=Symbol(0),_e=t=>{Ye(t),pt("Saved")},Ge=async t=>{try{it(!0),await t()}catch{_n()}finally{it(!1)}},Je=navigator.clipboard.readText;async function qe(t,n){const e=`web text/dgrm${N(Mt(n,!0))}`;await async function(t){try{await navigator.clipboard.writeText(t)}catch(t){alert(t)}}(e),window.ClipboardItem&&Le(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 We=t=>{if(!t?.startsWith("web text/dgrm"))return null;try{return Y(t.slice("web text/dgrm".length))}catch{return null}};function Qe(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(),qe(t,o),o}return null}async function o(n){const o=await e(n);o&&to(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&&to(t,e)}}return c(document,"keydown",i),function(){o(),r(document,"keydown",i)}}function to(t,n){t[q].select(null),It(t,n)}const no=async(t,n)=>{const e=`${Re(t)?.n??"dgrm"}.png`;if(eo())return void await Kn(n,e);const o={files:[new File([n],e,{type:n.type})]};navigator.canShare&&navigator.canShare(o)?await navigator.share(o):await Kn(n,e)},eo=()=>screen.width>768;class oo extends HTMLElement{constructor(t,n){super(),this.g=t,this.C=n}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${Kt}Create copy
Copy to clipboard
${eo()?Nt+"Download":Yt+"Send"} 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.C()))),e("copy",(async()=>await qe(this.g,this.C()))),e("save",(()=>Le(this.g,N(Mt(this.C(),!1)),!0,(async t=>no(this.g,t))))),e("del",(()=>It(this.g,this.C())))}}customElements.define("ap-grp-settings",oo);const io=Symbol("p"),ao=t=>{t[q].select(null),t[q].select(t.children)};const so=(t,n,e)=>{if(e[io])return null;const o=wt(e),i=Mo(o);if(!i)return go(n),null;const a=xo(n,o),s=$o(n);if(s&&a)return Ho(i,e),null;if(e.shiftKey||e.ctrlKey){if(s){const t=ko(n);Vo(t),po(n,t)}return a?fo(n,i):po(n,i),wo(n,e),null}return a?lt(new oo(t,(()=>vo(n))),e.clientX-10,e.clientY-10):(go(n),po(n,i),wo(n,e),null)},co=(t,n,e,o)=>{if(e[io])return;if(o===t.ownerSVGElement)return void t[q].moveCapture();const i=xo(n,o);if(!i&&!h(e))return void t[q].moveCapture();if(!i&&h(e))return go(n),void Lo(Mo(o),e,o);const a=$o(n);i&&a?Lo(Mo(o),e,o):i&&!a&&ro(t,n,e)},ro=(t,n,e)=>{const o=()=>lo(n),i=t=>uo(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())}))},lo=t=>Mt([...t.shapesToMove,...zo(t.pathEndsToMove)],!1,!0),uo=(t,n)=>{t.shapesToMove?.forEach((t=>{n(t[kt].data.position),t[kt].drawPosition()})),t.pathEndsToMove?.forEach((t=>{n(t.data.position),So(t)[bt].drawPosition()}))},ho=()=>({shapes:new Set,paths:new Set,pathEnds:new Set,shapesToMove:new Set,pathEndsToMove:new Set}),po=(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=So(n.pathEnd);if(t.paths.has(e))return;t.pathEnds.add(n.pathEnd),i(e,Zo(n.pathEnd))}},fo=(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=So(n.pathEnd);if(t.paths.delete(e)){a(e,"highlight","highlight-s","highlight-e");const o=Eo(n.pathEnd);t.pathEnds.add(o),i(e,Zo(o))}else t.pathEnds.delete(n.pathEnd),a(e,Zo(n.pathEnd))}},mo=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(So(t))))},go=t=>{mo(t);const n=ko(t);n&&Vo(n),t.pathEnds.clear(),t.pathEndsToMove.clear(),t.paths.clear(),t.shapes.clear(),t.shapesToMove.clear()},vo=t=>[...t.shapes,...t.paths,...zo(t.pathEnds)],wo=(t,n)=>{yo(t);const e=bo(t);e&&(mo(t),Ho(e,n))},yo=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)},xo=(t,n)=>[...t.shapes,...[...t.pathEnds].map((t=>t.el)),...t.paths].some((t=>t.contains(n))),bo=t=>Co(t)?ko(t):{},ko=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)},$o=t=>!!ko(t),Co=t=>t.shapes.size||t.pathEnds.size||t.paths.size,Mo=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=Bo(t);return o?{pathEnd:o}:null},Ho=(t,n)=>{t.shape?t.shape[kt].click(n):t.path?t.path[bt].click(n):t.pathEnd&&So(t.pathEnd)[bt].click(n,t.pathEnd.el)},Vo=t=>{t.shape?t.shape[kt].unselect():t.path?t.path[bt].unselect():t.pathEnd&&So(t.pathEnd)[bt].unselect()},Lo=(t,n,e)=>{t.shape?t.shape[kt].moveCapture(n,e):t.path?t.path[bt].moveCapture(n,e):t.pathEnd&&So(t.pathEnd)[bt].moveCapture(n,e)},zo=t=>[...t].map(So),So=t=>t.el.parentNode,Bo=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},Eo=t=>{const n=So(t)[bt].data;return n.s===t?n.e:n.s},Zo=t=>So(t)[bt].data.s===t?"highlight-s":"highlight-e";function Do(t){return!!Ro(t)&&(t.c[t.i].u(),t.i-=1,!0)}function To(t){const n=Ao(t);return null!==n&&(t.i=n,t.c[n].r(),!0)}const Ao=t=>{if(!t)return null;const n=t.i+1;return n<=t.c.length-1?n:null},Ro=t=>t&&t.i>=0,Uo=(t,n,e)=>Fo(t,(()=>{jo(t,Tt(n,e)),Lt(t,e),Qt(n)}),(()=>{jo(t,Dt(n,e)),Lt(t,e)})),Po=(t,n,e)=>Fo(t,(()=>{jo(t,Dt(n,e)),Lt(t,e)}),(()=>{jo(t,Tt(n,e)),Qt(n)})),Oo=(t,n,e)=>Fo(t,(()=>{jo(t,Rt(e,n)),Lt(t,n)}),(()=>{jo(t,Rt(n,e)),Lt(t,e)})),jo=(t,n)=>{n[Io]=!0,L(t,n)},Io=Symbol(0);function Fo(t,n,e){t[Go]??={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[Go],n,e),Xo(t)}function Ko(t,n){n(t[Go])&&(t[q].select(null),Xo(t))}const No=t=>Ko(t,Do),Yo=t=>Ko(t,To);const Xo=t=>{return L(t,V(_o,(n=t[Go],{u:Ro(n),r:null!==Ao(n)})));var n},_o="dgrmh",Go=Symbol("h");function Jo(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)Wn(n.img,(t=>i(Ht(t))));else if(n?.txt){const e=function(t,n){const e=p("text");Cn(e,null,n),e.style.visibility="hidden",t.append(e);const o=ae(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 qo 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,_o,(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=>No(this.g))),c(e,"click",(t=>Yo(this.g)))}}function Wo(t,n){Vt(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",qo);const Qo=-1!==window.navigator.userAgent.indexOf(" OPR"),ti=t=>Qo&&0!==t.button,ni=(t,n,e,o,i)=>t.x<=o&&o<=t.x+n&&t.y<=i&&i<=t.y+e,ei={iceServers:[{urls:"stun:stun3.l.google.com:19302"}]},oi=`${ft}m/`;async function ii(t,n,e,o){const i=pe();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(ei),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(`${oi}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(`${oi}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:ve(t,s.get(o[1]),o[2],o[3]);break;case 4:ye(t,o[1]);break;case 5:xe(t,o[1]);break;case 0:s.set(o[1],me(o[2]));break;case 7:s.get(o[1]).remove(),s.delete(o[1]);break;case 2:Wo(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=we(t,a,(t=>n.send(t))),it(!1)},d=()=>{s.forEach((t=>t.remove())),r&&r(),c(),it(!1)};return d}async function ai(t,n,e){const o=pe();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(`${oi}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(`${oi}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(ei);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(`${oi}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=Ct(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:ve(t,s.get(n).c,o[2],o[3]),c(e.data,n);break;case 4:ye(t,o[1]),c(e.data,n);break;case 5:xe(t,o[1]),c(e.data,n);break;case 0:s.set(n,{i:o[1],n:o[2],c:me(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=we(t,a,c),d=()=>{l(),s.forEach((t=>t.c.remove())),r()};return{dispose:d,id:i}}function si(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 ci=t=>{t[li]?.dispose(),t[li]=void 0,fe(!1)},ri=t=>{ci(t),alert("Live collaboration disabled")},li=Symbol(0);function di(t){L(t,V("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[Go])&&(n.c=[],n.i=-1),Xo(t)}(t),Ae(t))}async function ui(t){he()?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 hi(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(Pe(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===Re(t)?.k&&((t,n)=>{t[Oe].n=n,Ue(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===Re(t)?.k&&Ae(t)}))}))})),it(!1)}(t):hi(t)}async function hi(t){try{const n=await Nn(Gn);if(!n)return;if(!await fi(t,n.f))return void mi();n.h?Te(t,n.h):Ae(t)}catch(t){console.log(t),mi()}}async function pi(t,n,e){return!!await fi(t,n)||(qn(t,n,e.clientX,e.clientY),!1)}async function fi(t,n){if("image/png"!==n.type)return!1;const e=await Ze(n);return!!e&&(di(t),Wo(t,Y(e)),!0)}const mi=()=>alert("File cannot be read. Use the exact image file you got from the application.");async function gi(t){let e=!0,o={v:!0};const i=Re(t),a=i?.k;a&&(o={},he()&&(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 vi extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=he(),e=t||Yn,o=this.attachShadow({mode:"closed"});o.innerHTML=`
New
Open
${e?'
Save
':""}
Save${Xn||e?" as":""}
${eo()?Nt+"Download":Yt+"Send"} image
Share link
Live collaboration
${t?t.n:"Log in"}
`;const i=t=>n(o,t);this.M=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",(()=>{di(this.g)})),e&&u("save",(async()=>await je(this.g))),u("saveas",(()=>Ie(this.g))),u("open",(async()=>await ui(this.g))),u("collab",(async t=>await async function(t){const n=()=>ci(t),e=()=>ri(t);try{switch(t[li]?.clientType){case 1:si(t[li].id,n);break;case 2:si(t[li].id,n,!0);break;default:{const o=await ai(t,n,e);if(!o)return;c(t,"dgrmn",e,!0),t[li]={clientType:1,id:o.id,dispose:()=>{r(t,"dgrmn",e),o.dispose()}},fe(!0),si(t[li].id,n);break}}}catch{e()}}(this.g))),u("login",(t=>function(){const t=he();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=>gi(this.g))),u("down",(t=>(t=>Ve(t,(async n=>await no(t,n))))(this.g)))}bage(t){const n=t?i:a;n(this.M("menu"),"bage"),n(this.M("collab"),"bage")}}customElements.define("ap-menu",vi);class wi extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML='';const n=t.getElementById("menu");n.querySelectorAll('[data-cmd="shapeAdd"]').forEach((t=>{c(t,"pointerdown",this),c(t,"click",this)})),c(n,"pointerleave",this),c(n,"pointerup",this),c(n,"pointermove",this)}handleEvent(t){switch(t.type){case"pointermove":if(!this.H){const n=document.elementFromPoint(t.clientX,t.clientY);if(n===this.V)return;this.L===this.V&&this.g.ownerSVGElement.setPointerCapture(t.pointerId),this.V=n}break;case"pointerleave":if(this.H=!0,null!=this.S){const n=yi(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.L=document.elementFromPoint(t.clientX,t.clientY),this.V=this.L,this.H=null;break;case"pointerup":this.B();break;case"click":this.g.ownerSVGElement.focus(),this.g[q].select(yi(this.g,parseInt(d(t,"data-cmd-arg")),!0))}}B(){this.S=null,this.L=null,this.V=null}}customElements.define("ap-menu-shape",wi);const yi=(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 xi=document.getElementById("canvas");var bi,ki,$i;xi[q]={data:{position:{x:0,y:0},scale:1,cell:24},shapeMap:function(n){const e=t=>(n.append(t),t),a=t=>(en(n,t),t);return{0:{create:t=>e(pn(n,t))},1:{create:i=>e(function(n,e){e.r??=48;let i,a=e.r;const s=Sn(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,Xt(s),Dn(s.el,"outer",e.r+24),Dn(s.el,"main",e.r),s.draw()}return 48!==e.r?c():s.draw(),s.el[kt].patch=t=>{const n=qt(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(An(n,t))},3:{create:t=>e(ie(n,t))},4:{create:a=>e(function(n,e){e.w??=96;let a,s=e.w;const c=Sn(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&&ue(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=ue(t,s);n!==e.w&&(e.w=n,r())}));function r(){const t=de(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,Xt(c);const n=de(e.w,9);le(c.el,"main",n),le(c.el,"border",n),le(c.el,"outer",de(e.w,-24)),c.draw()}return i(c.el,"shrhomb"),96!==e.w?r():c.draw(),c.el[kt].patch=t=>{const n=qt(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(jn(n,t))},6:{create:t=>a(Qn(n,t))}}}(xi)},xi.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)||Jo(t,(t=>{const n=t.getData("text/plain"),e=We(n);return e?{data:e}:t.files?.length&&t.files.item(0).type?.startsWith("image/")?{img:t.files.item(0)}:n?{txt:n}:null})(n.clipboardData))}));const n=z((async()=>await je(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(),No(t);break;case"KeyY":ct(),Yo(t);break;case"KeyS":ct(),e.shiftKey?Ie(t):n();break;case"KeyA":ct(),ao(t)}}))}(xi),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[io]||!s)return void g();t[io]=!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[io]=!0;const e=n=>ni(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=ho(),a=t=>po(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||ti(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()}))}(xi),function(t){const n=t.ownerSVGElement;let e,o=ho();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=so(t,o,s),i(),e=c},c(n,"pointerup",s,!0),a=e=>{r(n,"pointerup",s),co(t,o,e,wt(l))},0===l.button&&c(n,"pointermove",a,!0)})),Qe(t,(()=>vo(o)),(()=>{e?i():Co(o)&&!ko(o)&&t[q].select(null)}));const l=t=>{go(o),o=t??ho(),wo(o,null)};t[q].selectedSet=l,t[q].select=t=>{i();const n=ho();if(t?.length)for(const e of t)po(n,Mo(e));l(n)}}(xi),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=()=>{n.position.x-=t.deltaX,n.position.y-=t.deltaY,o()},a=e=>i(n.scale+e,nt(t));Math.abs(t.deltaY)>50?t.ctrlKey?e():a(t.deltaY<0?.25:-.25):t.ctrlKey?a(-.01*t.deltaY):e()})),t[q].move=function(t,e,i){n.position.x=t,n.position.y=e,n.scale=i,o()},t[q].scale=i}(xi),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(`${Je?'
Paste
':""}
Select all
Add image
`),s=(t,e)=>{c(n(a,t),"click",(async t=>{await e(),i()}))};Je&&s("p",(async()=>Jo(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=We(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()=>ao(o))),s("i",(async()=>async function(t,n,e){const o=await Nn(Jn);o&&qn(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))}))}(xi),(t=>{const n=(n,e)=>c(t,n,(t=>{t[Io]||e(t)}));n("dgrmc",(n=>Uo(t,n.detail.newIds,n.detail.shapesToCreateData))),n("dgrmd",(n=>Po(t,n.detail.delIds,n.detail.shapesToDelData))),n("dgrmu",(n=>Oo(t,n.detail.shapesBeforeUpdData,n.detail.shapesToUpdData)))})(xi),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 pi(t,await e.getFile(),n)&&Te(t,e)}else{const e=n.dataTransfer.items[0].getAsFile();await pi(t,e,n)&&Ue(I(e.name))}else mi()}))}(xi),(t=>{t[Xe]={c:new Set,u:new Set,d:new Set};const n=n=>{t[Xe].c.add(n),t[Xe].u.delete(n),t[Xe].d.delete(n)},e=n=>{t[Xe].c.has(n)||t[Xe].u.add(n),t[Xe].d.delete(n)},o=n=>n?.forEach((n=>{t[Xe].d.add(n),t[Xe].c.delete(n),t[Xe].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,be,(t=>i(t.detail))),c(t,ke,(t=>o(t.detail))),t.addEventListener("dgrmn",(n=>{!Ne(t)||window.confirm("Changes you made may not be saved. Continue?")?Ye(t):n.preventDefault()})),window.addEventListener("beforeunload",(n=>{Ne(t)&&n.preventDefault()}))})(xi);{const t=new URL(window.location.href),n=n=>t.searchParams.get(n);n("usr")&&(bi=n("usr"),ki=n("usrn"),$i=parseInt(n("expr")),localStorage.setItem("dgrm",N({l:bi,n:ki,e:$i})),k("usr","usrn","expr"))}(t=>{const n=new vi(t);n.id="menu",g(n)})(xi),g(new qo(xi)),g(new wi(xi));{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);Wo(xi,n.d),n.p&&De(xi,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=()=>ri(t);try{const o=await ii(t,n,(()=>ci(t)),e);if(!o)return;c(t,"dgrmn",e,!0),t[li]={clientType:2,id:n,dispose:()=>{r(t,"dgrmn",e),o()}},fe(!0)}catch{e()}}(xi,t)}else he()||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,p=t=>({x:t.clientX,y:t.clientY});function f(t,n){const e=document.createElementNS("http://www.w3.org/2000/svg",t);return n&&(e.innerHTML=n),e}const m=(t,n)=>{const e=document.createElement("div");return t&&(e.innerHTML=t),e.style.cssText=n,e},g=(...t)=>document.body.append(...t);function v(t){let n,e=0;for(const o of t.getElementsByTagName("tspan"))for(const t of w(o.getBBox())){const o=Math.abs(t.x)+Math.abs(t.y);e[{x:t.x,y:t.y},{x:t.right,y:t.y},{x:t.x,y:t.bottom},{x:t.right,y:t.bottom}];function y(t){const n={x:1/0,y:1/0},e={x:-1/0,y:-1/0};for(const o of t){const t=o.getBoundingClientRect();n.x>t.x&&(n.x=t.x),n.y>t.y&&(n.y=t.y),e.x{let e;(n=n||{}).credentials="include";try{e=await fetch(t,n)}catch(t){throw console.log(t),t}if(!e.ok)throw 401===e.status&&alert("You have to relogin."),new Error;return e},b=(t,n)=>x(t,{method:"POST",body:n?Y(n):null}),k=(t,n)=>{const e=new URL(window.location.href);return e.searchParams.set(t,n),e.toString()},$=(...t)=>{const n=new URL(window.location.href);t?.forEach((t=>n.searchParams.delete(t))),history.replaceState(null,null,n.toString())};function M(t,n,e,o){function i(n){r(t,"pointercancel",i),r(t,"pointerup",i),r(t,"wheel",i),r(document,"pointerdown",i),r(t,"pointermove",e),o(n)}c(t,"pointercancel",i,!0),c(t,"pointerup",i,!0),c(t,"wheel",i,!0),c(document,"pointerdown",i,!0),c(t,"pointermove",e),t.setPointerCapture(n.pointerId)}const C=(t,n)=>H(t.ownerSVGElement,t,n);function H(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 L=t=>{const n=n=>{"Escape"===n.key&&t()};return c(document,"keydown",n),()=>r(document,"keydown",n)},V=(t,n,e)=>new CustomEvent(t,{detail:n,cancelable:e}),z=(t,n)=>t.dispatchEvent(n),S=(t,n)=>{let e,o,i=!1;return function a(){if(i)return e=arguments,void(o=this);t.apply(this,arguments),i=!0,setTimeout((function(){i=!1,e&&(a.apply(o,e),e=o=null)}),n)}};function Z(t,n,e){return t??=0,e<=t?t:t+Math.ceil((e-t)/n)*n}const B=t=>Math.round(100*(t+Number.EPSILON))/100,E=(t,n,e)=>{let o=!1;return n>e?n>t&&(o=!0,e*=t/n,n=t):e>t&&(o=!0,n*=t/e,e=t),{w:n,h:e,c:o}};function D(t,n){for(let e=t.length-1;e>=0;--e)n(t[e])}const T=t=>t.values().next().value;function A(t,n,e){const o=e??1;return t.x+=o*n.x,t.y+=o*n.y,t}const R=(t,n)=>({x:t.x+n,y:t.y+n}),U=(t,n)=>P(t,n.x,n.y),P=(t,n,e)=>(t.x=n,t.y=e,t),O=t=>({x:t.x,y:t.y}),I=(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 F=t=>t?.substring(0,t.lastIndexOf("."))||t,N=t=>X(Y(t)),K=t=>t&&!!Object.keys(t).length,Y=t=>JSON.stringify(t),X=t=>JSON.parse(t),_=(t,n,e,o)=>{let i,a;const s=()=>{a=setTimeout((async t=>{await e(),i||s()}),t)};s();const c=setTimeout((t=>{r(),o&&o()}),n),r=()=>{i=!0,clearTimeout(a),clearTimeout(c)};return r};function G(t,n,e){t.x+=e[q]/n,t.y+=e[J]/n}const q=Symbol("movementX"),J=Symbol("movementY"),W=Symbol("Canvas"),Q=(t,n,e)=>({x:(n-t.position.x)/t.scale,y:(e-t.position.y)/t.scale}),tt=(t,n)=>Q(t,n.clientX,n.clientY);function nt(t,n){const e=n/2;function o(t){const o=Math.round(t/n)*n;return t-o>=0?o+e:o-e}return t.x=o(t.x),t.y=o(t.y),t}function et(t,n){return{id:t.pointerId,pos:p(t),shift:{x:n.position.x-t.clientX,y:n.position.y-t.clientY}}}let ot;function it(t){t&&!ot?(ot=m("","z-index: 2; position: fixed; left: 0; top: 0; width:100%; height:100%; background: #fff; opacity: 0"),ot.classList.add("blnk"),g(ot)):t||(ot?.remove(),ot=null)}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)=>H(t,n,e,o))(t,n,e,o),a=L((()=>{((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 x(`${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,x(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=N(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:N(o.data)}):o.shape?(e&&a(o.shape.shapeEl),{s:o.shape.shapeEl[kt].data.id,k:o.shape.connectorKey}):{p:N(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=N(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)=>Vt(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 Vt(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=Zt(t,n.s[e],a);o&&s.push(o);break}default:i(n.s[e])}return[...o.values(),...s]}function zt(t,n){const o=e(n.id);return o?((o[kt]||o[bt]).patch(n),o):t[W].shapeMap[n.type].create(n)}const St=(t,n)=>zt(t,N(n)),Zt=(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:O(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?zt(t,o):null};function Bt(t){for(const n in t.s)Et(t.s[n]);return t}function Et(t){switch(t.type){case 6:delete t.d,delete t.r;break;case 7:delete t.d}}const Dt=(t,n,e)=>z(t,Tt(n,e)),Tt=(t,n)=>V("dgrmc",{newIds:t,shapesToCreateData:n}),At=(t,n)=>V("dgrmd",{delIds:t,shapesToDelData:n}),Rt=(t,n,e)=>z(t,Ut(Bt(n),Bt(e))),Ut=(t,n)=>V("dgrmu",{shapesBeforeUpdData:t,shapesToUpdData:n}),Pt=(t,n,e,o)=>V("dgrmdc",{delIds:t,shapesToDelData:n,newIds:e,shapesToCreateData:o});function Ot(t,n,e,o){const i=A(((t,n,e)=>{const o=Q(t,n,e);return nt(o,t.cell),o})(t[W].data,e,o),function(t){const n=function(t){const n={x:1/0,y:1/0},e={x:-1/0,y:-1/0};return It(t,(t=>{t&&(n.x>t.x&&(n.x=t.x),n.y>t.y&&(n.y=t.y),e.x{n&&nt(A(n,i),t[W].data.cell)}))}function It(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 jt=(t,n)=>t[W].select(Ft(t,Ct(n,!0),!0));function Ft(t,n,e,o,i){return Ot(t,n,null==o?window.innerWidth/2:o,null==i?window.innerHeight/2:i),Nt(t,n,e)}function Nt(t,n,e){return e&&Dt(t,Gt(n),N(n)),Kt(t,n)}function Kt(t,n){return t[W].select(null),Vt(t,n)}function Yt(t,n){((t,n,e)=>{z(t,At(n,e))})(t,_t(n),Ct(n,!1,!0)),qt(n)}function Xt(t,n,e){((t,n,e,o,i)=>{z(t,Pt(n,e,o,i))})(t,_t(n),Ct(n,!1,!0),Gt(e),N(e)),qt(n),Kt(t,e)}const _t=t=>t.map((t=>(t[kt]||t[bt]).data.id)),Gt=t=>Object.keys(t.s),qt=t=>t.forEach((t=>(t[kt]||t[bt]).del())),Jt='',Wt='',Qt='',tn='';function nn(n){for(const e in n.cons)o(t(n.el,e),n.cons[e].position)}function en(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 on=(t,n)=>({del:at(t,n.clientX-66,n.clientY-10)[ht].del}),an=(t,n,e)=>{const o=Ct([n],!1,!1);(n[kt]??n[bt]).patch(e),Rt(t,o,Ct([n],!1,!1))};function sn(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,ln(t.el))}(t,n),cn(t,n)}function cn(t,n){const e=n.position&&!I(t.el[kt].data.position,n.position);return e&&U(t.el[kt].data.position,n.position),e}const rn=t=>t.forEach((t=>{const n=e(t);n&&(n[kt]||n[bt]).del()}));function ln(t){const n=(t[kt]??t[bt]).data;n.styles||(n.styles=[]),D(t.classList,(n=>{n?.startsWith("d-")&&t.classList.remove(n)})),i(t,...n.styles)}const dn=(t,n,e)=>{const o=(n[kt]??n[bt]).data;o.styles||(o.styles=[]);const i={styles:N(o.styles)};an(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 un(t,n){const e=Number(n.getAttribute("bg"));if(3===e)return void t.append(n);const o=t.querySelector(hn(e+1,3).map((t=>`[bg="${t}"]`)).join());o?o.before(n):t.append(n)}const hn=(t,n)=>[...Array(n-t+1).keys()].map((n=>n+t));class pn 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":dn(this.g,this.o,t.detail.arg);break;case"del":Yt(this.g,[this.o]);break;case"copy":jt(this.g,[this.o])}})),l(n,"[data-cmd]",(t=>{const n=d(t,"data-cmd-arg"),e={styles:this.o[bt].data.styles?N(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")),an(this.g,this.o,e)}))}}function fn(t,n,e,o){const i=xn(t,n);i.shape={shapeEl:e,connectorKey:o},vn(t,i)}function mn(t,n,e){const o=xn(t,n);o.shape.shapeEl!==bn(t,o)&&wn(t,o),o.shape=null,o.data={dir:o.data.dir,position:e}}function gn(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&&mn(t,o,a.data.position):function(t,n,e,o){const i=xn(t,n);if(i.shape.shapeEl===e&&i.shape.connectorKey===o)return;i.shape.shapeEl!==e&&i.shape.shapeEl!==bn(t,i)&&wn(t,i);i.shape={shapeEl:e,connectorKey:o},vn(t,i)}(t,o,a.shape.shapeEl,a.shape.connectorKey):null!=a.shape?fn(t,o,a.shape.shapeEl,a.shape.connectorKey):U(i.data.position,a.data.position)};o("s"),o("e")}function vn(t,n){n.data=yn(n.shape).pathAdd(n.shape.connectorKey,t)}customElements.define("ap-path-settings",pn);const wn=(t,n)=>yn(n.shape)?.pathDel(t),yn=t=>t?.shapeEl[kt],xn=(t,n)=>t[bt].data[n],bn=(t,n)=>(t[bt].data.e===n?t[bt].data.s:t[bt].data.e).shape?.shapeEl;function kn(n,e){const o=f("g",'');o.id=e.id,i(o,"shpath"),e.s.el=t(o,"start"),e.e.el=t(o,"end"),e.styles=e.styles??["d-arw-e"];const s=zn(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 Sn(e,-.8,.8)?"left":Sn(e,.8,2.4)?"top":Sn(e,2.4,3.2)||Sn(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=Vn(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))),Cn(e.s),Cn(e.e)}let r,l;let d,u,p=0;const m=()=>{u&&(u(),u=null)},g=t=>{m(),u=L(t)};function v(t){switch(p){case 0:y();break;case 1:p=2,d=C(o,(t=>w(!0))),g((()=>{w(!0),y()})),function(t){r||(r=on(new pn(n,o),t))}(t)}}function w(t){t||(l=null),p=0,a(o,"select","highlight-s","highlight-e"),Hn(e.s,1),Hn(e.e,1),r?.del(),r=null,d&&(d(),d=null),m()}function y(){p=1,i(o,"s"===l?"highlight-s":"e"===l?"highlight-e":"select"),Hn(e.s,2),Hn(e.e,2),un(n,o),g((()=>{w(!0)}))}return o[bt]={data:e,drawPosition:c,click:(t,n)=>{if(0===p||l){const o=n||t?Zn(e,n??wt(t)):null;Bn(l,o)||w(),l=o}v(t)},patch:t=>{e.styles&&(e.styles=t.styles,ln(o)),t.s&&gn(o,t),c()},moveCapture:(t,i,a)=>{w(!0);const s=a?null:Ct([o],!1,!0),r=t=>{w(!0),s?Rt(n,s,Ct([o],!1,!0)):Dt(n,[e.id],Ct([o],!1,!0)),h(t)||v()},l=Zn(e,i);l?Mn(n,o,c,t,l,r):function(t,n,e,o,i){M(n,o,(o=>{!function(t,n,e,o){const i=n=>G(n,t.scale,o);$n(n.s,i),n.s.shape?.shapeEl&&n.s.shape?.shapeEl===n.e.shape?.shapeEl||$n(n.e,i);n.s.shape||n.e.shape||e()}(t[W].data,n[bt].data,e,o)}),(o=>{!function(t,n,e){const o=n=>nt(n,t.cell);$n(n.s,o),$n(n.e,o),n.s.shape&&n.e.shape||e()}(t[W].data,n[bt].data,e),i(o)}))}(n,o,c,t,r)},unselect:w,del:function(){w(),wn(o,e.s),wn(o,e.e),o.remove()}},e.styles&&i(o,...e.styles),e.s.shape&&vn(o,e.s),e.e.shape&&vn(o,e.e),c(),o}function $n(t,n){t.shape?(n(yn(t.shape).data.position),yn(t.shape).drawPosition()):n(t.data.position)}const Mn=(t,n,e,o,l,d)=>{n[bt].data[l].shape&&mn(n,l,tt(t[W].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);M(n[bt].data[l].el,o,(o=>{G(n[bt].data[l].data.position,t[W].data.scale,o),e()}),(o=>{const i=yt(o),a=i?.getAttribute("data-connect");a?fn(n,l,i.parentElement,a):nt(n[bt].data[l].data.position,t[W].data.cell),e(),u(),n.style.pointerEvents="unset",d(o)}))};function Cn(t){t.el.style.transform=`translate(${t.data.position.x}px, ${t.data.position.y}px) rotate(${Ln(t.data.dir)}deg)`}function Hn(t,n){t.el.firstElementChild.setAttribute("data-evt-index",n.toString())}const Ln=t=>"right"===t?180:"left"===t?0:"bottom"===t?270:90,Vn=t=>"left"===t?"right":"right"===t?"left":"top"===t?"bottom":"top";const zn=(n,...e)=>e.map((e=>t(n,e))),Sn=(t,n,e)=>n<=t&&t<=e,Zn=(t,n)=>t.e.el.contains(n)?"e":t.s.el.contains(n)?"s":null,Bn=(t,n)=>!t||t===n;function En(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 Dn=(t,n)=>en(n,An(t,n)),Tn=(t,n,e)=>on(An(t,n),e);function An(t,n){const e=new Rn;return c(e,"cmd",(e=>{switch(e.detail.cmd){case"style":dn(t,n,e.detail.arg);break;case"del":Yt(t,[n]);break;case"copy":jt(t,[n])}})),e}class Rn extends HTMLElement{connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${Wt} ${Jt}
`;{const n=t.getElementById("pnl");let e;l(t,"[data-toggle]",(o=>{const i=t=>{const e=st(this);e.style.top=`${e.getBoundingClientRect().top+window.scrollY+t*n.getBoundingClientRect().height}px`};e&&(i(1),Un(e,!1));const a=t.getElementById(d(o,"data-toggle"));e!==a?(Un(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 Un(t,n){t.style.display=n?"unset":"none"}function Pn(n,e,o,i,a,s,c,r,l=0){const d=In(e.id,o,i),u={el:t(d,"text"),vMid:l},h=()=>En(u.el,u.vMid,e.title);h();return{el:d,cons:i,draw:jn(n,d,e,i,u,r??Dn,(()=>c(u.el)),a,s,null).draw,drawTxt:h}}function On(t,n,e,o,i,a,s,c,r){const l=In(n.id,e,o);return{el:l,cons:o,draw:jn(t,l,n,o,null,c??Tn,null,i,a,s,r).draw}}customElements.define("ap-shape-edit",Rn);const In=(t,n,e)=>{const o=f("g",`${n}\n\t${Object.entries(e).map((t=>``)).join()}`);return o.id=t,o};function jn(t,n,e,s,c,r,l,d,u,p,m){let g,v,w,y;e.styles??=[];const x=()=>Ct([n],!1,!1);function b(t){e.title=t,l()}const k=function(t,n,e,s,c,r,l,d,u,p){i(n,"hovertrack");const m=N(s),g=new Set;let v,w,y,x=0;function b(){v?.remove(),v=null}const k=()=>{y&&(y(),y=null)},$=t=>{k(),y=L(t)};function H(){n.style.transform=`translate(${e.position.x}px, ${e.position.y}px)`;for(const t in s)m[t].position={x:s[t].position.x+e.position.x,y:s[t].position.y+e.position.y};for(const t of g)t[bt].drawPosition()}function V(){x=1,i(n,"select"),l&&(v=function(t,n){const e=f("circle");return e.setAttribute("data-evt-index","2"),i(e,"resizer"),o(e,n),t.append(e),e}(n,l())),$((()=>z()))}function z(t){r(),x=0,a(n,"select","highlight"),t||b(),w&&(w(),w=null),k()}const S=t=>{switch(x){case 0:V();break;case 1:x=2,a(n,"select"),i(n,"highlight"),b(),w=C(n,(t=>z())),$((()=>{z(),V()}))}c(t,x)},Z=(e,o,i)=>{if(o===v){z(!0);const o=Ct([n],!1,!1);return void M(v,e,d,(e=>{u&&u(),b(),V(),Rt(t,o,Ct([n],!1,!1))}))}z();const a=o.getAttribute("data-connect");if(a){const o=kn(t,{id:j(),type:0,s:{shape:{shapeEl:n,connectorKey:a}},e:{data:{dir:Vn(m[a].dir),position:tt(t[W].data,e)}}});return n.parentNode.append(o),t[W].select(h(e)?null:[o]),o[bt].moveCapture(e,o[bt].data.e.el,!0),void g.add(o)}B(e,i)};p??=()=>nt(e.position,t[W].data.cell);const B=(o,i)=>{const a=i?null:Ct([n],!1,!1);M(n,o,(n=>{G(e.position,t[W].data.scale,n),H()}),(o=>{p(),H(),a?Rt(t,a,Ct([n],!1,!1)):Dt(t,[e.id],Ct([n],!1)),h(o)||V()}))};return n[kt]={data:e,drawPosition:H,click:S,moveCapture:Z,unselect:z,del:function(){z();for(const t of g)t[bt].del();n.remove()},paths:g,pathAdd:function(t,n){return g.add(n),m[t]},pathDel:function(t){g.delete(t)}},H}(t,n,e,s,((o,i)=>{1===i&&un(t,n),2===i&&(c&&!g&&(w=e.title,v=x(),g=function(t,n,e,o,i){let a=f("foreignObject");const s=document.createElement("textarea"),c=()=>function(t,n,e,o,i){const a=t.getBBox(),s=a.width+20;n.width.baseVal.value=s+2*o+2,n.x.baseVal.value=a.x-o-("center"===i?10:"right"===i?20:0),n.height.baseVal.value=a.height+2*o+3,n.y.baseVal.value=a.y-o,e.style.width=`${s}px`,e.style.height=`${a.height}px`}(t,a,s,l,r.textAlign);s.value=e||"",s.oninput=function(){En(t,n,s.value),o(s.value),c()},i&&(s.onblur=function(){i(s.value)}),s.onpointerdown=function(t){t.stopImmediatePropagation()},a.appendChild(s),t.parentElement.appendChild(a);const r=getComputedStyle(s),l=parseInt(r.paddingLeft)+parseInt(r.borderWidth);return c(),s.focus(),{dispose:()=>{a.remove(),a=null},draw:c}}(c.el,c.vMid,e.title,b)),y||(y=r(t,n,o)))}),(function(){g?.dispose(),g=null,y?.del(),y=null,v&&w!==e.title&&Rt(t,v,x()),v=null,w=null}),d,u,p,m);return e.styles&&i(n,...e.styles),{draw:()=>{k(),y?.updPos&&y?.updPos(),g?.draw()}}}function Fn(n,e,o){t(n,e).r.baseVal.value=o}const Nn=(t,n)=>function(t,n,e){const o=v(t);return Z(n,e,Math.sqrt(o.x**2+o.y**2))}(t,n,24);function Kn(n,e){e.w??=96,e.h??=48;const a={w:e.w,h:e.h};let s;const c=Pn(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=A(tt(t[W].data,i),n,-1),c={};let r=!1;function l(t){s[t]<48&&(s[t]=48);const o=48*Math.round(s[t]/48),i="x"===t?"w":"h";e[i]!==o&&(!a||a()[i]<=o)&&(e[i]=o,e.position[t]=n[t]+o/2,r=!0),c[t]=s[t]-e[i]/2}return l("x"),l("y"),o(i.target,c),r})(n,s,e,i,(()=>(l??=_n(t(c.el,"text")),l)))&&(Xn(a,e),r())}),(t=>{const n=_n(t,a);Yn(e,n)||(Xn(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,nn(t),Gn(t.el,"main",n.w,n.h,e,o),Gn(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=sn(c,t),o=(null!=t.w||null!=t.h)&&!Yn(e,t);o&&Xn(e,t),o?r():n&&c.draw()},c.el}const Yn=(t,n)=>t.w===n.w&&t.h===n.h,Xn=(t,n)=>{t.h=n.h,t.w=n.w};function _n(t,n){const e=t.getBBox();return{w:Z(n?.w,48,e.width),h:Z(n?.h,48,e.height)}}const Gn=(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 qn(t,n){let e;n.w??=96,n.h??=96;const a=On(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=A(tt(t[W].data,i),n,-1);e.w=a.x<48?48:a.x;const s=e.w/2;e.position.x=n.x+s,e.h=a.y<48?48:a.y;const c=e.h/2;return e.position.y=n.y+c,o(i.target,{x:s,y:c}),{x:-s,y:-c}}(t,e,n,i))),(t=>s(function(t,n){const e=t=>24*Math.round(t/24);t.w=e(t.w),t.h=e(t.h);const o=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");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,nn(a),Gn(a.el,"main",n.w,n.h,t.x,t.y),Gn(a.el,"selected",n.w,n.h,t.x,t.y),Gn(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=sn(a,t),o=(null!=t.w||null!=t.h)&&!Yn(n,t);o&&Xn(n,t),o?s(Jn(n).tInner):e&&a.draw()},a.el}function Jn(t){const n=Wn(t.w),e=Wn(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 Wn=t=>24*Math.floor(t/2/24);function Qn(t,n){return ee?async function(t,n){try{const e=await window.showSaveFilePicker({suggestedName:n,types:[ie]}),o=await e.createWritable();return await o.write(t),await o.close(),e}catch(t){"AbortError"!==t.name&&oe()}}(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 te(t){const n="AbortError";try{return ne?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 ne="showOpenFilePicker"in window,ee="showSaveFilePicker"in window,oe=()=>alert("File not saved"),ie={accept:{"image/png":[".png"]},inputAccept:"image/png"},ae={accept:{"image/*":[".png",".gif",".jpeg",".jpg"]},inputAccept:"image/*"};function se(t,n,e){const o=m(`
${Wt} ${Jt}
`);return l(o,"[data-cmd]",(e=>{switch(d(e,"data-cmd")){case"del":Yt(t,[n]);break;case"copy":jt(t,[n])}})),on(o,e)}function ce(t,n,e,o){re(n,(n=>t[W].select(Ft(t,Ht(n),!0,e,o))))}function re(t,n){!function(t,n,e){const o=new Image;o.onload=function(){const i=E(n,o.width,o.height);if(!i.c){URL.revokeObjectURL(o.src);const n=new FileReader;return n.onloadend=function(){e(n.result,i.w,i.h)},void n.readAsDataURL(t)}const a=document.createElement("canvas");a.width=i.w,a.height=i.h;const s=a.getContext("2d");s.imageSmoothingEnabled=!1,s.imageSmoothingQuality="high",s.drawImage(o,0,0,i.w,i.h),URL.revokeObjectURL(o.src),e(a.toDataURL(),i.w,i.h)},o.src=URL.createObjectURL(t)}(t,2040,((t,e,o)=>{const i=E(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:B(o/e),d:t};de(a),n(a)}))}function le(t,n){let e;const a=On(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=A(tt(t[W].data,i),n,-1);e.w=a.x<48?48:a.x;const s=e.w/2;e.position.x=n.x+s,e.h=e.r*e.w;const c=e.h/2;return e.position.y=n.y+c,o(i.target,{x:s,y:c}),{x:-s,y:-c}}(t,e,n,i))),(t=>s(function(t,n){de(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))),se);i(a.el,"shimg");const s=t=>{a.cons.right.position.x=n.w+t.x,a.cons.left.position.x=t.x,a.cons.bottom.position.y=n.h+t.y,a.cons.top.position.y=t.y,nn(a),Gn(a.el,"main",n.w,n.h,t.x,t.y),Gn(a.el,"selected",n.w,n.h,t.x,t.y),Gn(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=cn(a,t),o=(null!=t.w||null!=t.h)&&!Yn(n,t);o&&Xn(n,t),o?s(Jn(n).tInner):e&&a.draw()},a.el}function de(t){t.w=24*Math.round(t.w/24),t.h=B(t.r*t.w)}const ue=(t,n)=>en(n,new he(t,n));class he 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":dn(this.g,this.$,t.detail.arg);break;case"del":Yt(this.g,[this.$]);break;case"copy":jt(this.g,[this.$])}}));const o=t=>an(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?N(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 pe(n,e){e.w??=48,e.h??=48,e.a??=1;const o=Pn(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)),ue,null);i(o.el,"shtxt"),i(o.el,`ta-${e.a}`);const a=()=>t(o.el,"text"),s=t=>{const n=fe(t);Yn(e,n)||(Xn(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=-Wn(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,nn(t),Gn(t.el,"main",n.w,n.h,e,o),Gn(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&&(ve(o.el,l,e.a),l=e.a),r=e.w}c!==e.h&&(a().y.baseVal[0].value=ge(e.h),e.position.y+=Wn(e.h)-Wn(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&&!I(e.position,t.position);n&&U(e.position,t.position);let i=!1;t.styles&&(e.styles=t.styles,ln(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=fe(a());Yn(e,t)||(Xn(e,t),s=!0)}null==t.w||Yn(e,t)||(Xn(e,t),d(),s=!0),null!=t.a&&e.a!==t.a&&(ve(o.el,e.a,t.a),e.a=t.a,l=e.a,s=!0),s?(a().y.baseVal[0].value=ge(e.h),u(!0)):n&&o.draw()},o.el}function fe(t){const n=t.getBBox();return{w:Z(0,48,n.width+12),h:Z(0,24,n.height+14)}}customElements.define("ap-rect-txt-settings",he);const me=t=>1===t.a?-16:2===t.a?0:16,ge=t=>24-Wn(t);function ve(t,n,e){a(t,`ta-${n}`),i(t,`ta-${e}`)}function we(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 ye(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 xe=(t,n)=>Z(n,48,function(t){const n=v(t);return 2*(Math.abs(n.x)+Math.abs(n.y))}(t)-20);async function be(t,n,e){return function(t,n,e){let o,i;const a=ke(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(),$e(n),e)}function ke(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 $e(t){return new DataView((new TextEncoder).encode(t).buffer).getUint32(0)}const Me=()=>"undefined"!=typeof navigator&&(/iPad|iPhone|iPod/.test(navigator.userAgent||"")||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1),Ce=(t,n)=>He(t,Y(Mt(t)),null,n);function He(t,n,e,o){const i=t.ownerSVGElement.cloneNode(!0),a=i.querySelector("#canvas");let c;i.style.backgroundImage=null,e?(c=y(Se(t,Le)),D(a.children,(t=>((t,n)=>n.some((n=>s(t,n))))(t,Le)?(ze(t),!0):(t.remove(),!1)))):(c=t.getBoundingClientRect(),Se(a,Ve).forEach((t=>ze(t))));const r=i.getElementsByTagName("foreignObject");for(;r[0];)r[0].parentNode.removeChild(r[0]);const l=t[W].data,d=1/l.scale;a.style.transform=`matrix(1, 0, 0, 1, ${d*(l.position.x+15*l.scale-c.x)}, ${d*(l.position.y+15*l.scale-c.y)})`,function(t,n,e,o){const i=E(Me()?4096:10836,n.width*e*window.devicePixelRatio,n.height*e*window.devicePixelRatio),a=new Image;a.width=n.width,a.height=n.height,a.onload=function(){const t=document.createElement("canvas");t.width=i.w,t.height=i.h,t.style.width=`${i.w}px`,t.style.height=`${i.h}px`;const e=t.getContext("2d");e.imageSmoothingEnabled=!1,e.drawImage(a,n.x,n.y,n.width,n.height,0,0,i.w,i.h),URL.revokeObjectURL(a.src),t.toBlob(o,"image/png")},t.width.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,a.width),t.height.baseVal.newValueSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PX,a.height),a.src=URL.createObjectURL(new Blob([(new XMLSerializer).serializeToString(t)],{type:"image/svg+xml;charset=utf-8"}))}(i,{x:0,y:0,height:c.height/l.scale+30,width:c.width/l.scale+30},3,(async t=>o(n?await be(t,"dgRm",(new TextEncoder).encode(n)):t)))}const Le=["select","highlight"],Ve=["select","highlight","highlight-e","highlight-s"],ze=t=>{a(t,...Ve),t.querySelector(".resizer")?.remove()},Se=(t,n)=>t.querySelectorAll(`.${n.join(", .")}`);async function Ze(t){const n=await async function(t,n){return ke(await t.arrayBuffer(),$e(n))}(t,"dgRm");return n?(new TextDecoder).decode(n):null}const Be=navigator.clipboard.readText;async function Ee(t,n){const e=`web text/dgrm${Y(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 De=t=>{if(!t?.startsWith("web text/dgrm"))return null;try{return X(t.slice("web text/dgrm".length))}catch{return null}};function Te(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(),Ee(t,o),o}return null}async function o(n){const o=await e(n);o&&Ae(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&&Ae(t,e)}}return c(document,"keydown",i),function(){o(),r(document,"keydown",i)}}function Ae(t,n){t[W].select(null),Yt(t,n)}const Re=(t,n,e,o,i)=>{t[Fe]={k:n,n:e,e:o,m:i},Ie(e)},Ue=(t,n)=>{const e=F(n.name);t[Fe]={f:n,n:e},Ie(e)},Pe=t=>{t[Fe]=void 0,Ie()},Oe=t=>t[Fe],Ie=t=>{document.title=t??"DGRM - Flowchart editor"},je=t=>t?.replace(/[/\\?%*:|"<>]/g,"-"),Fe=Symbol(0),Ne=async(t,n)=>{const e=`${Oe(t)?.n??"dgrm"}.png`;if(Ke())return void await Qn(n,e);const o={files:[new File([n],e,{type:n.type})]};navigator.canShare&&navigator.canShare(o)?await navigator.share(o):await Qn(n,e)},Ke=()=>screen.width>768;class Ye extends HTMLElement{constructor(t,n){super(),this.g=t,this.M=n}connectedCallback(){const t=this.attachShadow({mode:"closed"});t.innerHTML=`
${Wt}Create copy
Copy to clipboard
${Ke()?Qt+"Download":tn+"Send"} image
${Jt}Delete
`;const n=()=>st(this)[ht].del();function e(e,o){c(t.getElementById(e),"click",(t=>{n(),o()}))}e("clone",(()=>jt(this.g,this.M()))),e("copy",(async()=>await Ee(this.g,this.M()))),e("save",(()=>He(this.g,Y(Ct(this.M(),!1)),!0,(async t=>Ne(this.g,t))))),e("del",(()=>Yt(this.g,this.M())))}}customElements.define("ap-grp-settings",Ye);const Xe=Symbol("p"),_e=Symbol(0);const Ge=(t,n,e)=>{if(e[Xe])return null;const o=wt(e),i=mo(o);if(!i)return ao(n),null;const a=lo(n,o),s=po(n);if(s&&a)return go(i,e),null;if(e.shiftKey||e.ctrlKey){if(s){const t=ho(n);vo(t),eo(n,t)}return a?oo(n,i):eo(n,i),co(n,e),null}return a?lt(new Ye(t,(()=>so(n))),e.clientX-10,e.clientY-10):(ao(n),eo(n,i),co(n,e),null)},qe=(t,n,e,o)=>{if(e[Xe])return;if(t[_e]?.size)for(const n of t[_e])if(n(e,o))return;if(o===t.ownerSVGElement)return void t[W].moveCapture();const i=lo(n,o);if(!i&&!h(e))return void t[W].moveCapture();if(!i&&h(e))return ao(n),void wo(mo(o),e,o);const a=po(n);i&&a?wo(mo(o),e,o):i&&!a&&Je(t,n,e)},Je=(t,n,e)=>{const o=()=>We(n),i=t=>Qe(n,t),a=o();M(t.ownerSVGElement,e,(n=>i((e=>G(e,t[W].data.scale,n)))),(n=>{i((n=>nt(n,t[W].data.cell))),Rt(t,a,o())}))},We=t=>Ct([...t.shapesToMove,...yo(t.pathEndsToMove)],!1,!0),Qe=(t,n)=>{t.shapesToMove?.forEach((t=>{n(t[kt].data.position),t[kt].drawPosition()})),t.pathEndsToMove?.forEach((t=>{n(t.data.position),xo(t)[bt].drawPosition()}))},to=t=>{t[W].select(null),t[W].select(t.children)},no=()=>({shapes:new Set,paths:new Set,pathEnds:new Set,shapesToMove:new Set,pathEndsToMove:new Set}),eo=(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=xo(n.pathEnd);if(t.paths.has(e))return;t.pathEnds.add(n.pathEnd),i(e,$o(n.pathEnd))}},oo=(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=xo(n.pathEnd);if(t.paths.delete(e)){a(e,"highlight","highlight-s","highlight-e");const o=ko(n.pathEnd);t.pathEnds.add(o),i(e,$o(o))}else t.pathEnds.delete(n.pathEnd),a(e,$o(n.pathEnd))}},io=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(xo(t))))},ao=t=>{io(t);const n=ho(t);n&&vo(n),t.pathEnds.clear(),t.pathEndsToMove.clear(),t.paths.clear(),t.shapes.clear(),t.shapesToMove.clear()},so=t=>[...t.shapes,...t.paths,...yo(t.pathEnds)],co=(t,n)=>{ro(t);const e=uo(t);e&&(io(t),go(e,n))},ro=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)},lo=(t,n)=>[...t.shapes,...[...t.pathEnds].map((t=>t.el)),...t.paths].some((t=>t.contains(n))),uo=t=>fo(t)?ho(t):{},ho=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)},po=t=>!!ho(t),fo=t=>t.shapes.size||t.pathEnds.size||t.paths.size,mo=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=bo(t);return o?{pathEnd:o}:null},go=(t,n)=>{t.shape?t.shape[kt].click(n):t.path?t.path[bt].click(n):t.pathEnd&&xo(t.pathEnd)[bt].click(n,t.pathEnd.el)},vo=t=>{t.shape?t.shape[kt].unselect():t.path?t.path[bt].unselect():t.pathEnd&&xo(t.pathEnd)[bt].unselect()},wo=(t,n,e)=>{t.shape?t.shape[kt].moveCapture(n,e):t.path?t.path[bt].moveCapture(n,e):t.pathEnd&&xo(t.pathEnd)[bt].moveCapture(n,e)},yo=t=>[...t].map(xo),xo=t=>t.el.parentNode,bo=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},ko=t=>{const n=xo(t)[bt].data;return n.s===t?n.e:n.s},$o=t=>xo(t)[bt].data.s===t?"highlight-s":"highlight-e";function Mo(t){const n=(n,e)=>(function(t,n,e){const o=f("path");o.style.pointerEvents="none",i(o,"mrkp"),t.append(o);const a=[],c=n=>a.push(Q(t[W].data,n.clientX,n.clientY)),r=new Set,l=t=>{s(t,"mrkt")&&r.add(t)};l(e),c(n),M(o,n,(t=>{c(t),o.setAttribute("d",So(a)),l(document.elementFromPoint(t.clientX,t.clientY))}),(n=>{let e=Zo(y([o,...r]));o.remove(),e=Bo(Q(t[W].data,e.x,e.y));const i={id:j(),type:7,position:e,d:""},s=[{position:{x:0,y:0},d:So(a)}],c=[];r.forEach((t=>{const n=t.parentNode;c.push(n),s.push(n[kt].data)}));for(const t of s)Ho(i,t);c.length?Xt(t,c,Ht(i)):Nt(t,Ht(i),!0)}))}(t,n,e),!0);return((t,n)=>{t[_e]??=new Set,t[_e].add(n)})(t,n),()=>{((t,n)=>{t[_e]?.delete(n)})(t,n)}}const Co=t=>``;function Ho(t,n){const e=Lo(n.d),o=O(n.position);A(o,t.position,-1);let i=n.d;for(const t of e){const n=O(t);Bo(A(t,o)),i=zo(i,n,t)}t.d+=i}const Lo=t=>t.match(/M(.*?)l/g).map((t=>{const n=t.split(" ");return{x:Number.parseFloat(n[0].substring(1)),y:Number.parseFloat(n[1].substring(0,n[1].length-1))}})),Vo=t=>`M${t.x} ${t.y}l`,zo=(t,n,e)=>t.replaceAll(Vo(n),Vo(e));function So(t,n=5,e=2){const o=t.length,i=o-n-1==0;let a="M",s=[t[0].x,t[0].y];const[c,r]=s.map((t=>+t.toFixed(e)));let l=`${a}${c} ${r}`,d=c,u=r;const[h,p]=[t[1].x,t[1].y],[f,m]=[(c+h)/2,(r+p)/2],[g,v]=[f-d,m-u].map((t=>+t.toFixed(e)));l+=`l${g} ${v}`,d+=g,u+=v;for(let i=1;i1&&(a="t",s=[w,y]),l+=`${a}${s.map((t=>+t.toFixed(e))).join(" ")} `,d+=w,u+=y}return i||(s=[t[o-1].x-d,t[o-1].y-u],l+=`l${s.map((t=>+t.toFixed(e))).join(" ")}`),l}const Zo=t=>({x:Math.round(t.x+t.width/2),y:Math.round(t.y+t.height/2)}),Bo=t=>(t.x=Math.round(t.x),t.y=Math.round(t.y),t);const Eo=()=>{if(!document.cookie.includes("dgrma=1"))return null;const t=X(localStorage.getItem("dgrm"));return t&&new Date(t.e)>new Date?t:null},Do=()=>{const t=Eo()?.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},To=t=>document.getElementById("menu").bage(t),Ao=t=>{const n=Ro(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},Ro=t=>`hsl(${[...t].reduce(((t,n)=>n.charCodeAt(0)+((t<<5)-t)),0)%360}, 97%, 47%)`,Uo=(t,n,e,o)=>{const i=((t,n,e)=>({x:t.position.x+n*t.scale,y:t.position.y+e*t.scale}))(t[W].data,e,o);n.style.transform=`translate(${i.x}px, ${i.y}px)`},Po=(t,n,e)=>{const o=t=>e(Y(t)),i=c(t,"dgrmc",(t=>o([4,t.detail.shapesToCreateData]))),a=c(t,"dgrmu",(t=>o([4,t.detail.shapesToUpdData]))),s=c(t,"dgrmd",(t=>o([5,t.detail.delIds]))),l=c(t,"dgrmdc",(t=>{o([5,t.detail.delIds]),o([4,t.detail.shapesToCreateData])}));let d=!0;const u=c(t.ownerSVGElement,"pointermove",S((e=>{if(d){const i=Q(t[W].data,e.clientX,e.clientY);o([6,n,i.x,i.y])}}),60));return()=>{d=!1,r(t,"dgrmc",i),r(t,"dgrmu",a),r(t,"dgrmd",s),r(t,"dgrmdc",l),r(t.ownerSVGElement,"pointermove",u)}},Oo=(t,n)=>{Vt(t,n),z(t,new CustomEvent(jo,{detail:n,cancelable:!1}))},Io=(t,n)=>{rn(n),z(t,new CustomEvent(Fo,{detail:n,cancelable:!1}))},jo="dgrmcu",Fo="dgrmcd",No=async t=>{const n=Oe(t);n?.f?Ce(t,(async e=>{const o=await n.f.createWritable();await o.write(e),await o.close(),Jo(t)})):n?.k?n.e?Wo((async()=>await Xo(t,n.k))):alert('You do not have access to edit this diagram. Use "Save As" to make your own copy.'):Ko(t)};function Ko(t,e){const o=Eo(),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=()=>je(n(i,"nm").value);s("dvc",(n=>Yo(t,r()))),s("cl",(async n=>{Wo((async()=>{const n=r(),e=Oe(t);if(e?.k&&e.n===n&&e.m)return void await Xo(t,e.k);const o=function(){const t=new Date;return`${t.getUTCFullYear()}${(t.getUTCMonth()+1).toString().padStart(2,"0")}${j()}`}();Re(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 b(o.toString(),e)).text()}(o,n,Mt(t)),!0,!0),Jo(t)}))}))}const Yo=(t,n)=>Ce(t,(async e=>{const o=Qn(e,n??"dgrm.png");if(o){const n=await o;n&&(Ue(t,n),Jo(t))}else Jo(t)})),Xo=async(t,n)=>{let e;if(_o(t)){const o=[...t[qo].c,...t[qo].u],i=function(t,n){for(const e in t.s)n.has(e)&&Et(t.s[e]);return t}(Ct(o.length?[...document.querySelectorAll(o.map((t=>`[id="${t}"]`)).join(","))]:null,!1),t[qo].u);t[qo].d.forEach((t=>{i.s[t]=null})),e=await(async(t,n)=>(await b(`${mt}u/${t}`,n)).json())(n,i)}else e=(await gt(n)).d;Vt(t,e),Jo(t)},_o=t=>t[qo].c.size||t[qo].u.size||t[qo].d.size,Go=t=>{t[qo].c.clear(),t[qo].u.clear(),t[qo].d.clear()},qo=Symbol(0),Jo=t=>{Go(t),pt("Saved")},Wo=async t=>{try{it(!0),await t()}catch{oe()}finally{it(!1)}};function Qo(t){return!!ei(t)&&(t.c[t.i].u(),t.i-=1,!0)}function ti(t){const n=ni(t);return null!==n&&(t.i=n,t.c[n].r(),!0)}const ni=t=>{if(!t)return null;const n=t.i+1;return n<=t.c.length-1?n:null},ei=t=>t&&t.i>=0,oi=(t,n,e)=>li(t,(()=>{ci(t,At(n,e)),Vt(t,e),rn(n)}),(()=>{ci(t,Tt(n,e)),Vt(t,e)})),ii=(t,n,e)=>li(t,(()=>{ci(t,Tt(n,e)),Vt(t,e)}),(()=>{ci(t,At(n,e)),rn(n)})),ai=(t,n,e)=>li(t,(()=>{ci(t,Ut(e,n)),Vt(t,n)}),(()=>{ci(t,Ut(n,e)),Vt(t,e)})),si=(t,n,e,o,i)=>li(t,(()=>{ci(t,Tt(n,e)),Vt(t,e),ci(t,At(o,i)),Vt(t,i),rn(o)}),(()=>{ci(t,At(n,e)),rn(n),ci(t,Tt(o,i)),Vt(t,i)})),ci=(t,n)=>{n[ri]=!0,z(t,n)},ri=Symbol(0);function li(t,n,e){t[mi]??={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[mi],n,e),pi(t)}function di(t,n){n(t[mi])&&(t[W].select(null),pi(t))}const ui=t=>di(t,Qo),hi=t=>di(t,ti);const pi=t=>{return z(t,V(fi,(n=t[mi],{u:ei(n),r:null!==ni(n)})));var n},fi="dgrmh",mi=Symbol("h");function gi(t,n,e,o){const i=n=>t[W].select(Ft(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)re(n.img,(t=>i(Ht(t))));else if(n?.txt){const e=function(t,n){const e=f("text");En(e,null,n),e.style.visibility="hidden",t.append(e);const o=fe(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 vi 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,fi,(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=>ui(this.g))),c(e,"click",(t=>hi(this.g)))}}function wi(t,n){Lt(t,n),function(t){const n=t.getBoundingClientRect(),e=window.innerWidth/2,o=window.innerHeight/2;t[W].move(e-n.width/2-n.x,o-n.height/2-n.y,1);const i=t=>t>1?1:Math.max(t,.25),a=Math.min(i(window.innerWidth/n.width),i(window.innerHeight/n.height));a<1&&t[W].scale(a,{x:e,y:o})}(t)}customElements.define("ap-history",vi);const yi=-1!==window.navigator.userAgent.indexOf(" OPR"),xi=t=>yi&&0!==t.button,bi=(t,n,e,o,i)=>t.x<=o&&o<=t.x+n&&t.y<=i&&i<=t.y+e,ki={iceServers:[{urls:"stun:stun3.l.google.com:19302"}]},$i=`${ft}m/`;async function Mi(t,n,e,o){const i=Do();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(ki),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)=>b(`${$i}o`,{m:t,c:n,s:e}))(n,t,a.localDescription.sdp);const s=_(3e3,15e3,(async()=>{try{const e=await(async(t,n)=>(await x(`${$i}a?m=${t}&c=${n}`)).text())(n,t);e&&(s(),a.setRemoteDescription({sdp:e,type:"answer"}))}catch{c(),i()}}),i),c=()=>{s(),a.close(),it(!1)};return c}(a,n,((t,n)=>t.send(Y([0,a,i]))),((n,e)=>{const o=X(e.data);switch(o[0]){case 6:Uo(t,s.get(o[1]),o[2],o[3]);break;case 4:Oo(t,o[1]);break;case 5:Io(t,o[1]);break;case 0:s.set(o[1],Ao(o[2]));break;case 7:s.get(o[1]).remove(),s.delete(o[1]);break;case 2:wi(t,o[1]),l(n);break;case 3:t[W].move(o[1],o[2],1),l(n)}}),(()=>{d(),o()}));let r;const l=n=>{r=Po(t,a,(t=>n.send(t))),it(!1)},d=()=>{s.forEach((t=>t.remove())),r&&r(),c(),it(!1)};return d}async function Ci(t,n,e){const o=Do();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)=>b(`${$i}c`,{m:t,k:n}))(t,a);const s=new Set,c=_(5e3,72e5,(async()=>{try{const i=await(async(t,n)=>{const e=await(await x(`${$i}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(ki);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)=>b(`${$i}a`,{m:t,k:n,c:e,s:o}))(t,a,i[0],c.localDescription.sdp)}))}catch{r(),i()}})),r=()=>{c(),s?.forEach((t=>t.close()))};return r}(i,((n,e,i)=>{e.send(Y([0,a,o]));const c=Mt(t);e.send(Y(K(c.s)?[2,c]:[3,t[W].data.position.x,t[W].data.position.y])),s.forEach((t=>e.send(Y([0,t.i,t.n]))))}),((n,e)=>{const o=X(e.data);switch(o[0]){case 6:Uo(t,s.get(n).c,o[2],o[3]),c(e.data,n);break;case 4:Oo(t,o[1]),c(e.data,n);break;case 5:Io(t,o[1]),c(e.data,n);break;case 0:s.set(n,{i:o[1],n:o[2],c:Ao(o[2])}),c(e.data,n)}}),((t,n)=>{const e=s.get(t);c(Y([7,e.i]),t),e.c.remove(),s.delete(t)}),(()=>{d(),e()})),l=Po(t,a,c),d=()=>{l(),s.forEach((t=>t.c.remove())),r()};return{dispose:d,id:i}}function Hi(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(k("m",t)),pt("Link copied to clipboard")}))}const Li=t=>{t[zi]?.dispose(),t[zi]=void 0,To(!1)},Vi=t=>{Li(t),alert("Live collaboration disabled")},zi=Symbol(0);function Si(t){z(t,V("dgrmn",null,!0))&&(!function(t){for(t[W].select(null);t.firstChild;)(t.firstChild[kt]||t.firstChild[bt]).del();t[W].move(0,0,1)}(t),function(t){var n;(n=t[mi])&&(n.c=[],n.i=-1),pi(t)}(t),Pe(t))}async function Zi(t){Eo()?async function(t){let e;it(!0);try{e=await(async()=>{const t=await x(`${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 Bi(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(je(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===Oe(t)?.k&&((t,n)=>{t[Fe].n=n,Ie(n)})(t,e[i].n)}))}),(()=>{confirm("Delete?")&&s((async()=>{var n;await(n=i,b(`${mt}a/d/${n}`)),delete e[i],a(i).remove(),i===Oe(t)?.k&&Pe(t)}))}))})),it(!1)}(t):Bi(t)}async function Bi(t){try{const n=await te(ie);if(!n)return;if(!await Di(t,n.f))return void Ti();n.h?Ue(t,n.h):Pe(t)}catch(t){console.log(t),Ti()}}async function Ei(t,n,e){return!!await Di(t,n)||(ce(t,n,e.clientX,e.clientY),!1)}async function Di(t,n){if("image/png"!==n.type)return!1;const e=await Ze(n);return!!e&&(Si(t),wi(t,X(e)),!0)}const Ti=()=>alert("File cannot be read. Use the exact image file you got from the application.");async function Ai(t){let e=!0,o={v:!0};const i=Oe(t),a=i?.k;a&&(o={},Eo()&&(o=await(async t=>(await x(`${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},b(`${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(k("k",i.k)),pt("Link copied to clipboard"),r[ht].del()}))}class Ri extends HTMLElement{constructor(t){super(),this.g=t}connectedCallback(){const t=Eo(),e=t||ne,o=this.attachShadow({mode:"closed"});o.innerHTML=`
New
Open
${e?'
Save
':""}
Save${ee||e?" as":""}
${Ke()?Qt+"Download":tn+"Send"} image
Share link
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=L(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 No(this.g))),u("saveas",(()=>Ko(this.g))),u("open",(async()=>await Zi(this.g))),u("collab",(async t=>await async function(t){const n=()=>Li(t),e=()=>Vi(t);try{switch(t[zi]?.clientType){case 1:Hi(t[zi].id,n);break;case 2:Hi(t[zi].id,n,!0);break;default:{const o=await Ci(t,n,e);if(!o)return;c(t,"dgrmn",e,!0),t[zi]={clientType:1,id:o.id,dispose:()=>{r(t,"dgrmn",e),o.dispose()}},To(!0),Hi(t[zi].id,n);break}}}catch{e()}}(this.g))),u("login",(t=>function(){const t=Eo();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=>Ai(this.g))),u("down",(t=>(t=>Ce(t,(async n=>await Ne(t,n))))(this.g)))}bage(t){const n=t?i:a;n(this.C("menu"),"bage"),n(this.C("collab"),"bage")}}customElements.define("ap-menu",Ri);class Ui 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);const e=t.getElementById("m");let o;c(e,"click",(t=>{o?(o(),o=null,e.style.opacity="0.4"):(o=Mo(this.g),e.style.opacity="1")}))}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=Pi(this.g,this.S,!1,t.clientX,t.clientY)[0];h(t)||this.g[W].select([n]),n[kt]?n[kt]?.moveCapture(t,n,!0):n[bt]?.moveCapture(t,n,!0)}this.Z();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.Z();break;case"click":this.g.ownerSVGElement.focus(),this.g[W].select(Pi(this.g,parseInt(d(t,"data-cmd-arg")),!0))}}Z(){this.S=null,this.V=null,this.L=null}}customElements.define("ap-menu-shape",Ui);const Pi=(t,n,e,o,i)=>Ft(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 Oi=document.getElementById("canvas");var Ii,ji,Fi;Oi[W]={data:{position:{x:0,y:0},scale:1,cell:24},shapeMap:function(n){const e=(t,e)=>(t.setAttribute("bg",e.toString()),un(n,t),t);return{0:{create:t=>e(kn(n,t),2)},1:{create:i=>e(function(n,e){e.r??=48;let i,a=e.r;const s=Pn(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=R(e.position,-e.r),{x:e.r,y:e.r})),(r=>{let l=tt(n[W].data,r).x-i.x;l<48&&(l=48);const d=48*Math.round(l/48)/2;e.r!==d&&Nn(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=Nn(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,nn(s),Fn(s.el,"outer",e.r+24),Fn(s.el,"main",e.r),s.draw()}return 48!==e.r?c():s.draw(),s.el[kt].patch=t=>{const n=sn(s,t),o=null!=t.r&&e.r!==t.r;o&&(e.r=t.r),o?c():n&&s.draw()},s.el}(n,i),2)},2:{create:t=>e(Kn(n,t),2)},3:{create:t=>e(pe(n,t),2)},4:{create:a=>e(function(n,e){e.w??=96;let a,s=e.w;const c=Pn(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=R(e.position,-t),{x:t,y:t}}),(i=>{let l=tt(n[W].data,i).x-a.x;l<48&&(l=48);const d=48*Math.round(l/48);if(e.w!==d&&xe(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=xe(t,s);n!==e.w&&(e.w=n,r())}));function r(){const t=ye(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,nn(c);const n=ye(e.w,9);we(c.el,"main",n),we(c.el,"border",n),we(c.el,"outer",ye(e.w,-24)),c.draw()}return i(c.el,"shrhomb"),96!==e.w?r():c.draw(),c.el[kt].patch=t=>{const n=sn(c,t),o=null!=t.w&&e.w!==t.w;o&&(e.w=t.w),o?r():n&&c.draw()},c.el}(n,a),2)},5:{create:t=>e(qn(n,t),1)},6:{create:t=>e(le(n,t),0)},7:{create:t=>e(function(t,n){const e=On(t,n,Co(n),{},null,null,null,se,(()=>{Bo(n.position)}));return i(e.el,"shmrk"),e.draw(),e.el[kt].patch=t=>{cn(e,t)&&e.draw()},e.el}(n,t),3)}}}(Oi)},Oi.ownerSVGElement.addEventListener("contextmenu",(async t=>{u(t)||t.preventDefault()})),function(t){let n,e,o;function i(t){t.isPrimary&&t.isTrusted&&(n&&Math.abs(n.x-t.clientX)<3&&Math.abs(n.y-t.clientY)<3?t.stopImmediatePropagation():(n=null,void 0===t.movementX?(t[q]=e?t.clientX-e:0,t[J]=o?t.clientY-o:0,e=t.clientX,o=t.clientY):(t[q]=t.movementX,t[J]=t.movementY)))}t.addEventListener("pointerdown",(a=>{n={x:a.clientX,y:a.clientY},e=null,o=null,t.addEventListener("pointermove",i,{capture:!0,passive:!0}),t.addEventListener("pointerup",(n=>{r(t,"pointermove",i,!0)}),{capture:!0,once:!0,passive:!0})}),{capture:!0,passive:!0})}(document),function(t){c(document,"paste",(async n=>{u(n)||gi(t,(t=>{const n=t.getData("text/plain"),e=De(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=S((async()=>await No(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(),ui(t);break;case"KeyY":ct(),hi(t);break;case"KeyS":ct(),e.shiftKey?Ko(t):n();break;case"KeyA":ct(),to(t)}}))}(Oi),function(t){const n=t.ownerSVGElement;let e,a,s,l,d;const u=()=>{d?.remove(),d=null};let h=!1;function p(t){if(t[Xe]||!s)return void g();t[Xe]=!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[Xe]=!0;const e=n=>bi(Q(t[W].data,l.x,l.y),s.width.baseVal.value/t[W].data.scale,s.height.baseVal.value/t[W].data.scale,n.x,n.y);!function(t,n,e,o){const i=no(),a=t=>eo(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[W].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",p),r(n,"wheel",g),r(n,"pointercancel",g),r(n,"pointerup",m)}c(n,"pointerdown",(r=>{if(!r.isPrimary||wt(r)!==n||xi(r))return void g();c(n,"pointermove",p),c(n,"wheel",g,!0),c(n,"pointercancel",g,!0),c(n,"pointerup",m,!0);const u=()=>{t[W].selectedSet(null),h=!0,n.setPointerCapture(r.pointerId),a={x:r.clientX,y:r.clientY},l={x:r.clientX,y:r.clientY},s=f("rect"),s.style.cssText="rx:8px; fill: rgb(108 187 247 / 51%)",o(s,l),n.append(s)};2!==r.button?e=setTimeout((t=>{u(),d=f("circle"),i(d,"ative-elem"),d.style.cssText="r:10px; fill: rgb(108 187 247 / 51%)",o(d,{x:r.clientX,y:r.clientY}),n.append(d)}),500):u()}))}(Oi),function(t){const n=t.ownerSVGElement;let e,o=no();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[W].moveCapture();s=s=>{var c;r(n,"pointermove",a),c=Ge(t,o,s),i(),e=c},c(n,"pointerup",s,!0),a=e=>{r(n,"pointerup",s),qe(t,o,e,wt(l))},0===l.button&&c(n,"pointermove",a,!0)})),Te(t,(()=>so(o)),(()=>{e?i():fo(o)&&!ho(o)&&t[W].select(null)}));const l=t=>{ao(o),o=t??no(),co(o,null)};t[W].selectedSet=l,t[W].select=t=>{i();const n=no();if(t?.length)for(const e of t)eo(n,mo(e));l(n)}}(Oi),function(t){const n=t[W].data,e=function(t,n){let e;function o(n){e!==n&&(e=n,t.style.backgroundImage=`radial-gradient(rgb(73 80 87 / ${n}) 1px, transparent 0)`)}return o(.6),t.style.backgroundSize=`${n.cell}px ${n.cell}px`,function(){const e=n.cell*n.scale;n.scale<.5?o(0):n.scale<=.9?o(.2):o(.6),t.style.backgroundSize=`${e}px ${e}px`,t.style.backgroundPosition=`${n.position.x}px ${n.position.y}px`}}(t.ownerSVGElement,n);function o(){t.style.transform=`matrix(${n.scale}, 0, 0, ${n.scale}, ${n.position.x}, ${n.position.y})`,e()}function i(t,e){if(t<.25||t>4)return;const i=t/n.scale;n.scale=t,n.position.x=i*(n.position.x-e.x)+e.x,n.position.y=i*(n.position.y-e.y)+e.y,o()}!function(t,n,e,o){let i,a,s,l,d=!1;function u(t){s=null,l=null,i?.id===t.pointerId&&(i=null),a?.id===t.pointerId&&(a=null),i||a||(r(document,"pointermove",h),r(document,"pointercancel",u),r(document,"pointerup",u),d=!1)}function h(t){if(d){if(i&&!a||!i&&a)return n.position.x=t.clientX+(i||a).shift.x,n.position.y=t.clientY+(i||a).shift.y,void o();if(!a||!i||a?.id!==t.pointerId&&i?.id!==t.pointerId)return;const c=Math.hypot(i.pos.x-a.pos.x,i.pos.y-a.pos.y),r={x:(i.pos.x+a.pos.x)/2,y:(i.pos.y+a.pos.y)/2};s&&(n.position.x=n.position.x+r.x-l.x,n.position.y=n.position.y+r.y-l.y,e(n.scale/s*c,r)),s=c,l=r}i?.id===t.pointerId&&(i=et(t,n)),a?.id===t.pointerId&&(a=et(t,n))}c(t.ownerSVGElement,"pointerdown",(t=>{!i&&!t.isPrimary||i&&a||(i||(c(document,"pointermove",h),c(document,"pointercancel",u),c(document,"pointerup",u),i=et(t,n)),a||i?.id===t.pointerId||(a=et(t,n)))})),t[W].moveCapture=()=>{d=!0}}(t,n,i,o),t.ownerSVGElement.addEventListener("wheel",(t=>{t.preventDefault();const e=()=>{n.position.x-=t.deltaX,n.position.y-=t.deltaY,o()},a=e=>i(n.scale+e,p(t));Math.abs(t.deltaY)>50?t.ctrlKey?e():a(t.deltaY<0?.25:-.25):t.ctrlKey?a(-.01*t.deltaY):e()})),t[W].move=function(t,e,i){n.position.x=t,n.position.y=e,n.scale=i,o()},t[W].scale=i}(Oi),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(`${Be?'
Paste
':""}
Select all
Add image
`),s=(t,e)=>{c(n(a,t),"click",(async t=>{await e(),i()}))};Be&&s("p",(async()=>gi(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=De(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()=>to(o))),s("i",(async()=>async function(t,n,e){const o=await te(ae);o&&ce(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))}))}(Oi),(t=>{const n=(n,e)=>c(t,n,(t=>{t[ri]||e(t)}));n("dgrmc",(n=>oi(t,n.detail.newIds,n.detail.shapesToCreateData))),n("dgrmd",(n=>ii(t,n.detail.delIds,n.detail.shapesToDelData))),n("dgrmu",(n=>ai(t,n.detail.shapesBeforeUpdData,n.detail.shapesToUpdData))),n("dgrmdc",(n=>si(t,n.detail.delIds,n.detail.shapesToDelData,n.detail.newIds,n.detail.shapesToCreateData)))})(Oi),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 Ei(t,await e.getFile(),n)&&Ue(t,e)}else{const e=n.dataTransfer.items[0].getAsFile();await Ei(t,e,n)&&Ie(F(e.name))}else Ti()}))}(Oi),(t=>{t[qo]={c:new Set,u:new Set,d:new Set};const n=n=>{t[qo].c.add(n),t[qo].u.delete(n),t[qo].d.delete(n)},e=n=>{t[qo].c.has(n)||t[qo].u.add(n),t[qo].d.delete(n)},o=n=>n?.forEach((n=>{t[qo].d.add(n),t[qo].c.delete(n),t[qo].u.delete(n)})),i=t=>Object.keys(t.s).forEach(e);c(t,"dgrmc",(t=>{t.detail.newIds?.forEach(n)})),c(t,"dgrmu",(t=>i(t.detail.shapesToUpdData))),c(t,"dgrmd",(t=>o(t.detail.delIds))),c(t,"dgrmdc",(t=>{o(t.detail.delIds),t.detail.newIds?.forEach(n)})),c(t,jo,(t=>i(t.detail))),c(t,Fo,(t=>o(t.detail))),t.addEventListener("dgrmn",(n=>{!_o(t)||window.confirm("Changes you made may not be saved. Continue?")?Go(t):n.preventDefault()})),window.addEventListener("beforeunload",(n=>{_o(t)&&n.preventDefault()}))})(Oi);{const t=new URL(window.location.href),n=n=>t.searchParams.get(n);n("usr")&&(Ii=n("usr"),ji=n("usrn"),Fi=parseInt(n("expr")),localStorage.setItem("dgrm",Y({l:Ii,n:ji,e:Fi})),$("usr","usrn","expr"))}(t=>{const n=new Ri(t);n.id="menu",g(n)})(Oi),g(new vi(Oi)),g(new Ui(Oi));{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);wi(Oi,n.d),n.p&&Re(Oi,t,n.n,n.e,n.m)}catch{alert("The link is broken or you do not have permission. Try to relogin.")}$("k"),it(!1)}else if(e("m")){const t=e("m");$("m"),await async function(t,n){const e=()=>Vi(t);try{const o=await Mi(t,n,(()=>Li(t)),e);if(!o)return;c(t,"dgrmn",e,!0),t[zi]={clientType:2,id:n,dispose:()=>{r(t,"dgrmn",e),o()}},To(!0)}catch{e()}}(Oi,t)}else Eo()||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)}()}