@pie-qti/item-player 0.1.8 → 0.1.10

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 (199) hide show
  1. package/README.md +17 -0
  2. package/dist/catalog/applyGlossaryTriggers.d.ts +13 -2
  3. package/dist/catalog/applyGlossaryTriggers.d.ts.map +1 -1
  4. package/dist/catalog/glossaryAction.d.ts +4 -2
  5. package/dist/catalog/glossaryAction.d.ts.map +1 -1
  6. package/dist/components/utils/index.d.ts +1 -2
  7. package/dist/components/utils/index.d.ts.map +1 -1
  8. package/dist/components/utils/index.js +1 -2
  9. package/dist/core/ComponentRegistry.d.ts +10 -1
  10. package/dist/core/ComponentRegistry.d.ts.map +1 -1
  11. package/dist/core/ComponentRegistry.js +42 -1
  12. package/dist/core/Player.d.ts +10 -2
  13. package/dist/core/Player.d.ts.map +1 -1
  14. package/dist/core/Player.js +54 -46
  15. package/dist/document/AssessmentItemDocument.d.ts +1 -1
  16. package/dist/element.d.ts +15 -0
  17. package/dist/element.js +1921 -1785
  18. package/dist/element.js.map +1 -1
  19. package/dist/extraction/ExtractionRegistry.d.ts +1 -1
  20. package/dist/extraction/createContext.d.ts +1 -1
  21. package/dist/extraction/index.d.ts +2 -2
  22. package/dist/extraction/index.d.ts.map +1 -1
  23. package/dist/extraction/index.js +1 -1
  24. package/dist/extraction/interactionExtractionPipeline.d.ts +1 -1
  25. package/dist/extraction/interactionSecurity.d.ts +1 -1
  26. package/dist/extraction/types.d.ts +1 -1
  27. package/dist/presentation/itemPresentationPlan.d.ts +54 -0
  28. package/dist/presentation/itemPresentationPlan.d.ts.map +1 -0
  29. package/dist/presentation/itemPresentationPlan.js +98 -0
  30. package/dist/types/index.d.ts +17 -2
  31. package/dist/types/index.d.ts.map +1 -1
  32. package/dist/types/index.js +0 -3
  33. package/dist/web-components/QTIInteractionBase.d.ts +1 -1
  34. package/dist/web-components/QTIInteractionElement.d.ts +1 -1
  35. package/package.json +10 -12
  36. package/src/__tests__/catalog.test.ts +0 -267
  37. package/src/__tests__/fixtures/pci/echo-pci.js +0 -49
  38. package/src/__tests__/pci.test.ts +0 -353
  39. package/src/__tests__/qti3-attribute-extraction.test.ts +0 -174
  40. package/src/__tests__/qti3-debug.test.ts +0 -140
  41. package/src/__tests__/qti3-extraction.test.ts +0 -309
  42. package/src/catalog/applyGlossaryTriggers.ts +0 -372
  43. package/src/catalog/catalogExtractor.ts +0 -130
  44. package/src/catalog/catalogLookup.ts +0 -53
  45. package/src/catalog/glossaryAction.ts +0 -38
  46. package/src/catalog/types.ts +0 -37
  47. package/src/components/ItemBody.svelte +0 -363
  48. package/src/components/ItemPlayer.svelte +0 -163
  49. package/src/components/ModalFeedbackDisplay.svelte +0 -97
  50. package/src/components/actions/typesetAction.ts +0 -74
  51. package/src/components/index.ts +0 -22
  52. package/src/components/utils/assignProps.ts +0 -2
  53. package/src/components/utils/feedbackUtils.ts +0 -140
  54. package/src/components/utils/index.ts +0 -13
  55. package/src/components/utils/stimulusRender.ts +0 -93
  56. package/src/components/utils/stylesheetRender.ts +0 -58
  57. package/src/core/ComponentRegistry.ts +0 -248
  58. package/src/core/Player.ts +0 -1618
  59. package/src/core/Plugin.ts +0 -123
  60. package/src/core/PluginManager.ts +0 -182
  61. package/src/core/constants.ts +0 -117
  62. package/src/core/declarations.ts +0 -171
  63. package/src/core/parsingLimits.ts +0 -62
  64. package/src/core/random.ts +0 -24
  65. package/src/core/rolePolicy.ts +0 -34
  66. package/src/core/sanitizer.ts +0 -305
  67. package/src/core/trustedTypes.ts +0 -63
  68. package/src/core/urlPolicy.ts +0 -98
  69. package/src/document/AssessmentItemDocument.ts +0 -247
  70. package/src/element.ts +0 -128
  71. package/src/extensions/MathLiveEditor.ts +0 -313
  72. package/src/extraction/ExtractionRegistry.ts +0 -335
  73. package/src/extraction/createContext.ts +0 -52
  74. package/src/extraction/extractors/associateExtractor.ts +0 -1
  75. package/src/extraction/extractors/choiceExtractor.ts +0 -1
  76. package/src/extraction/extractors/conformance-qti3-advanced.test.ts +0 -239
  77. package/src/extraction/extractors/conformance-qti3-basic.test.ts +0 -82
  78. package/src/extraction/extractors/conformance.test.ts +0 -208
  79. package/src/extraction/extractors/customExtractor.ts +0 -1
  80. package/src/extraction/extractors/drawingExtractor.ts +0 -1
  81. package/src/extraction/extractors/endAttemptExtractor.ts +0 -1
  82. package/src/extraction/extractors/extendedTextExtractor.ts +0 -1
  83. package/src/extraction/extractors/gapMatchExtractor.ts +0 -1
  84. package/src/extraction/extractors/graphicAssociateExtractor.ts +0 -1
  85. package/src/extraction/extractors/graphicGapMatchExtractor.ts +0 -1
  86. package/src/extraction/extractors/graphicOrderExtractor.ts +0 -1
  87. package/src/extraction/extractors/hotspotExtractor.ts +0 -1
  88. package/src/extraction/extractors/hottextExtractor.ts +0 -1
  89. package/src/extraction/extractors/index.ts +0 -9
  90. package/src/extraction/extractors/inlineChoiceExtractor.ts +0 -1
  91. package/src/extraction/extractors/matchExtractor.ts +0 -1
  92. package/src/extraction/extractors/mediaExtractor.ts +0 -1
  93. package/src/extraction/extractors/orderExtractor.ts +0 -1
  94. package/src/extraction/extractors/portableCustomExtractor.ts +0 -1
  95. package/src/extraction/extractors/positionObjectExtractor.ts +0 -1
  96. package/src/extraction/extractors/selectPointExtractor.ts +0 -1
  97. package/src/extraction/extractors/sliderExtractor.ts +0 -1
  98. package/src/extraction/extractors/textEntryExtractor.ts +0 -1
  99. package/src/extraction/extractors/uploadExtractor.ts +0 -1
  100. package/src/extraction/index.ts +0 -108
  101. package/src/extraction/interactionExtractionPipeline.ts +0 -70
  102. package/src/extraction/interactionSecurity.ts +0 -60
  103. package/src/extraction/types.ts +0 -246
  104. package/src/extraction/utils.ts +0 -243
  105. package/src/iframe/IFramePlayerHost.ts +0 -265
  106. package/src/iframe/index.ts +0 -11
  107. package/src/iframe/protocol.ts +0 -137
  108. package/src/index.ts +0 -101
  109. package/src/interactions/associate/extractor.ts +0 -133
  110. package/src/interactions/associate/index.ts +0 -2
  111. package/src/interactions/associate/types.ts +0 -1
  112. package/src/interactions/choice/extractor.ts +0 -121
  113. package/src/interactions/choice/index.ts +0 -2
  114. package/src/interactions/choice/types.ts +0 -1
  115. package/src/interactions/custom/extractor.ts +0 -72
  116. package/src/interactions/custom/index.ts +0 -2
  117. package/src/interactions/custom/types.ts +0 -1
  118. package/src/interactions/drawing/extractor.ts +0 -113
  119. package/src/interactions/drawing/index.ts +0 -2
  120. package/src/interactions/drawing/types.ts +0 -1
  121. package/src/interactions/end-attempt/extractor.ts +0 -74
  122. package/src/interactions/end-attempt/index.ts +0 -2
  123. package/src/interactions/end-attempt/types.ts +0 -1
  124. package/src/interactions/extended-text/extractor.ts +0 -92
  125. package/src/interactions/extended-text/index.ts +0 -2
  126. package/src/interactions/extended-text/types.ts +0 -1
  127. package/src/interactions/gap-match/extractor.ts +0 -193
  128. package/src/interactions/gap-match/index.ts +0 -2
  129. package/src/interactions/gap-match/types.ts +0 -1
  130. package/src/interactions/graphic-associate/extractor.ts +0 -192
  131. package/src/interactions/graphic-associate/index.ts +0 -2
  132. package/src/interactions/graphic-associate/types.ts +0 -1
  133. package/src/interactions/graphic-gap-match/extractor.ts +0 -257
  134. package/src/interactions/graphic-gap-match/index.ts +0 -2
  135. package/src/interactions/graphic-gap-match/types.ts +0 -1
  136. package/src/interactions/graphic-order/extractor.ts +0 -162
  137. package/src/interactions/graphic-order/index.ts +0 -2
  138. package/src/interactions/graphic-order/types.ts +0 -1
  139. package/src/interactions/hotspot/extractor.ts +0 -208
  140. package/src/interactions/hotspot/index.ts +0 -2
  141. package/src/interactions/hotspot/types.ts +0 -1
  142. package/src/interactions/hottext/extractor.ts +0 -163
  143. package/src/interactions/hottext/index.ts +0 -2
  144. package/src/interactions/hottext/types.ts +0 -1
  145. package/src/interactions/index.ts +0 -52
  146. package/src/interactions/inline/index.ts +0 -1
  147. package/src/interactions/inline/render-plan.ts +0 -105
  148. package/src/interactions/inline-choice/InlineChoice.svelte +0 -57
  149. package/src/interactions/inline-choice/extractor.ts +0 -105
  150. package/src/interactions/inline-choice/index.ts +0 -2
  151. package/src/interactions/inline-choice/types.ts +0 -1
  152. package/src/interactions/match/extractor.ts +0 -187
  153. package/src/interactions/match/index.ts +0 -2
  154. package/src/interactions/match/types.ts +0 -1
  155. package/src/interactions/media/extractor.ts +0 -168
  156. package/src/interactions/media/index.ts +0 -2
  157. package/src/interactions/media/types.ts +0 -1
  158. package/src/interactions/modules.ts +0 -111
  159. package/src/interactions/order/extractor.ts +0 -120
  160. package/src/interactions/order/index.ts +0 -2
  161. package/src/interactions/order/types.ts +0 -1
  162. package/src/interactions/portable-custom/extractor.ts +0 -122
  163. package/src/interactions/portable-custom/index.ts +0 -2
  164. package/src/interactions/portable-custom/types.ts +0 -1
  165. package/src/interactions/position-object/extractor.ts +0 -243
  166. package/src/interactions/position-object/index.ts +0 -2
  167. package/src/interactions/position-object/types.ts +0 -1
  168. package/src/interactions/select-point/extractor.ts +0 -143
  169. package/src/interactions/select-point/index.ts +0 -2
  170. package/src/interactions/select-point/types.ts +0 -1
  171. package/src/interactions/shared/index.ts +0 -1
  172. package/src/interactions/shared/types.ts +0 -430
  173. package/src/interactions/slider/extractor.ts +0 -102
  174. package/src/interactions/slider/index.ts +0 -2
  175. package/src/interactions/slider/types.ts +0 -1
  176. package/src/interactions/text-entry/InlineTextEntry.svelte +0 -75
  177. package/src/interactions/text-entry/extractor.ts +0 -79
  178. package/src/interactions/text-entry/index.ts +0 -2
  179. package/src/interactions/text-entry/types.ts +0 -1
  180. package/src/interactions/upload/extractor.ts +0 -81
  181. package/src/interactions/upload/index.ts +0 -2
  182. package/src/interactions/upload/types.ts +0 -1
  183. package/src/pci/PciHost.ts +0 -138
  184. package/src/pci/types.ts +0 -70
  185. package/src/pnp/applyPnp.ts +0 -29
  186. package/src/pnp/parsePnpXml.ts +0 -215
  187. package/src/pnp/types.ts +0 -75
  188. package/src/server.ts +0 -14
  189. package/src/types/index.ts +0 -388
  190. package/src/types/interactions.ts +0 -8
  191. package/src/types/responseValidation.ts +0 -29
  192. package/src/types/xmldom.d.ts +0 -12
  193. package/src/utils/PerformanceMonitor.ts +0 -335
  194. package/src/utils/drawingUtils.ts +0 -73
  195. package/src/utils/responseUtils.ts +0 -51
  196. package/src/utils/touchDragHelper.ts +0 -1
  197. package/src/web-components/QTIInteractionBase.ts +0 -303
  198. package/src/web-components/QTIInteractionElement.ts +0 -125
  199. package/src/web-components/index.ts +0 -22
@@ -1,372 +0,0 @@
1
- import type { Player } from '../core/Player.js';
2
- import type { CatalogSupportPreference } from '../pnp/types.js';
3
-
4
- const PLATFORM_USAGES = [
5
- { usage: 'tts-pronunciation', flag: 'ttsPronunciation', label: 'Request pronunciation', text: 'P' },
6
- { usage: 'signing-definition', flag: 'signingDefinition', label: 'Request signing definition', text: 'S' },
7
- { usage: 'braille-text', flag: 'brailleText', label: 'Request braille text', text: 'B' },
8
- { usage: 'audio-description', flag: 'audioDescription', label: 'Request audio description', text: 'A' },
9
- { usage: 'extended-description', flag: 'extendedDescription', label: 'Request extended description', text: 'D' },
10
- ] as const;
11
- const ILLUSTRATED_GLOSSARY_USAGE = 'illustrated-glossary';
12
- const BUILT_IN_POPUP_USAGES = new Set<string>([
13
- 'glossary-on-screen',
14
- 'keyword-translation',
15
- ILLUSTRATED_GLOSSARY_USAGE,
16
- ]);
17
- const RESERVED_PLATFORM_FLAGS = new Set<string>(PLATFORM_USAGES.map((support) => support.flag));
18
- const RESERVED_PLATFORM_USAGES = new Set<string>(PLATFORM_USAGES.map((support) => support.usage));
19
- const SAFE_CATALOG_USAGE = /^[a-z][a-z0-9._-]*$/;
20
-
21
- interface HostCatalogSupport {
22
- usage: string;
23
- label: string;
24
- text: string;
25
- languageCode?: string;
26
- }
27
-
28
- /**
29
- * Inject glossary/keyword-translation trigger buttons into a rendered item container.
30
- *
31
- * When pnp.content.glossaryOnScreen is true, each [data-catalog-idref] element gets a
32
- * trigger button. Clicking the button looks up the catalog entry and shows a floating
33
- * popup rendered by the 'catalogPopup' component from the registry (defaults to
34
- * pie-qti-catalog-popup). Clicking again or pressing Escape closes it.
35
- *
36
- * For platform-level usages (tts-pronunciation, signing-definition, braille-text,
37
- * audio-description) the player fires a `qti-catalog-lookup` CustomEvent instead of
38
- * mounting a popup — the host application handles those usages.
39
- */
40
- export function applyGlossaryTriggers(container: HTMLElement, player: Player): () => void {
41
- cleanupGlossaryTriggers(container);
42
- const pnp = player.getPnp();
43
- if (!pnp) return () => cleanupGlossaryTriggers(container);
44
-
45
- const glossaryOn = pnp.content?.glossaryOnScreen === true;
46
- const kwTranslation = pnp.content?.keywordTranslation;
47
- const kwOn = kwTranslation?.active === true && !!kwTranslation.languageCode;
48
- const illustratedOn = pnp.content?.illustratedGlossary === true;
49
- const platformSupports = pnp.content?.catalogSupports ?? {};
50
- const hostSupports = getHostCatalogSupports(platformSupports);
51
-
52
- const anyPlatformOn = PLATFORM_USAGES.some((support) => isSupportActive(getPlatformSupportPreference(platformSupports, support)));
53
- if (!glossaryOn && !kwOn && !illustratedOn && !anyPlatformOn && hostSupports.length === 0) return () => cleanupGlossaryTriggers(container);
54
-
55
- const terms = container.querySelectorAll<HTMLElement>('[data-catalog-idref]');
56
- if (terms.length === 0) return () => cleanupGlossaryTriggers(container);
57
-
58
- // Track open popup so only one is open at a time
59
- let currentCleanup: (() => void) | null = null;
60
-
61
- for (const termEl of Array.from(terms)) {
62
- const idref = termEl.getAttribute('data-catalog-idref');
63
- if (!idref) continue;
64
- const stimulusIdentifier = getStimulusIdentifier(termEl);
65
-
66
- // Wrap term in a relative-positioned span so we can position popup against it
67
- const wrapper = document.createElement('span');
68
- wrapper.className = 'qti-catalog-term';
69
- wrapper.style.cssText = 'position:relative;display:inline';
70
-
71
- termEl.parentNode?.insertBefore(wrapper, termEl);
72
- wrapper.appendChild(termEl);
73
-
74
- if (glossaryOn) {
75
- const btn = createTriggerButton(termEl.textContent ?? idref, 'glossary-on-screen', 'Show definition');
76
- wrapper.appendChild(btn);
77
-
78
- btn.addEventListener('click', () => {
79
- if (currentCleanup) {
80
- currentCleanup();
81
- currentCleanup = null;
82
- return;
83
- }
84
- const html = player.getCatalogEntry(idref, 'glossary-on-screen', undefined, { stimulusIdentifier });
85
- if (html !== null) {
86
- currentCleanup = mountPopup(wrapper, termEl.textContent ?? idref, html, btn, player, () => {
87
- currentCleanup = null;
88
- });
89
- }
90
- });
91
- }
92
-
93
- if (kwOn) {
94
- const lang = kwTranslation!.languageCode;
95
- const btn = createTriggerButton(termEl.textContent ?? idref, 'keyword-translation', 'Show translation');
96
- wrapper.appendChild(btn);
97
-
98
- btn.addEventListener('click', () => {
99
- if (currentCleanup) {
100
- currentCleanup();
101
- currentCleanup = null;
102
- return;
103
- }
104
- const html = player.getCatalogEntry(idref, 'keyword-translation', lang, { stimulusIdentifier });
105
- if (html !== null) {
106
- currentCleanup = mountPopup(wrapper, termEl.textContent ?? idref, html, btn, player, () => {
107
- currentCleanup = null;
108
- });
109
- }
110
- });
111
- }
112
-
113
- if (illustratedOn) {
114
- const btn = createTriggerButton(termEl.textContent ?? idref, ILLUSTRATED_GLOSSARY_USAGE, 'Show illustrated glossary');
115
- wrapper.appendChild(btn);
116
-
117
- btn.addEventListener('click', () => {
118
- if (currentCleanup) {
119
- currentCleanup();
120
- currentCleanup = null;
121
- return;
122
- }
123
- const html = player.getCatalogEntry(idref, ILLUSTRATED_GLOSSARY_USAGE, undefined, { stimulusIdentifier });
124
- if (html !== null) {
125
- currentCleanup = mountPopup(wrapper, termEl.textContent ?? idref, html, btn, player, () => {
126
- currentCleanup = null;
127
- });
128
- }
129
- });
130
- }
131
-
132
- for (const support of PLATFORM_USAGES) {
133
- const preference = getPlatformSupportPreference(platformSupports, support);
134
- if (!isSupportActive(preference)) continue;
135
- addHostCatalogSupportButton(wrapper, termEl, idref, {
136
- ...support,
137
- languageCode: getSupportLanguageCode(preference),
138
- }, player, stimulusIdentifier);
139
- }
140
-
141
- for (const support of hostSupports) {
142
- addHostCatalogSupportButton(wrapper, termEl, idref, support, player, stimulusIdentifier);
143
- }
144
- }
145
-
146
- return () => {
147
- currentCleanup?.();
148
- cleanupGlossaryTriggers(container);
149
- };
150
- }
151
-
152
- export function cleanupGlossaryTriggers(container: HTMLElement): void {
153
- for (const wrapper of Array.from(container.querySelectorAll<HTMLElement>('.qti-catalog-term'))) {
154
- const term = Array.from(wrapper.children).find((child) =>
155
- (child as HTMLElement).getAttribute?.('data-catalog-idref') !== null
156
- ) as HTMLElement | undefined;
157
- if (term && wrapper.parentNode) {
158
- wrapper.parentNode.insertBefore(term, wrapper);
159
- }
160
- wrapper.remove();
161
- }
162
- }
163
-
164
- function createTriggerButton(termText: string, usage: string, labelPrefix: string, text?: string): HTMLButtonElement {
165
- const btn = document.createElement('button');
166
- btn.type = 'button';
167
- btn.className = 'qti-catalog-trigger';
168
- btn.style.cssText =
169
- 'display:inline-flex;align-items:center;justify-content:center;min-width:1.75rem;min-height:1.75rem;margin-inline:0.25rem;line-height:1;';
170
- btn.setAttribute('aria-label', `${labelPrefix}: ${termText}`);
171
- btn.setAttribute('data-catalog-usage', usage);
172
- btn.textContent = text ?? (usage === 'keyword-translation' ? 'T' : usage === ILLUSTRATED_GLOSSARY_USAGE ? 'I' : '?');
173
- return btn;
174
- }
175
-
176
- function addHostCatalogSupportButton(
177
- wrapper: HTMLElement,
178
- termEl: HTMLElement,
179
- idref: string,
180
- support: HostCatalogSupport,
181
- player: Player,
182
- stimulusIdentifier?: string
183
- ): void {
184
- const html = player.getCatalogEntry(idref, support.usage, support.languageCode, { stimulusIdentifier });
185
- if (html === null) return;
186
- const btn = createTriggerButton(termEl.textContent ?? idref, support.usage, support.label, support.text);
187
- wrapper.appendChild(btn);
188
- btn.addEventListener('click', () => {
189
- btn.dispatchEvent(
190
- new CustomEvent('qti-catalog-lookup', {
191
- bubbles: true,
192
- composed: true,
193
- detail: { idref, usage: support.usage, html, content: html, languageCode: support.languageCode },
194
- })
195
- );
196
- });
197
- }
198
-
199
- function getStimulusIdentifier(termEl: HTMLElement): string | undefined {
200
- const scopeEl = termEl.closest?.('[data-stimulus-idref]');
201
- const identifier = scopeEl?.getAttribute?.('data-stimulus-idref')?.trim();
202
- return identifier || undefined;
203
- }
204
-
205
- function getHostCatalogSupports(platformSupports: Record<string, CatalogSupportPreference | undefined>): HostCatalogSupport[] {
206
- return Object.entries(platformSupports)
207
- .map(([usage, preference]) => ({ usage: normalizeCatalogUsage(usage), preference }))
208
- .filter(({ usage, preference }) => usage !== null && isSupportActive(preference) && !isReservedCatalogUsage(usage))
209
- .map(({ usage, preference }) => ({
210
- usage: usage!,
211
- label: `Request ${usage!.replace(/[-_]+/g, ' ')}`,
212
- text: usage!.slice(0, 1).toUpperCase(),
213
- languageCode: getSupportLanguageCode(preference),
214
- }));
215
- }
216
-
217
- function getPlatformSupportPreference(
218
- platformSupports: Record<string, CatalogSupportPreference | undefined>,
219
- support: (typeof PLATFORM_USAGES)[number]
220
- ): CatalogSupportPreference | undefined {
221
- return platformSupports[support.flag] ?? platformSupports[support.usage];
222
- }
223
-
224
- function isSupportActive(preference: CatalogSupportPreference | undefined): boolean {
225
- if (preference === undefined) return false;
226
- if (typeof preference === 'boolean') return preference;
227
- return preference.active !== false;
228
- }
229
-
230
- function getSupportLanguageCode(preference: CatalogSupportPreference | undefined): string | undefined {
231
- if (!preference || typeof preference === 'boolean') return undefined;
232
- const languageCode = preference.languageCode?.trim();
233
- if (!languageCode || !/^[a-zA-Z]{2,8}(?:-[a-zA-Z0-9]{1,8})*$/.test(languageCode)) return undefined;
234
- return languageCode;
235
- }
236
-
237
- function normalizeCatalogUsage(value: string): string | null {
238
- const usage = value.trim().toLowerCase();
239
- if (!SAFE_CATALOG_USAGE.test(usage)) return null;
240
- return usage;
241
- }
242
-
243
- function isReservedCatalogUsage(usage: string): boolean {
244
- return RESERVED_PLATFORM_FLAGS.has(usage) || RESERVED_PLATFORM_USAGES.has(usage) || BUILT_IN_POPUP_USAGES.has(usage);
245
- }
246
-
247
- /**
248
- * Mount a catalog popup via the component registry. Falls back to a minimal
249
- * vanilla-JS popup if no 'catalogPopup' component is registered (e.g. in tests
250
- * that don't load default-components). Returns a cleanup function.
251
- */
252
- function mountPopup(
253
- anchor: HTMLElement,
254
- label: string,
255
- html: string,
256
- triggerBtn: HTMLButtonElement,
257
- player: Player,
258
- onClose: () => void
259
- ): () => void {
260
- const registry = player.getComponentRegistry();
261
-
262
- let popupEl: HTMLElement;
263
- let closed = false;
264
-
265
- function cleanup() {
266
- if (closed) return;
267
- closed = true;
268
- popupEl.remove();
269
- triggerBtn.focus();
270
- onClose();
271
- }
272
-
273
- const catalogPopupTag = registry.getTagNameForType('catalogPopup');
274
- if (catalogPopupTag !== null) {
275
- const el = document.createElement(catalogPopupTag) as HTMLElement & {
276
- content: string;
277
- label: string;
278
- onClose: () => void;
279
- };
280
- el.content = html;
281
- el.label = label;
282
- el.onClose = cleanup;
283
- anchor.appendChild(el);
284
- popupEl = el;
285
- } else {
286
- // Minimal fallback for environments without default-components
287
- popupEl = buildFallbackPopup(label, html, cleanup);
288
- anchor.appendChild(popupEl);
289
- }
290
-
291
- return cleanup;
292
- }
293
-
294
- /**
295
- * Minimal accessible fallback popup used when no catalogPopup component is registered.
296
- * Uses DaisyUI card classes where available; degrades gracefully without them.
297
- */
298
- function buildFallbackPopup(label: string, html: string, close: () => void): HTMLElement {
299
- const popup = document.createElement('div');
300
- popup.className = 'card card-bordered bg-base-100 shadow-lg qti-catalog-popup';
301
- popup.setAttribute('role', 'dialog');
302
- popup.setAttribute('aria-label', label);
303
- popup.setAttribute('aria-modal', 'true');
304
- popup.style.cssText = 'position:absolute;z-index:1000;min-width:16rem;max-width:24rem';
305
-
306
- const header = document.createElement('div');
307
- header.className = 'card-title flex justify-between items-center px-3 py-2 border-b border-base-300 bg-base-200';
308
-
309
- const title = document.createElement('span');
310
- title.className = 'text-sm font-semibold';
311
- title.textContent = label;
312
-
313
- const closeBtn = document.createElement('button');
314
- closeBtn.type = 'button';
315
- closeBtn.className = 'btn btn-ghost btn-xs';
316
- closeBtn.setAttribute('aria-label', 'Close');
317
- closeBtn.textContent = '✕';
318
-
319
- header.appendChild(title);
320
- header.appendChild(closeBtn);
321
-
322
- const body = document.createElement('div');
323
- body.className = 'card-body p-3';
324
-
325
- const isUrl = /^(https?:\/\/|\/)/i.test(html.trim());
326
- if (isUrl) {
327
- const img = document.createElement('img');
328
- img.src = html.trim();
329
- img.alt = label;
330
- img.className = 'max-w-full h-auto block mx-auto';
331
- body.appendChild(img);
332
- } else {
333
- body.innerHTML = html;
334
- }
335
-
336
- popup.appendChild(header);
337
- popup.appendChild(body);
338
-
339
- // Focus trap + Escape
340
- const focusableSelector = 'a[href], button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])';
341
-
342
- function handleKeyDown(e: KeyboardEvent) {
343
- if (e.key === 'Escape') {
344
- e.preventDefault();
345
- closeBtn.click();
346
- return;
347
- }
348
- if (e.key === 'Tab') {
349
- const focusable = Array.from(popup.querySelectorAll<HTMLElement>(focusableSelector)).filter(
350
- (el) => el.offsetParent !== null
351
- );
352
- if (focusable.length === 0) { e.preventDefault(); return; }
353
- const first = focusable[0];
354
- const last = focusable[focusable.length - 1];
355
- if (e.shiftKey && document.activeElement === first) {
356
- e.preventDefault();
357
- last.focus();
358
- } else if (!e.shiftKey && document.activeElement === last) {
359
- e.preventDefault();
360
- first.focus();
361
- }
362
- }
363
- }
364
-
365
- popup.addEventListener('keydown', handleKeyDown);
366
- closeBtn.addEventListener('click', () => close());
367
-
368
- // Move focus in on next tick
369
- setTimeout(() => closeBtn.focus(), 0);
370
-
371
- return popup;
372
- }
@@ -1,130 +0,0 @@
1
- import { parse as parseHtml } from 'node-html-parser';
2
- import type { HTMLElement as NhpElement } from 'node-html-parser';
3
- import type { CatalogCard, CatalogEntry, CatalogIndex } from './types.js';
4
-
5
- /**
6
- * Parse an XML string containing a QTI 3.0 <qti-catalog-info> element into a CatalogIndex.
7
- *
8
- * The mapper is not needed here because we parse the QTI 3.0 element names directly.
9
- * All element names use lower-case matching (node-html-parser normalises tags).
10
- */
11
- export function extractCatalog(xml: string): CatalogIndex {
12
- if (!xml.trim()) return new Map();
13
-
14
- const doc = parseHtml(`<root>${xml}</root>`, { lowerCaseTagName: true });
15
-
16
- // Find <qti-catalog-info> (QTI 3.0 kebab form)
17
- const infoEl = doc.querySelector('qti-catalog-info');
18
- if (!infoEl) return new Map();
19
-
20
- return buildIndex(infoEl);
21
- }
22
-
23
- /**
24
- * Parse a raw QTI item XML string and extract the embedded catalog.
25
- * Used by Player when integrating with the full item XML.
26
- */
27
- export function extractCatalogFromItemXml(itemXml: string): CatalogIndex {
28
- const doc = parseHtml(itemXml, { lowerCaseTagName: true });
29
- const infoEl = doc.querySelector('qti-catalog-info');
30
- if (!infoEl) return new Map();
31
- return buildIndex(infoEl);
32
- }
33
-
34
- /**
35
- * Merge two CatalogIndex maps. itemLevel entries win on collision (same identifier).
36
- */
37
- export function mergeCatalogs(base: CatalogIndex, override: CatalogIndex): CatalogIndex {
38
- const merged: CatalogIndex = new Map(base);
39
- for (const [id, card] of override) {
40
- merged.set(id, card);
41
- }
42
- return merged;
43
- }
44
-
45
- // ---------------------------------------------------------------------------
46
- // Internal
47
- // ---------------------------------------------------------------------------
48
-
49
- function buildIndex(infoEl: NhpElement): CatalogIndex {
50
- const index: CatalogIndex = new Map();
51
-
52
- for (const catalogEl of infoEl.querySelectorAll('qti-catalog')) {
53
- const identifier = catalogEl.getAttribute('id') ?? catalogEl.getAttribute('identifier');
54
- if (!identifier) continue;
55
-
56
- const entries: CatalogEntry[] = [];
57
- for (const cardEl of catalogEl.querySelectorAll('qti-card')) {
58
- const cardUsage = cardEl.getAttribute('support') ?? cardEl.getAttribute('usage');
59
- for (const entry of extractCardEntries(cardEl, cardUsage ?? undefined)) {
60
- entries.push(entry);
61
- }
62
- }
63
-
64
- if (entries.length > 0) {
65
- index.set(identifier, { entries });
66
- }
67
- }
68
-
69
- for (const cardEl of infoEl.querySelectorAll('qti-card')) {
70
- const identifier = cardEl.getAttribute('identifier');
71
- if (!identifier) continue;
72
-
73
- const entries = extractCardEntries(cardEl);
74
-
75
- if (entries.length > 0) {
76
- const existing = index.get(identifier);
77
- if (existing) {
78
- existing.entries.push(...entries);
79
- } else {
80
- index.set(identifier, { entries });
81
- }
82
- }
83
- }
84
-
85
- return index;
86
- }
87
-
88
- function extractCardEntries(cardEl: NhpElement, fallbackUsage?: string): CatalogEntry[] {
89
- const explicitEntries = cardEl.querySelectorAll('qti-card-entry');
90
- const entries: CatalogEntry[] = [];
91
-
92
- if (explicitEntries.length > 0) {
93
- for (const entryEl of explicitEntries) {
94
- const usage =
95
- entryEl.getAttribute('usage') ??
96
- entryEl.getAttribute('data-reading-type') ??
97
- fallbackUsage;
98
- if (!usage) continue;
99
- entries.push({
100
- usage,
101
- lang: getLang(entryEl),
102
- html: extractHtmlContent(entryEl),
103
- });
104
- }
105
- return entries;
106
- }
107
-
108
- if (fallbackUsage) {
109
- entries.push({
110
- usage: fallbackUsage,
111
- lang: getLang(cardEl),
112
- html: extractHtmlContent(cardEl),
113
- });
114
- }
115
-
116
- return entries;
117
- }
118
-
119
- function getLang(el: NhpElement): string | undefined {
120
- return el.getAttribute('xml:lang') ?? el.getAttribute('xmllang') ?? el.getAttribute('lang') ?? undefined;
121
- }
122
-
123
- function extractHtmlContent(el: NhpElement): string {
124
- const htmlContent = el.querySelector('qti-html-content');
125
- const fileHref = el.querySelector('qti-file-href');
126
-
127
- if (htmlContent) return htmlContent.innerHTML ?? htmlContent.text ?? '';
128
- if (fileHref) return fileHref.getAttribute('src') ?? fileHref.text?.trim() ?? '';
129
- return '';
130
- }
@@ -1,53 +0,0 @@
1
- import type { CatalogIndex } from './types.js';
2
-
3
- /**
4
- * Look up a catalog entry by card identifier, usage type, and optional language.
5
- *
6
- * Language fallback order (mirrors APIP 1.0 / HTTP Accept-Language conventions):
7
- * 1. Exact lang match (e.g. 'en-US')
8
- * 2. Language-prefix match (e.g. 'en' matches entry with lang 'en-US')
9
- * 3. Entry with no lang specified (universal fallback)
10
- * 4. null — no match found
11
- *
12
- * @param index CatalogIndex built by extractCatalog / extractCatalogFromItemXml
13
- * @param idref The data-catalog-idref value (maps to qti-card identifier)
14
- * @param usage Catalog usage type (e.g. 'glossary-on-screen')
15
- * @param lang Optional preferred language code (e.g. 'en-US', 'es')
16
- * @returns The matched entry's html string, or null if nothing found
17
- */
18
- export function getCatalogEntry(
19
- index: CatalogIndex,
20
- idref: string,
21
- usage: string,
22
- lang?: string
23
- ): string | null {
24
- const card = index.get(idref);
25
- if (!card) return null;
26
-
27
- const candidates = card.entries.filter((e) => e.usage === usage);
28
- if (candidates.length === 0) return null;
29
-
30
- // 1. Exact language match
31
- if (lang) {
32
- const exact = candidates.find((e) => e.lang === lang);
33
- if (exact) return exact.html;
34
-
35
- // 2. Language-prefix match: requested lang prefix matches entry lang
36
- // e.g. lang='en' matches entry.lang='en-US'
37
- // e.g. lang='en-US' matches entry.lang='en' (broader fallback)
38
- const prefix = lang.split('-')[0];
39
- const prefixMatch = candidates.find(
40
- (e) => e.lang && (e.lang === prefix || e.lang.startsWith(`${prefix}-`) || lang.startsWith(`${e.lang}-`))
41
- );
42
- if (prefixMatch) return prefixMatch.html;
43
- }
44
-
45
- // 3. No-lang fallback (entry without a lang attribute)
46
- const noLang = candidates.find((e) => !e.lang);
47
- if (noLang) return noLang.html;
48
-
49
- // 4. If we get here and there's only one candidate, return it regardless of lang
50
- if (candidates.length === 1) return candidates[0].html;
51
-
52
- return null;
53
- }
@@ -1,38 +0,0 @@
1
- import type { Player } from '../core/Player.js';
2
- import { applyGlossaryTriggers } from './applyGlossaryTriggers.js';
3
-
4
- export interface GlossaryActionParams {
5
- player: Player;
6
- }
7
-
8
- /**
9
- * Svelte action that reactively applies glossary trigger buttons to
10
- * [data-catalog-idref] elements in the rendered item body.
11
- *
12
- * Applied to the .qti-item-body container. When the player changes (new item
13
- * loaded), Svelte calls update() after {@html} has replaced the DOM content,
14
- * so the walk always runs on fresh markup.
15
- */
16
- export function glossaryAction(node: HTMLElement, params: GlossaryActionParams) {
17
- let cleanup = applyGlossaryTriggers(node, params.player);
18
- let unsubscribePnp = params.player.onPnpChange?.(() => {
19
- cleanup();
20
- cleanup = applyGlossaryTriggers(node, params.player);
21
- }) ?? (() => {});
22
-
23
- return {
24
- update(newParams: GlossaryActionParams) {
25
- cleanup();
26
- unsubscribePnp();
27
- cleanup = applyGlossaryTriggers(node, newParams.player);
28
- unsubscribePnp = newParams.player.onPnpChange?.(() => {
29
- cleanup();
30
- cleanup = applyGlossaryTriggers(node, newParams.player);
31
- }) ?? (() => {});
32
- },
33
- destroy() {
34
- unsubscribePnp();
35
- cleanup();
36
- },
37
- };
38
- }
@@ -1,37 +0,0 @@
1
- /**
2
- * QTI 3.0 §6.3 Catalog types.
3
- * A CatalogIndex maps card identifiers to their CatalogCard.
4
- * Cards hold multiple CatalogEntry records, one per usage/language combination.
5
- */
6
-
7
- export type CatalogIndex = Map<string, CatalogCard>;
8
-
9
- export interface CatalogCard {
10
- entries: CatalogEntry[];
11
- }
12
-
13
- export interface CatalogEntry {
14
- /**
15
- * QTI 3.0 catalog usage type.
16
- * The 8 standard types are listed below; additional string values are accepted
17
- * for forward-compatibility and host-defined extensions.
18
- */
19
- usage:
20
- | 'glossary-on-screen'
21
- | 'keyword-translation'
22
- | 'illustrated-glossary'
23
- | 'tts-pronunciation'
24
- | 'signing-definition'
25
- | 'braille-text'
26
- | 'audio-description'
27
- | 'extended-description'
28
- | string;
29
- /** xml:lang value (e.g. 'en-US', 'es'). Undefined when no language is specified. */
30
- lang?: string;
31
- /**
32
- * Inner HTML from <qti-html-content>, or the src URL from <qti-file-href>.
33
- * For illustrated-glossary entries the player checks whether this looks like a URL
34
- * and renders an <img> instead of innerHTML.
35
- */
36
- html: string;
37
- }