Skip to content

Commit

Permalink
add video with fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
zizdlp committed Sep 6, 2024
1 parent 37f1b48 commit 7d1727a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
4 changes: 2 additions & 2 deletions zbook_frontend/src/components/parsers/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function CodeBlock({
}, [codeString, t]);

return (
<div className="relative z-10 my-[1.25em] pb-1 col-span-3 font-base rounded-md dark:bg-slate-800/50 dark:ring-1 ring-slate-200/50 dark:ring-slate-900/10 border-[0.01rem] border-slate-300 dark:border-0">
<div className="relative z-10 my-[1.25em] md:pb-4 pb-2 col-span-3 font-base rounded-md dark:bg-slate-800/50 dark:ring-1 ring-slate-200/50 dark:ring-slate-900/10 border-[0.01rem] border-slate-300 dark:border-0">
<div className="relative py-1 md:py-2 space-x-4 rounded-t-md flex items-center justify-center text-slate-400 text-xs md:text-sm leading-6 border-b-[0.01rem] border-slate-300 dark:border-slate-700/30 dark:bg-slate-800/50">
<div className="relative ml-2 md:ml-4 w-7 h-7 rounded-full text-white flex items-center justify-center">
<AiFillCode className="w-5 h-5 md:w-6 md:h-6 text-slate-500 dark:text-slate-200" />
Expand All @@ -49,7 +49,7 @@ export default function CodeBlock({
/>
</div>
</div>
<div className="highlight px-2 md:px-4 mt-1 text-xs md:text-sm text-slate-800 dark:text-slate-200 scrollbar-thin scrollbar-thumb-rounded-md scrollbar-track-rounded-md overflow-auto">
<div className="highlight px-2 md:px-4 mt-2 md:mt-4 text-xs md:text-sm text-slate-800 dark:text-slate-200 scrollbar-thin scrollbar-thumb-rounded-md scrollbar-track-rounded-md overflow-auto">
{lang === "mermaid" ? (
<CodeMermaid graphDefinition={codeString} />
) : (
Expand Down
1 change: 0 additions & 1 deletion zbook_frontend/src/components/parsers/CodeMermaid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ function CodeMermaid({ graphDefinition }: { graphDefinition: string }) {
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mounted, resolvedTheme, mutationToggleTheme]);

if (!mounted) {
return (
<MdOutlineCode className="w-full h-96 rounded-md py-40 bg-gray-200 dark:bg-gray-700/75 animate-pulse text-slate-500 dark:text-slate-400" />
Expand Down
18 changes: 12 additions & 6 deletions zbook_frontend/src/components/parsers/HtmlParser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ImageWithFallback from "./ImageWithFallback";
import { ThemeColor } from "../TableOfContent";
import { headers } from "next/headers";
import { getAdmonitionType } from "@/utils/util";
import VideoWithFallBack from "./VideoWithFallBack";
interface Attribute {
name: string;
value: string;
Expand Down Expand Up @@ -288,13 +289,18 @@ const parseHTMLString = (
);
} else if (tagName === "IFRAME") {
return (
<iframe
key={randomKey}
<VideoWithFallBack
src={props.src}
className="w-full embed-video my-[1.25em] rounded-md"
>
{Array.from(node.childNodes).map(processNode)}
</iframe>
alt="Landscape picture"
key={randomKey}
/>
// <iframe
// key={randomKey}
// src={props.src}
// className="w-full embed-video my-[1.25em] rounded-md"
// >
// {Array.from(node.childNodes).map(processNode)}
// </iframe>
);
}
} else if (node instanceof window.Text) {
Expand Down
28 changes: 28 additions & 0 deletions zbook_frontend/src/components/parsers/VideoWithFallBack.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use client";
import React, { useState } from "react";
import { FaVideo, FaVideoSlash } from "react-icons/fa";

const VideoWithFallBack = ({ src, alt }: { src: string; alt: string }) => {
const [imgLoaded, setImgLoaded] = useState(false);
const [imgError, setImgError] = useState(false);

return (
<>
{!imgLoaded && !imgError && (
<FaVideo className="w-full my-[1.25em] h-96 rounded-md py-40 bg-gray-200 dark:bg-gray-700/75 animate-pulse text-slate-500 dark:text-slate-400" />
)}
{imgError && (
<FaVideoSlash className="w-full my-[1.25em] h-96 rounded-md py-40 bg-gray-200 dark:bg-gray-700/75 text-slate-500 dark:text-slate-400" />
)}
<iframe
className="w-full embed-video my-[1.25em] rounded-md"
src={src}
style={{ display: imgLoaded && !imgError ? "block" : "none" }}
onLoad={() => setImgLoaded(true)}
onError={() => setImgError(true)}
/>
</>
);
};

export default VideoWithFallBack;

0 comments on commit 7d1727a

Please sign in to comment.