/
Playground.tsx
69 lines (63 loc) · 2.17 KB
/
Playground.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as React from 'react';
import { useState } from 'react';
import { LiveProvider, LiveError, LivePreview } from 'react-live';
import copy from 'copy-text-to-clipboard';
import Editor from 'react-simple-code-editor';
import dracula from "prism-react-renderer/themes/dracula";
import HighlightImported, { defaultProps } from 'prism-react-renderer';
import { Copy, styles } from '@papyrum/ui'
import { Wrapper, LivePreviewWrapper, EditorWrapper } from './styled';
import { useContext } from 'react';
import { contextDB } from '@papyrum/ui';
export const Playground = ({ code: initialCode, scope }) => {
const [ code, setCode ] = useState(initialCode);
const [ clip, setClip ] = useState(false);
const { db: { config } } = useContext(contextDB as any);
const stylesPlain = (config.prism && config.prism.theme.plain) || dracula.plain;
const handleClipboard = () => {
copy(code);
setClip(true);
setTimeout(() => setClip(false), 500);
};
const highlight = code => (
<HighlightImported
{...defaultProps}
theme={(config.prism && config.prism.theme) || dracula as any}
code={code}
language="jsx"
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<React.Fragment>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
{line.map((token, key) => (
<span {...getTokenProps({ token, key })} />
))}
</div>
))}
</React.Fragment>
)}
</HighlightImported>
);
return(
<LiveProvider code={code} scope={scope}>
<LivePreviewWrapper>
<LivePreview />
<LiveError />
</LivePreviewWrapper>
<Wrapper>
live editor
</Wrapper>
<EditorWrapper>
<Copy onClick={handleClipboard} color={stylesPlain.backgroundColor}>{clip ? 'Copied' : 'Copy'}</Copy>
<Editor
value={code}
onValueChange={code => setCode(code)}
highlight={highlight}
padding={20}
style={{ ...styles, ...stylesPlain, borderBottomLeftRadius: '5px', borderBottomRightRadius: '5px' }}
/>
</EditorWrapper>
</LiveProvider>
);
};