|
| 1 | +import { useEffect, useState, useRef, useMemo } from "react"; |
| 2 | +import { useParams } from "react-router"; |
| 3 | +import { useWindowSize } from "@/app/utils"; |
| 4 | +import { IconButton } from "./button"; |
| 5 | +import { nanoid } from "nanoid"; |
| 6 | +import ExportIcon from "../icons/share.svg"; |
| 7 | +import CopyIcon from "../icons/copy.svg"; |
| 8 | +import DownloadIcon from "../icons/download.svg"; |
| 9 | +import GithubIcon from "../icons/github.svg"; |
| 10 | +import Locale from "../locales"; |
| 11 | +import { Modal, showToast } from "./ui-lib"; |
| 12 | +import { copyToClipboard, downloadAs } from "../utils"; |
| 13 | +import { Path, ApiPath, REPO_URL } from "@/app/constant"; |
| 14 | +import { Loading } from "./home"; |
| 15 | + |
| 16 | +export function HTMLPreview(props: { |
| 17 | + code: string; |
| 18 | + autoHeight?: boolean; |
| 19 | + height?: number; |
| 20 | +}) { |
| 21 | + const ref = useRef<HTMLIFrameElement>(null); |
| 22 | + const frameId = useRef<string>(nanoid()); |
| 23 | + const [iframeHeight, setIframeHeight] = useState(600); |
| 24 | + /* |
| 25 | + * https://stackoverflow.com/questions/19739001/what-is-the-difference-between-srcdoc-and-src-datatext-html-in-an |
| 26 | + * 1. using srcdoc |
| 27 | + * 2. using src with dataurl: |
| 28 | + * easy to share |
| 29 | + * length limit (Data URIs cannot be larger than 32,768 characters.) |
| 30 | + */ |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + window.addEventListener("message", (e) => { |
| 34 | + const { id, height } = e.data; |
| 35 | + if (id == frameId.current) { |
| 36 | + console.log("setHeight", height); |
| 37 | + setIframeHeight(height); |
| 38 | + } |
| 39 | + }); |
| 40 | + }, []); |
| 41 | + |
| 42 | + const height = useMemo(() => { |
| 43 | + const parentHeight = props.height || 600; |
| 44 | + if (props.autoHeight !== false) { |
| 45 | + return iframeHeight > parentHeight ? parentHeight : iframeHeight + 40; |
| 46 | + } else { |
| 47 | + return parentHeight; |
| 48 | + } |
| 49 | + }, [props.autoHeight, props.height, iframeHeight]); |
| 50 | + |
| 51 | + const srcDoc = useMemo(() => { |
| 52 | + const script = `<script>new ResizeObserver((entries) => parent.postMessage({id: '${frameId.current}', height: entries[0].target.clientHeight}, '*')).observe(document.body)</script>`; |
| 53 | + if (props.code.includes("</head>")) { |
| 54 | + props.code.replace("</head>", "</head>" + script); |
| 55 | + } |
| 56 | + return props.code + script; |
| 57 | + }, [props.code]); |
| 58 | + |
| 59 | + return ( |
| 60 | + <iframe |
| 61 | + id={frameId.current} |
| 62 | + ref={ref} |
| 63 | + frameBorder={0} |
| 64 | + sandbox="allow-forms allow-modals allow-scripts" |
| 65 | + style={{ width: "100%", height }} |
| 66 | + // src={`data:text/html,${encodeURIComponent(srcDoc)}`} |
| 67 | + srcDoc={srcDoc} |
| 68 | + ></iframe> |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +export function ArtifactShareButton({ getCode, id, style }) { |
| 73 | + const [name, setName] = useState(id); |
| 74 | + const [show, setShow] = useState(false); |
| 75 | + const shareUrl = useMemo(() => |
| 76 | + [location.origin, "#", Path.Artifact, "/", name].join(""), |
| 77 | + ); |
| 78 | + const upload = (code) => |
| 79 | + fetch(ApiPath.Artifact, { |
| 80 | + method: "POST", |
| 81 | + body: getCode(), |
| 82 | + }) |
| 83 | + .then((res) => res.json()) |
| 84 | + .then(({ id }) => { |
| 85 | + if (id) { |
| 86 | + setShow(true); |
| 87 | + return setName(id); |
| 88 | + } |
| 89 | + throw Error(); |
| 90 | + }) |
| 91 | + .catch((e) => { |
| 92 | + showToast(Locale.Export.Artifact.Error); |
| 93 | + }); |
| 94 | + return ( |
| 95 | + <> |
| 96 | + <div className="window-action-button" style={style}> |
| 97 | + <IconButton |
| 98 | + icon={<ExportIcon />} |
| 99 | + bordered |
| 100 | + title={Locale.Export.Artifact.Title} |
| 101 | + onClick={() => { |
| 102 | + upload(getCode()); |
| 103 | + }} |
| 104 | + /> |
| 105 | + </div> |
| 106 | + {show && ( |
| 107 | + <div className="modal-mask"> |
| 108 | + <Modal |
| 109 | + title={Locale.Export.Artifact.Title} |
| 110 | + onClose={() => setShow(false)} |
| 111 | + actions={[ |
| 112 | + <IconButton |
| 113 | + key="download" |
| 114 | + icon={<DownloadIcon />} |
| 115 | + bordered |
| 116 | + text={Locale.Export.Download} |
| 117 | + onClick={() => { |
| 118 | + downloadAs(getCode(), `${id}.html`).then(() => |
| 119 | + setShow(false), |
| 120 | + ); |
| 121 | + }} |
| 122 | + />, |
| 123 | + <IconButton |
| 124 | + key="copy" |
| 125 | + icon={<CopyIcon />} |
| 126 | + bordered |
| 127 | + text={Locale.Chat.Actions.Copy} |
| 128 | + onClick={() => { |
| 129 | + copyToClipboard(shareUrl).then(() => setShow(false)); |
| 130 | + }} |
| 131 | + />, |
| 132 | + ]} |
| 133 | + > |
| 134 | + <div> |
| 135 | + <a target="_blank" href={shareUrl}> |
| 136 | + {shareUrl} |
| 137 | + </a> |
| 138 | + </div> |
| 139 | + </Modal> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + </> |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +export function Artifact() { |
| 147 | + const { id } = useParams(); |
| 148 | + const [code, setCode] = useState(""); |
| 149 | + const { height } = useWindowSize(); |
| 150 | + |
| 151 | + useEffect(() => { |
| 152 | + if (id) { |
| 153 | + fetch(`${ApiPath.Artifact}?id=${id}`) |
| 154 | + .then((res) => res.text()) |
| 155 | + .then(setCode); |
| 156 | + } |
| 157 | + }, [id]); |
| 158 | + |
| 159 | + return ( |
| 160 | + <div |
| 161 | + style={{ |
| 162 | + disply: "block", |
| 163 | + width: "100%", |
| 164 | + height: "100%", |
| 165 | + position: "relative", |
| 166 | + }} |
| 167 | + > |
| 168 | + <div |
| 169 | + style={{ |
| 170 | + height: 40, |
| 171 | + display: "flex", |
| 172 | + alignItems: "center", |
| 173 | + padding: 12, |
| 174 | + }} |
| 175 | + > |
| 176 | + <div style={{ flex: 1 }}> |
| 177 | + <a href={REPO_URL} target="_blank" rel="noopener noreferrer"> |
| 178 | + <IconButton bordered icon={<GithubIcon />} shadow /> |
| 179 | + </a> |
| 180 | + </div> |
| 181 | + <ArtifactShareButton id={id} getCode={() => code} /> |
| 182 | + </div> |
| 183 | + {code ? ( |
| 184 | + <HTMLPreview code={code} autoHeight={false} height={height - 40} /> |
| 185 | + ) : ( |
| 186 | + <Loading /> |
| 187 | + )} |
| 188 | + </div> |
| 189 | + ); |
| 190 | +} |
0 commit comments