Skip to content

Commit 09110ae

Browse files
committed
feat: refactor dragposition into evaluator (#148)
1 parent 2e28780 commit 09110ae

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

packages/core/src/controlledEnvironment/DragAndDropProvider.tsx

+1-19
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export const DragAndDropProvider: React.FC<React.PropsWithChildren> = ({
3535
}>({});
3636
const [programmaticDragIndex, setProgrammaticDragIndex] = useState(0);
3737
const [draggingPosition, setDraggingPosition] = useState<DraggingPosition>();
38-
const [dragCode, setDragCode] = useState('_nodrag'); // TODO should be moved into evaluator
3938
const getViableDragPositions = useGetViableDragPositions();
4039
const callSoon = useCallSoon();
4140
const getOriginalItemOrder = useGetOriginalItemOrder();
@@ -100,7 +99,6 @@ export const DragAndDropProvider: React.FC<React.PropsWithChildren> = ({
10099
setViableDragPositions({});
101100
setProgrammaticDragIndex(0);
102101
setDraggingPosition(undefined);
103-
setDragCode('_nodrag');
104102
setDropPositionEvaluator(undefined);
105103
}, []);
106104

@@ -175,32 +173,16 @@ export const DragAndDropProvider: React.FC<React.PropsWithChildren> = ({
175173
treeId: string,
176174
containerRef: React.MutableRefObject<HTMLElement | undefined>
177175
) => {
178-
// TODO move dragcode check into evaluator
179176
if (!dropPositionEvaluator) return;
180-
const hoveringPosition = dropPositionEvaluator.getHoveringPosition(
177+
const newDraggingPosition = dropPositionEvaluator.getDraggingPosition(
181178
e,
182179
treeId,
183180
containerRef
184181
);
185-
const newDragCode = dropPositionEvaluator.getDragCode(
186-
e,
187-
treeId,
188-
hoveringPosition
189-
);
190-
if (newDragCode === dragCode) {
191-
return;
192-
}
193-
setDragCode(newDragCode);
194-
const newDraggingPosition = dropPositionEvaluator.getDraggingPosition(
195-
e,
196-
hoveringPosition,
197-
treeId
198-
);
199182
if (!newDraggingPosition) {
200183
setDraggingPosition(undefined);
201184
return;
202185
}
203-
// setItemHeight(dropPositionEvaluator.itemHeight);
204186
performDrag(newDraggingPosition);
205187
}
206188
);

packages/core/src/controlledEnvironment/DropPositionEvaluation.ts

+28-12
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,60 @@ export class DropPositionEvaluation {
1818

1919
private getParentOfLinearItem: ReturnType<typeof useGetGetParentOfLinearItem>;
2020

21-
public readonly draggingItems: TreeItem[] | undefined; // TODO make private again
21+
private dragCode = 'initial';
2222

23-
public readonly itemHeight: number; // TODO should maybe be calculated inside, when dragging starts/class is instantiated
23+
public readonly draggingItems: TreeItem[] | undefined;
24+
25+
public readonly itemHeight: number;
2426

2527
constructor(
26-
// TODO direct variables didnt work with the current ts version
2728
env: TreeEnvironmentContextProps,
2829
getParentOfLinearItem: ReturnType<typeof useGetGetParentOfLinearItem>,
29-
draggingItems: TreeItem[] | undefined, // TODO make private again
30-
itemHeight: number // TODO should maybe be calculated inside, when dragging starts/class is instantiated
30+
draggingItems: TreeItem[] | undefined,
31+
itemHeight: number
3132
) {
3233
this.env = env;
3334
this.getParentOfLinearItem = getParentOfLinearItem;
3435
this.draggingItems = draggingItems;
3536
this.itemHeight = itemHeight;
3637
}
3738

38-
getDragCode(
39+
isNewDragPosition(
3940
e: DragEvent,
4041
treeId: string,
4142
hoveringPosition: HoveringPosition | undefined
4243
) {
4344
if (!hoveringPosition) {
44-
return 'invalidhover';
45+
return false;
4546
}
4647
const { offset, linearIndex, veryBottom } = hoveringPosition;
4748

48-
return `${treeId}${linearIndex}${offset ?? ''}${veryBottom && 'vb'}`;
49+
const newDragCode = `${treeId}${linearIndex}${offset ?? ''}${
50+
veryBottom && 'vb'
51+
}`;
52+
if (newDragCode !== this.dragCode) {
53+
this.dragCode = newDragCode;
54+
return true;
55+
}
56+
57+
return false;
4958
}
5059

5160
// returning undefined means calling onDragAtPosition(undefined), returning a dropposition means calling onPerformDrag(dropposition)
5261
// TODO old function sometimes returned undefined when old state could be kept; is it okay to also return undefined to enter invalid drop state here? e.g. !this.draggingItems, !canDragAndDrop...
5362
getDraggingPosition(
5463
e: DragEvent,
55-
hoveringPosition: HoveringPosition | undefined,
56-
treeId: string
64+
treeId: string,
65+
containerRef: React.MutableRefObject<HTMLElement | undefined>
5766
): DraggingPosition | undefined {
58-
if (!this.draggingItems || !this.env.canDragAndDrop || !hoveringPosition) {
67+
const hoveringPosition = this.getHoveringPosition(e, treeId, containerRef);
68+
69+
if (
70+
!this.draggingItems ||
71+
!this.env.canDragAndDrop ||
72+
!hoveringPosition ||
73+
!this.isNewDragPosition(e, treeId, hoveringPosition)
74+
) {
5975
return undefined;
6076
}
6177

@@ -185,7 +201,7 @@ export class DropPositionEvaluation {
185201
/**
186202
* Returns undefined for invalid drop targets, like outside the tree.
187203
*/
188-
getHoveringPosition(
204+
private getHoveringPosition(
189205
e: DragEvent,
190206
treeId: string,
191207
containerRef: React.MutableRefObject<HTMLElement | undefined>

0 commit comments

Comments
 (0)