@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.cjs
CHANGED
|
@@ -22,6 +22,7 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
QuizContentType: () => QuizContentType,
|
|
24
24
|
QuizEditorType: () => QuizEditorType,
|
|
25
|
+
getQuizAuthoringIssues: () => getQuizAuthoringIssues,
|
|
25
26
|
renderQuizPlayback: () => renderQuizPlayback,
|
|
26
27
|
unmountQuizEditor: () => unmountQuizEditor,
|
|
27
28
|
unmountQuizPlayback: () => unmountQuizPlayback,
|
|
@@ -29,55 +30,11 @@ __export(index_exports, {
|
|
|
29
30
|
});
|
|
30
31
|
module.exports = __toCommonJS(index_exports);
|
|
31
32
|
|
|
33
|
+
// src/quiz-editor.ts
|
|
34
|
+
var import_core2 = require("@interlacejs/core");
|
|
35
|
+
|
|
32
36
|
// src/tokens.ts
|
|
33
|
-
var
|
|
34
|
-
accent: "#0066cc",
|
|
35
|
-
accentHover: "#0052a3",
|
|
36
|
-
accentLight: "rgba(0, 102, 204, 0.15)",
|
|
37
|
-
accentLighter: "rgba(0, 102, 204, 0.08)",
|
|
38
|
-
text: "#0f172a",
|
|
39
|
-
textMuted: "#64748b",
|
|
40
|
-
border: "#e2e8f0",
|
|
41
|
-
borderStrong: "#cbd5e1",
|
|
42
|
-
surface: "#ffffff",
|
|
43
|
-
surfaceRaised: "#f8fafc",
|
|
44
|
-
ground: "#f6f7f9",
|
|
45
|
-
errorBg: "#fef2f2",
|
|
46
|
-
errorText: "#b91c1c",
|
|
47
|
-
errorBorder: "#fecaca",
|
|
48
|
-
white: "#ffffff",
|
|
49
|
-
black: "#000000",
|
|
50
|
-
// Reveal-state aliases for quiz option feedback.
|
|
51
|
-
correctBg: "#0066cc",
|
|
52
|
-
correctText: "#ffffff",
|
|
53
|
-
incorrectBg: "#fef2f2",
|
|
54
|
-
incorrectText: "#b91c1c",
|
|
55
|
-
incorrectBorder: "#fecaca"
|
|
56
|
-
};
|
|
57
|
-
var FONTS = {
|
|
58
|
-
display: 'Georgia, "Times New Roman", Times, serif',
|
|
59
|
-
body: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif'
|
|
60
|
-
};
|
|
61
|
-
var SPACE = {
|
|
62
|
-
1: 4,
|
|
63
|
-
2: 8,
|
|
64
|
-
3: 12,
|
|
65
|
-
4: 16,
|
|
66
|
-
5: 24
|
|
67
|
-
};
|
|
68
|
-
var RADIUS = {
|
|
69
|
-
sm: 4,
|
|
70
|
-
md: 8,
|
|
71
|
-
lg: 12
|
|
72
|
-
};
|
|
73
|
-
var TYPE = {
|
|
74
|
-
xs: 12,
|
|
75
|
-
sm: 13,
|
|
76
|
-
base: 15,
|
|
77
|
-
md: 16,
|
|
78
|
-
lg: 20,
|
|
79
|
-
xl: 24
|
|
80
|
-
};
|
|
37
|
+
var import_core = require("@interlacejs/core");
|
|
81
38
|
|
|
82
39
|
// src/quiz-player.ts
|
|
83
40
|
var quizPlaybackSessions = /* @__PURE__ */ new WeakMap();
|
|
@@ -88,8 +45,8 @@ function createMediaImage(className, src, alt) {
|
|
|
88
45
|
img.src = src;
|
|
89
46
|
img.style.maxWidth = "100%";
|
|
90
47
|
img.style.height = "auto";
|
|
91
|
-
img.style.borderRadius = `${RADIUS.sm}
|
|
92
|
-
img.style.marginBottom = `${SPACE[2]}px`;
|
|
48
|
+
img.style.borderRadius = `${import_core.RADIUS.sm}`;
|
|
49
|
+
img.style.marginBottom = `${import_core.SPACE[2]}px`;
|
|
93
50
|
if (alt) img.alt = alt;
|
|
94
51
|
return img;
|
|
95
52
|
}
|
|
@@ -99,9 +56,9 @@ function lockOption(button) {
|
|
|
99
56
|
}
|
|
100
57
|
function revealCorrect(button, glyph, label) {
|
|
101
58
|
button.classList.add("quiz-playback-option-correct");
|
|
102
|
-
button.style.backgroundColor = COLORS.correctBg;
|
|
103
|
-
button.style.borderColor = COLORS.correctBg;
|
|
104
|
-
button.style.color = COLORS.correctText;
|
|
59
|
+
button.style.backgroundColor = import_core.COLORS.correctBg;
|
|
60
|
+
button.style.borderColor = import_core.COLORS.correctBg;
|
|
61
|
+
button.style.color = import_core.COLORS.correctText;
|
|
105
62
|
glyph.textContent = "\u2713";
|
|
106
63
|
label.textContent = "Correct";
|
|
107
64
|
glyph.style.opacity = "1";
|
|
@@ -109,9 +66,9 @@ function revealCorrect(button, glyph, label) {
|
|
|
109
66
|
}
|
|
110
67
|
function revealIncorrect(button, glyph, label) {
|
|
111
68
|
button.classList.add("quiz-playback-option-incorrect");
|
|
112
|
-
button.style.backgroundColor = COLORS.incorrectBg;
|
|
113
|
-
button.style.borderColor = COLORS.incorrectBorder;
|
|
114
|
-
button.style.color = COLORS.incorrectText;
|
|
69
|
+
button.style.backgroundColor = import_core.COLORS.incorrectBg;
|
|
70
|
+
button.style.borderColor = import_core.COLORS.incorrectBorder;
|
|
71
|
+
button.style.color = import_core.COLORS.incorrectText;
|
|
115
72
|
glyph.textContent = "\u2715";
|
|
116
73
|
label.textContent = "Incorrect";
|
|
117
74
|
glyph.style.opacity = "1";
|
|
@@ -125,23 +82,23 @@ function renderQuizPlayback(container, data, callbacks) {
|
|
|
125
82
|
let completed = false;
|
|
126
83
|
const root = document.createElement("div");
|
|
127
84
|
root.className = "quiz-playback";
|
|
128
|
-
root.style.fontFamily = FONTS.body;
|
|
129
|
-
root.style.color = COLORS.text;
|
|
85
|
+
root.style.fontFamily = import_core.FONTS.body;
|
|
86
|
+
root.style.color = import_core.COLORS.text;
|
|
130
87
|
root.style.lineHeight = "1.5";
|
|
131
88
|
const indicator = document.createElement("div");
|
|
132
89
|
indicator.className = "quiz-playback-indicator";
|
|
133
|
-
indicator.style.fontSize = `${TYPE.xs}px`;
|
|
90
|
+
indicator.style.fontSize = `${import_core.TYPE.xs}px`;
|
|
134
91
|
indicator.style.fontWeight = "600";
|
|
135
|
-
indicator.style.color = COLORS.textMuted;
|
|
136
|
-
indicator.style.marginBottom = `${SPACE[2]}px`;
|
|
92
|
+
indicator.style.color = import_core.COLORS.textMuted;
|
|
93
|
+
indicator.style.marginBottom = `${import_core.SPACE[2]}px`;
|
|
137
94
|
indicator.style.textTransform = "uppercase";
|
|
138
95
|
indicator.style.letterSpacing = "0.06em";
|
|
139
96
|
root.appendChild(indicator);
|
|
140
97
|
const question = document.createElement("p");
|
|
141
98
|
question.className = "quiz-playback-question";
|
|
142
|
-
question.style.margin = `0 0 ${SPACE[2]}px 0`;
|
|
143
|
-
question.style.fontFamily = FONTS.display;
|
|
144
|
-
question.style.fontSize = `${TYPE.lg}px`;
|
|
99
|
+
question.style.margin = `0 0 ${import_core.SPACE[2]}px 0`;
|
|
100
|
+
question.style.fontFamily = import_core.FONTS.display;
|
|
101
|
+
question.style.fontSize = `${import_core.TYPE.lg}px`;
|
|
145
102
|
question.style.fontWeight = "600";
|
|
146
103
|
root.appendChild(question);
|
|
147
104
|
const questionMediaSlot = document.createElement("div");
|
|
@@ -150,26 +107,26 @@ function renderQuizPlayback(container, data, callbacks) {
|
|
|
150
107
|
optionsBox.className = "quiz-playback-options";
|
|
151
108
|
optionsBox.style.display = "flex";
|
|
152
109
|
optionsBox.style.flexDirection = "column";
|
|
153
|
-
optionsBox.style.gap = `${SPACE[2]}px`;
|
|
110
|
+
optionsBox.style.gap = `${import_core.SPACE[2]}px`;
|
|
154
111
|
root.appendChild(optionsBox);
|
|
155
112
|
const navFooter = document.createElement("div");
|
|
156
113
|
navFooter.className = "quiz-playback-nav";
|
|
157
114
|
navFooter.style.display = "flex";
|
|
158
115
|
navFooter.style.justifyContent = "flex-end";
|
|
159
|
-
navFooter.style.gap = `${SPACE[2]}px`;
|
|
160
|
-
navFooter.style.marginTop = `${SPACE[4]}px`;
|
|
116
|
+
navFooter.style.gap = `${import_core.SPACE[2]}px`;
|
|
117
|
+
navFooter.style.marginTop = `${import_core.SPACE[4]}px`;
|
|
161
118
|
root.appendChild(navFooter);
|
|
162
119
|
const prevButton = document.createElement("button");
|
|
163
120
|
prevButton.type = "button";
|
|
164
121
|
prevButton.className = "quiz-playback-prev";
|
|
165
122
|
prevButton.textContent = "Previous";
|
|
166
|
-
prevButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
|
|
167
|
-
prevButton.style.backgroundColor = COLORS.surface;
|
|
168
|
-
prevButton.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
169
|
-
prevButton.style.borderRadius = `${RADIUS.md}
|
|
170
|
-
prevButton.style.color = COLORS.text;
|
|
123
|
+
prevButton.style.padding = `${import_core.SPACE[2]}px ${import_core.SPACE[4]}px`;
|
|
124
|
+
prevButton.style.backgroundColor = import_core.COLORS.surface;
|
|
125
|
+
prevButton.style.border = `1px solid ${import_core.COLORS.borderStrong}`;
|
|
126
|
+
prevButton.style.borderRadius = `${import_core.RADIUS.md}`;
|
|
127
|
+
prevButton.style.color = import_core.COLORS.text;
|
|
171
128
|
prevButton.style.cursor = "pointer";
|
|
172
|
-
prevButton.style.fontSize = `${TYPE.base}px`;
|
|
129
|
+
prevButton.style.fontSize = `${import_core.TYPE.base}px`;
|
|
173
130
|
prevButton.style.fontWeight = "600";
|
|
174
131
|
prevButton.style.transition = "background-color 150ms ease";
|
|
175
132
|
prevButton.addEventListener("click", () => {
|
|
@@ -183,13 +140,13 @@ function renderQuizPlayback(container, data, callbacks) {
|
|
|
183
140
|
nextButton.className = "quiz-playback-next";
|
|
184
141
|
nextButton.textContent = "Next";
|
|
185
142
|
nextButton.disabled = true;
|
|
186
|
-
nextButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
|
|
187
|
-
nextButton.style.backgroundColor = COLORS.accent;
|
|
143
|
+
nextButton.style.padding = `${import_core.SPACE[2]}px ${import_core.SPACE[4]}px`;
|
|
144
|
+
nextButton.style.backgroundColor = import_core.COLORS.accent;
|
|
188
145
|
nextButton.style.border = "none";
|
|
189
|
-
nextButton.style.borderRadius = `${RADIUS.md}
|
|
190
|
-
nextButton.style.color = COLORS.
|
|
146
|
+
nextButton.style.borderRadius = `${import_core.RADIUS.md}`;
|
|
147
|
+
nextButton.style.color = import_core.COLORS.accentContrast;
|
|
191
148
|
nextButton.style.cursor = "pointer";
|
|
192
|
-
nextButton.style.fontSize = `${TYPE.base}px`;
|
|
149
|
+
nextButton.style.fontSize = `${import_core.TYPE.base}px`;
|
|
193
150
|
nextButton.style.fontWeight = "600";
|
|
194
151
|
nextButton.style.transition = "background-color 150ms ease, opacity 150ms ease";
|
|
195
152
|
nextButton.addEventListener("click", () => {
|
|
@@ -223,15 +180,15 @@ function renderQuizPlayback(container, data, callbacks) {
|
|
|
223
180
|
button.className = "quiz-playback-option";
|
|
224
181
|
button.style.display = "flex";
|
|
225
182
|
button.style.alignItems = "center";
|
|
226
|
-
button.style.gap = `${SPACE[2]}px`;
|
|
183
|
+
button.style.gap = `${import_core.SPACE[2]}px`;
|
|
227
184
|
button.style.width = "100%";
|
|
228
|
-
button.style.padding = `${SPACE[3]}px ${SPACE[4]}px`;
|
|
229
|
-
button.style.backgroundColor = COLORS.surface;
|
|
230
|
-
button.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
231
|
-
button.style.borderRadius = `${RADIUS.md}
|
|
185
|
+
button.style.padding = `${import_core.SPACE[3]}px ${import_core.SPACE[4]}px`;
|
|
186
|
+
button.style.backgroundColor = import_core.COLORS.surface;
|
|
187
|
+
button.style.border = `1px solid ${import_core.COLORS.borderStrong}`;
|
|
188
|
+
button.style.borderRadius = `${import_core.RADIUS.md}`;
|
|
232
189
|
button.style.cursor = "pointer";
|
|
233
|
-
button.style.fontSize = `${TYPE.base}px`;
|
|
234
|
-
button.style.color = COLORS.text;
|
|
190
|
+
button.style.fontSize = `${import_core.TYPE.base}px`;
|
|
191
|
+
button.style.color = import_core.COLORS.text;
|
|
235
192
|
button.style.textAlign = "left";
|
|
236
193
|
button.style.transition = "background-color 150ms ease, border-color 150ms ease, color 150ms ease";
|
|
237
194
|
const text = document.createElement("span");
|
|
@@ -248,17 +205,17 @@ function renderQuizPlayback(container, data, callbacks) {
|
|
|
248
205
|
const status = document.createElement("span");
|
|
249
206
|
status.style.display = "flex";
|
|
250
207
|
status.style.alignItems = "center";
|
|
251
|
-
status.style.gap = `${SPACE[1]}px`;
|
|
208
|
+
status.style.gap = `${import_core.SPACE[1]}px`;
|
|
252
209
|
status.style.marginLeft = "auto";
|
|
253
210
|
status.style.transition = "opacity 150ms ease";
|
|
254
211
|
const glyph = document.createElement("span");
|
|
255
212
|
glyph.style.fontWeight = "700";
|
|
256
|
-
glyph.style.fontSize = `${TYPE.md}px`;
|
|
213
|
+
glyph.style.fontSize = `${import_core.TYPE.md}px`;
|
|
257
214
|
glyph.style.lineHeight = "1";
|
|
258
215
|
status.appendChild(glyph);
|
|
259
216
|
const label = document.createElement("span");
|
|
260
217
|
label.style.fontWeight = "600";
|
|
261
|
-
label.style.fontSize = `${TYPE.sm}px`;
|
|
218
|
+
label.style.fontSize = `${import_core.TYPE.sm}px`;
|
|
262
219
|
label.style.letterSpacing = "0.04em";
|
|
263
220
|
status.appendChild(label);
|
|
264
221
|
button.appendChild(status);
|
|
@@ -330,8 +287,64 @@ function unmountQuizPlayback(container) {
|
|
|
330
287
|
quizPlaybackSessions.delete(container);
|
|
331
288
|
}
|
|
332
289
|
|
|
290
|
+
// src/validate.ts
|
|
291
|
+
function validateQuizData(data) {
|
|
292
|
+
const errors = [];
|
|
293
|
+
if (data.questions.length === 0) {
|
|
294
|
+
errors.push("at least one question is required");
|
|
295
|
+
return errors;
|
|
296
|
+
}
|
|
297
|
+
data.questions.forEach((question, index) => {
|
|
298
|
+
if (question.text.trim().length === 0) {
|
|
299
|
+
errors.push(`questions[${index}].text must be a non-empty string`);
|
|
300
|
+
}
|
|
301
|
+
if (question.options.length === 0) {
|
|
302
|
+
errors.push(`questions[${index}].at least one option is required`);
|
|
303
|
+
} else {
|
|
304
|
+
question.options.forEach((option, optionIndex) => {
|
|
305
|
+
if (option.text.trim().length === 0) {
|
|
306
|
+
errors.push(
|
|
307
|
+
`questions[${index}].options[${optionIndex}].text must be a non-empty string`
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
if (question.correctOptionId === null) {
|
|
313
|
+
errors.push(
|
|
314
|
+
`questions[${index}].exactly one correct option must be resolvable`
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
return errors;
|
|
319
|
+
}
|
|
320
|
+
function getQuizAuthoringIssues(data) {
|
|
321
|
+
if (data.questions.length === 0) return ["Add at least one question"];
|
|
322
|
+
const issues = [];
|
|
323
|
+
data.questions.forEach((question, index) => {
|
|
324
|
+
const label = `Question ${index + 1}`;
|
|
325
|
+
if (question.text.trim().length === 0) {
|
|
326
|
+
issues.push(`${label} needs text`);
|
|
327
|
+
}
|
|
328
|
+
if (question.options.length < 2) {
|
|
329
|
+
issues.push(`${label} needs at least 2 answers`);
|
|
330
|
+
}
|
|
331
|
+
question.options.forEach((option, optionIndex) => {
|
|
332
|
+
if (option.text.trim().length === 0) {
|
|
333
|
+
issues.push(`${label}, answer ${optionIndex + 1} needs text`);
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
const hasCorrect = question.options.some(
|
|
337
|
+
(option) => option.id === question.correctOptionId
|
|
338
|
+
);
|
|
339
|
+
if (!hasCorrect) issues.push(`${label} needs a correct answer`);
|
|
340
|
+
});
|
|
341
|
+
return issues;
|
|
342
|
+
}
|
|
343
|
+
|
|
333
344
|
// src/quiz-editor.ts
|
|
334
345
|
var quizEditorControllers = /* @__PURE__ */ new WeakMap();
|
|
346
|
+
var editorInstanceCount = 0;
|
|
347
|
+
var MIN_ANSWERS = 2;
|
|
335
348
|
function buildMediaRef(src, alt) {
|
|
336
349
|
if (src.length === 0) return void 0;
|
|
337
350
|
return alt.length > 0 ? { src, alt } : { src };
|
|
@@ -342,9 +355,10 @@ function createPreview(media, className) {
|
|
|
342
355
|
img.className = className;
|
|
343
356
|
img.src = media.src;
|
|
344
357
|
img.style.maxWidth = "100%";
|
|
358
|
+
img.style.maxHeight = "160px";
|
|
345
359
|
img.style.height = "auto";
|
|
346
|
-
img.style.borderRadius = `${RADIUS.sm}
|
|
347
|
-
img.style.marginTop = `${SPACE[1]}px`;
|
|
360
|
+
img.style.borderRadius = `${import_core.RADIUS.sm}`;
|
|
361
|
+
img.style.marginTop = `${import_core.SPACE[1]}px`;
|
|
348
362
|
if (media.alt) img.alt = media.alt;
|
|
349
363
|
return img;
|
|
350
364
|
}
|
|
@@ -372,20 +386,189 @@ function nextQuestionId(questions, index) {
|
|
|
372
386
|
return `q-${candidate}`;
|
|
373
387
|
}
|
|
374
388
|
function emptyQuestion(id) {
|
|
375
|
-
return {
|
|
389
|
+
return {
|
|
390
|
+
id,
|
|
391
|
+
text: "",
|
|
392
|
+
options: [
|
|
393
|
+
{ id: `${id}-a`, text: "" },
|
|
394
|
+
{ id: `${id}-b`, text: "" }
|
|
395
|
+
],
|
|
396
|
+
correctOptionId: `${id}-a`
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
function setShown(el, shown, display = "") {
|
|
400
|
+
el.style.display = shown ? display : "none";
|
|
401
|
+
}
|
|
402
|
+
function setValue(input, value) {
|
|
403
|
+
if (input.value !== value) input.value = value;
|
|
404
|
+
}
|
|
405
|
+
function createButton(text, variant, className) {
|
|
406
|
+
const button = document.createElement("button");
|
|
407
|
+
button.type = "button";
|
|
408
|
+
button.className = `interlace-btn interlace-btn--${variant} ${className}`;
|
|
409
|
+
button.textContent = text;
|
|
410
|
+
return button;
|
|
411
|
+
}
|
|
412
|
+
function createRemoveButton(className) {
|
|
413
|
+
const button = createButton("\xD7", "ghost", `interlace-btn--icon ${className}`);
|
|
414
|
+
button.style.fontSize = `${import_core.TYPE.lg}px`;
|
|
415
|
+
button.style.lineHeight = "1";
|
|
416
|
+
return button;
|
|
417
|
+
}
|
|
418
|
+
function setRemoveAllowed(button, allowed, reason) {
|
|
419
|
+
button.disabled = !allowed;
|
|
420
|
+
if (allowed) button.removeAttribute("title");
|
|
421
|
+
else button.title = reason;
|
|
422
|
+
}
|
|
423
|
+
function createInput(className, id, placeholder) {
|
|
424
|
+
const input = document.createElement("input");
|
|
425
|
+
input.type = "text";
|
|
426
|
+
input.id = id;
|
|
427
|
+
input.className = `interlace-input ${className}`;
|
|
428
|
+
input.style.width = "100%";
|
|
429
|
+
input.style.boxSizing = "border-box";
|
|
430
|
+
if (placeholder) input.placeholder = placeholder;
|
|
431
|
+
return input;
|
|
432
|
+
}
|
|
433
|
+
function createLabel(text, forId) {
|
|
434
|
+
const label = document.createElement("label");
|
|
435
|
+
label.className = "interlace-label";
|
|
436
|
+
label.htmlFor = forId;
|
|
437
|
+
label.textContent = text;
|
|
438
|
+
label.style.fontSize = `${import_core.TYPE.sm}px`;
|
|
439
|
+
label.style.fontWeight = "600";
|
|
440
|
+
return label;
|
|
441
|
+
}
|
|
442
|
+
function createField(labelText, input) {
|
|
443
|
+
const field = document.createElement("div");
|
|
444
|
+
field.className = "interlace-field";
|
|
445
|
+
field.style.display = "flex";
|
|
446
|
+
field.style.flexDirection = "column";
|
|
447
|
+
field.style.gap = `${import_core.SPACE[1]}px`;
|
|
448
|
+
field.append(createLabel(labelText, input.id), input);
|
|
449
|
+
return field;
|
|
450
|
+
}
|
|
451
|
+
function createErrorText(id, className) {
|
|
452
|
+
const error = document.createElement("p");
|
|
453
|
+
error.id = id;
|
|
454
|
+
error.className = `interlace-hint quiz-field-error ${className}`;
|
|
455
|
+
error.style.margin = "0";
|
|
456
|
+
error.style.fontSize = `${import_core.TYPE.sm}px`;
|
|
457
|
+
error.style.color = import_core.COLORS.errorText;
|
|
458
|
+
setShown(error, false);
|
|
459
|
+
return error;
|
|
460
|
+
}
|
|
461
|
+
function setFieldError(input, error, message) {
|
|
462
|
+
if (message) {
|
|
463
|
+
error.textContent = message;
|
|
464
|
+
setShown(error, true);
|
|
465
|
+
input.setAttribute("aria-invalid", "true");
|
|
466
|
+
input.setAttribute("aria-describedby", error.id);
|
|
467
|
+
} else {
|
|
468
|
+
error.textContent = "";
|
|
469
|
+
setShown(error, false);
|
|
470
|
+
input.removeAttribute("aria-invalid");
|
|
471
|
+
input.removeAttribute("aria-describedby");
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
function createMediaEditor(options) {
|
|
475
|
+
const { classPrefix, idPrefix } = options;
|
|
476
|
+
const root = document.createElement("div");
|
|
477
|
+
root.className = classPrefix;
|
|
478
|
+
const addButton = createButton("+ Add image", "ghost", `${classPrefix}-add`);
|
|
479
|
+
addButton.setAttribute("aria-label", options.addLabel);
|
|
480
|
+
addButton.setAttribute("aria-controls", `${idPrefix}-panel`);
|
|
481
|
+
addButton.style.paddingLeft = `${import_core.SPACE[2]}px`;
|
|
482
|
+
addButton.style.paddingRight = `${import_core.SPACE[2]}px`;
|
|
483
|
+
const panel = document.createElement("div");
|
|
484
|
+
panel.id = `${idPrefix}-panel`;
|
|
485
|
+
panel.className = `${classPrefix}-panel`;
|
|
486
|
+
panel.style.flexDirection = "column";
|
|
487
|
+
panel.style.gap = `${import_core.SPACE[2]}px`;
|
|
488
|
+
panel.style.padding = `${import_core.SPACE[3]}px`;
|
|
489
|
+
panel.style.backgroundColor = import_core.COLORS.surfaceRaised;
|
|
490
|
+
panel.style.border = `1px solid ${import_core.COLORS.border}`;
|
|
491
|
+
panel.style.borderRadius = `${import_core.RADIUS.md}`;
|
|
492
|
+
const srcInput = createInput(
|
|
493
|
+
`${classPrefix}-src`,
|
|
494
|
+
`${idPrefix}-src`,
|
|
495
|
+
"https://example.com/image.png"
|
|
496
|
+
);
|
|
497
|
+
const altInput = createInput(
|
|
498
|
+
`${classPrefix}-alt`,
|
|
499
|
+
`${idPrefix}-alt`,
|
|
500
|
+
"Describe the image for screen readers"
|
|
501
|
+
);
|
|
502
|
+
const previewSlot = document.createElement("div");
|
|
503
|
+
previewSlot.className = options.previewSlotClass;
|
|
504
|
+
const removeButton = createButton(
|
|
505
|
+
"Remove image",
|
|
506
|
+
"ghost",
|
|
507
|
+
`${classPrefix}-remove`
|
|
508
|
+
);
|
|
509
|
+
removeButton.style.alignSelf = "flex-start";
|
|
510
|
+
panel.append(
|
|
511
|
+
createField("Image URL", srcInput),
|
|
512
|
+
createField("Image description (alt text)", altInput),
|
|
513
|
+
previewSlot,
|
|
514
|
+
removeButton
|
|
515
|
+
);
|
|
516
|
+
root.append(addButton, panel);
|
|
517
|
+
const setExpanded = (expanded) => {
|
|
518
|
+
setShown(panel, expanded, "flex");
|
|
519
|
+
setShown(addButton, !expanded);
|
|
520
|
+
addButton.setAttribute("aria-expanded", String(expanded));
|
|
521
|
+
};
|
|
522
|
+
setExpanded(false);
|
|
523
|
+
const handleEdit = () => {
|
|
524
|
+
const media = buildMediaRef(srcInput.value, altInput.value);
|
|
525
|
+
syncPreview(previewSlot, media, options.previewClass);
|
|
526
|
+
options.onEdit(media);
|
|
527
|
+
};
|
|
528
|
+
srcInput.addEventListener("input", handleEdit);
|
|
529
|
+
altInput.addEventListener("input", handleEdit);
|
|
530
|
+
addButton.addEventListener("click", () => {
|
|
531
|
+
setExpanded(true);
|
|
532
|
+
options.onExpand();
|
|
533
|
+
srcInput.focus();
|
|
534
|
+
});
|
|
535
|
+
removeButton.addEventListener("click", () => {
|
|
536
|
+
srcInput.value = "";
|
|
537
|
+
altInput.value = "";
|
|
538
|
+
syncPreview(previewSlot, void 0, options.previewClass);
|
|
539
|
+
setExpanded(false);
|
|
540
|
+
options.onRemove();
|
|
541
|
+
addButton.focus();
|
|
542
|
+
});
|
|
543
|
+
const sync = (media, expanded) => {
|
|
544
|
+
setValue(srcInput, media?.src ?? "");
|
|
545
|
+
setValue(altInput, media?.alt ?? "");
|
|
546
|
+
syncPreview(previewSlot, media, options.previewClass);
|
|
547
|
+
setExpanded(expanded);
|
|
548
|
+
};
|
|
549
|
+
return { root, srcInput, altInput, previewSlot, sync };
|
|
376
550
|
}
|
|
377
551
|
function createQuizEditorController(container, data, onChange) {
|
|
552
|
+
(0, import_core2.ensureInterlaceStyles)(container.ownerDocument);
|
|
553
|
+
editorInstanceCount += 1;
|
|
554
|
+
const uid = `quiz-editor-${editorInstanceCount}`;
|
|
378
555
|
container.innerHTML = "";
|
|
379
556
|
const root = document.createElement("div");
|
|
380
557
|
root.className = "quiz-editor";
|
|
381
|
-
root.style.fontFamily = FONTS.body;
|
|
382
|
-
root.style.color = COLORS.text;
|
|
558
|
+
root.style.fontFamily = import_core.FONTS.body;
|
|
559
|
+
root.style.color = import_core.COLORS.text;
|
|
383
560
|
root.style.lineHeight = "1.5";
|
|
561
|
+
root.style.display = "flex";
|
|
562
|
+
root.style.flexDirection = "column";
|
|
563
|
+
root.style.gap = `${import_core.SPACE[3]}px`;
|
|
384
564
|
let currentData = data;
|
|
385
565
|
let currentOnChange = onChange;
|
|
386
566
|
let selectedIndex = 0;
|
|
387
567
|
let bodyQuestionIndex = -1;
|
|
388
568
|
let optionRows = [];
|
|
569
|
+
let tabButtons = [];
|
|
570
|
+
const touched = /* @__PURE__ */ new Set();
|
|
571
|
+
const expandedMedia = /* @__PURE__ */ new Set();
|
|
389
572
|
const emit = (questions) => {
|
|
390
573
|
currentOnChange({ questions });
|
|
391
574
|
};
|
|
@@ -396,35 +579,41 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
396
579
|
questions[selectedIndex] = mutate(active);
|
|
397
580
|
emit(questions);
|
|
398
581
|
};
|
|
582
|
+
const activeKey = (optionIndex) => {
|
|
583
|
+
const active = currentData.questions[selectedIndex];
|
|
584
|
+
const questionKey = active?.id ?? `#${selectedIndex}`;
|
|
585
|
+
if (optionIndex === void 0) return questionKey;
|
|
586
|
+
return `${questionKey}/${active?.options[optionIndex]?.id ?? `#${optionIndex}`}`;
|
|
587
|
+
};
|
|
399
588
|
const questionsHeader = document.createElement("div");
|
|
589
|
+
questionsHeader.id = `${uid}-questions-label`;
|
|
590
|
+
questionsHeader.className = "interlace-label";
|
|
400
591
|
questionsHeader.textContent = "Questions";
|
|
401
|
-
questionsHeader.style.
|
|
592
|
+
questionsHeader.style.fontSize = `${import_core.TYPE.sm}px`;
|
|
402
593
|
questionsHeader.style.fontWeight = "600";
|
|
403
|
-
questionsHeader.style.
|
|
404
|
-
|
|
405
|
-
|
|
594
|
+
questionsHeader.style.color = import_core.COLORS.textMuted;
|
|
595
|
+
const tabsRow = document.createElement("div");
|
|
596
|
+
tabsRow.style.display = "flex";
|
|
597
|
+
tabsRow.style.flexWrap = "wrap";
|
|
598
|
+
tabsRow.style.alignItems = "center";
|
|
599
|
+
tabsRow.style.gap = `${import_core.SPACE[2]}px`;
|
|
406
600
|
const tabsBar = document.createElement("div");
|
|
407
601
|
tabsBar.className = "quiz-question-tabs";
|
|
602
|
+
tabsBar.setAttribute("role", "tablist");
|
|
603
|
+
tabsBar.setAttribute("aria-labelledby", questionsHeader.id);
|
|
408
604
|
tabsBar.style.display = "flex";
|
|
409
605
|
tabsBar.style.flexWrap = "wrap";
|
|
410
|
-
tabsBar.style.gap = `${SPACE[2]}px`;
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
addQuestionButton.textContent = "Add question";
|
|
417
|
-
addQuestionButton.style.marginBottom = `${SPACE[4]}px`;
|
|
418
|
-
addQuestionButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
|
|
419
|
-
addQuestionButton.style.backgroundColor = COLORS.surface;
|
|
420
|
-
addQuestionButton.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
421
|
-
addQuestionButton.style.borderRadius = `${RADIUS.sm}px`;
|
|
422
|
-
addQuestionButton.style.cursor = "pointer";
|
|
423
|
-
addQuestionButton.style.fontSize = `${TYPE.sm}px`;
|
|
424
|
-
addQuestionButton.style.fontWeight = "600";
|
|
425
|
-
addQuestionButton.style.color = COLORS.text;
|
|
606
|
+
tabsBar.style.gap = `${import_core.SPACE[2]}px`;
|
|
607
|
+
const addQuestionButton = createButton(
|
|
608
|
+
"+ Add question",
|
|
609
|
+
"secondary",
|
|
610
|
+
"quiz-add-question"
|
|
611
|
+
);
|
|
426
612
|
addQuestionButton.addEventListener("click", () => {
|
|
427
|
-
const id = nextQuestionId(
|
|
613
|
+
const id = nextQuestionId(
|
|
614
|
+
currentData.questions,
|
|
615
|
+
currentData.questions.length
|
|
616
|
+
);
|
|
428
617
|
const questions = [...currentData.questions, emptyQuestion(id)];
|
|
429
618
|
currentData = { questions };
|
|
430
619
|
selectedIndex = questions.length - 1;
|
|
@@ -433,107 +622,118 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
433
622
|
renderTabs();
|
|
434
623
|
syncActiveBody();
|
|
435
624
|
});
|
|
436
|
-
|
|
625
|
+
tabsRow.append(tabsBar, addQuestionButton);
|
|
626
|
+
root.append(questionsHeader, tabsRow);
|
|
437
627
|
const body = document.createElement("div");
|
|
628
|
+
body.id = `${uid}-panel`;
|
|
438
629
|
body.className = "quiz-editor-body";
|
|
630
|
+
body.setAttribute("role", "tabpanel");
|
|
631
|
+
body.style.display = "flex";
|
|
632
|
+
body.style.flexDirection = "column";
|
|
633
|
+
body.style.gap = `${import_core.SPACE[4]}px`;
|
|
634
|
+
body.style.padding = `${import_core.SPACE[4]}px`;
|
|
635
|
+
body.style.backgroundColor = import_core.COLORS.surface;
|
|
636
|
+
body.style.border = `1px solid ${import_core.COLORS.border}`;
|
|
637
|
+
body.style.borderRadius = `${import_core.RADIUS.md}`;
|
|
439
638
|
root.appendChild(body);
|
|
440
|
-
const questionInput =
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
questionInput.style.
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
639
|
+
const questionInput = createInput(
|
|
640
|
+
"quiz-question",
|
|
641
|
+
`${uid}-question`,
|
|
642
|
+
"Type your question"
|
|
643
|
+
);
|
|
644
|
+
questionInput.style.fontSize = `${import_core.TYPE.md}px`;
|
|
645
|
+
const questionError = createErrorText(
|
|
646
|
+
`${uid}-question-error`,
|
|
647
|
+
"quiz-question-error"
|
|
648
|
+
);
|
|
649
|
+
const refreshQuestionError = () => {
|
|
650
|
+
const show = touched.has(`text:${activeKey()}`) && questionInput.value.trim().length === 0;
|
|
651
|
+
setFieldError(
|
|
652
|
+
questionInput,
|
|
653
|
+
questionError,
|
|
654
|
+
show ? "Add the question text" : null
|
|
655
|
+
);
|
|
656
|
+
};
|
|
452
657
|
questionInput.addEventListener("input", () => {
|
|
453
658
|
patchActiveQuestion((question) => ({
|
|
454
659
|
...question,
|
|
455
660
|
text: questionInput.value
|
|
456
661
|
}));
|
|
662
|
+
refreshQuestionError();
|
|
457
663
|
});
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
const
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
optionsTitle.style.color = COLORS.textMuted;
|
|
518
|
-
body.appendChild(optionsTitle);
|
|
664
|
+
questionInput.addEventListener("blur", () => {
|
|
665
|
+
touched.add(`text:${activeKey()}`);
|
|
666
|
+
refreshQuestionError();
|
|
667
|
+
});
|
|
668
|
+
const removeQuestionButton = createRemoveButton("quiz-question-remove");
|
|
669
|
+
removeQuestionButton.addEventListener(
|
|
670
|
+
"click",
|
|
671
|
+
() => removeQuestion(selectedIndex)
|
|
672
|
+
);
|
|
673
|
+
const questionHeader = document.createElement("div");
|
|
674
|
+
questionHeader.style.display = "flex";
|
|
675
|
+
questionHeader.style.alignItems = "center";
|
|
676
|
+
questionHeader.style.justifyContent = "space-between";
|
|
677
|
+
questionHeader.style.gap = `${import_core.SPACE[2]}px`;
|
|
678
|
+
questionHeader.append(
|
|
679
|
+
createLabel("Question", questionInput.id),
|
|
680
|
+
removeQuestionButton
|
|
681
|
+
);
|
|
682
|
+
const questionField = document.createElement("div");
|
|
683
|
+
questionField.className = "interlace-field";
|
|
684
|
+
questionField.style.display = "flex";
|
|
685
|
+
questionField.style.flexDirection = "column";
|
|
686
|
+
questionField.style.gap = `${import_core.SPACE[1]}px`;
|
|
687
|
+
questionField.append(questionHeader, questionInput, questionError);
|
|
688
|
+
const questionMedia = createMediaEditor({
|
|
689
|
+
classPrefix: "quiz-question-media",
|
|
690
|
+
idPrefix: `${uid}-question-media`,
|
|
691
|
+
addLabel: "Add image to question",
|
|
692
|
+
previewSlotClass: "quiz-question-media-preview-slot",
|
|
693
|
+
previewClass: "quiz-question-media-preview",
|
|
694
|
+
onEdit: (media) => {
|
|
695
|
+
expandedMedia.add(activeKey());
|
|
696
|
+
patchActiveQuestion((question) => ({ ...question, media }));
|
|
697
|
+
},
|
|
698
|
+
onExpand: () => expandedMedia.add(activeKey()),
|
|
699
|
+
onRemove: () => {
|
|
700
|
+
expandedMedia.delete(activeKey());
|
|
701
|
+
patchActiveQuestion((question) => ({ ...question, media: void 0 }));
|
|
702
|
+
}
|
|
703
|
+
});
|
|
704
|
+
questionField.appendChild(questionMedia.root);
|
|
705
|
+
body.appendChild(questionField);
|
|
706
|
+
const answersSection = document.createElement("div");
|
|
707
|
+
answersSection.style.display = "flex";
|
|
708
|
+
answersSection.style.flexDirection = "column";
|
|
709
|
+
answersSection.style.gap = `${import_core.SPACE[2]}px`;
|
|
710
|
+
const answersHeading = document.createElement("div");
|
|
711
|
+
answersHeading.id = `${uid}-answers-label`;
|
|
712
|
+
answersHeading.className = "interlace-label quiz-answers-heading";
|
|
713
|
+
answersHeading.textContent = "Answers";
|
|
714
|
+
answersHeading.style.fontSize = `${import_core.TYPE.sm}px`;
|
|
715
|
+
answersHeading.style.fontWeight = "600";
|
|
716
|
+
const answersHint = document.createElement("p");
|
|
717
|
+
answersHint.id = `${uid}-answers-hint`;
|
|
718
|
+
answersHint.className = "interlace-hint quiz-answers-hint";
|
|
719
|
+
answersHint.textContent = "Select the correct answer.";
|
|
720
|
+
answersHint.style.margin = "0";
|
|
721
|
+
answersHint.style.fontSize = `${import_core.TYPE.sm}px`;
|
|
722
|
+
answersHint.style.color = import_core.COLORS.textMuted;
|
|
519
723
|
const optionsContainer = document.createElement("div");
|
|
724
|
+
optionsContainer.className = "quiz-options";
|
|
725
|
+
optionsContainer.setAttribute("role", "radiogroup");
|
|
726
|
+
optionsContainer.setAttribute("aria-labelledby", answersHeading.id);
|
|
727
|
+
optionsContainer.setAttribute("aria-describedby", answersHint.id);
|
|
520
728
|
optionsContainer.style.display = "flex";
|
|
521
729
|
optionsContainer.style.flexDirection = "column";
|
|
522
|
-
optionsContainer.style.gap = `${SPACE[2]}px`;
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
addOptionButton.style.
|
|
529
|
-
addOptionButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
|
|
530
|
-
addOptionButton.style.backgroundColor = COLORS.surface;
|
|
531
|
-
addOptionButton.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
532
|
-
addOptionButton.style.borderRadius = `${RADIUS.sm}px`;
|
|
533
|
-
addOptionButton.style.cursor = "pointer";
|
|
534
|
-
addOptionButton.style.fontSize = `${TYPE.sm}px`;
|
|
535
|
-
addOptionButton.style.fontWeight = "600";
|
|
536
|
-
addOptionButton.style.color = COLORS.text;
|
|
730
|
+
optionsContainer.style.gap = `${import_core.SPACE[2]}px`;
|
|
731
|
+
const addOptionButton = createButton(
|
|
732
|
+
"+ Add answer",
|
|
733
|
+
"secondary",
|
|
734
|
+
"quiz-add-option"
|
|
735
|
+
);
|
|
736
|
+
addOptionButton.style.alignSelf = "flex-start";
|
|
537
737
|
addOptionButton.addEventListener("click", () => {
|
|
538
738
|
patchActiveQuestion((question) => {
|
|
539
739
|
const nextOptions = [
|
|
@@ -551,57 +751,67 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
551
751
|
};
|
|
552
752
|
});
|
|
553
753
|
});
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
correctLabel.appendChild(document.createTextNode("Correct answer"));
|
|
562
|
-
const select = document.createElement("select");
|
|
563
|
-
select.className = "quiz-correct-answer";
|
|
564
|
-
select.style.display = "block";
|
|
565
|
-
select.style.width = "100%";
|
|
566
|
-
select.style.marginTop = `${SPACE[1]}px`;
|
|
567
|
-
select.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
|
|
568
|
-
select.style.fontSize = `${TYPE.base}px`;
|
|
569
|
-
select.style.color = COLORS.text;
|
|
570
|
-
select.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
571
|
-
select.style.borderRadius = `${RADIUS.sm}px`;
|
|
572
|
-
select.style.backgroundColor = COLORS.surface;
|
|
573
|
-
select.addEventListener("change", () => {
|
|
574
|
-
patchActiveQuestion((question) => ({
|
|
575
|
-
...question,
|
|
576
|
-
correctOptionId: select.value.length > 0 ? select.value : null
|
|
577
|
-
}));
|
|
578
|
-
});
|
|
579
|
-
correctLabel.appendChild(select);
|
|
580
|
-
body.appendChild(correctLabel);
|
|
754
|
+
answersSection.append(
|
|
755
|
+
answersHeading,
|
|
756
|
+
answersHint,
|
|
757
|
+
optionsContainer,
|
|
758
|
+
addOptionButton
|
|
759
|
+
);
|
|
760
|
+
body.appendChild(answersSection);
|
|
581
761
|
container.appendChild(root);
|
|
582
|
-
const createOptionRow = (option, optionIndex) => {
|
|
762
|
+
const createOptionRow = (option, optionIndex, optionCount) => {
|
|
763
|
+
const number = optionIndex + 1;
|
|
764
|
+
const idPrefix = `${uid}-q${selectedIndex}-opt${optionIndex}`;
|
|
583
765
|
const row = document.createElement("div");
|
|
584
|
-
row.className = "quiz-option-row";
|
|
585
|
-
row.style.display = "
|
|
586
|
-
row.style.
|
|
587
|
-
row.style.gap = `${SPACE[2]}px`;
|
|
766
|
+
row.className = "quiz-option-row interlace-choice";
|
|
767
|
+
row.style.display = "grid";
|
|
768
|
+
row.style.gridTemplateColumns = "auto minmax(0, 1fr) auto";
|
|
588
769
|
row.style.alignItems = "center";
|
|
589
|
-
row.style.
|
|
590
|
-
row.style.
|
|
591
|
-
row.style.
|
|
592
|
-
row.style.
|
|
593
|
-
const
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
770
|
+
row.style.columnGap = `${import_core.SPACE[2]}px`;
|
|
771
|
+
row.style.rowGap = `${import_core.SPACE[1]}px`;
|
|
772
|
+
row.style.cursor = "default";
|
|
773
|
+
row.style.outline = "none";
|
|
774
|
+
const radio = document.createElement("input");
|
|
775
|
+
radio.type = "radio";
|
|
776
|
+
radio.name = `${uid}-correct-${selectedIndex}`;
|
|
777
|
+
radio.className = "quiz-option-correct";
|
|
778
|
+
radio.value = option.id;
|
|
779
|
+
radio.checked = option.id === currentData.questions[selectedIndex]?.correctOptionId;
|
|
780
|
+
radio.setAttribute("aria-label", `Mark answer ${number} as correct`);
|
|
781
|
+
radio.style.width = "18px";
|
|
782
|
+
radio.style.height = "18px";
|
|
783
|
+
radio.style.margin = "0";
|
|
784
|
+
radio.style.accentColor = import_core.COLORS.accent;
|
|
785
|
+
radio.style.cursor = "pointer";
|
|
786
|
+
radio.style.gridColumn = "1";
|
|
787
|
+
radio.style.gridRow = "2";
|
|
788
|
+
radio.addEventListener("change", () => {
|
|
789
|
+
if (!radio.checked) return;
|
|
790
|
+
patchActiveQuestion((question) => ({
|
|
791
|
+
...question,
|
|
792
|
+
correctOptionId: question.options[optionIndex]?.id ?? null
|
|
793
|
+
}));
|
|
794
|
+
});
|
|
795
|
+
const textInput = createInput(
|
|
796
|
+
"quiz-option-text",
|
|
797
|
+
`${idPrefix}-text`,
|
|
798
|
+
"Answer text"
|
|
799
|
+
);
|
|
604
800
|
textInput.value = option.text;
|
|
801
|
+
textInput.style.gridColumn = "2";
|
|
802
|
+
textInput.style.gridRow = "2";
|
|
803
|
+
const label = createLabel(`Answer ${number}`, textInput.id);
|
|
804
|
+
label.style.gridColumn = "2";
|
|
805
|
+
label.style.gridRow = "1";
|
|
806
|
+
const error = createErrorText(`${idPrefix}-error`, "quiz-option-error");
|
|
807
|
+
const refreshError = () => {
|
|
808
|
+
const show = touched.has(`opt:${activeKey(optionIndex)}`) && textInput.value.trim().length === 0;
|
|
809
|
+
setFieldError(
|
|
810
|
+
textInput,
|
|
811
|
+
error,
|
|
812
|
+
show ? `Answer ${number} needs text` : null
|
|
813
|
+
);
|
|
814
|
+
};
|
|
605
815
|
textInput.addEventListener("input", () => {
|
|
606
816
|
patchActiveQuestion((question) => {
|
|
607
817
|
const nextOptions = question.options.slice();
|
|
@@ -611,63 +821,59 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
611
821
|
};
|
|
612
822
|
return { ...question, options: nextOptions };
|
|
613
823
|
});
|
|
824
|
+
refreshError();
|
|
825
|
+
});
|
|
826
|
+
textInput.addEventListener("blur", () => {
|
|
827
|
+
touched.add(`opt:${activeKey(optionIndex)}`);
|
|
828
|
+
refreshError();
|
|
614
829
|
});
|
|
615
|
-
const
|
|
616
|
-
mediaSrcInput.type = "text";
|
|
617
|
-
mediaSrcInput.className = "quiz-option-media-src";
|
|
618
|
-
mediaSrcInput.placeholder = "Option media URL";
|
|
619
|
-
mediaSrcInput.value = option.media?.src ?? "";
|
|
620
|
-
mediaSrcInput.style.flex = "1";
|
|
621
|
-
mediaSrcInput.style.minWidth = "120px";
|
|
622
|
-
mediaSrcInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
|
|
623
|
-
mediaSrcInput.style.fontSize = `${TYPE.base}px`;
|
|
624
|
-
mediaSrcInput.style.color = COLORS.text;
|
|
625
|
-
mediaSrcInput.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
626
|
-
mediaSrcInput.style.borderRadius = `${RADIUS.sm}px`;
|
|
627
|
-
mediaSrcInput.style.backgroundColor = COLORS.surface;
|
|
628
|
-
const mediaAltInput = document.createElement("input");
|
|
629
|
-
mediaAltInput.type = "text";
|
|
630
|
-
mediaAltInput.className = "quiz-option-media-alt";
|
|
631
|
-
mediaAltInput.placeholder = "Option media alt";
|
|
632
|
-
mediaAltInput.value = option.media?.alt ?? "";
|
|
633
|
-
mediaAltInput.style.flex = "1";
|
|
634
|
-
mediaAltInput.style.minWidth = "120px";
|
|
635
|
-
mediaAltInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
|
|
636
|
-
mediaAltInput.style.fontSize = `${TYPE.base}px`;
|
|
637
|
-
mediaAltInput.style.color = COLORS.text;
|
|
638
|
-
mediaAltInput.style.border = `1px solid ${COLORS.borderStrong}`;
|
|
639
|
-
mediaAltInput.style.borderRadius = `${RADIUS.sm}px`;
|
|
640
|
-
mediaAltInput.style.backgroundColor = COLORS.surface;
|
|
641
|
-
const previewSlot = document.createElement("div");
|
|
642
|
-
previewSlot.className = "quiz-option-media-preview";
|
|
643
|
-
syncPreview(previewSlot, option.media, "quiz-option-thumb");
|
|
644
|
-
const emitOptionMedia = () => {
|
|
645
|
-
const media = buildMediaRef(mediaSrcInput.value, mediaAltInput.value);
|
|
646
|
-
syncPreview(previewSlot, media, "quiz-option-thumb");
|
|
830
|
+
const patchOptionMedia = (media2) => {
|
|
647
831
|
patchActiveQuestion((question) => {
|
|
648
832
|
const nextOptions = question.options.slice();
|
|
649
|
-
nextOptions[optionIndex] = { ...nextOptions[optionIndex], media };
|
|
833
|
+
nextOptions[optionIndex] = { ...nextOptions[optionIndex], media: media2 };
|
|
650
834
|
return { ...question, options: nextOptions };
|
|
651
835
|
});
|
|
652
836
|
};
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
837
|
+
const media = createMediaEditor({
|
|
838
|
+
classPrefix: "quiz-option-media",
|
|
839
|
+
idPrefix: `${idPrefix}-media`,
|
|
840
|
+
addLabel: `Add image to answer ${number}`,
|
|
841
|
+
previewSlotClass: "quiz-option-media-preview",
|
|
842
|
+
previewClass: "quiz-option-thumb",
|
|
843
|
+
onEdit: (next) => {
|
|
844
|
+
expandedMedia.add(activeKey(optionIndex));
|
|
845
|
+
patchOptionMedia(next);
|
|
846
|
+
},
|
|
847
|
+
onExpand: () => expandedMedia.add(activeKey(optionIndex)),
|
|
848
|
+
onRemove: () => {
|
|
849
|
+
expandedMedia.delete(activeKey(optionIndex));
|
|
850
|
+
patchOptionMedia(void 0);
|
|
851
|
+
}
|
|
852
|
+
});
|
|
853
|
+
const details = document.createElement("div");
|
|
854
|
+
details.style.gridColumn = "2";
|
|
855
|
+
details.style.gridRow = "3";
|
|
856
|
+
details.style.display = "flex";
|
|
857
|
+
details.style.flexDirection = "column";
|
|
858
|
+
details.style.alignItems = "flex-start";
|
|
859
|
+
details.style.gap = `${import_core.SPACE[1]}px`;
|
|
860
|
+
media.root.style.alignSelf = "stretch";
|
|
861
|
+
details.append(error, media.root);
|
|
862
|
+
const remove = createRemoveButton("quiz-option-remove");
|
|
863
|
+
remove.setAttribute("aria-label", `Remove answer ${number}`);
|
|
864
|
+
remove.style.gridColumn = "3";
|
|
865
|
+
remove.style.gridRow = "2";
|
|
866
|
+
setRemoveAllowed(
|
|
867
|
+
remove,
|
|
868
|
+
optionCount > MIN_ANSWERS,
|
|
869
|
+
`A question needs at least ${MIN_ANSWERS} answers`
|
|
870
|
+
);
|
|
667
871
|
remove.addEventListener("click", () => {
|
|
668
872
|
patchActiveQuestion((question) => {
|
|
669
873
|
const removed = question.options[optionIndex];
|
|
670
|
-
const nextOptions = question.options.filter(
|
|
874
|
+
const nextOptions = question.options.filter(
|
|
875
|
+
(_, i) => i !== optionIndex
|
|
876
|
+
);
|
|
671
877
|
return {
|
|
672
878
|
...question,
|
|
673
879
|
options: nextOptions,
|
|
@@ -676,9 +882,9 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
676
882
|
};
|
|
677
883
|
});
|
|
678
884
|
});
|
|
679
|
-
row.append(
|
|
885
|
+
row.append(label, radio, textInput, remove, details);
|
|
680
886
|
optionsContainer.appendChild(row);
|
|
681
|
-
return { row,
|
|
887
|
+
return { row, radio, textInput, error, media };
|
|
682
888
|
};
|
|
683
889
|
const selectQuestion = (index) => {
|
|
684
890
|
selectedIndex = index;
|
|
@@ -702,85 +908,79 @@ function createQuizEditorController(container, data, onChange) {
|
|
|
702
908
|
};
|
|
703
909
|
const renderTabs = () => {
|
|
704
910
|
tabsBar.innerHTML = "";
|
|
705
|
-
currentData.questions.
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
tab.
|
|
714
|
-
tab.
|
|
715
|
-
tab.
|
|
716
|
-
tab.
|
|
717
|
-
tab.
|
|
718
|
-
tab.style.cursor = "pointer";
|
|
719
|
-
tab.style.fontSize = `${TYPE.sm}px`;
|
|
720
|
-
tab.style.fontWeight = "600";
|
|
721
|
-
if (index === selectedIndex) {
|
|
722
|
-
tab.style.backgroundColor = COLORS.accent;
|
|
723
|
-
tab.style.borderColor = COLORS.accent;
|
|
724
|
-
tab.style.color = COLORS.white;
|
|
725
|
-
} else {
|
|
726
|
-
tab.style.backgroundColor = COLORS.surface;
|
|
727
|
-
tab.style.color = COLORS.text;
|
|
728
|
-
}
|
|
911
|
+
const count = currentData.questions.length;
|
|
912
|
+
tabButtons = currentData.questions.map((_, index) => {
|
|
913
|
+
const selected = index === selectedIndex;
|
|
914
|
+
const tab = createButton(
|
|
915
|
+
`Question ${index + 1}`,
|
|
916
|
+
selected ? "primary" : "secondary",
|
|
917
|
+
"quiz-question-tab"
|
|
918
|
+
);
|
|
919
|
+
tab.id = `${uid}-tab-${index}`;
|
|
920
|
+
tab.setAttribute("role", "tab");
|
|
921
|
+
tab.setAttribute("aria-selected", String(selected));
|
|
922
|
+
tab.setAttribute("aria-controls", body.id);
|
|
923
|
+
tab.tabIndex = selected ? 0 : -1;
|
|
729
924
|
tab.addEventListener("click", () => selectQuestion(index));
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
tabsBar.appendChild(wrap);
|
|
925
|
+
tab.addEventListener("keydown", (event) => {
|
|
926
|
+
let target = -1;
|
|
927
|
+
if (event.key === "ArrowRight") target = (index + 1) % count;
|
|
928
|
+
else if (event.key === "ArrowLeft")
|
|
929
|
+
target = (index - 1 + count) % count;
|
|
930
|
+
else if (event.key === "Home") target = 0;
|
|
931
|
+
else if (event.key === "End") target = count - 1;
|
|
932
|
+
if (target < 0) return;
|
|
933
|
+
event.preventDefault();
|
|
934
|
+
selectQuestion(target);
|
|
935
|
+
tabButtons[target]?.focus();
|
|
936
|
+
});
|
|
937
|
+
tabsBar.appendChild(tab);
|
|
938
|
+
return tab;
|
|
745
939
|
});
|
|
940
|
+
body.setAttribute("aria-labelledby", `${uid}-tab-${selectedIndex}`);
|
|
941
|
+
removeQuestionButton.setAttribute(
|
|
942
|
+
"aria-label",
|
|
943
|
+
`Remove question ${selectedIndex + 1}`
|
|
944
|
+
);
|
|
945
|
+
setRemoveAllowed(
|
|
946
|
+
removeQuestionButton,
|
|
947
|
+
count > 1,
|
|
948
|
+
"A quiz needs at least one question"
|
|
949
|
+
);
|
|
746
950
|
};
|
|
747
951
|
const syncActiveBody = () => {
|
|
748
952
|
const active = currentData.questions[selectedIndex];
|
|
749
953
|
if (!active) return;
|
|
750
|
-
questionInput
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
954
|
+
setValue(questionInput, active.text);
|
|
955
|
+
questionMedia.sync(
|
|
956
|
+
active.media,
|
|
957
|
+
!!active.media || expandedMedia.has(activeKey())
|
|
958
|
+
);
|
|
959
|
+
refreshQuestionError();
|
|
754
960
|
const options = active.options;
|
|
755
961
|
if (bodyQuestionIndex !== selectedIndex || optionRows.length !== options.length) {
|
|
756
962
|
optionsContainer.innerHTML = "";
|
|
757
963
|
optionRows = options.map(
|
|
758
|
-
(option, optionIndex) => createOptionRow(option, optionIndex)
|
|
964
|
+
(option, optionIndex) => createOptionRow(option, optionIndex, options.length)
|
|
759
965
|
);
|
|
760
966
|
bodyQuestionIndex = selectedIndex;
|
|
761
|
-
} else {
|
|
762
|
-
optionRows.forEach((row, optionIndex) => {
|
|
763
|
-
const option = options[optionIndex];
|
|
764
|
-
row.textInput.value = option.text;
|
|
765
|
-
row.mediaSrcInput.value = option.media?.src ?? "";
|
|
766
|
-
row.mediaAltInput.value = option.media?.alt ?? "";
|
|
767
|
-
syncPreview(row.previewSlot, option.media, "quiz-option-thumb");
|
|
768
|
-
});
|
|
769
|
-
}
|
|
770
|
-
while (select.options.length < options.length) {
|
|
771
|
-
select.add(document.createElement("option"));
|
|
772
|
-
}
|
|
773
|
-
while (select.options.length > options.length) {
|
|
774
|
-
select.remove(select.options.length - 1);
|
|
775
|
-
}
|
|
776
|
-
for (let i = 0; i < options.length; i++) {
|
|
777
|
-
const el = select.options[i];
|
|
778
|
-
if (!el) continue;
|
|
779
|
-
const option = options[i];
|
|
780
|
-
el.value = option.id;
|
|
781
|
-
el.textContent = `Option ${i + 1}: ${option.text}`;
|
|
782
|
-
el.selected = option.id === active.correctOptionId;
|
|
783
967
|
}
|
|
968
|
+
optionRows.forEach((row, optionIndex) => {
|
|
969
|
+
const option = options[optionIndex];
|
|
970
|
+
setValue(row.textInput, option.text);
|
|
971
|
+
row.radio.value = option.id;
|
|
972
|
+
row.radio.checked = option.id === active.correctOptionId;
|
|
973
|
+
row.media.sync(
|
|
974
|
+
option.media,
|
|
975
|
+
!!option.media || expandedMedia.has(activeKey(optionIndex))
|
|
976
|
+
);
|
|
977
|
+
const show = touched.has(`opt:${activeKey(optionIndex)}`) && option.text.trim().length === 0;
|
|
978
|
+
setFieldError(
|
|
979
|
+
row.textInput,
|
|
980
|
+
row.error,
|
|
981
|
+
show ? `Answer ${optionIndex + 1} needs text` : null
|
|
982
|
+
);
|
|
983
|
+
});
|
|
784
984
|
};
|
|
785
985
|
const syncTo = (nextData, nextOnChange) => {
|
|
786
986
|
currentData = nextData;
|
|
@@ -813,6 +1013,11 @@ function unmountQuizEditor(container) {
|
|
|
813
1013
|
var QuizEditorType = {
|
|
814
1014
|
getId: () => "quiz-editor",
|
|
815
1015
|
getVersion: () => 1,
|
|
1016
|
+
getDisplayName: () => "Quiz",
|
|
1017
|
+
getDescription: () => "Multiple-choice questions that check understanding",
|
|
1018
|
+
validate(data) {
|
|
1019
|
+
return getQuizAuthoringIssues(data);
|
|
1020
|
+
},
|
|
816
1021
|
getMaximumScore(data) {
|
|
817
1022
|
return data.questions.length;
|
|
818
1023
|
},
|
|
@@ -845,6 +1050,11 @@ var QuizEditorType = {
|
|
|
845
1050
|
var QuizContentType = {
|
|
846
1051
|
getId: () => "quiz-editor",
|
|
847
1052
|
getVersion: () => 1,
|
|
1053
|
+
getDisplayName: () => "Quiz",
|
|
1054
|
+
getDescription: () => "Multiple-choice questions that check understanding",
|
|
1055
|
+
validate(data) {
|
|
1056
|
+
return getQuizAuthoringIssues(data);
|
|
1057
|
+
},
|
|
848
1058
|
getMaximumScore(data) {
|
|
849
1059
|
return data.questions.length;
|
|
850
1060
|
},
|
|
@@ -866,41 +1076,11 @@ var QuizContentType = {
|
|
|
866
1076
|
unmountQuizEditor(container);
|
|
867
1077
|
}
|
|
868
1078
|
};
|
|
869
|
-
|
|
870
|
-
// src/validate.ts
|
|
871
|
-
function validateQuizData(data) {
|
|
872
|
-
const errors = [];
|
|
873
|
-
if (data.questions.length === 0) {
|
|
874
|
-
errors.push("at least one question is required");
|
|
875
|
-
return errors;
|
|
876
|
-
}
|
|
877
|
-
data.questions.forEach((question, index) => {
|
|
878
|
-
if (question.text.trim().length === 0) {
|
|
879
|
-
errors.push(`questions[${index}].text must be a non-empty string`);
|
|
880
|
-
}
|
|
881
|
-
if (question.options.length === 0) {
|
|
882
|
-
errors.push(`questions[${index}].at least one option is required`);
|
|
883
|
-
} else {
|
|
884
|
-
question.options.forEach((option, optionIndex) => {
|
|
885
|
-
if (option.text.trim().length === 0) {
|
|
886
|
-
errors.push(
|
|
887
|
-
`questions[${index}].options[${optionIndex}].text must be a non-empty string`
|
|
888
|
-
);
|
|
889
|
-
}
|
|
890
|
-
});
|
|
891
|
-
}
|
|
892
|
-
if (question.correctOptionId === null) {
|
|
893
|
-
errors.push(
|
|
894
|
-
`questions[${index}].exactly one correct option must be resolvable`
|
|
895
|
-
);
|
|
896
|
-
}
|
|
897
|
-
});
|
|
898
|
-
return errors;
|
|
899
|
-
}
|
|
900
1079
|
// Annotate the CommonJS export names for ESM import in node:
|
|
901
1080
|
0 && (module.exports = {
|
|
902
1081
|
QuizContentType,
|
|
903
1082
|
QuizEditorType,
|
|
1083
|
+
getQuizAuthoringIssues,
|
|
904
1084
|
renderQuizPlayback,
|
|
905
1085
|
unmountQuizEditor,
|
|
906
1086
|
unmountQuizPlayback,
|