|
1 | 1 | import { tw } from "twind"; |
2 | | -import { FolderBlockProps } from "@githubnext/blocks"; |
| 2 | +import { FileBlockProps, FolderBlockProps } from "@githubnext/blocks"; |
3 | 3 | import { useCallback, useEffect, useRef, useState } from "react"; |
4 | | -import "./style.css"; |
5 | | -import { Button, Text, Link } from "@primer/react"; |
| 4 | +import { Button, Flash } from "@primer/react"; |
6 | 5 |
|
7 | | -const isDev = import.meta?.url |
| 6 | +const isDev = import.meta?.url; |
8 | 7 | const SCRIMBA_BASE_URL = isDev |
9 | 8 | ? "https://dev.scrimba.com:3000" |
10 | 9 | : "https://scrimba.com"; |
11 | 10 | const SCRIMBA_URL = `${SCRIMBA_BASE_URL}/new/htmlblocks`; |
12 | 11 |
|
13 | | -export default function (props: FolderBlockProps) { |
| 12 | +function copyToClipboard(text) { |
| 13 | + const textArea = document.createElement("textarea"); |
| 14 | + textArea.value = text; |
| 15 | + document.body.appendChild(textArea); |
| 16 | + textArea.focus(); |
| 17 | + textArea.select(); |
| 18 | + try { |
| 19 | + document.execCommand("copy"); |
| 20 | + } catch (err) { |
| 21 | + console.error("Unable to copy to clipboard", err); |
| 22 | + } |
| 23 | + document.body.removeChild(textArea); |
| 24 | +} |
| 25 | + |
| 26 | +export default function (props: FolderBlockProps | FileBlockProps) { |
14 | 27 | const { tree, context, onRequestGitHubData, files } = props; |
15 | 28 | const [scrimMounted, setScrimMounted] = useState(false); |
16 | | - const [scrimRecorded, setScrimRecorded] = useState(""); |
| 29 | + const [scrimRecorded, setScrimRecorded] = useState(false); |
| 30 | + const [scrimEmbedCopied, setScrimEmbedCopied] = useState(false); |
17 | 31 |
|
18 | 32 | const frame = useRef(null); |
19 | 33 |
|
@@ -128,32 +142,35 @@ export default function (props: FolderBlockProps) { |
128 | 142 | listenToRecorded(); |
129 | 143 | } |
130 | 144 | }, [frame]); |
131 | | - const publish = useCallback(() => { |
| 145 | + const embed = () => { |
| 146 | + copyToClipboard( |
| 147 | + `<iframe src="${url}" width="100%" height="700px"></iframe>` |
| 148 | + ); |
| 149 | + setScrimEmbedCopied(true); |
| 150 | + }; |
| 151 | + const publish = () => { |
132 | 152 | sendData({ action: "publish" }, true); |
133 | | - }, [sendData]); |
| 153 | + embed(); |
| 154 | + }; |
134 | 155 |
|
| 156 | + const url = `${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`; |
135 | 157 | return ( |
136 | 158 | <div className={tw(`w-full h-full`)} id="example-block-code-block"> |
137 | 159 | <div className={tw(`flex w-full h-full overflow-x-hidden flex-col`)}> |
| 160 | + {scrimEmbedCopied ? ( |
| 161 | + <Flash variant="success">Scrim embed code copied to clipboard</Flash> |
| 162 | + ) : null} |
138 | 163 | <div className={tw(`flex ai-center jc-between flex-row`)}> |
139 | 164 | {scrimMounted ? null : ( |
140 | 165 | <Button onClick={reload}>Not working? Reload</Button> |
141 | 166 | )} |
142 | 167 | <Button onClick={loadFiles}>Load all files</Button> |
143 | 168 | </div> |
144 | 169 | {scrimRecorded ? ( |
145 | | - <Text> |
146 | | - <Button onClick={publish} className={tw("inline-block")}> |
147 | | - Publish the scrim{" "} |
148 | | - </Button> |
149 | | - to make it available for everyone at{" "} |
150 | | - <Link |
151 | | - href={`${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`} |
152 | | - >{`${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`}</Link> |
153 | | - </Text> |
| 170 | + <Button onClick={publish}>Publish and copy embed</Button> |
154 | 171 | ) : null} |
155 | 172 | <iframe |
156 | | - allow="microphone;" |
| 173 | + allow="microphone;fullscreen;" |
157 | 174 | ref={frame} |
158 | 175 | src={SCRIMBA_URL} |
159 | 176 | className={tw`my-2 h-full w-full`} |
|
0 commit comments