@pie-lib/mask-markup 0.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 (77) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +4 -0
  2. package/dist/choices/choice.d.ts +24 -0
  3. package/dist/choices/choice.d.ts.map +1 -0
  4. package/dist/choices/choice.js +77 -0
  5. package/dist/choices/index.d.ts +26 -0
  6. package/dist/choices/index.d.ts.map +1 -0
  7. package/dist/choices/index.js +49 -0
  8. package/dist/componentize.d.ts +13 -0
  9. package/dist/componentize.d.ts.map +1 -0
  10. package/dist/componentize.js +4 -0
  11. package/dist/components/blank.d.ts +39 -0
  12. package/dist/components/blank.d.ts.map +1 -0
  13. package/dist/components/blank.js +236 -0
  14. package/dist/components/correct-input.d.ts +11 -0
  15. package/dist/components/correct-input.d.ts.map +1 -0
  16. package/dist/components/dropdown.d.ts +38 -0
  17. package/dist/components/dropdown.d.ts.map +1 -0
  18. package/dist/components/dropdown.js +309 -0
  19. package/dist/components/input.d.ts +37 -0
  20. package/dist/components/input.d.ts.map +1 -0
  21. package/dist/constructed-response.d.ts +24 -0
  22. package/dist/constructed-response.d.ts.map +1 -0
  23. package/dist/constructed-response.js +55 -0
  24. package/dist/customizable.d.ts +24 -0
  25. package/dist/customizable.d.ts.map +1 -0
  26. package/dist/customizable.js +8 -0
  27. package/dist/drag-in-the-blank.d.ts +38 -0
  28. package/dist/drag-in-the-blank.d.ts.map +1 -0
  29. package/dist/drag-in-the-blank.js +164 -0
  30. package/dist/index.d.ts +16 -0
  31. package/dist/index.d.ts.map +1 -0
  32. package/dist/index.js +7 -0
  33. package/dist/inline-dropdown.d.ts +24 -0
  34. package/dist/inline-dropdown.d.ts.map +1 -0
  35. package/dist/inline-dropdown.js +24 -0
  36. package/dist/mask.d.ts +31 -0
  37. package/dist/mask.d.ts.map +1 -0
  38. package/dist/mask.js +98 -0
  39. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/index.js +17 -0
  40. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssPrefix.js +9 -0
  41. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssUnitless.js +26 -0
  42. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/hasOwn.js +11 -0
  43. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isFunction.js +11 -0
  44. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isObject.js +11 -0
  45. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixInfo.js +24 -0
  46. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixProperties.js +32 -0
  47. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixer.js +29 -0
  48. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/camelize.js +14 -0
  49. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenRe.js +8 -0
  50. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenate.js +12 -0
  51. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/separate.js +11 -0
  52. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toLowerFirst.js +10 -0
  53. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toUpperFirst.js +10 -0
  54. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleObject.js +55 -0
  55. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleString.js +16 -0
  56. package/dist/serialization.d.ts +35 -0
  57. package/dist/serialization.d.ts.map +1 -0
  58. package/dist/serialization.js +132 -0
  59. package/dist/with-mask.d.ts +35 -0
  60. package/dist/with-mask.d.ts.map +1 -0
  61. package/dist/with-mask.js +45 -0
  62. package/package.json +44 -0
  63. package/src/choices/choice.tsx +107 -0
  64. package/src/choices/index.tsx +74 -0
  65. package/src/componentize.tsx +23 -0
  66. package/src/components/blank.tsx +396 -0
  67. package/src/components/correct-input.tsx +92 -0
  68. package/src/components/dropdown.tsx +448 -0
  69. package/src/components/input.tsx +58 -0
  70. package/src/constructed-response.tsx +91 -0
  71. package/src/customizable.tsx +44 -0
  72. package/src/drag-in-the-blank.tsx +251 -0
  73. package/src/index.ts +26 -0
  74. package/src/inline-dropdown.tsx +39 -0
  75. package/src/mask.tsx +178 -0
  76. package/src/serialization.ts +270 -0
  77. package/src/with-mask.tsx +85 -0
@@ -0,0 +1,396 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-lib/packages/mask-markup/src/components/blank.jsx
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import React, { useEffect, useRef, useState } from 'react';
12
+ import PropTypes from 'prop-types';
13
+ import { renderMath } from '@pie-element/shared-math-rendering-mathjax';
14
+ import debug from 'debug';
15
+ import { useDraggable, useDroppable } from '@dnd-kit/core';
16
+ import { CSS } from '@dnd-kit/utilities';
17
+ import { styled } from '@mui/material/styles';
18
+ import Chip from '@mui/material/Chip';
19
+ import classnames from 'classnames';
20
+ import { color } from '@pie-lib/render-ui';
21
+ import { grey } from '@mui/material/colors';
22
+
23
+ const log = debug('pie-lib:mask-markup:blank');
24
+
25
+ const StyledContent: any = styled('span')(({ dragged, over }) => ({
26
+ border: `solid 0px ${color.primary()}`,
27
+ minWidth: '200px',
28
+ touchAction: 'none',
29
+ overflow: 'hidden',
30
+ whiteSpace: 'nowrap',
31
+ opacity: 1,
32
+ ...(over && {
33
+ whiteSpace: 'nowrap',
34
+ overflow: 'hidden',
35
+ }),
36
+ ...(dragged && {
37
+ opacity: 0.5,
38
+ }),
39
+ }));
40
+
41
+ const StyledChip: any = styled(Chip)(() => ({
42
+ backgroundColor: color.background(),
43
+ border: `2px dashed ${color.text()}`,
44
+ touchAction: 'none',
45
+ color: color.text(),
46
+ fontSize: 'inherit',
47
+ maxWidth: '374px',
48
+ position: 'relative',
49
+ borderRadius: '3px',
50
+ '&.over': {
51
+ whiteSpace: 'nowrap',
52
+ overflow: 'hidden',
53
+ },
54
+ '&.parentOver': {
55
+ border: `1px solid ${grey[500]}`,
56
+ backgroundColor: `${grey[300]}`,
57
+ },
58
+ '&.correct': {
59
+ border: `solid 1px ${color.correct()}`,
60
+ },
61
+ '&.incorrect': {
62
+ border: `solid 1px ${color.incorrect()}`,
63
+ },
64
+ '&.Mui-disabled': {
65
+ opacity: 1,
66
+ },
67
+ }));
68
+
69
+ const StyledChipLabel: any = styled('span')(() => ({
70
+ whiteSpace: 'normal',
71
+ // Added for touch devices, for image content.
72
+ // This will prevent the context menu from appearing and not allowing other interactions with the image.
73
+ // If interactions with the image in the token will be requested we should handle only the context Menu.
74
+ pointerEvents: 'none',
75
+ '& img': {
76
+ display: 'block',
77
+ padding: '2px 0',
78
+ },
79
+ // Remove default <p> margins to ensure consistent spacing across all wrapped content (p, span, div, math)
80
+ // Padding for top and bottom will instead be controlled by the container for consistent layout
81
+ // Ensures consistent behavior with pie-api-browser, where marginTop is already removed by a Bootstrap stylesheet
82
+ '& p': {
83
+ marginTop: '0',
84
+ marginBottom: '0',
85
+ },
86
+ '& mjx-frac': {
87
+ fontSize: '120% !important',
88
+ },
89
+ '&.over': {
90
+ whiteSpace: 'nowrap',
91
+ overflow: 'hidden',
92
+ },
93
+ '&.hidden': {
94
+ color: 'transparent',
95
+ opacity: 0,
96
+ },
97
+ '&.dragged': {
98
+ position: 'absolute',
99
+ left: 16,
100
+ maxWidth: '60px',
101
+ top: '50%',
102
+ transform: 'translateY(-50%)',
103
+ },
104
+ }));
105
+
106
+ function BlankContent({
107
+ disabled,
108
+ choice,
109
+ isOver,
110
+ isDragging,
111
+ dragItem,
112
+ correct,
113
+ emptyResponseAreaWidth,
114
+ emptyResponseAreaHeight,
115
+ }) {
116
+ const rootRef = useRef(null);
117
+ const spanRef = useRef(null);
118
+ const frozenRef = useRef(null); // to use during dragging to prevent flickering
119
+ const [dimensions, setDimensions] = useState({ height: 0, width: 0 });
120
+
121
+ const handleImageLoad = () => {
122
+ updateDimensions();
123
+ };
124
+
125
+ const handleElements = () => {
126
+ const imageElement = spanRef.current?.querySelector('img');
127
+ if (imageElement) {
128
+ imageElement.onload = handleImageLoad;
129
+ } else {
130
+ setTimeout(() => {
131
+ updateDimensions();
132
+ }, 300);
133
+ }
134
+ };
135
+
136
+ const getMeasureNode = () => {
137
+ if (!spanRef.current) return null;
138
+ const mjx = spanRef.current.querySelector('mjx-container');
139
+ if (mjx && spanRef.current.parentElement) return spanRef.current.parentElement;
140
+ const img = spanRef.current.querySelector('img');
141
+ if (img) return img;
142
+ return spanRef.current;
143
+ };
144
+
145
+ const updateDimensions = () => {
146
+ if (spanRef.current && rootRef.current) {
147
+ // Temporarily set rootRef width to 'auto' for natural measurement
148
+ rootRef.current.style.width = 'auto';
149
+ rootRef.current.style.height = 'auto';
150
+ rootRef.current.offsetHeight;
151
+
152
+ const measureNode = getMeasureNode();
153
+ const node = measureNode || spanRef.current;
154
+ const rect = node.getBoundingClientRect();
155
+ const width = rect.width || node.offsetWidth || 0;
156
+ const height = Math.max(
157
+ rect.height || 0,
158
+ node.offsetHeight || 0,
159
+ node.scrollHeight || 0,
160
+ spanRef.current.scrollHeight || 0,
161
+ );
162
+
163
+ const widthWithPadding = width + 24; // 12px padding on each side
164
+ const heightWithPadding = height + 24; // 12px padding on top and bottom
165
+
166
+ const responseAreaWidth = parseFloat(emptyResponseAreaWidth) || 0;
167
+ const responseAreaHeight = parseFloat(emptyResponseAreaHeight) || 0;
168
+
169
+ const adjustedWidth = widthWithPadding <= responseAreaWidth ? responseAreaWidth : widthWithPadding;
170
+ const adjustedHeight = heightWithPadding <= responseAreaHeight ? responseAreaHeight : heightWithPadding;
171
+
172
+ setDimensions((prevState) => ({
173
+ width: adjustedWidth > responseAreaWidth ? adjustedWidth : prevState.width,
174
+ height: adjustedHeight > responseAreaHeight ? adjustedHeight : prevState.height,
175
+ }));
176
+
177
+ rootRef.current.style.width = `${adjustedWidth}px`;
178
+ rootRef.current.style.height = `${adjustedHeight}px`;
179
+ }
180
+ };
181
+
182
+ const getRootDimensions = () => {
183
+ // Handle potential non-numeric values
184
+ const responseAreaWidth = !isNaN(parseFloat(emptyResponseAreaWidth)) ? parseFloat(emptyResponseAreaWidth) : 0;
185
+ const responseAreaHeight = !isNaN(parseFloat(emptyResponseAreaHeight)) ? parseFloat(emptyResponseAreaHeight) : 0;
186
+
187
+ const rootStyle = {
188
+ height: dimensions.height || responseAreaHeight,
189
+ width: dimensions.width || responseAreaWidth,
190
+ };
191
+
192
+ // add minWidth, minHeight if width and height are not defined
193
+ return {
194
+ ...rootStyle,
195
+ ...(responseAreaWidth ? {} : { minWidth: 90 }),
196
+ ...(responseAreaHeight ? {} : { minHeight: 32 }),
197
+ };
198
+ };
199
+
200
+ useEffect(() => {
201
+ handleElements();
202
+ }, []);
203
+
204
+ // Render math for the placeholder/preview when dragging over
205
+ useEffect(() => {
206
+ if (rootRef.current) {
207
+ renderMath(rootRef.current);
208
+ }
209
+ }, [isOver, dragItem?.choice?.value]);
210
+
211
+ useEffect(() => {
212
+ if (!choice) {
213
+ setDimensions({ height: 0, width: 0 });
214
+ return;
215
+ }
216
+ handleElements();
217
+ }, [choice]);
218
+
219
+ useEffect(() => {
220
+ if (!isOver && !isDragging) {
221
+ frozenRef.current = {
222
+ width: rootRef.current.offsetWidth,
223
+ height: rootRef.current.offsetHeight,
224
+ };
225
+ }
226
+ }, [choice, isOver, isDragging]);
227
+
228
+ const draggedLabel = dragItem && isOver && dragItem.choice && dragItem.choice.value;
229
+ const label = choice && choice.value;
230
+ const style =
231
+ isOver || isDragging
232
+ ? {
233
+ width: frozenRef.current?.width,
234
+ height: frozenRef.current?.height,
235
+ }
236
+ : getRootDimensions();
237
+
238
+ return (
239
+ <StyledChip
240
+ clickable={false}
241
+ disabled={disabled}
242
+ ref={rootRef}
243
+ component="span"
244
+ label={
245
+ <React.Fragment>
246
+ <StyledChipLabel
247
+ ref={spanRef}
248
+ draggable={true}
249
+ className={classnames({
250
+ over: isOver,
251
+ hidden: draggedLabel,
252
+ })}
253
+ dangerouslySetInnerHTML={{ __html: label || '' }}
254
+ />
255
+ {draggedLabel && (
256
+ <StyledChipLabel
257
+ draggable={true}
258
+ className={classnames({
259
+ over: isOver,
260
+ dragged: true,
261
+ })}
262
+ dangerouslySetInnerHTML={{ __html: draggedLabel || '' }}
263
+ />
264
+ )}
265
+ </React.Fragment>
266
+ }
267
+ className={classnames({
268
+ over: isOver,
269
+ parentOver: isOver,
270
+ correct: correct !== undefined && correct,
271
+ incorrect: correct !== undefined && !correct,
272
+ })}
273
+ variant={disabled ? 'outlined' : undefined}
274
+ style={style}
275
+ />
276
+ );
277
+ }
278
+
279
+ BlankContent.defaultProps = {
280
+ emptyResponseAreaWidth: 0,
281
+ emptyResponseAreaHeight: 0,
282
+ };
283
+
284
+ BlankContent.propTypes = {
285
+ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
286
+ disabled: PropTypes.bool,
287
+ duplicates: PropTypes.bool,
288
+ choice: PropTypes.object,
289
+ isOver: PropTypes.bool,
290
+ dragItem: PropTypes.object,
291
+ correct: PropTypes.bool,
292
+ onChange: PropTypes.func,
293
+ emptyResponseAreaWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
294
+ emptyResponseAreaHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
295
+ instanceId: PropTypes.string,
296
+ };
297
+
298
+ // New functional component using @dnd-kit hooks
299
+ function DragDropBlank({
300
+ id,
301
+ disabled,
302
+ duplicates,
303
+ choice,
304
+ correct,
305
+ onChange,
306
+ emptyResponseAreaWidth,
307
+ emptyResponseAreaHeight,
308
+ instanceId,
309
+ }) {
310
+ // Setup draggable functionality
311
+ const {
312
+ attributes: dragAttributes,
313
+ listeners: dragListeners,
314
+ setNodeRef: setDragNodeRef,
315
+ transform,
316
+ isDragging,
317
+ } = useDraggable({
318
+ id: `mask-blank-drag-${id}`,
319
+ disabled: disabled || !choice,
320
+ data: {
321
+ id: id,
322
+ choice: choice,
323
+ instanceId: instanceId,
324
+ fromChoice: false, // This is from a blank, not from choices
325
+ type: 'MaskBlank',
326
+ },
327
+ });
328
+
329
+ // Setup droppable functionality
330
+ const {
331
+ setNodeRef: setDropNodeRef,
332
+ isOver,
333
+ active: dragItem,
334
+ } = useDroppable({
335
+ id: `mask-blank-drop-${id}`,
336
+ data: {
337
+ id: id,
338
+ accepts: ['MaskBlank'],
339
+ instanceId: instanceId,
340
+ },
341
+ });
342
+
343
+ // Combine refs for both drag and drop
344
+ const setNodeRef = (node) => {
345
+ setDragNodeRef(node);
346
+ setDropNodeRef(node);
347
+ };
348
+
349
+ const style = {
350
+ transform: CSS.Translate.toString(transform),
351
+ };
352
+
353
+ return (
354
+ <StyledContent
355
+ ref={setNodeRef}
356
+ style={style}
357
+ dragged={isDragging}
358
+ over={isOver}
359
+ {...dragAttributes}
360
+ {...dragListeners}
361
+ >
362
+ <BlankContent
363
+ id={id}
364
+ disabled={disabled}
365
+ duplicates={duplicates}
366
+ choice={choice}
367
+ isOver={isOver}
368
+ dragItem={dragItem?.data?.current}
369
+ correct={correct}
370
+ onChange={onChange}
371
+ emptyResponseAreaWidth={emptyResponseAreaWidth}
372
+ emptyResponseAreaHeight={emptyResponseAreaHeight}
373
+ instanceId={instanceId}
374
+ />
375
+ </StyledContent>
376
+ );
377
+ }
378
+
379
+ DragDropBlank.defaultProps = {
380
+ emptyResponseAreaWidth: 0,
381
+ emptyResponseAreaHeight: 0,
382
+ };
383
+
384
+ DragDropBlank.propTypes = {
385
+ id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
386
+ disabled: PropTypes.bool,
387
+ duplicates: PropTypes.bool,
388
+ choice: PropTypes.object,
389
+ correct: PropTypes.bool,
390
+ onChange: PropTypes.func,
391
+ emptyResponseAreaWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
392
+ emptyResponseAreaHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
393
+ instanceId: PropTypes.string,
394
+ };
395
+
396
+ export default DragDropBlank;
@@ -0,0 +1,92 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-lib/packages/mask-markup/src/components/correct-input.jsx
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import React from 'react';
12
+ import OutlinedInput from '@mui/material/OutlinedInput';
13
+ import classnames from 'classnames';
14
+ import { styled } from '@mui/material/styles';
15
+ import { color } from '@pie-lib/render-ui';
16
+
17
+ const StyledOutlinedInput: any = styled(OutlinedInput)(() => ({
18
+ padding: '2px',
19
+ borderRadius: '4px',
20
+ fontSize: 'inherit',
21
+ display: 'inline-block',
22
+ verticalAlign: 'middle',
23
+ '& fieldset': {
24
+ border: 0,
25
+ },
26
+ '& .MuiOutlinedInput-input': {
27
+ color: color.text(),
28
+ backgroundColor: color.background(),
29
+ borderRadius: '4px !important',
30
+ borderWidth: '1px',
31
+ borderStyle: 'solid',
32
+ padding: '10px 20px 10px 10px',
33
+ '&:disabled': {
34
+ opacity: 0.8,
35
+ cursor: 'not-allowed !important',
36
+ },
37
+ '&:hover': {
38
+ borderColor: color.primary(),
39
+ '&:disabled': {
40
+ borderColor: 'initial',
41
+ },
42
+ },
43
+ '&.Mui-focused': {
44
+ borderColor: color.primaryDark(),
45
+ },
46
+ '&.crInput': {
47
+ padding: '8px !important',
48
+ },
49
+ '&.correct': {
50
+ borderColor: `${color.correct()} !important`,
51
+ },
52
+ '&.incorrect': {
53
+ borderColor: `${color.incorrect()} !important`,
54
+ },
55
+ },
56
+ }));
57
+
58
+ const CorrectInput = (props) => {
59
+ const { correct, charactersLimit, disabled, isBox, isConstructedResponse, width, spellCheck, ...rest } = props;
60
+
61
+ const label = typeof correct === 'boolean' ? (correct ? 'correct' : 'incorrect') : undefined;
62
+ const inputProps = charactersLimit
63
+ ? { maxLength: charactersLimit, 'aria-label': 'Enter answer' }
64
+ : { 'aria-label': 'Enter answer' };
65
+
66
+ if (width) {
67
+ inputProps.style = {
68
+ width: `${width + Math.round(width / 10) + 1}ch`, // added some extra space for capital letters
69
+ };
70
+ }
71
+
72
+ return (
73
+ <StyledOutlinedInput
74
+ className={classnames({
75
+ disabledInput: disabled,
76
+ box: isBox,
77
+ })}
78
+ classes={{
79
+ input: classnames({
80
+ [label]: label,
81
+ crInput: isConstructedResponse,
82
+ }),
83
+ }}
84
+ inputProps={inputProps}
85
+ disabled={disabled}
86
+ spellCheck={spellCheck}
87
+ {...rest}
88
+ />
89
+ );
90
+ };
91
+
92
+ export default CorrectInput;