@interlacejs/quiz 0.2.2 → 0.3.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 +262 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +262 -32
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -318,6 +318,66 @@ function getQuizAuthoringIssues(data) {
|
|
|
318
318
|
return issues;
|
|
319
319
|
}
|
|
320
320
|
|
|
321
|
+
// src/image-file.ts
|
|
322
|
+
var MAX_IMAGE_UPLOAD_MB = 10;
|
|
323
|
+
var KEEP_AS_IS_BYTES = 200 * 1024;
|
|
324
|
+
var MAX_DIMENSION = 1280;
|
|
325
|
+
var MAX_DATA_URL_LENGTH = 1.4 * 1024 * 1024;
|
|
326
|
+
var TOO_LARGE = "This image is too large. Try a smaller one, or use a URL.";
|
|
327
|
+
async function imageFileToDataUrl(file) {
|
|
328
|
+
if (!file.type.startsWith("image/")) {
|
|
329
|
+
throw new Error("Choose an image file.");
|
|
330
|
+
}
|
|
331
|
+
if (file.size > MAX_IMAGE_UPLOAD_MB * 1024 * 1024) {
|
|
332
|
+
throw new Error(`The image must be ${MAX_IMAGE_UPLOAD_MB} MB or smaller.`);
|
|
333
|
+
}
|
|
334
|
+
let url;
|
|
335
|
+
if (file.size <= KEEP_AS_IS_BYTES || file.type === "image/svg+xml") {
|
|
336
|
+
url = await readAsDataUrl(file);
|
|
337
|
+
} else {
|
|
338
|
+
url = await resize(file);
|
|
339
|
+
}
|
|
340
|
+
if (url.length > MAX_DATA_URL_LENGTH) throw new Error(TOO_LARGE);
|
|
341
|
+
return url;
|
|
342
|
+
}
|
|
343
|
+
function readAsDataUrl(file) {
|
|
344
|
+
return new Promise((resolve, reject) => {
|
|
345
|
+
const reader = new FileReader();
|
|
346
|
+
reader.onload = () => resolve(String(reader.result));
|
|
347
|
+
reader.onerror = () => reject(new Error("Couldn't read that image."));
|
|
348
|
+
reader.readAsDataURL(file);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
async function resize(file) {
|
|
352
|
+
const image = await loadImage(file);
|
|
353
|
+
const longest = Math.max(image.naturalWidth, image.naturalHeight);
|
|
354
|
+
const scale = Math.min(1, MAX_DIMENSION / longest);
|
|
355
|
+
const canvas = document.createElement("canvas");
|
|
356
|
+
canvas.width = Math.max(1, Math.round(image.naturalWidth * scale));
|
|
357
|
+
canvas.height = Math.max(1, Math.round(image.naturalHeight * scale));
|
|
358
|
+
const context = canvas.getContext("2d");
|
|
359
|
+
if (!context) throw new Error("Couldn't read that image.");
|
|
360
|
+
context.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
361
|
+
const type = file.type === "image/jpeg" ? "image/jpeg" : "image/webp";
|
|
362
|
+
const url = canvas.toDataURL(type, 0.85);
|
|
363
|
+
return url.length <= MAX_DATA_URL_LENGTH ? url : canvas.toDataURL("image/jpeg", 0.8);
|
|
364
|
+
}
|
|
365
|
+
function loadImage(file) {
|
|
366
|
+
return new Promise((resolve, reject) => {
|
|
367
|
+
const objectUrl = URL.createObjectURL(file);
|
|
368
|
+
const image = new Image();
|
|
369
|
+
image.onload = () => {
|
|
370
|
+
URL.revokeObjectURL(objectUrl);
|
|
371
|
+
resolve(image);
|
|
372
|
+
};
|
|
373
|
+
image.onerror = () => {
|
|
374
|
+
URL.revokeObjectURL(objectUrl);
|
|
375
|
+
reject(new Error("Couldn't read that image."));
|
|
376
|
+
};
|
|
377
|
+
image.src = objectUrl;
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
321
381
|
// src/quiz-editor.ts
|
|
322
382
|
var quizEditorControllers = /* @__PURE__ */ new WeakMap();
|
|
323
383
|
var editorInstanceCount = 0;
|
|
@@ -332,10 +392,8 @@ function createPreview(media, className) {
|
|
|
332
392
|
img.className = className;
|
|
333
393
|
img.src = media.src;
|
|
334
394
|
img.style.maxWidth = "100%";
|
|
335
|
-
img.style.maxHeight = "
|
|
336
|
-
img.style.
|
|
337
|
-
img.style.borderRadius = `${RADIUS.sm}`;
|
|
338
|
-
img.style.marginTop = `${SPACE[1]}px`;
|
|
395
|
+
img.style.maxHeight = "100%";
|
|
396
|
+
img.style.objectFit = "contain";
|
|
339
397
|
if (media.alt) img.alt = media.alt;
|
|
340
398
|
return img;
|
|
341
399
|
}
|
|
@@ -448,6 +506,18 @@ function setFieldError(input, error, message) {
|
|
|
448
506
|
input.removeAttribute("aria-describedby");
|
|
449
507
|
}
|
|
450
508
|
}
|
|
509
|
+
function isDataUrl(src) {
|
|
510
|
+
return src.startsWith("data:");
|
|
511
|
+
}
|
|
512
|
+
function createLinkButton(text, className) {
|
|
513
|
+
const button = createButton(text, "ghost", className);
|
|
514
|
+
button.style.padding = `0 ${SPACE[1]}px`;
|
|
515
|
+
button.style.minHeight = "0";
|
|
516
|
+
button.style.height = "auto";
|
|
517
|
+
button.style.color = COLORS.accent;
|
|
518
|
+
button.style.fontSize = `${TYPE.sm}px`;
|
|
519
|
+
return button;
|
|
520
|
+
}
|
|
451
521
|
function createMediaEditor(options) {
|
|
452
522
|
const { classPrefix, idPrefix } = options;
|
|
453
523
|
const root = document.createElement("div");
|
|
@@ -462,65 +532,225 @@ function createMediaEditor(options) {
|
|
|
462
532
|
panel.className = `${classPrefix}-panel`;
|
|
463
533
|
panel.style.flexDirection = "column";
|
|
464
534
|
panel.style.gap = `${SPACE[2]}px`;
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
535
|
+
const fileInput = document.createElement("input");
|
|
536
|
+
fileInput.type = "file";
|
|
537
|
+
fileInput.accept = "image/*";
|
|
538
|
+
fileInput.className = `${classPrefix}-file`;
|
|
539
|
+
fileInput.tabIndex = -1;
|
|
540
|
+
fileInput.setAttribute("aria-hidden", "true");
|
|
541
|
+
setShown(fileInput, false);
|
|
542
|
+
const dropzone = document.createElement("div");
|
|
543
|
+
dropzone.className = `${classPrefix}-dropzone`;
|
|
544
|
+
dropzone.style.flexDirection = "column";
|
|
545
|
+
dropzone.style.alignItems = "center";
|
|
546
|
+
dropzone.style.gap = `${SPACE[1]}px`;
|
|
547
|
+
dropzone.style.padding = `${SPACE[4]}px ${SPACE[3]}px`;
|
|
548
|
+
dropzone.style.border = `1px dashed ${COLORS.borderStrong}`;
|
|
549
|
+
dropzone.style.borderRadius = `${RADIUS.md}`;
|
|
550
|
+
dropzone.style.backgroundColor = COLORS.surfaceRaised;
|
|
551
|
+
dropzone.style.textAlign = "center";
|
|
552
|
+
dropzone.style.cursor = "pointer";
|
|
553
|
+
dropzone.style.transition = "border-color 120ms, background-color 120ms";
|
|
554
|
+
const dropText = document.createElement("p");
|
|
555
|
+
dropText.style.margin = "0";
|
|
556
|
+
dropText.style.fontSize = `${TYPE.sm}px`;
|
|
557
|
+
dropText.style.color = COLORS.text;
|
|
558
|
+
const uploadButton = createLinkButton("browse", `${classPrefix}-upload`);
|
|
559
|
+
uploadButton.style.textDecoration = "underline";
|
|
560
|
+
uploadButton.style.fontWeight = "600";
|
|
561
|
+
dropText.append("Drag an image here, or ", uploadButton);
|
|
562
|
+
const dropHint = document.createElement("p");
|
|
563
|
+
dropHint.className = "interlace-hint";
|
|
564
|
+
dropHint.style.margin = "0";
|
|
565
|
+
dropHint.textContent = `JPG, PNG, GIF, WebP or SVG, up to ${MAX_IMAGE_UPLOAD_MB} MB`;
|
|
566
|
+
const busyText = document.createElement("p");
|
|
567
|
+
busyText.className = "interlace-hint";
|
|
568
|
+
busyText.style.margin = "0";
|
|
569
|
+
busyText.textContent = "Adding image\u2026";
|
|
570
|
+
busyText.setAttribute("role", "status");
|
|
571
|
+
setShown(busyText, false);
|
|
572
|
+
dropzone.append(dropText, dropHint, busyText);
|
|
469
573
|
const srcInput = createInput(
|
|
470
574
|
`${classPrefix}-src`,
|
|
471
575
|
`${idPrefix}-src`,
|
|
472
576
|
"https://example.com/image.png"
|
|
473
577
|
);
|
|
578
|
+
const urlField = createField("Image URL", srcInput);
|
|
579
|
+
const card = document.createElement("div");
|
|
580
|
+
card.className = `${classPrefix}-card`;
|
|
581
|
+
card.style.gap = `${SPACE[3]}px`;
|
|
582
|
+
card.style.alignItems = "flex-start";
|
|
583
|
+
card.style.padding = `${SPACE[2]}px`;
|
|
584
|
+
card.style.border = `1px solid ${COLORS.border}`;
|
|
585
|
+
card.style.borderRadius = `${RADIUS.md}`;
|
|
586
|
+
card.style.backgroundColor = COLORS.surfaceRaised;
|
|
587
|
+
const previewSlot = document.createElement("div");
|
|
588
|
+
previewSlot.className = options.previewSlotClass;
|
|
589
|
+
previewSlot.style.flex = "0 0 auto";
|
|
590
|
+
previewSlot.style.width = "112px";
|
|
591
|
+
previewSlot.style.height = "84px";
|
|
592
|
+
previewSlot.style.display = "flex";
|
|
593
|
+
previewSlot.style.alignItems = "center";
|
|
594
|
+
previewSlot.style.justifyContent = "center";
|
|
595
|
+
previewSlot.style.overflow = "hidden";
|
|
596
|
+
previewSlot.style.borderRadius = `${RADIUS.sm}`;
|
|
597
|
+
previewSlot.style.backgroundColor = COLORS.surface;
|
|
598
|
+
previewSlot.style.border = `1px solid ${COLORS.border}`;
|
|
599
|
+
previewSlot.style.color = COLORS.textMuted;
|
|
600
|
+
previewSlot.style.fontSize = `${TYPE.sm}px`;
|
|
601
|
+
previewSlot.style.textAlign = "center";
|
|
474
602
|
const altInput = createInput(
|
|
475
603
|
`${classPrefix}-alt`,
|
|
476
604
|
`${idPrefix}-alt`,
|
|
477
605
|
"Describe the image for screen readers"
|
|
478
606
|
);
|
|
479
|
-
const
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
);
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
607
|
+
const replaceButton = createLinkButton("Replace", `${classPrefix}-replace`);
|
|
608
|
+
const removeButton = createLinkButton("Remove", `${classPrefix}-remove`);
|
|
609
|
+
removeButton.style.color = COLORS.errorText;
|
|
610
|
+
const actions = document.createElement("div");
|
|
611
|
+
actions.style.display = "flex";
|
|
612
|
+
actions.style.gap = `${SPACE[2]}px`;
|
|
613
|
+
actions.append(replaceButton, removeButton);
|
|
614
|
+
const details = document.createElement("div");
|
|
615
|
+
details.style.flex = "1 1 auto";
|
|
616
|
+
details.style.minWidth = "0";
|
|
617
|
+
details.style.display = "flex";
|
|
618
|
+
details.style.flexDirection = "column";
|
|
619
|
+
details.style.gap = `${SPACE[1]}px`;
|
|
620
|
+
const altField = createField("Alt text", altInput);
|
|
621
|
+
details.append(urlField, altField, actions);
|
|
622
|
+
card.append(previewSlot, details);
|
|
623
|
+
const uploadError = createErrorText(
|
|
624
|
+
`${idPrefix}-upload-error`,
|
|
625
|
+
`${classPrefix}-upload-error`
|
|
492
626
|
);
|
|
627
|
+
uploadError.setAttribute("role", "alert");
|
|
628
|
+
const modeButton = createLinkButton("", `${classPrefix}-mode`);
|
|
629
|
+
const cancelButton = createLinkButton("Cancel", `${classPrefix}-cancel`);
|
|
630
|
+
cancelButton.style.color = COLORS.textMuted;
|
|
631
|
+
const links = document.createElement("div");
|
|
632
|
+
links.style.justifyContent = "space-between";
|
|
633
|
+
links.append(modeButton, cancelButton);
|
|
634
|
+
panel.append(fileInput, dropzone, card, uploadError, links);
|
|
493
635
|
root.append(addButton, panel);
|
|
636
|
+
let src = "";
|
|
637
|
+
let urlMode = false;
|
|
638
|
+
const render = () => {
|
|
639
|
+
const hasImage = src.length > 0;
|
|
640
|
+
const img = previewSlot.querySelector("img");
|
|
641
|
+
if (!img) previewSlot.textContent = "Preview";
|
|
642
|
+
else
|
|
643
|
+
img.onerror = () => {
|
|
644
|
+
previewSlot.textContent = "Can't load this image";
|
|
645
|
+
};
|
|
646
|
+
setShown(dropzone, !hasImage && !urlMode, "flex");
|
|
647
|
+
setShown(card, hasImage || urlMode, "flex");
|
|
648
|
+
setShown(urlField, urlMode || hasImage && !isDataUrl(src), "flex");
|
|
649
|
+
setShown(altField, hasImage, "flex");
|
|
650
|
+
setShown(actions, hasImage, "flex");
|
|
651
|
+
setShown(links, !hasImage, "flex");
|
|
652
|
+
modeButton.textContent = urlMode ? "Upload an image instead" : "Use an image URL instead";
|
|
653
|
+
};
|
|
494
654
|
const setExpanded = (expanded) => {
|
|
495
655
|
setShown(panel, expanded, "flex");
|
|
496
656
|
setShown(addButton, !expanded);
|
|
497
657
|
addButton.setAttribute("aria-expanded", String(expanded));
|
|
498
658
|
};
|
|
499
659
|
setExpanded(false);
|
|
500
|
-
|
|
501
|
-
|
|
660
|
+
render();
|
|
661
|
+
const setUploadError = (message) => {
|
|
662
|
+
uploadError.textContent = message ?? "";
|
|
663
|
+
setShown(uploadError, message !== null);
|
|
664
|
+
};
|
|
665
|
+
const emit = () => {
|
|
666
|
+
const media = buildMediaRef(src, altInput.value);
|
|
502
667
|
syncPreview(previewSlot, media, options.previewClass);
|
|
668
|
+
render();
|
|
503
669
|
options.onEdit(media);
|
|
504
670
|
};
|
|
505
|
-
srcInput.addEventListener("input",
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
options.onExpand();
|
|
510
|
-
srcInput.focus();
|
|
671
|
+
srcInput.addEventListener("input", () => {
|
|
672
|
+
src = srcInput.value;
|
|
673
|
+
setUploadError(null);
|
|
674
|
+
emit();
|
|
511
675
|
});
|
|
512
|
-
|
|
676
|
+
altInput.addEventListener("input", emit);
|
|
677
|
+
const useFile = async (file) => {
|
|
678
|
+
if (!file) return;
|
|
679
|
+
setUploadError(null);
|
|
680
|
+
setShown(busyText, true);
|
|
681
|
+
try {
|
|
682
|
+
src = await imageFileToDataUrl(file);
|
|
683
|
+
urlMode = false;
|
|
684
|
+
srcInput.value = "";
|
|
685
|
+
emit();
|
|
686
|
+
} catch (error) {
|
|
687
|
+
setUploadError(
|
|
688
|
+
error instanceof Error ? error.message : "Couldn't use that image."
|
|
689
|
+
);
|
|
690
|
+
} finally {
|
|
691
|
+
setShown(busyText, false);
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
fileInput.addEventListener("change", () => {
|
|
695
|
+
const file = fileInput.files?.[0];
|
|
696
|
+
fileInput.value = "";
|
|
697
|
+
void useFile(file);
|
|
698
|
+
});
|
|
699
|
+
dropzone.addEventListener("click", () => fileInput.click());
|
|
700
|
+
uploadButton.addEventListener("click", (event) => {
|
|
701
|
+
event.stopPropagation();
|
|
702
|
+
fileInput.click();
|
|
703
|
+
});
|
|
704
|
+
replaceButton.addEventListener("click", () => fileInput.click());
|
|
705
|
+
const setDragOver = (over) => {
|
|
706
|
+
dropzone.style.borderColor = over ? COLORS.accent : COLORS.borderStrong;
|
|
707
|
+
dropzone.style.backgroundColor = over ? COLORS.accentLighter : COLORS.surfaceRaised;
|
|
708
|
+
};
|
|
709
|
+
dropzone.addEventListener("dragover", (event) => {
|
|
710
|
+
event.preventDefault();
|
|
711
|
+
setDragOver(true);
|
|
712
|
+
});
|
|
713
|
+
dropzone.addEventListener("dragleave", () => setDragOver(false));
|
|
714
|
+
dropzone.addEventListener("drop", (event) => {
|
|
715
|
+
event.preventDefault();
|
|
716
|
+
setDragOver(false);
|
|
717
|
+
void useFile(event.dataTransfer?.files?.[0]);
|
|
718
|
+
});
|
|
719
|
+
modeButton.addEventListener("click", () => {
|
|
720
|
+
urlMode = !urlMode;
|
|
721
|
+
setUploadError(null);
|
|
722
|
+
render();
|
|
723
|
+
if (urlMode) srcInput.focus();
|
|
724
|
+
else uploadButton.focus();
|
|
725
|
+
});
|
|
726
|
+
const clear = () => {
|
|
727
|
+
src = "";
|
|
728
|
+
urlMode = false;
|
|
513
729
|
srcInput.value = "";
|
|
514
730
|
altInput.value = "";
|
|
731
|
+
setUploadError(null);
|
|
515
732
|
syncPreview(previewSlot, void 0, options.previewClass);
|
|
733
|
+
render();
|
|
516
734
|
setExpanded(false);
|
|
517
|
-
options.onRemove();
|
|
518
735
|
addButton.focus();
|
|
736
|
+
};
|
|
737
|
+
cancelButton.addEventListener("click", clear);
|
|
738
|
+
removeButton.addEventListener("click", () => {
|
|
739
|
+
clear();
|
|
740
|
+
options.onRemove();
|
|
741
|
+
});
|
|
742
|
+
addButton.addEventListener("click", () => {
|
|
743
|
+
setExpanded(true);
|
|
744
|
+
options.onExpand();
|
|
745
|
+
uploadButton.focus();
|
|
519
746
|
});
|
|
520
747
|
const sync = (media, expanded) => {
|
|
521
|
-
|
|
748
|
+
src = media?.src ?? "";
|
|
749
|
+
setValue(srcInput, isDataUrl(src) ? "" : src);
|
|
522
750
|
setValue(altInput, media?.alt ?? "");
|
|
523
751
|
syncPreview(previewSlot, media, options.previewClass);
|
|
752
|
+
urlMode = src.length > 0 && !isDataUrl(src);
|
|
753
|
+
render();
|
|
524
754
|
setExpanded(expanded);
|
|
525
755
|
};
|
|
526
756
|
return { root, srcInput, altInput, previewSlot, sync };
|