@pie-lib/mask-markup 3.0.4-next.32 → 3.0.4-next.34

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 (107) 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 +116 -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 +371 -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 +20 -39
  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 +75 -0
  42. package/src/choices/choice.jsx +97 -0
  43. package/src/choices/index.jsx +64 -0
  44. package/src/componentize.js +13 -0
  45. package/src/components/__tests__/blank.test.js +199 -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 +386 -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/clsx@2.1.1/node_modules/clsx/dist/clsx.js +0 -16
  87. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/index.js +0 -17
  88. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssPrefix.js +0 -9
  89. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssUnitless.js +0 -26
  90. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/hasOwn.js +0 -11
  91. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isFunction.js +0 -11
  92. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isObject.js +0 -11
  93. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixInfo.js +0 -24
  94. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixProperties.js +0 -32
  95. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixer.js +0 -29
  96. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/camelize.js +0 -14
  97. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenRe.js +0 -8
  98. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenate.js +0 -12
  99. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/separate.js +0 -11
  100. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toLowerFirst.js +0 -10
  101. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toUpperFirst.js +0 -10
  102. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleObject.js +0 -55
  103. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleString.js +0 -16
  104. package/dist/serialization.d.ts +0 -34
  105. package/dist/serialization.js +0 -132
  106. package/dist/with-mask.d.ts +0 -55
  107. package/dist/with-mask.js +0 -45
@@ -0,0 +1,386 @@
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
+ '&.over': {
80
+ whiteSpace: 'nowrap',
81
+ overflow: 'hidden',
82
+ },
83
+ '&.hidden': {
84
+ color: 'transparent',
85
+ opacity: 0,
86
+ },
87
+ '&.dragged': {
88
+ position: 'absolute',
89
+ left: 16,
90
+ maxWidth: '60px',
91
+ top: '50%',
92
+ transform: 'translateY(-50%)',
93
+ },
94
+ }));
95
+
96
+ function BlankContent({
97
+ disabled,
98
+ choice,
99
+ isOver,
100
+ isDragging,
101
+ dragItem,
102
+ correct,
103
+ emptyResponseAreaWidth,
104
+ emptyResponseAreaHeight,
105
+ }) {
106
+ const rootRef = useRef(null);
107
+ const spanRef = useRef(null);
108
+ const frozenRef = useRef(null); // to use during dragging to prevent flickering
109
+ const [dimensions, setDimensions] = useState({ height: 0, width: 0 });
110
+
111
+ const handleImageLoad = () => {
112
+ updateDimensions();
113
+ };
114
+
115
+ const handleElements = () => {
116
+ const imageElement = spanRef.current?.querySelector('img');
117
+ if (imageElement) {
118
+ imageElement.onload = handleImageLoad;
119
+ } else {
120
+ setTimeout(() => {
121
+ updateDimensions();
122
+ }, 300);
123
+ }
124
+ };
125
+
126
+ const getMeasureNode = () => {
127
+ if (!spanRef.current) return null;
128
+ const mjx = spanRef.current.querySelector('mjx-container');
129
+ if (mjx && spanRef.current.parentElement) return spanRef.current.parentElement;
130
+ const img = spanRef.current.querySelector('img');
131
+ if (img) return img;
132
+ return spanRef.current;
133
+ };
134
+
135
+ const updateDimensions = () => {
136
+ if (spanRef.current && rootRef.current) {
137
+ // Temporarily set rootRef width to 'auto' for natural measurement
138
+ rootRef.current.style.width = 'auto';
139
+ rootRef.current.style.height = 'auto';
140
+ rootRef.current.offsetHeight;
141
+
142
+ const measureNode = getMeasureNode();
143
+ const node = measureNode || spanRef.current;
144
+ const rect = node.getBoundingClientRect();
145
+ const width = rect.width || node.offsetWidth || 0;
146
+ const height = Math.max(
147
+ rect.height || 0,
148
+ node.offsetHeight || 0,
149
+ node.scrollHeight || 0,
150
+ spanRef.current.scrollHeight || 0,
151
+ );
152
+
153
+ const widthWithPadding = width + 24; // 12px padding on each side
154
+ const heightWithPadding = height + 24; // 12px padding on top and bottom
155
+
156
+ const responseAreaWidth = parseFloat(emptyResponseAreaWidth) || 0;
157
+ const responseAreaHeight = parseFloat(emptyResponseAreaHeight) || 0;
158
+
159
+ const adjustedWidth = widthWithPadding <= responseAreaWidth ? responseAreaWidth : widthWithPadding;
160
+ const adjustedHeight = heightWithPadding <= responseAreaHeight ? responseAreaHeight : heightWithPadding;
161
+
162
+ setDimensions((prevState) => ({
163
+ width: adjustedWidth > responseAreaWidth ? adjustedWidth : prevState.width,
164
+ height: adjustedHeight > responseAreaHeight ? adjustedHeight : prevState.height,
165
+ }));
166
+
167
+ rootRef.current.style.width = `${adjustedWidth}px`;
168
+ rootRef.current.style.height = `${adjustedHeight}px`;
169
+ }
170
+ };
171
+
172
+ const getRootDimensions = () => {
173
+ // Handle potential non-numeric values
174
+ const responseAreaWidth = !isNaN(parseFloat(emptyResponseAreaWidth)) ? parseFloat(emptyResponseAreaWidth) : 0;
175
+ const responseAreaHeight = !isNaN(parseFloat(emptyResponseAreaHeight)) ? parseFloat(emptyResponseAreaHeight) : 0;
176
+
177
+ const rootStyle = {
178
+ height: dimensions.height || responseAreaHeight,
179
+ width: dimensions.width || responseAreaWidth,
180
+ };
181
+
182
+ // add minWidth, minHeight if width and height are not defined
183
+ return {
184
+ ...rootStyle,
185
+ ...(responseAreaWidth ? {} : { minWidth: 90 }),
186
+ ...(responseAreaHeight ? {} : { minHeight: 32 }),
187
+ };
188
+ };
189
+
190
+ useEffect(() => {
191
+ handleElements();
192
+ }, []);
193
+
194
+ // Render math for the placeholder/preview when dragging over
195
+ useEffect(() => {
196
+ if (rootRef.current) {
197
+ renderMath(rootRef.current);
198
+ }
199
+ }, [correct, isOver, dragItem?.choice?.value]);
200
+
201
+ useEffect(() => {
202
+ if (!choice) {
203
+ setDimensions({ height: 0, width: 0 });
204
+ return;
205
+ }
206
+ handleElements();
207
+ }, [choice]);
208
+
209
+ useEffect(() => {
210
+ if (!isOver && !isDragging) {
211
+ frozenRef.current = {
212
+ width: rootRef.current.offsetWidth,
213
+ height: rootRef.current.offsetHeight,
214
+ };
215
+ }
216
+ }, [choice, isOver, isDragging]);
217
+
218
+ const draggedLabel = dragItem && isOver && dragItem.choice && dragItem.choice.value;
219
+ const label = choice && choice.value;
220
+ const style =
221
+ isOver || isDragging
222
+ ? {
223
+ width: frozenRef.current?.width,
224
+ height: frozenRef.current?.height,
225
+ }
226
+ : getRootDimensions();
227
+
228
+ return (
229
+ <StyledChip
230
+ clickable={false}
231
+ disabled={disabled}
232
+ ref={rootRef}
233
+ component="span"
234
+ label={
235
+ <React.Fragment>
236
+ <StyledChipLabel
237
+ ref={spanRef}
238
+ draggable={true}
239
+ className={classnames({
240
+ over: isOver,
241
+ hidden: draggedLabel,
242
+ })}
243
+ dangerouslySetInnerHTML={{ __html: label || '' }}
244
+ />
245
+ {draggedLabel && (
246
+ <StyledChipLabel
247
+ draggable={true}
248
+ className={classnames({
249
+ over: isOver,
250
+ dragged: true,
251
+ })}
252
+ dangerouslySetInnerHTML={{ __html: draggedLabel || '' }}
253
+ />
254
+ )}
255
+ </React.Fragment>
256
+ }
257
+ className={classnames({
258
+ over: isOver,
259
+ parentOver: isOver,
260
+ correct: correct !== undefined && correct,
261
+ incorrect: correct !== undefined && !correct,
262
+ })}
263
+ variant={disabled ? 'outlined' : undefined}
264
+ style={style}
265
+ />
266
+ );
267
+ }
268
+
269
+ BlankContent.defaultProps = {
270
+ emptyResponseAreaWidth: 0,
271
+ emptyResponseAreaHeight: 0,
272
+ };
273
+
274
+ BlankContent.propTypes = {
275
+ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
276
+ disabled: PropTypes.bool,
277
+ duplicates: PropTypes.bool,
278
+ choice: PropTypes.object,
279
+ isOver: PropTypes.bool,
280
+ dragItem: PropTypes.object,
281
+ correct: PropTypes.bool,
282
+ onChange: PropTypes.func,
283
+ emptyResponseAreaWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
284
+ emptyResponseAreaHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
285
+ instanceId: PropTypes.string,
286
+ };
287
+
288
+ // New functional component using @dnd-kit hooks
289
+ function DragDropBlank({
290
+ id,
291
+ disabled,
292
+ duplicates,
293
+ choice,
294
+ correct,
295
+ onChange,
296
+ emptyResponseAreaWidth,
297
+ emptyResponseAreaHeight,
298
+ instanceId,
299
+ }) {
300
+ // Setup draggable functionality
301
+ const {
302
+ attributes: dragAttributes,
303
+ listeners: dragListeners,
304
+ setNodeRef: setDragNodeRef,
305
+ transform,
306
+ isDragging,
307
+ } = useDraggable({
308
+ id: `mask-blank-drag-${id}`,
309
+ disabled: disabled || !choice,
310
+ data: {
311
+ id: id,
312
+ choice: choice,
313
+ instanceId: instanceId,
314
+ fromChoice: false, // This is from a blank, not from choices
315
+ type: 'MaskBlank',
316
+ },
317
+ });
318
+
319
+ // Setup droppable functionality
320
+ const {
321
+ setNodeRef: setDropNodeRef,
322
+ isOver,
323
+ active: dragItem,
324
+ } = useDroppable({
325
+ id: `mask-blank-drop-${id}`,
326
+ data: {
327
+ id: id,
328
+ accepts: ['MaskBlank'],
329
+ instanceId: instanceId,
330
+ },
331
+ });
332
+
333
+ // Combine refs for both drag and drop
334
+ const setNodeRef = (node) => {
335
+ setDragNodeRef(node);
336
+ setDropNodeRef(node);
337
+ };
338
+
339
+ const style = {
340
+ transform: CSS.Translate.toString(transform),
341
+ };
342
+
343
+ return (
344
+ <StyledContent
345
+ ref={setNodeRef}
346
+ style={style}
347
+ dragged={isDragging}
348
+ over={isOver}
349
+ {...dragAttributes}
350
+ {...dragListeners}
351
+ >
352
+ <BlankContent
353
+ id={id}
354
+ disabled={disabled}
355
+ duplicates={duplicates}
356
+ choice={choice}
357
+ isOver={isOver}
358
+ dragItem={dragItem?.data?.current}
359
+ correct={correct}
360
+ onChange={onChange}
361
+ emptyResponseAreaWidth={emptyResponseAreaWidth}
362
+ emptyResponseAreaHeight={emptyResponseAreaHeight}
363
+ instanceId={instanceId}
364
+ />
365
+ </StyledContent>
366
+ );
367
+ }
368
+
369
+ DragDropBlank.defaultProps = {
370
+ emptyResponseAreaWidth: 0,
371
+ emptyResponseAreaHeight: 0,
372
+ };
373
+
374
+ DragDropBlank.propTypes = {
375
+ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
376
+ disabled: PropTypes.bool,
377
+ duplicates: PropTypes.bool,
378
+ choice: PropTypes.object,
379
+ correct: PropTypes.bool,
380
+ onChange: PropTypes.func,
381
+ emptyResponseAreaWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
382
+ emptyResponseAreaHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
383
+ instanceId: PropTypes.string,
384
+ };
385
+
386
+ export default DragDropBlank;
@@ -0,0 +1,82 @@
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;