@pie-lib/mask-markup 3.0.4-next.0 → 3.0.4-next.104

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 (106) hide show
  1. package/CHANGELOG.json +17 -0
  2. package/CHANGELOG.md +1256 -0
  3. package/LICENSE.md +5 -0
  4. package/lib/choices/choice.js +120 -0
  5. package/lib/choices/choice.js.map +1 -0
  6. package/lib/choices/index.js +103 -0
  7. package/lib/choices/index.js.map +1 -0
  8. package/lib/componentize.js +21 -0
  9. package/lib/componentize.js.map +1 -0
  10. package/lib/components/blank.js +412 -0
  11. package/lib/components/blank.js.map +1 -0
  12. package/lib/components/correct-input.js +94 -0
  13. package/lib/components/correct-input.js.map +1 -0
  14. package/lib/components/dropdown.js +483 -0
  15. package/lib/components/dropdown.js.map +1 -0
  16. package/lib/components/input.js +50 -0
  17. package/lib/components/input.js.map +1 -0
  18. package/lib/constructed-response.js +101 -0
  19. package/lib/constructed-response.js.map +1 -0
  20. package/lib/customizable.js +42 -0
  21. package/lib/customizable.js.map +1 -0
  22. package/lib/drag-in-the-blank.js +254 -0
  23. package/lib/drag-in-the-blank.js.map +1 -0
  24. package/lib/index.js +55 -0
  25. package/lib/index.js.map +1 -0
  26. package/lib/inline-dropdown.js +40 -0
  27. package/lib/inline-dropdown.js.map +1 -0
  28. package/lib/mask.js +198 -0
  29. package/lib/mask.js.map +1 -0
  30. package/lib/serialization.js +261 -0
  31. package/lib/serialization.js.map +1 -0
  32. package/lib/with-mask.js +97 -0
  33. package/lib/with-mask.js.map +1 -0
  34. package/package.json +17 -35
  35. package/src/__tests__/drag-in-the-blank.test.js +111 -0
  36. package/src/__tests__/index.test.js +38 -0
  37. package/src/__tests__/mask.test.js +381 -0
  38. package/src/__tests__/serialization.test.js +54 -0
  39. package/src/__tests__/utils.js +1 -0
  40. package/src/__tests__/with-mask.test.js +76 -0
  41. package/src/choices/__tests__/index.test.js +109 -0
  42. package/src/choices/choice.jsx +100 -0
  43. package/src/choices/index.jsx +64 -0
  44. package/src/componentize.js +13 -0
  45. package/src/components/__tests__/blank.test.js +232 -0
  46. package/src/components/__tests__/correct-input.test.js +90 -0
  47. package/src/components/__tests__/dropdown.test.js +129 -0
  48. package/src/components/__tests__/input.test.js +102 -0
  49. package/src/components/blank.jsx +431 -0
  50. package/src/components/correct-input.jsx +82 -0
  51. package/src/components/dropdown.jsx +423 -0
  52. package/src/components/input.jsx +48 -0
  53. package/src/constructed-response.jsx +87 -0
  54. package/src/customizable.jsx +34 -0
  55. package/src/drag-in-the-blank.jsx +241 -0
  56. package/src/index.js +16 -0
  57. package/src/inline-dropdown.jsx +29 -0
  58. package/src/mask.jsx +172 -0
  59. package/src/serialization.js +260 -0
  60. package/src/with-mask.jsx +75 -0
  61. package/dist/_virtual/_rolldown/runtime.js +0 -4
  62. package/dist/choices/choice.d.ts +0 -24
  63. package/dist/choices/choice.js +0 -77
  64. package/dist/choices/index.d.ts +0 -25
  65. package/dist/choices/index.js +0 -49
  66. package/dist/componentize.d.ts +0 -12
  67. package/dist/componentize.js +0 -4
  68. package/dist/components/blank.d.ts +0 -39
  69. package/dist/components/blank.js +0 -240
  70. package/dist/components/correct-input.d.ts +0 -11
  71. package/dist/components/dropdown.d.ts +0 -37
  72. package/dist/components/dropdown.js +0 -320
  73. package/dist/components/input.d.ts +0 -37
  74. package/dist/constructed-response.d.ts +0 -44
  75. package/dist/constructed-response.js +0 -55
  76. package/dist/customizable.d.ts +0 -43
  77. package/dist/customizable.js +0 -8
  78. package/dist/drag-in-the-blank.d.ts +0 -37
  79. package/dist/drag-in-the-blank.js +0 -164
  80. package/dist/index.d.ts +0 -15
  81. package/dist/index.js +0 -7
  82. package/dist/inline-dropdown.d.ts +0 -44
  83. package/dist/inline-dropdown.js +0 -24
  84. package/dist/mask.d.ts +0 -30
  85. package/dist/mask.js +0 -99
  86. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/index.js +0 -17
  87. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssPrefix.js +0 -9
  88. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssUnitless.js +0 -26
  89. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/hasOwn.js +0 -11
  90. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isFunction.js +0 -11
  91. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isObject.js +0 -11
  92. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixInfo.js +0 -24
  93. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixProperties.js +0 -32
  94. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixer.js +0 -29
  95. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/camelize.js +0 -14
  96. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenRe.js +0 -8
  97. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenate.js +0 -12
  98. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/separate.js +0 -11
  99. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toLowerFirst.js +0 -10
  100. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toUpperFirst.js +0 -10
  101. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleObject.js +0 -55
  102. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleString.js +0 -16
  103. package/dist/serialization.d.ts +0 -34
  104. package/dist/serialization.js +0 -132
  105. package/dist/with-mask.d.ts +0 -55
  106. package/dist/with-mask.js +0 -45
@@ -0,0 +1,129 @@
1
+ import * as React from 'react';
2
+ import { render, screen, waitFor } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import { choice } from '../../__tests__/utils';
5
+ import Dropdown from '../dropdown';
6
+
7
+ describe('Dropdown', () => {
8
+ const onChange = jest.fn();
9
+ const defaultProps = {
10
+ onChange,
11
+ id: '1',
12
+ correct: false,
13
+ disabled: false,
14
+ value: 'Jumped',
15
+ choices: [choice('Jumped'), choice('Laughed'), choice('Smiled')],
16
+ };
17
+
18
+ beforeEach(() => {
19
+ onChange.mockClear();
20
+ });
21
+
22
+ describe('rendering', () => {
23
+ it('renders dropdown with default props', () => {
24
+ render(<Dropdown {...defaultProps} />);
25
+ const button = screen.getByRole('combobox');
26
+ expect(button).toBeInTheDocument();
27
+ // Button displays the selected value
28
+ expect(button).toHaveTextContent('Jumped');
29
+ });
30
+
31
+ it('renders with all choices as options when opened', async () => {
32
+ const user = userEvent.setup();
33
+ render(<Dropdown {...defaultProps} />);
34
+
35
+ const button = screen.getByRole('combobox');
36
+ await user.click(button);
37
+
38
+ // Options should now be visible - find them by role
39
+ const options = screen.getAllByRole('option');
40
+ expect(options).toHaveLength(3);
41
+ // Verify the text content of options using specific elements
42
+ expect(options[0]).toHaveTextContent('Jumped');
43
+ expect(options[1]).toHaveTextContent('Laughed');
44
+ expect(options[2]).toHaveTextContent('Smiled');
45
+ });
46
+
47
+ it('focuses the selected option when the menu opens', async () => {
48
+ const user = userEvent.setup();
49
+ render(<Dropdown {...defaultProps} />);
50
+
51
+ const button = screen.getByRole('combobox');
52
+ await user.click(button);
53
+
54
+ await waitFor(() => {
55
+ expect(document.getElementById('dropdown-option-1-0')).toHaveFocus();
56
+ });
57
+ expect(button).toHaveAttribute('aria-activedescendant', 'dropdown-option-1-0');
58
+ });
59
+
60
+ it('focuses a non-first selected option when the menu opens', async () => {
61
+ const user = userEvent.setup();
62
+ render(<Dropdown {...defaultProps} value="Laughed" />);
63
+
64
+ const button = screen.getByRole('combobox');
65
+ await user.click(button);
66
+
67
+ await waitFor(() => {
68
+ expect(document.getElementById('dropdown-option-1-1')).toHaveFocus();
69
+ });
70
+ expect(button).toHaveAttribute('aria-activedescendant', 'dropdown-option-1-1');
71
+ });
72
+
73
+ it('does not highlight an option when no value is selected', async () => {
74
+ const user = userEvent.setup();
75
+ render(<Dropdown {...defaultProps} value={undefined} />);
76
+
77
+ const button = screen.getByRole('combobox');
78
+ await user.click(button);
79
+
80
+ expect(button).not.toHaveAttribute('aria-activedescendant');
81
+ });
82
+
83
+ it('renders as disabled when disabled prop is true', () => {
84
+ render(<Dropdown {...defaultProps} disabled={true} />);
85
+ const button = screen.getByRole('combobox');
86
+ expect(button).toBeDisabled();
87
+ });
88
+
89
+ it('shows correct state when correct is true', () => {
90
+ const { container } = render(<Dropdown {...defaultProps} correct={true} />);
91
+ const button = screen.getByRole('combobox');
92
+ expect(button).toBeInTheDocument();
93
+ });
94
+ });
95
+
96
+ describe('user interactions', () => {
97
+ it('calls onChange when user selects a different option', async () => {
98
+ const user = userEvent.setup();
99
+ render(<Dropdown {...defaultProps} />);
100
+
101
+ // Click button to open menu
102
+ const button = screen.getByRole('combobox');
103
+ await user.click(button);
104
+
105
+ // Find the option by getting all options and selecting the one with "Laughed" text
106
+ const options = screen.getAllByRole('option');
107
+ const laughedOption = options.find((opt) => opt.textContent.includes('Laughed'));
108
+ await user.click(laughedOption);
109
+
110
+ expect(onChange).toHaveBeenCalledWith('1', 'Laughed');
111
+ });
112
+
113
+ it('calls onChange with correct value', async () => {
114
+ const user = userEvent.setup();
115
+ render(<Dropdown {...defaultProps} />);
116
+
117
+ // Click button to open menu
118
+ const button = screen.getByRole('combobox');
119
+ await user.click(button);
120
+
121
+ // Find the option by getting all options and selecting the one with "Smiled" text
122
+ const options = screen.getAllByRole('option');
123
+ const smiledOption = options.find((opt) => opt.textContent.includes('Smiled'));
124
+ await user.click(smiledOption);
125
+
126
+ expect(onChange).toHaveBeenCalledWith('1', 'Smiled');
127
+ });
128
+ });
129
+ });
@@ -0,0 +1,102 @@
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
+ });
@@ -0,0 +1,431 @@
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 { CSS } from '@dnd-kit/utilities';
7
+ import { styled } from '@mui/material/styles';
8
+ import Chip from '@mui/material/Chip';
9
+ import classnames from 'classnames';
10
+ import { color } from '@pie-lib/render-ui';
11
+ import { grey } from '@mui/material/colors';
12
+
13
+ const log = debug('pie-lib:mask-markup:blank');
14
+
15
+ const StyledContent = styled('span')(({ dragged, over }) => ({
16
+ border: `solid 0px ${color.primary()}`,
17
+ minWidth: '200px',
18
+ touchAction: 'none',
19
+ overflow: 'hidden',
20
+ whiteSpace: 'nowrap',
21
+ opacity: 1,
22
+ ...(over && {
23
+ whiteSpace: 'nowrap',
24
+ overflow: 'hidden',
25
+ }),
26
+ ...(dragged && {
27
+ opacity: 0.5,
28
+ }),
29
+ }));
30
+
31
+ const StyledChip = styled(Chip)(() => ({
32
+ backgroundColor: color.background(),
33
+ border: `2px dashed ${color.text()}`,
34
+ touchAction: 'none',
35
+ color: color.text(),
36
+ fontSize: 'inherit',
37
+ maxWidth: '374px',
38
+ position: 'relative',
39
+ borderRadius: '3px',
40
+ '&.over': {
41
+ whiteSpace: 'nowrap',
42
+ overflow: 'hidden',
43
+ },
44
+ '&.parentOver': {
45
+ border: `1px solid ${grey[500]}`,
46
+ backgroundColor: `${grey[300]}`,
47
+ },
48
+ '&.correct': {
49
+ border: `solid 1px ${color.correct()}`,
50
+ },
51
+ '&.incorrect': {
52
+ border: `solid 1px ${color.incorrect()}`,
53
+ },
54
+ '&.Mui-disabled': {
55
+ opacity: 1,
56
+ },
57
+ }));
58
+
59
+ const StyledChipLabel = styled('span')(() => ({
60
+ whiteSpace: 'normal',
61
+ // Added for touch devices, for image content.
62
+ // This will prevent the context menu from appearing and not allowing other interactions with the image.
63
+ // If interactions with the image in the token will be requested we should handle only the context Menu.
64
+ pointerEvents: 'none',
65
+ '& img': {
66
+ display: 'block',
67
+ padding: '2px 0',
68
+ },
69
+ // Remove default <p> margins to ensure consistent spacing across all wrapped content (p, span, div, math)
70
+ // Padding for top and bottom will instead be controlled by the container for consistent layout
71
+ // Ensures consistent behavior with pie-api-browser, where marginTop is already removed by a Bootstrap stylesheet
72
+ '& p': {
73
+ marginTop: '0',
74
+ marginBottom: '0',
75
+ },
76
+ '& mjx-frac': {
77
+ fontSize: '120% !important',
78
+ },
79
+ '& mjx-mn:has(~ mjx-mfrac), mjx-mfrac ~ mjx-mn': {
80
+ fontSize: '120% !important',
81
+ },
82
+ '&.over': {
83
+ whiteSpace: 'nowrap',
84
+ overflow: 'hidden',
85
+ },
86
+ '&.hidden': {
87
+ color: 'transparent',
88
+ opacity: 0,
89
+ },
90
+ '&.dragged': {
91
+ position: 'absolute',
92
+ left: 16,
93
+ maxWidth: '60px',
94
+ top: '50%',
95
+ transform: 'translateY(-50%)',
96
+ },
97
+ }));
98
+
99
+ function BlankContent({
100
+ disabled,
101
+ choice,
102
+ isOver,
103
+ isDragging,
104
+ dragItem,
105
+ correct,
106
+ emptyResponseAreaWidth,
107
+ emptyResponseAreaHeight,
108
+ }) {
109
+ const rootRef = useRef(null);
110
+ const spanRef = useRef(null);
111
+ const frozenRef = useRef(null); // to use during dragging to prevent flickering
112
+ const measuringRef = useRef(false); // guard against ResizeObserver feedback loops
113
+ const [dimensions, setDimensions] = useState({ height: 0, width: 0 });
114
+
115
+ const handleImageLoad = () => {
116
+ updateDimensions();
117
+ };
118
+
119
+ const handleElements = () => {
120
+ const imageElement = spanRef.current?.querySelector('img');
121
+ if (imageElement) {
122
+ imageElement.onload = handleImageLoad;
123
+ } else {
124
+ setTimeout(() => {
125
+ updateDimensions();
126
+ }, 300);
127
+ }
128
+ };
129
+
130
+ const getMeasureNode = () => {
131
+ if (!spanRef.current) {
132
+ return null;
133
+ }
134
+
135
+ const mjx = spanRef.current.querySelector('mjx-container');
136
+
137
+ if (mjx && spanRef.current.parentElement) {
138
+ return spanRef.current.parentElement;
139
+ }
140
+
141
+ const img = spanRef.current.querySelector('img');
142
+
143
+ if (img) {
144
+ // If there's text alongside the image, measure the full span to capture both dimensions
145
+ const hasTextContent = spanRef.current.textContent.trim().length > 0;
146
+
147
+ return hasTextContent ? spanRef.current : img;
148
+ }
149
+
150
+ return spanRef.current;
151
+ };
152
+
153
+ const updateDimensions = () => {
154
+ if (spanRef.current && rootRef.current && !measuringRef.current) {
155
+ measuringRef.current = true;
156
+ // Temporarily set rootRef width to 'auto' for natural measurement
157
+ rootRef.current.style.width = 'auto';
158
+ rootRef.current.style.height = 'auto';
159
+ rootRef.current.offsetHeight;
160
+
161
+ const measureNode = getMeasureNode();
162
+ const node = measureNode || spanRef.current;
163
+ const rect = node.getBoundingClientRect();
164
+ const width = node.offsetWidth || rect.width || 0;
165
+ const height = Math.max(
166
+ node.offsetHeight || 0,
167
+ rect.height || 0,
168
+ node.scrollHeight || 0,
169
+ spanRef.current.scrollHeight || 0,
170
+ );
171
+
172
+ const PADDING = 12;
173
+ const BORDER_WIDTH = 2;
174
+ const ADDITIONAL_SPACE = 1;
175
+ // padding and border on each side
176
+ const widthWithPadding = width + 2 * PADDING + 2 * BORDER_WIDTH + ADDITIONAL_SPACE;
177
+ // padding and border on top and bottom
178
+ const heightWithPadding = height + 2 * PADDING + 2 * BORDER_WIDTH + ADDITIONAL_SPACE;
179
+
180
+ const responseAreaWidth = parseFloat(emptyResponseAreaWidth) || 0;
181
+ const responseAreaHeight = parseFloat(emptyResponseAreaHeight) || 0;
182
+
183
+ const adjustedWidth = widthWithPadding <= responseAreaWidth ? responseAreaWidth : widthWithPadding;
184
+ const adjustedHeight = heightWithPadding <= responseAreaHeight ? responseAreaHeight : heightWithPadding;
185
+
186
+ setDimensions((prevState) => ({
187
+ width: adjustedWidth > responseAreaWidth ? adjustedWidth : prevState.width,
188
+ height: adjustedHeight > responseAreaHeight ? adjustedHeight : prevState.height,
189
+ }));
190
+
191
+ const nextWidth = `${adjustedWidth}px`;
192
+ const nextHeight = `${adjustedHeight}px`;
193
+ if (rootRef.current.style.width !== nextWidth) {
194
+ rootRef.current.style.width = nextWidth;
195
+ }
196
+ if (rootRef.current.style.height !== nextHeight) {
197
+ rootRef.current.style.height = nextHeight;
198
+ }
199
+ measuringRef.current = false;
200
+ }
201
+ };
202
+
203
+ const getRootDimensions = () => {
204
+ // Handle potential non-numeric values
205
+ const responseAreaWidth = !isNaN(parseFloat(emptyResponseAreaWidth)) ? parseFloat(emptyResponseAreaWidth) : 0;
206
+ const responseAreaHeight = !isNaN(parseFloat(emptyResponseAreaHeight)) ? parseFloat(emptyResponseAreaHeight) : 0;
207
+
208
+ const rootStyle = {
209
+ height: dimensions.height || responseAreaHeight,
210
+ width: dimensions.width || responseAreaWidth,
211
+ };
212
+
213
+ // add minWidth, minHeight if width and height are not defined
214
+ return {
215
+ ...rootStyle,
216
+ ...(responseAreaWidth ? {} : { minWidth: 90 }),
217
+ ...(responseAreaHeight ? {} : { minHeight: 32 }),
218
+ };
219
+ };
220
+
221
+ useEffect(() => {
222
+ handleElements();
223
+ }, []);
224
+
225
+ // Re-measure when the element first becomes visible — covers the tabbed-view case
226
+ // where the initial measurement happened while the tab was hidden (size 0).
227
+ useEffect(() => {
228
+ if (typeof IntersectionObserver === 'undefined' || !rootRef.current) return undefined;
229
+ const io = new IntersectionObserver(
230
+ (entries) => {
231
+ if (entries.some((e) => e.isIntersecting)) updateDimensions();
232
+ },
233
+ { threshold: 0 },
234
+ );
235
+ io.observe(rootRef.current);
236
+ return () => io.disconnect();
237
+ }, []);
238
+
239
+ // Render math for the placeholder/preview when dragging over
240
+ useEffect(() => {
241
+ if (rootRef.current) {
242
+ renderMath(rootRef.current);
243
+ }
244
+ }, [correct, isOver, dragItem?.choice?.value]);
245
+
246
+ useEffect(() => {
247
+ if (!choice) {
248
+ setDimensions({ height: 0, width: 0 });
249
+ return;
250
+ }
251
+ handleElements();
252
+ }, [choice?.value]);
253
+
254
+ useEffect(() => {
255
+ if (!isOver && !isDragging) {
256
+ frozenRef.current = {
257
+ width: rootRef.current.offsetWidth,
258
+ height: rootRef.current.offsetHeight,
259
+ };
260
+ }
261
+ }, [choice, isOver, isDragging]);
262
+
263
+ const draggedLabel = dragItem && isOver && dragItem.choice && dragItem.choice.value;
264
+ const label = choice && choice.value;
265
+ const style =
266
+ isOver || isDragging
267
+ ? {
268
+ width: frozenRef.current?.width,
269
+ height: frozenRef.current?.height,
270
+ }
271
+ : getRootDimensions();
272
+
273
+ return (
274
+ <StyledChip
275
+ clickable={false}
276
+ disabled={disabled}
277
+ ref={rootRef}
278
+ component="span"
279
+ label={
280
+ <React.Fragment>
281
+ <StyledChipLabel
282
+ ref={spanRef}
283
+ draggable={true}
284
+ className={classnames({
285
+ over: isOver,
286
+ hidden: draggedLabel,
287
+ })}
288
+ dangerouslySetInnerHTML={{ __html: label || '' }}
289
+ />
290
+ {draggedLabel && (
291
+ <StyledChipLabel
292
+ draggable={true}
293
+ className={classnames({
294
+ over: isOver,
295
+ dragged: true,
296
+ })}
297
+ dangerouslySetInnerHTML={{ __html: draggedLabel || '' }}
298
+ />
299
+ )}
300
+ </React.Fragment>
301
+ }
302
+ className={classnames({
303
+ over: isOver,
304
+ parentOver: isOver,
305
+ correct: correct !== undefined && correct,
306
+ incorrect: correct !== undefined && !correct,
307
+ })}
308
+ variant={disabled ? 'outlined' : undefined}
309
+ style={style}
310
+ />
311
+ );
312
+ }
313
+
314
+ BlankContent.defaultProps = {
315
+ emptyResponseAreaWidth: 0,
316
+ emptyResponseAreaHeight: 0,
317
+ };
318
+
319
+ BlankContent.propTypes = {
320
+ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
321
+ disabled: PropTypes.bool,
322
+ duplicates: PropTypes.bool,
323
+ choice: PropTypes.object,
324
+ isOver: PropTypes.bool,
325
+ dragItem: PropTypes.object,
326
+ correct: PropTypes.bool,
327
+ onChange: PropTypes.func,
328
+ emptyResponseAreaWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
329
+ emptyResponseAreaHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
330
+ instanceId: PropTypes.string,
331
+ };
332
+
333
+ // New functional component using @dnd-kit hooks
334
+ function DragDropBlank({
335
+ id,
336
+ disabled,
337
+ duplicates,
338
+ choice,
339
+ correct,
340
+ onChange,
341
+ emptyResponseAreaWidth,
342
+ emptyResponseAreaHeight,
343
+ instanceId,
344
+ }) {
345
+ // Setup draggable functionality
346
+ const {
347
+ attributes: dragAttributes,
348
+ listeners: dragListeners,
349
+ setNodeRef: setDragNodeRef,
350
+ transform,
351
+ isDragging,
352
+ } = useDraggable({
353
+ id: `mask-blank-drag-${id}`,
354
+ disabled: disabled || !choice,
355
+ data: {
356
+ id: id,
357
+ choice: choice,
358
+ instanceId: instanceId,
359
+ fromChoice: false, // This is from a blank, not from choices
360
+ type: 'MaskBlank',
361
+ },
362
+ });
363
+
364
+ // Setup droppable functionality
365
+ const {
366
+ setNodeRef: setDropNodeRef,
367
+ isOver,
368
+ active: dragItem,
369
+ } = useDroppable({
370
+ id: `mask-blank-drop-${id}`,
371
+ data: {
372
+ id: id,
373
+ accepts: ['MaskBlank'],
374
+ instanceId: instanceId,
375
+ },
376
+ });
377
+
378
+ // Combine refs for both drag and drop
379
+ const setNodeRef = (node) => {
380
+ setDragNodeRef(node);
381
+ setDropNodeRef(node);
382
+ };
383
+
384
+ const style = {
385
+ transform: CSS.Translate.toString(transform),
386
+ };
387
+
388
+ return (
389
+ <StyledContent
390
+ ref={setNodeRef}
391
+ style={style}
392
+ dragged={isDragging}
393
+ over={isOver}
394
+ {...dragAttributes}
395
+ {...dragListeners}
396
+ >
397
+ <BlankContent
398
+ id={id}
399
+ disabled={disabled}
400
+ duplicates={duplicates}
401
+ choice={choice}
402
+ isOver={isOver}
403
+ dragItem={dragItem?.data?.current}
404
+ correct={correct}
405
+ onChange={onChange}
406
+ emptyResponseAreaWidth={emptyResponseAreaWidth}
407
+ emptyResponseAreaHeight={emptyResponseAreaHeight}
408
+ instanceId={instanceId}
409
+ />
410
+ </StyledContent>
411
+ );
412
+ }
413
+
414
+ DragDropBlank.defaultProps = {
415
+ emptyResponseAreaWidth: 0,
416
+ emptyResponseAreaHeight: 0,
417
+ };
418
+
419
+ DragDropBlank.propTypes = {
420
+ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
421
+ disabled: PropTypes.bool,
422
+ duplicates: PropTypes.bool,
423
+ choice: PropTypes.object,
424
+ correct: PropTypes.bool,
425
+ onChange: PropTypes.func,
426
+ emptyResponseAreaWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
427
+ emptyResponseAreaHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
428
+ instanceId: PropTypes.string,
429
+ };
430
+
431
+ export default DragDropBlank;