Skip to content

Commit

Permalink
share url to snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerZANP committed Aug 10, 2023
1 parent a720351 commit 5f1aeec
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions examples/web/components/CodeSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import domtoimage from "dom-to-image";
import { useCallback, useEffect, useRef, useState } from "react";
import { AiOutlineDownload } from "react-icons/ai";
import { BsStars } from "react-icons/bs";
import { RiShareBoxLine } from "react-icons/ri";
import { remark } from "remark";
import html from "remark-html";
import { remarkShakuCodeAnnotate } from "remark-shaku-code-annotate";
Expand Down Expand Up @@ -412,6 +413,19 @@ export function CodeSnippet({ code: _code }: { code?: string }) {

return;
};

const share = () => {
const query = "code=" + encodeURIComponent(code);
const url = location.origin + "/snippet?" + query;
const type = "text/plain";
const blob = new Blob([url], { type });
const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then(
() => alert("link copied"),
() => alert("failed to copy link.")
);
};

return (
<Column height={"100vh"} padding={12} gap={12}>
<View>
Expand All @@ -433,7 +447,7 @@ export function CodeSnippet({ code: _code }: { code?: string }) {

<Row gap={10} flex="1 0 0 ">
<View flex="1 0 0" maxWidth={600}>
<View marginBottom={"1.5rem"}>
<Row marginBottom={"1.5rem"}>
<select
value={lang}
// @ts-ignore
Expand All @@ -445,7 +459,12 @@ export function CodeSnippet({ code: _code }: { code?: string }) {
</option>
))}
</select>
</View>
<Button
onClick={share}
label="Share page with below code"
icon={<RiShareBoxLine />}
></Button>
</Row>
<Editor
language={lang}
height="100%"
Expand Down

1 comment on commit 5f1aeec

@vercel
Copy link

@vercel vercel bot commented on 5f1aeec Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shaku-web – ./

shaku-web.vercel.app
shaku-web-jserzanp.vercel.app
shaku-web-git-main-jserzanp.vercel.app

Please sign in to comment.