@pie-lib/mask-markup 3.1.0-beta.6 → 3.1.0

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 (51) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/LICENSE.md +5 -0
  3. package/lib/choices/choice.js +24 -6
  4. package/lib/choices/choice.js.map +1 -1
  5. package/lib/choices/index.js +32 -9
  6. package/lib/choices/index.js.map +1 -1
  7. package/lib/components/blank.js +106 -29
  8. package/lib/components/blank.js.map +1 -1
  9. package/lib/components/dropdown.js +2 -9
  10. package/lib/components/dropdown.js.map +1 -1
  11. package/lib/drag-in-the-blank.js +139 -30
  12. package/lib/drag-in-the-blank.js.map +1 -1
  13. package/lib/keyboard-coordinates.js +156 -0
  14. package/lib/keyboard-coordinates.js.map +1 -0
  15. package/lib/mask.js +20 -15
  16. package/lib/mask.js.map +1 -1
  17. package/package.json +6 -6
  18. package/src/__tests__/drag-in-the-blank.test.js +147 -2
  19. package/src/__tests__/keyboard-coordinates.test.js +202 -0
  20. package/src/__tests__/mask.test.js +98 -0
  21. package/src/choices/__tests__/index.test.js +104 -1
  22. package/src/choices/choice.jsx +16 -3
  23. package/src/choices/index.jsx +27 -3
  24. package/src/components/__tests__/blank.test.js +186 -5
  25. package/src/components/blank.jsx +108 -35
  26. package/src/components/dropdown.jsx +2 -9
  27. package/src/drag-in-the-blank.jsx +132 -22
  28. package/src/keyboard-coordinates.js +147 -0
  29. package/src/mask.jsx +13 -11
  30. package/lib/__tests__/drag-in-the-blank.test.js +0 -129
  31. package/lib/__tests__/drag-in-the-blank.test.js.map +0 -1
  32. package/lib/__tests__/index.test.js +0 -39
  33. package/lib/__tests__/index.test.js.map +0 -1
  34. package/lib/__tests__/mask.test.js +0 -344
  35. package/lib/__tests__/mask.test.js.map +0 -1
  36. package/lib/__tests__/serialization.test.js +0 -44
  37. package/lib/__tests__/serialization.test.js.map +0 -1
  38. package/lib/__tests__/utils.js +0 -14
  39. package/lib/__tests__/utils.js.map +0 -1
  40. package/lib/__tests__/with-mask.test.js +0 -110
  41. package/lib/__tests__/with-mask.test.js.map +0 -1
  42. package/lib/choices/__tests__/index.test.js +0 -139
  43. package/lib/choices/__tests__/index.test.js.map +0 -1
  44. package/lib/components/__tests__/blank.test.js +0 -293
  45. package/lib/components/__tests__/blank.test.js.map +0 -1
  46. package/lib/components/__tests__/correct-input.test.js +0 -132
  47. package/lib/components/__tests__/correct-input.test.js.map +0 -1
  48. package/lib/components/__tests__/dropdown.test.js +0 -202
  49. package/lib/components/__tests__/dropdown.test.js.map +0 -1
  50. package/lib/components/__tests__/input.test.js +0 -129
  51. package/lib/components/__tests__/input.test.js.map +0 -1
@@ -6,6 +6,13 @@ 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;
9
16
 
10
17
  const Masked = withMask('blank', (props) => (node, data, onChange) => {
11
18
  const dataset = node.data?.dataset || {};
@@ -21,6 +28,9 @@ const Masked = withMask('blank', (props) => (node, data, onChange) => {
21
28
  emptyResponseAreaHeight,
22
29
  instanceId,
23
30
  isDragging,
31
+ selectedItem,
32
+ onSelectClick,
33
+ onPlacementClick,
24
34
  } = props;
25
35
  const choiceId = showCorrectAnswer ? correctResponse[dataset.id] : data[dataset.id];
26
36
  // eslint-disable-next-line react/prop-types
@@ -47,6 +57,9 @@ const Masked = withMask('blank', (props) => (node, data, onChange) => {
47
57
  }}
48
58
  instanceId={instanceId}
49
59
  isDragging={isDragging}
60
+ selectedItem={selectedItem}
61
+ onSelectClick={onSelectClick}
62
+ onPlacementClick={onPlacementClick}
50
63
  />
51
64
  );
52
65
  }
@@ -58,7 +71,9 @@ export default class DragInTheBlank extends React.Component {
58
71
  this.state = {
59
72
  activeDragItem: null,
60
73
  dropAnimation: undefined,
74
+ selectedItem: null,
61
75
  };
76
+ this.lastDragEndAt = 0;
62
77
  }
63
78
 
64
79
  static propTypes = {
@@ -89,6 +104,7 @@ export default class DragInTheBlank extends React.Component {
89
104
  this.setState({
90
105
  activeDragItem: active.data.current,
91
106
  dropAnimation: undefined, // default during drag
107
+ selectedItem: active.data.current,
92
108
  });
93
109
  }
94
110
  };
@@ -110,9 +126,40 @@ export default class DragInTheBlank extends React.Component {
110
126
  return null;
111
127
  };
112
128
 
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
+
113
161
  handleDragEnd = (event) => {
114
162
  const { active, over } = event;
115
- const { onChange, value } = this.props;
116
163
 
117
164
  const draggedData = active?.data?.current;
118
165
  const dropData = over?.data?.current;
@@ -126,29 +173,83 @@ export default class DragInTheBlank extends React.Component {
126
173
  dropAnimation: isValidDrop ? null : undefined,
127
174
  });
128
175
 
129
- if (!isValidDrop || !onChange) return;
176
+ this.cancelSelection();
177
+ this.lastDragEndAt = Date.now();
130
178
 
131
- const draggedItem = draggedData;
132
- const targetId = dropData.id;
179
+ if (!isValidDrop) return;
133
180
 
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
- }
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
+ };
248
+
249
+ onPlacementClick = (targetId) => {
250
+ if (this.isClickSoonAfterDragEnd()) return;
251
+
252
+ this.placeSelectedItem(targetId);
152
253
  };
153
254
 
154
255
  getPositionDirection = (choicePosition) => {
@@ -203,7 +304,10 @@ export default class DragInTheBlank extends React.Component {
203
304
  <DragProvider
204
305
  onDragStart={this.handleDragStart}
205
306
  onDragEnd={this.handleDragEnd}
307
+ onDragCancel={this.onDragCancel}
206
308
  collisionDetection={rectIntersection}
309
+ keyboardCoordinateGetter={closestDroppableKeyboardCoordinates}
310
+ keyboardCodes={{ start: ['Space', 'Enter'], cancel: ['Escape'], end: ['Space', 'Enter'] }}
207
311
  >
208
312
  <div ref={(ref) => (this.rootRef = ref)} style={style}>
209
313
  <Choices
@@ -213,6 +317,9 @@ export default class DragInTheBlank extends React.Component {
213
317
  duplicates={duplicates}
214
318
  disabled={disabled}
215
319
  instanceId={instanceId}
320
+ selectedItem={this.state.selectedItem}
321
+ onSelectClick={this.onItemClick}
322
+ onPlacementClick={this.onPlacementClick}
216
323
  />
217
324
  <Masked
218
325
  elementType="drag-in-the-blank"
@@ -230,6 +337,9 @@ export default class DragInTheBlank extends React.Component {
230
337
  emptyResponseAreaHeight={emptyResponseAreaHeight}
231
338
  instanceId={instanceId}
232
339
  isDragging={!!this.state.activeDragItem}
340
+ selectedItem={this.state.selectedItem}
341
+ onSelectClick={this.onItemClick}
342
+ onPlacementClick={this.onPlacementClick}
233
343
  />
234
344
  <DragOverlay style={{ pointerEvents: 'none' }} dropAnimation={this.state.dropAnimation}>
235
345
  {this.renderDragOverlay()}
@@ -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);
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[0];
35
+ return mark.marks;
36
36
  }
37
37
 
38
38
  return null;
@@ -83,18 +83,20 @@ 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 mark = getMark(n);
86
+ const marks = getMark(n);
87
87
 
88
- if (mark) {
89
- let markKey;
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);
90
92
 
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
- }
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);
98
100
  }
99
101
  } else if (content.length > 0) {
100
102
  children.push(content);
@@ -1,129 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- var _typeof = require("@babel/runtime/helpers/typeof");
5
- var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
6
- var React = _interopRequireWildcard(require("react"));
7
- var _react2 = require("@testing-library/react");
8
- var _dragInTheBlank = _interopRequireDefault(require("../drag-in-the-blank"));
9
- function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
10
- var markup = "<div>\n <img src=\"https://image.shutterstock.com/image-vector/cow-jumped-over-moon-traditional-260nw-1152899330.jpg\"></img>\n <h5>Hey Diddle Diddle <i>by ?</i></h5>\n <p>1: Hey, diddle, diddle,</p>\n <p>2: The cat and the fiddle,</p>\n <p>3: The cow {{0}} over the moon;</p>\n <p>4: The little dog {{1}},</p>\n <p>5: To see such sport,</p>\n <p>6: And the dish ran away with the {{2}}.</p>\n</div>";
11
- var choice = function choice(v, id) {
12
- return {
13
- value: v,
14
- id: id
15
- };
16
- };
17
-
18
- // Mock DragProvider and DragDroppablePlaceholder to avoid DndContext requirement
19
- jest.mock('@pie-lib/drag', function () {
20
- return {
21
- DragProvider: function DragProvider(_ref) {
22
- var children = _ref.children,
23
- onDragStart = _ref.onDragStart,
24
- onDragEnd = _ref.onDragEnd;
25
- // Simple wrapper that doesn't require DndContext
26
- return /*#__PURE__*/React.createElement("div", {
27
- "data-testid": "drag-provider"
28
- }, children);
29
- },
30
- DragDroppablePlaceholder: function DragDroppablePlaceholder(_ref2) {
31
- var children = _ref2.children,
32
- disabled = _ref2.disabled,
33
- instanceId = _ref2.instanceId;
34
- // Simple wrapper that doesn't require useDroppable
35
- return /*#__PURE__*/React.createElement("div", {
36
- "data-testid": "drag-droppable-placeholder"
37
- }, children);
38
- }
39
- };
40
- });
41
-
42
- // Mock @dnd-kit/core components and hooks used by DragInTheBlank and child components
43
- jest.mock('@dnd-kit/core', function () {
44
- return {
45
- DragOverlay: function DragOverlay(_ref3) {
46
- var children = _ref3.children;
47
- return /*#__PURE__*/React.createElement("div", {
48
- "data-testid": "drag-overlay"
49
- }, children);
50
- },
51
- closestCenter: jest.fn(),
52
- useDraggable: jest.fn(function () {
53
- return {
54
- attributes: {},
55
- listeners: {},
56
- setNodeRef: jest.fn(),
57
- transform: null,
58
- isDragging: false
59
- };
60
- }),
61
- useDroppable: jest.fn(function () {
62
- return {
63
- setNodeRef: jest.fn(),
64
- isOver: false,
65
- active: null
66
- };
67
- })
68
- };
69
- });
70
- jest.mock('@dnd-kit/utilities', function () {
71
- return {
72
- CSS: {
73
- Translate: {
74
- toString: jest.fn(function () {
75
- return 'translate3d(0, 0, 0)';
76
- })
77
- }
78
- }
79
- };
80
- });
81
- describe('DragInTheBlank', function () {
82
- var defaultProps = {
83
- disabled: false,
84
- feedback: {},
85
- markup: markup,
86
- choices: [choice('Jumped', '0'), choice('Laughed', '1'), choice('Spoon', '2'), choice('Fork', '3'), choice('Bumped', '4'), choice('Smiled', '5')],
87
- value: {
88
- 0: undefined
89
- }
90
- };
91
- describe('render', function () {
92
- it('renders correctly with default props', function () {
93
- var _render = (0, _react2.render)(/*#__PURE__*/React.createElement(_dragInTheBlank["default"], defaultProps)),
94
- container = _render.container;
95
- expect(container.firstChild).toBeInTheDocument();
96
- // Check that markup content is rendered
97
- expect(_react2.screen.getByText(/Hey Diddle Diddle/)).toBeInTheDocument();
98
- expect(_react2.screen.getByText(/Hey, diddle, diddle,/)).toBeInTheDocument();
99
- });
100
- it('renders correctly with disabled prop as true', function () {
101
- var _render2 = (0, _react2.render)(/*#__PURE__*/React.createElement(_dragInTheBlank["default"], (0, _extends2["default"])({}, defaultProps, {
102
- disabled: true
103
- }))),
104
- container = _render2.container;
105
- expect(container.firstChild).toBeInTheDocument();
106
- });
107
- it('renders correctly with feedback', function () {
108
- var _render3 = (0, _react2.render)(/*#__PURE__*/React.createElement(_dragInTheBlank["default"], (0, _extends2["default"])({}, defaultProps, {
109
- feedback: {
110
- 0: {
111
- value: 'Jumped',
112
- correct: 'Jumped'
113
- },
114
- 1: {
115
- value: 'Laughed',
116
- correct: 'Laughed'
117
- },
118
- 2: {
119
- value: 'Spoon',
120
- correct: 'Spoon'
121
- }
122
- }
123
- }))),
124
- container = _render3.container;
125
- expect(container.firstChild).toBeInTheDocument();
126
- });
127
- });
128
- });
129
- //# sourceMappingURL=drag-in-the-blank.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"drag-in-the-blank.test.js","names":["React","_interopRequireWildcard","require","_react2","_dragInTheBlank","_interopRequireDefault","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","_typeof","has","get","set","_t","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","markup","choice","v","id","value","jest","mock","DragProvider","_ref","children","onDragStart","onDragEnd","createElement","DragDroppablePlaceholder","_ref2","disabled","instanceId","DragOverlay","_ref3","closestCenter","fn","useDraggable","attributes","listeners","setNodeRef","transform","isDragging","useDroppable","isOver","active","CSS","Translate","toString","describe","defaultProps","feedback","choices","undefined","it","_render","render","container","expect","firstChild","toBeInTheDocument","screen","getByText","_render2","_extends2","_render3","correct"],"sources":["../../src/__tests__/drag-in-the-blank.test.js"],"sourcesContent":["import * as React from 'react';\nimport { render, screen } from '@testing-library/react';\nimport DragInTheBlank from '../drag-in-the-blank';\n\nconst markup = `<div>\n <img src=\"https://image.shutterstock.com/image-vector/cow-jumped-over-moon-traditional-260nw-1152899330.jpg\"></img>\n <h5>Hey Diddle Diddle <i>by ?</i></h5>\n <p>1: Hey, diddle, diddle,</p>\n <p>2: The cat and the fiddle,</p>\n <p>3: The cow {{0}} over the moon;</p>\n <p>4: The little dog {{1}},</p>\n <p>5: To see such sport,</p>\n <p>6: And the dish ran away with the {{2}}.</p>\n</div>`;\nconst choice = (v, id) => ({ value: v, id });\n\n// Mock DragProvider and DragDroppablePlaceholder to avoid DndContext requirement\njest.mock('@pie-lib/drag', () => ({\n DragProvider: ({ children, onDragStart, onDragEnd }) => {\n // Simple wrapper that doesn't require DndContext\n return <div data-testid=\"drag-provider\">{children}</div>;\n },\n DragDroppablePlaceholder: ({ children, disabled, instanceId }) => {\n // Simple wrapper that doesn't require useDroppable\n return <div data-testid=\"drag-droppable-placeholder\">{children}</div>;\n },\n}));\n\n// Mock @dnd-kit/core components and hooks used by DragInTheBlank and child components\njest.mock('@dnd-kit/core', () => ({\n DragOverlay: ({ children }) => <div data-testid=\"drag-overlay\">{children}</div>,\n closestCenter: jest.fn(),\n useDraggable: jest.fn(() => ({\n attributes: {},\n listeners: {},\n setNodeRef: jest.fn(),\n transform: null,\n isDragging: false,\n })),\n useDroppable: jest.fn(() => ({\n setNodeRef: jest.fn(),\n isOver: false,\n active: null,\n })),\n}));\n\njest.mock('@dnd-kit/utilities', () => ({\n CSS: {\n Translate: {\n toString: jest.fn(() => 'translate3d(0, 0, 0)'),\n },\n },\n}));\n\ndescribe('DragInTheBlank', () => {\n const defaultProps = {\n disabled: false,\n feedback: {},\n markup,\n choices: [\n choice('Jumped', '0'),\n choice('Laughed', '1'),\n choice('Spoon', '2'),\n choice('Fork', '3'),\n choice('Bumped', '4'),\n choice('Smiled', '5'),\n ],\n\n value: {\n 0: undefined,\n },\n };\n\n describe('render', () => {\n it('renders correctly with default props', () => {\n const { container } = render(<DragInTheBlank {...defaultProps} />);\n expect(container.firstChild).toBeInTheDocument();\n // Check that markup content is rendered\n expect(screen.getByText(/Hey Diddle Diddle/)).toBeInTheDocument();\n expect(screen.getByText(/Hey, diddle, diddle,/)).toBeInTheDocument();\n });\n\n it('renders correctly with disabled prop as true', () => {\n const { container } = render(<DragInTheBlank {...defaultProps} disabled={true} />);\n expect(container.firstChild).toBeInTheDocument();\n });\n\n it('renders correctly with feedback', () => {\n const { container } = render(\n <DragInTheBlank\n {...defaultProps}\n feedback={{\n 0: {\n value: 'Jumped',\n correct: 'Jumped',\n },\n 1: {\n value: 'Laughed',\n correct: 'Laughed',\n },\n 2: {\n value: 'Spoon',\n correct: 'Spoon',\n },\n }}\n />,\n );\n expect(container.firstChild).toBeInTheDocument();\n });\n });\n});\n"],"mappings":";;;;;AAAA,IAAAA,KAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,eAAA,GAAAC,sBAAA,CAAAH,OAAA;AAAkD,SAAAD,wBAAAK,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAP,uBAAA,YAAAA,wBAAAK,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,mBAAAT,CAAA,iBAAAA,CAAA,gBAAAU,OAAA,CAAAV,CAAA,0BAAAA,CAAA,SAAAQ,CAAA,MAAAF,CAAA,GAAAL,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAG,CAAA,CAAAK,GAAA,CAAAX,CAAA,UAAAM,CAAA,CAAAM,GAAA,CAAAZ,CAAA,GAAAM,CAAA,CAAAO,GAAA,CAAAb,CAAA,EAAAQ,CAAA,cAAAM,EAAA,IAAAd,CAAA,gBAAAc,EAAA,OAAAC,cAAA,CAAAC,IAAA,CAAAhB,CAAA,EAAAc,EAAA,OAAAP,CAAA,IAAAD,CAAA,GAAAW,MAAA,CAAAC,cAAA,KAAAD,MAAA,CAAAE,wBAAA,CAAAnB,CAAA,EAAAc,EAAA,OAAAP,CAAA,CAAAK,GAAA,IAAAL,CAAA,CAAAM,GAAA,IAAAP,CAAA,CAAAE,CAAA,EAAAM,EAAA,EAAAP,CAAA,IAAAC,CAAA,CAAAM,EAAA,IA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AAC;EACJ,CAAC,CAAC;AACJ,CAAC,CAAC","ignoreList":[]}
@@ -1,39 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- var _componentize = _interopRequireDefault(require("../componentize"));
5
- var _serialization = require("../serialization");
6
- describe('index', function () {
7
- describe('componentize', function () {
8
- it('should return an array with the appropriate markup', function () {
9
- var dropDownMarkup = (0, _componentize["default"])('{{0}} foo {{1}}', 'dropdown');
10
- expect(dropDownMarkup).toEqual({
11
- markup: '<span data-component="dropdown" data-id="0"></span> foo <span data-component="dropdown" data-id="1"></span>'
12
- });
13
- });
14
- });
15
- describe('serialization', function () {
16
- it('should have default node a span', function () {
17
- expect((0, _serialization.deserialize)('something')).toEqual(expect.objectContaining({
18
- object: 'value',
19
- document: {
20
- object: 'document',
21
- data: {},
22
- nodes: [{
23
- object: 'block',
24
- data: {},
25
- isVoid: false,
26
- type: 'span',
27
- nodes: [{
28
- object: 'text',
29
- leaves: [{
30
- text: 'something'
31
- }]
32
- }]
33
- }]
34
- }
35
- }));
36
- });
37
- });
38
- });
39
- //# sourceMappingURL=index.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.test.js","names":["_componentize","_interopRequireDefault","require","_serialization","describe","it","dropDownMarkup","componentize","expect","toEqual","markup","deserialize","objectContaining","object","document","data","nodes","isVoid","type","leaves","text"],"sources":["../../src/__tests__/index.test.js"],"sourcesContent":["import componentize from '../componentize';\nimport { deserialize } from '../serialization';\n\ndescribe('index', () => {\n describe('componentize', () => {\n it('should return an array with the appropriate markup', () => {\n const dropDownMarkup = componentize('{{0}} foo {{1}}', 'dropdown');\n\n expect(dropDownMarkup).toEqual({\n markup:\n '<span data-component=\"dropdown\" data-id=\"0\"></span> foo <span data-component=\"dropdown\" data-id=\"1\"></span>',\n });\n });\n });\n\n describe('serialization', () => {\n it('should have default node a span', () => {\n expect(deserialize('something')).toEqual(\n expect.objectContaining({\n object: 'value',\n document: {\n object: 'document',\n data: {},\n nodes: [\n {\n object: 'block',\n data: {},\n isVoid: false,\n type: 'span',\n nodes: [{ object: 'text', leaves: [{ text: 'something' }] }],\n },\n ],\n },\n }),\n );\n });\n });\n});\n"],"mappings":";;;AAAA,IAAAA,aAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,cAAA,GAAAD,OAAA;AAEAE,QAAQ,CAAC,OAAO,EAAE,YAAM;EACtBA,QAAQ,CAAC,cAAc,EAAE,YAAM;IAC7BC,EAAE,CAAC,oDAAoD,EAAE,YAAM;MAC7D,IAAMC,cAAc,GAAG,IAAAC,wBAAY,EAAC,iBAAiB,EAAE,UAAU,CAAC;MAElEC,MAAM,CAACF,cAAc,CAAC,CAACG,OAAO,CAAC;QAC7BC,MAAM,EACJ;MACJ,CAAC,CAAC;IACJ,CAAC,CAAC;EACJ,CAAC,CAAC;EAEFN,QAAQ,CAAC,eAAe,EAAE,YAAM;IAC9BC,EAAE,CAAC,iCAAiC,EAAE,YAAM;MAC1CG,MAAM,CAAC,IAAAG,0BAAW,EAAC,WAAW,CAAC,CAAC,CAACF,OAAO,CACtCD,MAAM,CAACI,gBAAgB,CAAC;QACtBC,MAAM,EAAE,OAAO;QACfC,QAAQ,EAAE;UACRD,MAAM,EAAE,UAAU;UAClBE,IAAI,EAAE,CAAC,CAAC;UACRC,KAAK,EAAE,CACL;YACEH,MAAM,EAAE,OAAO;YACfE,IAAI,EAAE,CAAC,CAAC;YACRE,MAAM,EAAE,KAAK;YACbC,IAAI,EAAE,MAAM;YACZF,KAAK,EAAE,CAAC;cAAEH,MAAM,EAAE,MAAM;cAAEM,MAAM,EAAE,CAAC;gBAAEC,IAAI,EAAE;cAAY,CAAC;YAAE,CAAC;UAC7D,CAAC;QAEL;MACF,CAAC,CACH,CAAC;IACH,CAAC,CAAC;EACJ,CAAC,CAAC;AACJ,CAAC,CAAC","ignoreList":[]}