Skip to content

Commit 21cc0a3

Browse files
committed
wip
1 parent 65025cd commit 21cc0a3

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

packages/dflex-dnd/src/Mechanism/DFlexMechanismController.ts

+44-21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import DFlexScrollableElement from "./DFlexScrollableElement";
1313
export function isIDEligible(elmID: string, draggedID: string): boolean {
1414
const { registry } = store;
1515

16+
if (__DEV__) {
17+
if (!elmID) {
18+
throw new Error("isIDEligible: received undefined elmID");
19+
}
20+
}
21+
1622
return (
1723
elmID.length > 0 &&
1824
elmID !== draggedID &&
@@ -21,6 +27,23 @@ export function isIDEligible(elmID: string, draggedID: string): boolean {
2127
);
2228
}
2329

30+
// eslint-disable-next-line no-unused-vars
31+
type ForEachSiblingsCB = (elmID: string, index: number) => boolean;
32+
33+
function forEachDraggedSiblings(cb: ForEachSiblingsCB) {
34+
const { SK } = store.migration.latest();
35+
36+
const siblings = store.getElmSiblingsByKey(SK);
37+
38+
for (let i = 0; i < siblings.length; i += 1) {
39+
const id = siblings[i];
40+
41+
if (cb(id, i)) {
42+
break;
43+
}
44+
}
45+
}
46+
2447
class DFlexMechanismController extends DFlexScrollableElement {
2548
private isOnDragOutThresholdEvtEmitted: boolean;
2649

@@ -71,27 +94,27 @@ class DFlexMechanismController extends DFlexScrollableElement {
7194

7295
const { draggedElm } = this.draggable;
7396

74-
const { SK } = store.migration.latest();
75-
76-
const siblings = store.getElmSiblingsByKey(SK);
97+
forEachDraggedSiblings((id, i) => {
98+
if (!isIDEligible(id, draggedElm.id)) {
99+
return false;
100+
}
77101

78-
for (let i = 0; i < siblings.length; i += 1) {
79-
const id = siblings[i];
102+
let isQualified = false;
80103

81-
if (isIDEligible(id, draggedElm.id)) {
82-
const element = store.registry.get(id)!;
104+
const dflexElm = store.registry.get(id)!;
83105

84-
const isQualified = element.rect.isIntersect(
85-
this.draggable.getAbsoluteCurrentPosition()
86-
);
106+
isQualified = dflexElm.rect.isIntersect(
107+
this.draggable.getAbsoluteCurrentPosition()
108+
);
87109

88-
if (isQualified) {
89-
droppableIndex = i;
110+
if (isQualified) {
111+
droppableIndex = i;
90112

91-
break;
92-
}
113+
return true;
93114
}
94-
}
115+
116+
return false;
117+
});
95118

96119
return droppableIndex;
97120
}
@@ -109,7 +132,7 @@ class DFlexMechanismController extends DFlexScrollableElement {
109132
* If tempIndex is zero, the dragged is coming from the top. So, move them
110133
* down all: to=0
111134
*/
112-
let hasToMoveSiblingsDown = true;
135+
let hasToShiftSiblings = true;
113136

114137
const isEmpty = isEmptyBranch(siblings);
115138

@@ -129,7 +152,7 @@ class DFlexMechanismController extends DFlexScrollableElement {
129152

130153
insertAt = siblings.length - 1;
131154

132-
hasToMoveSiblingsDown = false;
155+
hasToShiftSiblings = false;
133156
}
134157

135158
this.draggable.setDraggedTempIndex(insertAt);
@@ -144,15 +167,15 @@ class DFlexMechanismController extends DFlexScrollableElement {
144167
this.getComposedOccupiedTranslateAndGrid(
145168
SK,
146169
insertAt,
147-
hasToMoveSiblingsDown,
170+
hasToShiftSiblings,
148171
"y"
149172
));
150173
}
151174

152175
// If it has solo empty id then there's no need to move down. Because it's
153176
// empty branch.
154-
if (hasToMoveSiblingsDown && !isEmpty) {
155-
this._moveDown(insertAt);
177+
if (hasToShiftSiblings && !isEmpty) {
178+
this._shiftSiblings(insertAt);
156179
}
157180

158181
draggedElm.removeAttribute(this.draggable.draggedDOM, "OUT_CONTAINER");
@@ -372,7 +395,7 @@ class DFlexMechanismController extends DFlexScrollableElement {
372395
*
373396
* @param to - index
374397
*/
375-
private _moveDown(to: number): void {
398+
private _shiftSiblings(to: number): void {
376399
const { events, draggedElm } = this.draggable;
377400
const { migration } = store;
378401

packages/dflex-utils/src/Box/BoxNum.ts

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ class BoxNum extends Box<number> {
1616
);
1717
}
1818

19+
isIntersectY(box: BoxNum): boolean {
20+
return this.top < box.bottom || this.bottom > box.top;
21+
}
22+
23+
isIntersectX(box: BoxNum): boolean {
24+
return this.left < box.right || this.right > box.left;
25+
}
26+
1927
/**
2028
* True when it's intersected with other box.
2129
*

packages/dflex-utils/src/constants.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** Single Axis. */
2+
export const AXIS = Object.freeze({
3+
X: "x",
4+
Y: "y",
5+
});
6+
7+
export type Axis = (typeof AXIS)[keyof typeof AXIS];

packages/dflex-utils/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export type {
1616
RectDimensions,
1717
RectBoundaries,
1818
Axes,
19-
Axis,
2019
Direction,
2120
} from "./types";
2221

@@ -35,3 +34,7 @@ export {
3534
export { canUseDOM, getSelection, getParentElm } from "./dom";
3635

3736
export * as featureFlags from "./FeatureFlags";
37+
38+
export { AXIS } from "./constants";
39+
40+
export type { Axis } from "./constants";

0 commit comments

Comments
 (0)