From 73a1ddf416d9274515cee433d653fa89cbca56f0 Mon Sep 17 00:00:00 2001 From: abdellah Date: Thu, 13 Apr 2023 13:22:40 +0300 Subject: [PATCH] copy embed code on publish --- blocks/scrimba-block.tsx | 53 ++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/blocks/scrimba-block.tsx b/blocks/scrimba-block.tsx index 2ca2a56..1e3d75e 100644 --- a/blocks/scrimba-block.tsx +++ b/blocks/scrimba-block.tsx @@ -1,19 +1,33 @@ import { tw } from "twind"; -import { FolderBlockProps } from "@githubnext/blocks"; +import { FileBlockProps, FolderBlockProps } from "@githubnext/blocks"; import { useCallback, useEffect, useRef, useState } from "react"; -import "./style.css"; -import { Button, Text, Link } from "@primer/react"; +import { Button, Flash } from "@primer/react"; -const isDev = import.meta?.url +const isDev = import.meta?.url; const SCRIMBA_BASE_URL = isDev ? "https://dev.scrimba.com:3000" : "https://scrimba.com"; const SCRIMBA_URL = `${SCRIMBA_BASE_URL}/new/htmlblocks`; -export default function (props: FolderBlockProps) { +function copyToClipboard(text) { + const textArea = document.createElement("textarea"); + textArea.value = text; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + try { + document.execCommand("copy"); + } catch (err) { + console.error("Unable to copy to clipboard", err); + } + document.body.removeChild(textArea); +} + +export default function (props: FolderBlockProps | FileBlockProps) { const { tree, context, onRequestGitHubData, files } = props; const [scrimMounted, setScrimMounted] = useState(false); - const [scrimRecorded, setScrimRecorded] = useState(""); + const [scrimRecorded, setScrimRecorded] = useState(false); + const [scrimEmbedCopied, setScrimEmbedCopied] = useState(false); const frame = useRef(null); @@ -128,13 +142,24 @@ export default function (props: FolderBlockProps) { listenToRecorded(); } }, [frame]); - const publish = useCallback(() => { + const embed = () => { + copyToClipboard( + `` + ); + setScrimEmbedCopied(true); + }; + const publish = () => { sendData({ action: "publish" }, true); - }, [sendData]); + embed(); + }; + const url = `${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`; return (
+ {scrimEmbedCopied ? ( + Scrim embed code copied to clipboard + ) : null}
{scrimMounted ? null : ( @@ -142,18 +167,10 @@ export default function (props: FolderBlockProps) {
{scrimRecorded ? ( - - - to make it available for everyone at{" "} - {`${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`} - + ) : null}