@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.cjs ADDED
@@ -0,0 +1,909 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ QuizContentType: () => QuizContentType,
24
+ QuizEditorType: () => QuizEditorType,
25
+ renderQuizPlayback: () => renderQuizPlayback,
26
+ unmountQuizEditor: () => unmountQuizEditor,
27
+ unmountQuizPlayback: () => unmountQuizPlayback,
28
+ validateQuizData: () => validateQuizData
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/tokens.ts
33
+ var COLORS = {
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
+ };
81
+
82
+ // src/quiz-player.ts
83
+ var quizPlaybackSessions = /* @__PURE__ */ new WeakMap();
84
+ function createMediaImage(className, src, alt) {
85
+ if (src.length === 0) return null;
86
+ const img = document.createElement("img");
87
+ img.className = className;
88
+ img.src = src;
89
+ img.style.maxWidth = "100%";
90
+ img.style.height = "auto";
91
+ img.style.borderRadius = `${RADIUS.sm}px`;
92
+ img.style.marginBottom = `${SPACE[2]}px`;
93
+ if (alt) img.alt = alt;
94
+ return img;
95
+ }
96
+ function lockOption(button) {
97
+ button.disabled = true;
98
+ button.style.cursor = "default";
99
+ }
100
+ function revealCorrect(button, glyph, label) {
101
+ 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;
105
+ glyph.textContent = "\u2713";
106
+ label.textContent = "Correct";
107
+ glyph.style.opacity = "1";
108
+ label.style.opacity = "1";
109
+ }
110
+ function revealIncorrect(button, glyph, label) {
111
+ 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;
115
+ glyph.textContent = "\u2715";
116
+ label.textContent = "Incorrect";
117
+ glyph.style.opacity = "1";
118
+ label.style.opacity = "1";
119
+ }
120
+ function renderQuizPlayback(container, data, callbacks) {
121
+ unmountQuizPlayback(container);
122
+ const total = data.questions.length;
123
+ const answers = data.questions.map(() => null);
124
+ let currentIndex = 0;
125
+ let completed = false;
126
+ const root = document.createElement("div");
127
+ root.className = "quiz-playback";
128
+ root.style.fontFamily = FONTS.body;
129
+ root.style.color = COLORS.text;
130
+ root.style.lineHeight = "1.5";
131
+ const indicator = document.createElement("div");
132
+ indicator.className = "quiz-playback-indicator";
133
+ indicator.style.fontSize = `${TYPE.xs}px`;
134
+ indicator.style.fontWeight = "600";
135
+ indicator.style.color = COLORS.textMuted;
136
+ indicator.style.marginBottom = `${SPACE[2]}px`;
137
+ indicator.style.textTransform = "uppercase";
138
+ indicator.style.letterSpacing = "0.06em";
139
+ root.appendChild(indicator);
140
+ const question = document.createElement("p");
141
+ 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`;
145
+ question.style.fontWeight = "600";
146
+ root.appendChild(question);
147
+ const questionMediaSlot = document.createElement("div");
148
+ root.appendChild(questionMediaSlot);
149
+ const optionsBox = document.createElement("div");
150
+ optionsBox.className = "quiz-playback-options";
151
+ optionsBox.style.display = "flex";
152
+ optionsBox.style.flexDirection = "column";
153
+ optionsBox.style.gap = `${SPACE[2]}px`;
154
+ root.appendChild(optionsBox);
155
+ const navFooter = document.createElement("div");
156
+ navFooter.className = "quiz-playback-nav";
157
+ navFooter.style.display = "flex";
158
+ navFooter.style.justifyContent = "flex-end";
159
+ navFooter.style.gap = `${SPACE[2]}px`;
160
+ navFooter.style.marginTop = `${SPACE[4]}px`;
161
+ root.appendChild(navFooter);
162
+ const prevButton = document.createElement("button");
163
+ prevButton.type = "button";
164
+ prevButton.className = "quiz-playback-prev";
165
+ 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}px`;
170
+ prevButton.style.color = COLORS.text;
171
+ prevButton.style.cursor = "pointer";
172
+ prevButton.style.fontSize = `${TYPE.base}px`;
173
+ prevButton.style.fontWeight = "600";
174
+ prevButton.style.transition = "background-color 150ms ease";
175
+ prevButton.addEventListener("click", () => {
176
+ if (currentIndex > 0) {
177
+ renderQuestion(currentIndex - 1);
178
+ }
179
+ });
180
+ navFooter.appendChild(prevButton);
181
+ const nextButton = document.createElement("button");
182
+ nextButton.type = "button";
183
+ nextButton.className = "quiz-playback-next";
184
+ nextButton.textContent = "Next";
185
+ nextButton.disabled = true;
186
+ nextButton.style.padding = `${SPACE[2]}px ${SPACE[4]}px`;
187
+ nextButton.style.backgroundColor = COLORS.accent;
188
+ nextButton.style.border = "none";
189
+ nextButton.style.borderRadius = `${RADIUS.md}px`;
190
+ nextButton.style.color = COLORS.white;
191
+ nextButton.style.cursor = "pointer";
192
+ nextButton.style.fontSize = `${TYPE.base}px`;
193
+ nextButton.style.fontWeight = "600";
194
+ nextButton.style.transition = "background-color 150ms ease, opacity 150ms ease";
195
+ nextButton.addEventListener("click", () => {
196
+ if (completed) return;
197
+ if (currentIndex < total - 1) {
198
+ renderQuestion(currentIndex + 1);
199
+ } else {
200
+ completed = true;
201
+ const correctCount = data.questions.reduce(
202
+ (count, questionSpec, questionIndex) => count + (answers[questionIndex] !== null && answers[questionIndex] === questionSpec.correctOptionId ? 1 : 0),
203
+ 0
204
+ );
205
+ nextButton.disabled = true;
206
+ nextButton.style.opacity = "0.55";
207
+ callbacks.onComplete(correctCount);
208
+ }
209
+ });
210
+ navFooter.appendChild(nextButton);
211
+ container.appendChild(root);
212
+ let optionEntries = [];
213
+ const syncFooter = (index) => {
214
+ prevButton.style.display = index > 0 ? "block" : "none";
215
+ nextButton.textContent = "Next";
216
+ const answered = answers[index] !== null;
217
+ nextButton.disabled = completed || !answered;
218
+ nextButton.style.opacity = nextButton.disabled ? "0.55" : "1";
219
+ };
220
+ const createOptionButton = (option, isReview) => {
221
+ const button = document.createElement("button");
222
+ button.type = "button";
223
+ button.className = "quiz-playback-option";
224
+ button.style.display = "flex";
225
+ button.style.alignItems = "center";
226
+ button.style.gap = `${SPACE[2]}px`;
227
+ 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}px`;
232
+ button.style.cursor = "pointer";
233
+ button.style.fontSize = `${TYPE.base}px`;
234
+ button.style.color = COLORS.text;
235
+ button.style.textAlign = "left";
236
+ button.style.transition = "background-color 150ms ease, border-color 150ms ease, color 150ms ease";
237
+ const text = document.createElement("span");
238
+ text.className = "quiz-playback-option-text";
239
+ text.textContent = option.text;
240
+ text.style.flex = "1";
241
+ button.appendChild(text);
242
+ const thumbnail = createMediaImage(
243
+ "quiz-playback-option-media",
244
+ option.media?.src ?? "",
245
+ option.media?.alt
246
+ );
247
+ if (thumbnail) button.appendChild(thumbnail);
248
+ const status = document.createElement("span");
249
+ status.style.display = "flex";
250
+ status.style.alignItems = "center";
251
+ status.style.gap = `${SPACE[1]}px`;
252
+ status.style.marginLeft = "auto";
253
+ status.style.transition = "opacity 150ms ease";
254
+ const glyph = document.createElement("span");
255
+ glyph.style.fontWeight = "700";
256
+ glyph.style.fontSize = `${TYPE.md}px`;
257
+ glyph.style.lineHeight = "1";
258
+ status.appendChild(glyph);
259
+ const label = document.createElement("span");
260
+ label.style.fontWeight = "600";
261
+ label.style.fontSize = `${TYPE.sm}px`;
262
+ label.style.letterSpacing = "0.04em";
263
+ status.appendChild(label);
264
+ button.appendChild(status);
265
+ if (isReview) {
266
+ lockOption(button);
267
+ }
268
+ return { button, option, statusGlyph: glyph, statusLabel: label };
269
+ };
270
+ const revealQuestion = (questionIndex, selectedOptionId) => {
271
+ const spec = data.questions[questionIndex];
272
+ const correctOptionId = spec.correctOptionId;
273
+ optionEntries.forEach((entry) => {
274
+ lockOption(entry.button);
275
+ if (entry.option.id === correctOptionId) {
276
+ revealCorrect(entry.button, entry.statusGlyph, entry.statusLabel);
277
+ }
278
+ if (entry.option.id === selectedOptionId && selectedOptionId !== correctOptionId) {
279
+ revealIncorrect(entry.button, entry.statusGlyph, entry.statusLabel);
280
+ }
281
+ });
282
+ };
283
+ const renderQuestion = (index) => {
284
+ currentIndex = index;
285
+ const spec = data.questions[index];
286
+ const correctOptionId = spec.correctOptionId;
287
+ const answered = answers[index] ?? null;
288
+ indicator.textContent = `Question ${index + 1} of ${total}`;
289
+ question.textContent = spec.text;
290
+ questionMediaSlot.innerHTML = "";
291
+ const questionMedia = createMediaImage(
292
+ "quiz-playback-question-media",
293
+ spec.media?.src ?? "",
294
+ spec.media?.alt
295
+ );
296
+ if (questionMedia) questionMediaSlot.appendChild(questionMedia);
297
+ optionsBox.innerHTML = "";
298
+ optionEntries = spec.options.map((option) => {
299
+ const isReview = answered !== null;
300
+ const entry = createOptionButton(option, isReview);
301
+ if (isReview) {
302
+ if (option.id === correctOptionId) {
303
+ revealCorrect(entry.button, entry.statusGlyph, entry.statusLabel);
304
+ }
305
+ if (option.id === answered && answered !== correctOptionId) {
306
+ revealIncorrect(entry.button, entry.statusGlyph, entry.statusLabel);
307
+ }
308
+ } else {
309
+ entry.button.addEventListener("click", () => {
310
+ if (completed) return;
311
+ answers[index] = option.id;
312
+ revealQuestion(index, option.id);
313
+ syncFooter(index);
314
+ });
315
+ }
316
+ optionsBox.appendChild(entry.button);
317
+ return entry;
318
+ });
319
+ syncFooter(index);
320
+ };
321
+ renderQuestion(0);
322
+ quizPlaybackSessions.set(container, {
323
+ destroy: () => {
324
+ root.remove();
325
+ }
326
+ });
327
+ }
328
+ function unmountQuizPlayback(container) {
329
+ quizPlaybackSessions.get(container)?.destroy();
330
+ quizPlaybackSessions.delete(container);
331
+ }
332
+
333
+ // src/quiz-editor.ts
334
+ var quizEditorControllers = /* @__PURE__ */ new WeakMap();
335
+ function buildMediaRef(src, alt) {
336
+ if (src.length === 0) return void 0;
337
+ return alt.length > 0 ? { src, alt } : { src };
338
+ }
339
+ function createPreview(media, className) {
340
+ if (!media || media.src.length === 0) return null;
341
+ const img = document.createElement("img");
342
+ img.className = className;
343
+ img.src = media.src;
344
+ img.style.maxWidth = "100%";
345
+ img.style.height = "auto";
346
+ img.style.borderRadius = `${RADIUS.sm}px`;
347
+ img.style.marginTop = `${SPACE[1]}px`;
348
+ if (media.alt) img.alt = media.alt;
349
+ return img;
350
+ }
351
+ function syncPreview(slot, media, className) {
352
+ slot.innerHTML = "";
353
+ const img = createPreview(media, className);
354
+ if (img) slot.appendChild(img);
355
+ }
356
+ function nextOptionId(options, index) {
357
+ const base = `opt-${index}`;
358
+ if (!options.some((option) => option.id === base)) return base;
359
+ let candidate = index + 1;
360
+ while (options.some((option) => option.id === `opt-${candidate}`)) {
361
+ candidate += 1;
362
+ }
363
+ return `opt-${candidate}`;
364
+ }
365
+ function nextQuestionId(questions, index) {
366
+ const base = `q-${index}`;
367
+ if (!questions.some((question) => question.id === base)) return base;
368
+ let candidate = index + 1;
369
+ while (questions.some((question) => question.id === `q-${candidate}`)) {
370
+ candidate += 1;
371
+ }
372
+ return `q-${candidate}`;
373
+ }
374
+ function emptyQuestion(id) {
375
+ return { id, text: "", options: [], correctOptionId: null };
376
+ }
377
+ function createQuizEditorController(container, data, onChange) {
378
+ container.innerHTML = "";
379
+ const root = document.createElement("div");
380
+ root.className = "quiz-editor";
381
+ root.style.fontFamily = FONTS.body;
382
+ root.style.color = COLORS.text;
383
+ root.style.lineHeight = "1.5";
384
+ let currentData = data;
385
+ let currentOnChange = onChange;
386
+ let selectedIndex = 0;
387
+ let bodyQuestionIndex = -1;
388
+ let optionRows = [];
389
+ const emit = (questions) => {
390
+ currentOnChange({ questions });
391
+ };
392
+ const patchActiveQuestion = (mutate) => {
393
+ const active = currentData.questions[selectedIndex];
394
+ if (!active) return;
395
+ const questions = currentData.questions.slice();
396
+ questions[selectedIndex] = mutate(active);
397
+ emit(questions);
398
+ };
399
+ const questionsHeader = document.createElement("div");
400
+ questionsHeader.textContent = "Questions";
401
+ questionsHeader.style.marginBottom = `${SPACE[2]}px`;
402
+ questionsHeader.style.fontWeight = "600";
403
+ questionsHeader.style.fontSize = `${TYPE.sm}px`;
404
+ questionsHeader.style.color = COLORS.textMuted;
405
+ root.appendChild(questionsHeader);
406
+ const tabsBar = document.createElement("div");
407
+ tabsBar.className = "quiz-question-tabs";
408
+ tabsBar.style.display = "flex";
409
+ tabsBar.style.flexWrap = "wrap";
410
+ tabsBar.style.gap = `${SPACE[2]}px`;
411
+ tabsBar.style.marginBottom = `${SPACE[3]}px`;
412
+ root.appendChild(tabsBar);
413
+ const addQuestionButton = document.createElement("button");
414
+ addQuestionButton.type = "button";
415
+ addQuestionButton.className = "quiz-add-question";
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;
426
+ addQuestionButton.addEventListener("click", () => {
427
+ const id = nextQuestionId(currentData.questions, currentData.questions.length);
428
+ const questions = [...currentData.questions, emptyQuestion(id)];
429
+ currentData = { questions };
430
+ selectedIndex = questions.length - 1;
431
+ emit(questions);
432
+ bodyQuestionIndex = -1;
433
+ renderTabs();
434
+ syncActiveBody();
435
+ });
436
+ root.appendChild(addQuestionButton);
437
+ const body = document.createElement("div");
438
+ body.className = "quiz-editor-body";
439
+ root.appendChild(body);
440
+ const questionInput = document.createElement("input");
441
+ questionInput.type = "text";
442
+ questionInput.className = "quiz-question";
443
+ questionInput.style.display = "block";
444
+ questionInput.style.width = "100%";
445
+ questionInput.style.boxSizing = "border-box";
446
+ questionInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
447
+ questionInput.style.fontSize = `${TYPE.md}px`;
448
+ questionInput.style.color = COLORS.text;
449
+ questionInput.style.border = `1px solid ${COLORS.borderStrong}`;
450
+ questionInput.style.borderRadius = `${RADIUS.sm}px`;
451
+ questionInput.style.backgroundColor = COLORS.surface;
452
+ questionInput.addEventListener("input", () => {
453
+ patchActiveQuestion((question) => ({
454
+ ...question,
455
+ text: questionInput.value
456
+ }));
457
+ });
458
+ const questionLabel = document.createElement("label");
459
+ questionLabel.style.display = "block";
460
+ questionLabel.style.marginBottom = `${SPACE[3]}px`;
461
+ questionLabel.style.fontWeight = "600";
462
+ questionLabel.style.fontSize = `${TYPE.sm}px`;
463
+ questionLabel.style.color = COLORS.textMuted;
464
+ questionLabel.appendChild(document.createTextNode("Question"));
465
+ questionLabel.appendChild(questionInput);
466
+ body.appendChild(questionLabel);
467
+ const questionMediaBox = document.createElement("div");
468
+ questionMediaBox.className = "quiz-question-media";
469
+ questionMediaBox.style.padding = `${SPACE[3]}px`;
470
+ questionMediaBox.style.marginBottom = `${SPACE[4]}px`;
471
+ questionMediaBox.style.backgroundColor = COLORS.surfaceRaised;
472
+ questionMediaBox.style.border = `1px solid ${COLORS.border}`;
473
+ questionMediaBox.style.borderRadius = `${RADIUS.md}px`;
474
+ const questionMediaSrc = document.createElement("input");
475
+ questionMediaSrc.type = "text";
476
+ questionMediaSrc.className = "quiz-question-media-src";
477
+ questionMediaSrc.placeholder = "Question media URL";
478
+ questionMediaSrc.style.display = "block";
479
+ questionMediaSrc.style.width = "100%";
480
+ questionMediaSrc.style.boxSizing = "border-box";
481
+ questionMediaSrc.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
482
+ questionMediaSrc.style.marginBottom = `${SPACE[2]}px`;
483
+ questionMediaSrc.style.fontSize = `${TYPE.base}px`;
484
+ questionMediaSrc.style.color = COLORS.text;
485
+ questionMediaSrc.style.border = `1px solid ${COLORS.borderStrong}`;
486
+ questionMediaSrc.style.borderRadius = `${RADIUS.sm}px`;
487
+ questionMediaSrc.style.backgroundColor = COLORS.surface;
488
+ const questionMediaAlt = document.createElement("input");
489
+ questionMediaAlt.type = "text";
490
+ questionMediaAlt.className = "quiz-question-media-alt";
491
+ questionMediaAlt.placeholder = "Media alt text";
492
+ questionMediaAlt.style.display = "block";
493
+ questionMediaAlt.style.width = "100%";
494
+ questionMediaAlt.style.boxSizing = "border-box";
495
+ questionMediaAlt.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
496
+ questionMediaAlt.style.fontSize = `${TYPE.base}px`;
497
+ questionMediaAlt.style.color = COLORS.text;
498
+ questionMediaAlt.style.border = `1px solid ${COLORS.borderStrong}`;
499
+ questionMediaAlt.style.borderRadius = `${RADIUS.sm}px`;
500
+ questionMediaAlt.style.backgroundColor = COLORS.surface;
501
+ const questionPreviewSlot = document.createElement("div");
502
+ questionPreviewSlot.className = "quiz-question-media-preview-slot";
503
+ const emitQuestionMedia = () => {
504
+ const media = buildMediaRef(questionMediaSrc.value, questionMediaAlt.value);
505
+ syncPreview(questionPreviewSlot, media, "quiz-question-media-preview");
506
+ patchActiveQuestion((question) => ({ ...question, media }));
507
+ };
508
+ questionMediaSrc.addEventListener("input", emitQuestionMedia);
509
+ questionMediaAlt.addEventListener("input", emitQuestionMedia);
510
+ questionMediaBox.append(questionMediaSrc, questionMediaAlt, questionPreviewSlot);
511
+ body.appendChild(questionMediaBox);
512
+ const optionsTitle = document.createElement("div");
513
+ optionsTitle.textContent = "Options";
514
+ optionsTitle.style.marginBottom = `${SPACE[2]}px`;
515
+ optionsTitle.style.fontWeight = "600";
516
+ optionsTitle.style.fontSize = `${TYPE.sm}px`;
517
+ optionsTitle.style.color = COLORS.textMuted;
518
+ body.appendChild(optionsTitle);
519
+ const optionsContainer = document.createElement("div");
520
+ optionsContainer.style.display = "flex";
521
+ optionsContainer.style.flexDirection = "column";
522
+ optionsContainer.style.gap = `${SPACE[2]}px`;
523
+ body.appendChild(optionsContainer);
524
+ const addOptionButton = document.createElement("button");
525
+ addOptionButton.type = "button";
526
+ addOptionButton.className = "quiz-add-option";
527
+ addOptionButton.textContent = "Add option";
528
+ addOptionButton.style.marginTop = `${SPACE[2]}px`;
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;
537
+ addOptionButton.addEventListener("click", () => {
538
+ patchActiveQuestion((question) => {
539
+ const nextOptions = [
540
+ ...question.options,
541
+ {
542
+ id: nextOptionId(question.options, question.options.length),
543
+ text: ""
544
+ }
545
+ ];
546
+ const nextCorrectOptionId = question.options.length === 0 ? nextOptions[0].id : question.correctOptionId;
547
+ return {
548
+ ...question,
549
+ options: nextOptions,
550
+ correctOptionId: nextCorrectOptionId
551
+ };
552
+ });
553
+ });
554
+ body.appendChild(addOptionButton);
555
+ const correctLabel = document.createElement("label");
556
+ correctLabel.style.display = "block";
557
+ correctLabel.style.marginTop = `${SPACE[4]}px`;
558
+ correctLabel.style.fontWeight = "600";
559
+ correctLabel.style.fontSize = `${TYPE.sm}px`;
560
+ correctLabel.style.color = COLORS.textMuted;
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);
581
+ container.appendChild(root);
582
+ const createOptionRow = (option, optionIndex) => {
583
+ const row = document.createElement("div");
584
+ row.className = "quiz-option-row";
585
+ row.style.display = "flex";
586
+ row.style.flexWrap = "wrap";
587
+ row.style.gap = `${SPACE[2]}px`;
588
+ row.style.alignItems = "center";
589
+ row.style.padding = `${SPACE[2]}px`;
590
+ row.style.backgroundColor = COLORS.surfaceRaised;
591
+ row.style.border = `1px solid ${COLORS.border}`;
592
+ row.style.borderRadius = `${RADIUS.md}px`;
593
+ const textInput = document.createElement("input");
594
+ textInput.type = "text";
595
+ textInput.className = "quiz-option-text";
596
+ textInput.style.flex = "1";
597
+ textInput.style.minWidth = "120px";
598
+ textInput.style.padding = `${SPACE[2]}px ${SPACE[3]}px`;
599
+ textInput.style.fontSize = `${TYPE.base}px`;
600
+ textInput.style.color = COLORS.text;
601
+ textInput.style.border = `1px solid ${COLORS.borderStrong}`;
602
+ textInput.style.borderRadius = `${RADIUS.sm}px`;
603
+ textInput.style.backgroundColor = COLORS.surface;
604
+ textInput.value = option.text;
605
+ textInput.addEventListener("input", () => {
606
+ patchActiveQuestion((question) => {
607
+ const nextOptions = question.options.slice();
608
+ nextOptions[optionIndex] = {
609
+ ...nextOptions[optionIndex],
610
+ text: textInput.value
611
+ };
612
+ return { ...question, options: nextOptions };
613
+ });
614
+ });
615
+ const mediaSrcInput = document.createElement("input");
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");
647
+ patchActiveQuestion((question) => {
648
+ const nextOptions = question.options.slice();
649
+ nextOptions[optionIndex] = { ...nextOptions[optionIndex], media };
650
+ return { ...question, options: nextOptions };
651
+ });
652
+ };
653
+ mediaSrcInput.addEventListener("input", emitOptionMedia);
654
+ mediaAltInput.addEventListener("input", emitOptionMedia);
655
+ const remove = document.createElement("button");
656
+ remove.type = "button";
657
+ remove.className = "quiz-option-remove";
658
+ remove.textContent = "Remove";
659
+ remove.style.padding = `${SPACE[1]}px ${SPACE[3]}px`;
660
+ remove.style.backgroundColor = COLORS.errorBg;
661
+ remove.style.border = `1px solid ${COLORS.errorBorder}`;
662
+ remove.style.borderRadius = `${RADIUS.sm}px`;
663
+ remove.style.cursor = "pointer";
664
+ remove.style.fontSize = `${TYPE.sm}px`;
665
+ remove.style.fontWeight = "600";
666
+ remove.style.color = COLORS.errorText;
667
+ remove.addEventListener("click", () => {
668
+ patchActiveQuestion((question) => {
669
+ const removed = question.options[optionIndex];
670
+ const nextOptions = question.options.filter((_, i) => i !== optionIndex);
671
+ return {
672
+ ...question,
673
+ options: nextOptions,
674
+ // Removing the correct option clears correctness; do not shift it.
675
+ correctOptionId: removed && removed.id === question.correctOptionId ? null : question.correctOptionId
676
+ };
677
+ });
678
+ });
679
+ row.append(textInput, mediaSrcInput, mediaAltInput, previewSlot, remove);
680
+ optionsContainer.appendChild(row);
681
+ return { row, textInput, mediaSrcInput, mediaAltInput, previewSlot };
682
+ };
683
+ const selectQuestion = (index) => {
684
+ selectedIndex = index;
685
+ bodyQuestionIndex = -1;
686
+ renderTabs();
687
+ syncActiveBody();
688
+ };
689
+ const removeQuestion = (index) => {
690
+ const next = currentData.questions.filter((_, i) => i !== index);
691
+ const questions = next.length > 0 ? next : [emptyQuestion(nextQuestionId([], 0))];
692
+ if (selectedIndex === index) {
693
+ selectedIndex = 0;
694
+ } else if (index < selectedIndex) {
695
+ selectedIndex -= 1;
696
+ }
697
+ currentData = { questions };
698
+ emit(questions);
699
+ bodyQuestionIndex = -1;
700
+ renderTabs();
701
+ syncActiveBody();
702
+ };
703
+ const renderTabs = () => {
704
+ tabsBar.innerHTML = "";
705
+ currentData.questions.forEach((question, index) => {
706
+ const wrap = document.createElement("div");
707
+ wrap.className = "quiz-question-tab-wrap";
708
+ wrap.style.display = "inline-flex";
709
+ wrap.style.alignItems = "center";
710
+ wrap.style.gap = `${SPACE[1]}px`;
711
+ const tab = document.createElement("button");
712
+ tab.type = "button";
713
+ tab.className = "quiz-question-tab";
714
+ tab.textContent = `Question ${index + 1}`;
715
+ tab.style.padding = `${SPACE[1]}px ${SPACE[3]}px`;
716
+ tab.style.borderRadius = `${RADIUS.sm}px`;
717
+ tab.style.border = `1px solid ${COLORS.borderStrong}`;
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
+ }
729
+ tab.addEventListener("click", () => selectQuestion(index));
730
+ const remove = document.createElement("button");
731
+ remove.type = "button";
732
+ remove.className = "quiz-question-remove";
733
+ remove.textContent = "Remove";
734
+ remove.style.padding = `${SPACE[1]}px ${SPACE[2]}px`;
735
+ remove.style.borderRadius = `${RADIUS.sm}px`;
736
+ remove.style.border = `1px solid ${COLORS.errorBorder}`;
737
+ remove.style.backgroundColor = COLORS.errorBg;
738
+ remove.style.color = COLORS.errorText;
739
+ remove.style.cursor = "pointer";
740
+ remove.style.fontSize = `${TYPE.xs}px`;
741
+ remove.style.fontWeight = "600";
742
+ remove.addEventListener("click", () => removeQuestion(index));
743
+ wrap.append(tab, remove);
744
+ tabsBar.appendChild(wrap);
745
+ });
746
+ };
747
+ const syncActiveBody = () => {
748
+ const active = currentData.questions[selectedIndex];
749
+ if (!active) return;
750
+ questionInput.value = active.text;
751
+ questionMediaSrc.value = active.media?.src ?? "";
752
+ questionMediaAlt.value = active.media?.alt ?? "";
753
+ syncPreview(questionPreviewSlot, active.media, "quiz-question-media-preview");
754
+ const options = active.options;
755
+ if (bodyQuestionIndex !== selectedIndex || optionRows.length !== options.length) {
756
+ optionsContainer.innerHTML = "";
757
+ optionRows = options.map(
758
+ (option, optionIndex) => createOptionRow(option, optionIndex)
759
+ );
760
+ 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
+ }
784
+ };
785
+ const syncTo = (nextData, nextOnChange) => {
786
+ currentData = nextData;
787
+ currentOnChange = nextOnChange;
788
+ if (currentData.questions.length === 0) {
789
+ currentData = { questions: [emptyQuestion("q-0")] };
790
+ }
791
+ if (selectedIndex >= currentData.questions.length) {
792
+ selectedIndex = 0;
793
+ }
794
+ renderTabs();
795
+ syncActiveBody();
796
+ };
797
+ renderTabs();
798
+ syncActiveBody();
799
+ return { syncTo };
800
+ }
801
+ function getOrCreateQuizEditorController(container, data, onChange) {
802
+ let controller = quizEditorControllers.get(container);
803
+ if (!controller) {
804
+ controller = createQuizEditorController(container, data, onChange);
805
+ quizEditorControllers.set(container, controller);
806
+ }
807
+ return controller;
808
+ }
809
+ function unmountQuizEditor(container) {
810
+ quizEditorControllers.delete(container);
811
+ container.innerHTML = "";
812
+ }
813
+ var QuizEditorType = {
814
+ getId: () => "quiz-editor",
815
+ getVersion: () => 1,
816
+ getMaximumScore(data) {
817
+ return data.questions.length;
818
+ },
819
+ async preload() {
820
+ },
821
+ renderEditor(container, data, onChange) {
822
+ getOrCreateQuizEditorController(container, data, onChange).syncTo(
823
+ data,
824
+ onChange
825
+ );
826
+ },
827
+ updateEditor(container, data, onChange) {
828
+ getOrCreateQuizEditorController(container, data, onChange).syncTo(
829
+ data,
830
+ onChange
831
+ );
832
+ },
833
+ renderPlayback(container, data, callbacks) {
834
+ renderQuizPlayback(container, data, callbacks);
835
+ },
836
+ unmountPlayback(container) {
837
+ unmountQuizPlayback(container);
838
+ },
839
+ unmount(container) {
840
+ unmountQuizEditor(container);
841
+ }
842
+ };
843
+
844
+ // src/quiz-content-type.ts
845
+ var QuizContentType = {
846
+ getId: () => "quiz-editor",
847
+ getVersion: () => 1,
848
+ getMaximumScore(data) {
849
+ return data.questions.length;
850
+ },
851
+ async preload() {
852
+ },
853
+ renderEditor(container, data, onChange) {
854
+ QuizEditorType.renderEditor(container, data, onChange);
855
+ },
856
+ updateEditor(container, data, onChange) {
857
+ QuizEditorType.updateEditor?.(container, data, onChange);
858
+ },
859
+ renderPlayback(container, data, callbacks) {
860
+ renderQuizPlayback(container, data, callbacks);
861
+ },
862
+ unmountPlayback(container) {
863
+ unmountQuizPlayback(container);
864
+ },
865
+ unmount(container) {
866
+ unmountQuizEditor(container);
867
+ }
868
+ };
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
+ // Annotate the CommonJS export names for ESM import in node:
901
+ 0 && (module.exports = {
902
+ QuizContentType,
903
+ QuizEditorType,
904
+ renderQuizPlayback,
905
+ unmountQuizEditor,
906
+ unmountQuizPlayback,
907
+ validateQuizData
908
+ });
909
+ //# sourceMappingURL=index.cjs.map