@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,297 @@
1
+ <script lang="ts">
2
+ /**
3
+ * Sortable list component with keyboard, mouse, and touch support
4
+ * Accessible via drag-and-drop, keyboard (Space/Enter to grab, arrow keys to move), or touch
5
+ */
6
+
7
+ import type { I18nProvider } from '@pie-qti/i18n';
8
+ import { touchDrag } from '../utils/touchDragHelper.js';
9
+ import DragHandle from './DragHandle.svelte';
10
+ import '../styles/shared.css';
11
+
12
+ interface Item {
13
+ id: string;
14
+ text: string;
15
+ }
16
+
17
+ interface Props {
18
+ items: Item[];
19
+ orderedIds: string[];
20
+ correctOrder?: string[];
21
+ orientation?: 'vertical' | 'horizontal';
22
+ disabled?: boolean;
23
+ i18n?: I18nProvider;
24
+ onReorder: (newOrder: string[]) => void;
25
+ }
26
+
27
+ const {
28
+ items,
29
+ orderedIds,
30
+ correctOrder = [],
31
+ orientation = 'vertical',
32
+ disabled = false,
33
+ i18n,
34
+ onReorder,
35
+ }: Props = $props();
36
+
37
+ let draggedId = $state<string | null>(null);
38
+ let dragOverId = $state<string | null>(null);
39
+ let keyboardSelectedId = $state<string | null>(null); // Item picked up via keyboard
40
+ let announceText = $state<string>(''); // For screen reader announcements
41
+
42
+ // Mouse drag handlers
43
+ function handleDragStart(id: string) {
44
+ if (disabled) return;
45
+ draggedId = id;
46
+ }
47
+
48
+ function handleDragOver(event: DragEvent, id: string) {
49
+ if (disabled) return;
50
+ event.preventDefault();
51
+ dragOverId = id;
52
+ }
53
+
54
+ function handleDragLeave() {
55
+ dragOverId = null;
56
+ }
57
+
58
+ function handleDrop(event: DragEvent, dropId: string) {
59
+ if (disabled || !draggedId) return;
60
+ event.preventDefault();
61
+
62
+ if (draggedId !== dropId) {
63
+ const oldIndex = orderedIds.indexOf(draggedId);
64
+ const newIndex = orderedIds.indexOf(dropId);
65
+
66
+ const newOrder = [...orderedIds];
67
+ newOrder.splice(oldIndex, 1);
68
+ newOrder.splice(newIndex, 0, draggedId);
69
+
70
+ onReorder(newOrder);
71
+ }
72
+
73
+ draggedId = null;
74
+ dragOverId = null;
75
+ }
76
+
77
+ function handleDragEnd() {
78
+ draggedId = null;
79
+ dragOverId = null;
80
+ }
81
+
82
+ // Keyboard handlers
83
+ function handleKeyDown(event: KeyboardEvent, id: string) {
84
+ if (disabled) return;
85
+
86
+ const currentIndex = orderedIds.indexOf(id);
87
+ const item = items.find((i) => i.id === id);
88
+ const itemName = item?.text || 'Item';
89
+
90
+ // Space or Enter: Pick up or drop item
91
+ if (event.key === ' ' || event.key === 'Enter') {
92
+ handlePickupOrDrop(event, id, itemName, currentIndex);
93
+ return;
94
+ }
95
+
96
+ // Escape: Cancel keyboard selection
97
+ if (event.key === 'Escape' && keyboardSelectedId) {
98
+ handleCancelSelection(event, itemName);
99
+ return;
100
+ }
101
+
102
+ // Arrow keys: Move item or change focus
103
+ if (keyboardSelectedId === id) {
104
+ handleArrowKeyMovement(event, itemName, currentIndex);
105
+ }
106
+ }
107
+
108
+ function handlePickupOrDrop(
109
+ event: KeyboardEvent,
110
+ id: string,
111
+ itemName: string,
112
+ currentIndex: number
113
+ ) {
114
+ event.preventDefault();
115
+
116
+ if (keyboardSelectedId === id) {
117
+ // Drop item
118
+ keyboardSelectedId = null;
119
+ announceText = `${itemName} dropped at position ${currentIndex + 1} of ${orderedIds.length}`;
120
+ } else {
121
+ // Pick up item
122
+ keyboardSelectedId = id;
123
+ announceText = `${itemName} grabbed. Current position ${currentIndex + 1} of ${orderedIds.length}. Use arrow keys to move, Space or Enter to drop.`;
124
+ }
125
+ }
126
+
127
+ function handleCancelSelection(event: KeyboardEvent, itemName: string) {
128
+ event.preventDefault();
129
+ announceText = `${itemName} selection cancelled`;
130
+ keyboardSelectedId = null;
131
+ }
132
+
133
+ function handleArrowKeyMovement(event: KeyboardEvent, itemName: string, currentIndex: number) {
134
+ if (event.key === 'ArrowUp' && orientation === 'vertical' && currentIndex > 0) {
135
+ event.preventDefault();
136
+ moveItem(currentIndex, currentIndex - 1);
137
+ announceText = `${itemName} moved to position ${currentIndex} of ${orderedIds.length}`;
138
+ } else if (
139
+ event.key === 'ArrowDown' &&
140
+ orientation === 'vertical' &&
141
+ currentIndex < orderedIds.length - 1
142
+ ) {
143
+ event.preventDefault();
144
+ moveItem(currentIndex, currentIndex + 1);
145
+ announceText = `${itemName} moved to position ${currentIndex + 2} of ${orderedIds.length}`;
146
+ } else if (event.key === 'ArrowLeft' && orientation === 'horizontal' && currentIndex > 0) {
147
+ event.preventDefault();
148
+ moveItem(currentIndex, currentIndex - 1);
149
+ announceText = `${itemName} moved to position ${currentIndex} of ${orderedIds.length}`;
150
+ } else if (
151
+ event.key === 'ArrowRight' &&
152
+ orientation === 'horizontal' &&
153
+ currentIndex < orderedIds.length - 1
154
+ ) {
155
+ event.preventDefault();
156
+ moveItem(currentIndex, currentIndex + 1);
157
+ announceText = `${itemName} moved to position ${currentIndex + 2} of ${orderedIds.length}`;
158
+ }
159
+ }
160
+
161
+ function moveItem(fromIndex: number, toIndex: number) {
162
+ const newOrder = [...orderedIds];
163
+ const [movedItem] = newOrder.splice(fromIndex, 1);
164
+ newOrder.splice(toIndex, 0, movedItem);
165
+ onReorder(newOrder);
166
+ }
167
+ </script>
168
+
169
+ <!-- Screen reader announcements -->
170
+ <div aria-live="polite" aria-atomic="true" class="sr-only">
171
+ {announceText}
172
+ </div>
173
+
174
+ <!-- Instructions for keyboard users -->
175
+ <div id="sortable-instructions" class="sr-only">
176
+ Press Space or Enter to grab an item. Use arrow keys to move the item. Press Space or Enter again to drop. Press Escape to cancel.
177
+ </div>
178
+
179
+ <div
180
+ role="list"
181
+ aria-label={i18n?.t('interactions.order.listLabel') ?? 'Reorderable list of choices'}
182
+ aria-describedby="sortable-instructions"
183
+ data-orientation={orientation}
184
+ part="list"
185
+ class="qti-sortable-list space-y-2"
186
+ class:flex={orientation === 'horizontal'}
187
+ class:gap-2={orientation === 'horizontal'}
188
+ >
189
+ {#each orderedIds as id, index (id)}
190
+ {@const item = items.find((i) => i.id === id)}
191
+ {#if item}
192
+ {@const isGrabbed = keyboardSelectedId === id}
193
+ {@const correctIndex = correctOrder.indexOf(id)}
194
+ {@const isCorrect = correctIndex !== -1 && correctIndex === index}
195
+ <div role="listitem">
196
+ <button
197
+ type="button"
198
+ draggable={!disabled}
199
+ use:touchDrag
200
+ ondragstart={() => handleDragStart(id)}
201
+ ondragover={(e) => handleDragOver(e, id)}
202
+ ondragleave={handleDragLeave}
203
+ ondrop={(e) => handleDrop(e, id)}
204
+ ondragend={handleDragEnd}
205
+ onkeydown={(e) => handleKeyDown(e, id)}
206
+ disabled={disabled}
207
+ aria-label="{item.text}. Position {index + 1} of {orderedIds.length}{isGrabbed ? '. Grabbed. Use arrow keys to move.' : ''}{isCorrect ? '. Correct position' : ''}"
208
+ aria-grabbed={isGrabbed}
209
+ data-grabbed={isGrabbed}
210
+ data-correct={isCorrect}
211
+ data-disabled={disabled}
212
+ part="item"
213
+ class="qti-sortable-item flex items-center gap-2 p-3 bg-base-200 rounded select-none transition-all w-full text-left"
214
+ class:cursor-grab={!disabled && !isGrabbed}
215
+ class:cursor-not-allowed={disabled}
216
+ class:opacity-50={disabled || draggedId === id}
217
+ class:ring-2={(dragOverId === id && draggedId !== id) || isGrabbed}
218
+ class:ring-primary={(dragOverId === id && draggedId !== id) || isGrabbed}
219
+ class:bg-primary={isGrabbed}
220
+ class:bg-opacity-20={isGrabbed || isCorrect}
221
+ class:bg-success={isCorrect}
222
+ class:border-success={isCorrect}
223
+ >
224
+ <span part="index" class="qti-sortable-index badge {isCorrect ? 'badge-success' : 'badge-neutral'}">{index + 1}</span>
225
+ <DragHandle size={1} opacity={0.5} class="text-base-content" />
226
+ <span part="text" class="qti-sortable-text flex-1">{item.text}</span>
227
+ {#if isCorrect}
228
+ <span class="badge badge-success badge-sm">{i18n?.t('interactions.choice.correct', 'Correct') ?? 'Correct'}</span>
229
+ {/if}
230
+ </button>
231
+ </div>
232
+ {/if}
233
+ {/each}
234
+ </div>
235
+
236
+ <style>
237
+ /* Minimal, self-contained layout so the component doesn't break when Tailwind utilities aren't present */
238
+ .qti-sortable-list {
239
+ display: flex;
240
+ flex-direction: column;
241
+ gap: 0.5rem;
242
+ }
243
+ .qti-sortable-list[data-orientation='horizontal'] {
244
+ flex-direction: row;
245
+ flex-wrap: wrap;
246
+ }
247
+
248
+ .qti-sortable-item {
249
+ display: flex;
250
+ align-items: center;
251
+ gap: 0.5rem;
252
+ width: 100%;
253
+ padding: 0.75rem;
254
+ border-radius: 0.5rem;
255
+ border: 1px solid var(--color-base-300, oklch(95% 0 0));
256
+ background: var(--color-base-200, oklch(98% 0 0));
257
+ color: var(--color-base-content, oklch(21% 0 0));
258
+ text-align: left;
259
+ user-select: none;
260
+ transition: background-color 120ms ease, border-color 120ms ease, outline-color 120ms ease,
261
+ opacity 120ms ease;
262
+ }
263
+ .qti-sortable-item:focus-visible {
264
+ outline: 2px solid var(--color-primary, oklch(45% 0.24 277));
265
+ outline-offset: 2px;
266
+ }
267
+ .qti-sortable-item[data-grabbed='true'] {
268
+ border-color: var(--color-primary, oklch(45% 0.24 277));
269
+ background: color-mix(in oklch, var(--color-primary, oklch(45% 0.24 277)) 8%, transparent);
270
+ }
271
+ .qti-sortable-item[data-correct='true'] {
272
+ border-color: var(--color-success, oklch(76% 0.177 163.223));
273
+ background: color-mix(in oklch, var(--color-success, oklch(76% 0.177 163.223)) 8%, transparent);
274
+ }
275
+ .qti-sortable-item[data-disabled='true'] {
276
+ opacity: 0.55;
277
+ cursor: not-allowed;
278
+ }
279
+ .qti-sortable-index {
280
+ display: inline-flex;
281
+ align-items: center;
282
+ justify-content: center;
283
+ min-width: 1.75rem;
284
+ height: 1.25rem;
285
+ padding: 0 0.4rem;
286
+ border-radius: 9999px;
287
+ font-size: 0.75rem;
288
+ font-weight: 600;
289
+ border: 1px solid var(--color-base-300, oklch(95% 0 0));
290
+ background: var(--color-base-100, oklch(100% 0 0));
291
+ color: var(--color-base-content, oklch(21% 0 0));
292
+ }
293
+ .qti-sortable-text {
294
+ flex: 1 1 auto;
295
+ min-width: 0;
296
+ }
297
+ </style>
@@ -0,0 +1,304 @@
1
+ /// <reference lib="dom" />
2
+
3
+ /**
4
+ * MathLive Editor Extension for TipTap
5
+ *
6
+ * Integrates MathLive (visual math editor) with TipTap's Mathematics extension.
7
+ * Provides a modal dialog with MathLive mathfield for editing LaTeX formulas.
8
+ *
9
+ * Note: MathLive CSS is loaded dynamically to avoid build issues with package exports.
10
+ */
11
+
12
+ import type { MathfieldElement } from 'mathlive';
13
+
14
+ // Dynamically import MathLive to register the custom element
15
+ let mathLiveLoaded: Promise<void> | null = null;
16
+
17
+ async function ensureMathLiveLoaded(): Promise<void> {
18
+ if (!mathLiveLoaded && typeof window !== 'undefined') {
19
+ mathLiveLoaded = (async () => {
20
+ // Import MathLive to register the <math-field> custom element
21
+ await import('mathlive');
22
+
23
+ // Load MathLive CSS if not already loaded
24
+ if (!document.querySelector('link[href*="mathlive"]')) {
25
+ const link = document.createElement('link');
26
+ link.rel = 'stylesheet';
27
+ link.href = 'https://cdn.jsdelivr.net/npm/mathlive@0.108.2/dist/mathlive-static.css';
28
+ document.head.appendChild(link);
29
+ }
30
+ })();
31
+ }
32
+ return mathLiveLoaded || Promise.resolve();
33
+ }
34
+
35
+ export interface MathLiveEditorOptions {
36
+ /** Called when user confirms the LaTeX input */
37
+ onConfirm: (latex: string) => void;
38
+ /** Called when user cancels */
39
+ onCancel: () => void;
40
+ /** Initial LaTeX value */
41
+ initialLatex?: string;
42
+ /** Whether this is inline or block math */
43
+ isBlock?: boolean;
44
+ /** Optional i18n provider for translations */
45
+ i18n?: { t: (key: string) => string | undefined };
46
+ }
47
+
48
+ /**
49
+ * Opens a modal dialog with MathLive editor for LaTeX input
50
+ */
51
+ export async function openMathLiveEditor(options: MathLiveEditorOptions): Promise<() => void> {
52
+ const { onConfirm, onCancel, initialLatex = '', isBlock = false } = options;
53
+
54
+ // Ensure MathLive is loaded and registered before creating the mathfield
55
+ await ensureMathLiveLoaded();
56
+
57
+ // Create modal overlay - use DaisyUI modal classes
58
+ const overlay = document.createElement('div');
59
+ overlay.className = 'modal modal-open';
60
+ overlay.style.cssText = `
61
+ position: fixed;
62
+ top: 0;
63
+ left: 0;
64
+ right: 0;
65
+ bottom: 0;
66
+ background: rgba(0, 0, 0, 0.5);
67
+ display: flex;
68
+ align-items: center;
69
+ justify-content: center;
70
+ z-index: 9999;
71
+ `;
72
+
73
+ // Create modal dialog - use DaisyUI modal-box class
74
+ const modal = document.createElement('div');
75
+ modal.className = 'modal-box max-w-2xl';
76
+ modal.style.cssText = `
77
+ padding: 1.5rem;
78
+ position: relative;
79
+ z-index: 10000;
80
+ `;
81
+
82
+ // Create header - use DaisyUI font-bold text-lg
83
+ const header = document.createElement('h3');
84
+ header.textContent = isBlock ? (options.i18n?.t('interactions.extendedText.insertBlockMath') ?? 'Insert Block Math') : (options.i18n?.t('interactions.extendedText.insertInlineMath') ?? 'Insert Inline Math');
85
+ header.className = 'font-bold text-lg mb-4';
86
+
87
+ // Create form control wrapper
88
+ const formControl = document.createElement('div');
89
+ formControl.className = 'form-control';
90
+
91
+ // Create label for mathfield
92
+ const label = document.createElement('label');
93
+ label.className = 'label';
94
+ const labelText = document.createElement('span');
95
+ labelText.className = 'label-text';
96
+ labelText.textContent = options.i18n?.t('interactions.extendedText.enterMathExpression') ?? 'Enter your math expression:';
97
+ label.appendChild(labelText);
98
+
99
+ // Create MathLive mathfield - style like DaisyUI input
100
+ const mathfield = document.createElement('math-field') as MathfieldElement;
101
+ mathfield.value = initialLatex;
102
+ mathfield.className = 'input input-bordered w-full';
103
+ mathfield.style.cssText = `
104
+ display: block;
105
+ min-height: 100px;
106
+ padding: 0.75rem;
107
+ font-size: 1.125rem;
108
+ `;
109
+
110
+ // Set MathLive options for better UX via attributes
111
+ // Use 'manual' mode so keyboard only shows when explicitly requested
112
+ mathfield.setAttribute('virtual-keyboard-mode', 'manual');
113
+ mathfield.setAttribute('smart-mode', 'on');
114
+ // Explicitly enable virtual keyboard
115
+ mathfield.setAttribute('use-shared-virtual-keyboard', 'true');
116
+ // Hide the built-in virtual keyboard toggle
117
+ mathfield.setAttribute('virtual-keyboard-toggle-glyph', '');
118
+
119
+ // Inject CSS to ensure virtual keyboard appears above modal and accepts clicks
120
+ const style = document.createElement('style');
121
+ style.textContent = `
122
+ /* Ensure MathLive virtual keyboard appears above the modal and is clickable */
123
+ .ML__keyboard {
124
+ z-index: 10001 !important;
125
+ pointer-events: auto !important;
126
+ }
127
+ /* Hide the built-in keyboard toggle button in the mathfield - try multiple selectors */
128
+ .ML__virtual-keyboard-toggle,
129
+ .ML__keyboard-toggle,
130
+ math-field::part(virtual-keyboard-toggle) {
131
+ display: none !important;
132
+ visibility: hidden !important;
133
+ opacity: 0 !important;
134
+ pointer-events: none !important;
135
+ }
136
+ `;
137
+ document.head.appendChild(style);
138
+
139
+ // Create LaTeX preview label
140
+ const previewLabel = document.createElement('label');
141
+ previewLabel.className = 'label';
142
+ const previewLabelText = document.createElement('span');
143
+ previewLabelText.className = 'label-text';
144
+ previewLabelText.textContent = options.i18n?.t('interactions.extendedText.latexSource') ?? 'LaTeX source:';
145
+ previewLabel.appendChild(previewLabelText);
146
+
147
+ // Create LaTeX preview/source - use alert info styling
148
+ const latexPreview = document.createElement('div');
149
+ latexPreview.className = 'alert alert-info';
150
+ latexPreview.style.cssText = `
151
+ font-family: 'Monaco', 'Courier New', monospace;
152
+ font-size: 0.875rem;
153
+ word-break: break-all;
154
+ margin-bottom: 1rem;
155
+ `;
156
+ latexPreview.textContent = initialLatex || '(LaTeX will appear here)';
157
+
158
+ // Update preview when mathfield changes
159
+ mathfield.addEventListener('input', () => {
160
+ const latex = mathfield.value;
161
+ latexPreview.textContent = latex || '(LaTeX will appear here)';
162
+ });
163
+
164
+ // Create button container - use DaisyUI modal-action class
165
+ const buttons = document.createElement('div');
166
+ buttons.className = 'modal-action';
167
+ buttons.style.cssText = 'justify-content: space-between;';
168
+
169
+ // Create left-side buttons (keyboard toggle)
170
+ const leftButtons = document.createElement('div');
171
+ leftButtons.style.cssText = 'display: flex; gap: 0.5rem;';
172
+
173
+ // Create Keyboard Toggle button
174
+ const keyboardBtn = document.createElement('button');
175
+ keyboardBtn.textContent = `⌨️ ${options.i18n?.t('interactions.extendedText.keyboard') ?? 'Keyboard'}`;
176
+ keyboardBtn.type = 'button';
177
+ keyboardBtn.className = 'btn btn-sm btn-outline';
178
+ keyboardBtn.onclick = () => {
179
+ if (mathfield.executeCommand) {
180
+ mathfield.executeCommand('toggleVirtualKeyboard');
181
+ }
182
+ };
183
+
184
+ leftButtons.appendChild(keyboardBtn);
185
+
186
+ // Create right-side buttons (Cancel/Insert)
187
+ const rightButtons = document.createElement('div');
188
+ rightButtons.style.cssText = 'display: flex; gap: 0.5rem;';
189
+
190
+ // Create Cancel button - use DaisyUI classes
191
+ const cancelBtn = document.createElement('button');
192
+ cancelBtn.textContent = options.i18n?.t('common.cancel') ?? 'Cancel';
193
+ cancelBtn.type = 'button';
194
+ cancelBtn.className = 'btn btn-ghost';
195
+
196
+ // Create Insert button - use DaisyUI classes
197
+ const insertBtn = document.createElement('button');
198
+ insertBtn.textContent = initialLatex
199
+ ? (options.i18n?.t('interactions.extendedText.update') ?? 'Update')
200
+ : (options.i18n?.t('interactions.extendedText.insert') ?? 'Insert');
201
+ insertBtn.type = 'button';
202
+ insertBtn.className = 'btn btn-primary';
203
+
204
+ rightButtons.appendChild(cancelBtn);
205
+ rightButtons.appendChild(insertBtn);
206
+
207
+ // Assemble modal
208
+ buttons.appendChild(leftButtons);
209
+ buttons.appendChild(rightButtons);
210
+
211
+ // Assemble form-control
212
+ formControl.appendChild(label);
213
+ formControl.appendChild(mathfield);
214
+ formControl.appendChild(previewLabel);
215
+ formControl.appendChild(latexPreview);
216
+
217
+ modal.appendChild(header);
218
+ modal.appendChild(formControl);
219
+ modal.appendChild(buttons);
220
+
221
+ overlay.appendChild(modal);
222
+
223
+ // Cleanup function
224
+ const cleanup = () => {
225
+ // Remove event listeners to prevent memory leaks
226
+ cancelBtn.removeEventListener('click', handleCancel);
227
+ insertBtn.removeEventListener('click', handleConfirm);
228
+ document.removeEventListener('keydown', handleEscape);
229
+ overlay.removeEventListener('click', handleOverlayClick);
230
+
231
+ // Hide virtual keyboard before cleanup
232
+ if (mathfield.executeCommand) {
233
+ mathfield.executeCommand('hideVirtualKeyboard');
234
+ }
235
+
236
+ // Remove DOM elements
237
+ overlay.remove();
238
+ style.remove();
239
+
240
+ // Restore body scroll
241
+ document.body.style.overflow = '';
242
+ };
243
+
244
+ // Event handlers
245
+ const handleCancel = () => {
246
+ cleanup();
247
+ onCancel();
248
+ };
249
+
250
+ const handleConfirm = () => {
251
+ const latex = mathfield.value;
252
+ cleanup();
253
+ onConfirm(latex);
254
+ };
255
+
256
+ const handleEscape = (e: KeyboardEvent) => {
257
+ if (e.key === 'Escape') {
258
+ handleCancel();
259
+ }
260
+ };
261
+
262
+ const handleOverlayClick = (e: MouseEvent) => {
263
+ // Don't close if clicking on the modal content or virtual keyboard
264
+ const target = e.target as HTMLElement;
265
+
266
+ // Check if click is on overlay background (not modal or keyboard)
267
+ if (target === overlay) {
268
+ // Additional check: don't close if virtual keyboard is open
269
+ // MathLive virtual keyboard has class 'ML__keyboard'
270
+ const virtualKeyboard = document.querySelector('.ML__keyboard');
271
+ if (!virtualKeyboard) {
272
+ handleCancel();
273
+ }
274
+ }
275
+ };
276
+
277
+ // Attach event listeners
278
+ cancelBtn.addEventListener('click', handleCancel);
279
+ insertBtn.addEventListener('click', handleConfirm);
280
+ document.addEventListener('keydown', handleEscape);
281
+ overlay.addEventListener('click', handleOverlayClick);
282
+
283
+ // Prevent body scroll when modal is open
284
+ document.body.style.overflow = 'hidden';
285
+
286
+ // Add to DOM
287
+ document.body.appendChild(overlay);
288
+
289
+ // Focus the mathfield (but don't auto-show keyboard)
290
+ setTimeout(() => {
291
+ mathfield.focus();
292
+ // Try to programmatically disable the built-in toggle via options
293
+ try {
294
+ (mathfield as any).setOptions({
295
+ virtualKeyboardToggleGlyph: '',
296
+ });
297
+ } catch (e) {
298
+ // Silently ignore if this API doesn't exist
299
+ }
300
+ }, 100);
301
+
302
+ // Return cleanup function
303
+ return cleanup;
304
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Shared utilities and helpers for QTI interaction plugins
3
+ *
4
+ * This module exports common utilities used across all interaction plugins.
5
+ */
6
+
7
+
8
+ // Typeset action for math rendering
9
+ export * from './actions/typesetAction.js';
10
+ // MathLive editor extension for visual math input
11
+ export * from './extensions/MathLiveEditor.js';
12
+ // Event helpers for creating CustomEvents
13
+ export * from './utils/eventHelpers.js';
14
+ // Pair helpers for matching/association interactions
15
+ export * from './utils/pairHelpers.js';
16
+ // Touch drag helper for mobile-friendly drag-and-drop
17
+ export * from './utils/touchDragHelper.js';
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Shared styles for QTI components
3
+ */
4
+
5
+ /* Screen reader only - hides content visually but keeps it accessible */
6
+ .sr-only {
7
+ position: absolute;
8
+ width: 1px;
9
+ height: 1px;
10
+ padding: 0;
11
+ margin: -1px;
12
+ overflow: hidden;
13
+ clip: rect(0, 0, 0, 0);
14
+ white-space: nowrap;
15
+ border-width: 0;
16
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Event emission helpers for QTI components
3
+ */
4
+
5
+ /**
6
+ * Creates a standardized QTI change event
7
+ * @param responseId - The response identifier
8
+ * @param value - The response value
9
+ * @returns A CustomEvent that can be dispatched
10
+ */
11
+ export function createQtiChangeEvent(responseId: string | undefined, value: any): CustomEvent {
12
+ return new CustomEvent('qti-change', {
13
+ detail: {
14
+ responseId,
15
+ value,
16
+ timestamp: Date.now(),
17
+ },
18
+ bubbles: true,
19
+ composed: true,
20
+ });
21
+ }
22
+
23
+ /**
24
+ * Emits a QTI change event from an element
25
+ * @param element - The element to dispatch from (usually component root)
26
+ * @param responseId - The response identifier
27
+ * @param value - The response value
28
+ */
29
+ export function emitQtiChange(
30
+ element: HTMLElement | EventTarget,
31
+ responseId: string | undefined,
32
+ value: any
33
+ ): void {
34
+ const event = createQtiChangeEvent(responseId, value);
35
+ element.dispatchEvent(event);
36
+ }