@longsightgroup/qti3-player 0.2.1 → 0.4.0
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/README.md +7 -0
- package/dist/icons.d.ts +8 -0
- package/dist/icons.d.ts.map +1 -0
- package/dist/icons.js +45 -0
- package/dist/icons.js.map +1 -0
- package/dist/index.d.ts +3 -134
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -4712
- package/dist/index.js.map +1 -1
- package/dist/interaction-support.d.ts +34 -0
- package/dist/interaction-support.d.ts.map +1 -0
- package/dist/interaction-support.js +189 -0
- package/dist/interaction-support.js.map +1 -0
- package/dist/movement.d.ts +3 -0
- package/dist/movement.d.ts.map +1 -0
- package/dist/movement.js +21 -0
- package/dist/movement.js.map +1 -0
- package/dist/player-element.d.ts +60 -0
- package/dist/player-element.d.ts.map +1 -0
- package/dist/player-element.js +367 -0
- package/dist/player-element.js.map +1 -0
- package/dist/player-locale.d.ts +6 -0
- package/dist/player-locale.d.ts.map +1 -0
- package/dist/player-locale.js +205 -0
- package/dist/player-locale.js.map +1 -0
- package/dist/player-messages.d.ts +40 -0
- package/dist/player-messages.d.ts.map +1 -0
- package/dist/player-messages.js +2 -0
- package/dist/player-messages.js.map +1 -0
- package/dist/player-styles.d.ts +3 -0
- package/dist/player-styles.d.ts.map +1 -0
- package/dist/player-styles.js +24 -0
- package/dist/player-styles.js.map +1 -0
- package/dist/player-types.d.ts +71 -0
- package/dist/player-types.d.ts.map +1 -0
- package/dist/player-types.js +2 -0
- package/dist/player-types.js.map +1 -0
- package/dist/player-validation-dom.d.ts +3 -0
- package/dist/player-validation-dom.d.ts.map +1 -0
- package/dist/player-validation-dom.js +28 -0
- package/dist/player-validation-dom.js.map +1 -0
- package/dist/player-validation.d.ts +13 -0
- package/dist/player-validation.d.ts.map +1 -0
- package/dist/player-validation.js +123 -0
- package/dist/player-validation.js.map +1 -0
- package/dist/portable-custom-support.d.ts +11 -0
- package/dist/portable-custom-support.d.ts.map +1 -0
- package/dist/portable-custom-support.js +70 -0
- package/dist/portable-custom-support.js.map +1 -0
- package/dist/response-limits.d.ts +9 -0
- package/dist/response-limits.d.ts.map +1 -0
- package/dist/response-limits.js +44 -0
- package/dist/response-limits.js.map +1 -0
- package/package.json +4 -4
- package/src/content/content-dom.ts +274 -0
- package/src/content/content-renderer.ts +114 -0
- package/src/controls/remove-button.ts +13 -0
- package/src/icons.ts +47 -0
- package/src/index.ts +26 -5307
- package/src/interaction-support.ts +263 -0
- package/src/interactions/choice-interaction.ts +92 -0
- package/src/interactions/drawing-interaction.ts +447 -0
- package/src/interactions/end-attempt-interaction.ts +19 -0
- package/src/interactions/gap-match-interaction.ts +337 -0
- package/src/interactions/graphic-associate-interaction.ts +324 -0
- package/src/interactions/graphic-context.ts +33 -0
- package/src/interactions/hotspot-interaction.ts +87 -0
- package/src/interactions/hottext-interaction.ts +81 -0
- package/src/interactions/inline-choice-interaction.ts +45 -0
- package/src/interactions/inline-controls.ts +21 -0
- package/src/interactions/interaction-diagnostics.ts +159 -0
- package/src/interactions/interaction-dispatch.ts +9 -0
- package/src/interactions/interaction-label.ts +10 -0
- package/src/interactions/interaction-registry.ts +209 -0
- package/src/interactions/match-interaction.ts +199 -0
- package/src/interactions/object-asset.ts +212 -0
- package/src/interactions/pair-interaction.ts +147 -0
- package/src/interactions/point-value.ts +41 -0
- package/src/interactions/portable-custom-interaction.ts +139 -0
- package/src/interactions/position-object-interaction.ts +210 -0
- package/src/interactions/routing.ts +27 -0
- package/src/interactions/select-point-interaction.ts +185 -0
- package/src/interactions/shared.ts +56 -0
- package/src/interactions/text-interaction.ts +127 -0
- package/src/interactions/unsupported-interaction.ts +25 -0
- package/src/interactions/upload-interaction.ts +16 -0
- package/src/movement.ts +29 -0
- package/src/player/attempt-availability.ts +36 -0
- package/src/player/content-state.ts +74 -0
- package/src/player/dynamic-body.ts +40 -0
- package/src/player/feedback-panel.ts +23 -0
- package/src/player/fetch-xml.ts +8 -0
- package/src/player/interaction-render.ts +89 -0
- package/src/player/render-shell.ts +44 -0
- package/src/player/resolve-assets.ts +12 -0
- package/src/player/validation-messages.ts +42 -0
- package/src/player-element.ts +493 -0
- package/src/player-locale.ts +232 -0
- package/src/player-messages.ts +31 -0
- package/src/player-styles.ts +25 -0
- package/src/player-types.ts +99 -0
- package/src/player-validation-dom.ts +31 -0
- package/src/player-validation.ts +158 -0
- package/src/portable-custom-support.ts +74 -0
- package/src/reorder/a11y.ts +40 -0
- package/src/reorder/graphic-order-interaction.ts +260 -0
- package/src/reorder/list-controls.ts +114 -0
- package/src/reorder/order-interaction.ts +75 -0
- package/src/response-limits.ts +47 -0
- package/src/styles/base-styles.ts +117 -0
- package/src/styles/choice-hottext-styles.ts +75 -0
- package/src/styles/control-styles.ts +113 -0
- package/src/styles/drawing-styles.ts +29 -0
- package/src/styles/gap-match-styles.ts +32 -0
- package/src/styles/graphic-styles.ts +294 -0
- package/src/styles/match-pair-styles.ts +61 -0
- package/src/styles/text-slider-styles.ts +34 -0
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
import type { QtiInteraction, QtiObjectAsset, QtiValue } from "@longsightgroup/qti3-core";
|
|
2
|
+
import { applyResponsiveGraphicSize, objectIsImage, readableType } from "../interaction-support.js";
|
|
3
|
+
import type { QtiPlayerMessages } from "../player-messages.js";
|
|
4
|
+
|
|
5
|
+
export const DRAWING_STROKE_COLOR = "#000";
|
|
6
|
+
export const DRAWING_STROKE_WIDTH = 3;
|
|
7
|
+
|
|
8
|
+
export function renderDrawingResponse(
|
|
9
|
+
interaction: QtiInteraction,
|
|
10
|
+
update: (value: QtiValue) => void,
|
|
11
|
+
currentValue: QtiValue,
|
|
12
|
+
messages: QtiPlayerMessages,
|
|
13
|
+
): HTMLElement {
|
|
14
|
+
const group = document.createElement("div");
|
|
15
|
+
group.role = "group";
|
|
16
|
+
group.setAttribute("aria-label", `${readableType(interaction.type)} response`);
|
|
17
|
+
|
|
18
|
+
const surface = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
19
|
+
surface.classList.add("qti3-drawing-surface");
|
|
20
|
+
surface.setAttribute("role", "img");
|
|
21
|
+
surface.setAttribute("aria-label", "Drawing response surface");
|
|
22
|
+
surface.setAttribute("tabindex", "0");
|
|
23
|
+
const width = drawingWidth(interaction);
|
|
24
|
+
const height = drawingHeight(interaction);
|
|
25
|
+
surface.setAttribute("viewBox", `0 0 ${width} ${height}`);
|
|
26
|
+
applyResponsiveGraphicSize(surface, width, height);
|
|
27
|
+
surface.style.touchAction = "none";
|
|
28
|
+
const restoredStrokes = parseDrawingValue(currentValue);
|
|
29
|
+
const authoredBackgroundHref = drawingBackgroundHref(interaction);
|
|
30
|
+
let resolvedAuthoredBackgroundHref = authoredBackgroundHref;
|
|
31
|
+
let activeBackgroundIsAuthored =
|
|
32
|
+
restoredStrokes.length > 0 || !drawingResponseImage(currentValue);
|
|
33
|
+
let activeBackgroundHref =
|
|
34
|
+
restoredStrokes.length === 0
|
|
35
|
+
? (drawingResponseImage(currentValue) ?? authoredBackgroundHref)
|
|
36
|
+
: authoredBackgroundHref;
|
|
37
|
+
const resetSurface = () => {
|
|
38
|
+
const background = activeBackgroundHref
|
|
39
|
+
? drawingImageElement(activeBackgroundHref, width, height)
|
|
40
|
+
: undefined;
|
|
41
|
+
surface.replaceChildren(...(background ? [background] : []));
|
|
42
|
+
};
|
|
43
|
+
resetSurface();
|
|
44
|
+
|
|
45
|
+
const summary = document.createElement("output");
|
|
46
|
+
summary.className = "qti3-coordinate-output qti-visually-hidden";
|
|
47
|
+
summary.setAttribute("aria-live", "polite");
|
|
48
|
+
const strokes: DrawingStroke[] = [];
|
|
49
|
+
let activeStroke: DrawingStroke | undefined;
|
|
50
|
+
let commitVersion = 0;
|
|
51
|
+
const commit = (emitResponse = true) => {
|
|
52
|
+
const version = ++commitVersion;
|
|
53
|
+
if (emitResponse) {
|
|
54
|
+
if (strokes.length === 0) {
|
|
55
|
+
update(null);
|
|
56
|
+
} else {
|
|
57
|
+
void exportDrawingResponse(interaction, width, height, strokes, () => {
|
|
58
|
+
const currentHref = currentDrawingBackgroundHref(surface);
|
|
59
|
+
if (activeBackgroundIsAuthored && currentHref) {
|
|
60
|
+
resolvedAuthoredBackgroundHref = currentHref;
|
|
61
|
+
}
|
|
62
|
+
return currentHref ?? activeBackgroundHref;
|
|
63
|
+
}).then((value) => {
|
|
64
|
+
if (version === commitVersion) update(value);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const count = strokes.length;
|
|
69
|
+
summary.value = serializeDrawingStrokes(strokes);
|
|
70
|
+
summary.textContent =
|
|
71
|
+
count === 0 ? "No drawing strokes." : `${count} drawing stroke${count === 1 ? "" : "s"}.`;
|
|
72
|
+
surface.setAttribute(
|
|
73
|
+
"aria-label",
|
|
74
|
+
count === 0
|
|
75
|
+
? "Drawing response surface, no strokes"
|
|
76
|
+
: `Drawing response surface, ${count} stroke${count === 1 ? "" : "s"}`,
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
for (const points of restoredStrokes) {
|
|
80
|
+
const element = polylineElement(points);
|
|
81
|
+
strokes.push({ points, element });
|
|
82
|
+
surface.append(element);
|
|
83
|
+
}
|
|
84
|
+
const addPoint = (event: PointerEvent) => {
|
|
85
|
+
if (!activeStroke) return;
|
|
86
|
+
const point = svgPoint(surface, event);
|
|
87
|
+
const previous = activeStroke.points.at(-1);
|
|
88
|
+
if (previous && previous.x === point.x && previous.y === point.y) return;
|
|
89
|
+
activeStroke.points.push(point);
|
|
90
|
+
activeStroke.element.setAttribute("points", serializeSvgPoints(activeStroke.points));
|
|
91
|
+
};
|
|
92
|
+
const finishStroke = (event: PointerEvent) => {
|
|
93
|
+
if (!activeStroke) return;
|
|
94
|
+
addPoint(event);
|
|
95
|
+
const firstPoint = activeStroke.points[0];
|
|
96
|
+
if (activeStroke.points.length === 1 && firstPoint) activeStroke.points.push(firstPoint);
|
|
97
|
+
activeStroke.element.setAttribute("points", serializeSvgPoints(activeStroke.points));
|
|
98
|
+
activeStroke = undefined;
|
|
99
|
+
commit();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
surface.addEventListener("pointerdown", (event) => {
|
|
103
|
+
const point = svgPoint(surface, event);
|
|
104
|
+
const element = polylineElement([point]);
|
|
105
|
+
activeStroke = { points: [point], element };
|
|
106
|
+
strokes.push(activeStroke);
|
|
107
|
+
surface.append(element);
|
|
108
|
+
surface.setPointerCapture(event.pointerId);
|
|
109
|
+
});
|
|
110
|
+
surface.addEventListener("pointermove", addPoint);
|
|
111
|
+
surface.addEventListener("pointerup", finishStroke);
|
|
112
|
+
surface.addEventListener("pointercancel", () => {
|
|
113
|
+
activeStroke = undefined;
|
|
114
|
+
});
|
|
115
|
+
surface.addEventListener("keydown", (event) => {
|
|
116
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
const points = [
|
|
119
|
+
{ x: 10, y: 10 },
|
|
120
|
+
{ x: 90, y: 90 },
|
|
121
|
+
];
|
|
122
|
+
const element = polylineElement(points);
|
|
123
|
+
strokes.push({ points, element });
|
|
124
|
+
surface.append(element);
|
|
125
|
+
commit();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const clear = document.createElement("button");
|
|
129
|
+
clear.type = "button";
|
|
130
|
+
clear.textContent = messages.clearDrawing();
|
|
131
|
+
clear.addEventListener("click", () => {
|
|
132
|
+
strokes.splice(0, strokes.length);
|
|
133
|
+
activeStroke = undefined;
|
|
134
|
+
if (activeBackgroundIsAuthored) {
|
|
135
|
+
resolvedAuthoredBackgroundHref =
|
|
136
|
+
currentDrawingBackgroundHref(surface) ?? resolvedAuthoredBackgroundHref;
|
|
137
|
+
}
|
|
138
|
+
activeBackgroundHref = resolvedAuthoredBackgroundHref;
|
|
139
|
+
activeBackgroundIsAuthored = true;
|
|
140
|
+
resetSurface();
|
|
141
|
+
commit();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const tools = document.createElement("div");
|
|
145
|
+
tools.className = "qti3-drawing-tools";
|
|
146
|
+
tools.append(clear);
|
|
147
|
+
commit(false);
|
|
148
|
+
group.append(surface, summary, tools);
|
|
149
|
+
return group;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
type DrawingPoint = { x: number; y: number };
|
|
153
|
+
type DrawingStroke = { points: DrawingPoint[]; element: SVGPolylineElement };
|
|
154
|
+
|
|
155
|
+
function dimension(value: string | undefined, fallback: number): number {
|
|
156
|
+
const parsed = Number(value);
|
|
157
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function scalarString(value: QtiValue): string {
|
|
161
|
+
if (value === null || Array.isArray(value) || typeof value === "object") return "";
|
|
162
|
+
return String(value);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function parseDrawingValue(value: QtiValue): DrawingPoint[][] {
|
|
166
|
+
const raw = scalarString(value);
|
|
167
|
+
if (!raw) return [];
|
|
168
|
+
|
|
169
|
+
const metadata = drawingMetadataFromSvgDataUrl(raw);
|
|
170
|
+
if (metadata) return parseDrawingStrokePayload(metadata);
|
|
171
|
+
|
|
172
|
+
return parseDrawingStrokePayload(raw);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function drawingMetadataFromSvgDataUrl(raw: string): string | undefined {
|
|
176
|
+
if (!raw.startsWith("data:image/svg+xml")) return undefined;
|
|
177
|
+
const commaIndex = raw.indexOf(",");
|
|
178
|
+
if (commaIndex === -1) return undefined;
|
|
179
|
+
const encoded = raw.slice(commaIndex + 1);
|
|
180
|
+
let svg = "";
|
|
181
|
+
try {
|
|
182
|
+
svg = raw.slice(0, commaIndex).includes(";base64")
|
|
183
|
+
? atob(encoded)
|
|
184
|
+
: decodeURIComponent(encoded);
|
|
185
|
+
} catch {
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
const match = svg.match(/\sdata-qti3-strokes="([^"]*)"/);
|
|
189
|
+
if (!match?.[1]) return undefined;
|
|
190
|
+
try {
|
|
191
|
+
return decodeURIComponent(match[1]);
|
|
192
|
+
} catch {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function drawingResponseImage(value: QtiValue): string | undefined {
|
|
198
|
+
const raw = scalarString(value);
|
|
199
|
+
return raw?.startsWith("data:image/") ? raw : undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function parseDrawingStrokePayload(raw: string): DrawingPoint[][] {
|
|
203
|
+
return raw
|
|
204
|
+
.split("|")
|
|
205
|
+
.map((stroke) => {
|
|
206
|
+
const numbers = stroke
|
|
207
|
+
.trim()
|
|
208
|
+
.split(/\s+/)
|
|
209
|
+
.map(Number)
|
|
210
|
+
.filter((item) => Number.isFinite(item));
|
|
211
|
+
const points: DrawingPoint[] = [];
|
|
212
|
+
for (let index = 0; index + 1 < numbers.length; index += 2) {
|
|
213
|
+
points.push({ x: numbers[index]!, y: numbers[index + 1]! });
|
|
214
|
+
}
|
|
215
|
+
return points;
|
|
216
|
+
})
|
|
217
|
+
.filter((points) => points.length > 0);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function drawingWidth(interaction: QtiInteraction): number {
|
|
221
|
+
return dimension(interaction.object?.width, 640);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function drawingHeight(interaction: QtiInteraction): number {
|
|
225
|
+
return dimension(interaction.object?.height, 360);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function svgPoint(surface: SVGSVGElement, event: PointerEvent): { x: number; y: number } {
|
|
229
|
+
const rect = surface.getBoundingClientRect();
|
|
230
|
+
const viewBox = surface.viewBox.baseVal;
|
|
231
|
+
const width = viewBox.width || 160;
|
|
232
|
+
const height = viewBox.height || 120;
|
|
233
|
+
const x = Math.round(((event.clientX - rect.left) / rect.width) * width);
|
|
234
|
+
const y = Math.round(((event.clientY - rect.top) / rect.height) * height);
|
|
235
|
+
return {
|
|
236
|
+
x: Math.max(0, Math.min(width, x)),
|
|
237
|
+
y: Math.max(0, Math.min(height, y)),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function exportDrawingResponse(
|
|
242
|
+
interaction: QtiInteraction,
|
|
243
|
+
width: number,
|
|
244
|
+
height: number,
|
|
245
|
+
strokes: DrawingStroke[],
|
|
246
|
+
backgroundHref: () => string | undefined,
|
|
247
|
+
): Promise<string> {
|
|
248
|
+
const href = backgroundHref();
|
|
249
|
+
const mime = drawingResponseMime(interaction.object);
|
|
250
|
+
if (mime === "image/svg+xml") {
|
|
251
|
+
return svgDrawingDataUrl(interaction, width, height, strokes, await portableImageHref(href));
|
|
252
|
+
}
|
|
253
|
+
return rasterDrawingDataUrl(interaction, width, height, strokes, href, mime);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function drawingResponseMime(
|
|
257
|
+
object: QtiObjectAsset | undefined,
|
|
258
|
+
): "image/svg+xml" | "image/png" | "image/jpeg" | "image/webp" {
|
|
259
|
+
const candidates = [
|
|
260
|
+
object?.type,
|
|
261
|
+
object?.data,
|
|
262
|
+
...(object?.sources.map((source) => source.type ?? source.src) ?? []),
|
|
263
|
+
];
|
|
264
|
+
for (const candidate of candidates) {
|
|
265
|
+
const mime = imageMime(candidate);
|
|
266
|
+
if (mime) return mime;
|
|
267
|
+
}
|
|
268
|
+
return "image/svg+xml";
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function imageMime(
|
|
272
|
+
value: string | undefined,
|
|
273
|
+
): "image/svg+xml" | "image/png" | "image/jpeg" | "image/webp" | undefined {
|
|
274
|
+
if (!value) return undefined;
|
|
275
|
+
const normalized = value.toLowerCase().split(";")[0] ?? "";
|
|
276
|
+
if (normalized === "image/svg+xml") return "image/svg+xml";
|
|
277
|
+
if (normalized === "image/png") return "image/png";
|
|
278
|
+
if (normalized === "image/jpeg" || normalized === "image/jpg") return "image/jpeg";
|
|
279
|
+
if (normalized === "image/webp") return "image/webp";
|
|
280
|
+
const dataMime = value.match(/^data:([^;,]+)/i)?.[1]?.toLowerCase();
|
|
281
|
+
if (dataMime) return imageMime(dataMime);
|
|
282
|
+
if (/\.svg(?:[?#].*)?$/i.test(value)) return "image/svg+xml";
|
|
283
|
+
if (/\.png(?:[?#].*)?$/i.test(value)) return "image/png";
|
|
284
|
+
if (/\.jpe?g(?:[?#].*)?$/i.test(value)) return "image/jpeg";
|
|
285
|
+
if (/\.webp(?:[?#].*)?$/i.test(value)) return "image/webp";
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function svgDrawingDataUrl(
|
|
290
|
+
interaction: QtiInteraction,
|
|
291
|
+
width: number,
|
|
292
|
+
height: number,
|
|
293
|
+
strokes: DrawingStroke[],
|
|
294
|
+
backgroundHref: string | undefined,
|
|
295
|
+
): string {
|
|
296
|
+
const markup = svgDrawingMarkup(interaction, width, height, strokes, backgroundHref);
|
|
297
|
+
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(markup)}`;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function svgDrawingMarkup(
|
|
301
|
+
interaction: QtiInteraction,
|
|
302
|
+
width: number,
|
|
303
|
+
height: number,
|
|
304
|
+
strokes: DrawingStroke[],
|
|
305
|
+
backgroundHref: string | undefined,
|
|
306
|
+
): string {
|
|
307
|
+
const strokePayload = serializeDrawingStrokes(strokes);
|
|
308
|
+
const background =
|
|
309
|
+
backgroundHref && interaction.object && objectIsImage(interaction.object)
|
|
310
|
+
? `<image href="${xmlAttribute(backgroundHref)}" width="${width}" height="${height}" preserveAspectRatio="xMidYMid meet"/>`
|
|
311
|
+
: "";
|
|
312
|
+
const lines = strokes
|
|
313
|
+
.map((stroke) => {
|
|
314
|
+
return `<polyline points="${xmlAttribute(serializeSvgPoints(stroke.points))}" fill="none" stroke="${DRAWING_STROKE_COLOR}" stroke-width="${DRAWING_STROKE_WIDTH}" stroke-linecap="round" stroke-linejoin="round"/>`;
|
|
315
|
+
})
|
|
316
|
+
.join("");
|
|
317
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}"><metadata id="qti3-drawing-response" data-qti3-strokes="${xmlAttribute(encodeURIComponent(strokePayload))}"></metadata>${background}${lines}</svg>`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async function rasterDrawingDataUrl(
|
|
321
|
+
interaction: QtiInteraction,
|
|
322
|
+
width: number,
|
|
323
|
+
height: number,
|
|
324
|
+
strokes: DrawingStroke[],
|
|
325
|
+
backgroundHref: string | undefined,
|
|
326
|
+
mime: "image/png" | "image/jpeg" | "image/webp",
|
|
327
|
+
): Promise<string> {
|
|
328
|
+
const canvas = document.createElement("canvas");
|
|
329
|
+
canvas.width = width;
|
|
330
|
+
canvas.height = height;
|
|
331
|
+
const context = canvas.getContext("2d");
|
|
332
|
+
if (!context) return svgDrawingDataUrl(interaction, width, height, strokes, backgroundHref);
|
|
333
|
+
|
|
334
|
+
if (mime === "image/jpeg") {
|
|
335
|
+
context.fillStyle = "#fff";
|
|
336
|
+
context.fillRect(0, 0, width, height);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (backgroundHref && interaction.object && objectIsImage(interaction.object)) {
|
|
340
|
+
try {
|
|
341
|
+
const image = await loadCanvasImage(backgroundHref);
|
|
342
|
+
context.drawImage(image, 0, 0, width, height);
|
|
343
|
+
} catch {
|
|
344
|
+
// Export the candidate marks even when the authored background cannot be rasterized.
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
context.strokeStyle = DRAWING_STROKE_COLOR;
|
|
349
|
+
context.lineWidth = DRAWING_STROKE_WIDTH;
|
|
350
|
+
context.lineCap = "round";
|
|
351
|
+
context.lineJoin = "round";
|
|
352
|
+
for (const stroke of strokes) {
|
|
353
|
+
const [first, ...rest] = stroke.points;
|
|
354
|
+
if (!first) continue;
|
|
355
|
+
context.beginPath();
|
|
356
|
+
context.moveTo(first.x, first.y);
|
|
357
|
+
for (const point of rest) context.lineTo(point.x, point.y);
|
|
358
|
+
context.stroke();
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
try {
|
|
362
|
+
return canvas.toDataURL(mime);
|
|
363
|
+
} catch {
|
|
364
|
+
return svgDrawingDataUrl(interaction, width, height, strokes, backgroundHref);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function loadCanvasImage(src: string): Promise<HTMLImageElement> {
|
|
369
|
+
return new Promise((resolve, reject) => {
|
|
370
|
+
const image = new Image();
|
|
371
|
+
image.addEventListener("load", () => resolve(image), { once: true });
|
|
372
|
+
image.addEventListener("error", () => reject(new Error(`Unable to load ${src}`)), {
|
|
373
|
+
once: true,
|
|
374
|
+
});
|
|
375
|
+
image.src = src;
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async function portableImageHref(href: string | undefined): Promise<string | undefined> {
|
|
380
|
+
if (!href || href.startsWith("data:")) return href;
|
|
381
|
+
try {
|
|
382
|
+
const response = await fetch(href);
|
|
383
|
+
if (!response.ok) return href;
|
|
384
|
+
return await blobToDataUrl(await response.blob());
|
|
385
|
+
} catch {
|
|
386
|
+
return href;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function blobToDataUrl(blob: Blob): Promise<string> {
|
|
391
|
+
return new Promise((resolve, reject) => {
|
|
392
|
+
const reader = new FileReader();
|
|
393
|
+
reader.addEventListener("load", () => {
|
|
394
|
+
resolve(typeof reader.result === "string" ? reader.result : "");
|
|
395
|
+
});
|
|
396
|
+
reader.addEventListener("error", () => {
|
|
397
|
+
reject(reader.error ?? new Error("Unable to read drawing background."));
|
|
398
|
+
});
|
|
399
|
+
reader.readAsDataURL(blob);
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function drawingBackgroundHref(interaction: QtiInteraction): string | undefined {
|
|
404
|
+
if (!interaction.object?.data || !objectIsImage(interaction.object)) return undefined;
|
|
405
|
+
return interaction.object.data;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function drawingImageElement(href: string, width: number, height: number): SVGImageElement {
|
|
409
|
+
const image = document.createElementNS("http://www.w3.org/2000/svg", "image");
|
|
410
|
+
image.setAttribute("href", href);
|
|
411
|
+
image.setAttribute("width", String(width));
|
|
412
|
+
image.setAttribute("height", String(height));
|
|
413
|
+
image.setAttribute("preserveAspectRatio", "xMidYMid meet");
|
|
414
|
+
image.setAttribute("aria-hidden", "true");
|
|
415
|
+
return image;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function currentDrawingBackgroundHref(surface: SVGSVGElement): string | undefined {
|
|
419
|
+
return surface.querySelector("image")?.getAttribute("href") ?? undefined;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function serializeSvgPoints(points: Array<{ x: number; y: number }>): string {
|
|
423
|
+
return points.map((point) => `${point.x},${point.y}`).join(" ");
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function serializeDrawingStrokes(strokes: Pick<DrawingStroke, "points">[]): string {
|
|
427
|
+
return strokes.map((stroke) => serializeDrawingStroke(stroke.points)).join(" | ");
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function serializeDrawingStroke(points: DrawingPoint[]): string {
|
|
431
|
+
return points.map((point) => `${point.x} ${point.y}`).join(" ");
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function xmlAttribute(value: string): string {
|
|
435
|
+
return value
|
|
436
|
+
.replaceAll("&", "&")
|
|
437
|
+
.replaceAll('"', """)
|
|
438
|
+
.replaceAll("<", "<")
|
|
439
|
+
.replaceAll(">", ">");
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function polylineElement(points: DrawingPoint[]): SVGPolylineElement {
|
|
443
|
+
const line = document.createElementNS("http://www.w3.org/2000/svg", "polyline");
|
|
444
|
+
line.classList.add("qti3-drawing-stroke");
|
|
445
|
+
line.setAttribute("points", serializeSvgPoints(points));
|
|
446
|
+
return line;
|
|
447
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { QtiInteraction, QtiValue } from "@longsightgroup/qti3-core";
|
|
2
|
+
import type { QtiPlayerMessages } from "../player-messages.js";
|
|
3
|
+
|
|
4
|
+
export function renderEndAttemptResponse(
|
|
5
|
+
interaction: QtiInteraction,
|
|
6
|
+
update: (value: QtiValue) => void,
|
|
7
|
+
endAttempt: () => void,
|
|
8
|
+
messages: QtiPlayerMessages,
|
|
9
|
+
): HTMLElement {
|
|
10
|
+
const button = document.createElement("button");
|
|
11
|
+
button.type = "button";
|
|
12
|
+
button.className = "qti3-end-attempt-button";
|
|
13
|
+
button.textContent = interaction.attributes.title ?? messages.endAttempt();
|
|
14
|
+
button.addEventListener("click", () => {
|
|
15
|
+
if (interaction.responseIdentifier) update(true);
|
|
16
|
+
endAttempt();
|
|
17
|
+
});
|
|
18
|
+
return button;
|
|
19
|
+
}
|