@antv/g-plugin-gesture
Version:
A G plugin for Gesture implemented with PointerEvents
11 lines (10 loc) • 9.9 kB
JavaScript
/*!
* @antv/g-plugin-gesture
* @description A G plugin for Gesture implemented with PointerEvents
* @version 2.0.31
* @date 1/17/2025, 2:07:02 PM
* @author AntVis
* @docs https://g.antv.antgroup.com/
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@antv/g-lite")):"function"==typeof define&&define.amd?define(["exports","@antv/g-lite"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).G=t.G||{},t.G.Gesture={}),t.window.G)}(this,(function(t,e){"use strict";function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function r(t){var e=function(t,e){if("object"!=n(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var i=r.call(t,e||"default");if("object"!=n(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==n(e)?e:e+""}function i(t,e,n){return(e=r(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function o(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function s(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t,e){for(var n=0;e.length>n;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,r(i.key),i)}}function u(t,e,n){return e&&a(t.prototype,e),n&&a(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function c(t){return c=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},c(t)}function l(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(l=function(){return!!t})()}function v(t,e){if(e&&("object"==n(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function f(t,e){return f=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},f(t,e)}function p(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=Array(e);e>n;n++)r[n]=t[n];return r}function h(t){return function(t){if(Array.isArray(t))return p(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return p(t,e);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?p(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var y="object"==typeof performance&&performance.now?performance:Date,d=function(t,e){var n=e.x-t.x,r=e.y-t.y;return Math.abs(n)>Math.abs(r)?n>0?"right":"left":r>0?"down":"up"},m=function(t,e){var n=Math.abs(e.x-t.x),r=Math.abs(e.y-t.y);return Math.sqrt(n*n+r*r)},g=function(){function t(e){var n=this;s(this,t),this.evCache=[],this.startPoints=[],this.processEvent={},this.throttleTimer=0,this.emitThrottles=[],this._start=function(t,e){n.reset(),n.startTime=y.now();var r=n.evCache,i=n.startPoints;if(t){var o,s=t.pointerId,a=t.x,u=t.y,c=r.findIndex((function(t){return s===t.pointerId}));-1!==c&&r.splice(c,1),r.push({pointerId:s,x:a,y:u,ev:t});for(var l=h((null===(o=t.nativeEvent)||void 0===o?void 0:o.touches)||[]),v=function(t){if(l.find((function(e){return r[t].pointerId===e.identifier})))return 1;r.splice(t,1)},f=r.length-1;f>-1;f--)v(f)}i.length=r.length;for(var p=0;r.length>p;p++){var d=r[p];i[p]={x:d.x,y:d.y}}if(1!==i.length){var g,b;n.startDistance=m(i[0],i[1]),n.center={x:(g=i[0]).x+((b=i[1]).x-g.x)/2,y:g.y+(b.y-g.y)/2}}else{var T=r[0].ev;n.pressTimeout=setTimeout((function(){var t="press",r="none";T.direction=r,T.deltaX=0,T.deltaY=0,T.points=i,n.emitStart(t,T,e),T.type=t,e.dispatchEvent(T),n.eventType=t,n.direction=r,n.movingTarget=e}),250)}},this._move=function(t,e){n.clearPressTimeout();var r=n.startPoints,i=n.evCache;if(r.length){for(var o=t.x,s=t.y,a=t.pointerId,u=0,c=i.length;c>u;u++)if(a===i[u].pointerId){i[u]={pointerId:a,x:o,y:s,ev:t};break}var l={x:o,y:s},v=i.map((function(t){return{x:t.x,y:t.y}})),f=y.now();if(n.prevMoveTime=n.lastMoveTime,n.prevMovePoint=n.lastMovePoint,n.lastMoveTime=f,n.lastMovePoint=l,1===r.length){var p=r[0],h=o-p.x,g=s-p.y,b=n.direction||d(p,l);n.direction=b;var T=n.getEventType(l,e,t);return t.direction=b,t.deltaX=h,t.deltaY=g,t.points=v,n.emitStart(T,t,e),t.type=T,void n.refreshAndGetTarget(e).dispatchEvent(t)}var P=n.startDistance,E=m(v[0],v[1]);t.zoom=E/P,t.center=n.center,t.points=v,n.emitStart("pinch",t,e),n._throttleEmit("pinch",t,e)}},this._end=function(t,e){var r=n.evCache,i=n.startPoints,o=r.map((function(t){return{x:t.x,y:t.y}}));if(t.points=o,n.emitEnd(t,n.refreshAndGetTarget(e)),1===r.length){var s=y.now(),a=n.lastMoveTime;if(100>s-a){var u=a-(n.prevMoveTime||n.startTime);if(u>0){var c=n.prevMovePoint||i[0],l=n.lastMovePoint||i[0],v=m(c,l)/u;v>.3&&(t.velocity=v,t.direction=d(c,l),t.type="swipe",e.dispatchEvent(t))}}}for(var f=0,p=r.length;p>f;f++)if(r[f].pointerId===t.pointerId){r.splice(f,1),i.splice(f,1);break}n.reset(),r.length>0&&n._start(void 0,e)},this._cancel=function(t,e){var r=n.evCache.map((function(t){return{x:t.x,y:t.y}}));t.points=r,n.emitEnd(t,n.refreshAndGetTarget(e)),n.evCache=[],n.reset()},this.options=e}return u(t,[{key:"apply",value:function(e){var n=this,r=e.renderingService,i=e.renderingContext.root.ownerDocument,o=i.defaultView;this.canvas=o;var s=function(t){return t===i&&n.options.isDocumentGestureEnabled?i:t},a=function(t){var e=s(t.target);e&&n._move(t,e)},u=function(t){var e=s(t.target);e&&n._start(t,e)},c=function(t){var e=s(t.target);e&&n._end(t,e)},l=function(t){var e=s(t.target);e&&n._cancel(t,e)},v=function(t){var e=s(t.target);e&&n._end(t,e)};r.hooks.init.tap(t.tag,(function(){o.addEventListener("pointermove",a),o.addEventListener("pointerdown",u),o.addEventListener("pointerup",c),o.addEventListener("pointercancel",l),o.addEventListener("pointerupoutside",v)})),r.hooks.destroy.tap(t.tag,(function(){o.removeEventListener("pointermove",a),o.removeEventListener("pointerdown",u),o.removeEventListener("pointerup",c),o.removeEventListener("pointercancel",l),o.removeEventListener("pointerupoutside",v)}))}},{key:"getEventType",value:function(t,e,n){var r,i=this.eventType,o=this.startTime,s=this.startPoints;if(i)return i;(this.movingTarget=e,this.isPanListenerInPath=n.path.some((function(t){var e;return!(null===(e=t.emitter)||void 0===e||null===(e=e.eventNames())||void 0===e||!e.includes("pan"))})),this.isPanListenerInPath)?r=y.now()-o>250&&10>m(s[0],t)?"press":"pan":r="press";return this.eventType=r,r}},{key:"enable",value:function(t){this.processEvent[t]=!0}},{key:"isProcess",value:function(t){return this.processEvent[t]}},{key:"emitStart",value:function(t,e,n){this.isProcess(t)||(this.enable(t),e.type="".concat(t,"start"),n.dispatchEvent(e))}},{key:"_throttleEmit",value:function(t,e,n){var r=this;this.pushEvent(t,e);var i=this.emitThrottles,o=this.processEvent;this.throttleTimer||(this.throttleTimer=this.canvas.requestAnimationFrame((function(){for(var t=0,e=i.length;e>t;t++){var s=i[t],a=s.type,u=s.ev;o[a]&&(u.type=a,n.dispatchEvent(u))}r.throttleTimer=0,r.emitThrottles.length=0})))}},{key:"emitEnd",value:function(t,e){var n=this.processEvent;Object.keys(n).forEach((function(r){t.type="".concat(r,"end"),e.dispatchEvent(t),delete n[r]}))}},{key:"pushEvent",value:function(t,e){for(var n=this.emitThrottles,r={type:t,ev:e},i=0,o=n.length;o>i;i++)if(n[i].type===t)return void n.splice(i,1,r);n.push(r)}},{key:"clearPressTimeout",value:function(){this.pressTimeout&&(clearTimeout(this.pressTimeout),this.pressTimeout=null)}},{key:"refreshAndGetTarget",value:function(t){return this.movingTarget?(this.movingTarget&&!this.movingTarget.isConnected&&(this.movingTarget=t),this.movingTarget):t}},{key:"reset",value:function(){this.clearPressTimeout(),this.startTime=0,this.startDistance=0,this.direction=null,this.eventType=null,this.prevMoveTime=0,this.prevMovePoint=null,this.lastMoveTime=0,this.lastMovePoint=null,this.movingTarget=null,this.isPanListenerInPath=null}}])}();g.tag="Gesture";var b=function(t){function e(){var t,n,r,i,o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return s(this,e),n=this,r=c(r=e),(t=v(n,l()?Reflect.construct(r,i||[],c(n).constructor):r.apply(n,i))).name="gesture",t.options=o,t}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&f(t,e)}(e,t),u(e,[{key:"init",value:function(){this.addRenderingPlugin(new g(function(t){for(var e=1;arguments.length>e;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?o(Object(n),!0).forEach((function(e){i(t,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}({isDocumentGestureEnabled:!1},this.options)))}},{key:"destroy",value:function(){this.removeAllRenderingPlugins()}}])}(e.AbstractRendererPlugin);t.Plugin=b}));
//# sourceMappingURL=index.umd.min.js.map