Bilibili-Evolved/min/touch-move.min.js
2019-10-11 14:35:56 +08:00

1 line
1.4 KiB
JavaScript

(()=>{return(e,t)=>{const n=(e,t,n)=>{const c=Math.pow(Math.abs(e.x-t.x),2);const s=Math.pow(Math.abs(e.y-t.y),2);return c+s>=n*n};const c=(e,t)=>{const n={screenX:t.screenX,screenY:t.screenY,clientX:t.clientX,clientY:t.clientY};return new MouseEvent(e,{...n,bubbles:true,cancelable:true,view:unsafeWindow,detail:1})};const s=[];const o={passive:false,capture:true};const r=(e,t=20)=>{if(s.some(t=>t.element===e)){return}let r;let u;let a;const l=e=>{if(e.touches.length<1){return}const t=e.touches[0];r={x:t.clientX,y:t.clientY};e.target.dispatchEvent(c("mousedown",t))};e.addEventListener("touchstart",l,o);const i=e=>{if(e.touches.length!==1){return}const s=e.touches[0];const o={x:s.clientX,y:s.clientY};u=s;if(n(r,o,t)){e.target.dispatchEvent(c("mousemove",s));a=true;if(e.cancelable){e.preventDefault()}}else{a=false}};e.addEventListener("touchmove",i,o);const h=e=>{if(a){e.target.dispatchEvent(c("mouseup",u));if(e.cancelable){e.preventDefault()}a=false}};e.addEventListener("touchend",h,o);e.addEventListener("touchcancel",h,o);s.push({element:e,touchstart:l,touchmove:i,touchend:h})};const u=e=>{const t=s.findIndex(t=>t.element===e);if(t===-1){return}const n=s[t];["touchstart","touchmove","touchend"].forEach(t=>{e.removeEventListener(t,n[t],o)});e.removeEventListener("touchcancel",n.touchend,o);s.splice(t,1)};return{export:{enableTouchMove:r,disableTouchMove:u}}}})();