UNPKG

coherent-gameface-interaction-manager

Version:
7 lines (6 loc) 42.9 kB
(function(g,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(g=typeof globalThis<"u"?globalThis:g||self,p(g.interactionManager={}))})(this,(function(g){"use strict";class p{constructor(){this.actions=[],this.keyboardFunctions=[],this.gamepadFunctions=[]}init(){window._IM||(window._IM=new p)}getKeys(e){return _IM.keyboardFunctions.filter(t=>t.keys.every(s=>e.includes(s)))}getKeysIndex(e){return _IM.keyboardFunctions.findIndex(t=>t.keys.every(s=>e.includes(s)))}getGamepadAction({actions:e,type:t}){return _IM.gamepadFunctions.find(s=>s.actions.every(i=>e.includes(i))&&s.type===t&&s.actions.length===e.length)}getGamepadActions(e,t=!0){return _IM.gamepadFunctions.filter(s=>s.actions.every(i=>e.includes(i))&&(t?s.actions.length===e.length:!0))}getGamepadActionIndex(e){return _IM.gamepadFunctions.findIndex(t=>t.actions.every(s=>e.includes(s)))}getAction(e){return _IM.actions.find(t=>t.name===e)}getActionIndex(e){return _IM.actions.findIndex(t=>t.name===e)}hasDuplicateCallback(e,t){return e.callbacks.some(s=>s===t)}addCallbackToEntry(e,t,s){if(this.hasDuplicateCallback(e,t)){const i=typeof t=="string"?"action":"function",n=typeof t=="string"?t:"(anonymous function)";return console.error(`Duplicate callback detected! ${s.identifier} Type: '${s.type}' Callback: ${n} This ${i} is already registered for this combination and type. To update it, first remove with the .off() method.`)}return e.callbacks.push(t)}removeKeyboardFunction(e){const t=_IM.keyboardFunctions.indexOf(e);t!==-1&&_IM.keyboardFunctions.splice(t,1)}removeGamepadFunction(e){const t=_IM.gamepadFunctions.indexOf(e);t!==-1&&_IM.gamepadFunctions.splice(t,1)}}const m=new p,M={ALT:18,ARROW_DOWN:40,ARROW_LEFT:37,ARROW_RIGHT:39,ARROW_UP:38,BACKSPACE:8,CAPS_LOCK:20,CTRL:17,DELETE:46,END:35,ENTER:13,ESC:27,F1:112,F2:113,F3:114,F4:115,F5:116,F6:117,F7:118,F8:119,F9:120,F10:121,F11:122,F12:123,HOME:36,INSERT:45,NUM_LOCK:144,NUMPAD_ENTER:13,NUMPAD_DASH:109,NUMPAD_STAR:106,NUMPAD_DOT:110,NUMPAD_FORWARD_SLASH:111,NUMPAD_PLUS:107,NUMPAD_0:96,NUMPAD_1:97,NUMPAD_2:98,NUMPAD_3:99,NUMPAD_4:100,NUMPAD_5:101,NUMPAD_6:102,NUMPAD_7:103,NUMPAD_8:104,NUMPAD_9:105,PAGE_DOWN:34,PAGE_UP:33,PAUSE:19,PRINT_SCRN:44,SCROLL_LOCK:145,SHIFT:16,SPACEBAR:32,TAB:9,A:65,B:66,C:67,D:68,E:69,F:70,G:71,H:72,I:73,J:74,K:75,L:76,M:77,N:78,O:79,P:80,Q:81,R:82,S:83,T:84,U:85,V:86,W:87,X:88,Y:89,Z:90,1:49,2:50,3:51,4:52,5:53,6:54,7:55,8:56,9:57,0:48,QUOTE:222,DASH:189,COMMA:188,DOT:190,FORWARD_SLASH:191,SEMI_COLON:186,SQUARE_BRACKET_LEFT:219,SQUARE_BRACKET_RIGHT:221,BACKWARD_SLASH:220,BACKTICK:192,EQUAL:187,SYSTEM:91},R=Object.keys(M);class P{register(e,t){if(m.getAction(e))return console.error(`The following action "${e}" is already registered!`);_IM.actions.push({name:e,callback:t})}remove(e){const t=m.getActionIndex(e);if(t===-1)return console.error(`${e} is not a registered action!`);_IM.actions.splice(t,1)}execute(e,t){const s=m.getAction(e);if(!s)return console.error(`${e} is not a registered action!`);s.callback(t)}}const l=new P;class U{constructor(){this.eventListenerAttached=!1,this.keysPressed=new Set,this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),window.KEYS||(window.KEYS=M)}on(e){const t=this.normalizeKeys(e.keys),s=t.filter(n=>!M[n]);if(s.length>0)return console.error(`The following keys [${s.join(", ")}] you have entered are incorrect! `);this.eventListenerAttached||(document.addEventListener("keydown",this.onKeyDown),document.addEventListener("keyup",this.onKeyUp),this.eventListenerAttached=!0),(e.type?Array.isArray(e.type)?e.type:[e.type]:["press"]).forEach(n=>{const r=m.getKeys(t).find(h=>h.type===n);if(r)return m.addCallbackToEntry(r,e.callback,{identifier:`Keys: [${t.join(", ")}]`,type:n});if(n==="lift"&&t.length>1)return console.error("You can only have a single key trigger an action on lift");_IM.keyboardFunctions.push({keys:t,type:n,callbacks:[e.callback]})})}off(e,t){e=this.normalizeKeys(e);const s=m.getKeys(e);if(s.length===0)return console.error("You are trying to remove a non-existent key combination!");if(t){const i=s.filter(n=>n.callbacks.includes(t));if(i.length===0)return console.error("You are trying to remove a non-existent callback from this key combination!");i.forEach(n=>{const o=n.callbacks.indexOf(t);n.callbacks.splice(o,1),n.callbacks.length===0&&m.removeKeyboardFunction(n)})}else s.forEach(i=>{m.removeKeyboardFunction(i)});_IM.keyboardFunctions.length===0&&(document.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("keyup",this.onKeyUp),this.eventListenerAttached=!1)}onKeyDown(e){const t=this.keyCodeToString(e.keyCode);if(!t)return;this.keysPressed.add(t);const s=m.getKeys([...this.keysPressed]);s.length!==0&&s.forEach(i=>{i.type==="press"&&e.repeat||i.type!=="press"&&i.type!=="hold"||i.type==="hold"&&!e.repeat||this.executeCallbacks(e,i)})}onKeyUp(e){const t=this.keyCodeToString(e.keyCode);if(!t)return;this.keysPressed.delete(t);const s=m.getKeys([t]);s.length!==0&&s.forEach(i=>{i.type==="lift"&&i.keys.indexOf(t)!==-1&&this.executeCallbacks(e,i)})}keyCodeToString(e){return R.find(t=>M[t]===e)}normalizeKeys(e){const t=e.map(s=>{if(typeof s=="number"){const i=this.keyCodeToString(s);if(!i)throw new Error(`Invalid KeyCode: ${s}`);return i}return s.toUpperCase()});return[...new Set(t)]}executeCallbacks(e,t){t.callbacks.forEach(s=>{if(typeof s=="string")return l.execute(s,e);s(e)})}}const D=new U,v={FACE_BUTTON_DOWN:0,FACE_BUTTON_RIGHT:1,FACE_BUTTON_LEFT:2,FACE_BUTTON_TOP:3,LEFT_SHOULDER:4,RIGHT_SHOULDER:5,LEFT_SHOULDER_BOTTOM:6,RIGHT_SHOULDER_BOTTOM:7,SELECT:8,START:9,LEFT_ANALOGUE_STICK:10,RIGHT_ANALOGUE_STICK:11,PAD_UP:12,PAD_DOWN:13,PAD_LEFT:14,PAD_RIGHT:15,CENTER_BUTTON:16,aliases:{"face-button-down":"FACE_BUTTON_DOWN","face-button-left":"FACE_BUTTON_LEFT","face-button-right":"FACE_BUTTON_RIGHT","face-button-top":"FACE_BUTTON_TOP","left-shoulder":"LEFT_SHOULDER","right-shoulder":"RIGHT_SHOULDER","left-shoulder-bottom":"LEFT_SHOULDER_BOTTOM","right-shoulder-bottom":"RIGHT_SHOULDER_BOTTOM",select:"SELECT",start:"START","left-analogue-stick":"LEFT_ANALOGUE_STICK","right-analogue-stick":"RIGHT_ANALOGUE_STICK","pad-up":"PAD_UP","pad-down":"PAD_DOWN","pad-left":"PAD_LEFT","pad-right":"PAD_RIGHT","center-button":"CENTER_BUTTON","playstation.x":"FACE_BUTTON_DOWN","playstation.square":"FACE_BUTTON_LEFT","playstation.circle":"FACE_BUTTON_RIGHT","playstation.triangle":"FACE_BUTTON_TOP","playstation.l1":"LEFT_SHOULDER","playstation.r1":"RIGHT_SHOULDER","playstation.l2":"LEFT_SHOULDER_BOTTOM","playstation.r2":"RIGHT_SHOULDER_BOTTOM","playstation.share":"SELECT","playstation.options":"START","playstation.l3":"LEFT_ANALOGUE_STICK","playstation.r3":"RIGHT_ANALOGUE_STICK","playstation.d-pad-up":"PAD_UP","playstation.d-pad-down":"PAD_DOWN","playstation.d-pad-left":"PAD_LEFT","playstation.d-pad-right":"PAD_RIGHT","playstation.center":"CENTER_BUTTON","xbox.a":"FACE_BUTTON_DOWN","xbox.x":"FACE_BUTTON_LEFT","xbox.b":"FACE_BUTTON_RIGHT","xbox.y":"FACE_BUTTON_TOP","xbox.lb":"LEFT_SHOULDER","xbox.rb":"RIGHT_SHOULDER","xbox.lt":"LEFT_SHOULDER_BOTTOM","xbox.rt":"RIGHT_SHOULDER_BOTTOM","xbox.view":"SELECT","xbox.menu":"START","xbox.left-thumbstick":"LEFT_ANALOGUE_STICK","xbox.right-thumbstick":"RIGHT_ANALOGUE_STICK","xbox.d-pad-up":"PAD_UP","xbox.d-pad-down":"PAD_DOWN","xbox.d-pad-left":"PAD_LEFT","xbox.d-pad-right":"PAD_RIGHT","xbox.center":"CENTER_BUTTON"},axisAliases:["right.joystick","left.joystick","left.joystick.down","left.joystick.up","left.joystick.left","left.joystick.right","right.joystick.down","right.joystick.up","right.joystick.left","right.joystick.right"]},E=.9,K=["press","hold"];class z{constructor(){this._gamepadEnabled=!1,this._pollingInterval=200,this._pressedAction=null,this._pressedButtons=[],this.sanitizeAction=this.sanitizeAction.bind(this)}set enabled(e){this._gamepadEnabled=e,this._gamepadEnabled?this.startPolling():this.stopPolling()}set pollingInterval(e){this._pollingInterval=e,this._gamepadEnabled&&(this.pollingIntervalRef&&this.stopPolling(),this.startPolling())}on(e){const t=e.actions.map(this.sanitizeAction),s=v.axisAliases.some(o=>t.includes(o)),i=e.type&&K.includes(e.type)?e.type:"hold";if(i==="press"&&s)return console.error("You can't use an axis action with a 'press' type!");if(t.length>1&&s)return console.error("You can't use an axis action in a combination with a button action");const n=m.getGamepadAction({actions:t,type:i});if(n)return m.addCallbackToEntry(n,e.callback,{identifier:`Actions: [${t.join(", ")}]`,type:i});_IM.gamepadFunctions.push({actions:t,type:i,callbacks:[e.callback]})}off(e,t){const s=m.getGamepadActions(e.map(this.sanitizeAction));if(s.length===0)return console.error("You are trying to remove a non-existent action!");if(t){const i=s.filter(n=>n.callbacks.includes(t));if(i.length===0)return console.error("You are trying to remove a non-existent callback from this action!");i.forEach(n=>{const o=n.callbacks.indexOf(t);n.callbacks.splice(o,1),n.callbacks.length===0&&m.removeGamepadFunction(n)})}else s.forEach(i=>m.removeGamepadFunction(i))}startPolling(){this.pollingIntervalRef=setInterval(()=>{const e=navigator.getGamepads();e.length!==0&&e.forEach(t=>{t&&(this.handleButtons(t.buttons),this.handleJoysticks(t.axes))})},this._pollingInterval)}stopPolling(){this.pollingIntervalRef&&clearInterval(this.pollingIntervalRef)}handleButtons(e){const t=e.reduce((i,n,o)=>(n.pressed&&(i.buttonIndexes.push(o),i.buttons.push(n)),i),{buttonIndexes:[],buttons:[]}),s=m.getGamepadActions(t.buttonIndexes,!1);this._pressedAction&&(s.includes(this._pressedAction)||this.executeCallbacks(this._pressedAction,this._pressedButtons),this._pressedAction=null,this._pressedButtons=[]),s.forEach(i=>{if(i.type==="press"){this._pressedAction=i,this._pressedButtons=t.buttons;return}this.executeCallbacks(i,t.buttons)})}handleJoysticks(e){this.getJoystickActions().forEach(s=>{switch(s.actions[0]){case"left.joystick":return this.executeCallbacks(s,[e[0],e[1]]);case"right.joystick":return this.executeCallbacks(s,[e[2],e[3]]);case"left.joystick.down":if(e[1]>E)return this.executeCallbacks(s,[e[0],e[1]]);break;case"left.joystick.up":if(e[1]<-E)return this.executeCallbacks(s,[e[0],e[1]]);break;case"left.joystick.left":if(e[0]<-E)return this.executeCallbacks(s,[e[0],e[1]]);break;case"left.joystick.right":if(e[0]>E)return this.executeCallbacks(s,[e[0],e[1]]);break;case"right.joystick.down":if(e[3]>E)return this.executeCallbacks(s,[e[2],e[3]]);break;case"right.joystick.up":if(e[3]<-E)return this.executeCallbacks(s,[e[2],e[3]]);break;case"right.joystick.left":if(e[2]<-E)return this.executeCallbacks(s,[e[2],e[3]]);break;case"right.joystick.right":if(e[2]>E)return this.executeCallbacks(s,[e[2],e[3]]);break}})}sanitizeAction(e){if(typeof e=="number")return e;const t=e.toLowerCase();if(v.axisAliases.includes(t))return t;const s=v.aliases[t];if(s)return v[s];throw new Error(`You have entered a non-supported button alias: ${e}`)}getJoystickActions(){return _IM.gamepadFunctions.filter(e=>v.axisAliases.includes(e.actions[0]))}executeCallbacks(e,t){e.callbacks.forEach(s=>{if(typeof s=="string")return l.execute(s,t);s(t)})}}const x=new z;function L(c){return c*180/Math.PI}function y(c,e,t){return Math.min(Math.max(c,e),t)}function _(){return(Math.random()+1).toString(36).substring(7)}function S(c,e,t,s){const i=c-t,n=e-s;return Math.hypot(i,n)}function $(c,e,t,s){return{x:(c+t)/2,y:(e+s)/2}}const F=["down","up","left","right"],k={up:["arrow_up"],down:["arrow_down"],right:["arrow_right"],left:["arrow_left"]};class B{constructor(){this.enabled=!1,this.areas={default:this.createDefaultAreaState()},this.registeredKeys=new Set,this.clearCurrentActiveKeys=!1,this.overlapPercentage=.5,this.lastFocusedElement=null,this.paused=!1,this.syncLastFocused=e=>{const t=e.target;if(!(!this.enabled||!t))for(const s in this.areas){const i=this.areas[s];if(i.elements.includes(t)){i.lastFocusedElement=t,this.lastFocusedElement=t;break}}}}init(e=[],t){this.enabled||(this.enabled=!0,this.add(e),this.activeKeys=JSON.parse(JSON.stringify(k)),this.registerKeyActions(),window.addEventListener("focusin",this.syncLastFocused),t&&0<=t&&t<=1&&(this.overlapPercentage=t))}deinit(){this.enabled&&(this.enabled=!1,this.areas={default:this.createDefaultAreaState()},this.removeKeyActions(),this.overlapPercentage=.5,this.lastFocusedElement=null,window.removeEventListener("focusin",this.syncLastFocused))}add(e){if(this.enabled){if(e.every(t=>t instanceof HTMLElement)){e.forEach(t=>this.makeFocusable(t)),this.setNavigationAreaProperties("default",e);return}e.forEach(t=>{typeof t=="string"?this.handleString(t):this.handleObject(t)})}}remove(e="default"){if(this.enabled){if(!this.areas[e])return console.error(`The area '${e}' you are trying to remove doesn't exist`);this.areas[e].elements.forEach(t=>t.removeAttribute("tabindex")),delete this.areas[e]}}getLastFocused(e="default"){const t=this.areas[e]?.lastFocusedElement;if(t&&!document.contains(t)){this.areas[e].lastFocusedElement=void 0;return}return t}createDefaultAreaState(){return{elements:[],lastFocusedElement:void 0}}handleString(e){const t=[...document.querySelectorAll(e)];if(t.length===0)return console.error(`${e} is either not a correct selector or the element is not present in the DOM.`);t.forEach(this.makeFocusable),this.setNavigationAreaProperties("default",t)}handleObject(e){const t=e.elements.reduce((s,i)=>{if(i instanceof HTMLElement)return s.push(i),this.makeFocusable(i),s;const n=[...document.querySelectorAll(i)];return n.forEach(this.makeFocusable),s.push(...n),s},[]);if(t.length===0)return console.error(`${e.elements.join(", ")} are either not a correct selectors or the elements are not present in the DOM.`);this.areas[e.area]||(this.areas[e.area]=this.createDefaultAreaState()),this.setNavigationAreaProperties(e.area,t)}setNavigationAreaProperties(e,t){this.areas[e].elements.push(...t)}makeFocusable(e){e.setAttribute("tabindex","1")}getFocusableGroup(e,t){return t.reduce((s,i)=>{if(i!==e&&!i.hasAttribute("disabled")){const{x:n,y:o,height:r,width:h}=i.getBoundingClientRect();s.push({element:i,x:n,y:o,height:r,width:h})}return s},[])}getCurrentArea(e){return Object.values(this.areas).find(t=>{if(t.elements.includes(e))return!0})}getClosestToEdge(e,t){return t.reduce((s,i)=>{switch(e){case"down":return i.y<s.y?i:s;case"up":return i.y>s.y?i:s;case"right":return i.x<s.x?i:s;case"left":return i.x>s.x?i:s;default:return s}})}moveFocus(e){if(!this.enabled)return;const t=this.isActiveElementInGroup()&&document.activeElement||this.lastFocusedElement,s=this.getCurrentArea(t);if(!s)return console.error("The active element is not in a focusable area!");const{elements:i}=s,n=this.getFocusableGroup(t,i),{x:o,y:r,width:h,height:a}=t.getBoundingClientRect(),d={x:o,y:r,width:h,height:a};if(n.length===0)return;const f=this.filterGroupByCurrentAxis(e,n,d);if(!f.length)return;let u=this.findNextElement(e,f,d.x,d.y);u||(u=this.getClosestToEdge(e,f)),u&&(u.element.focus(),this.lastFocusedElement=u.element,s.lastFocusedElement=this.lastFocusedElement)}filterGroupByCurrentAxis(e,t,s){return t.filter(i=>e==="left"||e==="right"?this.isOverlappingX(s,i):this.isOverlappingY(s,i))}isOverlappingX(e,t){const s=Math.min(e.y+e.height,t.y+t.height),i=Math.max(e.y,t.y),n=Math.max(0,s-i),o=Math.min(e.height,t.height);return n/o>=this.overlapPercentage}isOverlappingY(e,t){const s=Math.min(e.x+e.width,t.x+t.width),i=Math.max(e.x,t.x),n=Math.max(0,s-i),o=Math.min(e.width,t.width);return n/o>=this.overlapPercentage}findNextElement(e,t,s,i){return t.reduce((n,o)=>{const r=o.x-s,h=o.y-i,a=L(Math.atan2(h,r));if(this.getDirectionAngle(e,a)){n||(n=o);const d=Math.hypot(r,h),f=Math.hypot(n.x-s,n.y-i);n=d<f?o:n}return n},null)}getDirectionAngle(e,t){switch(e){case"down":return t>0&&t<180;case"up":return t>-180&&t<0;case"left":return t<-90||t>90;case"right":return t>-90&&t<90}}registerKeyActions(){F.forEach(e=>{const t=()=>{this.paused||this.moveFocus(e)};l.register(`move-focus-${e}`,t);const s=this.activeKeys[e];for(const i of s)D.on({keys:[i],callback:`move-focus-${e}`,type:["press","hold"]}),this.registeredKeys.add(i);x.on({actions:[`playstation.d-pad-${e}`],callback:`move-focus-${e}`})})}resetKeys(){this.enabled&&(this.removeKeyActions(),this.activeKeys=JSON.parse(JSON.stringify(k)),this.registerKeyActions())}changeKeys(e,t={clearCurrentActiveKeys:!1}){if(!this.enabled)return;const s=Object.keys(e);if(s.length===0)return;const i=s.filter(n=>!F.includes(n));if(i.length>0)return console.error(`The following directions: [${i.join(", ")}] you have entered are incorrect! `);this.clearCurrentActiveKeys=t.clearCurrentActiveKeys,this.removeKeyActions();for(const n in this.activeKeys){const o=e[n];typeof o=="string"&&!this.activeKeys[n].includes(o)&&this.activeKeys[n].push(o.toUpperCase())}this.registerKeyActions()}removeKeyActions(){this.registeredKeys.size!==0&&(F.forEach(e=>{const t=`move-focus-${e}`;for(const s of this.activeKeys[e])D.off([s],t);l.remove(t),x.off([`playstation.d-pad-${e}`],t),this.clearCurrentActiveKeys&&(this.activeKeys[e]=[])}),this.registeredKeys.clear(),this.clearCurrentActiveKeys=!1)}focusFirst(e="default"){if(!this.enabled)return;const t=this.areas[e].elements;if(!t||t.length===0)return console.error(`The area '${e}' you are trying to focus doesn't exist or the spatial navigation hasn't been initialized`);if(this.lastFocusedElement=t.find(s=>!s.hasAttribute("disabled")),!this.lastFocusedElement)return console.error(`The area '${e}' you are trying to focus doesn't have any focusable elements`);this.lastFocusedElement.focus(),this.areas[e].lastFocusedElement=this.lastFocusedElement}focusLast(e="default"){if(!this.enabled)return;const t=this.areas[e].elements;if(!t||t.length===0)return console.error(`The area '${e}' you are trying to focus doesn't exist or the spatial navigation hasn't been initialized`);let s;for(let i=t.length-1;i>=0;i--)if(!t[i].hasAttribute("disabled")){s=t[i];break}if(this.lastFocusedElement=s,!this.lastFocusedElement)return console.error(`The area '${e}' you are trying to focus doesn't have any focusable elements`);this.lastFocusedElement.focus(),this.areas[e].lastFocusedElement=this.lastFocusedElement}switchArea(e){if(!this.enabled)return;const t=this.getLastFocused(e);if(!t||t.hasAttribute("disabled"))return this.focusFirst(e);t.focus(),this.lastFocusedElement=t}isElementInGroup(e){return Object.values(this.areas).some(t=>t.elements.includes(e))}isActiveElementInGroup(){return document.activeElement?this.isElementInGroup(document.activeElement):!1}clearFocus(){this.enabled&&this.isActiveElementInGroup()&&document.activeElement.blur()}pause(){this.enabled&&(this.paused=!0)}resume(){this.enabled&&(this.paused=!1)}}const W=new B;class C{constructor(e){this.options=e,this.draggableElements=[],this.draggedElement=null,this.enabled=!1,this.offset={x:0,y:0}}get draggedItemIndex(){return[...this.draggableElements].indexOf(this.draggedElement)}get bodyScrollOffset(){return{x:document.body.scrollLeft,y:document.body.scrollTop}}setPointerOffset(e,t,s){const{x:i,y:n}=s.getBoundingClientRect();this.offset={x:e-i,y:t-n}}}function H(c,e){if(e<0&&c>-200&&c<200)return"top";if(e>0&&c>-200&&c<200)return"bottom";if(c<0&&e>-200&&e<200)return"left";if(c>0&&e>-200&&e<200)return"right";if(c<=-200&&e<=-200)return"top-left";if(c>=200&&e<=-200)return"top-right";if(c<=-200&&e>=200)return"bottom-left";if(c>=200&&e>=200)return"bottom-right"}function b(c){return c instanceof HTMLElement?c:document.querySelector(c)}const A=2;class G{constructor(){this.activeTouches=new Map}hold(e){if(!e)return console.error("Options not provided for hold!");let t;const s=b(e.element);if(!s)return console.error("Element not found!");const i=({touches:o})=>{this.activeTouches.set(o[0].identifier,o[0]),t=setTimeout(()=>{e.callback&&e.callback()},e.time||1e3)},n=({touches:o})=>{this.activeTouches.delete(o[0].identifier),clearTimeout(t)};return s.addEventListener("touchstart",i),s.addEventListener("touchend",n),{remove(){s.removeEventListener("touchstart",i),s.removeEventListener("touchend",n)}}}tap(e){if(!e)return console.error("Options not provided for tap!");let t,s,i=!0,n=e.tapsNumber||1;const o=b(e.element);if(!o)return console.error("Element not found!");const r=({touches:a})=>{this.activeTouches.set(a[0].identifier,a[0]),clearTimeout(s),i=!0,t=setTimeout(()=>{i=!1},e.tapTime||200)},h=({touches:a})=>{this.activeTouches.delete(a[0].identifier),clearTimeout(t),i&&(n--,s=setTimeout(()=>{n=e.tapsNumber||1,clearTimeout(s)},e.betweenTapsTime||500),!(n!==0||!e.callback)&&(e.callback(),i=!0,clearTimeout(t),n=e.tapsNumber||1))};return o.addEventListener("touchstart",r),o.addEventListener("touchend",h),{remove(){o.removeEventListener("touchstart",r),o.removeEventListener("touchend",h)}}}drag(e){if(!e)return console.error("Options not provided for drag!");const t=b(e.element);if(!t)return console.error("Element not found!");const s=({touches:o,target:r,currentTarget:h})=>{this.activeTouches.set(o[0].identifier,o[0]),document.addEventListener("touchmove",i),document.addEventListener("touchend",n),e.onDragStart&&e.onDragStart({x:o[0].clientX,y:o[0].clientY,target:r,currentTarget:h})},i=({touches:o})=>{this.activeTouches.has(o[0].identifier)&&e.onDrag&&e.onDrag({x:o[0].clientX,y:o[0].clientY})},n=({touches:o})=>{this.activeTouches.delete(o[0].identifier),document.removeEventListener("touchmove",i),document.removeEventListener("touchend",n),e.onDragEnd&&e.onDragEnd({x:o[0].clientX,y:o[0].clientY})};return t.addEventListener("touchstart",s),{remove(){t.removeEventListener("touchstart",s)}}}swipe(e){if(!e)return console.error("Options not provided for swipe!");let t,s,i,n=!0;const o=100;e.touchNumber||=1;const r=b(e.element);if(!r)return console.error("Element not found!");const h=({touches:u})=>{this.activeTouches.set(u[0].identifier,u[0]),this.activeTouches.size>e.touchNumber&&(document.removeEventListener("touchmove",a),document.removeEventListener("touchend",d)),this.activeTouches.size===e.touchNumber&&(t=setTimeout(()=>{n=!1,clearTimeout(t),t=null},1e3),document.addEventListener("touchmove",a),document.addEventListener("touchend",d))},a=({touches:u})=>{if(!this.activeTouches.has(u[0].identifier))return;const O=this.activeTouches.get(u[0].identifier);if(!O)return;const{clientX:w,clientY:N}=O,V=u[0].clientX-w,ee=u[0].clientY-N;s=H(V,ee),i=S(w,N,u[0].clientX,u[0].clientY)},d=({touches:u})=>{this.activeTouches.delete(u[0].identifier),this.activeTouches.size===0&&(document.removeEventListener("touchmove",a),document.removeEventListener("touchend",d),f()&&e.callback(s),clearTimeout(t),n=!0,t=null)},f=()=>n&&s&&i&&i>o;return r.addEventListener("touchstart",h),{remove(){r.removeEventListener("touchstart",h)}}}pinch(e){if(!e)return console.error("Options not provided for pinch!");let t;const s=40,i=b(e.element);if(!i)return console.error("Element not found!");const n=({touches:h})=>{if(this.activeTouches.set(h[0].identifier,h[0]),this.activeTouches.size<A)return;const a=this.activeTouches.get(0),d=this.activeTouches.get(1);!a||!d||(document.addEventListener("touchmove",o),document.addEventListener("touchend",r),t=S(a.clientX,a.clientY,d.clientX,d.clientY))},o=({touches:h})=>{if(this.activeTouches.size!==A)return;this.activeTouches.set(h[0].identifier,h[0]);const a=this.activeTouches.get(0),d=this.activeTouches.get(1);if(!a||!d)return;const f=S(a.clientX,a.clientY,d.clientX,d.clientY),u=Math.sign(f-t)*s;t=f;const O=$(a.clientX,a.clientY,d.clientX,d.clientY);e.callback&&e.callback({pinchDelta:u,midpoint:O})},r=({touches:h})=>{this.activeTouches.delete(h[0].identifier),this.activeTouches.size===0&&(document.removeEventListener("touchmove",o),document.removeEventListener("touchend",r))};return i.addEventListener("touchstart",n),{remove(){i.removeEventListener("touchstart",n)}}}rotate(e){if(!e)return console.error("Options not provided for rotate!");let t=0,s=0;const i=b(e.element);if(!i)return console.error("Element not found!");const n=({touches:a})=>{this.activeTouches.size<A||(this.activeTouches.set(a[0].identifier,a[0]),t=h()-s,e.callback&&e.callback(t))},o=({touches:a})=>{this.activeTouches.set(a[0].identifier,a[0]),this.activeTouches.size===A&&(s=h()-t,document.addEventListener("touchmove",n),document.addEventListener("touchend",r))},r=({touches:a})=>{this.activeTouches.delete(a[0].identifier),document.removeEventListener("touchmove",n),document.removeEventListener("touchend",r)};i.addEventListener("touchstart",o);const h=()=>{const f=this.activeTouches.get(0).clientY-this.activeTouches.get(1).clientY,u=this.activeTouches.get(0).clientX-this.activeTouches.get(1).clientX;return(L(Math.atan2(f,u))+360+90)%360};return{remove(){i.removeEventListener("touchstart",o)}}}}const T=new G,Y=["x","y"];class j extends C{constructor(e){super(e),this.actionName=`drag-around-${_()}`,this.restrict={top:0,left:0,right:1/0,bottom:1/0},this.touchEvents=[],this._touchEnabled=!1,this.onMouseDown=this.onMouseDown.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.init()}set touchEnabled(e){this._touchEnabled!==e&&(this._touchEnabled=e,this._touchEnabled?this.addTouchEvents():this.removeTouchEvents())}init(){if(!this.enabled){if(this.draggableElements=document.querySelectorAll(this.options.element),this.draggableElements.length===0)return console.error(`${this.options.element} is not a valid element selector.`);this.draggableElements.forEach(e=>e.addEventListener("mousedown",this.onMouseDown)),this.registerDragActions(),this.enabled=!0}}deinit(){this.enabled&&(this.enabled=!1,this.draggableElements.forEach(e=>e.removeEventListener("mousedown",this.onMouseDown)),this.removeDragActions())}onMouseDown(e){this.draggedElement=e.currentTarget,this.draggedElement.style.position="absolute",this.elementRect=this.draggedElement.getBoundingClientRect(),this.setRestriction(),this.setPointerOffset(e.clientX,e.clientY,this.draggedElement),l.execute(this.actionName,{x:e.clientX+this.bodyScrollOffset.x-this.offset.x,y:e.clientY+this.bodyScrollOffset.y-this.offset.y,index:this.draggedItemIndex}),this.options.dragClass&&this.draggedElement.classList.add(this.options.dragClass),this.options.onDragStart&&this.options.onDragStart(this.draggedElement),document.addEventListener("mousemove",this.onMouseMove),document.addEventListener("mouseup",this.onMouseUp)}onMouseMove(e){l.execute(this.actionName,{x:e.clientX+this.bodyScrollOffset.x-this.offset.x,y:e.clientY+this.bodyScrollOffset.y-this.offset.y,index:this.draggedItemIndex})}onMouseUp(){document.removeEventListener("mousemove",this.onMouseMove),document.removeEventListener("mouseup",this.onMouseUp),this.options.onDragEnd&&this.options.onDragEnd(this.draggedElement),this.options.dragClass&&this.draggedElement.classList.remove(this.options.dragClass),this.draggedElement=null}registerDragActions(){l.register(this.actionName,({x:e,y:t,index:s})=>{if(!this.draggableElements[s])return console.error(`There is no draggable element at index ${s}`);this.options.lockAxis&&Y.includes(this.options.lockAxis)&&(e=this.options.lockAxis==="y"?this.elementRect.x:e,t=this.options.lockAxis==="x"?this.elementRect.y:t),this.draggableElements[s].style.left=`${y(e,this.restrict.left,this.restrict.right)}px`,this.draggableElements[s].style.top=`${y(t,this.restrict.top,this.restrict.bottom)}px`,this.options.onDragMove&&this.options.onDragMove({x:e,y:t})})}removeDragActions(){l.remove(this.actionName)}addTouchEvents(){this.draggableElements.forEach(e=>{this.touchEvents.push(T.drag({element:e,onDragStart:t=>{this.onMouseDown({currentTarget:t.currentTarget,clientX:t.x,clientY:t.y})},onDrag:({x:t,y:s})=>{this.onMouseMove({clientX:t,clientY:s})},onDragEnd:()=>{this.onMouseUp()}}))})}removeTouchEvents(){this.touchEvents.forEach(e=>e?.remove()),this.touchEvents=[]}setRestriction(){if(!this.options.restrictTo)return;const e=document.querySelector(this.options.restrictTo);if(!e)return console.error(`The element ${this.options.restrictTo} you trying to restrict dragging to is not a valid element`);const{x:t,y:s,height:i,width:n}=e.getBoundingClientRect();this.restrict={top:s,left:t,right:n+t-this.elementRect.width,bottom:i+s-this.elementRect.height}}}class X extends C{constructor(e){super(e),this.dropzones=[],this.draggedOver=null,this.touchEvents=[],this._touchEnabled=!1,this.options=e;const t=_();this.actionName=`drag-and-drop-${t}`,this.automaticAction=`move-to-${t}`,this.onMouseDown=this.onMouseDown.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.onMouseEnter=this.onMouseEnter.bind(this),this.onMouseLeave=this.onMouseLeave.bind(this),this.init()}set touchEnabled(e){this._touchEnabled!==e&&(this._touchEnabled=e,this._touchEnabled?this.addTouchEvents():this.removeTouchEvents())}init(){if(!this.enabled){if(this.draggableElements=document.querySelectorAll(this.options.element),this.draggableElements.length===0)return console.error(`${this.options.element} is not a valid element selector.`);this.draggableElements.forEach(e=>e.addEventListener("mousedown",this.onMouseDown)),this.createDropzones(),this.dropzones.length!==0&&(this.dropzones.forEach(e=>{e.addEventListener("mouseenter",this.onMouseEnter),e.addEventListener("mouseleave",this.onMouseLeave)}),this.registerDragActions(),this.enabled=!0)}}deinit(){this.enabled&&(this.draggableElements.forEach(e=>e.removeEventListener("mousedown",this.onMouseDown)),this.dropzones.forEach(e=>{e.removeEventListener("mouseenter",this.onMouseEnter),e.removeEventListener("mouseleave",this.onMouseLeave)}),this.removeActions(),this.enabled=!1)}onMouseDown(e){this.draggedElement=e.currentTarget,this.draggedElement.style.position="absolute",this.draggedElement.style.pointerEvents="none",this.setPointerOffset(e.clientX,e.clientY,this.draggedElement),l.execute(this.actionName,{x:e.clientX+this.bodyScrollOffset.x-this.offset.x,y:e.clientY+this.bodyScrollOffset.y-this.offset.y,index:this.draggedItemIndex}),this.options.dragStyle&&this.draggedElement.classList.add(this.options.dragStyle),this.options.onDragStart&&this.options.onDragStart(this.draggedElement),document.addEventListener("mousemove",this.onMouseMove),document.addEventListener("mouseup",this.onMouseUp)}onMouseMove(e){l.execute(this.actionName,{x:e.clientX+this.bodyScrollOffset.x-this.offset.x,y:e.clientY+this.bodyScrollOffset.y-this.offset.y,index:this.draggedItemIndex})}onMouseUp(){document.removeEventListener("mousemove",this.onMouseMove),document.removeEventListener("mouseup",this.onMouseUp),this.draggedElement&&(this.draggedElement.style.position="",this.draggedElement.style.pointerEvents="",this.draggedElement.style.left="",this.draggedElement.style.top="",this.options.onDragEnd&&this.options.onDragEnd(this.draggedElement),this.options.dragStyle&&this.draggedElement.classList.remove(this.options.dragStyle)),this.draggedOver&&this.handleDrop(),this.draggedElement=null}onMouseEnter(e){this.draggedElement&&(this.draggedOver=e.currentTarget,this.options.dropzoneActiveClass&&this.draggedOver.classList.add(this.options.dropzoneActiveClass))}onMouseLeave(){this.draggedElement&&(this.options.dropzoneActiveClass&&this.draggedOver&&this.draggedOver.classList.remove(this.options.dropzoneActiveClass),this.draggedOver=null)}registerDragActions(){l.register(this.actionName,({x:e,y:t,index:s})=>{if(!this.draggableElements[s])return console.error(`There is no draggable element at index ${s}`);this.draggableElements[s].style.left=`${e}px`,this.draggableElements[s].style.top=`${t}px`,this.options.onDragMove&&this.options.onDragMove({x:e,y:t})}),l.register(this.automaticAction,this.automaticMove.bind(this))}removeActions(){l.remove(this.actionName),l.remove(this.automaticAction)}addTouchEvents(){this.draggableElements.forEach(e=>{this.touchEvents.push(T.drag({element:e,onDragStart:t=>{this.onMouseDown({currentTarget:t.currentTarget,clientX:t.x,clientY:t.y})},onDrag:({x:t,y:s})=>{this.onMouseMove({clientX:t,clientY:s});const i=document.elementFromPoint(t,s);let n=this.options.dropzones.reduce((o,r)=>o||!i?o:o=i.closest(r),null);if(n||(n=this.dropzones.includes(i)?i:null),n){this.onMouseEnter({currentTarget:n});return}this.onMouseLeave()},onDragEnd:()=>{this.onMouseUp()}}))})}removeTouchEvents(){this.touchEvents.forEach(e=>e?.remove()),this.touchEvents=[]}automaticMove({elementIndex:e,dropzoneIndex:t}){const{x:s,y:i}=this.dropzones[t].getBoundingClientRect(),{x:n,y:o}=this.draggableElements[e].getBoundingClientRect(),r={x:Math.sign(s-n),y:Math.sign(i-o)};this.draggableElements[e].style.position="absolute";const h=(a,d)=>{if(a===s&&d===i){this.dropzones[t].appendChild(this.draggableElements[e]),this.draggableElements[e].style.position="";return}a=a!==s?r.x+a:a,d=d!==i?r.y+d:d,this.draggableElements[e].style.left=`${a}px`,this.draggableElements[e].style.top=`${d}px`,(a!==s||d!==i)&&requestAnimationFrame(()=>h(a,d))};h(n,o)}createDropzones(){this.dropzones=this.options.dropzones.reduce((e,t)=>{const s=[...document.querySelectorAll(t)];return s.length===0&&console.error(`${t} is not a valid html element to be used as a dropzone`),e.concat(s)},[])}handleDrop(){let e=this.options.dropType;const t={preventDefault:()=>{e="ignore"},target:this.draggedElement,dropzone:this.draggedOver};if(this.options.onDrop&&this.options.onDrop(t),!(e==="ignore"||!this.draggedOver)){switch(this.draggedOver.children.length===0&&(e="add"),e){case"add":this.draggedOver.appendChild(this.draggedElement);break;case"shift":this.shiftElements();break;case"switch":this.draggedElement?.parentNode.appendChild(this.draggedOver.children[0]),this.draggedOver.appendChild(this.draggedElement);break}this.onMouseLeave()}}shiftElements(){const e=this.dropzones.findIndex(o=>o===this.draggedOver),t=this.dropzones.map(o=>o.children[0]===this.draggedElement?[]:[...o.children]),s=t.reduce((o,r,h)=>(r.length===0&&(o=Math.abs(e-h)<Math.abs(e-o)?h:o),o),1e5),i=Math.sign(e-s)>0?0:1,n=Math.sign(e-s)<0?0:1;t.splice(e+n,0,[this.draggedElement]),t.splice(s+i,1),t.forEach((o,r)=>{o.forEach(h=>this.dropzones[r].appendChild(h))})}}const I=360,q=90;class Z{constructor(e){this.rotatingElement=null,this.elementPosition={x:0,y:0},this.angle=0,this.enabled=!1,this.initalAngle=0,this.actionName=`rotate-${_()}`,this.touchEvents=null,this._touchEnabled=!1,this.options=e,this.onMouseDown=this.onMouseDown.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.init()}set touchEnabled(e){this._touchEnabled!==e&&(this._touchEnabled=e,this._touchEnabled?this.addTouchEvents():this.removeTouchEvents())}init(){if(!this.enabled){if(this.rotatingElement=this.rotatingElement||document.querySelector(this.options.element),!this.rotatingElement)return console.error(`${this.options.element} is not a valid element selector`);this.rotatingElement.addEventListener("mousedown",this.onMouseDown),this.registerAction(),this.enabled=!0}}deinit(){this.enabled&&(this.rotatingElement.removeEventListener("mousedown",this.onMouseDown),this.removeActions(),this.enabled=!1)}onMouseDown(e){document.addEventListener("mousemove",this.onMouseMove),document.addEventListener("mouseup",this.onMouseUp);const{x:t,y:s,height:i,width:n}=this.rotatingElement.getBoundingClientRect();this.elementPosition={x:t+n/2,y:s+i/2};const o=this.getAngle(e.clientX,e.clientY);this.initalAngle=o-this.angle}onMouseMove(e){this.angle=this.getAngle(e.clientX,e.clientY)-this.initalAngle,this.options.snapAngle&&(this.angle=Math.floor(this.angle/this.options.snapAngle)*this.options.snapAngle),l.execute(this.actionName,this.angle)}onMouseUp(){document.removeEventListener("mousemove",this.onMouseMove),document.removeEventListener("mouseup",this.onMouseUp)}registerAction(){l.register(this.actionName,e=>{this.rotatingElement.style.transform=`rotate(${e}deg)`,this.options.onRotation&&this.options.onRotation(e<0?I+e:e)})}removeActions(){l.remove(this.actionName)}addTouchEvents(){this.touchEvents=T.drag({element:this.rotatingElement,onDragStart:({x:e,y:t})=>{this.onMouseDown({clientX:e,clientY:t})},onDrag:({x:e,y:t})=>{this.onMouseMove({clientX:e,clientY:t})},onDragEnd:()=>{this.onMouseUp()}})}removeTouchEvents(){this.touchEvents?.remove(),this.touchEvents=null}getAngle(e,t){const s=e-this.elementPosition.x,i=t-this.elementPosition.y;return(L(Math.atan2(i,s))+I+q)%I}}class J{constructor(e){this.enabled=!1,this.activeEdge=null,this.edges={bottom:null,right:null,bottomRight:null},this.elementRect={x:0,y:0},this.touchEvents=null,this._touchEnabled=!1;const t=_();this.heightAction=`resize-height-${t}`,this.widthAction=`resize-width-${t}`,this.options=e,this.options.edgeWidth=e.edgeWidth??5,this.options.heightMin=this.options.heightMin??50,this.options.heightMax=this.options.heightMax??window.innerHeight,this.options.widthMin=this.options.widthMin??50,this.options.widthMax=this.options.widthMax??window.innerWidth,this.onMouseDown=this.onMouseDown.bind(this),this.onMouseMove=this.onMouseMove.bind(this),this.onMouseUp=this.onMouseUp.bind(this),this.init()}set touchEnabled(e){this._touchEnabled!==e&&(this._touchEnabled=e,this._touchEnabled?this.addTouchEvents():this.removeTouchEvents())}init(){if(!this.enabled){if(this.resizableElement=document.querySelector(this.options.element),!this.resizableElement)return console.error(`${this.options.element} is not a correct selector`);this.resizableElement.addEventListener("mousedown",this.onMouseDown),this.addEdges(),this.registerActions(),this.enabled=!0}}deinit(){this.enabled&&(this.resizableElement.removeEventListener("mousedown",this.onMouseDown),this.removeEdges(),this.removeActions(),this.enabled=!1)}onMouseDown(e){if(this.activeEdge=this.setEdge(e.target),!this.activeEdge)return;const{x:t,y:s}=this.resizableElement.getBoundingClientRect();this.elementRect={x:t,y:s},document.addEventListener("mousemove",this.onMouseMove),document.addEventListener("mouseup",this.onMouseUp)}onMouseMove(e){const t=e.clientX-this.elementRect.x,s=e.clientY-this.elementRect.y;switch(this.activeEdge){case"bottom":l.execute(this.heightAction,s),this.options.onHeightChange&&this.options.onHeightChange(s);break;case"right":l.execute(this.widthAction,t),this.options.onWidthChange&&this.options.onWidthChange(t);break;case"bottomRight":l.execute(this.heightAction,s),l.execute(this.widthAction,t),this.options.onWidthChange&&this.options.onWidthChange(t),this.options.onHeightChange&&this.options.onHeightChange(s);break}}onMouseUp(){this.activeEdge=null,document.removeEventListener("mousemove",this.onMouseMove),document.removeEventListener("mouseup",this.onMouseUp)}addTouchEvents(){this.touchEvents=T.drag({element:this.resizableElement,onDragStart:e=>{this.onMouseDown(e)},onDrag:({x:e,y:t})=>{this.onMouseMove({clientX:e,clientY:t})},onDragEnd:()=>{this.onMouseUp()}})}removeTouchEvents(){this.touchEvents?.remove(),this.touchEvents=null}setEdge(e){return e.dataset.edge?e.dataset.edge:null}checkPosition(){const{position:e}=getComputedStyle(this.resizableElement);e==="absolute"||e==="relative"||(this.resizableElement.style.position="relative")}addEdges(){this.checkPosition();const{width:e,height:t}=this.resizableElement.getBoundingClientRect();Object.entries(this.edges).forEach(([s,i])=>{switch(i=document.createElement("DIV"),i.dataset.edge=s,i.style.position="absolute",s){case"bottom":i.style.width=`${e-this.options.edgeWidth}px`,i.style.height=`${this.options.edgeWidth}px`,i.style.bottom=`-${this.options.edgeWidth}px`;break;case"bottomRight":i.style.width=`${this.options.edgeWidth}px`,i.style.height=`${this.options.edgeWidth}px`,i.style.bottom=`-${this.options.edgeWidth}px`,i.style.right=`-${this.options.edgeWidth}px`;break;case"right":i.style.width=`${this.options.edgeWidth}px`,i.style.height=`${t-this.options.edgeWidth}px`,i.style.right=`-${this.options.edgeWidth}px`;break}this.edges[s]=i,this.resizableElement.appendChild(i)})}removeEdges(){Object.values(this.edges).forEach(e=>{this.resizableElement.removeChild(e)})}registerActions(){l.register(this.heightAction,e=>{this.resizableElement.style.height=`${y(e,this.options.heightMin,this.options.heightMax)}px`,this.edges.right.style.height=`${y(e,this.options.heightMin,this.options.heightMax)-this.options.edgeWidth}px`}),l.register(this.widthAction,e=>{this.resizableElement.style.width=`${y(e,this.options.widthMin,this.options.widthMax)}px`,this.edges.bottom.style.width=`${y(e,this.options.widthMin,this.options.widthMax)-this.options.edgeWidth}px`})}removeActions(){l.remove(this.heightAction),l.remove(this.widthAction)}}class Q{constructor(e){this.enabled=!1,this.actionName=`pan-and-zoom-${_()}`,this.offset={x:0,y:0},this.transform={x:0,y:0,scale:1},this.touchEvents=null,this._touchEnabled=!1,this.options=e,this.options.maxZoom=this.options.maxZoom||1/0,this.options.minZoom=this.options.minZoom||.1,this.options.zoomFactor=this.options.zoomFactor||.1,this.onWheel=this.onWheel.bind(this),this.init()}set touchEnabled(e){this._touchEnabled!==e&&(this._touchEnabled=e,this._touchEnabled?this.addTouchEvents():this.removeTouchEvents())}init(){if(!this.enabled){if(this.zoomableElement=document.querySelector(this.options.element),!this.zoomableElement)return console.error(`${this.options.element} is not a correct element selector`);this.zoomableElement.addEventListener("wheel",this.onWheel),this.registerActions(),this.zoomableElement.style.transformOrigin="top left",this.enabled=!0}}deinit(){this.enabled&&(this.zoomableElement.removeEventListener("wheel",this.onWheel),this.removeActions(),this.enabled=!1)}onWheel(e){e.preventDefault(),l.execute(this.actionName,{x:e.clientX,y:e.clientY,zoomDirection:Math.sign(e.deltaY)})}registerActions(){l.register(this.actionName,({x:e,y:t,zoomDirection:s})=>{const i=this.calculateOffsets(e,t);this.options.onZoom&&this.options.onZoom();const n=Number((this.transform.scale-s*this.options.zoomFactor).toFixed(5));if(n<this.options.minZoom||n>this.options.maxZoom)return;const o={x:i.x/this.transform.scale,y:i.y/this.transform.scale},r=this.transform;r.x+=o.x*(this.transform.scale-n),r.y+=o.y*(this.transform.scale-n),r.scale=n,this.zoomableElement.style.transform=`matrix(${r.scale}, 0, 0, ${r.scale}, ${r.x}, ${r.y})`,this.transform=r})}removeActions(){l.remove(this.actionName)}addTouchEvents(){this.touchEvents=T.pinch({element:this.zoomableElement,callback:({pinchDelta:e,midpoint:t})=>{l.execute(this.actionName,{x:t.x,y:t.y,zoomDirection:Math.sign(e)*-1})}})}removeTouchEvents(){this.touchEvents?.remove(),this.touchEvents=null}calculateOffsets(e,t){const s=this.zoomableElement.getBoundingClientRect();return{x:e-s.x,y:t-s.y}}}m.init(),g.GamepadMappings=v,g.KeyboardMappings=M,g.actions=l,g.draggable=j,g.dropzone=X,g.gamepad=x,g.keyboard=D,g.resize=J,g.rotate=Z,g.spatialNavigation=W,g.touchGestures=T,g.zoom=Q,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})})); //# sourceMappingURL=interaction-manager.min.js.map