diff --git a/docs/dragmove.js b/docs/dragmove.js deleted file mode 100644 index 5880adb..0000000 --- a/docs/dragmove.js +++ /dev/null @@ -1,89 +0,0 @@ -// https://github.com/knadh/dragmove.js -// Kailash Nadh (c) 2020. -// MIT License. - -let _loaded = false; -let _callbacks = []; -const _isTouch = window.ontouchstart !== undefined; - -export const dragmove = function(target, handler, onStart, onEnd) { - // Register a global event to capture mouse moves (once). - if (!_loaded) { - document.addEventListener(_isTouch ? "touchmove" : "mousemove", function(e) { - let c = e; - if (e.touches) { - c = e.touches[0]; - } - - // On mouse move, dispatch the coords to all registered callbacks. - for (var i = 0; i < _callbacks.length; i++) { - _callbacks[i](c.clientX, c.clientY); - } - }); - } - - _loaded = true; - let isMoving = false, hasStarted = false; - let startX = 0, startY = 0, lastX = 0, lastY = 0; - - // On the first click and hold, record the offset of the pointer in relation - // to the point of click inside the element. - handler.addEventListener(_isTouch ? "touchstart" : "mousedown", function(e) { - e.stopPropagation(); - e.preventDefault(); - if (target.dataset.dragEnabled === "false") { - return; - } - - let c = e; - if (e.touches) { - c = e.touches[0]; - } - - isMoving = true; - startX = target.offsetLeft - c.clientX; - startY = target.offsetTop - c.clientY; - }); - - // On leaving click, stop moving. - document.addEventListener(_isTouch ? "touchend" : "mouseup", function(e) { - if (onEnd && hasStarted) { - onEnd(target, parseInt(target.style.left), parseInt(target.style.top)); - } - - isMoving = false; - hasStarted = false; - }); - - // Register mouse-move callback to move the element. - _callbacks.push(function move(x, y) { - if (!isMoving) { - return; - } - - if (!hasStarted) { - hasStarted = true; - if (onStart) { - onStart(target, lastX, lastY); - } - } - - lastX = x + startX; - lastY = y + startY; - - // If boundary checking is on, don't let the element cross the viewport. - if (target.dataset.dragBoundary === "true") { - if (lastX < 1 || lastX >= window.innerWidth - target.offsetWidth) { - return; - } - if (lastY < 1 || lastY >= window.innerHeight - target.offsetHeight) { - return; - } - } - - target.style.left = lastX + "px"; - target.style.top = lastY + "px"; - }); -} - -export { dragmove as default }; diff --git a/docs/dragmove.min.js b/docs/dragmove.min.js new file mode 100644 index 0000000..51a294f --- /dev/null +++ b/docs/dragmove.min.js @@ -0,0 +1 @@ +let _t=void 0!==window.ontouchstart,_evStart=_t?"touchstart":"mousedown",_evMove=_t?"touchmove":"mousemove",_evStop=_t?"touchend":"mouseup",_h=null,_on=!1,_items=null;export const dragmove=(t,e,o,n)=>(_items||(_items=new WeakMap,[_evStart,_evMove,_evStop].forEach(t=>{document.addEventListener(t,handle)})),_items.set(e,{t:t,onStart:o,onEnd:n}),{remove:()=>_items.delete(e)});let handle=t=>{let e=t.touches?t.touches[0]:t,o=e.target,n=_items.get(_h||o);if(n){if(e.type===_evStart)e.stopPropagation(),e.preventDefault(),_on=!0,n.sX=n.t.offsetLeft-e.clientX,n.sY=n.t.offsetTop-e.clientY;else if(e.type===_evMove){if(!_on)return;!_h&&(_h=o,n.onStart&&n.onStart(o,n.x,n.y)),n.x=e.clientX+n.sX,n.y=e.clientY+n.sY,"true"===n.t.dataset.dragBoundary&&(n.x=Math.min(window.innerWidth-n.t.offsetWidth,Math.max(0,n.x)),n.y=Math.min(window.innerHeight-n.t.offsetHeight,Math.max(0,n.y))),n.t.style.left=n.x+"px",n.t.style.top=n.y+"px"}else n.onEnd&&_h&&n.onEnd(n.t,n.x,n.y),_on=!1,_h=null}};export{dragmove as default}; \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index fcd143b..37eaec4 100644 --- a/docs/index.html +++ b/docs/index.html @@ -33,7 +33,6 @@ } .handle span { display: inline-block; - background: red; pointer-events: none; } @@ -48,7 +47,7 @@