@pie-lib/mask-markup 3.1.5 → 3.1.6-beta.10

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.
Files changed (41) hide show
  1. package/CHANGELOG.md +0 -105
  2. package/lib/__tests__/drag-in-the-blank.test.js +129 -0
  3. package/lib/__tests__/index.test.js +39 -0
  4. package/lib/__tests__/keyboard-coordinates.test.js +449 -0
  5. package/lib/__tests__/mask.test.js +344 -0
  6. package/lib/__tests__/serialization.test.js +44 -0
  7. package/lib/__tests__/utils.js +14 -0
  8. package/lib/__tests__/with-mask.test.js +110 -0
  9. package/lib/choices/__tests__/index.test.js +139 -0
  10. package/lib/choices/choice.js +6 -24
  11. package/lib/choices/choice.js.map +1 -1
  12. package/lib/choices/index.js +9 -32
  13. package/lib/choices/index.js.map +1 -1
  14. package/lib/components/__tests__/blank.test.js +293 -0
  15. package/lib/components/__tests__/correct-input.test.js +132 -0
  16. package/lib/components/__tests__/dropdown.test.js +202 -0
  17. package/lib/components/__tests__/input.test.js +129 -0
  18. package/lib/components/blank.js +29 -106
  19. package/lib/components/blank.js.map +1 -1
  20. package/lib/components/dropdown.js +9 -6
  21. package/lib/components/dropdown.js.map +1 -1
  22. package/lib/drag-in-the-blank.js +30 -139
  23. package/lib/drag-in-the-blank.js.map +1 -1
  24. package/lib/keyboard-coordinates.js +1 -1
  25. package/lib/mask.js +15 -20
  26. package/lib/mask.js.map +1 -1
  27. package/package.json +6 -6
  28. package/src/__tests__/drag-in-the-blank.test.js +2 -147
  29. package/src/__tests__/mask.test.js +0 -98
  30. package/src/choices/__tests__/index.test.js +1 -104
  31. package/src/choices/choice.jsx +3 -16
  32. package/src/choices/index.jsx +3 -27
  33. package/src/components/__tests__/blank.test.js +5 -186
  34. package/src/components/blank.jsx +35 -108
  35. package/src/components/dropdown.jsx +9 -7
  36. package/src/drag-in-the-blank.jsx +22 -132
  37. package/src/mask.jsx +11 -13
  38. package/LICENSE.md +0 -5
  39. package/lib/keyboard-coordinates.js.map +0 -1
  40. package/src/__tests__/keyboard-coordinates.test.js +0 -202
  41. package/src/keyboard-coordinates.js +0 -147
@@ -6,13 +6,6 @@ import Choices from './choices';
6
6
  import Choice from './choices/choice';
7
7
  import Blank from './components/blank';
8
8
  import { withMask } from './with-mask';
9
- import { closestDroppableKeyboardCoordinates } from './keyboard-coordinates';
10
-
11
- // A click that lands right after a real drag gesture ends (pointer drag-and-drop, or
12
- // the browser's own synthetic click for a keyboard Space/Enter) must be ignored by the
13
- // click-to-select/click-to-place handlers below, or it would immediately reopen or
14
- // re-trigger a selection for a drag that just completed.
15
- const CLICK_AFTER_DRAG_GUARD_MS = 250;
16
9
 
17
10
  const Masked = withMask('blank', (props) => (node, data, onChange) => {
18
11
  const dataset = node.data?.dataset || {};
@@ -28,9 +21,6 @@ const Masked = withMask('blank', (props) => (node, data, onChange) => {
28
21
  emptyResponseAreaHeight,
29
22
  instanceId,
30
23
  isDragging,
31
- selectedItem,
32
- onSelectClick,
33
- onPlacementClick,
34
24
  } = props;
35
25
  const choiceId = showCorrectAnswer ? correctResponse[dataset.id] : data[dataset.id];
36
26
  // eslint-disable-next-line react/prop-types
@@ -57,9 +47,6 @@ const Masked = withMask('blank', (props) => (node, data, onChange) => {
57
47
  }}
58
48
  instanceId={instanceId}
59
49
  isDragging={isDragging}
60
- selectedItem={selectedItem}
61
- onSelectClick={onSelectClick}
62
- onPlacementClick={onPlacementClick}
63
50
  />
64
51
  );
65
52
  }
@@ -71,9 +58,7 @@ export default class DragInTheBlank extends React.Component {
71
58
  this.state = {
72
59
  activeDragItem: null,
73
60
  dropAnimation: undefined,
74
- selectedItem: null,
75
61
  };
76
- this.lastDragEndAt = 0;
77
62
  }
78
63
 
79
64
  static propTypes = {
@@ -104,7 +89,6 @@ export default class DragInTheBlank extends React.Component {
104
89
  this.setState({
105
90
  activeDragItem: active.data.current,
106
91
  dropAnimation: undefined, // default during drag
107
- selectedItem: active.data.current,
108
92
  });
109
93
  }
110
94
  };
@@ -126,40 +110,9 @@ export default class DragInTheBlank extends React.Component {
126
110
  return null;
127
111
  };
128
112
 
129
- // Shared placement logic for both the drag-end path and the click-to-place path, so
130
- // neither reimplements the other's mutation rules.
131
- //
132
- // `targetId === undefined` means "the choice board" — placing there removes the item
133
- // from wherever it currently is (mirroring image-cloze-association's
134
- // `containerIndex === undefined` convention for its own choices pool).
135
- commitPlacement = (draggedItem, targetId) => {
136
- const { onChange, value } = this.props;
137
-
138
- if (!onChange) return;
139
-
140
- if (targetId === undefined) {
141
- if (!draggedItem.fromChoice && draggedItem.id) {
142
- const newValue = { ...value };
143
- delete newValue[draggedItem.id];
144
- onChange(newValue);
145
- }
146
- return;
147
- }
148
-
149
- if (draggedItem.fromChoice === true) {
150
- const newValue = { ...value };
151
- newValue[targetId] = draggedItem.choice.id;
152
- onChange(newValue);
153
- } else if (draggedItem.id && draggedItem.id !== targetId) {
154
- const newValue = { ...value };
155
- newValue[targetId] = draggedItem.choice.id;
156
- delete newValue[draggedItem.id];
157
- onChange(newValue);
158
- }
159
- };
160
-
161
113
  handleDragEnd = (event) => {
162
114
  const { active, over } = event;
115
+ const { onChange, value } = this.props;
163
116
 
164
117
  const draggedData = active?.data?.current;
165
118
  const dropData = over?.data?.current;
@@ -173,83 +126,29 @@ export default class DragInTheBlank extends React.Component {
173
126
  dropAnimation: isValidDrop ? null : undefined,
174
127
  });
175
128
 
176
- this.cancelSelection();
177
- this.lastDragEndAt = Date.now();
178
-
179
- if (!isValidDrop) return;
180
-
181
- const targetId = dropData.toChoiceBoard === true ? undefined : dropData.id;
182
-
183
- this.commitPlacement(draggedData, targetId);
184
- };
185
-
186
- onDragCancel = () => {
187
- this.setState({ activeDragItem: null, dropAnimation: undefined });
188
- this.cancelSelection();
189
- this.lastDragEndAt = Date.now();
190
- };
191
-
192
- isSameItem = (a, b) => {
193
- if (!a || !b || a.fromChoice !== b.fromChoice) return false;
194
- return a.fromChoice ? a.choice.id === b.choice.id : a.id === b.id;
195
- };
196
-
197
- // Click-to-select semantics: selecting the currently-selected item again clears the
198
- // selection instead of re-selecting it.
199
- toggleItemSelection = (data) => {
200
- this.setState((state) => ({
201
- selectedItem: this.isSameItem(state.selectedItem, data) ? null : data,
202
- }));
203
- };
204
-
205
- cancelSelection = () => {
206
- this.setState({ selectedItem: null });
207
- };
208
-
209
- // If a real dnd-kit drag (started via keyboard Space/Enter) is still live when a
210
- // click completes the placement below, it needs to be cleanly ended — otherwise
211
- // dnd-kit would still think a drag is in progress. Escape is already configured as
212
- // this sensor's cancel key (see the keyboardCodes passed to DragProvider below), and
213
- // dispatching it as a real DOM KeyboardEvent is how dnd-kit's own document-level
214
- // listener is reached from outside its sensor.
215
- //
216
- // Only dispatch when a drag is actually live — this is a synthetic Escape keydown on
217
- // `document`, so an unconditional dispatch would also be observed by any other
218
- // document-level Escape listener (host player modals/dialogs, or another mounted
219
- // instance of this same component) even when nothing here actually needed cancelling.
220
- endAnyLiveKeyboardDrag = () => {
221
- if (!this.state.activeDragItem) return;
222
-
223
- document.dispatchEvent(new KeyboardEvent('keydown', { code: 'Escape', bubbles: true, cancelable: true }));
224
- };
225
-
226
- placeSelectedItem = (targetId) => {
227
- const { selectedItem } = this.state;
228
-
229
- if (!selectedItem) return;
230
-
231
- this.commitPlacement(selectedItem, targetId);
232
- this.cancelSelection();
233
- this.endAnyLiveKeyboardDrag();
234
- };
235
-
236
- isClickSoonAfterDragEnd = () => Date.now() - this.lastDragEndAt < CLICK_AFTER_DRAG_GUARD_MS;
237
-
238
- onItemClick = (data) => {
239
- if (this.isClickSoonAfterDragEnd()) return;
240
-
241
- // End any still-live keyboard drag BEFORE toggling the new selection: ending it
242
- // also cancels the current selection as a side effect (see onDragCancel above), so
243
- // doing it before — not after — lets this click's own selection be the one that
244
- // sticks.
245
- this.endAnyLiveKeyboardDrag();
246
- this.toggleItemSelection(data);
247
- };
129
+ if (!isValidDrop || !onChange) return;
248
130
 
249
- onPlacementClick = (targetId) => {
250
- if (this.isClickSoonAfterDragEnd()) return;
131
+ const draggedItem = draggedData;
132
+ const targetId = dropData.id;
251
133
 
252
- this.placeSelectedItem(targetId);
134
+ if (dropData.toChoiceBoard === true) {
135
+ if (!draggedItem.fromChoice && draggedItem.id) {
136
+ const newValue = { ...value };
137
+ delete newValue[draggedItem.id];
138
+ onChange(newValue);
139
+ }
140
+ } else if (draggedItem.fromChoice === true) {
141
+ if (targetId && targetId !== 'drag-in-the-blank-droppable') {
142
+ const newValue = { ...value };
143
+ newValue[targetId] = draggedItem.choice.id;
144
+ onChange(newValue);
145
+ }
146
+ } else if (draggedItem.id && draggedItem.id !== targetId) {
147
+ const newValue = { ...value };
148
+ newValue[targetId] = draggedItem.choice.id;
149
+ delete newValue[draggedItem.id];
150
+ onChange(newValue);
151
+ }
253
152
  };
254
153
 
255
154
  getPositionDirection = (choicePosition) => {
@@ -304,10 +203,7 @@ export default class DragInTheBlank extends React.Component {
304
203
  <DragProvider
305
204
  onDragStart={this.handleDragStart}
306
205
  onDragEnd={this.handleDragEnd}
307
- onDragCancel={this.onDragCancel}
308
206
  collisionDetection={rectIntersection}
309
- keyboardCoordinateGetter={closestDroppableKeyboardCoordinates}
310
- keyboardCodes={{ start: ['Space', 'Enter'], cancel: ['Escape'], end: ['Space', 'Enter'] }}
311
207
  >
312
208
  <div ref={(ref) => (this.rootRef = ref)} style={style}>
313
209
  <Choices
@@ -317,9 +213,6 @@ export default class DragInTheBlank extends React.Component {
317
213
  duplicates={duplicates}
318
214
  disabled={disabled}
319
215
  instanceId={instanceId}
320
- selectedItem={this.state.selectedItem}
321
- onSelectClick={this.onItemClick}
322
- onPlacementClick={this.onPlacementClick}
323
216
  />
324
217
  <Masked
325
218
  elementType="drag-in-the-blank"
@@ -337,9 +230,6 @@ export default class DragInTheBlank extends React.Component {
337
230
  emptyResponseAreaHeight={emptyResponseAreaHeight}
338
231
  instanceId={instanceId}
339
232
  isDragging={!!this.state.activeDragItem}
340
- selectedItem={this.state.selectedItem}
341
- onSelectClick={this.onItemClick}
342
- onPlacementClick={this.onPlacementClick}
343
233
  />
344
234
  <DragOverlay style={{ pointerEvents: 'none' }} dropAnimation={this.state.dropAnimation}>
345
235
  {this.renderDragOverlay()}
package/src/mask.jsx CHANGED
@@ -32,7 +32,7 @@ const getMark = (n) => {
32
32
  const mark = n.leaves.find((leave) => get(leave, 'marks', []).length);
33
33
 
34
34
  if (mark) {
35
- return mark.marks;
35
+ return mark.marks[0];
36
36
  }
37
37
 
38
38
  return null;
@@ -83,20 +83,18 @@ export const renderChildren = (layout, value, onChange, rootRenderChildren, pare
83
83
  const extraText = addText(parentNode, t);
84
84
  return extraText ? acc + extraText : acc;
85
85
  }, '');
86
- const marks = getMark(n);
86
+ const mark = getMark(n);
87
87
 
88
- if (marks?.length > 0) {
89
- const tagsToWrap = marks
90
- .map((mark) => Object.keys(MARK_TAGS).find((markKey) => MARK_TAGS[markKey] === mark.type))
91
- .filter(Boolean);
88
+ if (mark) {
89
+ let markKey;
92
90
 
93
- if (tagsToWrap.length > 0) {
94
- // nest from the inside out so the first mark ends up as the outermost tag: <b><em>content</em></b>
95
- children.push(
96
- tagsToWrap.reduceRight((acc, Tag, tagIndex) => <Tag key={`${key}-${Tag}-${tagIndex}`}>{acc}</Tag>, content),
97
- );
98
- } else {
99
- children.push(content);
91
+ for (markKey in MARK_TAGS) {
92
+ if (MARK_TAGS[markKey] === mark.type) {
93
+ const Tag = markKey;
94
+
95
+ children.push(<Tag key={key}>{content}</Tag>);
96
+ break;
97
+ }
100
98
  }
101
99
  } else if (content.length > 0) {
102
100
  children.push(content);
package/LICENSE.md DELETED
@@ -1,5 +0,0 @@
1
- Copyright 2019 CoreSpring Inc
2
-
3
- Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4
-
5
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -1 +0,0 @@
1
- {"version":3,"file":"keyboard-coordinates.js","names":["_core","require","_createForOfIteratorHelper","r","e","t","Symbol","iterator","Array","isArray","_unsupportedIterableToArray","length","_n","F","s","n","done","value","f","TypeError","o","a","u","call","next","_arrayLikeToArray","toString","slice","constructor","name","from","test","CHOICE_BOARD_ID","closestDroppableKeyboardCoordinates","exports","event","_ref","_active$data","active","context","currentCoordinates","code","isTab","KeyboardCode","Tab","isArrow","Down","Up","Left","Right","undefined","defaultKeyboardCoordinateGetter","preventDefault","droppableRects","droppableContainers","collisionRect","size","draggedData","data","current","ownDropId","fromChoice","id","concat","targets","boardTarget","_iterator","_step","_step$value","_slicedToArray2","container","disabled","rect","get","center","x","left","width","y","top","height","dropPosition","target","push","err","reverse","shiftKey","sort","b","Math","abs","draggedCenter","currentIndex","findIndex","right","bottom","minDist","Infinity","i","dist","distance","nextIndex","sqrt","pow"],"sources":["../src/keyboard-coordinates.js"],"sourcesContent":["import { defaultKeyboardCoordinateGetter, KeyboardCode } from '@dnd-kit/core';\n\n// Matches the id `drag-in-the-blank-dp.jsx` registers for the choice board's droppable.\nconst CHOICE_BOARD_ID = 'drag-in-the-blank-droppable';\n\n/**\n * Custom keyboard coordinate getter for drag-in-the-blank's Tab-based placement.\n *\n * Tab/Shift+Tab cycle the dragged item directly onto the next/previous enabled\n * droppable — a blank (`mask-blank-drop-{id}`), sorted by on-screen position\n * (top-to-bottom, then left-to-right, since blanks sit inline in flowing text rather\n * than a simple list) — followed by the choice board as a fixed last stop (see below\n * for why the board isn't sorted alongside the blanks).\n *\n * Arrow keys are delegated to dnd-kit's own `defaultKeyboardCoordinateGetter`, leaving\n * the existing free-form arrow-key dragging behavior completely unchanged.\n */\nexport const closestDroppableKeyboardCoordinates = (event, { active, context, currentCoordinates }) => {\n const { code } = event;\n const isTab = code === KeyboardCode.Tab;\n const isArrow =\n code === KeyboardCode.Down || code === KeyboardCode.Up || code === KeyboardCode.Left || code === KeyboardCode.Right;\n\n if (!isTab && !isArrow) {\n return undefined;\n }\n\n if (isArrow) {\n return defaultKeyboardCoordinateGetter(event, { context, currentCoordinates });\n }\n\n event.preventDefault();\n\n const { droppableRects, droppableContainers, collisionRect } = context;\n\n if (!droppableRects || droppableRects.size === 0) {\n return currentCoordinates;\n }\n\n // A blank is itself a droppable for its own slot (\"mask-blank-drop-{id}\"), and\n // simultaneously draggable from that same slot — the same shape as match-list's\n // placed \"target\" tiles. That self drop-zone must never be treated as a navigable\n // target: exclude it outright rather than relying on a distance threshold.\n const draggedData = active?.data?.current;\n const ownDropId =\n draggedData && draggedData.fromChoice === false && draggedData.id != null\n ? `mask-blank-drop-${draggedData.id}`\n : undefined;\n\n const targets = [];\n let boardTarget;\n\n for (const [id, container] of droppableContainers) {\n if (container?.disabled) continue;\n if (id === ownDropId) continue;\n\n const rect = droppableRects.get(id);\n if (!rect) continue;\n\n const center = {\n x: rect.left + rect.width / 2,\n y: rect.top + rect.height / 2,\n };\n // Land the dragged item's own top-left corner at the target's center-left point,\n // rather than at the target's own top-left corner (avoids overshooting into a\n // neighboring droppable when the target is much wider than the dragged item).\n const dropPosition = {\n x: rect.left,\n y: rect.top + rect.height / 2,\n };\n\n const target = { id, rect, dropPosition, center };\n\n if (id === CHOICE_BOARD_ID) {\n boardTarget = target;\n } else {\n targets.push(target);\n }\n }\n\n if (targets.length === 0 && !boardTarget) {\n return currentCoordinates;\n }\n\n const reverse = event.shiftKey;\n\n targets.sort((a, b) => {\n if (Math.abs(a.center.y - b.center.y) > 10) return a.center.y - b.center.y;\n return a.center.x - b.center.x;\n });\n\n // The choice board can be an arbitrarily large container — e.g. several choices\n // stacked beside the markup (`choicePosition: 'right'`/`'left'`) — so its own center\n // can land anywhere relative to the blanks it sits beside, including numerically\n // between two of them. Sorting it by position the same way as a blank would then\n // interleave it into the middle of the cycle, making a single Tab press jump over\n // more than one blank (confirmed live: with 3 stacked choices beside wrapped text,\n // the board's center fell between blank 2 and blank 3, so the very first Tab press\n // from the pool landed on blank 3, skipping blanks 1 and 2 entirely). Anchor it as a\n // fixed stop after every blank, in every layout, instead of trusting its own bounds.\n if (boardTarget) {\n targets.push(boardTarget);\n }\n\n // Find the current target: whichever target's rect actually contains the dragged\n // item's own center. This holds regardless of how the dragged item's size compares\n // to the target's — a compact chip landed (per `dropPosition` above) at a much wider\n // target's left edge is still contained within that target's rect. Comparing\n // distances between reconstructed centers instead breaks down for exactly that shape\n // (a small item in a much wider target): the reconstructed center can end up closer\n // to a completely different droppable than to the one the item is actually sitting\n // on, so the *next* press re-matches the wrong target and looks like it does nothing.\n const draggedCenter = collisionRect\n ? { x: collisionRect.left + collisionRect.width / 2, y: collisionRect.top + collisionRect.height / 2 }\n : currentCoordinates;\n\n let currentIndex = targets.findIndex(\n (t) =>\n draggedCenter.x >= t.rect.left &&\n draggedCenter.x <= t.rect.right &&\n draggedCenter.y >= t.rect.top &&\n draggedCenter.y <= t.rect.bottom,\n );\n\n // Fall back to nearest-by-dropPosition if the dragged item's center isn't strictly\n // inside any target (e.g. mid-flight after a free arrow-key move).\n if (currentIndex === -1) {\n let minDist = Infinity;\n\n for (let i = 0; i < targets.length; i++) {\n const dist = distance(currentCoordinates, targets[i].dropPosition);\n\n if (dist < minDist) {\n minDist = dist;\n currentIndex = i;\n }\n }\n }\n\n const nextIndex = reverse\n ? (currentIndex - 1 + targets.length) % targets.length\n : (currentIndex + 1) % targets.length;\n\n return targets[nextIndex].dropPosition;\n};\n\nconst distance = (a, b) => Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) 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,GAAGG,IAAI;QACdP,YAAY,GAAGM,CAAC;MAClB;IACF;EACF;EAEA,IAAMG,SAAS,GAAGhB,OAAO,GACrB,CAACO,YAAY,GAAG,CAAC,GAAG5B,OAAO,CAACrD,MAAM,IAAIqD,OAAO,CAACrD,MAAM,GACpD,CAACiF,YAAY,GAAG,CAAC,IAAI5B,OAAO,CAACrD,MAAM;EAEvC,OAAOqD,OAAO,CAACqC,SAAS,CAAC,CAACpB,YAAY;AACxC,CAAC;AAED,IAAMmB,QAAQ,GAAG,SAAXA,QAAQA,CAAI/E,CAAC,EAAEmE,CAAC;EAAA,OAAKC,IAAI,CAACa,IAAI,CAACb,IAAA,CAAAc,GAAA,CAAClF,CAAC,CAACsD,CAAC,GAAGa,CAAC,CAACb,CAAC,EAAK,CAAC,IAAAc,IAAA,CAAAc,GAAA,CAAIlF,CAAC,CAACyD,CAAC,GAAGU,CAAC,CAACV,CAAC,EAAK,CAAC,EAAC;AAAA","ignoreList":[]}
@@ -1,202 +0,0 @@
1
- import { KeyboardCode } from '@dnd-kit/core';
2
- import { closestDroppableKeyboardCoordinates } from '../keyboard-coordinates';
3
-
4
- function rectsToContext(rects) {
5
- const droppableRects = new Map(Object.entries(rects));
6
- const droppableContainers = new Map(Object.keys(rects).map((id) => [id, { disabled: false }]));
7
-
8
- return { droppableRects, droppableContainers };
9
- }
10
-
11
- function makeEvent(code, shiftKey = false) {
12
- return { code, preventDefault: () => {}, shiftKey };
13
- }
14
-
15
- // Mirrors dnd-kit's own KeyboardSensor, which always derives collisionRect's top-left
16
- // from the dragged item's current on-screen position on every keydown — so a test's
17
- // collisionRect must track currentCoordinates the same way on every simulated press,
18
- // not stay fixed while currentCoordinates changes across multiple presses.
19
- function press(rects, currentCoordinates, itemSize, event, active) {
20
- const context = {
21
- ...rectsToContext(rects),
22
- collisionRect: { left: currentCoordinates.x, top: currentCoordinates.y, ...itemSize },
23
- };
24
-
25
- return closestDroppableKeyboardCoordinates(event, { active, context, currentCoordinates });
26
- }
27
-
28
- describe('closestDroppableKeyboardCoordinates', () => {
29
- describe('arrow keys', () => {
30
- it("nudges the dragged item by dnd-kit's own default step (25px), unchanged", () => {
31
- const context = { ...rectsToContext({}), collisionRect: { left: 0, top: 0, width: 100, height: 40 } };
32
- const currentCoordinates = { x: 10, y: 20 };
33
-
34
- expect(
35
- closestDroppableKeyboardCoordinates(makeEvent(KeyboardCode.Down), { context, currentCoordinates }),
36
- ).toEqual({ x: 10, y: 45 });
37
- expect(
38
- closestDroppableKeyboardCoordinates(makeEvent(KeyboardCode.Up), { context, currentCoordinates }),
39
- ).toEqual({ x: 10, y: -5 });
40
- expect(
41
- closestDroppableKeyboardCoordinates(makeEvent(KeyboardCode.Right), { context, currentCoordinates }),
42
- ).toEqual({ x: 35, y: 20 });
43
- expect(
44
- closestDroppableKeyboardCoordinates(makeEvent(KeyboardCode.Left), { context, currentCoordinates }),
45
- ).toEqual({ x: -15, y: 20 });
46
- });
47
- });
48
-
49
- describe('Tab / Shift+Tab', () => {
50
- const blank0Rect = { left: 0, top: 0, width: 120, height: 40, right: 120, bottom: 40 };
51
- const blank1Rect = { left: 0, top: 60, width: 120, height: 40, right: 120, bottom: 100 };
52
- const boardRect = { left: 0, top: 120, width: 400, height: 300, right: 400, bottom: 420 };
53
- const baseRects = {
54
- 'mask-blank-drop-0': blank0Rect,
55
- 'mask-blank-drop-1': blank1Rect,
56
- 'drag-in-the-blank-droppable': boardRect,
57
- };
58
-
59
- it('jumps to the next droppable in top-to-bottom order, landing at its center-left point', () => {
60
- const currentCoordinates = { x: blank0Rect.left, y: blank0Rect.top };
61
-
62
- const next = press(baseRects, currentCoordinates, { width: 120, height: 40 }, makeEvent('Tab'));
63
-
64
- expect(next).toEqual({ x: blank1Rect.left, y: blank1Rect.top + blank1Rect.height / 2 });
65
- });
66
-
67
- it('cycles backwards with Shift+Tab, wrapping to the choice board', () => {
68
- const currentCoordinates = { x: blank0Rect.left, y: blank0Rect.top };
69
-
70
- const next = press(baseRects, currentCoordinates, { width: 120, height: 40 }, makeEvent('Tab', true));
71
-
72
- expect(next).toEqual({ x: boardRect.left, y: boardRect.top + boardRect.height / 2 });
73
- });
74
-
75
- it("excludes a dragged blank's own drop-zone from the cycle", () => {
76
- // A blank is simultaneously draggable and droppable for its own slot (the same
77
- // shape as match-list's placed "target" tiles). Without excluding it, containment
78
- // would match the blank to itself and cycle relative to itself instead of its
79
- // real neighbors — this test is constructed so the two behaviors give different,
80
- // distinguishable results (not just coincidentally the same answer).
81
- const blankARect = { left: 0, top: 0, width: 120, height: 40, right: 120, bottom: 40 };
82
- const blankBRect = { left: 0, top: 60, width: 120, height: 40, right: 120, bottom: 100 };
83
- const blankCRect = { left: 0, top: 120, width: 120, height: 40, right: 120, bottom: 160 };
84
- const rects = {
85
- 'mask-blank-drop-a': blankARect,
86
- 'mask-blank-drop-b': blankBRect,
87
- 'mask-blank-drop-c': blankCRect,
88
- };
89
- const active = { data: { current: { id: 'b', fromChoice: false, type: 'MaskBlank' } } };
90
- // Sitting exactly at blank B's own position.
91
- const currentCoordinates = { x: blankBRect.left, y: blankBRect.top };
92
-
93
- const next = press(rects, currentCoordinates, { width: 120, height: 40 }, makeEvent('Tab', true), active);
94
-
95
- // With B's own slot excluded, Shift+Tab from B's position falls back to
96
- // nearest-by-dropPosition among the remaining targets (A and C) — A is closer to
97
- // B's actual position, making A the resolved "current", so reverse from there
98
- // lands on C. Without the exclusion, containment would match B to itself
99
- // directly (index 1 of [A, B, C] sorted by position), landing on A instead.
100
- expect(next).toEqual({ x: blankCRect.left, y: blankCRect.top + blankCRect.height / 2 });
101
- });
102
-
103
- it('keeps advancing on repeated Shift+Tab when the choice board is much wider than the dragged item', () => {
104
- // Regression case for the bug already found and fixed in match-list/image-cloze-
105
- // association: matching the "current" target by reconstructing a center from the
106
- // dragged item's own small size (instead of checking which target's rect actually
107
- // contains it) makes the *next* press re-match a completely different droppable
108
- // once the item is actually sitting on a wide target, so it looks like the press
109
- // does nothing.
110
- const wideBlankRect = { left: 0, top: 0, width: 900, height: 40, right: 900, bottom: 40 };
111
- const rects = { 'mask-blank-drop-0': wideBlankRect, 'drag-in-the-blank-droppable': boardRect };
112
- const itemSize = { width: 100, height: 40 };
113
-
114
- const afterFirst = press(rects, { x: boardRect.left, y: boardRect.top }, itemSize, makeEvent('Tab', true));
115
-
116
- expect(afterFirst).toEqual({ x: 0, y: 20 }); // wide blank's center-left point
117
-
118
- const afterSecond = press(rects, afterFirst, itemSize, makeEvent('Tab', true));
119
-
120
- expect(afterSecond).toEqual({ x: boardRect.left, y: boardRect.top + boardRect.height / 2 });
121
- });
122
-
123
- it('correctly identifies the choice board as the current target when picking up an item positioned near its edge, close to a small neighboring blank', () => {
124
- // Regression case for the OTHER failure mode: matching "current target" purely by
125
- // nearest-dropPosition (instead of containment first) can misidentify a large
126
- // droppable as some small, unrelated nearby one, because a large droppable's
127
- // dropPosition is anchored at its own vertical middle.
128
- const tallBoardRect = { left: 0, top: 0, width: 400, height: 600, right: 400, bottom: 600 };
129
- const smallBlankRect = { left: 0, top: -60, width: 100, height: 40, right: 100, bottom: -20 };
130
- const rects = { 'mask-blank-drop-0': smallBlankRect, 'drag-in-the-blank-droppable': tallBoardRect };
131
- const itemSize = { width: 100, height: 40 };
132
-
133
- const currentCoordinates = { x: 0, y: 10 };
134
-
135
- const next = press(rects, currentCoordinates, itemSize, makeEvent('Tab'));
136
-
137
- expect(next).toEqual({ x: smallBlankRect.left, y: smallBlankRect.top + smallBlankRect.height / 2 });
138
- });
139
-
140
- it('keeps the choice board as a fixed last stop instead of sorting it in by its own center, when the board spans multiple blank rows', () => {
141
- // Regression case for a real bug found via manual browser testing: with 3
142
- // choices stacked beside wrapped inline text (`choicePosition: 'right'`), the
143
- // tall choice board's own center fell numerically BETWEEN blank 2 and blank 3's
144
- // rows. Sorting it by position the same way as a blank interleaved it into the
145
- // middle of the cycle, so the very first Tab press after picking up a pool
146
- // choice jumped straight to blank 3, skipping blanks 1 and 2 entirely.
147
- const blank0Rect = { left: 0, top: 200, width: 50, height: 50, right: 50, bottom: 250 };
148
- const blank1Rect = { left: 0, top: 250, width: 50, height: 50, right: 50, bottom: 300 };
149
- const blank2Rect = { left: 0, top: 285, width: 50, height: 50, right: 50, bottom: 335 };
150
- // Spans all three choices stacked beside the text — its center (287.5) falls
151
- // between blank1's (275) and blank2's (310), more than 10px from either, so the
152
- // old position-based sort placed it between them.
153
- const tallBoardRect = { left: 400, top: 195, width: 80, height: 185, right: 480, bottom: 380 };
154
- const rects = {
155
- 'mask-blank-drop-0': blank0Rect,
156
- 'mask-blank-drop-1': blank1Rect,
157
- 'mask-blank-drop-2': blank2Rect,
158
- 'drag-in-the-blank-droppable': tallBoardRect,
159
- };
160
- const active = { data: { current: { fromChoice: true, choice: { id: 'x' }, type: 'MaskBlank' } } };
161
- const itemSize = { width: 48, height: 50 };
162
- // Picking up a choice from within the board — its own current position, before
163
- // any movement, sits inside the board's rect.
164
- const currentCoordinates = { x: 415, y: 205 };
165
-
166
- const next = press(rects, currentCoordinates, itemSize, makeEvent('Tab'), active);
167
-
168
- expect(next).toEqual({ x: blank0Rect.left, y: blank0Rect.top + blank0Rect.height / 2 });
169
- });
170
-
171
- it('cycles through every blank in position order, then the board, then wraps — even when the board would otherwise sort into the middle', () => {
172
- const blank0Rect = { left: 0, top: 200, width: 50, height: 50, right: 50, bottom: 250 };
173
- const blank1Rect = { left: 0, top: 250, width: 50, height: 50, right: 50, bottom: 300 };
174
- const blank2Rect = { left: 0, top: 285, width: 50, height: 50, right: 50, bottom: 335 };
175
- const tallBoardRect = { left: 400, top: 195, width: 80, height: 185, right: 480, bottom: 380 };
176
- const rects = {
177
- 'mask-blank-drop-0': blank0Rect,
178
- 'mask-blank-drop-1': blank1Rect,
179
- 'mask-blank-drop-2': blank2Rect,
180
- 'drag-in-the-blank-droppable': tallBoardRect,
181
- };
182
- const active = { data: { current: { fromChoice: true, choice: { id: 'x' }, type: 'MaskBlank' } } };
183
- const itemSize = { width: 48, height: 50 };
184
-
185
- let coords = { x: 415, y: 205 };
186
- const visited = [];
187
-
188
- for (let i = 0; i < 5; i++) {
189
- coords = press(rects, coords, itemSize, makeEvent('Tab'), active);
190
- visited.push(coords);
191
- }
192
-
193
- expect(visited).toEqual([
194
- { x: blank0Rect.left, y: blank0Rect.top + blank0Rect.height / 2 },
195
- { x: blank1Rect.left, y: blank1Rect.top + blank1Rect.height / 2 },
196
- { x: blank2Rect.left, y: blank2Rect.top + blank2Rect.height / 2 },
197
- { x: tallBoardRect.left, y: tallBoardRect.top + tallBoardRect.height / 2 },
198
- { x: blank0Rect.left, y: blank0Rect.top + blank0Rect.height / 2 }, // wraps
199
- ]);
200
- });
201
- });
202
- });
@@ -1,147 +0,0 @@
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);