@pie-qti/default-components 0.1.1

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 (146) hide show
  1. package/.turbo/turbo-build.log +1 -0
  2. package/CHANGELOG.md +9 -0
  3. package/STYLING.md +226 -0
  4. package/dist/index.d.ts +35 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +190 -0
  7. package/dist/plugins/associate/index.d.ts +8 -0
  8. package/dist/plugins/associate/index.d.ts.map +1 -0
  9. package/dist/plugins/associate/index.js +8 -0
  10. package/dist/plugins/choice/index.d.ts +8 -0
  11. package/dist/plugins/choice/index.d.ts.map +1 -0
  12. package/dist/plugins/choice/index.js +8 -0
  13. package/dist/plugins/custom/index.d.ts +8 -0
  14. package/dist/plugins/custom/index.d.ts.map +1 -0
  15. package/dist/plugins/custom/index.js +8 -0
  16. package/dist/plugins/end-attempt/index.d.ts +8 -0
  17. package/dist/plugins/end-attempt/index.d.ts.map +1 -0
  18. package/dist/plugins/end-attempt/index.js +8 -0
  19. package/dist/plugins/extended-text/index.d.ts +8 -0
  20. package/dist/plugins/extended-text/index.d.ts.map +1 -0
  21. package/dist/plugins/extended-text/index.js +8 -0
  22. package/dist/plugins/gap-match/index.d.ts +8 -0
  23. package/dist/plugins/gap-match/index.d.ts.map +1 -0
  24. package/dist/plugins/gap-match/index.js +8 -0
  25. package/dist/plugins/graphic-associate/index.d.ts +8 -0
  26. package/dist/plugins/graphic-associate/index.d.ts.map +1 -0
  27. package/dist/plugins/graphic-associate/index.js +8 -0
  28. package/dist/plugins/graphic-gap-match/index.d.ts +8 -0
  29. package/dist/plugins/graphic-gap-match/index.d.ts.map +1 -0
  30. package/dist/plugins/graphic-gap-match/index.js +8 -0
  31. package/dist/plugins/graphic-order/index.d.ts +8 -0
  32. package/dist/plugins/graphic-order/index.d.ts.map +1 -0
  33. package/dist/plugins/graphic-order/index.js +8 -0
  34. package/dist/plugins/hotspot/index.d.ts +8 -0
  35. package/dist/plugins/hotspot/index.d.ts.map +1 -0
  36. package/dist/plugins/hotspot/index.js +8 -0
  37. package/dist/plugins/hottext/index.d.ts +8 -0
  38. package/dist/plugins/hottext/index.d.ts.map +1 -0
  39. package/dist/plugins/hottext/index.js +8 -0
  40. package/dist/plugins/index.d.ts +30 -0
  41. package/dist/plugins/index.d.ts.map +1 -0
  42. package/dist/plugins/index.js +30 -0
  43. package/dist/plugins/match/index.d.ts +8 -0
  44. package/dist/plugins/match/index.d.ts.map +1 -0
  45. package/dist/plugins/match/index.js +8 -0
  46. package/dist/plugins/media/index.d.ts +8 -0
  47. package/dist/plugins/media/index.d.ts.map +1 -0
  48. package/dist/plugins/media/index.js +8 -0
  49. package/dist/plugins/order/index.d.ts +8 -0
  50. package/dist/plugins/order/index.d.ts.map +1 -0
  51. package/dist/plugins/order/index.js +8 -0
  52. package/dist/plugins/position-object/index.d.ts +8 -0
  53. package/dist/plugins/position-object/index.d.ts.map +1 -0
  54. package/dist/plugins/position-object/index.js +8 -0
  55. package/dist/plugins/select-point/index.d.ts +8 -0
  56. package/dist/plugins/select-point/index.d.ts.map +1 -0
  57. package/dist/plugins/select-point/index.js +8 -0
  58. package/dist/plugins/slider/index.d.ts +8 -0
  59. package/dist/plugins/slider/index.d.ts.map +1 -0
  60. package/dist/plugins/slider/index.js +8 -0
  61. package/dist/shared/actions/typesetAction.d.ts +26 -0
  62. package/dist/shared/actions/typesetAction.d.ts.map +1 -0
  63. package/dist/shared/actions/typesetAction.js +70 -0
  64. package/dist/shared/extensions/MathLiveEditor.d.ts +19 -0
  65. package/dist/shared/extensions/MathLiveEditor.d.ts.map +1 -0
  66. package/dist/shared/extensions/MathLiveEditor.js +239 -0
  67. package/dist/shared/index.d.ts +11 -0
  68. package/dist/shared/index.d.ts.map +1 -0
  69. package/dist/shared/index.js +15 -0
  70. package/dist/shared/utils/eventHelpers.d.ts +18 -0
  71. package/dist/shared/utils/eventHelpers.d.ts.map +1 -0
  72. package/dist/shared/utils/eventHelpers.js +30 -0
  73. package/dist/shared/utils/pairHelpers.d.ts +55 -0
  74. package/dist/shared/utils/pairHelpers.d.ts.map +1 -0
  75. package/dist/shared/utils/pairHelpers.js +76 -0
  76. package/dist/shared/utils/touchDragHelper.d.ts +33 -0
  77. package/dist/shared/utils/touchDragHelper.d.ts.map +1 -0
  78. package/dist/shared/utils/touchDragHelper.js +168 -0
  79. package/dist/shared/utils/webComponentHelpers.d.ts +9 -0
  80. package/dist/shared/utils/webComponentHelpers.d.ts.map +1 -0
  81. package/dist/shared/utils/webComponentHelpers.js +26 -0
  82. package/package.json +93 -0
  83. package/src/index.ts +213 -0
  84. package/src/plugins/associate/AssociateInteraction.svelte +273 -0
  85. package/src/plugins/associate/index.ts +9 -0
  86. package/src/plugins/choice/ChoiceInteraction.svelte +212 -0
  87. package/src/plugins/choice/index.ts +9 -0
  88. package/src/plugins/custom/CustomInteraction.svelte +61 -0
  89. package/src/plugins/custom/index.ts +9 -0
  90. package/src/plugins/drawing/DrawingInteraction.svelte +90 -0
  91. package/src/plugins/end-attempt/EndAttemptInteraction.svelte +142 -0
  92. package/src/plugins/end-attempt/index.ts +9 -0
  93. package/src/plugins/extended-text/ExtendedTextInteraction.svelte +77 -0
  94. package/src/plugins/extended-text/index.ts +9 -0
  95. package/src/plugins/gap-match/GapMatchInteraction.svelte +379 -0
  96. package/src/plugins/gap-match/index.ts +9 -0
  97. package/src/plugins/graphic-associate/GraphicAssociateInteraction.svelte +427 -0
  98. package/src/plugins/graphic-associate/index.ts +9 -0
  99. package/src/plugins/graphic-gap-match/GraphicGapMatchInteraction.svelte +508 -0
  100. package/src/plugins/graphic-gap-match/index.ts +9 -0
  101. package/src/plugins/graphic-order/GraphicOrderInteraction.svelte +241 -0
  102. package/src/plugins/graphic-order/index.ts +9 -0
  103. package/src/plugins/hotspot/HotspotInteraction.svelte +244 -0
  104. package/src/plugins/hotspot/index.ts +9 -0
  105. package/src/plugins/hottext/HottextInteraction.svelte +296 -0
  106. package/src/plugins/hottext/index.ts +9 -0
  107. package/src/plugins/index.ts +31 -0
  108. package/src/plugins/match/MatchInteraction.svelte +70 -0
  109. package/src/plugins/match/index.ts +9 -0
  110. package/src/plugins/media/MediaInteraction.svelte +213 -0
  111. package/src/plugins/media/index.ts +9 -0
  112. package/src/plugins/order/OrderInteraction.svelte +159 -0
  113. package/src/plugins/order/index.ts +9 -0
  114. package/src/plugins/position-object/PositionObjectInteraction.svelte +671 -0
  115. package/src/plugins/position-object/README.md +130 -0
  116. package/src/plugins/position-object/index.ts +9 -0
  117. package/src/plugins/select-point/SelectPointInteraction.svelte +453 -0
  118. package/src/plugins/select-point/index.ts +9 -0
  119. package/src/plugins/slider/SliderInteraction.svelte +141 -0
  120. package/src/plugins/slider/index.ts +9 -0
  121. package/src/plugins/upload/UploadInteraction.svelte +59 -0
  122. package/src/shared/actions/typesetAction.ts +87 -0
  123. package/src/shared/components/CustomInteractionFallback.svelte +153 -0
  124. package/src/shared/components/DragHandle.svelte +44 -0
  125. package/src/shared/components/DrawingCanvas.svelte +243 -0
  126. package/src/shared/components/FileUpload.svelte +153 -0
  127. package/src/shared/components/GraphicGapMatch.svelte +316 -0
  128. package/src/shared/components/GraphicGapMatchAdapter.svelte +36 -0
  129. package/src/shared/components/InlineInteractionRenderer.svelte +124 -0
  130. package/src/shared/components/MatchDragDrop.svelte +434 -0
  131. package/src/shared/components/MathContent.svelte +18 -0
  132. package/src/shared/components/ModalFeedbackDisplay.svelte +81 -0
  133. package/src/shared/components/RichTextEditor.svelte +316 -0
  134. package/src/shared/components/ShadowBaseStyles.svelte +391 -0
  135. package/src/shared/components/SortableList.svelte +297 -0
  136. package/src/shared/extensions/MathLiveEditor.ts +304 -0
  137. package/src/shared/index.ts +17 -0
  138. package/src/shared/styles/shared.css +16 -0
  139. package/src/shared/utils/eventHelpers.ts +36 -0
  140. package/src/shared/utils/pairHelpers.ts +86 -0
  141. package/src/shared/utils/touchDragHelper.ts +211 -0
  142. package/src/shared/utils/webComponentHelpers.ts +27 -0
  143. package/src/svelte-shims.d.ts +9 -0
  144. package/svelte.config.js +9 -0
  145. package/tests/index.test.ts +8 -0
  146. package/tsconfig.json +20 -0
@@ -0,0 +1,671 @@
1
+ <svelte:options customElement="pie-qti-position-object" />
2
+
3
+ <!--
4
+ QTI 2.2 Position Object Interaction Component
5
+
6
+ ⚠️ IMPORTANT LIMITATIONS ⚠️
7
+
8
+ This interaction type has severe limitations in the QTI 2.2 specification that make it
9
+ impractical for most educational use cases. After extensive research, we found:
10
+
11
+ 1. NO real-world implementations beyond the official spec example
12
+ 2. NOT included in QTI 3.0 shared vocabulary/styling guide (likely deprecated)
13
+ 3. Major QTI platforms (TAO, Citolab) show no prominent support
14
+ 4. Only suitable for very narrow use cases
15
+
16
+ THE CORE PROBLEM:
17
+ - Response format is baseType="point" which only stores coordinates: ["158 168", "210 195"]
18
+ - There is NO way to track WHICH object was placed WHERE
19
+ - The spec only supports placing multiple copies of the SAME object (like airport icons)
20
+ - It CANNOT support the obvious use case: "Place these labeled cities on the map"
21
+
22
+ SPEC-COMPLIANT USAGE (Airport Example):
23
+ - One draggable object (airport icon) that can be placed multiple times
24
+ - Response is just an array of coordinates
25
+ - Scoring uses areaMapping to check if ANY placement hits target zones
26
+ - No object identity tracking needed
27
+
28
+ NON-STANDARD EXTENSION (Current Implementation):
29
+ - This component supports multiple positionObjectStage elements with identifiers
30
+ - It tracks which stage (object) is at which position internally
31
+ - BUT the QTI response format cannot preserve this information
32
+ - On reload, objects are mapped to positions by array order (best effort)
33
+
34
+ RECOMMENDATION:
35
+ For labeled-object placement scenarios, use graphicGapMatchInteraction instead,
36
+ which uses baseType="directedPair" to properly track object+location pairs.
37
+
38
+ This implementation exists for QTI 2.2 compatibility but has limited practical utility.
39
+ -->
40
+
41
+ <script lang="ts">
42
+ import type { PositionObjectInteractionData } from '@pie-qti/item-player';
43
+ import type { I18nProvider } from '@pie-qti/i18n';
44
+ import ShadowBaseStyles from '../../shared/components/ShadowBaseStyles.svelte';
45
+ import DragHandle from '../../shared/components/DragHandle.svelte';
46
+ import { parseJsonProp } from '../../shared/utils/webComponentHelpers';
47
+ import { createQtiChangeEvent } from '../../shared/utils/eventHelpers';
48
+
49
+ interface Position {
50
+ stageId: string;
51
+ x: number;
52
+ y: number;
53
+ }
54
+
55
+ interface Props {
56
+ interaction?: PositionObjectInteractionData | string;
57
+ response?: string[] | null; // QTI format: array of "x y" strings
58
+ correctResponse?: string[] | null; // QTI format: array of "x y" strings
59
+ disabled?: boolean;
60
+ role?: string;
61
+ i18n?: I18nProvider;
62
+ onChange?: (value: string[]) => void;
63
+ }
64
+
65
+ let { interaction = $bindable(), response = $bindable(), correctResponse = $bindable(), disabled = false, role = 'candidate', i18n = $bindable(), onChange }: Props = $props();
66
+
67
+ // Parse props that may be JSON strings (web component usage)
68
+ const parsedInteraction = $derived(parseJsonProp<PositionObjectInteractionData>(interaction));
69
+ const parsedResponse = $derived(parseJsonProp<string[]>(response));
70
+ const parsedCorrectResponse = $derived(parseJsonProp<string[]>(correctResponse));
71
+ const isShowingCorrect = $derived(role === 'scorer' && parsedCorrectResponse !== null && parsedCorrectResponse !== undefined);
72
+
73
+ // Get reference to the root element for event dispatching
74
+ let rootElement: HTMLDivElement | undefined = $state();
75
+
76
+ // Positions stored as {stageId, x, y}
77
+ let positions = $state<Position[]>([]);
78
+ let draggedStageId: string | null = $state(null);
79
+ let draggedPositionIndex: number | null = $state(null);
80
+ let imageContainer: HTMLDivElement | null = $state(null);
81
+
82
+ const canInteract = $derived(!disabled);
83
+ const canAddMore = $derived(
84
+ !parsedInteraction || parsedInteraction.maxChoices === 0 || positions.length < parsedInteraction.maxChoices
85
+ );
86
+
87
+ // Calculate scale factor based on actual vs original image dimensions
88
+ const scaleFactor = $derived.by(() => {
89
+ if (!imageContainer || !parsedInteraction?.imageData?.width) return 1;
90
+ const actualWidth = imageContainer.offsetWidth;
91
+ const originalWidth = Number(parsedInteraction.imageData.width);
92
+ return actualWidth / originalWidth;
93
+ });
94
+
95
+ $effect(() => {
96
+ // Sync with parent response changes - convert QTI point format back to internal Position format
97
+ // NOTE: This is best-effort reconstruction since QTI baseType="point" doesn't store object identity
98
+ // We map points to stages by array order, which works for single-stage scenarios but may
99
+ // produce incorrect results for multi-stage scenarios if objects were placed in different order
100
+ if (!parsedResponse || parsedResponse.length === 0) {
101
+ positions = [];
102
+ return;
103
+ }
104
+
105
+ // Convert QTI point strings ("x y") back to Position objects with stageIds
106
+ // Map each point to stages in order - assumes response array order matches stage order
107
+ const newPositions: Position[] = [];
108
+ const stages = parsedInteraction?.positionObjectStages || [];
109
+
110
+ for (let i = 0; i < parsedResponse.length; i++) {
111
+ const pointStr = parsedResponse[i];
112
+ const parts = pointStr.trim().split(/\s+/);
113
+ if (parts.length >= 2) {
114
+ const x = parseFloat(parts[0]);
115
+ const y = parseFloat(parts[1]);
116
+
117
+ // Determine which stage this point belongs to
118
+ // For simple cases with one stage, all points belong to that stage
119
+ // For multiple stages, we need to map based on order and matchMax constraints
120
+ let stageId = '';
121
+ if (stages.length === 1) {
122
+ // Single stage - all points belong to it
123
+ stageId = stages[0].identifier;
124
+ } else {
125
+ // Multiple stages - distribute points based on stage order and matchMax
126
+ let pointIndex = 0;
127
+ for (const stage of stages) {
128
+ if (pointIndex + stage.matchMax > i) {
129
+ stageId = stage.identifier;
130
+ break;
131
+ }
132
+ pointIndex += stage.matchMax;
133
+ }
134
+ }
135
+
136
+ if (stageId && !isNaN(x) && !isNaN(y)) {
137
+ newPositions.push({ stageId, x, y });
138
+ }
139
+ }
140
+ }
141
+
142
+ positions = newPositions;
143
+ });
144
+
145
+ function handleDragStart(event: DragEvent, stageId: string, existingIndex?: number) {
146
+ if (!canInteract) return;
147
+ draggedStageId = stageId;
148
+ draggedPositionIndex = existingIndex ?? null;
149
+
150
+ // Set drag image to be the object being dragged
151
+ if (event.dataTransfer) {
152
+ event.dataTransfer.effectAllowed = 'move';
153
+
154
+ // Create a custom drag image at full size
155
+ const stage = getStageById(stageId);
156
+ if (stage?.objectData) {
157
+ const width = parseInt(stage.objectData.width || '50');
158
+ const height = parseInt(stage.objectData.height || '50');
159
+
160
+ // Create a temporary element for the drag image
161
+ const dragImage = document.createElement('div');
162
+ dragImage.style.position = 'absolute';
163
+ dragImage.style.left = '-9999px';
164
+ dragImage.style.width = `${width}px`;
165
+ dragImage.style.height = `${height}px`;
166
+
167
+ if (stage.objectData.type === 'svg' && stage.objectData.content) {
168
+ dragImage.innerHTML = stage.objectData.content;
169
+ } else if (stage.objectData.src) {
170
+ const img = document.createElement('img');
171
+ img.src = stage.objectData.src;
172
+ img.style.width = '100%';
173
+ img.style.height = '100%';
174
+ dragImage.appendChild(img);
175
+ }
176
+
177
+ document.body.appendChild(dragImage);
178
+ event.dataTransfer.setDragImage(dragImage, width / 2, height / 2);
179
+
180
+ // Clean up after a short delay
181
+ setTimeout(() => document.body.removeChild(dragImage), 0);
182
+ }
183
+ }
184
+ }
185
+
186
+ function handleDrop(event: DragEvent) {
187
+ if (!canInteract || !draggedStageId || !imageContainer) return;
188
+
189
+ event.preventDefault();
190
+
191
+ // Get drop coordinates relative to the image container
192
+ const rect = imageContainer.getBoundingClientRect();
193
+ let x = event.clientX - rect.left;
194
+ let y = event.clientY - rect.top;
195
+
196
+ // Convert scaled coordinates back to original image coordinates
197
+ x = x / scaleFactor;
198
+ y = y / scaleFactor;
199
+
200
+ // If centerPoint is true, adjust coordinates to be at the center
201
+ if (parsedInteraction?.centerPoint) {
202
+ const stage = parsedInteraction.positionObjectStages.find((s) => s.identifier === draggedStageId);
203
+ if (stage?.objectData) {
204
+ const width = parseInt(stage.objectData.width || '50');
205
+ const height = parseInt(stage.objectData.height || '50');
206
+ x -= width / 2;
207
+ y -= height / 2;
208
+ }
209
+ }
210
+
211
+ // Check if we're moving an existing position or creating a new one
212
+ if (draggedPositionIndex !== null) {
213
+ // Moving existing position
214
+ positions = positions.map((pos, idx) =>
215
+ idx === draggedPositionIndex ? { ...pos, x, y } : pos
216
+ );
217
+ } else {
218
+ // Creating new position
219
+ if (!parsedInteraction) return;
220
+ const stage = parsedInteraction.positionObjectStages.find((s) => s.identifier === draggedStageId);
221
+ if (!stage) return;
222
+
223
+ // Check how many times this stage has been used
224
+ const usageCount = positions.filter((p) => p.stageId === draggedStageId).length;
225
+ if (usageCount >= stage.matchMax) return;
226
+
227
+ // Check if we can add more positions
228
+ if (!canAddMore) return;
229
+
230
+ positions = [...positions, { stageId: draggedStageId, x, y }];
231
+ }
232
+
233
+ // Convert positions to QTI point format (space-separated "x y" strings)
234
+ // QTI baseType="point" expects array of strings like ["158 168", "250 200"]
235
+ const responseValue = positions.map(pos => `${Math.round(pos.x)} ${Math.round(pos.y)}`);
236
+
237
+ console.log('[PositionObject] Emitting response:', {
238
+ responseId: parsedInteraction?.responseId,
239
+ value: responseValue,
240
+ positions: positions
241
+ });
242
+
243
+ response = responseValue;
244
+ // Call onChange callback if provided (for Svelte component usage)
245
+ onChange?.(responseValue);
246
+ // Dispatch custom event for web component usage - event will bubble up to the host element
247
+ if (rootElement) {
248
+ rootElement.dispatchEvent(createQtiChangeEvent(parsedInteraction?.responseId, responseValue));
249
+ }
250
+ draggedStageId = null;
251
+ draggedPositionIndex = null;
252
+ }
253
+
254
+ function handleDragOver(event: DragEvent) {
255
+ if (!canInteract) return;
256
+ event.preventDefault();
257
+ if (event.dataTransfer) {
258
+ event.dataTransfer.dropEffect = 'move';
259
+ }
260
+ }
261
+
262
+ function removePosition(index: number) {
263
+ if (!canInteract) return;
264
+ positions = positions.filter((_, i) => i !== index);
265
+
266
+ // Convert positions to QTI point format (space-separated "x y" strings)
267
+ // QTI baseType="point" expects array of strings like ["158 168", "250 200"]
268
+ const responseValue = positions.map(pos => `${Math.round(pos.x)} ${Math.round(pos.y)}`);
269
+
270
+ response = responseValue;
271
+ // Call onChange callback if provided (for Svelte component usage)
272
+ onChange?.(responseValue);
273
+ // Dispatch custom event for web component usage - event will bubble up to the host element
274
+ if (rootElement) {
275
+ rootElement.dispatchEvent(createQtiChangeEvent(parsedInteraction?.responseId, responseValue));
276
+ }
277
+ }
278
+
279
+ function getStageById(id: string) {
280
+ return parsedInteraction?.positionObjectStages.find((s) => s.identifier === id);
281
+ }
282
+
283
+ function getStageUsageCount(id: string): number {
284
+ return positions.filter((p) => p.stageId === id).length;
285
+ }
286
+
287
+ function canDragStage(id: string): boolean {
288
+ const stage = getStageById(id);
289
+ if (!stage) return false;
290
+ return getStageUsageCount(id) < stage.matchMax;
291
+ }
292
+
293
+ /**
294
+ * Parse correct response points (same format as user response)
295
+ */
296
+ function parseCorrectPositions(value: string[] | null | undefined): Position[] {
297
+ if (!value || !Array.isArray(value) || value.length === 0) return [];
298
+ if (!parsedInteraction) return [];
299
+
300
+ const stages = parsedInteraction.positionObjectStages || [];
301
+ const correctPositions: Position[] = [];
302
+
303
+ for (let i = 0; i < value.length; i++) {
304
+ const pointStr = value[i];
305
+ const parts = pointStr.trim().split(/\s+/);
306
+ if (parts.length >= 2) {
307
+ const x = parseFloat(parts[0]);
308
+ const y = parseFloat(parts[1]);
309
+
310
+ // Determine which stage this point belongs to (same logic as user response)
311
+ let stageId = '';
312
+ if (stages.length === 1) {
313
+ stageId = stages[0].identifier;
314
+ } else {
315
+ let pointIndex = 0;
316
+ for (const stage of stages) {
317
+ if (pointIndex + stage.matchMax > i) {
318
+ stageId = stage.identifier;
319
+ break;
320
+ }
321
+ pointIndex += stage.matchMax;
322
+ }
323
+ }
324
+
325
+ if (stageId && !isNaN(x) && !isNaN(y)) {
326
+ correctPositions.push({ stageId, x, y });
327
+ }
328
+ }
329
+ }
330
+
331
+ return correctPositions;
332
+ }
333
+
334
+ const correctPositions = $derived(
335
+ isShowingCorrect ? parseCorrectPositions(parsedCorrectResponse) : []
336
+ );
337
+
338
+ /**
339
+ * Check if a user position matches a correct position (within tolerance)
340
+ */
341
+ function isPositionCorrect(pos: Position): boolean {
342
+ if (!isShowingCorrect) return false;
343
+ return correctPositions.some(
344
+ (correct) =>
345
+ correct.stageId === pos.stageId &&
346
+ Math.abs(correct.x - pos.x) < 5 &&
347
+ Math.abs(correct.y - pos.y) < 5
348
+ );
349
+ }
350
+
351
+ /**
352
+ * Check if a correct position is already placed by the user
353
+ */
354
+ function isCorrectPositionPlaced(correctPos: Position): boolean {
355
+ return positions.some(
356
+ (pos) =>
357
+ pos.stageId === correctPos.stageId &&
358
+ Math.abs(pos.x - correctPos.x) < 5 &&
359
+ Math.abs(pos.y - correctPos.y) < 5
360
+ );
361
+ }
362
+ </script>
363
+
364
+ <ShadowBaseStyles />
365
+
366
+ <div bind:this={rootElement} part="root" class="qti-position-object-interaction">
367
+ {#if !parsedInteraction}
368
+ <div class="alert alert-error">{i18n?.t('common.errorNoData', 'No interaction data provided')}</div>
369
+ {:else}
370
+ {#if parsedInteraction.prompt}
371
+ <p part="prompt" class="qti-po-prompt font-semibold mb-3">{@html parsedInteraction.prompt}</p>
372
+ {/if}
373
+
374
+ <div part="layout" class="qti-po-layout flex flex-col lg:flex-row gap-4 items-start">
375
+ <!-- Canvas Area with Background Image -->
376
+ <div part="canvas-area" class="qti-po-canvas-area flex-1 min-w-0">
377
+ <div
378
+ bind:this={imageContainer}
379
+ part="canvas"
380
+ class="qti-po-canvas relative border-2 border-base-300 rounded-lg overflow-hidden bg-base-200"
381
+ style="width: 100%; aspect-ratio: {parsedInteraction.imageData?.width || 800} / {parsedInteraction.imageData?.height || 600}; box-sizing: border-box;"
382
+ ondrop={handleDrop}
383
+ ondragover={handleDragOver}
384
+ role="region"
385
+ aria-label={i18n?.t('interactions.positionObject.canvasLabel') ?? 'Positioning canvas'}
386
+ >
387
+ <!-- Background Image -->
388
+ {#if parsedInteraction.imageData}
389
+ {#if parsedInteraction.imageData.type === 'svg' && parsedInteraction.imageData.content}
390
+ <div class="w-full h-full pointer-events-none">
391
+ {@html parsedInteraction.imageData.content}
392
+ </div>
393
+ {:else if parsedInteraction.imageData.src}
394
+ <img
395
+ src={parsedInteraction.imageData.src}
396
+ alt={i18n?.t('interactions.positionObject.backgroundAlt') ?? 'Positioning background'}
397
+ class="pointer-events-none"
398
+ style="display: block; width: 100%; height: 100%; object-fit: contain;"
399
+ />
400
+ {/if}
401
+ {/if}
402
+
403
+ <!-- Positioned Objects -->
404
+ {#each positions as position, index}
405
+ {@const stage = getStageById(position.stageId)}
406
+ {@const isCorrect = isPositionCorrect(position)}
407
+ {#if stage}
408
+ <div
409
+ part="placed"
410
+ class="qti-po-placed absolute cursor-move {isCorrect ? 'qti-po-placed-correct' : ''}"
411
+ style="left: {position.x * scaleFactor}px; top: {position.y * scaleFactor}px; z-index: {10 + index}; transform-origin: top left;"
412
+ draggable={canInteract}
413
+ ondragstart={(e) => handleDragStart(e, position.stageId, index)}
414
+ role="button"
415
+ tabindex={canInteract ? 0 : -1}
416
+ aria-label="Positioned {stage.label} at ({position.x}, {position.y}){isCorrect ? '. Correct position' : ''}"
417
+ >
418
+ {#if stage.objectData}
419
+ {#if stage.objectData.type === 'svg' && stage.objectData.content}
420
+ <div
421
+ style="width: {(parseInt(stage.objectData.width || '50')) * scaleFactor}px; height: {(parseInt(stage.objectData.height || '50')) * scaleFactor}px;"
422
+ >
423
+ {@html stage.objectData.content}
424
+ </div>
425
+ {:else if stage.objectData.src}
426
+ <img
427
+ src={stage.objectData.src}
428
+ alt={stage.label}
429
+ style="width: {(parseInt(stage.objectData.width || '50')) * scaleFactor}px; height: {(parseInt(stage.objectData.height || '50')) * scaleFactor}px;"
430
+ class="pointer-events-none"
431
+ />
432
+ {/if}
433
+ {:else}
434
+ <!-- Text-only object -->
435
+ <div
436
+ class="px-3 py-2 rounded-lg shadow-lg font-medium {isCorrect ? 'bg-success text-white' : 'bg-primary text-primary-content'}"
437
+ >
438
+ {stage.label}
439
+ </div>
440
+ {/if}
441
+
442
+ {#if isCorrect}
443
+ <span class="badge badge-success badge-xs absolute -top-1 -right-1">✓</span>
444
+ {/if}
445
+
446
+ {#if canInteract}
447
+ <button
448
+ part="remove"
449
+ class="absolute -top-2 -right-2 btn btn-xs btn-circle btn-error opacity-80 hover:opacity-100"
450
+ onclick={(e) => {
451
+ e.stopPropagation();
452
+ removePosition(index);
453
+ }}
454
+ aria-label="Remove {stage.label}"
455
+ >
456
+ <svg
457
+ xmlns="http://www.w3.org/2000/svg"
458
+ part="remove-icon"
459
+ class="qti-icon-xs h-3 w-3"
460
+ fill="none"
461
+ viewBox="0 0 24 24"
462
+ stroke="currentColor"
463
+ >
464
+ <path
465
+ stroke-linecap="round"
466
+ stroke-linejoin="round"
467
+ stroke-width="2"
468
+ d="M6 18L18 6M6 6l12 12"
469
+ />
470
+ </svg>
471
+ </button>
472
+ {/if}
473
+ </div>
474
+ {/if}
475
+ {/each}
476
+
477
+ <!-- Render correct positions that haven't been placed by the user -->
478
+ {#if isShowingCorrect && correctPositions.length > 0}
479
+ {#each correctPositions as correctPos, index}
480
+ {@const stage = getStageById(correctPos.stageId)}
481
+ {@const isPlaced = isCorrectPositionPlaced(correctPos)}
482
+ {#if stage && !isPlaced}
483
+ <div
484
+ part="correct-placed"
485
+ class="qti-po-placed qti-po-placed-correct qti-po-placed-ghost absolute pointer-events-none"
486
+ style="left: {correctPos.x * scaleFactor}px; top: {correctPos.y * scaleFactor}px; z-index: {5 + index}; transform-origin: top left;"
487
+ aria-label="Correct position for {stage.label} at ({correctPos.x}, {correctPos.y})"
488
+ >
489
+ {#if stage.objectData}
490
+ {#if stage.objectData.type === 'svg' && stage.objectData.content}
491
+ <div
492
+ style="width: {(parseInt(stage.objectData.width || '50')) * scaleFactor}px; height: {(parseInt(stage.objectData.height || '50')) * scaleFactor}px;"
493
+ >
494
+ {@html stage.objectData.content}
495
+ </div>
496
+ {:else if stage.objectData.src}
497
+ <img
498
+ src={stage.objectData.src}
499
+ alt={stage.label}
500
+ style="width: {(parseInt(stage.objectData.width || '50')) * scaleFactor}px; height: {(parseInt(stage.objectData.height || '50')) * scaleFactor}px;"
501
+ class="pointer-events-none"
502
+ />
503
+ {/if}
504
+ {:else}
505
+ <!-- Text-only object -->
506
+ <div
507
+ class="px-3 py-2 bg-success text-white rounded-lg shadow-lg font-medium opacity-60"
508
+ >
509
+ {stage.label}
510
+ </div>
511
+ {/if}
512
+ <span class="badge badge-success badge-xs absolute -top-1 -right-1">✓</span>
513
+ </div>
514
+ {/if}
515
+ {/each}
516
+ {/if}
517
+ </div>
518
+
519
+ {#if parsedInteraction.minChoices > 0}
520
+ <div class="text-xs text-base-content/60 mt-2">
521
+ Minimum required: {parsedInteraction.minChoices} {#if parsedInteraction.maxChoices > 0}
522
+ | Maximum: {parsedInteraction.maxChoices}
523
+ {/if}
524
+ </div>
525
+ {/if}
526
+ </div>
527
+
528
+ <!-- Object Palette -->
529
+ <div part="palette" class="qti-po-palette w-full lg:w-auto lg:flex-1 lg:min-w-[18rem]">
530
+ <div class="qti-po-card card bg-base-100 border border-base-300 h-full">
531
+ <div class="qti-po-card-body card-body p-4">
532
+ <h3 class="card-title text-sm">
533
+ Available Objects ({positions.length}{#if parsedInteraction.maxChoices > 0}/{parsedInteraction.maxChoices}{/if})
534
+ </h3>
535
+
536
+ <p class="text-xs text-base-content/60 mb-2">Drag objects onto the canvas to position them.</p>
537
+
538
+ <div class="space-y-2">
539
+ {#each parsedInteraction.positionObjectStages as stage}
540
+ {@const usageCount = getStageUsageCount(stage.identifier)}
541
+ {@const canDrag = canDragStage(stage.identifier)}
542
+ <div
543
+ part="palette-item"
544
+ class="qti-po-palette-item flex items-center gap-3 p-3 rounded-lg bg-base-200 border border-base-300 {canInteract &&
545
+ canDrag
546
+ ? 'cursor-move hover:border-primary'
547
+ : 'opacity-50 cursor-not-allowed'}"
548
+ draggable={canInteract && canDrag}
549
+ ondragstart={(e) => {
550
+ if (canDrag) handleDragStart(e, stage.identifier);
551
+ }}
552
+ role="button"
553
+ tabindex={canInteract && canDrag ? 0 : -1}
554
+ aria-label="{stage.label} ({usageCount}/{stage.matchMax} used)"
555
+ >
556
+ <!-- Object Preview -->
557
+ <div class="qti-po-preview flex-shrink-0">
558
+ {#if stage.objectData}
559
+ {#if stage.objectData.type === 'svg' && stage.objectData.content}
560
+ <div class="w-10 h-10" data-drag-preview>
561
+ {@html stage.objectData.content}
562
+ </div>
563
+ {:else if stage.objectData.src}
564
+ <img
565
+ src={stage.objectData.src}
566
+ alt={stage.label}
567
+ class="w-10 h-10 object-contain qti-po-preview-image"
568
+ data-drag-preview
569
+ />
570
+ {/if}
571
+ {:else}
572
+ <div class="w-10 h-10 flex items-center justify-center" data-drag-preview>
573
+ <span class="text-2xl">📦</span>
574
+ </div>
575
+ {/if}
576
+ </div>
577
+
578
+ <!-- Label and Usage -->
579
+ <div class="flex-1 min-w-0">
580
+ <div class="font-medium text-sm truncate">{stage.label}</div>
581
+ <div class="text-xs text-base-content/60">
582
+ {usageCount}/{stage.matchMax} used
583
+ </div>
584
+ </div>
585
+
586
+ <!-- Drag Indicator -->
587
+ {#if canInteract && canDrag}
588
+ <DragHandle size={1.25} opacity={0.4} />
589
+ {/if}
590
+ </div>
591
+ {/each}
592
+ </div>
593
+ </div>
594
+ </div>
595
+ </div>
596
+ </div>
597
+ {/if}
598
+ </div>
599
+
600
+ <style>
601
+ /* Ensure proper draggable areas */
602
+ :global(.qti-position-object-interaction [draggable='true']) {
603
+ user-select: none;
604
+ -webkit-user-drag: element;
605
+ }
606
+
607
+ /* Minimal icon sizing so SVGs don't fall back to browser default size without Tailwind */
608
+ .qti-icon-xs {
609
+ width: 0.75rem;
610
+ height: 0.75rem;
611
+ flex: 0 0 auto;
612
+ }
613
+
614
+ /* Minimal layout so this works without Tailwind utilities */
615
+ .qti-po-layout {
616
+ display: flex;
617
+ gap: 1rem;
618
+ flex-wrap: wrap;
619
+ align-items: flex-start;
620
+ }
621
+ .qti-po-canvas-area {
622
+ flex: 1 1 0;
623
+ min-width: 280px;
624
+ }
625
+ .qti-po-canvas {
626
+ position: relative;
627
+ border-radius: 0.75rem;
628
+ overflow: hidden;
629
+ border: 2px solid var(--color-base-300, oklch(95% 0 0));
630
+ background: var(--color-base-200, oklch(98% 0 0));
631
+ width: 100%;
632
+ }
633
+ /* Critical: positioned items must be absolutely positioned even without Tailwind's `absolute` utility */
634
+ .qti-po-placed {
635
+ position: absolute;
636
+ }
637
+
638
+ .qti-po-placed-correct {
639
+ border: 3px solid var(--color-success, oklch(76% 0.177 163.223));
640
+ border-radius: 4px;
641
+ box-shadow: 0 0 0 2px var(--color-success, oklch(76% 0.177 163.223)) inset;
642
+ }
643
+
644
+ .qti-po-placed-ghost {
645
+ opacity: 0.6;
646
+ border: 3px dashed var(--color-success, oklch(76% 0.177 163.223));
647
+ border-radius: 4px;
648
+ }
649
+ .qti-po-palette {
650
+ flex: 1 1 0;
651
+ min-width: 18rem;
652
+ }
653
+ .qti-po-card {
654
+ border: 1px solid var(--color-base-300, oklch(95% 0 0));
655
+ border-radius: 0.75rem;
656
+ background: var(--color-base-100, oklch(100% 0 0));
657
+ height: 100%;
658
+ }
659
+ .qti-po-card-body {
660
+ padding: 1rem;
661
+ }
662
+ .qti-po-palette-item {
663
+ display: flex;
664
+ align-items: center;
665
+ gap: 0.75rem;
666
+ padding: 0.75rem;
667
+ border-radius: 0.75rem;
668
+ border: 1px solid var(--color-base-300, oklch(95% 0 0));
669
+ background: var(--color-base-200, oklch(98% 0 0));
670
+ }
671
+ </style>