@pie-qti/default-components 0.1.9 → 0.1.11

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 (181) hide show
  1. package/dist/chunks/__vite-browser-external-BjvdLQIS.js +9 -0
  2. package/dist/chunks/__vite-browser-external-BjvdLQIS.js.map +1 -0
  3. package/dist/chunks/chunk-Bayd9M4T.js +20 -0
  4. package/dist/chunks/{mathlive.min-Bvtiaxg4.js → mathlive.min-CAPebTxM.js} +245 -29
  5. package/dist/chunks/{mathlive.min-Bvtiaxg4.js.map → mathlive.min-CAPebTxM.js.map} +1 -1
  6. package/dist/index.d.ts +0 -1
  7. package/dist/index.js +1 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/plugins/associate/index.d.ts +0 -1
  10. package/dist/plugins/associate/index.js +1 -0
  11. package/dist/plugins/associate/index.js.map +1 -0
  12. package/dist/plugins/choice/index.d.ts +0 -1
  13. package/dist/plugins/choice/index.js +1 -0
  14. package/dist/plugins/choice/index.js.map +1 -0
  15. package/dist/plugins/custom/index.d.ts +0 -1
  16. package/dist/plugins/custom/index.js +1 -0
  17. package/dist/plugins/custom/index.js.map +1 -0
  18. package/dist/plugins/end-attempt/index.d.ts +0 -1
  19. package/dist/plugins/end-attempt/index.js +1 -0
  20. package/dist/plugins/end-attempt/index.js.map +1 -0
  21. package/dist/plugins/extended-text/index.d.ts +0 -1
  22. package/dist/plugins/extended-text/index.js +1 -0
  23. package/dist/plugins/extended-text/index.js.map +1 -0
  24. package/dist/plugins/gap-match/index.d.ts +0 -1
  25. package/dist/plugins/gap-match/index.js +1 -0
  26. package/dist/plugins/gap-match/index.js.map +1 -0
  27. package/dist/plugins/graphic-associate/index.d.ts +0 -1
  28. package/dist/plugins/graphic-associate/index.js +1 -0
  29. package/dist/plugins/graphic-associate/index.js.map +1 -0
  30. package/dist/plugins/graphic-gap-match/index.d.ts +0 -1
  31. package/dist/plugins/graphic-gap-match/index.js +1 -0
  32. package/dist/plugins/graphic-gap-match/index.js.map +1 -0
  33. package/dist/plugins/graphic-order/index.d.ts +0 -1
  34. package/dist/plugins/graphic-order/index.js +1 -0
  35. package/dist/plugins/graphic-order/index.js.map +1 -0
  36. package/dist/plugins/hotspot/index.d.ts +0 -1
  37. package/dist/plugins/hotspot/index.js +1 -0
  38. package/dist/plugins/hotspot/index.js.map +1 -0
  39. package/dist/plugins/hottext/index.d.ts +0 -1
  40. package/dist/plugins/hottext/index.js +1 -0
  41. package/dist/plugins/hottext/index.js.map +1 -0
  42. package/dist/plugins/match/index.d.ts +0 -1
  43. package/dist/plugins/match/index.js +1 -0
  44. package/dist/plugins/match/index.js.map +1 -0
  45. package/dist/plugins/media/index.d.ts +0 -1
  46. package/dist/plugins/media/index.js +1 -0
  47. package/dist/plugins/media/index.js.map +1 -0
  48. package/dist/plugins/order/index.d.ts +0 -1
  49. package/dist/plugins/order/index.js +1 -0
  50. package/dist/plugins/order/index.js.map +1 -0
  51. package/dist/plugins/position-object/index.d.ts +0 -1
  52. package/dist/plugins/position-object/index.js +1 -0
  53. package/dist/plugins/position-object/index.js.map +1 -0
  54. package/dist/plugins/public-types.d.ts +5 -0
  55. package/dist/plugins/public-types.js +2 -0
  56. package/dist/plugins/public-types.js.map +1 -0
  57. package/dist/plugins/select-point/index.d.ts +0 -1
  58. package/dist/plugins/select-point/index.js +1 -0
  59. package/dist/plugins/select-point/index.js.map +1 -0
  60. package/dist/plugins/slider/index.d.ts +0 -1
  61. package/dist/plugins/slider/index.js +1 -0
  62. package/dist/plugins/slider/index.js.map +1 -0
  63. package/dist/plugins.js +25958 -7690
  64. package/dist/plugins.js.map +1 -1
  65. package/dist/shared/actions/typesetAction.d.ts +0 -1
  66. package/dist/shared/actions/typesetAction.js +1 -0
  67. package/dist/shared/actions/typesetAction.js.map +1 -0
  68. package/dist/shared/extensions/MathLiveEditor.d.ts +0 -1
  69. package/dist/shared/extensions/MathLiveEditor.js +1 -0
  70. package/dist/shared/extensions/MathLiveEditor.js.map +1 -0
  71. package/dist/shared/index.d.ts +0 -2
  72. package/dist/shared/index.js +1 -2
  73. package/dist/shared/index.js.map +1 -0
  74. package/dist/shared/utils/eventHelpers.d.ts +6 -1
  75. package/dist/shared/utils/eventHelpers.js +7 -0
  76. package/dist/shared/utils/eventHelpers.js.map +1 -0
  77. package/dist/shared/utils/matchGroupUtils.d.ts +0 -1
  78. package/dist/shared/utils/matchGroupUtils.js +1 -0
  79. package/dist/shared/utils/matchGroupUtils.js.map +1 -0
  80. package/dist/shared/utils/pairHelpers.d.ts +0 -1
  81. package/dist/shared/utils/pairHelpers.js +1 -0
  82. package/dist/shared/utils/pairHelpers.js.map +1 -0
  83. package/dist/shared/utils/webComponentHelpers.d.ts +16 -1
  84. package/dist/shared/utils/webComponentHelpers.js +13 -0
  85. package/dist/shared/utils/webComponentHelpers.js.map +1 -0
  86. package/package.json +11 -33
  87. package/dist/index.d.ts.map +0 -1
  88. package/dist/plugins/associate/index.d.ts.map +0 -1
  89. package/dist/plugins/choice/index.d.ts.map +0 -1
  90. package/dist/plugins/custom/index.d.ts.map +0 -1
  91. package/dist/plugins/end-attempt/index.d.ts.map +0 -1
  92. package/dist/plugins/extended-text/index.d.ts.map +0 -1
  93. package/dist/plugins/gap-match/index.d.ts.map +0 -1
  94. package/dist/plugins/graphic-associate/index.d.ts.map +0 -1
  95. package/dist/plugins/graphic-gap-match/index.d.ts.map +0 -1
  96. package/dist/plugins/graphic-order/index.d.ts.map +0 -1
  97. package/dist/plugins/hotspot/index.d.ts.map +0 -1
  98. package/dist/plugins/hottext/index.d.ts.map +0 -1
  99. package/dist/plugins/index.d.ts +0 -31
  100. package/dist/plugins/index.d.ts.map +0 -1
  101. package/dist/plugins/index.js +0 -31
  102. package/dist/plugins/match/index.d.ts.map +0 -1
  103. package/dist/plugins/media/index.d.ts.map +0 -1
  104. package/dist/plugins/order/index.d.ts.map +0 -1
  105. package/dist/plugins/position-object/index.d.ts.map +0 -1
  106. package/dist/plugins/select-point/index.d.ts.map +0 -1
  107. package/dist/plugins/slider/index.d.ts.map +0 -1
  108. package/dist/shared/actions/typesetAction.d.ts.map +0 -1
  109. package/dist/shared/extensions/MathLiveEditor.d.ts.map +0 -1
  110. package/dist/shared/index.d.ts.map +0 -1
  111. package/dist/shared/utils/eventHelpers.d.ts.map +0 -1
  112. package/dist/shared/utils/matchGroupUtils.d.ts.map +0 -1
  113. package/dist/shared/utils/pairHelpers.d.ts.map +0 -1
  114. package/dist/shared/utils/touchDragHelper.d.ts +0 -2
  115. package/dist/shared/utils/touchDragHelper.d.ts.map +0 -1
  116. package/dist/shared/utils/touchDragHelper.js +0 -1
  117. package/dist/shared/utils/webComponentHelpers.d.ts.map +0 -1
  118. package/src/catalog/CatalogPopup.svelte +0 -163
  119. package/src/index.ts +0 -141
  120. package/src/plugins/associate/AssociateInteraction.svelte +0 -310
  121. package/src/plugins/associate/index.ts +0 -9
  122. package/src/plugins/choice/ChoiceInteraction.svelte +0 -358
  123. package/src/plugins/choice/index.ts +0 -9
  124. package/src/plugins/custom/CustomInteraction.svelte +0 -61
  125. package/src/plugins/custom/index.ts +0 -9
  126. package/src/plugins/drawing/DrawingInteraction.svelte +0 -99
  127. package/src/plugins/end-attempt/EndAttemptInteraction.svelte +0 -142
  128. package/src/plugins/end-attempt/index.ts +0 -9
  129. package/src/plugins/extended-text/ExtendedTextInteraction.svelte +0 -150
  130. package/src/plugins/extended-text/index.ts +0 -9
  131. package/src/plugins/gap-match/GapMatchInteraction.svelte +0 -556
  132. package/src/plugins/gap-match/index.ts +0 -9
  133. package/src/plugins/graphic-associate/GraphicAssociateInteraction.svelte +0 -503
  134. package/src/plugins/graphic-associate/index.ts +0 -9
  135. package/src/plugins/graphic-gap-match/GraphicGapMatchInteraction.svelte +0 -593
  136. package/src/plugins/graphic-gap-match/index.ts +0 -9
  137. package/src/plugins/graphic-order/GraphicOrderInteraction.svelte +0 -241
  138. package/src/plugins/graphic-order/index.ts +0 -9
  139. package/src/plugins/hotspot/HotspotInteraction.svelte +0 -287
  140. package/src/plugins/hotspot/index.ts +0 -9
  141. package/src/plugins/hottext/HottextInteraction.svelte +0 -325
  142. package/src/plugins/hottext/index.ts +0 -9
  143. package/src/plugins/index.ts +0 -32
  144. package/src/plugins/match/MatchInteraction.svelte +0 -82
  145. package/src/plugins/match/index.ts +0 -9
  146. package/src/plugins/media/MediaInteraction.svelte +0 -216
  147. package/src/plugins/media/index.ts +0 -9
  148. package/src/plugins/order/OrderInteraction.svelte +0 -280
  149. package/src/plugins/order/index.ts +0 -9
  150. package/src/plugins/position-object/PositionObjectInteraction.svelte +0 -903
  151. package/src/plugins/position-object/README.md +0 -130
  152. package/src/plugins/position-object/index.ts +0 -9
  153. package/src/plugins/select-point/SelectPointInteraction.svelte +0 -571
  154. package/src/plugins/select-point/index.ts +0 -9
  155. package/src/plugins/slider/SliderInteraction.svelte +0 -141
  156. package/src/plugins/slider/index.ts +0 -9
  157. package/src/plugins/upload/UploadInteraction.svelte +0 -59
  158. package/src/shared/actions/typesetAction.ts +0 -87
  159. package/src/shared/components/CustomInteractionFallback.svelte +0 -153
  160. package/src/shared/components/DragHandle.svelte +0 -44
  161. package/src/shared/components/DrawingCanvas.svelte +0 -259
  162. package/src/shared/components/FileUpload.svelte +0 -158
  163. package/src/shared/components/GraphicGapMatch.svelte +0 -318
  164. package/src/shared/components/GraphicGapMatchAdapter.svelte +0 -45
  165. package/src/shared/components/InlineInteractionRenderer.svelte +0 -141
  166. package/src/shared/components/MatchDragDrop.svelte +0 -481
  167. package/src/shared/components/MathContent.svelte +0 -18
  168. package/src/shared/components/ModalFeedbackDisplay.svelte +0 -91
  169. package/src/shared/components/RichTextEditor.svelte +0 -316
  170. package/src/shared/components/ShadowBaseStyles.svelte +0 -390
  171. package/src/shared/components/SortableList.svelte +0 -297
  172. package/src/shared/extensions/MathLiveEditor.ts +0 -299
  173. package/src/shared/index.ts +0 -17
  174. package/src/shared/styles/shared.css +0 -16
  175. package/src/shared/utils/eventHelpers.ts +0 -52
  176. package/src/shared/utils/matchGroupUtils.ts +0 -14
  177. package/src/shared/utils/pairHelpers.ts +0 -109
  178. package/src/shared/utils/touchDragHelper.ts +0 -1
  179. package/src/shared/utils/webComponentHelpers.ts +0 -27
  180. package/src/svelte-shims.d.ts +0 -9
  181. /package/{src → dist}/shared/styles/qti-shared-vocabulary.css +0 -0
@@ -1,259 +0,0 @@
1
- <script lang="ts">
2
- /**
3
- * Drawing canvas component for drawingInteraction
4
- *
5
- * Simple freehand drawing on a canvas, emitting a serializable QTIFileResponse (PNG dataUrl).
6
- */
7
-
8
- import type { QTIFileResponse } from '@pie-qti/item-player';
9
- import type { I18nProvider } from '@pie-qti/i18n';
10
-
11
- type ImageData =
12
- | { type: 'svg'; content?: string; width: string; height: string }
13
- | { type: 'image'; src?: string; width: string; height: string };
14
-
15
- interface Props {
16
- responseId: string;
17
- label?: string;
18
- imageData?: ImageData | null;
19
- disabled?: boolean;
20
- value?: QTIFileResponse | null;
21
- i18n?: I18nProvider;
22
- onChange: (value: QTIFileResponse | null) => void;
23
- testIdCanvas?: string;
24
- testIdClear?: string;
25
- /** Stroke color (default: #111827) */
26
- strokeColor?: string;
27
- /** Line width in pixels (default: 3) */
28
- lineWidth?: number;
29
- /** Line cap style (default: round) */
30
- lineCap?: 'butt' | 'round' | 'square';
31
- /** Line join style (default: round) */
32
- lineJoin?: 'bevel' | 'round' | 'miter';
33
- }
34
-
35
- const {
36
- responseId,
37
- label,
38
- imageData = null,
39
- disabled = false,
40
- value = null,
41
- i18n,
42
- onChange,
43
- testIdCanvas,
44
- testIdClear,
45
- strokeColor = '#111827',
46
- lineWidth = 3,
47
- lineCap = 'round',
48
- lineJoin = 'round',
49
- }: Props = $props();
50
-
51
- let canvasEl = $state<HTMLCanvasElement | null>(null);
52
- let ctx = $state<CanvasRenderingContext2D | null>(null);
53
- let isDrawing = $state(false);
54
- let lastX = $state(0);
55
- let lastY = $state(0);
56
- let announceText = $state('');
57
-
58
- function toNumber(v: string | undefined, fallback: number) {
59
- if (!v) return fallback;
60
- const n = Number(v);
61
- return Number.isFinite(n) ? n : fallback;
62
- }
63
-
64
- const canvasWidth = $derived(toNumber(imageData?.width, 600));
65
- const canvasHeight = $derived(toNumber(imageData?.height, 400));
66
-
67
- // Translations
68
- const translations = $derived({
69
- clear: i18n?.t('interactions.drawing.clear') ?? 'interactions.drawing.clear',
70
- instructions: i18n?.t('interactions.drawing.instructions') ?? 'interactions.drawing.instructions',
71
- canvas: i18n?.t('interactions.drawing.canvas') ?? 'interactions.drawing.canvas',
72
- updated: i18n?.t('interactions.drawing.updated') ?? 'interactions.drawing.updated',
73
- cleared: i18n?.t('interactions.drawing.cleared') ?? 'interactions.drawing.cleared',
74
- generated: i18n?.t('interactions.drawing.generated') ?? 'interactions.drawing.generated',
75
- });
76
-
77
- function setupCanvas() {
78
- if (!canvasEl) return;
79
- const context = canvasEl.getContext('2d');
80
- if (!context) return;
81
- ctx = context;
82
- ctx.lineJoin = lineJoin;
83
- ctx.lineCap = lineCap;
84
- ctx.strokeStyle = strokeColor;
85
- ctx.lineWidth = lineWidth;
86
- }
87
-
88
- $effect(() => {
89
- setupCanvas();
90
- });
91
-
92
- function getCanvasPoint(e: PointerEvent) {
93
- if (!canvasEl) return { x: 0, y: 0 };
94
- const rect = canvasEl.getBoundingClientRect();
95
- // Get coordinates relative to canvas
96
- let x = e.clientX - rect.left;
97
- let y = e.clientY - rect.top;
98
-
99
- // Scale from display size to canvas internal size
100
- const scaleX = canvasWidth / rect.width;
101
- const scaleY = canvasHeight / rect.height;
102
- x = x * scaleX;
103
- y = y * scaleY;
104
-
105
- // Clamp to canvas bounds
106
- x = Math.max(0, Math.min(x, canvasWidth));
107
- y = Math.max(0, Math.min(y, canvasHeight));
108
-
109
- return { x, y };
110
- }
111
-
112
- function pointerDown(e: PointerEvent) {
113
- if (disabled || !ctx || !canvasEl) return;
114
- canvasEl.setPointerCapture(e.pointerId);
115
- isDrawing = true;
116
- const { x, y } = getCanvasPoint(e);
117
- lastX = x;
118
- lastY = y;
119
- }
120
-
121
- function pointerMove(e: PointerEvent) {
122
- if (disabled || !ctx || !isDrawing) return;
123
- const { x, y } = getCanvasPoint(e);
124
- ctx.beginPath();
125
- ctx.moveTo(lastX, lastY);
126
- ctx.lineTo(x, y);
127
- ctx.stroke();
128
- lastX = x;
129
- lastY = y;
130
- }
131
-
132
- function dataUrlSize(dataUrl: string): number {
133
- const comma = dataUrl.indexOf(',');
134
- if (comma === -1) return dataUrl.length;
135
- const b64 = dataUrl.slice(comma + 1);
136
- // base64 size estimate
137
- const padding = b64.endsWith('==') ? 2 : b64.endsWith('=') ? 1 : 0;
138
- return Math.floor((b64.length * 3) / 4) - padding;
139
- }
140
-
141
- function commitValue() {
142
- if (!canvasEl || !ctx) return;
143
- const dataUrl = canvasEl.toDataURL('image/png');
144
-
145
- // Extract ImageData synchronously from canvas and include it in the response
146
- // This allows custom operators to analyze drawing content without async image loading
147
- let canvasImageData: globalThis.ImageData | undefined;
148
- try {
149
- canvasImageData = ctx.getImageData(0, 0, canvasEl.width, canvasEl.height);
150
- } catch (e) {
151
- console.warn('[DrawingCanvas] Failed to extract image data:', e);
152
- }
153
-
154
- const size = dataUrlSize(dataUrl);
155
- const response: QTIFileResponse = {
156
- name: `drawing-${responseId}.png`,
157
- type: 'image/png',
158
- size,
159
- lastModified: Date.now(),
160
- dataUrl,
161
- // Include ImageData if available (for drawing analysis in custom operators)
162
- imageData: canvasImageData ? {
163
- data: canvasImageData.data,
164
- width: canvasImageData.width,
165
- height: canvasImageData.height,
166
- } : undefined,
167
- };
168
- onChange(response);
169
- announceText = translations.updated;
170
- }
171
-
172
- function pointerUp(e: PointerEvent) {
173
- if (disabled || !isDrawing) return;
174
- isDrawing = false;
175
- commitValue();
176
- try {
177
- canvasEl?.releasePointerCapture(e.pointerId);
178
- } catch {
179
- // ignore
180
- }
181
- }
182
-
183
- function clear() {
184
- if (disabled || !ctx || !canvasEl) return;
185
- ctx.clearRect(0, 0, canvasEl.width, canvasEl.height);
186
- onChange(null);
187
- announceText = translations.cleared;
188
- }
189
- </script>
190
-
191
- <div class="space-y-3">
192
- <div class="flex items-center justify-between gap-4">
193
- {#if label}
194
- <div class="font-semibold">{label}</div>
195
- {/if}
196
- <button
197
- type="button"
198
- class="btn btn-sm"
199
- data-testid={testIdClear}
200
- onclick={clear}
201
- disabled={disabled}
202
- >
203
- {translations.clear}
204
- </button>
205
- </div>
206
-
207
- <div class="text-xs text-base-content/70" id={`drawing-instructions-${responseId}`}>
208
- {translations.instructions}
209
- </div>
210
-
211
- <div
212
- class="relative inline-block"
213
- style="width: {canvasWidth}px; height: {canvasHeight}px; user-select: none;"
214
- >
215
- {#if imageData}
216
- {#if imageData.type === 'svg'}
217
- <div class="absolute inset-0" style="pointer-events: none;" aria-hidden="true">
218
- {@html imageData.content}
219
- </div>
220
- {:else}
221
- <img
222
- class="absolute inset-0 w-full h-full object-contain"
223
- src={imageData.src}
224
- alt=""
225
- aria-hidden="true"
226
- style="pointer-events: none;"
227
- />
228
- {/if}
229
- {/if}
230
-
231
- <canvas
232
- bind:this={canvasEl}
233
- data-testid={testIdCanvas}
234
- width={canvasWidth}
235
- height={canvasHeight}
236
- class="absolute inset-0 touch-none"
237
- aria-label={translations.canvas}
238
- aria-describedby={`drawing-instructions-${responseId}`}
239
- onpointerdown={pointerDown}
240
- onpointermove={pointerMove}
241
- onpointerup={pointerUp}
242
- onpointercancel={pointerUp}
243
- style="cursor: {disabled ? 'not-allowed' : 'crosshair'}; pointer-events: auto; border: 1px solid var(--color-base-300, #d1d5db); border-radius: 0.25rem;"
244
- ></canvas>
245
- </div>
246
-
247
- <div aria-live="polite" class="sr-only">{announceText}</div>
248
-
249
- {#if value}
250
- <div class="text-sm">
251
- <div><strong>{translations.generated}</strong> {value.name}</div>
252
- <div class="text-xs text-base-content/70">
253
- {i18n?.t('interactions.upload.fileSize', { size: value.size }) ?? `${value.size} bytes`}
254
- </div>
255
- </div>
256
- {/if}
257
- </div>
258
-
259
-
@@ -1,158 +0,0 @@
1
- <script lang="ts">
2
- /**
3
- * File upload component for uploadInteraction
4
- *
5
- * Produces a serializable QTIFileResponse (dataUrl-based) so session state / exports work.
6
- */
7
-
8
- import type { QTIFileResponse } from '@pie-qti/item-player';
9
- import type { I18nProvider } from '@pie-qti/i18n';
10
-
11
- interface Props {
12
- /** Optional label shown above the input */
13
- label?: string;
14
- /** Response id (used for stable ids/test selectors) */
15
- responseId: string;
16
- /** Allowed file types (MIME types and/or extensions); empty means accept any */
17
- fileTypes?: string[];
18
- disabled?: boolean;
19
- value?: QTIFileResponse | null;
20
- onChange: (value: QTIFileResponse | null) => void;
21
- /** Optional test id for the input */
22
- testId?: string;
23
- /** Optional i18n provider for translations */
24
- i18n?: I18nProvider;
25
- }
26
-
27
- const {
28
- label,
29
- responseId,
30
- fileTypes = [],
31
- disabled = false,
32
- value = null,
33
- onChange,
34
- testId,
35
- i18n,
36
- }: Props = $props();
37
-
38
- // Translations
39
- const translations = $derived({
40
- label: i18n?.t('interactions.upload.label') ?? 'interactions.upload.label',
41
- allowedTypes: i18n?.t('interactions.upload.allowedTypes') ?? 'interactions.upload.allowedTypes',
42
- selectedFile: i18n?.t('interactions.upload.selectedFile') ?? 'interactions.upload.selectedFile',
43
- unknownType: i18n?.t('interactions.upload.unknownType') ?? 'interactions.upload.unknownType',
44
- removeFile: i18n?.t('interactions.upload.removeFile') ?? 'interactions.upload.removeFile',
45
- errorInvalidType: (types: string) => i18n?.t('interactions.upload.errorInvalidType', { types }) ?? `interactions.upload.errorInvalidType ${types}`,
46
- });
47
-
48
- // Use label prop or fallback to translated text
49
- const displayLabel = $derived(label || translations.label);
50
-
51
- let error = $state<string>('');
52
-
53
- const acceptAttr = $derived(fileTypes.length > 0 ? fileTypes.join(',') : undefined);
54
- const allowedTypesId = $derived(fileTypes.length > 0 ? `upload-${responseId}-allowed-types` : undefined);
55
- const errorId = $derived(error ? `upload-${responseId}-error` : undefined);
56
- const describedBy = $derived([allowedTypesId, errorId].filter(Boolean).join(' ') || undefined);
57
-
58
- async function fileToResponse(file: File): Promise<QTIFileResponse> {
59
- const dataUrl = await new Promise<string>((resolve, reject) => {
60
- const reader = new FileReader();
61
- reader.onload = () => resolve(String(reader.result || ''));
62
- reader.onerror = () => reject(new Error(i18n?.t('interactions.upload.errorReadFailed') ?? 'Failed to read file'));
63
- reader.readAsDataURL(file);
64
- });
65
-
66
- return {
67
- name: file.name,
68
- type: file.type,
69
- size: file.size,
70
- lastModified: file.lastModified,
71
- dataUrl,
72
- };
73
- }
74
-
75
- async function handleFileChange(e: Event) {
76
- error = '';
77
- const input = e.currentTarget as HTMLInputElement;
78
- const file = input.files?.[0];
79
- if (!file) {
80
- onChange(null);
81
- return;
82
- }
83
-
84
- // Best-effort type check: fileTypes may contain mime types and/or extensions
85
- if (fileTypes.length > 0) {
86
- const allowed = fileTypes.some((t) => {
87
- const trimmed = t.trim();
88
- if (!trimmed) return false;
89
- if (trimmed.startsWith('.')) return file.name.toLowerCase().endsWith(trimmed.toLowerCase());
90
- if (trimmed.includes('/')) return file.type === trimmed;
91
- return false;
92
- });
93
- if (!allowed) {
94
- error = translations.errorInvalidType(fileTypes.join(', '));
95
- input.value = '';
96
- onChange(null);
97
- return;
98
- }
99
- }
100
-
101
- const response = await fileToResponse(file);
102
- onChange(response);
103
- }
104
-
105
- function clear() {
106
- error = '';
107
- onChange(null);
108
- const el = document.getElementById(`upload-${responseId}`) as HTMLInputElement | null;
109
- if (el) el.value = '';
110
- }
111
- </script>
112
-
113
- <div class="space-y-2" role="group" aria-label={displayLabel}>
114
- <label class="label" for={`upload-${responseId}`}>
115
- <span class="label-text font-semibold">{displayLabel}</span>
116
- </label>
117
-
118
- <input
119
- id={`upload-${responseId}`}
120
- data-testid={testId}
121
- class="file-input file-input-bordered w-full"
122
- type="file"
123
- accept={acceptAttr}
124
- aria-describedby={describedBy}
125
- aria-invalid={error ? 'true' : undefined}
126
- disabled={disabled}
127
- onchange={handleFileChange}
128
- />
129
-
130
- {#if fileTypes.length > 0}
131
- <div id={allowedTypesId} class="text-xs text-base-content/70">
132
- {translations.allowedTypes} {fileTypes.join(', ')}
133
- </div>
134
- {/if}
135
-
136
- {#if error}
137
- <div id={errorId} class="alert alert-error py-2" role="alert" aria-live="assertive">
138
- <span class="text-sm">{error}</span>
139
- </div>
140
- {/if}
141
-
142
- {#if value}
143
- <div class="flex items-start justify-between gap-4">
144
- <div class="text-sm">
145
- <div><strong>{translations.selectedFile}</strong> {value.name}</div>
146
- <div class="text-xs text-base-content/70">
147
- {value.type || translations.unknownType} •
148
- {i18n?.t('interactions.upload.fileSize', { size: value.size }) ?? `${value.size} bytes`}
149
- </div>
150
- </div>
151
- <button type="button" class="btn btn-sm" onclick={clear} disabled={disabled}>
152
- {translations.removeFile}
153
- </button>
154
- </div>
155
- {/if}
156
- </div>
157
-
158
-