@interlacejs/quiz 0.1.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.js ADDED
@@ -0,0 +1,877 @@
1
+ // src/tokens.ts
2
+ var COLORS = {
3
+ accent: "#0066cc",
4
+ accentHover: "#0052a3",
5
+ accentLight: "rgba(0, 102, 204, 0.15)",
6
+ accentLighter: "rgba(0, 102, 204, 0.08)",
7
+ text: "#0f172a",
8
+ textMuted: "#64748b",
9
+ border: "#e2e8f0",
10
+ borderStrong: "#cbd5e1",
11
+ surface: "#ffffff",
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
+ };
50
+
51
+ // src/quiz-player.ts
52
+ var quizPlaybackSessions = /* @__PURE__ */ new WeakMap();
53
+ function createMediaImage(className, src, alt) {
54
+ if (src.length === 0) return null;
55
+ const img = document.createElement("img");
56
+ img.className = className;
57
+ img.src = src;
58
+ img.style.maxWidth = "100%";
59
+ img.style.height = "auto";
60
+ img.style.borderRadius = `${RADIUS.sm}px`;
61
+ img.style.marginBottom = `${SPACE[2]}px`;
62
+ if (alt) img.alt = alt;
63
+ return img;
64
+ }
65
+ function lockOption(button) {
66
+ button.disabled = true;
67
+ button.style.cursor = "default";
68
+ }
69
+ function revealCorrect(button, glyph, label) {
70
+ button.classList.add("quiz-playback-option-correct");
71
+ button.style.backgroundColor = COLORS.correctBg;
72
+ button.style.borderColor = COLORS.correctBg;
73
+ button.style.color = COLORS.correctText;
74
+ glyph.textContent = "\u2713";
75
+ label.textContent = "Correct";
76
+ glyph.style.opacity = "1";
77
+ label.style.opacity = "1";
78
+ }
79
+ function revealIncorrect(button, glyph, label) {
80
+ button.classList.add("quiz-playback-option-incorrect");
81
+ button.style.backgroundColor = COLORS.incorrectBg;
82
+ button.style.borderColor = COLORS.incorrectBorder;
83
+ button.style.color = COLORS.incorrectText;
84
+ glyph.textContent = "\u2715";
85
+ label.textContent = "Incorrect";
86
+ glyph.style.opacity = "1";
87
+ label.style.opacity = "1";
88
+ }
89
+ function renderQuizPlayback(container, data, callbacks) {
90
+ unmountQuizPlayback(container);
91
+ const total = data.questions.length;
92
+ const answers = data.questions.map(() => null);
93
+ let currentIndex = 0;
94
+ let completed = false;
95
+ const root = document.createElement("div");
96
+ root.className = "quiz-playback";
97
+ root.style.fontFamily = FONTS.body;
98
+ root.style.color = COLORS.text;
99
+ root.style.lineHeight = "1.5";
100
+ const indicator = document.createElement("div");
101
+ indicator.className = "quiz-playback-indicator";
102
+ indicator.style.fontSize = `${TYPE.xs}px`;
103
+ indicator.style.fontWeight = "600";
104
+ indicator.style.color = COLORS.textMuted;
105
+ indicator.style.marginBottom = `${SPACE[2]}px`;
106
+ indicator.style.textTransform = "uppercase";
107
+ indicator.style.letterSpacing = "0.06em";
108
+ root.appendChild(indicator);
109
+ const question = document.createElement("p");
110
+ question.className = "quiz-playback-question";
111
+ question.style.margin = `0 0 ${SPACE[2]}px 0`;
112
+ question.style.fontFamily = FONTS.display;
113
+ question.style.fontSize = `${TYPE.lg}px`;
114
+ question.style.fontWeight = "600";
115
+ root.appendChild(question);
116
+ const questionMediaSlot = document.createElement("div");
117
+ root.appendChild(questionMediaSlot);
118
+ const optionsBox = document.createElement("div");
119
+ optionsBox.className = "quiz-playback-options";
120
+ optionsBox.style.display = "flex";
121
+ optionsBox.style.flexDirection = "column";
122
+ optionsBox.style.gap = `${SPACE[2]}px`;
123
+ root.appendChild(optionsBox);
124
+ const navFooter = document.createElement("div");
125
+ navFooter.className = "quiz-playback-nav";
126
+ navFooter.style.display = "flex";
127
+ navFooter.style.justifyContent = "flex-end";
128
+ navFooter.style.gap = `${SPACE[2]}px`;
129
+ navFooter.style.marginTop = `${SPACE[4]}px`;
130
+ root.appendChild(navFooter);
131
+ const prevButton = document.createElement("button");
132
+ prevButton.type = "button";
133
+ prevButton.className = "quiz-playback-prev";
134
+ prevButton.textContent = "Previous";
135
+ prevButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
136
+ prevButton.style.backgroundColor = COLORS.surface;
137
+ prevButton.style.border = `1px solid ${COLORS.borderStrong}`;
138
+ prevButton.style.borderRadius = `${RADIUS.md}px`;
139
+ prevButton.style.color = COLORS.text;
140
+ prevButton.style.cursor = "pointer";
141
+ prevButton.style.fontSize = `${TYPE.base}px`;
142
+ prevButton.style.fontWeight = "600";
143
+ prevButton.style.transition = "background-color 150ms ease";
144
+ prevButton.addEventListener("click", () => {
145
+ if (currentIndex > 0) {
146
+ renderQuestion(currentIndex - 1);
147
+ }
148
+ });
149
+ navFooter.appendChild(prevButton);
150
+ const nextButton = document.createElement("button");
151
+ nextButton.type = "button";
152
+ nextButton.className = "quiz-playback-next";
153
+ nextButton.textContent = "Next";
154
+ nextButton.disabled = true;
155
+ nextButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
156
+ nextButton.style.backgroundColor = COLORS.accent;
157
+ nextButton.style.border = "none";
158
+ nextButton.style.borderRadius = `${RADIUS.md}px`;
159
+ nextButton.style.color = COLORS.white;
160
+ nextButton.style.cursor = "pointer";
161
+ nextButton.style.fontSize = `${TYPE.base}px`;
162
+ nextButton.style.fontWeight = "600";
163
+ nextButton.style.transition = "background-color 150ms ease, opacity 150ms ease";
164
+ nextButton.addEventListener("click", () => {
165
+ if (completed) return;
166
+ if (currentIndex < total - 1) {
167
+ renderQuestion(currentIndex + 1);
168
+ } else {
169
+ completed = true;
170
+ const correctCount = data.questions.reduce(
171
+ (count, questionSpec, questionIndex) => count + (answers[questionIndex] !== null && answers[questionIndex] === questionSpec.correctOptionId ? 1 : 0),
172
+ 0
173
+ );
174
+ nextButton.disabled = true;
175
+ nextButton.style.opacity = "0.55";
176
+ callbacks.onComplete(correctCount);
177
+ }
178
+ });
179
+ navFooter.appendChild(nextButton);
180
+ container.appendChild(root);
181
+ let optionEntries = [];
182
+ const syncFooter = (index) => {
183
+ prevButton.style.display = index > 0 ? "block" : "none";
184
+ nextButton.textContent = "Next";
185
+ const answered = answers[index] !== null;
186
+ nextButton.disabled = completed || !answered;
187
+ nextButton.style.opacity = nextButton.disabled ? "0.55" : "1";
188
+ };
189
+ const createOptionButton = (option, isReview) => {
190
+ const button = document.createElement("button");
191
+ button.type = "button";
192
+ button.className = "quiz-playback-option";
193
+ button.style.display = "flex";
194
+ button.style.alignItems = "center";
195
+ button.style.gap = `${SPACE[2]}px`;
196
+ button.style.width = "100%";
197
+ button.style.padding = `${SPACE[3]}px ${SPACE[4]}px`;
198
+ button.style.backgroundColor = COLORS.surface;
199
+ button.style.border = `1px solid ${COLORS.borderStrong}`;
200
+ button.style.borderRadius = `${RADIUS.md}px`;
201
+ button.style.cursor = "pointer";
202
+ button.style.fontSize = `${TYPE.base}px`;
203
+ button.style.color = COLORS.text;
204
+ button.style.textAlign = "left";
205
+ button.style.transition = "background-color 150ms ease, border-color 150ms ease, color 150ms ease";
206
+ const text = document.createElement("span");
207
+ text.className = "quiz-playback-option-text";
208
+ text.textContent = option.text;
209
+ text.style.flex = "1";
210
+ button.appendChild(text);
211
+ const thumbnail = createMediaImage(
212
+ "quiz-playback-option-media",
213
+ option.media?.src ?? "",
214
+ option.media?.alt
215
+ );
216
+ if (thumbnail) button.appendChild(thumbnail);
217
+ const status = document.createElement("span");
218
+ status.style.display = "flex";
219
+ status.style.alignItems = "center";
220
+ status.style.gap = `${SPACE[1]}px`;
221
+ status.style.marginLeft = "auto";
222
+ status.style.transition = "opacity 150ms ease";
223
+ const glyph = document.createElement("span");
224
+ glyph.style.fontWeight = "700";
225
+ glyph.style.fontSize = `${TYPE.md}px`;
226
+ glyph.style.lineHeight = "1";
227
+ status.appendChild(glyph);
228
+ const label = document.createElement("span");
229
+ label.style.fontWeight = "600";
230
+ label.style.fontSize = `${TYPE.sm}px`;
231
+ label.style.letterSpacing = "0.04em";
232
+ status.appendChild(label);
233
+ button.appendChild(status);
234
+ if (isReview) {
235
+ lockOption(button);
236
+ }
237
+ return { button, option, statusGlyph: glyph, statusLabel: label };
238
+ };
239
+ const revealQuestion = (questionIndex, selectedOptionId) => {
240
+ const spec = data.questions[questionIndex];
241
+ const correctOptionId = spec.correctOptionId;
242
+ optionEntries.forEach((entry) => {
243
+ lockOption(entry.button);
244
+ if (entry.option.id === correctOptionId) {
245
+ revealCorrect(entry.button, entry.statusGlyph, entry.statusLabel);
246
+ }
247
+ if (entry.option.id === selectedOptionId && selectedOptionId !== correctOptionId) {
248
+ revealIncorrect(entry.button, entry.statusGlyph, entry.statusLabel);
249
+ }
250
+ });
251
+ };
252
+ const renderQuestion = (index) => {
253
+ currentIndex = index;
254
+ const spec = data.questions[index];
255
+ const correctOptionId = spec.correctOptionId;
256
+ const answered = answers[index] ?? null;
257
+ indicator.textContent = `Question ${index + 1} of ${total}`;
258
+ question.textContent = spec.text;
259
+ questionMediaSlot.innerHTML = "";
260
+ const questionMedia = createMediaImage(
261
+ "quiz-playback-question-media",
262
+ spec.media?.src ?? "",
263
+ spec.media?.alt
264
+ );
265
+ if (questionMedia) questionMediaSlot.appendChild(questionMedia);
266
+ optionsBox.innerHTML = "";
267
+ optionEntries = spec.options.map((option) => {
268
+ const isReview = answered !== null;
269
+ const entry = createOptionButton(option, isReview);
270
+ if (isReview) {
271
+ if (option.id === correctOptionId) {
272
+ revealCorrect(entry.button, entry.statusGlyph, entry.statusLabel);
273
+ }
274
+ if (option.id === answered && answered !== correctOptionId) {
275
+ revealIncorrect(entry.button, entry.statusGlyph, entry.statusLabel);
276
+ }
277
+ } else {
278
+ entry.button.addEventListener("click", () => {
279
+ if (completed) return;
280
+ answers[index] = option.id;
281
+ revealQuestion(index, option.id);
282
+ syncFooter(index);
283
+ });
284
+ }
285
+ optionsBox.appendChild(entry.button);
286
+ return entry;
287
+ });
288
+ syncFooter(index);
289
+ };
290
+ renderQuestion(0);
291
+ quizPlaybackSessions.set(container, {
292
+ destroy: () => {
293
+ root.remove();
294
+ }
295
+ });
296
+ }
297
+ function unmountQuizPlayback(container) {
298
+ quizPlaybackSessions.get(container)?.destroy();
299
+ quizPlaybackSessions.delete(container);
300
+ }
301
+
302
+ // src/quiz-editor.ts
303
+ var quizEditorControllers = /* @__PURE__ */ new WeakMap();
304
+ function buildMediaRef(src, alt) {
305
+ if (src.length === 0) return void 0;
306
+ return alt.length > 0 ? { src, alt } : { src };
307
+ }
308
+ function createPreview(media, className) {
309
+ if (!media || media.src.length === 0) return null;
310
+ const img = document.createElement("img");
311
+ img.className = className;
312
+ img.src = media.src;
313
+ img.style.maxWidth = "100%";
314
+ img.style.height = "auto";
315
+ img.style.borderRadius = `${RADIUS.sm}px`;
316
+ img.style.marginTop = `${SPACE[1]}px`;
317
+ if (media.alt) img.alt = media.alt;
318
+ return img;
319
+ }
320
+ function syncPreview(slot, media, className) {
321
+ slot.innerHTML = "";
322
+ const img = createPreview(media, className);
323
+ if (img) slot.appendChild(img);
324
+ }
325
+ function nextOptionId(options, index) {
326
+ const base = `opt-${index}`;
327
+ if (!options.some((option) => option.id === base)) return base;
328
+ let candidate = index + 1;
329
+ while (options.some((option) => option.id === `opt-${candidate}`)) {
330
+ candidate += 1;
331
+ }
332
+ return `opt-${candidate}`;
333
+ }
334
+ function nextQuestionId(questions, index) {
335
+ const base = `q-${index}`;
336
+ if (!questions.some((question) => question.id === base)) return base;
337
+ let candidate = index + 1;
338
+ while (questions.some((question) => question.id === `q-${candidate}`)) {
339
+ candidate += 1;
340
+ }
341
+ return `q-${candidate}`;
342
+ }
343
+ function emptyQuestion(id) {
344
+ return { id, text: "", options: [], correctOptionId: null };
345
+ }
346
+ function createQuizEditorController(container, data, onChange) {
347
+ container.innerHTML = "";
348
+ const root = document.createElement("div");
349
+ root.className = "quiz-editor";
350
+ root.style.fontFamily = FONTS.body;
351
+ root.style.color = COLORS.text;
352
+ root.style.lineHeight = "1.5";
353
+ let currentData = data;
354
+ let currentOnChange = onChange;
355
+ let selectedIndex = 0;
356
+ let bodyQuestionIndex = -1;
357
+ let optionRows = [];
358
+ const emit = (questions) => {
359
+ currentOnChange({ questions });
360
+ };
361
+ const patchActiveQuestion = (mutate) => {
362
+ const active = currentData.questions[selectedIndex];
363
+ if (!active) return;
364
+ const questions = currentData.questions.slice();
365
+ questions[selectedIndex] = mutate(active);
366
+ emit(questions);
367
+ };
368
+ const questionsHeader = document.createElement("div");
369
+ questionsHeader.textContent = "Questions";
370
+ questionsHeader.style.marginBottom = `${SPACE[2]}px`;
371
+ questionsHeader.style.fontWeight = "600";
372
+ questionsHeader.style.fontSize = `${TYPE.sm}px`;
373
+ questionsHeader.style.color = COLORS.textMuted;
374
+ root.appendChild(questionsHeader);
375
+ const tabsBar = document.createElement("div");
376
+ tabsBar.className = "quiz-question-tabs";
377
+ tabsBar.style.display = "flex";
378
+ tabsBar.style.flexWrap = "wrap";
379
+ tabsBar.style.gap = `${SPACE[2]}px`;
380
+ tabsBar.style.marginBottom = `${SPACE[3]}px`;
381
+ root.appendChild(tabsBar);
382
+ const addQuestionButton = document.createElement("button");
383
+ addQuestionButton.type = "button";
384
+ addQuestionButton.className = "quiz-add-question";
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;
395
+ addQuestionButton.addEventListener("click", () => {
396
+ const id = nextQuestionId(currentData.questions, currentData.questions.length);
397
+ const questions = [...currentData.questions, emptyQuestion(id)];
398
+ currentData = { questions };
399
+ selectedIndex = questions.length - 1;
400
+ emit(questions);
401
+ bodyQuestionIndex = -1;
402
+ renderTabs();
403
+ syncActiveBody();
404
+ });
405
+ root.appendChild(addQuestionButton);
406
+ const body = document.createElement("div");
407
+ body.className = "quiz-editor-body";
408
+ root.appendChild(body);
409
+ const questionInput = document.createElement("input");
410
+ questionInput.type = "text";
411
+ questionInput.className = "quiz-question";
412
+ questionInput.style.display = "block";
413
+ questionInput.style.width = "100%";
414
+ questionInput.style.boxSizing = "border-box";
415
+ questionInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
416
+ questionInput.style.fontSize = `${TYPE.md}px`;
417
+ questionInput.style.color = COLORS.text;
418
+ questionInput.style.border = `1px solid ${COLORS.borderStrong}`;
419
+ questionInput.style.borderRadius = `${RADIUS.sm}px`;
420
+ questionInput.style.backgroundColor = COLORS.surface;
421
+ questionInput.addEventListener("input", () => {
422
+ patchActiveQuestion((question) => ({
423
+ ...question,
424
+ text: questionInput.value
425
+ }));
426
+ });
427
+ const questionLabel = document.createElement("label");
428
+ questionLabel.style.display = "block";
429
+ questionLabel.style.marginBottom = `${SPACE[3]}px`;
430
+ questionLabel.style.fontWeight = "600";
431
+ questionLabel.style.fontSize = `${TYPE.sm}px`;
432
+ questionLabel.style.color = COLORS.textMuted;
433
+ questionLabel.appendChild(document.createTextNode("Question"));
434
+ questionLabel.appendChild(questionInput);
435
+ body.appendChild(questionLabel);
436
+ const questionMediaBox = document.createElement("div");
437
+ questionMediaBox.className = "quiz-question-media";
438
+ questionMediaBox.style.padding = `${SPACE[3]}px`;
439
+ questionMediaBox.style.marginBottom = `${SPACE[4]}px`;
440
+ questionMediaBox.style.backgroundColor = COLORS.surfaceRaised;
441
+ questionMediaBox.style.border = `1px solid ${COLORS.border}`;
442
+ questionMediaBox.style.borderRadius = `${RADIUS.md}px`;
443
+ const questionMediaSrc = document.createElement("input");
444
+ questionMediaSrc.type = "text";
445
+ questionMediaSrc.className = "quiz-question-media-src";
446
+ questionMediaSrc.placeholder = "Question media URL";
447
+ questionMediaSrc.style.display = "block";
448
+ questionMediaSrc.style.width = "100%";
449
+ questionMediaSrc.style.boxSizing = "border-box";
450
+ questionMediaSrc.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
451
+ questionMediaSrc.style.marginBottom = `${SPACE[2]}px`;
452
+ questionMediaSrc.style.fontSize = `${TYPE.base}px`;
453
+ questionMediaSrc.style.color = COLORS.text;
454
+ questionMediaSrc.style.border = `1px solid ${COLORS.borderStrong}`;
455
+ questionMediaSrc.style.borderRadius = `${RADIUS.sm}px`;
456
+ questionMediaSrc.style.backgroundColor = COLORS.surface;
457
+ const questionMediaAlt = document.createElement("input");
458
+ questionMediaAlt.type = "text";
459
+ questionMediaAlt.className = "quiz-question-media-alt";
460
+ questionMediaAlt.placeholder = "Media alt text";
461
+ questionMediaAlt.style.display = "block";
462
+ questionMediaAlt.style.width = "100%";
463
+ questionMediaAlt.style.boxSizing = "border-box";
464
+ questionMediaAlt.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
465
+ questionMediaAlt.style.fontSize = `${TYPE.base}px`;
466
+ questionMediaAlt.style.color = COLORS.text;
467
+ questionMediaAlt.style.border = `1px solid ${COLORS.borderStrong}`;
468
+ questionMediaAlt.style.borderRadius = `${RADIUS.sm}px`;
469
+ questionMediaAlt.style.backgroundColor = COLORS.surface;
470
+ const questionPreviewSlot = document.createElement("div");
471
+ questionPreviewSlot.className = "quiz-question-media-preview-slot";
472
+ const emitQuestionMedia = () => {
473
+ const media = buildMediaRef(questionMediaSrc.value, questionMediaAlt.value);
474
+ syncPreview(questionPreviewSlot, media, "quiz-question-media-preview");
475
+ patchActiveQuestion((question) => ({ ...question, media }));
476
+ };
477
+ questionMediaSrc.addEventListener("input", emitQuestionMedia);
478
+ questionMediaAlt.addEventListener("input", emitQuestionMedia);
479
+ questionMediaBox.append(questionMediaSrc, questionMediaAlt, questionPreviewSlot);
480
+ body.appendChild(questionMediaBox);
481
+ const optionsTitle = document.createElement("div");
482
+ optionsTitle.textContent = "Options";
483
+ optionsTitle.style.marginBottom = `${SPACE[2]}px`;
484
+ optionsTitle.style.fontWeight = "600";
485
+ optionsTitle.style.fontSize = `${TYPE.sm}px`;
486
+ optionsTitle.style.color = COLORS.textMuted;
487
+ body.appendChild(optionsTitle);
488
+ const optionsContainer = document.createElement("div");
489
+ optionsContainer.style.display = "flex";
490
+ optionsContainer.style.flexDirection = "column";
491
+ optionsContainer.style.gap = `${SPACE[2]}px`;
492
+ body.appendChild(optionsContainer);
493
+ const addOptionButton = document.createElement("button");
494
+ addOptionButton.type = "button";
495
+ addOptionButton.className = "quiz-add-option";
496
+ addOptionButton.textContent = "Add option";
497
+ addOptionButton.style.marginTop = `${SPACE[2]}px`;
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;
506
+ addOptionButton.addEventListener("click", () => {
507
+ patchActiveQuestion((question) => {
508
+ const nextOptions = [
509
+ ...question.options,
510
+ {
511
+ id: nextOptionId(question.options, question.options.length),
512
+ text: ""
513
+ }
514
+ ];
515
+ const nextCorrectOptionId = question.options.length === 0 ? nextOptions[0].id : question.correctOptionId;
516
+ return {
517
+ ...question,
518
+ options: nextOptions,
519
+ correctOptionId: nextCorrectOptionId
520
+ };
521
+ });
522
+ });
523
+ body.appendChild(addOptionButton);
524
+ const correctLabel = document.createElement("label");
525
+ correctLabel.style.display = "block";
526
+ correctLabel.style.marginTop = `${SPACE[4]}px`;
527
+ correctLabel.style.fontWeight = "600";
528
+ correctLabel.style.fontSize = `${TYPE.sm}px`;
529
+ correctLabel.style.color = COLORS.textMuted;
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);
550
+ container.appendChild(root);
551
+ const createOptionRow = (option, optionIndex) => {
552
+ const row = document.createElement("div");
553
+ row.className = "quiz-option-row";
554
+ row.style.display = "flex";
555
+ row.style.flexWrap = "wrap";
556
+ row.style.gap = `${SPACE[2]}px`;
557
+ row.style.alignItems = "center";
558
+ row.style.padding = `${SPACE[2]}px`;
559
+ row.style.backgroundColor = COLORS.surfaceRaised;
560
+ row.style.border = `1px solid ${COLORS.border}`;
561
+ row.style.borderRadius = `${RADIUS.md}px`;
562
+ const textInput = document.createElement("input");
563
+ textInput.type = "text";
564
+ textInput.className = "quiz-option-text";
565
+ textInput.style.flex = "1";
566
+ textInput.style.minWidth = "120px";
567
+ textInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
568
+ textInput.style.fontSize = `${TYPE.base}px`;
569
+ textInput.style.color = COLORS.text;
570
+ textInput.style.border = `1px solid ${COLORS.borderStrong}`;
571
+ textInput.style.borderRadius = `${RADIUS.sm}px`;
572
+ textInput.style.backgroundColor = COLORS.surface;
573
+ textInput.value = option.text;
574
+ textInput.addEventListener("input", () => {
575
+ patchActiveQuestion((question) => {
576
+ const nextOptions = question.options.slice();
577
+ nextOptions[optionIndex] = {
578
+ ...nextOptions[optionIndex],
579
+ text: textInput.value
580
+ };
581
+ return { ...question, options: nextOptions };
582
+ });
583
+ });
584
+ const mediaSrcInput = document.createElement("input");
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");
616
+ patchActiveQuestion((question) => {
617
+ const nextOptions = question.options.slice();
618
+ nextOptions[optionIndex] = { ...nextOptions[optionIndex], media };
619
+ return { ...question, options: nextOptions };
620
+ });
621
+ };
622
+ mediaSrcInput.addEventListener("input", emitOptionMedia);
623
+ mediaAltInput.addEventListener("input", emitOptionMedia);
624
+ const remove = document.createElement("button");
625
+ remove.type = "button";
626
+ remove.className = "quiz-option-remove";
627
+ remove.textContent = "Remove";
628
+ remove.style.padding = `${SPACE[1]}px ${SPACE[3]}px`;
629
+ remove.style.backgroundColor = COLORS.errorBg;
630
+ remove.style.border = `1px solid ${COLORS.errorBorder}`;
631
+ remove.style.borderRadius = `${RADIUS.sm}px`;
632
+ remove.style.cursor = "pointer";
633
+ remove.style.fontSize = `${TYPE.sm}px`;
634
+ remove.style.fontWeight = "600";
635
+ remove.style.color = COLORS.errorText;
636
+ remove.addEventListener("click", () => {
637
+ patchActiveQuestion((question) => {
638
+ const removed = question.options[optionIndex];
639
+ const nextOptions = question.options.filter((_, i) => i !== optionIndex);
640
+ return {
641
+ ...question,
642
+ options: nextOptions,
643
+ // Removing the correct option clears correctness; do not shift it.
644
+ correctOptionId: removed && removed.id === question.correctOptionId ? null : question.correctOptionId
645
+ };
646
+ });
647
+ });
648
+ row.append(textInput, mediaSrcInput, mediaAltInput, previewSlot, remove);
649
+ optionsContainer.appendChild(row);
650
+ return { row, textInput, mediaSrcInput, mediaAltInput, previewSlot };
651
+ };
652
+ const selectQuestion = (index) => {
653
+ selectedIndex = index;
654
+ bodyQuestionIndex = -1;
655
+ renderTabs();
656
+ syncActiveBody();
657
+ };
658
+ const removeQuestion = (index) => {
659
+ const next = currentData.questions.filter((_, i) => i !== index);
660
+ const questions = next.length > 0 ? next : [emptyQuestion(nextQuestionId([], 0))];
661
+ if (selectedIndex === index) {
662
+ selectedIndex = 0;
663
+ } else if (index < selectedIndex) {
664
+ selectedIndex -= 1;
665
+ }
666
+ currentData = { questions };
667
+ emit(questions);
668
+ bodyQuestionIndex = -1;
669
+ renderTabs();
670
+ syncActiveBody();
671
+ };
672
+ const renderTabs = () => {
673
+ tabsBar.innerHTML = "";
674
+ currentData.questions.forEach((question, index) => {
675
+ const wrap = document.createElement("div");
676
+ wrap.className = "quiz-question-tab-wrap";
677
+ wrap.style.display = "inline-flex";
678
+ wrap.style.alignItems = "center";
679
+ wrap.style.gap = `${SPACE[1]}px`;
680
+ const tab = document.createElement("button");
681
+ tab.type = "button";
682
+ tab.className = "quiz-question-tab";
683
+ tab.textContent = `Question ${index + 1}`;
684
+ tab.style.padding = `${SPACE[1]}px ${SPACE[3]}px`;
685
+ tab.style.borderRadius = `${RADIUS.sm}px`;
686
+ tab.style.border = `1px solid ${COLORS.borderStrong}`;
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
+ }
698
+ tab.addEventListener("click", () => selectQuestion(index));
699
+ const remove = document.createElement("button");
700
+ remove.type = "button";
701
+ remove.className = "quiz-question-remove";
702
+ remove.textContent = "Remove";
703
+ remove.style.padding = `${SPACE[1]}px ${SPACE[2]}px`;
704
+ remove.style.borderRadius = `${RADIUS.sm}px`;
705
+ remove.style.border = `1px solid ${COLORS.errorBorder}`;
706
+ remove.style.backgroundColor = COLORS.errorBg;
707
+ remove.style.color = COLORS.errorText;
708
+ remove.style.cursor = "pointer";
709
+ remove.style.fontSize = `${TYPE.xs}px`;
710
+ remove.style.fontWeight = "600";
711
+ remove.addEventListener("click", () => removeQuestion(index));
712
+ wrap.append(tab, remove);
713
+ tabsBar.appendChild(wrap);
714
+ });
715
+ };
716
+ const syncActiveBody = () => {
717
+ const active = currentData.questions[selectedIndex];
718
+ if (!active) return;
719
+ questionInput.value = active.text;
720
+ questionMediaSrc.value = active.media?.src ?? "";
721
+ questionMediaAlt.value = active.media?.alt ?? "";
722
+ syncPreview(questionPreviewSlot, active.media, "quiz-question-media-preview");
723
+ const options = active.options;
724
+ if (bodyQuestionIndex !== selectedIndex || optionRows.length !== options.length) {
725
+ optionsContainer.innerHTML = "";
726
+ optionRows = options.map(
727
+ (option, optionIndex) => createOptionRow(option, optionIndex)
728
+ );
729
+ 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
+ }
753
+ };
754
+ const syncTo = (nextData, nextOnChange) => {
755
+ currentData = nextData;
756
+ currentOnChange = nextOnChange;
757
+ if (currentData.questions.length === 0) {
758
+ currentData = { questions: [emptyQuestion("q-0")] };
759
+ }
760
+ if (selectedIndex >= currentData.questions.length) {
761
+ selectedIndex = 0;
762
+ }
763
+ renderTabs();
764
+ syncActiveBody();
765
+ };
766
+ renderTabs();
767
+ syncActiveBody();
768
+ return { syncTo };
769
+ }
770
+ function getOrCreateQuizEditorController(container, data, onChange) {
771
+ let controller = quizEditorControllers.get(container);
772
+ if (!controller) {
773
+ controller = createQuizEditorController(container, data, onChange);
774
+ quizEditorControllers.set(container, controller);
775
+ }
776
+ return controller;
777
+ }
778
+ function unmountQuizEditor(container) {
779
+ quizEditorControllers.delete(container);
780
+ container.innerHTML = "";
781
+ }
782
+ var QuizEditorType = {
783
+ getId: () => "quiz-editor",
784
+ getVersion: () => 1,
785
+ getMaximumScore(data) {
786
+ return data.questions.length;
787
+ },
788
+ async preload() {
789
+ },
790
+ renderEditor(container, data, onChange) {
791
+ getOrCreateQuizEditorController(container, data, onChange).syncTo(
792
+ data,
793
+ onChange
794
+ );
795
+ },
796
+ updateEditor(container, data, onChange) {
797
+ getOrCreateQuizEditorController(container, data, onChange).syncTo(
798
+ data,
799
+ onChange
800
+ );
801
+ },
802
+ renderPlayback(container, data, callbacks) {
803
+ renderQuizPlayback(container, data, callbacks);
804
+ },
805
+ unmountPlayback(container) {
806
+ unmountQuizPlayback(container);
807
+ },
808
+ unmount(container) {
809
+ unmountQuizEditor(container);
810
+ }
811
+ };
812
+
813
+ // src/quiz-content-type.ts
814
+ var QuizContentType = {
815
+ getId: () => "quiz-editor",
816
+ getVersion: () => 1,
817
+ getMaximumScore(data) {
818
+ return data.questions.length;
819
+ },
820
+ async preload() {
821
+ },
822
+ renderEditor(container, data, onChange) {
823
+ QuizEditorType.renderEditor(container, data, onChange);
824
+ },
825
+ updateEditor(container, data, onChange) {
826
+ QuizEditorType.updateEditor?.(container, data, onChange);
827
+ },
828
+ renderPlayback(container, data, callbacks) {
829
+ renderQuizPlayback(container, data, callbacks);
830
+ },
831
+ unmountPlayback(container) {
832
+ unmountQuizPlayback(container);
833
+ },
834
+ unmount(container) {
835
+ unmountQuizEditor(container);
836
+ }
837
+ };
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
+ export {
870
+ QuizContentType,
871
+ QuizEditorType,
872
+ renderQuizPlayback,
873
+ unmountQuizEditor,
874
+ unmountQuizPlayback,
875
+ validateQuizData
876
+ };
877
+ //# sourceMappingURL=index.js.map