Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support fallback for media src #1490

Merged
merged 5 commits into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions elements/storytelling/src/helpers/render-html-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,48 @@ function processNode(node, initDispatchFunc) {
const lightboxElements = [];

const images = node.querySelectorAll("img");
// Loop over each image
images.forEach((img) => {
const videos = node.querySelectorAll("video");

// Loop over each image/video
[...images, ...videos].forEach((media) => {
// Check if the image is already inside a link (to avoid double wrapping)
const mode = img.getAttribute("mode");
const mode = media.getAttribute("mode");

if (img.parentNode.tagName !== "A" && mode !== "hero") {
img.style.cursor = "zoom-in";
img.addEventListener("click", () => {
if (media.parentNode.tagName !== "A" && mode !== "hero") {
media.style.cursor = "zoom-in";
media.addEventListener("click", () => {
lightboxGallery.open();
});

// Handle media loading error by switching to backup URL if available
media.onerror = () => {
if (
document.body.contains(media) &&
media.getAttribute("data-fallback-src")
) {
media.src = media.getAttribute("data-fallback-src");
media.removeAttribute("data-fallback-src");
} else {
media.src = "https://placehold.co/600x400?text=Media+not+found";
}
};

// Function to clear the backup URL title attribute after successful load
const loadFunc = () => {
if (
document.body.contains(media) &&
media.getAttribute("data-fallback-src")
) {
media.removeAttribute("data-fallback-src");
}
};

media.onload = loadFunc;
media.onloadeddata = loadFunc;

lightboxElements.push({
type: "image",
href: img.src,
href: media.src,
});
}
});
Expand Down
Loading