From 7d31861ee7c68297bbf7a50d57ad24637bbca72a Mon Sep 17 00:00:00 2001 From: Sasha Milenkovic Date: Wed, 11 Sep 2024 21:45:29 -0400 Subject: [PATCH] code cleanup --- src/index.ts | 54 +++++++++++++++++++++++++--------------------------- src/types.ts | 4 ++-- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/index.ts b/src/index.ts index 059b5a0..61d00c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -113,7 +113,7 @@ export function setDragState( ): DragState | SynthDragState { Object.assign(state, dragStateProps); - state.emit("dragStarted", state.clonedDraggedNode); + state.emit("dragStarted", state); return state as DragState | SynthDragState; } @@ -998,7 +998,6 @@ export function handleEnd( state: DragState | SynthDragState ) { if (!state) return; - return; data.e.preventDefault(); @@ -1013,7 +1012,6 @@ export function end( _eventData: NodeEventData, state: DragState | SynthDragState ) { - state.scrolling = false; if (documentController) { documentController.abort(); @@ -1223,8 +1221,6 @@ function getScrollData( }; } -let interval: number | null = null; - let animationFrameId: number | null = null; function setSynthScrollDirection( @@ -1232,7 +1228,7 @@ function setSynthScrollDirection( el: HTMLElement, state: SynthDragState ) { - if (state.syntheticScrollDirection === direction) return; + if (state.synthScrollDirection === direction) return; if (direction === "up" && el.scrollTop === 0) return; @@ -1244,7 +1240,7 @@ function setSynthScrollDirection( if (direction === "right" && el.scrollLeft + el.clientWidth >= el.scrollWidth) return; - state.syntheticScrollDirection = direction; + state.synthScrollDirection = direction; // Cancel any ongoing animation frame when direction changes if (animationFrameId !== null) { @@ -1262,11 +1258,11 @@ function setSynthScrollDirection( const elapsed = timestamp - lastTimestamp; // Base scroll speed in pixels per second - const baseSpeed = 1000; // Adjust this value as needed + const baseSpeed = 500; // Adjust this value as needed // Calculate how much to scroll based on time elapsed const distance = (baseSpeed * elapsed) / 1000; // Pixels to scroll - if (state.syntheticScrollDirection === undefined && animationFrameId) { + if (state.synthScrollDirection === undefined && animationFrameId) { cancelAnimationFrame(animationFrameId); animationFrameId = null; @@ -1365,29 +1361,29 @@ function shouldScrollLeft( return state; } -function shouldScrollUp( - state: DragState, - data: ScrollData -): DragState | void { +function shouldScrollUp(state: DragState, data: ScrollData): boolean { const diff = data.scrollParent.clientHeight + data.y - state.coordinates.y; - if (!data.scrollOutside && diff > data.scrollParent.clientHeight) return; + if (!data.scrollOutside && diff > data.scrollParent.clientHeight) + return false; if ( diff > data.yThresh * data.scrollParent.clientHeight && data.scrollParent.scrollTop !== 0 ) { - return data.scrollParent; + return true; } + + return false; } function shouldScrollDown( state: DragState, data: ScrollData -): HTMLElement | void { +): boolean { const diff = data.scrollParent.clientHeight + data.y - state.coordinates.y; - if (!data.scrollOutside && diff < 0) return; + if (!data.scrollOutside && diff < 0) return false; if ( diff < (1 - data.yThresh) * data.scrollParent.clientHeight && @@ -1396,8 +1392,10 @@ function shouldScrollDown( data.scrollParent.scrollHeight ) ) { - return data.scrollParent; + return true; } + + return false; } function moveNode(data: NodePointerEventData, state: SynthDragState) { @@ -1438,8 +1436,6 @@ export function synthMove( moveNode(data, state); - //handleScroll(data, state); - const elFromPoint = getElFromPoint(data); if (!elFromPoint) return; @@ -1472,7 +1468,11 @@ export function handleScroll(e: DragEvent | PointerEvent) { for (const direction of Object.keys(scrollConfig)) { if (shouldScroll(direction, e, state)) { - setSynthScrollDirection(direction, e.currentTarget, state); + setSynthScrollDirection( + direction as "up" | "down" | "left" | "right" | undefined, + e.currentTarget as HTMLElement, + state + ); directionSet = true; @@ -1480,7 +1480,7 @@ export function handleScroll(e: DragEvent | PointerEvent) { } } - if (!directionSet) state.syntheticScrollDirection = undefined; + if (!directionSet) state.synthScrollDirection = undefined; } export function handleDragoverNode( @@ -1508,15 +1508,12 @@ export function handleDragoverParent( state.coordinates.x = x; - //if (!state.initialParent.data.config.nativeDragScroll) handleScroll(state); - transfer(data, state); } -export function handlePointeroverParent(e: PointeroverParentEvent) { - if (!state) return; - - transfer(e.detail, state); +export function handlePointeroverParent(e: PointeroverNodeEvent) { + if (e.detail.targetData.parent.el !== e.detail.state.lastParent.el) + transfer(e.detail, e.detail.state); } export function validateTransfer( @@ -1613,6 +1610,7 @@ export function validateSort( if (state.draggedNodes.map((x) => x.el).includes(data.targetData.node.el)) { state.lastTargetValue = undefined; + return false; } diff --git a/src/types.ts b/src/types.ts index 93380da..1a7e03d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -651,7 +651,7 @@ export interface SynthDragStateProps { /** * Direction of the synthetic drag scroll */ - syntheticScrollDirection: "up" | "down" | "left" | "right" | undefined; + synthScrollDirection: "up" | "down" | "left" | "right" | undefined; /** * The display of the synthetic node. */ @@ -735,7 +735,7 @@ export interface DragStateProps { /** * The last value the dragged node targeted. */ - lastTargetValue: T; + lastTargetValue: T | undefined; /** * longPress - A flag to indicate whether a long press has occurred. */