Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamilenkovic committed Sep 12, 2024
1 parent cc53fc1 commit 7d31861
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
54 changes: 26 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function setDragState<T>(
): DragState<T> | SynthDragState<T> {
Object.assign(state, dragStateProps);

state.emit("dragStarted", state.clonedDraggedNode);
state.emit("dragStarted", state);

return state as DragState<T> | SynthDragState<T>;
}
Expand Down Expand Up @@ -998,7 +998,6 @@ export function handleEnd<T>(
state: DragState<T> | SynthDragState<T>
) {
if (!state) return;
return;

data.e.preventDefault();

Expand All @@ -1013,7 +1012,6 @@ export function end<T>(
_eventData: NodeEventData<T>,
state: DragState<T> | SynthDragState<T>
) {
state.scrolling = false;
if (documentController) {
documentController.abort();

Expand Down Expand Up @@ -1223,16 +1221,14 @@ function getScrollData<T>(
};
}

let interval: number | null = null;

let animationFrameId: number | null = null;

function setSynthScrollDirection<T>(
direction: "up" | "down" | "left" | "right" | undefined,
el: HTMLElement,
state: SynthDragState<T>
) {
if (state.syntheticScrollDirection === direction) return;
if (state.synthScrollDirection === direction) return;

if (direction === "up" && el.scrollTop === 0) return;

Expand All @@ -1244,7 +1240,7 @@ function setSynthScrollDirection<T>(
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) {
Expand All @@ -1262,11 +1258,11 @@ function setSynthScrollDirection<T>(
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;
Expand Down Expand Up @@ -1365,29 +1361,29 @@ function shouldScrollLeft<T>(
return state;
}

function shouldScrollUp<T>(
state: DragState<T>,
data: ScrollData<T>
): DragState<T> | void {
function shouldScrollUp<T>(state: DragState<T>, data: ScrollData<T>): 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<T>(
state: DragState<T>,
data: ScrollData<T>
): 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 &&
Expand All @@ -1396,8 +1392,10 @@ function shouldScrollDown<T>(
data.scrollParent.scrollHeight
)
) {
return data.scrollParent;
return true;
}

return false;
}

function moveNode<T>(data: NodePointerEventData<T>, state: SynthDragState<T>) {
Expand Down Expand Up @@ -1438,8 +1436,6 @@ export function synthMove<T>(

moveNode(data, state);

//handleScroll(data, state);

const elFromPoint = getElFromPoint(data);

if (!elFromPoint) return;
Expand Down Expand Up @@ -1472,15 +1468,19 @@ export function handleScroll<T>(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;

break;
}
}

if (!directionSet) state.syntheticScrollDirection = undefined;
if (!directionSet) state.synthScrollDirection = undefined;
}

export function handleDragoverNode<T>(
Expand Down Expand Up @@ -1508,15 +1508,12 @@ export function handleDragoverParent<T>(

state.coordinates.x = x;

//if (!state.initialParent.data.config.nativeDragScroll) handleScroll(state);

transfer(data, state);
}

export function handlePointeroverParent<T>(e: PointeroverParentEvent<T>) {
if (!state) return;

transfer(e.detail, state);
export function handlePointeroverParent<T>(e: PointeroverNodeEvent<T>) {
if (e.detail.targetData.parent.el !== e.detail.state.lastParent.el)
transfer(e.detail, e.detail.state);
}

export function validateTransfer<T>(
Expand Down Expand Up @@ -1613,6 +1610,7 @@ export function validateSort<T>(

if (state.draggedNodes.map((x) => x.el).includes(data.targetData.node.el)) {
state.lastTargetValue = undefined;

return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ export interface SynthDragStateProps<T> {
/**
* Direction of the synthetic drag scroll
*/
syntheticScrollDirection: "up" | "down" | "left" | "right" | undefined;
synthScrollDirection: "up" | "down" | "left" | "right" | undefined;
/**
* The display of the synthetic node.
*/
Expand Down Expand Up @@ -735,7 +735,7 @@ export interface DragStateProps<T> {
/**
* The last value the dragged node targeted.
*/
lastTargetValue: T;
lastTargetValue: T | undefined;
/**
* longPress - A flag to indicate whether a long press has occurred.
*/
Expand Down

0 comments on commit 7d31861

Please sign in to comment.