@pie-lib/mask-markup 3.0.14-next.35 → 3.0.14-next.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/choices/choice.js +24 -6
- package/lib/choices/choice.js.map +1 -1
- package/lib/choices/index.js +32 -9
- package/lib/choices/index.js.map +1 -1
- package/lib/components/blank.js +106 -29
- package/lib/components/blank.js.map +1 -1
- package/lib/drag-in-the-blank.js +139 -30
- package/lib/drag-in-the-blank.js.map +1 -1
- package/lib/keyboard-coordinates.js +156 -0
- package/lib/keyboard-coordinates.js.map +1 -0
- package/package.json +5 -5
- package/src/__tests__/drag-in-the-blank.test.js +147 -2
- package/src/__tests__/keyboard-coordinates.test.js +202 -0
- package/src/choices/__tests__/index.test.js +104 -1
- package/src/choices/choice.jsx +16 -3
- package/src/choices/index.jsx +27 -3
- package/src/components/__tests__/blank.test.js +186 -5
- package/src/components/blank.jsx +108 -35
- package/src/drag-in-the-blank.jsx +132 -22
- package/src/keyboard-coordinates.js +147 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { defaultKeyboardCoordinateGetter, KeyboardCode } from '@dnd-kit/core';
|
|
2
|
+
|
|
3
|
+
// Matches the id `drag-in-the-blank-dp.jsx` registers for the choice board's droppable.
|
|
4
|
+
const CHOICE_BOARD_ID = 'drag-in-the-blank-droppable';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Custom keyboard coordinate getter for drag-in-the-blank's Tab-based placement.
|
|
8
|
+
*
|
|
9
|
+
* Tab/Shift+Tab cycle the dragged item directly onto the next/previous enabled
|
|
10
|
+
* droppable — a blank (`mask-blank-drop-{id}`), sorted by on-screen position
|
|
11
|
+
* (top-to-bottom, then left-to-right, since blanks sit inline in flowing text rather
|
|
12
|
+
* than a simple list) — followed by the choice board as a fixed last stop (see below
|
|
13
|
+
* for why the board isn't sorted alongside the blanks).
|
|
14
|
+
*
|
|
15
|
+
* Arrow keys are delegated to dnd-kit's own `defaultKeyboardCoordinateGetter`, leaving
|
|
16
|
+
* the existing free-form arrow-key dragging behavior completely unchanged.
|
|
17
|
+
*/
|
|
18
|
+
export const closestDroppableKeyboardCoordinates = (event, { active, context, currentCoordinates }) => {
|
|
19
|
+
const { code } = event;
|
|
20
|
+
const isTab = code === KeyboardCode.Tab;
|
|
21
|
+
const isArrow =
|
|
22
|
+
code === KeyboardCode.Down || code === KeyboardCode.Up || code === KeyboardCode.Left || code === KeyboardCode.Right;
|
|
23
|
+
|
|
24
|
+
if (!isTab && !isArrow) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (isArrow) {
|
|
29
|
+
return defaultKeyboardCoordinateGetter(event, { context, currentCoordinates });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
event.preventDefault();
|
|
33
|
+
|
|
34
|
+
const { droppableRects, droppableContainers, collisionRect } = context;
|
|
35
|
+
|
|
36
|
+
if (!droppableRects || droppableRects.size === 0) {
|
|
37
|
+
return currentCoordinates;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// A blank is itself a droppable for its own slot ("mask-blank-drop-{id}"), and
|
|
41
|
+
// simultaneously draggable from that same slot — the same shape as match-list's
|
|
42
|
+
// placed "target" tiles. That self drop-zone must never be treated as a navigable
|
|
43
|
+
// target: exclude it outright rather than relying on a distance threshold.
|
|
44
|
+
const draggedData = active?.data?.current;
|
|
45
|
+
const ownDropId =
|
|
46
|
+
draggedData && draggedData.fromChoice === false && draggedData.id != null
|
|
47
|
+
? `mask-blank-drop-${draggedData.id}`
|
|
48
|
+
: undefined;
|
|
49
|
+
|
|
50
|
+
const targets = [];
|
|
51
|
+
let boardTarget;
|
|
52
|
+
|
|
53
|
+
for (const [id, container] of droppableContainers) {
|
|
54
|
+
if (container?.disabled) continue;
|
|
55
|
+
if (id === ownDropId) continue;
|
|
56
|
+
|
|
57
|
+
const rect = droppableRects.get(id);
|
|
58
|
+
if (!rect) continue;
|
|
59
|
+
|
|
60
|
+
const center = {
|
|
61
|
+
x: rect.left + rect.width / 2,
|
|
62
|
+
y: rect.top + rect.height / 2,
|
|
63
|
+
};
|
|
64
|
+
// Land the dragged item's own top-left corner at the target's center-left point,
|
|
65
|
+
// rather than at the target's own top-left corner (avoids overshooting into a
|
|
66
|
+
// neighboring droppable when the target is much wider than the dragged item).
|
|
67
|
+
const dropPosition = {
|
|
68
|
+
x: rect.left,
|
|
69
|
+
y: rect.top + rect.height / 2,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const target = { id, rect, dropPosition, center };
|
|
73
|
+
|
|
74
|
+
if (id === CHOICE_BOARD_ID) {
|
|
75
|
+
boardTarget = target;
|
|
76
|
+
} else {
|
|
77
|
+
targets.push(target);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (targets.length === 0 && !boardTarget) {
|
|
82
|
+
return currentCoordinates;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const reverse = event.shiftKey;
|
|
86
|
+
|
|
87
|
+
targets.sort((a, b) => {
|
|
88
|
+
if (Math.abs(a.center.y - b.center.y) > 10) return a.center.y - b.center.y;
|
|
89
|
+
return a.center.x - b.center.x;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// The choice board can be an arbitrarily large container — e.g. several choices
|
|
93
|
+
// stacked beside the markup (`choicePosition: 'right'`/`'left'`) — so its own center
|
|
94
|
+
// can land anywhere relative to the blanks it sits beside, including numerically
|
|
95
|
+
// between two of them. Sorting it by position the same way as a blank would then
|
|
96
|
+
// interleave it into the middle of the cycle, making a single Tab press jump over
|
|
97
|
+
// more than one blank (confirmed live: with 3 stacked choices beside wrapped text,
|
|
98
|
+
// the board's center fell between blank 2 and blank 3, so the very first Tab press
|
|
99
|
+
// from the pool landed on blank 3, skipping blanks 1 and 2 entirely). Anchor it as a
|
|
100
|
+
// fixed stop after every blank, in every layout, instead of trusting its own bounds.
|
|
101
|
+
if (boardTarget) {
|
|
102
|
+
targets.push(boardTarget);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Find the current target: whichever target's rect actually contains the dragged
|
|
106
|
+
// item's own center. This holds regardless of how the dragged item's size compares
|
|
107
|
+
// to the target's — a compact chip landed (per `dropPosition` above) at a much wider
|
|
108
|
+
// target's left edge is still contained within that target's rect. Comparing
|
|
109
|
+
// distances between reconstructed centers instead breaks down for exactly that shape
|
|
110
|
+
// (a small item in a much wider target): the reconstructed center can end up closer
|
|
111
|
+
// to a completely different droppable than to the one the item is actually sitting
|
|
112
|
+
// on, so the *next* press re-matches the wrong target and looks like it does nothing.
|
|
113
|
+
const draggedCenter = collisionRect
|
|
114
|
+
? { x: collisionRect.left + collisionRect.width / 2, y: collisionRect.top + collisionRect.height / 2 }
|
|
115
|
+
: currentCoordinates;
|
|
116
|
+
|
|
117
|
+
let currentIndex = targets.findIndex(
|
|
118
|
+
(t) =>
|
|
119
|
+
draggedCenter.x >= t.rect.left &&
|
|
120
|
+
draggedCenter.x <= t.rect.right &&
|
|
121
|
+
draggedCenter.y >= t.rect.top &&
|
|
122
|
+
draggedCenter.y <= t.rect.bottom,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
// Fall back to nearest-by-dropPosition if the dragged item's center isn't strictly
|
|
126
|
+
// inside any target (e.g. mid-flight after a free arrow-key move).
|
|
127
|
+
if (currentIndex === -1) {
|
|
128
|
+
let minDist = Infinity;
|
|
129
|
+
|
|
130
|
+
for (let i = 0; i < targets.length; i++) {
|
|
131
|
+
const dist = distance(currentCoordinates, targets[i].dropPosition);
|
|
132
|
+
|
|
133
|
+
if (dist < minDist) {
|
|
134
|
+
minDist = dist;
|
|
135
|
+
currentIndex = i;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const nextIndex = reverse
|
|
141
|
+
? (currentIndex - 1 + targets.length) % targets.length
|
|
142
|
+
: (currentIndex + 1) % targets.length;
|
|
143
|
+
|
|
144
|
+
return targets[nextIndex].dropPosition;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const distance = (a, b) => Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
|