@interlacejs/quiz 0.1.2 → 0.2.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/dist/index.cjs +577 -397
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +543 -355
- package/dist/index.js.map +1 -1
- package/package.json +8 -4
package/dist/index.js
CHANGED
|
@@ -1,52 +1,17 @@
|
|
|
1
|
+
// src/quiz-editor.ts
|
|
2
|
+
import { ensureInterlaceStyles } from "@interlacejs/core";
|
|
3
|
+
|
|
1
4
|
// src/tokens.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
surfaceRaised: "#f8fafc",
|
|
13
|
-
ground: "#f6f7f9",
|
|
14
|
-
errorBg: "#fef2f2",
|
|
15
|
-
errorText: "#b91c1c",
|
|
16
|
-
errorBorder: "#fecaca",
|
|
17
|
-
white: "#ffffff",
|
|
18
|
-
black: "#000000",
|
|
19
|
-
// Reveal-state aliases for quiz option feedback.
|
|
20
|
-
correctBg: "#0066cc",
|
|
21
|
-
correctText: "#ffffff",
|
|
22
|
-
incorrectBg: "#fef2f2",
|
|
23
|
-
incorrectText: "#b91c1c",
|
|
24
|
-
incorrectBorder: "#fecaca"
|
|
25
|
-
};
|
|
26
|
-
var FONTS = {
|
|
27
|
-
display: 'Georgia, "Times New Roman", Times, serif',
|
|
28
|
-
body: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif'
|
|
29
|
-
};
|
|
30
|
-
var SPACE = {
|
|
31
|
-
1: 4,
|
|
32
|
-
2: 8,
|
|
33
|
-
3: 12,
|
|
34
|
-
4: 16,
|
|
35
|
-
5: 24
|
|
36
|
-
};
|
|
37
|
-
var RADIUS = {
|
|
38
|
-
sm: 4,
|
|
39
|
-
md: 8,
|
|
40
|
-
lg: 12
|
|
41
|
-
};
|
|
42
|
-
var TYPE = {
|
|
43
|
-
xs: 12,
|
|
44
|
-
sm: 13,
|
|
45
|
-
base: 15,
|
|
46
|
-
md: 16,
|
|
47
|
-
lg: 20,
|
|
48
|
-
xl: 24
|
|
49
|
-
};
|
|
5
|
+
import {
|
|
6
|
+
COLORS,
|
|
7
|
+
CONTROL_HEIGHT,
|
|
8
|
+
FOCUS_RING,
|
|
9
|
+
FONTS,
|
|
10
|
+
RADIUS,
|
|
11
|
+
SHADOWS,
|
|
12
|
+
SPACE,
|
|
13
|
+
TYPE
|
|
14
|
+
} from "@interlacejs/core";
|
|
50
15
|
|
|
51
16
|
// src/quiz-player.ts
|
|
52
17
|
var quizPlaybackSessions = /* @__PURE__ */ new WeakMap();
|
|
@@ -57,7 +22,7 @@ function createMediaImage(className, src, alt) {
|
|
|
57
22
|
img.src = src;
|
|
58
23
|
img.style.maxWidth = "100%";
|
|
59
24
|
img.style.height = "auto";
|
|
60
|
-
img.style.borderRadius = `${RADIUS.sm}
|
|
25
|
+
img.style.borderRadius = `${RADIUS.sm}`;
|
|
61
26
|
img.style.marginBottom = `${SPACE[2]}px`;
|
|
62
27
|
if (alt) img.alt = alt;
|
|
63
28
|
return img;
|
|
@@ -135,7 +100,7 @@ function renderQuizPlayback(container, data, callbacks) {
|
|
|
135
100
|
prevButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
|
|
136
101
|
prevButton.style.backgroundColor = COLORS.surface;
|
|
137
102
|
prevButton.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
138
|
-
prevButton.style.borderRadius = `${RADIUS.md}
|
|
103
|
+
prevButton.style.borderRadius = `${RADIUS.md}`;
|
|
139
104
|
prevButton.style.color = COLORS.text;
|
|
140
105
|
prevButton.style.cursor = "pointer";
|
|
141
106
|
prevButton.style.fontSize = `${TYPE.base}px`;
|
|
@@ -155,8 +120,8 @@ function renderQuizPlayback(container, data, callbacks) {
|
|
|
155
120
|
nextButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
|
|
156
121
|
nextButton.style.backgroundColor = COLORS.accent;
|
|
157
122
|
nextButton.style.border = "none";
|
|
158
|
-
nextButton.style.borderRadius = `${RADIUS.md}
|
|
159
|
-
nextButton.style.color = COLORS.
|
|
123
|
+
nextButton.style.borderRadius = `${RADIUS.md}`;
|
|
124
|
+
nextButton.style.color = COLORS.accentContrast;
|
|
160
125
|
nextButton.style.cursor = "pointer";
|
|
161
126
|
nextButton.style.fontSize = `${TYPE.base}px`;
|
|
162
127
|
nextButton.style.fontWeight = "600";
|
|
@@ -197,7 +162,7 @@ function renderQuizPlayback(container, data, callbacks) {
|
|
|
197
162
|
button.style.padding = `${SPACE[3]}px ${SPACE[4]}px`;
|
|
198
163
|
button.style.backgroundColor = COLORS.surface;
|
|
199
164
|
button.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
200
|
-
button.style.borderRadius = `${RADIUS.md}
|
|
165
|
+
button.style.borderRadius = `${RADIUS.md}`;
|
|
201
166
|
button.style.cursor = "pointer";
|
|
202
167
|
button.style.fontSize = `${TYPE.base}px`;
|
|
203
168
|
button.style.color = COLORS.text;
|
|
@@ -299,8 +264,64 @@ function unmountQuizPlayback(container) {
|
|
|
299
264
|
quizPlaybackSessions.delete(container);
|
|
300
265
|
}
|
|
301
266
|
|
|
267
|
+
// src/validate.ts
|
|
268
|
+
function validateQuizData(data) {
|
|
269
|
+
const errors = [];
|
|
270
|
+
if (data.questions.length === 0) {
|
|
271
|
+
errors.push("at least one question is required");
|
|
272
|
+
return errors;
|
|
273
|
+
}
|
|
274
|
+
data.questions.forEach((question, index) => {
|
|
275
|
+
if (question.text.trim().length === 0) {
|
|
276
|
+
errors.push(`questions[${index}].text must be a non-empty string`);
|
|
277
|
+
}
|
|
278
|
+
if (question.options.length === 0) {
|
|
279
|
+
errors.push(`questions[${index}].at least one option is required`);
|
|
280
|
+
} else {
|
|
281
|
+
question.options.forEach((option, optionIndex) => {
|
|
282
|
+
if (option.text.trim().length === 0) {
|
|
283
|
+
errors.push(
|
|
284
|
+
`questions[${index}].options[${optionIndex}].text must be a non-empty string`
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
if (question.correctOptionId === null) {
|
|
290
|
+
errors.push(
|
|
291
|
+
`questions[${index}].exactly one correct option must be resolvable`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
return errors;
|
|
296
|
+
}
|
|
297
|
+
function getQuizAuthoringIssues(data) {
|
|
298
|
+
if (data.questions.length === 0) return ["Add at least one question"];
|
|
299
|
+
const issues = [];
|
|
300
|
+
data.questions.forEach((question, index) => {
|
|
301
|
+
const label = `Question ${index + 1}`;
|
|
302
|
+
if (question.text.trim().length === 0) {
|
|
303
|
+
issues.push(`${label} needs text`);
|
|
304
|
+
}
|
|
305
|
+
if (question.options.length < 2) {
|
|
306
|
+
issues.push(`${label} needs at least 2 answers`);
|
|
307
|
+
}
|
|
308
|
+
question.options.forEach((option, optionIndex) => {
|
|
309
|
+
if (option.text.trim().length === 0) {
|
|
310
|
+
issues.push(`${label}, answer ${optionIndex + 1} needs text`);
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
const hasCorrect = question.options.some(
|
|
314
|
+
(option) => option.id === question.correctOptionId
|
|
315
|
+
);
|
|
316
|
+
if (!hasCorrect) issues.push(`${label} needs a correct answer`);
|
|
317
|
+
});
|
|
318
|
+
return issues;
|
|
319
|
+
}
|
|
320
|
+
|
|
302
321
|
// src/quiz-editor.ts
|
|
303
322
|
var quizEditorControllers = /* @__PURE__ */ new WeakMap();
|
|
323
|
+
var editorInstanceCount = 0;
|
|
324
|
+
var MIN_ANSWERS = 2;
|
|
304
325
|
function buildMediaRef(src, alt) {
|
|
305
326
|
if (src.length === 0) return void 0;
|
|
306
327
|
return alt.length > 0 ? { src, alt } : { src };
|
|
@@ -311,8 +332,9 @@ function createPreview(media, className) {
|
|
|
311
332
|
img.className = className;
|
|
312
333
|
img.src = media.src;
|
|
313
334
|
img.style.maxWidth = "100%";
|
|
335
|
+
img.style.maxHeight = "160px";
|
|
314
336
|
img.style.height = "auto";
|
|
315
|
-
img.style.borderRadius = `${RADIUS.sm}
|
|
337
|
+
img.style.borderRadius = `${RADIUS.sm}`;
|
|
316
338
|
img.style.marginTop = `${SPACE[1]}px`;
|
|
317
339
|
if (media.alt) img.alt = media.alt;
|
|
318
340
|
return img;
|
|
@@ -341,20 +363,189 @@ function nextQuestionId(questions, index) {
|
|
|
341
363
|
return `q-${candidate}`;
|
|
342
364
|
}
|
|
343
365
|
function emptyQuestion(id) {
|
|
344
|
-
return {
|
|
366
|
+
return {
|
|
367
|
+
id,
|
|
368
|
+
text: "",
|
|
369
|
+
options: [
|
|
370
|
+
{ id: `${id}-a`, text: "" },
|
|
371
|
+
{ id: `${id}-b`, text: "" }
|
|
372
|
+
],
|
|
373
|
+
correctOptionId: `${id}-a`
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
function setShown(el, shown, display = "") {
|
|
377
|
+
el.style.display = shown ? display : "none";
|
|
378
|
+
}
|
|
379
|
+
function setValue(input, value) {
|
|
380
|
+
if (input.value !== value) input.value = value;
|
|
381
|
+
}
|
|
382
|
+
function createButton(text, variant, className) {
|
|
383
|
+
const button = document.createElement("button");
|
|
384
|
+
button.type = "button";
|
|
385
|
+
button.className = `interlace-btn interlace-btn--${variant} ${className}`;
|
|
386
|
+
button.textContent = text;
|
|
387
|
+
return button;
|
|
388
|
+
}
|
|
389
|
+
function createRemoveButton(className) {
|
|
390
|
+
const button = createButton("\xD7", "ghost", `interlace-btn--icon ${className}`);
|
|
391
|
+
button.style.fontSize = `${TYPE.lg}px`;
|
|
392
|
+
button.style.lineHeight = "1";
|
|
393
|
+
return button;
|
|
394
|
+
}
|
|
395
|
+
function setRemoveAllowed(button, allowed, reason) {
|
|
396
|
+
button.disabled = !allowed;
|
|
397
|
+
if (allowed) button.removeAttribute("title");
|
|
398
|
+
else button.title = reason;
|
|
399
|
+
}
|
|
400
|
+
function createInput(className, id, placeholder) {
|
|
401
|
+
const input = document.createElement("input");
|
|
402
|
+
input.type = "text";
|
|
403
|
+
input.id = id;
|
|
404
|
+
input.className = `interlace-input ${className}`;
|
|
405
|
+
input.style.width = "100%";
|
|
406
|
+
input.style.boxSizing = "border-box";
|
|
407
|
+
if (placeholder) input.placeholder = placeholder;
|
|
408
|
+
return input;
|
|
409
|
+
}
|
|
410
|
+
function createLabel(text, forId) {
|
|
411
|
+
const label = document.createElement("label");
|
|
412
|
+
label.className = "interlace-label";
|
|
413
|
+
label.htmlFor = forId;
|
|
414
|
+
label.textContent = text;
|
|
415
|
+
label.style.fontSize = `${TYPE.sm}px`;
|
|
416
|
+
label.style.fontWeight = "600";
|
|
417
|
+
return label;
|
|
418
|
+
}
|
|
419
|
+
function createField(labelText, input) {
|
|
420
|
+
const field = document.createElement("div");
|
|
421
|
+
field.className = "interlace-field";
|
|
422
|
+
field.style.display = "flex";
|
|
423
|
+
field.style.flexDirection = "column";
|
|
424
|
+
field.style.gap = `${SPACE[1]}px`;
|
|
425
|
+
field.append(createLabel(labelText, input.id), input);
|
|
426
|
+
return field;
|
|
427
|
+
}
|
|
428
|
+
function createErrorText(id, className) {
|
|
429
|
+
const error = document.createElement("p");
|
|
430
|
+
error.id = id;
|
|
431
|
+
error.className = `interlace-hint quiz-field-error ${className}`;
|
|
432
|
+
error.style.margin = "0";
|
|
433
|
+
error.style.fontSize = `${TYPE.sm}px`;
|
|
434
|
+
error.style.color = COLORS.errorText;
|
|
435
|
+
setShown(error, false);
|
|
436
|
+
return error;
|
|
437
|
+
}
|
|
438
|
+
function setFieldError(input, error, message) {
|
|
439
|
+
if (message) {
|
|
440
|
+
error.textContent = message;
|
|
441
|
+
setShown(error, true);
|
|
442
|
+
input.setAttribute("aria-invalid", "true");
|
|
443
|
+
input.setAttribute("aria-describedby", error.id);
|
|
444
|
+
} else {
|
|
445
|
+
error.textContent = "";
|
|
446
|
+
setShown(error, false);
|
|
447
|
+
input.removeAttribute("aria-invalid");
|
|
448
|
+
input.removeAttribute("aria-describedby");
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
function createMediaEditor(options) {
|
|
452
|
+
const { classPrefix, idPrefix } = options;
|
|
453
|
+
const root = document.createElement("div");
|
|
454
|
+
root.className = classPrefix;
|
|
455
|
+
const addButton = createButton("+ Add image", "ghost", `${classPrefix}-add`);
|
|
456
|
+
addButton.setAttribute("aria-label", options.addLabel);
|
|
457
|
+
addButton.setAttribute("aria-controls", `${idPrefix}-panel`);
|
|
458
|
+
addButton.style.paddingLeft = `${SPACE[2]}px`;
|
|
459
|
+
addButton.style.paddingRight = `${SPACE[2]}px`;
|
|
460
|
+
const panel = document.createElement("div");
|
|
461
|
+
panel.id = `${idPrefix}-panel`;
|
|
462
|
+
panel.className = `${classPrefix}-panel`;
|
|
463
|
+
panel.style.flexDirection = "column";
|
|
464
|
+
panel.style.gap = `${SPACE[2]}px`;
|
|
465
|
+
panel.style.padding = `${SPACE[3]}px`;
|
|
466
|
+
panel.style.backgroundColor = COLORS.surfaceRaised;
|
|
467
|
+
panel.style.border = `1px solid ${COLORS.border}`;
|
|
468
|
+
panel.style.borderRadius = `${RADIUS.md}`;
|
|
469
|
+
const srcInput = createInput(
|
|
470
|
+
`${classPrefix}-src`,
|
|
471
|
+
`${idPrefix}-src`,
|
|
472
|
+
"https://example.com/image.png"
|
|
473
|
+
);
|
|
474
|
+
const altInput = createInput(
|
|
475
|
+
`${classPrefix}-alt`,
|
|
476
|
+
`${idPrefix}-alt`,
|
|
477
|
+
"Describe the image for screen readers"
|
|
478
|
+
);
|
|
479
|
+
const previewSlot = document.createElement("div");
|
|
480
|
+
previewSlot.className = options.previewSlotClass;
|
|
481
|
+
const removeButton = createButton(
|
|
482
|
+
"Remove image",
|
|
483
|
+
"ghost",
|
|
484
|
+
`${classPrefix}-remove`
|
|
485
|
+
);
|
|
486
|
+
removeButton.style.alignSelf = "flex-start";
|
|
487
|
+
panel.append(
|
|
488
|
+
createField("Image URL", srcInput),
|
|
489
|
+
createField("Image description (alt text)", altInput),
|
|
490
|
+
previewSlot,
|
|
491
|
+
removeButton
|
|
492
|
+
);
|
|
493
|
+
root.append(addButton, panel);
|
|
494
|
+
const setExpanded = (expanded) => {
|
|
495
|
+
setShown(panel, expanded, "flex");
|
|
496
|
+
setShown(addButton, !expanded);
|
|
497
|
+
addButton.setAttribute("aria-expanded", String(expanded));
|
|
498
|
+
};
|
|
499
|
+
setExpanded(false);
|
|
500
|
+
const handleEdit = () => {
|
|
501
|
+
const media = buildMediaRef(srcInput.value, altInput.value);
|
|
502
|
+
syncPreview(previewSlot, media, options.previewClass);
|
|
503
|
+
options.onEdit(media);
|
|
504
|
+
};
|
|
505
|
+
srcInput.addEventListener("input", handleEdit);
|
|
506
|
+
altInput.addEventListener("input", handleEdit);
|
|
507
|
+
addButton.addEventListener("click", () => {
|
|
508
|
+
setExpanded(true);
|
|
509
|
+
options.onExpand();
|
|
510
|
+
srcInput.focus();
|
|
511
|
+
});
|
|
512
|
+
removeButton.addEventListener("click", () => {
|
|
513
|
+
srcInput.value = "";
|
|
514
|
+
altInput.value = "";
|
|
515
|
+
syncPreview(previewSlot, void 0, options.previewClass);
|
|
516
|
+
setExpanded(false);
|
|
517
|
+
options.onRemove();
|
|
518
|
+
addButton.focus();
|
|
519
|
+
});
|
|
520
|
+
const sync = (media, expanded) => {
|
|
521
|
+
setValue(srcInput, media?.src ?? "");
|
|
522
|
+
setValue(altInput, media?.alt ?? "");
|
|
523
|
+
syncPreview(previewSlot, media, options.previewClass);
|
|
524
|
+
setExpanded(expanded);
|
|
525
|
+
};
|
|
526
|
+
return { root, srcInput, altInput, previewSlot, sync };
|
|
345
527
|
}
|
|
346
528
|
function createQuizEditorController(container, data, onChange) {
|
|
529
|
+
ensureInterlaceStyles(container.ownerDocument);
|
|
530
|
+
editorInstanceCount += 1;
|
|
531
|
+
const uid = `quiz-editor-${editorInstanceCount}`;
|
|
347
532
|
container.innerHTML = "";
|
|
348
533
|
const root = document.createElement("div");
|
|
349
534
|
root.className = "quiz-editor";
|
|
350
535
|
root.style.fontFamily = FONTS.body;
|
|
351
536
|
root.style.color = COLORS.text;
|
|
352
537
|
root.style.lineHeight = "1.5";
|
|
538
|
+
root.style.display = "flex";
|
|
539
|
+
root.style.flexDirection = "column";
|
|
540
|
+
root.style.gap = `${SPACE[3]}px`;
|
|
353
541
|
let currentData = data;
|
|
354
542
|
let currentOnChange = onChange;
|
|
355
543
|
let selectedIndex = 0;
|
|
356
544
|
let bodyQuestionIndex = -1;
|
|
357
545
|
let optionRows = [];
|
|
546
|
+
let tabButtons = [];
|
|
547
|
+
const touched = /* @__PURE__ */ new Set();
|
|
548
|
+
const expandedMedia = /* @__PURE__ */ new Set();
|
|
358
549
|
const emit = (questions) => {
|
|
359
550
|
currentOnChange({ questions });
|
|
360
551
|
};
|
|
@@ -365,35 +556,41 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
365
556
|
questions[selectedIndex] = mutate(active);
|
|
366
557
|
emit(questions);
|
|
367
558
|
};
|
|
559
|
+
const activeKey = (optionIndex) => {
|
|
560
|
+
const active = currentData.questions[selectedIndex];
|
|
561
|
+
const questionKey = active?.id ?? `#${selectedIndex}`;
|
|
562
|
+
if (optionIndex === void 0) return questionKey;
|
|
563
|
+
return `${questionKey}/${active?.options[optionIndex]?.id ?? `#${optionIndex}`}`;
|
|
564
|
+
};
|
|
368
565
|
const questionsHeader = document.createElement("div");
|
|
566
|
+
questionsHeader.id = `${uid}-questions-label`;
|
|
567
|
+
questionsHeader.className = "interlace-label";
|
|
369
568
|
questionsHeader.textContent = "Questions";
|
|
370
|
-
questionsHeader.style.marginBottom = `${SPACE[2]}px`;
|
|
371
|
-
questionsHeader.style.fontWeight = "600";
|
|
372
569
|
questionsHeader.style.fontSize = `${TYPE.sm}px`;
|
|
570
|
+
questionsHeader.style.fontWeight = "600";
|
|
373
571
|
questionsHeader.style.color = COLORS.textMuted;
|
|
374
|
-
|
|
572
|
+
const tabsRow = document.createElement("div");
|
|
573
|
+
tabsRow.style.display = "flex";
|
|
574
|
+
tabsRow.style.flexWrap = "wrap";
|
|
575
|
+
tabsRow.style.alignItems = "center";
|
|
576
|
+
tabsRow.style.gap = `${SPACE[2]}px`;
|
|
375
577
|
const tabsBar = document.createElement("div");
|
|
376
578
|
tabsBar.className = "quiz-question-tabs";
|
|
579
|
+
tabsBar.setAttribute("role", "tablist");
|
|
580
|
+
tabsBar.setAttribute("aria-labelledby", questionsHeader.id);
|
|
377
581
|
tabsBar.style.display = "flex";
|
|
378
582
|
tabsBar.style.flexWrap = "wrap";
|
|
379
583
|
tabsBar.style.gap = `${SPACE[2]}px`;
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
addQuestionButton.textContent = "Add question";
|
|
386
|
-
addQuestionButton.style.marginBottom = `${SPACE[4]}px`;
|
|
387
|
-
addQuestionButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
|
|
388
|
-
addQuestionButton.style.backgroundColor = COLORS.surface;
|
|
389
|
-
addQuestionButton.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
390
|
-
addQuestionButton.style.borderRadius = `${RADIUS.sm}px`;
|
|
391
|
-
addQuestionButton.style.cursor = "pointer";
|
|
392
|
-
addQuestionButton.style.fontSize = `${TYPE.sm}px`;
|
|
393
|
-
addQuestionButton.style.fontWeight = "600";
|
|
394
|
-
addQuestionButton.style.color = COLORS.text;
|
|
584
|
+
const addQuestionButton = createButton(
|
|
585
|
+
"+ Add question",
|
|
586
|
+
"secondary",
|
|
587
|
+
"quiz-add-question"
|
|
588
|
+
);
|
|
395
589
|
addQuestionButton.addEventListener("click", () => {
|
|
396
|
-
const id = nextQuestionId(
|
|
590
|
+
const id = nextQuestionId(
|
|
591
|
+
currentData.questions,
|
|
592
|
+
currentData.questions.length
|
|
593
|
+
);
|
|
397
594
|
const questions = [...currentData.questions, emptyQuestion(id)];
|
|
398
595
|
currentData = { questions };
|
|
399
596
|
selectedIndex = questions.length - 1;
|
|
@@ -402,107 +599,118 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
402
599
|
renderTabs();
|
|
403
600
|
syncActiveBody();
|
|
404
601
|
});
|
|
405
|
-
|
|
602
|
+
tabsRow.append(tabsBar, addQuestionButton);
|
|
603
|
+
root.append(questionsHeader, tabsRow);
|
|
406
604
|
const body = document.createElement("div");
|
|
605
|
+
body.id = `${uid}-panel`;
|
|
407
606
|
body.className = "quiz-editor-body";
|
|
607
|
+
body.setAttribute("role", "tabpanel");
|
|
608
|
+
body.style.display = "flex";
|
|
609
|
+
body.style.flexDirection = "column";
|
|
610
|
+
body.style.gap = `${SPACE[4]}px`;
|
|
611
|
+
body.style.padding = `${SPACE[4]}px`;
|
|
612
|
+
body.style.backgroundColor = COLORS.surface;
|
|
613
|
+
body.style.border = `1px solid ${COLORS.border}`;
|
|
614
|
+
body.style.borderRadius = `${RADIUS.md}`;
|
|
408
615
|
root.appendChild(body);
|
|
409
|
-
const questionInput =
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
questionInput.style.boxSizing = "border-box";
|
|
415
|
-
questionInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
|
|
616
|
+
const questionInput = createInput(
|
|
617
|
+
"quiz-question",
|
|
618
|
+
`${uid}-question`,
|
|
619
|
+
"Type your question"
|
|
620
|
+
);
|
|
416
621
|
questionInput.style.fontSize = `${TYPE.md}px`;
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
622
|
+
const questionError = createErrorText(
|
|
623
|
+
`${uid}-question-error`,
|
|
624
|
+
"quiz-question-error"
|
|
625
|
+
);
|
|
626
|
+
const refreshQuestionError = () => {
|
|
627
|
+
const show = touched.has(`text:${activeKey()}`) && questionInput.value.trim().length === 0;
|
|
628
|
+
setFieldError(
|
|
629
|
+
questionInput,
|
|
630
|
+
questionError,
|
|
631
|
+
show ? "Add the question text" : null
|
|
632
|
+
);
|
|
633
|
+
};
|
|
421
634
|
questionInput.addEventListener("input", () => {
|
|
422
635
|
patchActiveQuestion((question) => ({
|
|
423
636
|
...question,
|
|
424
637
|
text: questionInput.value
|
|
425
638
|
}));
|
|
639
|
+
refreshQuestionError();
|
|
426
640
|
});
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
const
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
optionsTitle.style.color = COLORS.textMuted;
|
|
487
|
-
body.appendChild(optionsTitle);
|
|
641
|
+
questionInput.addEventListener("blur", () => {
|
|
642
|
+
touched.add(`text:${activeKey()}`);
|
|
643
|
+
refreshQuestionError();
|
|
644
|
+
});
|
|
645
|
+
const removeQuestionButton = createRemoveButton("quiz-question-remove");
|
|
646
|
+
removeQuestionButton.addEventListener(
|
|
647
|
+
"click",
|
|
648
|
+
() => removeQuestion(selectedIndex)
|
|
649
|
+
);
|
|
650
|
+
const questionHeader = document.createElement("div");
|
|
651
|
+
questionHeader.style.display = "flex";
|
|
652
|
+
questionHeader.style.alignItems = "center";
|
|
653
|
+
questionHeader.style.justifyContent = "space-between";
|
|
654
|
+
questionHeader.style.gap = `${SPACE[2]}px`;
|
|
655
|
+
questionHeader.append(
|
|
656
|
+
createLabel("Question", questionInput.id),
|
|
657
|
+
removeQuestionButton
|
|
658
|
+
);
|
|
659
|
+
const questionField = document.createElement("div");
|
|
660
|
+
questionField.className = "interlace-field";
|
|
661
|
+
questionField.style.display = "flex";
|
|
662
|
+
questionField.style.flexDirection = "column";
|
|
663
|
+
questionField.style.gap = `${SPACE[1]}px`;
|
|
664
|
+
questionField.append(questionHeader, questionInput, questionError);
|
|
665
|
+
const questionMedia = createMediaEditor({
|
|
666
|
+
classPrefix: "quiz-question-media",
|
|
667
|
+
idPrefix: `${uid}-question-media`,
|
|
668
|
+
addLabel: "Add image to question",
|
|
669
|
+
previewSlotClass: "quiz-question-media-preview-slot",
|
|
670
|
+
previewClass: "quiz-question-media-preview",
|
|
671
|
+
onEdit: (media) => {
|
|
672
|
+
expandedMedia.add(activeKey());
|
|
673
|
+
patchActiveQuestion((question) => ({ ...question, media }));
|
|
674
|
+
},
|
|
675
|
+
onExpand: () => expandedMedia.add(activeKey()),
|
|
676
|
+
onRemove: () => {
|
|
677
|
+
expandedMedia.delete(activeKey());
|
|
678
|
+
patchActiveQuestion((question) => ({ ...question, media: void 0 }));
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
questionField.appendChild(questionMedia.root);
|
|
682
|
+
body.appendChild(questionField);
|
|
683
|
+
const answersSection = document.createElement("div");
|
|
684
|
+
answersSection.style.display = "flex";
|
|
685
|
+
answersSection.style.flexDirection = "column";
|
|
686
|
+
answersSection.style.gap = `${SPACE[2]}px`;
|
|
687
|
+
const answersHeading = document.createElement("div");
|
|
688
|
+
answersHeading.id = `${uid}-answers-label`;
|
|
689
|
+
answersHeading.className = "interlace-label quiz-answers-heading";
|
|
690
|
+
answersHeading.textContent = "Answers";
|
|
691
|
+
answersHeading.style.fontSize = `${TYPE.sm}px`;
|
|
692
|
+
answersHeading.style.fontWeight = "600";
|
|
693
|
+
const answersHint = document.createElement("p");
|
|
694
|
+
answersHint.id = `${uid}-answers-hint`;
|
|
695
|
+
answersHint.className = "interlace-hint quiz-answers-hint";
|
|
696
|
+
answersHint.textContent = "Select the correct answer.";
|
|
697
|
+
answersHint.style.margin = "0";
|
|
698
|
+
answersHint.style.fontSize = `${TYPE.sm}px`;
|
|
699
|
+
answersHint.style.color = COLORS.textMuted;
|
|
488
700
|
const optionsContainer = document.createElement("div");
|
|
701
|
+
optionsContainer.className = "quiz-options";
|
|
702
|
+
optionsContainer.setAttribute("role", "radiogroup");
|
|
703
|
+
optionsContainer.setAttribute("aria-labelledby", answersHeading.id);
|
|
704
|
+
optionsContainer.setAttribute("aria-describedby", answersHint.id);
|
|
489
705
|
optionsContainer.style.display = "flex";
|
|
490
706
|
optionsContainer.style.flexDirection = "column";
|
|
491
707
|
optionsContainer.style.gap = `${SPACE[2]}px`;
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
addOptionButton.style.
|
|
498
|
-
addOptionButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
|
|
499
|
-
addOptionButton.style.backgroundColor = COLORS.surface;
|
|
500
|
-
addOptionButton.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
501
|
-
addOptionButton.style.borderRadius = `${RADIUS.sm}px`;
|
|
502
|
-
addOptionButton.style.cursor = "pointer";
|
|
503
|
-
addOptionButton.style.fontSize = `${TYPE.sm}px`;
|
|
504
|
-
addOptionButton.style.fontWeight = "600";
|
|
505
|
-
addOptionButton.style.color = COLORS.text;
|
|
708
|
+
const addOptionButton = createButton(
|
|
709
|
+
"+ Add answer",
|
|
710
|
+
"secondary",
|
|
711
|
+
"quiz-add-option"
|
|
712
|
+
);
|
|
713
|
+
addOptionButton.style.alignSelf = "flex-start";
|
|
506
714
|
addOptionButton.addEventListener("click", () => {
|
|
507
715
|
patchActiveQuestion((question) => {
|
|
508
716
|
const nextOptions = [
|
|
@@ -520,57 +728,67 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
520
728
|
};
|
|
521
729
|
});
|
|
522
730
|
});
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
correctLabel.appendChild(document.createTextNode("Correct answer"));
|
|
531
|
-
const select = document.createElement("select");
|
|
532
|
-
select.className = "quiz-correct-answer";
|
|
533
|
-
select.style.display = "block";
|
|
534
|
-
select.style.width = "100%";
|
|
535
|
-
select.style.marginTop = `${SPACE[1]}px`;
|
|
536
|
-
select.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
|
|
537
|
-
select.style.fontSize = `${TYPE.base}px`;
|
|
538
|
-
select.style.color = COLORS.text;
|
|
539
|
-
select.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
540
|
-
select.style.borderRadius = `${RADIUS.sm}px`;
|
|
541
|
-
select.style.backgroundColor = COLORS.surface;
|
|
542
|
-
select.addEventListener("change", () => {
|
|
543
|
-
patchActiveQuestion((question) => ({
|
|
544
|
-
...question,
|
|
545
|
-
correctOptionId: select.value.length > 0 ? select.value : null
|
|
546
|
-
}));
|
|
547
|
-
});
|
|
548
|
-
correctLabel.appendChild(select);
|
|
549
|
-
body.appendChild(correctLabel);
|
|
731
|
+
answersSection.append(
|
|
732
|
+
answersHeading,
|
|
733
|
+
answersHint,
|
|
734
|
+
optionsContainer,
|
|
735
|
+
addOptionButton
|
|
736
|
+
);
|
|
737
|
+
body.appendChild(answersSection);
|
|
550
738
|
container.appendChild(root);
|
|
551
|
-
const createOptionRow = (option, optionIndex) => {
|
|
739
|
+
const createOptionRow = (option, optionIndex, optionCount) => {
|
|
740
|
+
const number = optionIndex + 1;
|
|
741
|
+
const idPrefix = `${uid}-q${selectedIndex}-opt${optionIndex}`;
|
|
552
742
|
const row = document.createElement("div");
|
|
553
|
-
row.className = "quiz-option-row";
|
|
554
|
-
row.style.display = "
|
|
555
|
-
row.style.
|
|
556
|
-
row.style.gap = `${SPACE[2]}px`;
|
|
743
|
+
row.className = "quiz-option-row interlace-choice";
|
|
744
|
+
row.style.display = "grid";
|
|
745
|
+
row.style.gridTemplateColumns = "auto minmax(0, 1fr) auto";
|
|
557
746
|
row.style.alignItems = "center";
|
|
558
|
-
row.style.
|
|
559
|
-
row.style.
|
|
560
|
-
row.style.
|
|
561
|
-
row.style.
|
|
562
|
-
const
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
747
|
+
row.style.columnGap = `${SPACE[2]}px`;
|
|
748
|
+
row.style.rowGap = `${SPACE[1]}px`;
|
|
749
|
+
row.style.cursor = "default";
|
|
750
|
+
row.style.outline = "none";
|
|
751
|
+
const radio = document.createElement("input");
|
|
752
|
+
radio.type = "radio";
|
|
753
|
+
radio.name = `${uid}-correct-${selectedIndex}`;
|
|
754
|
+
radio.className = "quiz-option-correct";
|
|
755
|
+
radio.value = option.id;
|
|
756
|
+
radio.checked = option.id === currentData.questions[selectedIndex]?.correctOptionId;
|
|
757
|
+
radio.setAttribute("aria-label", `Mark answer ${number} as correct`);
|
|
758
|
+
radio.style.width = "18px";
|
|
759
|
+
radio.style.height = "18px";
|
|
760
|
+
radio.style.margin = "0";
|
|
761
|
+
radio.style.accentColor = COLORS.accent;
|
|
762
|
+
radio.style.cursor = "pointer";
|
|
763
|
+
radio.style.gridColumn = "1";
|
|
764
|
+
radio.style.gridRow = "2";
|
|
765
|
+
radio.addEventListener("change", () => {
|
|
766
|
+
if (!radio.checked) return;
|
|
767
|
+
patchActiveQuestion((question) => ({
|
|
768
|
+
...question,
|
|
769
|
+
correctOptionId: question.options[optionIndex]?.id ?? null
|
|
770
|
+
}));
|
|
771
|
+
});
|
|
772
|
+
const textInput = createInput(
|
|
773
|
+
"quiz-option-text",
|
|
774
|
+
`${idPrefix}-text`,
|
|
775
|
+
"Answer text"
|
|
776
|
+
);
|
|
573
777
|
textInput.value = option.text;
|
|
778
|
+
textInput.style.gridColumn = "2";
|
|
779
|
+
textInput.style.gridRow = "2";
|
|
780
|
+
const label = createLabel(`Answer ${number}`, textInput.id);
|
|
781
|
+
label.style.gridColumn = "2";
|
|
782
|
+
label.style.gridRow = "1";
|
|
783
|
+
const error = createErrorText(`${idPrefix}-error`, "quiz-option-error");
|
|
784
|
+
const refreshError = () => {
|
|
785
|
+
const show = touched.has(`opt:${activeKey(optionIndex)}`) && textInput.value.trim().length === 0;
|
|
786
|
+
setFieldError(
|
|
787
|
+
textInput,
|
|
788
|
+
error,
|
|
789
|
+
show ? `Answer ${number} needs text` : null
|
|
790
|
+
);
|
|
791
|
+
};
|
|
574
792
|
textInput.addEventListener("input", () => {
|
|
575
793
|
patchActiveQuestion((question) => {
|
|
576
794
|
const nextOptions = question.options.slice();
|
|
@@ -580,63 +798,59 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
580
798
|
};
|
|
581
799
|
return { ...question, options: nextOptions };
|
|
582
800
|
});
|
|
801
|
+
refreshError();
|
|
802
|
+
});
|
|
803
|
+
textInput.addEventListener("blur", () => {
|
|
804
|
+
touched.add(`opt:${activeKey(optionIndex)}`);
|
|
805
|
+
refreshError();
|
|
583
806
|
});
|
|
584
|
-
const
|
|
585
|
-
mediaSrcInput.type = "text";
|
|
586
|
-
mediaSrcInput.className = "quiz-option-media-src";
|
|
587
|
-
mediaSrcInput.placeholder = "Option media URL";
|
|
588
|
-
mediaSrcInput.value = option.media?.src ?? "";
|
|
589
|
-
mediaSrcInput.style.flex = "1";
|
|
590
|
-
mediaSrcInput.style.minWidth = "120px";
|
|
591
|
-
mediaSrcInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
|
|
592
|
-
mediaSrcInput.style.fontSize = `${TYPE.base}px`;
|
|
593
|
-
mediaSrcInput.style.color = COLORS.text;
|
|
594
|
-
mediaSrcInput.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
595
|
-
mediaSrcInput.style.borderRadius = `${RADIUS.sm}px`;
|
|
596
|
-
mediaSrcInput.style.backgroundColor = COLORS.surface;
|
|
597
|
-
const mediaAltInput = document.createElement("input");
|
|
598
|
-
mediaAltInput.type = "text";
|
|
599
|
-
mediaAltInput.className = "quiz-option-media-alt";
|
|
600
|
-
mediaAltInput.placeholder = "Option media alt";
|
|
601
|
-
mediaAltInput.value = option.media?.alt ?? "";
|
|
602
|
-
mediaAltInput.style.flex = "1";
|
|
603
|
-
mediaAltInput.style.minWidth = "120px";
|
|
604
|
-
mediaAltInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
|
|
605
|
-
mediaAltInput.style.fontSize = `${TYPE.base}px`;
|
|
606
|
-
mediaAltInput.style.color = COLORS.text;
|
|
607
|
-
mediaAltInput.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
608
|
-
mediaAltInput.style.borderRadius = `${RADIUS.sm}px`;
|
|
609
|
-
mediaAltInput.style.backgroundColor = COLORS.surface;
|
|
610
|
-
const previewSlot = document.createElement("div");
|
|
611
|
-
previewSlot.className = "quiz-option-media-preview";
|
|
612
|
-
syncPreview(previewSlot, option.media, "quiz-option-thumb");
|
|
613
|
-
const emitOptionMedia = () => {
|
|
614
|
-
const media = buildMediaRef(mediaSrcInput.value, mediaAltInput.value);
|
|
615
|
-
syncPreview(previewSlot, media, "quiz-option-thumb");
|
|
807
|
+
const patchOptionMedia = (media2) => {
|
|
616
808
|
patchActiveQuestion((question) => {
|
|
617
809
|
const nextOptions = question.options.slice();
|
|
618
|
-
nextOptions[optionIndex] = { ...nextOptions[optionIndex], media };
|
|
810
|
+
nextOptions[optionIndex] = { ...nextOptions[optionIndex], media: media2 };
|
|
619
811
|
return { ...question, options: nextOptions };
|
|
620
812
|
});
|
|
621
813
|
};
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
814
|
+
const media = createMediaEditor({
|
|
815
|
+
classPrefix: "quiz-option-media",
|
|
816
|
+
idPrefix: `${idPrefix}-media`,
|
|
817
|
+
addLabel: `Add image to answer ${number}`,
|
|
818
|
+
previewSlotClass: "quiz-option-media-preview",
|
|
819
|
+
previewClass: "quiz-option-thumb",
|
|
820
|
+
onEdit: (next) => {
|
|
821
|
+
expandedMedia.add(activeKey(optionIndex));
|
|
822
|
+
patchOptionMedia(next);
|
|
823
|
+
},
|
|
824
|
+
onExpand: () => expandedMedia.add(activeKey(optionIndex)),
|
|
825
|
+
onRemove: () => {
|
|
826
|
+
expandedMedia.delete(activeKey(optionIndex));
|
|
827
|
+
patchOptionMedia(void 0);
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
const details = document.createElement("div");
|
|
831
|
+
details.style.gridColumn = "2";
|
|
832
|
+
details.style.gridRow = "3";
|
|
833
|
+
details.style.display = "flex";
|
|
834
|
+
details.style.flexDirection = "column";
|
|
835
|
+
details.style.alignItems = "flex-start";
|
|
836
|
+
details.style.gap = `${SPACE[1]}px`;
|
|
837
|
+
media.root.style.alignSelf = "stretch";
|
|
838
|
+
details.append(error, media.root);
|
|
839
|
+
const remove = createRemoveButton("quiz-option-remove");
|
|
840
|
+
remove.setAttribute("aria-label", `Remove answer ${number}`);
|
|
841
|
+
remove.style.gridColumn = "3";
|
|
842
|
+
remove.style.gridRow = "2";
|
|
843
|
+
setRemoveAllowed(
|
|
844
|
+
remove,
|
|
845
|
+
optionCount > MIN_ANSWERS,
|
|
846
|
+
`A question needs at least ${MIN_ANSWERS} answers`
|
|
847
|
+
);
|
|
636
848
|
remove.addEventListener("click", () => {
|
|
637
849
|
patchActiveQuestion((question) => {
|
|
638
850
|
const removed = question.options[optionIndex];
|
|
639
|
-
const nextOptions = question.options.filter(
|
|
851
|
+
const nextOptions = question.options.filter(
|
|
852
|
+
(_, i) => i !== optionIndex
|
|
853
|
+
);
|
|
640
854
|
return {
|
|
641
855
|
...question,
|
|
642
856
|
options: nextOptions,
|
|
@@ -645,9 +859,9 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
645
859
|
};
|
|
646
860
|
});
|
|
647
861
|
});
|
|
648
|
-
row.append(
|
|
862
|
+
row.append(label, radio, textInput, remove, details);
|
|
649
863
|
optionsContainer.appendChild(row);
|
|
650
|
-
return { row,
|
|
864
|
+
return { row, radio, textInput, error, media };
|
|
651
865
|
};
|
|
652
866
|
const selectQuestion = (index) => {
|
|
653
867
|
selectedIndex = index;
|
|
@@ -671,85 +885,79 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
671
885
|
};
|
|
672
886
|
const renderTabs = () => {
|
|
673
887
|
tabsBar.innerHTML = "";
|
|
674
|
-
currentData.questions.
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
tab.
|
|
683
|
-
tab.
|
|
684
|
-
tab.
|
|
685
|
-
tab.
|
|
686
|
-
tab.
|
|
687
|
-
tab.style.cursor = "pointer";
|
|
688
|
-
tab.style.fontSize = `${TYPE.sm}px`;
|
|
689
|
-
tab.style.fontWeight = "600";
|
|
690
|
-
if (index === selectedIndex) {
|
|
691
|
-
tab.style.backgroundColor = COLORS.accent;
|
|
692
|
-
tab.style.borderColor = COLORS.accent;
|
|
693
|
-
tab.style.color = COLORS.white;
|
|
694
|
-
} else {
|
|
695
|
-
tab.style.backgroundColor = COLORS.surface;
|
|
696
|
-
tab.style.color = COLORS.text;
|
|
697
|
-
}
|
|
888
|
+
const count = currentData.questions.length;
|
|
889
|
+
tabButtons = currentData.questions.map((_, index) => {
|
|
890
|
+
const selected = index === selectedIndex;
|
|
891
|
+
const tab = createButton(
|
|
892
|
+
`Question ${index + 1}`,
|
|
893
|
+
selected ? "primary" : "secondary",
|
|
894
|
+
"quiz-question-tab"
|
|
895
|
+
);
|
|
896
|
+
tab.id = `${uid}-tab-${index}`;
|
|
897
|
+
tab.setAttribute("role", "tab");
|
|
898
|
+
tab.setAttribute("aria-selected", String(selected));
|
|
899
|
+
tab.setAttribute("aria-controls", body.id);
|
|
900
|
+
tab.tabIndex = selected ? 0 : -1;
|
|
698
901
|
tab.addEventListener("click", () => selectQuestion(index));
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
tabsBar.appendChild(wrap);
|
|
902
|
+
tab.addEventListener("keydown", (event) => {
|
|
903
|
+
let target = -1;
|
|
904
|
+
if (event.key === "ArrowRight") target = (index + 1) % count;
|
|
905
|
+
else if (event.key === "ArrowLeft")
|
|
906
|
+
target = (index - 1 + count) % count;
|
|
907
|
+
else if (event.key === "Home") target = 0;
|
|
908
|
+
else if (event.key === "End") target = count - 1;
|
|
909
|
+
if (target < 0) return;
|
|
910
|
+
event.preventDefault();
|
|
911
|
+
selectQuestion(target);
|
|
912
|
+
tabButtons[target]?.focus();
|
|
913
|
+
});
|
|
914
|
+
tabsBar.appendChild(tab);
|
|
915
|
+
return tab;
|
|
714
916
|
});
|
|
917
|
+
body.setAttribute("aria-labelledby", `${uid}-tab-${selectedIndex}`);
|
|
918
|
+
removeQuestionButton.setAttribute(
|
|
919
|
+
"aria-label",
|
|
920
|
+
`Remove question ${selectedIndex + 1}`
|
|
921
|
+
);
|
|
922
|
+
setRemoveAllowed(
|
|
923
|
+
removeQuestionButton,
|
|
924
|
+
count > 1,
|
|
925
|
+
"A quiz needs at least one question"
|
|
926
|
+
);
|
|
715
927
|
};
|
|
716
928
|
const syncActiveBody = () => {
|
|
717
929
|
const active = currentData.questions[selectedIndex];
|
|
718
930
|
if (!active) return;
|
|
719
|
-
questionInput
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
931
|
+
setValue(questionInput, active.text);
|
|
932
|
+
questionMedia.sync(
|
|
933
|
+
active.media,
|
|
934
|
+
!!active.media || expandedMedia.has(activeKey())
|
|
935
|
+
);
|
|
936
|
+
refreshQuestionError();
|
|
723
937
|
const options = active.options;
|
|
724
938
|
if (bodyQuestionIndex !== selectedIndex || optionRows.length !== options.length) {
|
|
725
939
|
optionsContainer.innerHTML = "";
|
|
726
940
|
optionRows = options.map(
|
|
727
|
-
(option, optionIndex) => createOptionRow(option, optionIndex)
|
|
941
|
+
(option, optionIndex) => createOptionRow(option, optionIndex, options.length)
|
|
728
942
|
);
|
|
729
943
|
bodyQuestionIndex = selectedIndex;
|
|
730
|
-
} else {
|
|
731
|
-
optionRows.forEach((row, optionIndex) => {
|
|
732
|
-
const option = options[optionIndex];
|
|
733
|
-
row.textInput.value = option.text;
|
|
734
|
-
row.mediaSrcInput.value = option.media?.src ?? "";
|
|
735
|
-
row.mediaAltInput.value = option.media?.alt ?? "";
|
|
736
|
-
syncPreview(row.previewSlot, option.media, "quiz-option-thumb");
|
|
737
|
-
});
|
|
738
|
-
}
|
|
739
|
-
while (select.options.length < options.length) {
|
|
740
|
-
select.add(document.createElement("option"));
|
|
741
|
-
}
|
|
742
|
-
while (select.options.length > options.length) {
|
|
743
|
-
select.remove(select.options.length - 1);
|
|
744
|
-
}
|
|
745
|
-
for (let i = 0; i < options.length; i++) {
|
|
746
|
-
const el = select.options[i];
|
|
747
|
-
if (!el) continue;
|
|
748
|
-
const option = options[i];
|
|
749
|
-
el.value = option.id;
|
|
750
|
-
el.textContent = `Option ${i + 1}: ${option.text}`;
|
|
751
|
-
el.selected = option.id === active.correctOptionId;
|
|
752
944
|
}
|
|
945
|
+
optionRows.forEach((row, optionIndex) => {
|
|
946
|
+
const option = options[optionIndex];
|
|
947
|
+
setValue(row.textInput, option.text);
|
|
948
|
+
row.radio.value = option.id;
|
|
949
|
+
row.radio.checked = option.id === active.correctOptionId;
|
|
950
|
+
row.media.sync(
|
|
951
|
+
option.media,
|
|
952
|
+
!!option.media || expandedMedia.has(activeKey(optionIndex))
|
|
953
|
+
);
|
|
954
|
+
const show = touched.has(`opt:${activeKey(optionIndex)}`) && option.text.trim().length === 0;
|
|
955
|
+
setFieldError(
|
|
956
|
+
row.textInput,
|
|
957
|
+
row.error,
|
|
958
|
+
show ? `Answer ${optionIndex + 1} needs text` : null
|
|
959
|
+
);
|
|
960
|
+
});
|
|
753
961
|
};
|
|
754
962
|
const syncTo = (nextData, nextOnChange) => {
|
|
755
963
|
currentData = nextData;
|
|
@@ -782,6 +990,11 @@ function unmountQuizEditor(container) {
|
|
|
782
990
|
var QuizEditorType = {
|
|
783
991
|
getId: () => "quiz-editor",
|
|
784
992
|
getVersion: () => 1,
|
|
993
|
+
getDisplayName: () => "Quiz",
|
|
994
|
+
getDescription: () => "Multiple-choice questions that check understanding",
|
|
995
|
+
validate(data) {
|
|
996
|
+
return getQuizAuthoringIssues(data);
|
|
997
|
+
},
|
|
785
998
|
getMaximumScore(data) {
|
|
786
999
|
return data.questions.length;
|
|
787
1000
|
},
|
|
@@ -814,6 +1027,11 @@ var QuizEditorType = {
|
|
|
814
1027
|
var QuizContentType = {
|
|
815
1028
|
getId: () => "quiz-editor",
|
|
816
1029
|
getVersion: () => 1,
|
|
1030
|
+
getDisplayName: () => "Quiz",
|
|
1031
|
+
getDescription: () => "Multiple-choice questions that check understanding",
|
|
1032
|
+
validate(data) {
|
|
1033
|
+
return getQuizAuthoringIssues(data);
|
|
1034
|
+
},
|
|
817
1035
|
getMaximumScore(data) {
|
|
818
1036
|
return data.questions.length;
|
|
819
1037
|
},
|
|
@@ -835,40 +1053,10 @@ var QuizContentType = {
|
|
|
835
1053
|
unmountQuizEditor(container);
|
|
836
1054
|
}
|
|
837
1055
|
};
|
|
838
|
-
|
|
839
|
-
// src/validate.ts
|
|
840
|
-
function validateQuizData(data) {
|
|
841
|
-
const errors = [];
|
|
842
|
-
if (data.questions.length === 0) {
|
|
843
|
-
errors.push("at least one question is required");
|
|
844
|
-
return errors;
|
|
845
|
-
}
|
|
846
|
-
data.questions.forEach((question, index) => {
|
|
847
|
-
if (question.text.trim().length === 0) {
|
|
848
|
-
errors.push(`questions[${index}].text must be a non-empty string`);
|
|
849
|
-
}
|
|
850
|
-
if (question.options.length === 0) {
|
|
851
|
-
errors.push(`questions[${index}].at least one option is required`);
|
|
852
|
-
} else {
|
|
853
|
-
question.options.forEach((option, optionIndex) => {
|
|
854
|
-
if (option.text.trim().length === 0) {
|
|
855
|
-
errors.push(
|
|
856
|
-
`questions[${index}].options[${optionIndex}].text must be a non-empty string`
|
|
857
|
-
);
|
|
858
|
-
}
|
|
859
|
-
});
|
|
860
|
-
}
|
|
861
|
-
if (question.correctOptionId === null) {
|
|
862
|
-
errors.push(
|
|
863
|
-
`questions[${index}].exactly one correct option must be resolvable`
|
|
864
|
-
);
|
|
865
|
-
}
|
|
866
|
-
});
|
|
867
|
-
return errors;
|
|
868
|
-
}
|
|
869
1056
|
export {
|
|
870
1057
|
QuizContentType,
|
|
871
1058
|
QuizEditorType,
|
|
1059
|
+
getQuizAuthoringIssues,
|
|
872
1060
|
renderQuizPlayback,
|
|
873
1061
|
unmountQuizEditor,
|
|
874
1062
|
unmountQuizPlayback,
|