@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.
- package/.turbo/turbo-build.log +1 -0
- package/CHANGELOG.md +9 -0
- package/STYLING.md +226 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +190 -0
- package/dist/plugins/associate/index.d.ts +8 -0
- package/dist/plugins/associate/index.d.ts.map +1 -0
- package/dist/plugins/associate/index.js +8 -0
- package/dist/plugins/choice/index.d.ts +8 -0
- package/dist/plugins/choice/index.d.ts.map +1 -0
- package/dist/plugins/choice/index.js +8 -0
- package/dist/plugins/custom/index.d.ts +8 -0
- package/dist/plugins/custom/index.d.ts.map +1 -0
- package/dist/plugins/custom/index.js +8 -0
- package/dist/plugins/end-attempt/index.d.ts +8 -0
- package/dist/plugins/end-attempt/index.d.ts.map +1 -0
- package/dist/plugins/end-attempt/index.js +8 -0
- package/dist/plugins/extended-text/index.d.ts +8 -0
- package/dist/plugins/extended-text/index.d.ts.map +1 -0
- package/dist/plugins/extended-text/index.js +8 -0
- package/dist/plugins/gap-match/index.d.ts +8 -0
- package/dist/plugins/gap-match/index.d.ts.map +1 -0
- package/dist/plugins/gap-match/index.js +8 -0
- package/dist/plugins/graphic-associate/index.d.ts +8 -0
- package/dist/plugins/graphic-associate/index.d.ts.map +1 -0
- package/dist/plugins/graphic-associate/index.js +8 -0
- package/dist/plugins/graphic-gap-match/index.d.ts +8 -0
- package/dist/plugins/graphic-gap-match/index.d.ts.map +1 -0
- package/dist/plugins/graphic-gap-match/index.js +8 -0
- package/dist/plugins/graphic-order/index.d.ts +8 -0
- package/dist/plugins/graphic-order/index.d.ts.map +1 -0
- package/dist/plugins/graphic-order/index.js +8 -0
- package/dist/plugins/hotspot/index.d.ts +8 -0
- package/dist/plugins/hotspot/index.d.ts.map +1 -0
- package/dist/plugins/hotspot/index.js +8 -0
- package/dist/plugins/hottext/index.d.ts +8 -0
- package/dist/plugins/hottext/index.d.ts.map +1 -0
- package/dist/plugins/hottext/index.js +8 -0
- package/dist/plugins/index.d.ts +30 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +30 -0
- package/dist/plugins/match/index.d.ts +8 -0
- package/dist/plugins/match/index.d.ts.map +1 -0
- package/dist/plugins/match/index.js +8 -0
- package/dist/plugins/media/index.d.ts +8 -0
- package/dist/plugins/media/index.d.ts.map +1 -0
- package/dist/plugins/media/index.js +8 -0
- package/dist/plugins/order/index.d.ts +8 -0
- package/dist/plugins/order/index.d.ts.map +1 -0
- package/dist/plugins/order/index.js +8 -0
- package/dist/plugins/position-object/index.d.ts +8 -0
- package/dist/plugins/position-object/index.d.ts.map +1 -0
- package/dist/plugins/position-object/index.js +8 -0
- package/dist/plugins/select-point/index.d.ts +8 -0
- package/dist/plugins/select-point/index.d.ts.map +1 -0
- package/dist/plugins/select-point/index.js +8 -0
- package/dist/plugins/slider/index.d.ts +8 -0
- package/dist/plugins/slider/index.d.ts.map +1 -0
- package/dist/plugins/slider/index.js +8 -0
- package/dist/shared/actions/typesetAction.d.ts +26 -0
- package/dist/shared/actions/typesetAction.d.ts.map +1 -0
- package/dist/shared/actions/typesetAction.js +70 -0
- package/dist/shared/extensions/MathLiveEditor.d.ts +19 -0
- package/dist/shared/extensions/MathLiveEditor.d.ts.map +1 -0
- package/dist/shared/extensions/MathLiveEditor.js +239 -0
- package/dist/shared/index.d.ts +11 -0
- package/dist/shared/index.d.ts.map +1 -0
- package/dist/shared/index.js +15 -0
- package/dist/shared/utils/eventHelpers.d.ts +18 -0
- package/dist/shared/utils/eventHelpers.d.ts.map +1 -0
- package/dist/shared/utils/eventHelpers.js +30 -0
- package/dist/shared/utils/pairHelpers.d.ts +55 -0
- package/dist/shared/utils/pairHelpers.d.ts.map +1 -0
- package/dist/shared/utils/pairHelpers.js +76 -0
- package/dist/shared/utils/touchDragHelper.d.ts +33 -0
- package/dist/shared/utils/touchDragHelper.d.ts.map +1 -0
- package/dist/shared/utils/touchDragHelper.js +168 -0
- package/dist/shared/utils/webComponentHelpers.d.ts +9 -0
- package/dist/shared/utils/webComponentHelpers.d.ts.map +1 -0
- package/dist/shared/utils/webComponentHelpers.js +26 -0
- package/package.json +93 -0
- package/src/index.ts +213 -0
- package/src/plugins/associate/AssociateInteraction.svelte +273 -0
- package/src/plugins/associate/index.ts +9 -0
- package/src/plugins/choice/ChoiceInteraction.svelte +212 -0
- package/src/plugins/choice/index.ts +9 -0
- package/src/plugins/custom/CustomInteraction.svelte +61 -0
- package/src/plugins/custom/index.ts +9 -0
- package/src/plugins/drawing/DrawingInteraction.svelte +90 -0
- package/src/plugins/end-attempt/EndAttemptInteraction.svelte +142 -0
- package/src/plugins/end-attempt/index.ts +9 -0
- package/src/plugins/extended-text/ExtendedTextInteraction.svelte +77 -0
- package/src/plugins/extended-text/index.ts +9 -0
- package/src/plugins/gap-match/GapMatchInteraction.svelte +379 -0
- package/src/plugins/gap-match/index.ts +9 -0
- package/src/plugins/graphic-associate/GraphicAssociateInteraction.svelte +427 -0
- package/src/plugins/graphic-associate/index.ts +9 -0
- package/src/plugins/graphic-gap-match/GraphicGapMatchInteraction.svelte +508 -0
- package/src/plugins/graphic-gap-match/index.ts +9 -0
- package/src/plugins/graphic-order/GraphicOrderInteraction.svelte +241 -0
- package/src/plugins/graphic-order/index.ts +9 -0
- package/src/plugins/hotspot/HotspotInteraction.svelte +244 -0
- package/src/plugins/hotspot/index.ts +9 -0
- package/src/plugins/hottext/HottextInteraction.svelte +296 -0
- package/src/plugins/hottext/index.ts +9 -0
- package/src/plugins/index.ts +31 -0
- package/src/plugins/match/MatchInteraction.svelte +70 -0
- package/src/plugins/match/index.ts +9 -0
- package/src/plugins/media/MediaInteraction.svelte +213 -0
- package/src/plugins/media/index.ts +9 -0
- package/src/plugins/order/OrderInteraction.svelte +159 -0
- package/src/plugins/order/index.ts +9 -0
- package/src/plugins/position-object/PositionObjectInteraction.svelte +671 -0
- package/src/plugins/position-object/README.md +130 -0
- package/src/plugins/position-object/index.ts +9 -0
- package/src/plugins/select-point/SelectPointInteraction.svelte +453 -0
- package/src/plugins/select-point/index.ts +9 -0
- package/src/plugins/slider/SliderInteraction.svelte +141 -0
- package/src/plugins/slider/index.ts +9 -0
- package/src/plugins/upload/UploadInteraction.svelte +59 -0
- package/src/shared/actions/typesetAction.ts +87 -0
- package/src/shared/components/CustomInteractionFallback.svelte +153 -0
- package/src/shared/components/DragHandle.svelte +44 -0
- package/src/shared/components/DrawingCanvas.svelte +243 -0
- package/src/shared/components/FileUpload.svelte +153 -0
- package/src/shared/components/GraphicGapMatch.svelte +316 -0
- package/src/shared/components/GraphicGapMatchAdapter.svelte +36 -0
- package/src/shared/components/InlineInteractionRenderer.svelte +124 -0
- package/src/shared/components/MatchDragDrop.svelte +434 -0
- package/src/shared/components/MathContent.svelte +18 -0
- package/src/shared/components/ModalFeedbackDisplay.svelte +81 -0
- package/src/shared/components/RichTextEditor.svelte +316 -0
- package/src/shared/components/ShadowBaseStyles.svelte +391 -0
- package/src/shared/components/SortableList.svelte +297 -0
- package/src/shared/extensions/MathLiveEditor.ts +304 -0
- package/src/shared/index.ts +17 -0
- package/src/shared/styles/shared.css +16 -0
- package/src/shared/utils/eventHelpers.ts +36 -0
- package/src/shared/utils/pairHelpers.ts +86 -0
- package/src/shared/utils/touchDragHelper.ts +211 -0
- package/src/shared/utils/webComponentHelpers.ts +27 -0
- package/src/svelte-shims.d.ts +9 -0
- package/svelte.config.js +9 -0
- package/tests/index.test.ts +8 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
<svelte:options customElement="pie-qti-graphic-order" />
|
|
2
|
+
|
|
3
|
+
<script lang="ts">
|
|
4
|
+
import type { GraphicOrderInteractionData } from '@pie-qti/item-player';
|
|
5
|
+
import type { I18nProvider } from '@pie-qti/i18n';
|
|
6
|
+
import ShadowBaseStyles from '../../shared/components/ShadowBaseStyles.svelte';
|
|
7
|
+
import SortableList from '../../shared/components/SortableList.svelte';
|
|
8
|
+
import { parseJsonProp } from '../../shared/utils/webComponentHelpers';
|
|
9
|
+
import { createQtiChangeEvent } from '../../shared/utils/eventHelpers';
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
interaction?: GraphicOrderInteractionData | string;
|
|
13
|
+
response?: string[] | null;
|
|
14
|
+
correctResponse?: string[] | null;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
role?: string;
|
|
17
|
+
i18n?: I18nProvider;
|
|
18
|
+
onChange?: (value: string[]) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let { interaction = $bindable(), response = $bindable(), correctResponse = $bindable(), disabled = false, role = 'candidate', i18n = $bindable(), onChange }: Props = $props();
|
|
22
|
+
|
|
23
|
+
// Parse props that may be JSON strings (web component usage)
|
|
24
|
+
const parsedInteraction = $derived(parseJsonProp<GraphicOrderInteractionData>(interaction));
|
|
25
|
+
const parsedResponse = $derived(parseJsonProp<string[]>(response));
|
|
26
|
+
const parsedCorrectResponse = $derived(parseJsonProp<string[]>(correctResponse));
|
|
27
|
+
const isShowingCorrect = $derived(role === 'scorer' && parsedCorrectResponse !== null);
|
|
28
|
+
|
|
29
|
+
// Get reference to the root element for event dispatching
|
|
30
|
+
let rootElement: HTMLDivElement | undefined = $state();
|
|
31
|
+
|
|
32
|
+
let orderedIds = $state<string[]>([]);
|
|
33
|
+
let imageContainer: HTMLDivElement | null = $state(null);
|
|
34
|
+
let imageElement: HTMLElement | null = $state(null);
|
|
35
|
+
|
|
36
|
+
// Track whether user has confirmed their order
|
|
37
|
+
let hasConfirmed = $state(false);
|
|
38
|
+
|
|
39
|
+
// Track initial order to detect if user has made changes
|
|
40
|
+
const initialOrder = $derived(parsedInteraction?.hotspotChoices.map((c) => c.identifier) || []);
|
|
41
|
+
const hasReordered = $derived(
|
|
42
|
+
orderedIds.length > 0 &&
|
|
43
|
+
orderedIds.some((id, index) => id !== initialOrder[index])
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// Show confirmation button when response is null or when not confirmed yet
|
|
47
|
+
const needsConfirmation = $derived(
|
|
48
|
+
!disabled &&
|
|
49
|
+
(!parsedResponse || !hasConfirmed) &&
|
|
50
|
+
orderedIds.length > 0
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
$effect(() => {
|
|
54
|
+
// Sync with parent response changes
|
|
55
|
+
orderedIds = parsedResponse ? [...parsedResponse] : parsedInteraction?.hotspotChoices.map((c) => c.identifier) || [];
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Reset confirmation state when response is externally cleared
|
|
59
|
+
$effect(() => {
|
|
60
|
+
if (parsedResponse === null || (Array.isArray(parsedResponse) && parsedResponse.length === 0)) {
|
|
61
|
+
hasConfirmed = false;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
function handleReorder(newOrder: string[]) {
|
|
66
|
+
hasConfirmed = true; // Auto-confirm on drag
|
|
67
|
+
orderedIds = newOrder;
|
|
68
|
+
response = orderedIds;
|
|
69
|
+
// Call onChange callback if provided (for Svelte component usage)
|
|
70
|
+
onChange?.(orderedIds);
|
|
71
|
+
// Dispatch custom event for web component usage - event will bubble up to the host element
|
|
72
|
+
if (rootElement) {
|
|
73
|
+
rootElement.dispatchEvent(createQtiChangeEvent(parsedInteraction?.responseId, orderedIds));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function confirmOrder() {
|
|
78
|
+
if (disabled || hasConfirmed) return;
|
|
79
|
+
|
|
80
|
+
hasConfirmed = true;
|
|
81
|
+
const currentOrder = orderedIds;
|
|
82
|
+
response = currentOrder;
|
|
83
|
+
|
|
84
|
+
// Call onChange callback if provided (for Svelte component usage)
|
|
85
|
+
onChange?.(currentOrder);
|
|
86
|
+
// Dispatch custom event for web component usage - event will bubble up to the host element
|
|
87
|
+
if (rootElement) {
|
|
88
|
+
rootElement.dispatchEvent(createQtiChangeEvent(parsedInteraction?.responseId, currentOrder));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function getChoiceByIdentifier(identifier: string) {
|
|
93
|
+
return parsedInteraction?.hotspotChoices.find((c) => c.identifier === identifier);
|
|
94
|
+
}
|
|
95
|
+
</script>
|
|
96
|
+
|
|
97
|
+
<ShadowBaseStyles />
|
|
98
|
+
|
|
99
|
+
<div bind:this={rootElement} part="root" class="qti-graphic-order-interaction">
|
|
100
|
+
{#if !parsedInteraction}
|
|
101
|
+
<div class="alert alert-error">{i18n?.t('common.errorNoData', 'No interaction data provided')}</div>
|
|
102
|
+
{:else}
|
|
103
|
+
{#if parsedInteraction.prompt}
|
|
104
|
+
<p part="prompt" class="qti-go-prompt font-semibold mb-3">{@html parsedInteraction.prompt}</p>
|
|
105
|
+
{/if}
|
|
106
|
+
|
|
107
|
+
<div part="layout" class="qti-go-layout flex flex-col gap-4">
|
|
108
|
+
<!-- Image Area -->
|
|
109
|
+
<div part="image-area" class="qti-go-image-area">
|
|
110
|
+
<div
|
|
111
|
+
bind:this={imageContainer}
|
|
112
|
+
part="stage"
|
|
113
|
+
class="qti-go-stage relative border-2 border-base-300 rounded-lg overflow-hidden bg-base-200"
|
|
114
|
+
>
|
|
115
|
+
{#if parsedInteraction.imageData}
|
|
116
|
+
{#if parsedInteraction.imageData.type === 'svg' && parsedInteraction.imageData.content}
|
|
117
|
+
<div
|
|
118
|
+
bind:this={imageElement}
|
|
119
|
+
class="w-full h-auto"
|
|
120
|
+
style="width: {parsedInteraction.imageData.width}px; height: {parsedInteraction.imageData.height}px;"
|
|
121
|
+
>
|
|
122
|
+
{@html parsedInteraction.imageData.content}
|
|
123
|
+
</div>
|
|
124
|
+
{:else if parsedInteraction.imageData.src}
|
|
125
|
+
<img
|
|
126
|
+
bind:this={imageElement}
|
|
127
|
+
src={parsedInteraction.imageData.src}
|
|
128
|
+
alt={i18n?.t('interactions.graphicOrder.altText') ?? 'Ordering diagram'}
|
|
129
|
+
class="w-full h-auto"
|
|
130
|
+
style="width: {parsedInteraction.imageData.width}px; height: {parsedInteraction.imageData
|
|
131
|
+
.height}px;"
|
|
132
|
+
/>
|
|
133
|
+
{/if}
|
|
134
|
+
{/if}
|
|
135
|
+
</div>
|
|
136
|
+
</div>
|
|
137
|
+
|
|
138
|
+
<!-- Ordering Area -->
|
|
139
|
+
<div part="panel" class="qti-go-panel w-full">
|
|
140
|
+
<div class="qti-go-card card bg-base-100 border border-base-300">
|
|
141
|
+
<div class="qti-go-card-body card-body p-4">
|
|
142
|
+
<h3 part="panel-title" class="qti-go-title card-title text-sm">{i18n?.t('interactions.graphicOrder.orderHeading') ?? 'Order (drag to reorder)'}</h3>
|
|
143
|
+
<SortableList
|
|
144
|
+
items={parsedInteraction.hotspotChoices.map(c => ({ id: c.identifier, text: c.label }))}
|
|
145
|
+
{orderedIds}
|
|
146
|
+
correctOrder={isShowingCorrect ? (parsedCorrectResponse || []) : []}
|
|
147
|
+
orientation="vertical"
|
|
148
|
+
{disabled}
|
|
149
|
+
onReorder={handleReorder}
|
|
150
|
+
/>
|
|
151
|
+
|
|
152
|
+
<!-- Confirmation button for WCAG 2.2 SC 3.3.4 compliance -->
|
|
153
|
+
<div part="confirm" class="qti-go-confirm mt-4 pt-3 border-t border-base-300">
|
|
154
|
+
{#if needsConfirmation}
|
|
155
|
+
<button
|
|
156
|
+
type="button"
|
|
157
|
+
class="btn btn-primary btn-sm w-full"
|
|
158
|
+
onclick={confirmOrder}
|
|
159
|
+
aria-label={i18n?.t('interactions.graphicOrder.confirmAria') ?? 'Confirm this order as your answer'}
|
|
160
|
+
>
|
|
161
|
+
{hasReordered
|
|
162
|
+
? (i18n?.t('interactions.graphicOrder.confirmOrder') ?? 'Confirm Order')
|
|
163
|
+
: (i18n?.t('interactions.graphicOrder.confirmOrderNoChanges') ?? 'Confirm Order (No Changes)')}
|
|
164
|
+
</button>
|
|
165
|
+
<p class="text-xs text-base-content/60 mt-2 text-center">
|
|
166
|
+
{i18n?.t('interactions.graphicOrder.instruction') ?? 'Drag to reorder, or click to confirm'}
|
|
167
|
+
</p>
|
|
168
|
+
{:else if hasConfirmed}
|
|
169
|
+
<div class="alert alert-success py-2">
|
|
170
|
+
<svg
|
|
171
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
172
|
+
fill="none"
|
|
173
|
+
viewBox="0 0 24 24"
|
|
174
|
+
part="confirmed-icon"
|
|
175
|
+
class="qti-icon qti-go-confirmed stroke-current shrink-0 w-5 h-5"
|
|
176
|
+
>
|
|
177
|
+
<path
|
|
178
|
+
stroke-linecap="round"
|
|
179
|
+
stroke-linejoin="round"
|
|
180
|
+
stroke-width="2"
|
|
181
|
+
d="M5 13l4 4L19 7"
|
|
182
|
+
></path>
|
|
183
|
+
</svg>
|
|
184
|
+
<span class="text-sm">Order confirmed</span>
|
|
185
|
+
</div>
|
|
186
|
+
{/if}
|
|
187
|
+
</div>
|
|
188
|
+
</div>
|
|
189
|
+
</div>
|
|
190
|
+
</div>
|
|
191
|
+
</div>
|
|
192
|
+
{/if}
|
|
193
|
+
</div>
|
|
194
|
+
|
|
195
|
+
<style>
|
|
196
|
+
/* Ensure proper drag and drop visual feedback */
|
|
197
|
+
:global(.qti-graphic-order-interaction [draggable='true']) {
|
|
198
|
+
user-select: none;
|
|
199
|
+
-webkit-user-drag: element;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* Minimal icon sizing so SVGs don't fall back to browser default size without Tailwind */
|
|
203
|
+
.qti-icon {
|
|
204
|
+
width: 1.25rem;
|
|
205
|
+
height: 1.25rem;
|
|
206
|
+
flex: 0 0 auto;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/* Minimal layout so this works without Tailwind utilities */
|
|
210
|
+
.qti-go-layout {
|
|
211
|
+
display: flex;
|
|
212
|
+
flex-direction: column;
|
|
213
|
+
gap: 1rem;
|
|
214
|
+
}
|
|
215
|
+
.qti-go-image-area {
|
|
216
|
+
width: 100%;
|
|
217
|
+
}
|
|
218
|
+
.qti-go-panel {
|
|
219
|
+
width: 100%;
|
|
220
|
+
}
|
|
221
|
+
.qti-go-stage {
|
|
222
|
+
position: relative;
|
|
223
|
+
border-radius: 0.75rem;
|
|
224
|
+
overflow: hidden;
|
|
225
|
+
border: 2px solid var(--color-base-300, oklch(95% 0 0));
|
|
226
|
+
background: var(--color-base-200, oklch(98% 0 0));
|
|
227
|
+
}
|
|
228
|
+
.qti-go-card {
|
|
229
|
+
border: 1px solid var(--color-base-300, oklch(95% 0 0));
|
|
230
|
+
border-radius: 0.75rem;
|
|
231
|
+
background: var(--color-base-100, oklch(100% 0 0));
|
|
232
|
+
}
|
|
233
|
+
.qti-go-card-body {
|
|
234
|
+
padding: 1rem;
|
|
235
|
+
}
|
|
236
|
+
.qti-go-confirm {
|
|
237
|
+
border-top: 1px solid var(--color-base-300, oklch(95% 0 0));
|
|
238
|
+
padding-top: 0.75rem;
|
|
239
|
+
margin-top: 1rem;
|
|
240
|
+
}
|
|
241
|
+
</style>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Svelte component exported via web component
|
|
2
|
+
|
|
3
|
+
export const metadata = {
|
|
4
|
+
interactionType: 'graphicOrderInteraction',
|
|
5
|
+
tagName: 'pie-qti-graphic-order',
|
|
6
|
+
name: 'default-graphic-order',
|
|
7
|
+
description: 'Default Svelte-based graphic order interaction',
|
|
8
|
+
priority: 0,
|
|
9
|
+
};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
<svelte:options customElement="pie-qti-hotspot" />
|
|
2
|
+
|
|
3
|
+
<script lang="ts">
|
|
4
|
+
import type { HotspotInteractionData } from '@pie-qti/item-player';
|
|
5
|
+
import type { I18nProvider } from '@pie-qti/i18n';
|
|
6
|
+
import ShadowBaseStyles from '../../shared/components/ShadowBaseStyles.svelte';
|
|
7
|
+
import { createQtiChangeEvent } from '../../shared/utils/eventHelpers';
|
|
8
|
+
import { parseJsonProp } from '../../shared/utils/webComponentHelpers';
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
interaction?: HotspotInteractionData | string;
|
|
12
|
+
response?: string | null;
|
|
13
|
+
correctResponse?: string | string[] | null;
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
role?: string;
|
|
16
|
+
i18n?: I18nProvider;
|
|
17
|
+
onChange?: (value: string) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let { interaction = $bindable(), response = $bindable(), correctResponse = $bindable(), disabled = false, role = 'candidate', i18n = $bindable(), onChange }: Props = $props();
|
|
21
|
+
|
|
22
|
+
// Parse props that may be JSON strings (web component usage)
|
|
23
|
+
const parsedInteraction = $derived(parseJsonProp<HotspotInteractionData>(interaction));
|
|
24
|
+
const parsedResponse = $derived(parseJsonProp<string>(response));
|
|
25
|
+
const parsedCorrectResponse = $derived(parseJsonProp<string | string[]>(correctResponse));
|
|
26
|
+
const isShowingCorrect = $derived(role === 'scorer' && parsedCorrectResponse !== null);
|
|
27
|
+
|
|
28
|
+
// Get correct IDs as array
|
|
29
|
+
const correctIds = $derived.by(() => {
|
|
30
|
+
if (!isShowingCorrect) return [];
|
|
31
|
+
if (Array.isArray(parsedCorrectResponse)) {
|
|
32
|
+
return parsedCorrectResponse;
|
|
33
|
+
}
|
|
34
|
+
return parsedCorrectResponse ? [parsedCorrectResponse] : [];
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
function isCorrect(identifier: string): boolean {
|
|
38
|
+
return isShowingCorrect && correctIds.includes(identifier);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Get reference to the root element for event dispatching
|
|
42
|
+
let rootElement: HTMLDivElement | undefined = $state();
|
|
43
|
+
|
|
44
|
+
const imageWidth = $derived.by(() => {
|
|
45
|
+
const w = parsedInteraction?.imageData?.width;
|
|
46
|
+
const n = w ? Number(w) : NaN;
|
|
47
|
+
return Number.isFinite(n) && n > 0 ? n : null;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const imageHeight = $derived.by(() => {
|
|
51
|
+
const h = parsedInteraction?.imageData?.height;
|
|
52
|
+
const n = h ? Number(h) : NaN;
|
|
53
|
+
return Number.isFinite(n) && n > 0 ? n : null;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const mediaStyle = $derived.by(() => {
|
|
57
|
+
if (imageWidth && imageHeight) {
|
|
58
|
+
// Fill available width, but don't exceed the asset's natural width.
|
|
59
|
+
return `width: 100%; max-width: ${imageWidth}px; aspect-ratio: ${imageWidth} / ${imageHeight};`;
|
|
60
|
+
}
|
|
61
|
+
return 'width: 100%;';
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
function handleClick(identifier: string) {
|
|
65
|
+
if (!disabled) {
|
|
66
|
+
response = identifier;
|
|
67
|
+
// Call onChange callback if provided (for Svelte component usage)
|
|
68
|
+
onChange?.(identifier);
|
|
69
|
+
// Dispatch event for web component usage - event will bubble up to the host element
|
|
70
|
+
if (rootElement) {
|
|
71
|
+
rootElement.dispatchEvent(createQtiChangeEvent(parsedInteraction?.responseId, identifier));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function handleKeyDown(e: KeyboardEvent, identifier: string) {
|
|
77
|
+
if (disabled) return;
|
|
78
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
handleClick(identifier);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
</script>
|
|
84
|
+
|
|
85
|
+
<ShadowBaseStyles />
|
|
86
|
+
|
|
87
|
+
<div bind:this={rootElement} class="qti-hotspot-interaction space-y-3">
|
|
88
|
+
{#if !parsedInteraction}
|
|
89
|
+
<div class="alert alert-error">{i18n?.t('common.errorNoData', 'No interaction data provided')}</div>
|
|
90
|
+
{:else}
|
|
91
|
+
{#if parsedInteraction.prompt}
|
|
92
|
+
<p part="prompt" class="qti-hotspot-prompt font-semibold">{@html parsedInteraction.prompt}</p>
|
|
93
|
+
{/if}
|
|
94
|
+
|
|
95
|
+
<div part="stage" class="qti-hotspot-stage">
|
|
96
|
+
<div class="qti-hotspot-media" style={mediaStyle}>
|
|
97
|
+
<!-- Render the image/SVG -->
|
|
98
|
+
{#if parsedInteraction.imageData}
|
|
99
|
+
{#if parsedInteraction.imageData.type === 'svg'}
|
|
100
|
+
<div part="image" class="qti-hotspot-image">
|
|
101
|
+
{@html parsedInteraction.imageData.content}
|
|
102
|
+
</div>
|
|
103
|
+
{:else}
|
|
104
|
+
<img
|
|
105
|
+
src={parsedInteraction.imageData.src}
|
|
106
|
+
alt={i18n?.t('interactions.hotspot.altText') ?? 'Hotspot interaction'}
|
|
107
|
+
part="image"
|
|
108
|
+
class="qti-hotspot-image"
|
|
109
|
+
/>
|
|
110
|
+
{/if}
|
|
111
|
+
{/if}
|
|
112
|
+
|
|
113
|
+
<!-- Overlay clickable areas using SVG -->
|
|
114
|
+
<svg
|
|
115
|
+
part="overlay"
|
|
116
|
+
class="qti-hotspot-overlay"
|
|
117
|
+
style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"
|
|
118
|
+
viewBox="0 0 {parsedInteraction.imageData?.width || '800'} {parsedInteraction.imageData?.height || '600'}"
|
|
119
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
120
|
+
>
|
|
121
|
+
{#each parsedInteraction.hotspotChoices as choice}
|
|
122
|
+
{@const isSelected = parsedResponse === choice.identifier}
|
|
123
|
+
{@const isCorrectChoice = isCorrect(choice.identifier)}
|
|
124
|
+
{@const coords = choice.coords.split(',').map(Number)}
|
|
125
|
+
|
|
126
|
+
{#if choice.shape === 'circle'}
|
|
127
|
+
<!-- Circle: coords are cx, cy, radius -->
|
|
128
|
+
<circle
|
|
129
|
+
cx={coords[0]}
|
|
130
|
+
cy={coords[1]}
|
|
131
|
+
r={coords[2]}
|
|
132
|
+
fill={isSelected ? 'rgba(59, 130, 246, 0.3)' : isCorrectChoice ? 'rgba(34, 197, 94, 0.3)' : 'rgba(0, 0, 0, 0)'}
|
|
133
|
+
stroke={isSelected ? '#3b82f6' : isCorrectChoice ? '#22c55e' : 'transparent'}
|
|
134
|
+
stroke-width="2"
|
|
135
|
+
class="qti-hotspot-shape hover:fill-[rgba(59,130,246,0.1)] transition-all"
|
|
136
|
+
role="button"
|
|
137
|
+
aria-label={`Select hotspot ${choice.identifier}${isCorrectChoice ? '. Correct answer' : ''}`}
|
|
138
|
+
aria-pressed={isSelected ? 'true' : 'false'}
|
|
139
|
+
tabindex={disabled ? -1 : 0}
|
|
140
|
+
onclick={() => handleClick(choice.identifier)}
|
|
141
|
+
onkeydown={(e) => handleKeyDown(e, choice.identifier)}
|
|
142
|
+
/>
|
|
143
|
+
{:else if choice.shape === 'rect'}
|
|
144
|
+
<!-- Rectangle: coords are x, y, width, height -->
|
|
145
|
+
<rect
|
|
146
|
+
x={coords[0]}
|
|
147
|
+
y={coords[1]}
|
|
148
|
+
width={coords[2]}
|
|
149
|
+
height={coords[3]}
|
|
150
|
+
fill={isSelected ? 'rgba(59, 130, 246, 0.3)' : isCorrectChoice ? 'rgba(34, 197, 94, 0.3)' : 'rgba(0, 0, 0, 0)'}
|
|
151
|
+
stroke={isSelected ? '#3b82f6' : isCorrectChoice ? '#22c55e' : 'transparent'}
|
|
152
|
+
stroke-width="2"
|
|
153
|
+
class="qti-hotspot-shape hover:fill-[rgba(59,130,246,0.1)] transition-all"
|
|
154
|
+
role="button"
|
|
155
|
+
aria-label={`Select hotspot ${choice.identifier}${isCorrectChoice ? '. Correct answer' : ''}`}
|
|
156
|
+
aria-pressed={isSelected ? 'true' : 'false'}
|
|
157
|
+
tabindex={disabled ? -1 : 0}
|
|
158
|
+
onclick={() => handleClick(choice.identifier)}
|
|
159
|
+
onkeydown={(e) => handleKeyDown(e, choice.identifier)}
|
|
160
|
+
/>
|
|
161
|
+
{:else if choice.shape === 'poly'}
|
|
162
|
+
<!-- Polygon: coords are x1,y1,x2,y2,... -->
|
|
163
|
+
{@const points = coords
|
|
164
|
+
.reduce((acc: string[], val: number, idx: number) => {
|
|
165
|
+
if (idx % 2 === 0 && idx + 1 < coords.length) {
|
|
166
|
+
acc.push(`${coords[idx]},${coords[idx + 1]}`);
|
|
167
|
+
}
|
|
168
|
+
return acc;
|
|
169
|
+
}, [])
|
|
170
|
+
.join(' ')}
|
|
171
|
+
<polygon
|
|
172
|
+
{points}
|
|
173
|
+
fill={isSelected ? 'rgba(59, 130, 246, 0.3)' : isCorrectChoice ? 'rgba(34, 197, 94, 0.3)' : 'rgba(0, 0, 0, 0)'}
|
|
174
|
+
stroke={isSelected ? '#3b82f6' : isCorrectChoice ? '#22c55e' : 'transparent'}
|
|
175
|
+
stroke-width="2"
|
|
176
|
+
class="qti-hotspot-shape hover:fill-[rgba(59,130,246,0.1)] transition-all"
|
|
177
|
+
role="button"
|
|
178
|
+
aria-label={`Select hotspot ${choice.identifier}${isCorrectChoice ? '. Correct answer' : ''}`}
|
|
179
|
+
aria-pressed={isSelected ? 'true' : 'false'}
|
|
180
|
+
tabindex={disabled ? -1 : 0}
|
|
181
|
+
onclick={() => handleClick(choice.identifier)}
|
|
182
|
+
onkeydown={(e) => handleKeyDown(e, choice.identifier)}
|
|
183
|
+
/>
|
|
184
|
+
{/if}
|
|
185
|
+
{/each}
|
|
186
|
+
</svg>
|
|
187
|
+
</div>
|
|
188
|
+
</div>
|
|
189
|
+
|
|
190
|
+
{#if parsedResponse || (isShowingCorrect && correctIds.length > 0)}
|
|
191
|
+
<div part="selected" class="alert alert-info">
|
|
192
|
+
{#if parsedResponse}
|
|
193
|
+
<span>{i18n?.t('interactions.hotspot.selected') ?? 'Selected:'} {parsedResponse}</span>
|
|
194
|
+
{/if}
|
|
195
|
+
{#if isShowingCorrect && correctIds.length > 0}
|
|
196
|
+
<span class="ml-2">Correct: {correctIds.join(', ')}</span>
|
|
197
|
+
{/if}
|
|
198
|
+
</div>
|
|
199
|
+
{/if}
|
|
200
|
+
{/if}
|
|
201
|
+
</div>
|
|
202
|
+
|
|
203
|
+
<style>
|
|
204
|
+
/* Minimal layout so this works without Tailwind utilities */
|
|
205
|
+
.qti-hotspot-interaction {
|
|
206
|
+
display: grid;
|
|
207
|
+
gap: 0.75rem;
|
|
208
|
+
}
|
|
209
|
+
.qti-hotspot-prompt {
|
|
210
|
+
margin: 0;
|
|
211
|
+
}
|
|
212
|
+
.qti-hotspot-stage {
|
|
213
|
+
position: relative;
|
|
214
|
+
display: block;
|
|
215
|
+
width: 100%;
|
|
216
|
+
max-width: 100%;
|
|
217
|
+
}
|
|
218
|
+
.qti-hotspot-media {
|
|
219
|
+
position: relative;
|
|
220
|
+
}
|
|
221
|
+
.qti-hotspot-image {
|
|
222
|
+
display: block;
|
|
223
|
+
width: 100%;
|
|
224
|
+
height: 100%;
|
|
225
|
+
}
|
|
226
|
+
/* Inline SVG injected via {@html} is not scoped; use :global to size it inside the shadow root. */
|
|
227
|
+
.qti-hotspot-image :global(svg) {
|
|
228
|
+
width: 100%;
|
|
229
|
+
height: 100%;
|
|
230
|
+
display: block;
|
|
231
|
+
}
|
|
232
|
+
.qti-hotspot-overlay {
|
|
233
|
+
position: absolute;
|
|
234
|
+
inset: 0;
|
|
235
|
+
cursor: pointer;
|
|
236
|
+
}
|
|
237
|
+
/* Tailwind `hover:` utilities won't exist everywhere; provide a real hover style too */
|
|
238
|
+
.qti-hotspot-shape {
|
|
239
|
+
transition: fill 120ms ease, stroke 120ms ease, opacity 120ms ease;
|
|
240
|
+
}
|
|
241
|
+
.qti-hotspot-shape:hover {
|
|
242
|
+
fill: rgba(59, 130, 246, 0.1);
|
|
243
|
+
}
|
|
244
|
+
</style>
|