@pie-lib/mask-markup 3.1.3 → 4.0.0-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.
Files changed (113) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +4 -0
  2. package/dist/choices/choice.d.ts +28 -0
  3. package/dist/choices/choice.js +92 -0
  4. package/dist/choices/index.d.ts +29 -0
  5. package/dist/choices/index.js +59 -0
  6. package/dist/componentize.d.ts +12 -0
  7. package/dist/componentize.js +4 -0
  8. package/dist/components/blank.d.ts +45 -0
  9. package/dist/components/blank.js +273 -0
  10. package/dist/components/correct-input.d.ts +11 -0
  11. package/dist/components/dropdown.d.ts +37 -0
  12. package/dist/components/dropdown.js +311 -0
  13. package/dist/components/input.d.ts +37 -0
  14. package/dist/constructed-response.d.ts +44 -0
  15. package/dist/constructed-response.js +56 -0
  16. package/dist/customizable.d.ts +43 -0
  17. package/dist/customizable.js +8 -0
  18. package/dist/drag-in-the-blank.d.ts +47 -0
  19. package/dist/drag-in-the-blank.js +220 -0
  20. package/dist/index.d.ts +15 -0
  21. package/dist/index.js +7 -0
  22. package/dist/inline-dropdown.d.ts +44 -0
  23. package/dist/inline-dropdown.js +24 -0
  24. package/dist/keyboard-coordinates.d.ts +25 -0
  25. package/dist/keyboard-coordinates.js +49 -0
  26. package/dist/mask.d.ts +30 -0
  27. package/dist/mask.js +95 -0
  28. package/dist/node_modules/.bun/clsx@2.1.1/node_modules/clsx/dist/clsx.js +16 -0
  29. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/index.js +17 -0
  30. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssPrefix.js +9 -0
  31. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssUnitless.js +26 -0
  32. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/hasOwn.js +11 -0
  33. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isFunction.js +11 -0
  34. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isObject.js +11 -0
  35. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixInfo.js +24 -0
  36. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixProperties.js +32 -0
  37. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixer.js +29 -0
  38. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/camelize.js +14 -0
  39. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenRe.js +8 -0
  40. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenate.js +12 -0
  41. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/separate.js +11 -0
  42. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toLowerFirst.js +10 -0
  43. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toUpperFirst.js +10 -0
  44. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleObject.js +55 -0
  45. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleString.js +16 -0
  46. package/dist/serialization.d.ts +34 -0
  47. package/dist/serialization.js +132 -0
  48. package/dist/with-mask.d.ts +55 -0
  49. package/dist/with-mask.js +45 -0
  50. package/package.json +35 -19
  51. package/CHANGELOG.json +0 -17
  52. package/CHANGELOG.md +0 -1353
  53. package/LICENSE.md +0 -5
  54. package/lib/choices/choice.js +0 -138
  55. package/lib/choices/choice.js.map +0 -1
  56. package/lib/choices/index.js +0 -126
  57. package/lib/choices/index.js.map +0 -1
  58. package/lib/componentize.js +0 -21
  59. package/lib/componentize.js.map +0 -1
  60. package/lib/components/blank.js +0 -489
  61. package/lib/components/blank.js.map +0 -1
  62. package/lib/components/correct-input.js +0 -94
  63. package/lib/components/correct-input.js.map +0 -1
  64. package/lib/components/dropdown.js +0 -480
  65. package/lib/components/dropdown.js.map +0 -1
  66. package/lib/components/input.js +0 -50
  67. package/lib/components/input.js.map +0 -1
  68. package/lib/constructed-response.js +0 -101
  69. package/lib/constructed-response.js.map +0 -1
  70. package/lib/customizable.js +0 -42
  71. package/lib/customizable.js.map +0 -1
  72. package/lib/drag-in-the-blank.js +0 -363
  73. package/lib/drag-in-the-blank.js.map +0 -1
  74. package/lib/index.js +0 -55
  75. package/lib/index.js.map +0 -1
  76. package/lib/inline-dropdown.js +0 -40
  77. package/lib/inline-dropdown.js.map +0 -1
  78. package/lib/keyboard-coordinates.js +0 -156
  79. package/lib/keyboard-coordinates.js.map +0 -1
  80. package/lib/mask.js +0 -203
  81. package/lib/mask.js.map +0 -1
  82. package/lib/serialization.js +0 -261
  83. package/lib/serialization.js.map +0 -1
  84. package/lib/with-mask.js +0 -97
  85. package/lib/with-mask.js.map +0 -1
  86. package/src/__tests__/drag-in-the-blank.test.js +0 -256
  87. package/src/__tests__/index.test.js +0 -38
  88. package/src/__tests__/keyboard-coordinates.test.js +0 -202
  89. package/src/__tests__/mask.test.js +0 -479
  90. package/src/__tests__/serialization.test.js +0 -54
  91. package/src/__tests__/utils.js +0 -1
  92. package/src/__tests__/with-mask.test.js +0 -76
  93. package/src/choices/__tests__/index.test.js +0 -212
  94. package/src/choices/choice.jsx +0 -113
  95. package/src/choices/index.jsx +0 -88
  96. package/src/componentize.js +0 -13
  97. package/src/components/__tests__/blank.test.js +0 -413
  98. package/src/components/__tests__/correct-input.test.js +0 -90
  99. package/src/components/__tests__/dropdown.test.js +0 -129
  100. package/src/components/__tests__/input.test.js +0 -102
  101. package/src/components/blank.jsx +0 -504
  102. package/src/components/correct-input.jsx +0 -82
  103. package/src/components/dropdown.jsx +0 -421
  104. package/src/components/input.jsx +0 -48
  105. package/src/constructed-response.jsx +0 -87
  106. package/src/customizable.jsx +0 -34
  107. package/src/drag-in-the-blank.jsx +0 -351
  108. package/src/index.js +0 -16
  109. package/src/inline-dropdown.jsx +0 -29
  110. package/src/keyboard-coordinates.js +0 -147
  111. package/src/mask.jsx +0 -174
  112. package/src/serialization.js +0 -260
  113. package/src/with-mask.jsx +0 -75
@@ -1,102 +0,0 @@
1
- import * as React from 'react';
2
- import { render, screen } from '@testing-library/react';
3
- import userEvent from '@testing-library/user-event';
4
- import Input from '../input';
5
-
6
- // Mock CorrectInput to simplify testing
7
- jest.mock('../correct-input', () => {
8
- return function CorrectInput({ value, onChange, disabled, correct, variant }) {
9
- return (
10
- <input
11
- data-testid="correct-input"
12
- value={value || ''}
13
- onChange={onChange}
14
- disabled={disabled}
15
- data-correct={correct}
16
- data-variant={variant}
17
- />
18
- );
19
- };
20
- });
21
-
22
- describe('Input', () => {
23
- const onChange = jest.fn();
24
- const defaultProps = {
25
- disabled: false,
26
- correct: false,
27
- value: 'Cow',
28
- id: '1',
29
- onChange,
30
- };
31
-
32
- beforeEach(() => {
33
- onChange.mockClear();
34
- });
35
-
36
- describe('rendering', () => {
37
- it('renders with default props', () => {
38
- render(<Input {...defaultProps} />);
39
- const input = screen.getByTestId('correct-input');
40
-
41
- expect(input).toBeInTheDocument();
42
- expect(input).toHaveValue('Cow');
43
- expect(input).not.toBeDisabled();
44
- expect(input).toHaveAttribute('data-correct', 'false');
45
- });
46
-
47
- it('renders as disabled when disabled prop is true', () => {
48
- render(<Input {...defaultProps} disabled={true} />);
49
- const input = screen.getByTestId('correct-input');
50
-
51
- expect(input).toBeDisabled();
52
- });
53
-
54
- it('renders with correct state', () => {
55
- render(<Input {...defaultProps} correct={true} />);
56
- const input = screen.getByTestId('correct-input');
57
-
58
- expect(input).toHaveAttribute('data-correct', 'true');
59
- });
60
-
61
- it('shows correct answer when showCorrectAnswer is true', () => {
62
- render(<Input {...defaultProps} showCorrectAnswer={true} />);
63
- const input = screen.getByTestId('correct-input');
64
-
65
- expect(input).toHaveAttribute('data-correct', 'true');
66
- });
67
- });
68
-
69
- describe('user interactions', () => {
70
- it('calls onChange with id and value when user types', async () => {
71
- const user = userEvent.setup();
72
- render(<Input {...defaultProps} value="" />);
73
-
74
- const input = screen.getByTestId('correct-input');
75
- await user.type(input, '20');
76
-
77
- // userEvent.type types character by character, so onChange is called for each character
78
- expect(onChange).toHaveBeenCalled();
79
- expect(onChange).toHaveBeenCalledTimes(2);
80
- // Check the last call has both characters
81
- expect(onChange).toHaveBeenLastCalledWith('1', '0');
82
- });
83
-
84
- it('calls onChange with updated value', async () => {
85
- const user = userEvent.setup();
86
- render(<Input {...defaultProps} />);
87
-
88
- const input = screen.getByTestId('correct-input');
89
- await user.clear(input);
90
- await user.type(input, 'New Value');
91
-
92
- // userEvent.type types character by character
93
- // After clear, we start with empty string, and each character is typed
94
- // The last call should have the full accumulated value up to the last character
95
- expect(onChange).toHaveBeenCalled();
96
- // With clear + "New Value", onChange is called for clearing ("") and each typed character
97
- // The value accumulated in the input element after typing will be "CowNew Value"
98
- // because the component starts with value="Cow" and we clear then type
99
- expect(onChange.mock.calls.length).toBeGreaterThan(0);
100
- });
101
- });
102
- });
@@ -1,504 +0,0 @@
1
- import React, { useEffect, useRef, useState } from 'react';
2
- import PropTypes from 'prop-types';
3
- import { renderMath } from '@pie-lib/math-rendering';
4
- import debug from 'debug';
5
- import { useDraggable, useDroppable } from '@dnd-kit/core';
6
- import { styled } from '@mui/material/styles';
7
- import Chip from '@mui/material/Chip';
8
- import classnames from 'classnames';
9
- import { color } from '@pie-lib/render-ui';
10
- import { grey } from '@mui/material/colors';
11
-
12
- const log = debug('pie-lib:mask-markup:blank');
13
-
14
- const StyledContent = styled('span')(({ dragged, over, selected, showsPointerCursor }) => ({
15
- border: `solid 0px ${color.primary()}`,
16
- minWidth: '200px',
17
- overflow: 'hidden',
18
- whiteSpace: 'nowrap',
19
- opacity: 1,
20
- cursor: 'default',
21
- ...(over && {
22
- whiteSpace: 'nowrap',
23
- overflow: 'hidden',
24
- }),
25
- ...((dragged || selected) && {
26
- opacity: 0.5,
27
- }),
28
- ...(showsPointerCursor && {
29
- '&:hover': {
30
- cursor: 'pointer',
31
- },
32
- }),
33
- }));
34
-
35
- const StyledDragHandle = styled('span')({
36
- display: 'inline-flex',
37
- touchAction: 'none',
38
- });
39
-
40
- const StyledChip = styled(Chip)(() => ({
41
- backgroundColor: color.background(),
42
- border: `2px dashed ${color.text()}`,
43
- touchAction: 'none',
44
- color: color.text(),
45
- fontSize: 'inherit',
46
- maxWidth: '374px',
47
- position: 'relative',
48
- borderRadius: '3px',
49
- '&.over': {
50
- whiteSpace: 'nowrap',
51
- overflow: 'hidden',
52
- },
53
- '&.parentOver': {
54
- border: `1px solid ${grey[500]}`,
55
- backgroundColor: `${grey[300]}`,
56
- },
57
- '&.correct': {
58
- border: `solid 1px ${color.correct()}`,
59
- },
60
- '&.incorrect': {
61
- border: `solid 1px ${color.incorrect()}`,
62
- },
63
- '&.Mui-disabled': {
64
- opacity: 1,
65
- },
66
- }));
67
-
68
- const StyledChipLabel = styled('span')(() => ({
69
- whiteSpace: 'normal',
70
- // Added for touch devices, for image content.
71
- // This will prevent the context menu from appearing and not allowing other interactions with the image.
72
- // If interactions with the image in the token will be requested we should handle only the context Menu.
73
- pointerEvents: 'none',
74
- '& img': {
75
- display: 'block',
76
- padding: '2px 0',
77
- },
78
- // Remove default <p> margins to ensure consistent spacing across all wrapped content (p, span, div, math)
79
- // Padding for top and bottom will instead be controlled by the container for consistent layout
80
- // Ensures consistent behavior with pie-api-browser, where marginTop is already removed by a Bootstrap stylesheet
81
- '& p': {
82
- marginTop: '0',
83
- marginBottom: '0',
84
- },
85
- '& mjx-frac': {
86
- fontSize: '120% !important',
87
- },
88
- '& mjx-mn:has(~ mjx-mfrac), mjx-mfrac ~ mjx-mn': {
89
- fontSize: '120% !important',
90
- },
91
- '&.over': {
92
- whiteSpace: 'nowrap',
93
- overflow: 'hidden',
94
- },
95
- '&.hidden': {
96
- color: 'transparent',
97
- opacity: 0,
98
- },
99
- '&.dragged': {
100
- position: 'absolute',
101
- left: 16,
102
- maxWidth: '60px',
103
- top: '50%',
104
- transform: 'translateY(-50%)',
105
- },
106
- }));
107
-
108
- function BlankContent({
109
- disabled,
110
- choice,
111
- isOver,
112
- isDragging,
113
- dragItem,
114
- correct,
115
- emptyResponseAreaWidth,
116
- emptyResponseAreaHeight,
117
- }) {
118
- const rootRef = useRef(null);
119
- const spanRef = useRef(null);
120
- const frozenRef = useRef(null); // to use during dragging to prevent flickering
121
- const measuringRef = useRef(false); // guard against ResizeObserver feedback loops
122
- const [dimensions, setDimensions] = useState({ height: 0, width: 0 });
123
-
124
- const handleImageLoad = () => {
125
- updateDimensions();
126
- };
127
-
128
- const handleElements = () => {
129
- const imageElement = spanRef.current?.querySelector('img');
130
- if (imageElement) {
131
- imageElement.onload = handleImageLoad;
132
- } else {
133
- setTimeout(() => {
134
- updateDimensions();
135
- }, 300);
136
- }
137
- };
138
-
139
- const getMeasureNode = () => {
140
- if (!spanRef.current) {
141
- return null;
142
- }
143
-
144
- const mjx = spanRef.current.querySelector('mjx-container');
145
-
146
- if (mjx && spanRef.current.parentElement) {
147
- return spanRef.current.parentElement;
148
- }
149
-
150
- const img = spanRef.current.querySelector('img');
151
-
152
- if (img) {
153
- // If there's text alongside the image, measure the full span to capture both dimensions
154
- const hasTextContent = spanRef.current.textContent.trim().length > 0;
155
-
156
- return hasTextContent ? spanRef.current : img;
157
- }
158
-
159
- return spanRef.current;
160
- };
161
-
162
- const updateDimensions = () => {
163
- if (spanRef.current && rootRef.current && !measuringRef.current) {
164
- measuringRef.current = true;
165
- // Temporarily set rootRef width to 'auto' for natural measurement
166
- rootRef.current.style.width = 'auto';
167
- rootRef.current.style.height = 'auto';
168
- rootRef.current.offsetHeight;
169
-
170
- const measureNode = getMeasureNode();
171
- const node = measureNode || spanRef.current;
172
- const rect = node.getBoundingClientRect();
173
- const width = node.offsetWidth || rect.width || 0;
174
- const height = Math.max(
175
- node.offsetHeight || 0,
176
- rect.height || 0,
177
- node.scrollHeight || 0,
178
- spanRef.current.scrollHeight || 0,
179
- );
180
-
181
- const PADDING = 12;
182
- const BORDER_WIDTH = 2;
183
- const ADDITIONAL_SPACE = 1;
184
- // padding and border on each side
185
- const widthWithPadding = width + 2 * PADDING + 2 * BORDER_WIDTH + ADDITIONAL_SPACE;
186
- // padding and border on top and bottom
187
- const heightWithPadding = height + 2 * PADDING + 2 * BORDER_WIDTH + ADDITIONAL_SPACE;
188
-
189
- const responseAreaWidth = parseFloat(emptyResponseAreaWidth) || 0;
190
- const responseAreaHeight = parseFloat(emptyResponseAreaHeight) || 0;
191
-
192
- const adjustedWidth = widthWithPadding <= responseAreaWidth ? responseAreaWidth : widthWithPadding;
193
- const adjustedHeight = heightWithPadding <= responseAreaHeight ? responseAreaHeight : heightWithPadding;
194
-
195
- setDimensions((prevState) => ({
196
- width: adjustedWidth > responseAreaWidth ? adjustedWidth : prevState.width,
197
- height: adjustedHeight > responseAreaHeight ? adjustedHeight : prevState.height,
198
- }));
199
-
200
- const nextWidth = `${adjustedWidth}px`;
201
- const nextHeight = `${adjustedHeight}px`;
202
- if (rootRef.current.style.width !== nextWidth) {
203
- rootRef.current.style.width = nextWidth;
204
- }
205
- if (rootRef.current.style.height !== nextHeight) {
206
- rootRef.current.style.height = nextHeight;
207
- }
208
- measuringRef.current = false;
209
- }
210
- };
211
-
212
- const getRootDimensions = () => {
213
- // Handle potential non-numeric values
214
- const responseAreaWidth = !isNaN(parseFloat(emptyResponseAreaWidth)) ? parseFloat(emptyResponseAreaWidth) : 0;
215
- const responseAreaHeight = !isNaN(parseFloat(emptyResponseAreaHeight)) ? parseFloat(emptyResponseAreaHeight) : 0;
216
-
217
- const rootStyle = {
218
- height: dimensions.height || responseAreaHeight,
219
- width: dimensions.width || responseAreaWidth,
220
- };
221
-
222
- // add minWidth, minHeight if width and height are not defined
223
- return {
224
- ...rootStyle,
225
- ...(responseAreaWidth ? {} : { minWidth: 90 }),
226
- ...(responseAreaHeight ? {} : { minHeight: 32 }),
227
- };
228
- };
229
-
230
- useEffect(() => {
231
- handleElements();
232
- }, []);
233
-
234
- // Re-measure when the element first becomes visible — covers the tabbed-view case
235
- // where the initial measurement happened while the tab was hidden (size 0).
236
- useEffect(() => {
237
- if (typeof IntersectionObserver === 'undefined' || !rootRef.current) return undefined;
238
- const io = new IntersectionObserver(
239
- (entries) => {
240
- if (entries.some((e) => e.isIntersecting)) updateDimensions();
241
- },
242
- { threshold: 0 },
243
- );
244
- io.observe(rootRef.current);
245
- return () => io.disconnect();
246
- }, []);
247
-
248
- // Render math for the placeholder/preview when dragging over
249
- useEffect(() => {
250
- if (rootRef.current) {
251
- renderMath(rootRef.current);
252
- }
253
- }, [correct, isOver, dragItem?.choice?.value]);
254
-
255
- useEffect(() => {
256
- if (!choice) {
257
- setDimensions({ height: 0, width: 0 });
258
- return;
259
- }
260
- handleElements();
261
- }, [choice?.value]);
262
-
263
- useEffect(() => {
264
- if (!isOver && !isDragging) {
265
- frozenRef.current = {
266
- width: rootRef.current.offsetWidth,
267
- height: rootRef.current.offsetHeight,
268
- };
269
- }
270
- }, [choice, isOver, isDragging]);
271
-
272
- const draggedLabel = dragItem && isOver && dragItem.choice && dragItem.choice.value;
273
- const label = choice && choice.value;
274
- const style =
275
- isOver || isDragging
276
- ? {
277
- width: frozenRef.current?.width,
278
- height: frozenRef.current?.height,
279
- }
280
- : getRootDimensions();
281
-
282
- return (
283
- <StyledChip
284
- clickable={false}
285
- disabled={disabled}
286
- ref={rootRef}
287
- component="span"
288
- label={
289
- <React.Fragment>
290
- <StyledChipLabel
291
- ref={spanRef}
292
- draggable={true}
293
- className={classnames({
294
- over: isOver,
295
- hidden: draggedLabel,
296
- })}
297
- dangerouslySetInnerHTML={{ __html: label || '' }}
298
- />
299
- {draggedLabel && (
300
- <StyledChipLabel
301
- draggable={true}
302
- className={classnames({
303
- over: isOver,
304
- dragged: true,
305
- })}
306
- dangerouslySetInnerHTML={{ __html: draggedLabel || '' }}
307
- />
308
- )}
309
- </React.Fragment>
310
- }
311
- className={classnames({
312
- over: isOver,
313
- parentOver: isOver,
314
- correct: correct !== undefined && correct,
315
- incorrect: correct !== undefined && !correct,
316
- })}
317
- variant={disabled ? 'outlined' : undefined}
318
- style={style}
319
- />
320
- );
321
- }
322
-
323
- BlankContent.defaultProps = {
324
- emptyResponseAreaWidth: 0,
325
- emptyResponseAreaHeight: 0,
326
- };
327
-
328
- BlankContent.propTypes = {
329
- id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
330
- disabled: PropTypes.bool,
331
- duplicates: PropTypes.bool,
332
- choice: PropTypes.object,
333
- isOver: PropTypes.bool,
334
- dragItem: PropTypes.object,
335
- correct: PropTypes.bool,
336
- onChange: PropTypes.func,
337
- emptyResponseAreaWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
338
- emptyResponseAreaHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
339
- instanceId: PropTypes.string,
340
- };
341
-
342
- // New functional component using @dnd-kit hooks
343
- function DragDropBlank({
344
- id,
345
- disabled,
346
- duplicates,
347
- choice,
348
- correct,
349
- onChange,
350
- emptyResponseAreaWidth,
351
- emptyResponseAreaHeight,
352
- instanceId,
353
- selectedItem,
354
- onSelectClick,
355
- onPlacementClick,
356
- }) {
357
- const [isHovered, setIsHovered] = useState(false);
358
-
359
- const dragData = {
360
- id,
361
- choice,
362
- instanceId,
363
- fromChoice: false, // This is from a blank, not from choices
364
- type: 'MaskBlank',
365
- };
366
-
367
- // Setup draggable functionality
368
- const {
369
- attributes: dragAttributes,
370
- listeners: dragListeners,
371
- setNodeRef: setDragNodeRef,
372
- isDragging,
373
- } = useDraggable({
374
- id: `mask-blank-drag-${id}`,
375
- disabled: disabled || !choice,
376
- data: dragData,
377
- // dnd-kit's own `attributes` default tabIndex to 0 unconditionally, even when
378
- // `disabled` — so a non-draggable (empty) blank stays a native Tab stop of its own.
379
- // For a drop-zone (droppable blank), that duplicates the outer wrapper's own tab stop
380
- // (see isNativeTabStop below) at the exact same position, so Tab/Shift+Tab has to
381
- // pass through both to move anywhere visibly, making every other press look like a
382
- // no-op. Drop it out of the tab order here whenever it isn't independently
383
- // reachable/interactive, leaving the outer wrapper (for an empty blank) or
384
- // nothing (for a disabled blank) as the sole stop.
385
- attributes: {
386
- tabIndex: choice && !disabled ? 0 : -1,
387
- },
388
- });
389
-
390
- // Setup droppable functionality
391
- const {
392
- setNodeRef: setDropNodeRef,
393
- isOver,
394
- active: dragItem,
395
- } = useDroppable({
396
- id: `mask-blank-drop-${id}`,
397
- data: {
398
- id: id,
399
- accepts: ['MaskBlank'],
400
- instanceId: instanceId,
401
- },
402
- });
403
-
404
- const isSelected = !!selectedItem && selectedItem.fromChoice === false && selectedItem.id === id;
405
-
406
- const handleClick = () => {
407
- if (disabled) return;
408
-
409
- if (isSelected) {
410
- // Clicking this blank's already-selected content again deselects it.
411
- onSelectClick?.(dragData);
412
- } else if (selectedItem) {
413
- // Something else is selected — place it here, whether this blank is currently
414
- // empty or already filled (the same commitPlacement logic drag-and-drop uses).
415
- onPlacementClick?.(id);
416
- } else if (choice) {
417
- // Nothing selected yet, and this blank holds an answer — select it for moving
418
- // elsewhere, the same way Tab+Space/Enter does.
419
- onSelectClick?.(dragData);
420
- }
421
-
422
- // Empty blank clicked with nothing selected: nothing to place or select.
423
- };
424
-
425
- // An empty blank isn't draggable, so dnd-kit's own attributes (only applied to the
426
- // inner node, and only when draggable) never make it tabbable — this outer wrapper
427
- // needs its own focus/activation handling so "select a choice, then Tab to a blank
428
- // and press Space/Enter" works even when the blank is empty. This is independent of,
429
- // and doesn't change, the existing in-drag Tab-cycling (that's driven by an active
430
- // dnd-kit drag, not native focus).
431
- //
432
- // Only made a native Tab stop when NOT draggable (i.e. empty): when the blank is
433
- // filled, the inner node is already independently tabbable via dnd-kit's own
434
- // attributes for the existing pick-up-to-move gesture, and adding a second, outer Tab
435
- // stop for the same visual chip would add an extra stop to the existing Tab order —
436
- // the same double-tab-stop bug already found and fixed once in match-list/image-
437
- // cloze-association's equivalent code.
438
- const isNativeTabStop = !choice && !disabled;
439
- const isInnerDraggable = !!choice && !disabled;
440
-
441
- const handleKeyDown = (e) => {
442
- if (e.code === 'Space' || e.code === 'Enter') {
443
- e.preventDefault();
444
- handleClick();
445
- }
446
- };
447
-
448
- const hasSelection = !!selectedItem;
449
- const showsHoverEffect = isOver || (hasSelection && isHovered && !disabled);
450
-
451
- return (
452
- <StyledContent
453
- ref={setDropNodeRef}
454
- role={isNativeTabStop ? 'button' : undefined}
455
- tabIndex={isNativeTabStop ? 0 : -1}
456
- onClick={handleClick}
457
- onKeyDown={isNativeTabStop ? handleKeyDown : undefined}
458
- onMouseEnter={() => setIsHovered(true)}
459
- onMouseLeave={() => setIsHovered(false)}
460
- dragged={isDragging}
461
- over={showsHoverEffect}
462
- selected={isSelected}
463
- showsPointerCursor={hasSelection && !disabled}
464
- >
465
- <StyledDragHandle ref={setDragNodeRef} {...(isInnerDraggable ? dragAttributes : {})} {...dragListeners}>
466
- <BlankContent
467
- id={id}
468
- disabled={disabled}
469
- duplicates={duplicates}
470
- choice={choice}
471
- isOver={showsHoverEffect}
472
- dragItem={dragItem?.data?.current}
473
- correct={correct}
474
- onChange={onChange}
475
- emptyResponseAreaWidth={emptyResponseAreaWidth}
476
- emptyResponseAreaHeight={emptyResponseAreaHeight}
477
- instanceId={instanceId}
478
- />
479
- </StyledDragHandle>
480
- </StyledContent>
481
- );
482
- }
483
-
484
- DragDropBlank.defaultProps = {
485
- emptyResponseAreaWidth: 0,
486
- emptyResponseAreaHeight: 0,
487
- };
488
-
489
- DragDropBlank.propTypes = {
490
- id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
491
- disabled: PropTypes.bool,
492
- duplicates: PropTypes.bool,
493
- choice: PropTypes.object,
494
- correct: PropTypes.bool,
495
- onChange: PropTypes.func,
496
- emptyResponseAreaWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
497
- emptyResponseAreaHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
498
- instanceId: PropTypes.string,
499
- selectedItem: PropTypes.object,
500
- onSelectClick: PropTypes.func,
501
- onPlacementClick: PropTypes.func,
502
- };
503
-
504
- export default DragDropBlank;
@@ -1,82 +0,0 @@
1
- import React from 'react';
2
- import OutlinedInput from '@mui/material/OutlinedInput';
3
- import classnames from 'classnames';
4
- import { styled } from '@mui/material/styles';
5
- import { color } from '@pie-lib/render-ui';
6
-
7
- const StyledOutlinedInput = styled(OutlinedInput)(() => ({
8
- padding: '2px',
9
- borderRadius: '4px',
10
- fontSize: 'inherit',
11
- display: 'inline-block',
12
- verticalAlign: 'middle',
13
- '& fieldset': {
14
- border: 0,
15
- },
16
- '& .MuiOutlinedInput-input': {
17
- color: color.text(),
18
- backgroundColor: color.background(),
19
- borderRadius: '4px !important',
20
- borderWidth: '1px',
21
- borderStyle: 'solid',
22
- padding: '10px 20px 10px 10px',
23
- '&:disabled': {
24
- opacity: 0.8,
25
- cursor: 'not-allowed !important',
26
- },
27
- '&:hover': {
28
- borderColor: color.primary(),
29
- '&:disabled': {
30
- borderColor: 'initial',
31
- },
32
- },
33
- '&.Mui-focused': {
34
- borderColor: color.primaryDark(),
35
- },
36
- '&.crInput': {
37
- padding: '8px !important',
38
- },
39
- '&.correct': {
40
- borderColor: `${color.correct()} !important`,
41
- },
42
- '&.incorrect': {
43
- borderColor: `${color.incorrect()} !important`,
44
- },
45
- },
46
- }));
47
-
48
- const CorrectInput = (props) => {
49
- const { correct, charactersLimit, disabled, isBox, isConstructedResponse, width, spellCheck, ...rest } = props;
50
-
51
- const label = typeof correct === 'boolean' ? (correct ? 'correct' : 'incorrect') : undefined;
52
- const inputProps = charactersLimit
53
- ? { maxLength: charactersLimit, 'aria-label': 'Enter answer' }
54
- : { 'aria-label': 'Enter answer' };
55
-
56
- if (width) {
57
- inputProps.style = {
58
- width: `${width + Math.round(width / 10) + 1}ch`, // added some extra space for capital letters
59
- };
60
- }
61
-
62
- return (
63
- <StyledOutlinedInput
64
- className={classnames({
65
- disabledInput: disabled,
66
- box: isBox,
67
- })}
68
- classes={{
69
- input: classnames({
70
- [label]: label,
71
- crInput: isConstructedResponse,
72
- }),
73
- }}
74
- inputProps={inputProps}
75
- disabled={disabled}
76
- spellCheck={spellCheck}
77
- {...rest}
78
- />
79
- );
80
- };
81
-
82
- export default CorrectInput;