import { useEffect, useRef, useState } from "react"; import { userService } from "../services"; import toImage from "../labs/html2image"; import { ANIMATION_DURATION, IMAGE_URL_REG } from "../helpers/consts"; import * as utils from "../helpers/utils"; import { showDialog } from "./Dialog"; import { formatMemoContent } from "./Memo"; import Only from "./common/OnlyWhen"; import toastHelper from "./Toast"; import "../less/share-memo-image-dialog.less"; interface Props extends DialogProps { memo: Memo; } const ShareMemoImageDialog: React.FC = (props: Props) => { const { memo: propsMemo, destroy } = props; const { user: userinfo } = userService.getState(); const memo = { ...propsMemo, createdAtStr: utils.getDateTimeString(propsMemo.createdTs), }; const memoImgUrls = Array.from(memo.content.match(IMAGE_URL_REG) ?? []); const [shortcutImgUrl, setShortcutImgUrl] = useState(""); const [imgAmount, setImgAmount] = useState(memoImgUrls.length); const memoElRef = useRef(null); useEffect(() => { if (imgAmount > 0) { return; } setTimeout(() => { if (!memoElRef.current) { return; } toImage(memoElRef.current, { backgroundColor: "#eaeaea", pixelRatio: window.devicePixelRatio * 2, }) .then((url) => { setShortcutImgUrl(url); }) .catch(() => { // do nth }); }, ANIMATION_DURATION); }, [imgAmount]); const handleCloseBtnClick = () => { destroy(); }; const handleImageOnLoad = (ev: React.SyntheticEvent) => { if (ev.type === "error") { toastHelper.error("有个图片加载失败了😟"); (ev.target as HTMLImageElement).remove(); } setImgAmount(imgAmount - 1); }; return ( <>

🥰Share Memo

{shortcutImgUrl ? "Right click or long press to save image 👇" : "Generating the screenshot..."}

{memo.createdAtStr}
0}>
{memoImgUrls.map((imgUrl, idx) => ( ))}
✍️ by {userinfo?.name}
); }; export default function showShareMemoImageDialog(memo: Memo): void { showDialog( { className: "share-memo-image-dialog", }, ShareMemoImageDialog, { memo } ); }