Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | 1x 10x 10x 10x 10x 10x 13x 13x 13x 13x 13x 10x 10x 10x 10x 10x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 5x 5x 12x 12x 12x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 6x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 8x 13x 13x 13x 13x 13x 13x 13x 3x 3x 13x 7x 7x 7x 13x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x | /*
* Copyright (c) 2022-2023 Braun Nathanael
*
* This project is dual licensed under one of the following licenses:
* - Creative Commons Attribution-NoDerivatives 4.0 International License.
* - GNU AFFERO GENERAL PUBLIC LICENSE Version 3
*
* You should have received a copy of theses licenses along with this work.
* If not, see <http://creativecommons.org/licenses/by-nd/4.0/> or <http://www.gnu.org/licenses/agpl-3.0.txt>.
*/
import is from "is";
import React from 'react';
import ReactDom from "react-dom";
import useVoodoo from "../hooks/useVoodoo";
import domUtils from "../utils/dom";
/**
* Draggable — captures pointer (touch / mouse) events and maps them to axis positions
* on the parent Tweener hierarchy.
*
* On drag, it walks up the tweener tree via `_parentTweener` links (not the DOM) to
* find all ancestor tweeners that have the requested axis. This supports nested
* scrollable regions: the innermost tweener that is still in-bounds consumes the drag;
* if it hits a bound the event falls through to the next ancestor.
*
* Dispatch flow:
* dragstart → captures parent tweeners and starts the 16ms inertia loop
* drag → computes delta as a fraction of scrollableWindow/scrollableArea,
* calls inertia.hold(newPos) on the first tweener that is in-bounds
* dropped → calls inertia.release() which computes the momentum arc and may
* snap to the nearest waypoint
*/
const Draggable = React.forwardRef(( {
children,
Comp = 'div',
nodeRef,
items = [],
xAxis, yAxis, yBoxRef,
xBoxRef, yRef,
yHook, xHook,
tweener,
mouseDrag = false,
touchDrag = true,
button = 0,
...props
}, ref ) => {
let root = React.useRef(),
µ = React.useRef({ root, _: {} }).current,
[parentTweener] = useVoodoo(true),
api = React.useMemo(
() => ({
/**
* Traverse the tweener parent chain (not the DOM ancestry) and collect all
* ancestor Tweener instances that could potentially consume scroll input.
* The result is iterated during drag to find the first in-bounds tweener.
*/
getScrollableNodes( node ) {
let scrollable = [], parent = µ._parentTweener;
while ( parent ) {
scrollable.push(parent);
parent = parent._parentTweener;
}
return scrollable
},
/**
* Attach the touch/mouse event listeners to the root DOM node.
* All drag state is captured in closure variables (dX, dY, cLock,
* parents, parentsState) so individual handler functions stay small.
*/
_registerScrollListeners() {
let rootNode = µ.root?.current,
{
xAxis, yAxis, yHook, xHook, mouseDrag, touchDrag, tweener, xBoxRef, yBoxRef, button
} = µ.props,
lastStartTm,
cLock, dX,
parents,
dY, parentsState, refWidth, refHeight,
// true once hold() has been dispatched during the gesture — i.e. the
// content actually moved under the pointer. Releasing such a gesture
// must never produce a click, regardless of the NET pointer offset
// (an out-and-back swipe can end within maxClickOffset).
didDrag,
_ = tweener._;
if ( rootNode ) {
domUtils.addEvent(
rootNode, µ._.dragList = {
'dragstart': ( e, touch, descr ) => {//@todo
let pTweener,
x,
y, i, style;
Iif ( (e) instanceof MouseEvent && e.button !== button ) {// allow undefined so µ work for touch events
return;
}
parents = api.getScrollableNodes(e.target);
lastStartTm = Date.now();
dX = 0;
dY = 0;
didDrag = false;
parentsState = [];
refWidth = (xBoxRef?.current || rootNode)?.offsetWidth;
refHeight = (yBoxRef?.current || rootNode)?.offsetHeight;
for ( i = 0; i < parents.length; i++ ) {
pTweener = parents[i];
pTweener._updateBox();
// react comp with tweener support
Eif ( pTweener.__isTweener ) {
x = xAxis && pTweener.axes?.[xAxis];
y = yAxis && pTweener.axes?.[yAxis];
pTweener._updateNodeInertia()
}
}
//tweener._updateNodeInertia()
//e.stopPropagation();
//e.preventDefault();
},
'click' : ( e, touch, descr ) => {//@todo
Iif ( (e) instanceof MouseEvent && e.button !== button ) {// allow undefined so µ work for touch events
return;
}
Iif ( lastStartTm &&
(
didDrag ||
Math.abs(dY) > _.options.maxClickOffset ||
Math.abs(dX) > _.options.maxClickOffset
)
) {
e.preventDefault();
e.stopPropagation();
}
},
// drag: runs on every pointer move. Converts pixel offset to axis units,
// applies dragDirectionLock, then dispatches to the first ancestor tweener
// whose inertia.isInbound() returns true — preventing over-scroll propagation.
'drag': ( e, touch, descr ) => {
// no e.button check here : mouse button filtering is done at mousedown time
// in domUtils.dragstartAnywhere (e.button is always 0 on mousemove events,
// so it can't be tested reliably here)
let pTweener,
x, deltaX, xDispatched, xBox,
y, deltaY, yDispatched, yBox,
cState, i;
dX = -(descr._lastPos.x - descr._startPos.x);
dY = -(descr._lastPos.y - descr._startPos.y);
if ( lastStartTm && ((lastStartTm > Date.now() - _.options.maxClickTm) && Math.abs(dY) < _.options.maxClickOffset && Math.abs(dX) < _.options.maxClickOffset) )// skip tap & click
{
return;
}
else {
xDispatched = !dX;
yDispatched = !dY;
Iif ( _.options.dragDirectionLock ) {
if ( cLock === "Y" || !cLock && Math.abs(dY * .5) > Math.abs(dX) ) {
cLock = "Y";
dX = 0;
//xDispatched = true;
}
else if ( cLock === "X" || !cLock && Math.abs(dX * .5) > Math.abs(dY) ) {
cLock = "X";
dY = 0;
//yDispatched = true;
}
}
if ( parents )
for ( i = 0; i < parents.length; i++ ) {
pTweener = parents[i];
// react comp with tweener support
Eif ( pTweener.__isTweener ) {
x = xAxis && pTweener.axes?.[xAxis];
y = yAxis && pTweener.axes?.[yAxis];
if ( !x && !y )
continue;
if ( !parentsState[i] ) {
parentsState[i] = {
x: x?.scrollPos,
y: y?.scrollPos
};
x?.inertia?.startMove();
y?.inertia?.startMove();
xAxis && x && !x?.inertiaFrame && pTweener.applyInertia(x, xAxis);
yAxis && y && !y?.inertiaFrame && pTweener.applyInertia(y, yAxis);
}
Eif ( x ) {
xBox = xBoxRef?.current
? refWidth
: pTweener._.box.x
deltaX = dX && (dX / xBox) * (
x.scrollableWindow ||
x.scrollableArea) || 0;
Iif ( xHook )
deltaX = xHook(deltaX);
}
Iif ( y ) {
yBox = yBoxRef?.current
? refHeight
: pTweener._.box.y
deltaY = dY && (dY / yBox) * (
y.scrollableWindow ||
y.scrollableArea) || 0;
if ( yHook )
deltaY = yHook(deltaY);
}
Eif ( x && !xDispatched && deltaX && x?.inertia?.isInbound(parentsState[i].x + deltaX)
&& (pTweener.componentShouldScroll(xAxis, deltaX)) ) {
x.inertia.hold(parentsState[i].x + deltaX);
//parentsState[i].x = x.inertia._.pos;
xDispatched = didDrag = true;
}
Iif ( y && !yDispatched && deltaY && y?.inertia?.isInbound(parentsState[i].y + deltaY)
&& (pTweener.componentShouldScroll(yAxis, deltaY)) ) {
y.inertia.hold(parentsState[i].y + deltaY);
//parentsState[i].y = y.inertia._.pos;
yDispatched = didDrag = true;
}
}
}
else
Econsole.warn("React-Voodoo : drag called without dragstart !")
if ( yDispatched && xDispatched ) {
//e.stopPropagation();
//e.cancelable && e.preventDefault();
//return;
}
//dX = 0;
//dY = 0;
}
}
,
// dropped: fires when the pointer is released. Calls inertia.release() on each
// ancestor tweener that was participating in the drag; inertia then computes
// momentum and snaps to the nearest waypoint if configured.
'dropped': ( e, touch, descr ) => {
let pTweener,
x, deltaX, xDispatched, vX,
y, deltaY, yDispatched, vY,
cState, i;
// no e.button check here : domUtils ends the tracked gesture on ANY mouseup,
// so inertia must always be released to avoid a stuck hold
cLock = undefined;
// recompute the offsets from the release position : on a fast flick the
// OS coalesces moves and most of the displacement may only be reported
// by the mouseup event (descr._lastPos is updated by dropAnywhere before
// this handler runs) — the last 'drag' values would be stale
dX = -(descr._lastPos.x - descr._startPos.x);
dY = -(descr._lastPos.y - descr._startPos.y);
//lastStartTm = undefined;
//document.body.style.userSelect = '';
//document.body.style.touchAction = '';
if ( parents )
for ( i = 0; i < parents.length; i++ ) {
pTweener = parents[i];
// react comp with tweener support
if ( pTweener.__isTweener && parentsState[i] ) {
pTweener.axes?.[xAxis]?.inertia?.release();
pTweener.axes?.[yAxis]?.inertia?.release();
//pTweener._updateNodeInertia()
}
//else if ( is.element(tweener) ) {
// cState = parentsState[i];
// if ( cState ) {
// cState.inertia.x.release();
// cState.inertia.y.release();
// }
//}
}
else
Econsole.warn("React-Voodoo : dropped called without dragstart !")
// suppress the post-gesture click iff the gesture MOVED — content
// dragged (didDrag) or pointer travelled beyond maxClickOffset.
// Duration alone never disqualifies a click: a motionless slow
// press is still a click, per browser semantics.
if ( lastStartTm && (didDrag
|| Math.abs(dY) > _.options.maxClickOffset
|| Math.abs(dX) > _.options.maxClickOffset) )
{
e.stopPropagation();
e.cancelable && e.preventDefault();
// real drag: arm the one-shot capture-phase click interceptor
// (domUtils.dropWithoutClick) — preventDefault on mouseup does NOT
// suppress the native click that follows, unlike touchend
descr.preventClick = true;
//return;
}
//else {
//}
//lastStartTm = 0;
parents = parentsState = null;
}
},
null,
_.options.enableMouseDrag || mouseDrag,
touchDrag,
null,
button
)
µ._.doRegister = !!rootNode;
}
else E{
µ._.doRegister = true;
}
}
}),
[]
);
React.useEffect(
() => {
api._registerScrollListeners();
return () => {
let node = µ.root?.current;
Iif ( µ._.scrollEnabled ) {
µ._.scrollEnabled = false;
node && µ._.dragList && domUtils.removeEvent(node
, µ._.dragList)
}
}
},
[]
)
React.useEffect(
() => {
Iif ( is.function(nodeRef) )
nodeRef(root.current)
Iif ( is.function(ref) )
ref(root.current)
else Iif ( ref )
ref.current = root.current;
},
[nodeRef, ref]
)
µ.props = {
xAxis, yAxis, yHook, xHook, mouseDrag, touchDrag, tweener: tweener || parentTweener, xBoxRef, yBoxRef, button
}
µ._parentTweener = parentTweener;
return <Comp ref={root} {...props}>{children}</Comp>;
})
export default Draggable;
Draggable.div = ( props ) => {
return <Draggable {...props}/>;
}
|