Skip to content

Commit

Permalink
chore(utils): rename collision utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcrea committed Nov 21, 2024
1 parent 77eb033 commit c71f945
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 47 deletions.
10 changes: 4 additions & 6 deletions example/src/pages/DraggableBasicExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import {
DroppableProps,
useDraggableStyle,
useDroppableStyle,
} from '@mgcrea/react-native-dnd/src';
} from '@mgcrea/react-native-dnd';
import React, {useState, type FunctionComponent} from 'react';
import {SafeAreaView, StyleSheet, Text, View} from 'react-native';
import {GestureHandlerRootView} from 'react-native-gesture-handler';
import {SafeAreaView, StyleSheet, Text} from 'react-native';
import Animated, {
runOnJS,
useSharedValue,
withSpring,
} from 'react-native-reanimated';
import {MyDraggableBox} from '../components';

export const DraggableBasicExample: FunctionComponent = () => {
const [count, setCount] = useState(0);
Expand Down Expand Up @@ -47,7 +45,7 @@ export const DraggableBasicExample: FunctionComponent = () => {
};

return (
<View style={styles.container}>
<SafeAreaView style={styles.container}>
<DndProvider
onBegin={handleBegin}
onFinalize={handleFinalize}
Expand All @@ -60,7 +58,7 @@ export const DraggableBasicExample: FunctionComponent = () => {
</MyDraggable>
<Text testID="button">count is {count}</Text>
</DndProvider>
</View>
</SafeAreaView>
);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"lint": "eslint src/ test/",
"spec": "jest",
"prettycheck": "prettier --check src/ test/",
"typecheck": "tsc --noEmit",
"check": "tsc --noEmit",
"test": "npm run typecheck && npm run spec && npm run lint && npm run prettycheck",
"prepublishOnly": "npm run build"
},
Expand Down
4 changes: 2 additions & 2 deletions src/features/sort/hooks/useDraggableGrid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type FlexStyle } from "react-native";
import { useAnimatedReaction } from "react-native-reanimated";
import { swapByItemCenterPoint } from "../../../utils";
import { doesCenterOverlap } from "../../../utils";
import { useDndContext } from "./../../../DndContext";
import { useDraggableSort, type UseDraggableSortOptions } from "./useDraggableSort";

Expand All @@ -20,7 +20,7 @@ export const useDraggableGrid = ({
gap = 0,
size,
direction = "row",
shouldSwapWorklet = swapByItemCenterPoint,
shouldSwapWorklet = doesCenterOverlap,
}: UseDraggableGridOptions) => {
const { draggableActiveId, draggableOffsets, draggableRestingOffsets, draggableLayouts } = useDndContext();
const horizontal = ["row", "row-reverse"].includes(direction);
Expand Down
14 changes: 10 additions & 4 deletions src/features/sort/hooks/useDraggableSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import {
applyOffset,
arraysEqual,
centerAxis,
type Direction,
moveArrayIndex,
overlapsAxis,
type Rectangle,
} from "../../../utils";

export type ShouldSwapWorklet = (activeLayout: Rectangle, itemLayout: Rectangle) => boolean;
export type ShouldSwapWorklet = (
activeLayout: Rectangle,
itemLayout: Rectangle,
direction: Direction,
) => boolean;

export type UseDraggableSortOptions = {
initialOrder?: UniqueIdentifier[];
Expand All @@ -29,6 +34,7 @@ export const useDraggableSort = ({
shouldSwapWorklet,
}: UseDraggableSortOptions) => {
const { draggableActiveId, draggableActiveLayout, draggableOffsets, draggableLayouts } = useDndContext();
const direction = horizontal ? "horizontal" : "vertical";

const draggablePlaceholderIndex = useSharedValue(-1);
const draggableLastOrder = useSharedValue<UniqueIdentifier[]>(initialOrder);
Expand Down Expand Up @@ -59,16 +65,16 @@ export const useDraggableSort = ({
});

if (shouldSwapWorklet) {
if (shouldSwapWorklet(activeLayout, itemLayout)) {
if (shouldSwapWorklet(activeLayout, itemLayout, direction)) {
// console.log(`Found placeholder index ${itemIndex} using custom shouldSwapWorklet!`);
return itemIndex;
}
continue;
}

// Default to center axis
const itemCenterAxis = centerAxis(itemLayout, horizontal);
if (overlapsAxis(activeLayout, itemCenterAxis, horizontal)) {
const itemCenterAxis = centerAxis(itemLayout, direction);
if (overlapsAxis(activeLayout, itemCenterAxis, direction)) {
return itemIndex;
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/features/sort/hooks/useDraggableStack.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from "react";
import { useAnimatedReaction } from "react-native-reanimated";
import { useDndContext } from "../../../DndContext";
import { swapByItemHorizontalAxis, swapByItemVerticalAxis } from "../../../utils";
import { doesOverlapOnAxis } from "../../../utils";
import { useDraggableSort, type UseDraggableSortOptions } from "./useDraggableSort";

export type UseDraggableStackOptions = Pick<
Expand All @@ -23,9 +23,8 @@ export const useDraggableStack = ({
const axis = horizontal ? "x" : "y";
const size = horizontal ? "width" : "height";
const worklet = useMemo(
() =>
shouldSwapWorklet ? shouldSwapWorklet : horizontal ? swapByItemHorizontalAxis : swapByItemVerticalAxis,
[horizontal, shouldSwapWorklet],
() => (shouldSwapWorklet ? shouldSwapWorklet : doesOverlapOnAxis),
[shouldSwapWorklet],
);

const { draggablePlaceholderIndex, draggableSortOrder } = useDraggableSort({
Expand Down
23 changes: 23 additions & 0 deletions src/utils/collision.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { centerAxis, centerPoint, Direction, includesPoint, overlapsAxis, type Rectangle } from "./geometry";

export const doesCenterOverlap = (activeLayout: Rectangle, itemLayout: Rectangle) => {
"worklet";
const itemCenterPoint = centerPoint(itemLayout);
return includesPoint(activeLayout, itemCenterPoint);
};

export const doesOverlapOnAxis = (activeLayout: Rectangle, itemLayout: Rectangle, direction: Direction) => {
"worklet";
const itemCenterAxis = centerAxis(itemLayout, direction);
return overlapsAxis(activeLayout, itemCenterAxis, direction);
};

export const doesOverlapHorizontally = (activeLayout: Rectangle, itemLayout: Rectangle) => {
"worklet";
return doesOverlapOnAxis(activeLayout, itemLayout, "horizontal");
};

export const doesOverlapVertically = (activeLayout: Rectangle, itemLayout: Rectangle) => {
"worklet";
return doesOverlapOnAxis(activeLayout, itemLayout, "vertical");
};
10 changes: 6 additions & 4 deletions src/utils/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type Rectangle = {
height: number;
};

export type Direction = "horizontal" | "vertical";

/**
* @summary Split a `Rectangle` in two
* @worklet
Expand Down Expand Up @@ -104,18 +106,18 @@ export const centerPoint = (layout: Rectangle): Point => {
* @summary Compute a center axis
* @worklet
*/
export const centerAxis = (layout: Rectangle, horizontal: boolean): number => {
export const centerAxis = (layout: Rectangle, direction: Direction): number => {
"worklet";
return horizontal ? layout.x + layout.width / 2 : layout.y + layout.height / 2;
return direction === "horizontal" ? layout.x + layout.width / 2 : layout.y + layout.height / 2;
};

/**
* @summary Checks if a `Rectangle` overlaps with an axis
* @worklet
*/
export const overlapsAxis = (layout: Rectangle, axis: number, horizontal: boolean) => {
export const overlapsAxis = (layout: Rectangle, axis: number, direction: Direction) => {
"worklet";
return horizontal
return direction === "horizontal"
? layout.x < axis && layout.x + layout.width > axis
: layout.y < axis && layout.y + layout.height > axis;
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./array";
export * from "./assert";
export * from "./collision";
export * from "./geometry";
export * from "./random";
export * from "./reanimated";
export * from "./swap";
25 changes: 0 additions & 25 deletions src/utils/swap.ts

This file was deleted.

0 comments on commit c71f945

Please sign in to comment.