matthoffner commited on
Commit
74d34b9
·
1 Parent(s): b016e58

Update components/Markdown/CodeBlock.tsx

Browse files
Files changed (1) hide show
  1. components/Markdown/CodeBlock.tsx +20 -1
components/Markdown/CodeBlock.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import { IconCheck, IconClipboard, IconDownload } from '@tabler/icons-react';
2
  import { FC, memo, useState } from 'react';
3
  import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
4
  import { oneDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
@@ -18,6 +18,11 @@ interface Props {
18
  export const CodeBlock: FC<Props> = memo(({ language, value }) => {
19
  const { t } = useTranslation('markdown');
20
  const [isCopied, setIsCopied] = useState<Boolean>(false);
 
 
 
 
 
21
 
22
  const copyToClipboard = () => {
23
  if (!navigator.clipboard || !navigator.clipboard.writeText) {
@@ -72,6 +77,13 @@ export const CodeBlock: FC<Props> = memo(({ language, value }) => {
72
  {isCopied ? <IconCheck size={18} /> : <IconClipboard size={18} />}
73
  {isCopied ? t('Copied!') : t('Copy code')}
74
  </button>
 
 
 
 
 
 
 
75
  <button
76
  className="flex items-center rounded bg-none p-1 text-xs text-white"
77
  onClick={downloadAsFile}
@@ -88,6 +100,13 @@ export const CodeBlock: FC<Props> = memo(({ language, value }) => {
88
  >
89
  {value}
90
  </SyntaxHighlighter>
 
 
 
 
 
 
 
91
  </div>
92
  );
93
  });
 
1
+ import { IconCheck, IconClipboard, IconDownload, IconPlay } from '@tabler/icons-react';
2
  import { FC, memo, useState } from 'react';
3
  import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
4
  import { oneDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
 
18
  export const CodeBlock: FC<Props> = memo(({ language, value }) => {
19
  const { t } = useTranslation('markdown');
20
  const [isCopied, setIsCopied] = useState<Boolean>(false);
21
+ const [isRunning, setIsRunning] = useState<Boolean>(false);
22
+
23
+ const toggleRunCode = () => {
24
+ setIsRunning(!isRunning);
25
+ };
26
 
27
  const copyToClipboard = () => {
28
  if (!navigator.clipboard || !navigator.clipboard.writeText) {
 
77
  {isCopied ? <IconCheck size={18} /> : <IconClipboard size={18} />}
78
  {isCopied ? t('Copied!') : t('Copy code')}
79
  </button>
80
+ <button
81
+ className="flex gap-1.5 items-center rounded bg-none p-1 text-xs text-white"
82
+ onClick={toggleRunCode}
83
+ >
84
+ <IconPlay size={18} />
85
+ {isRunning ? t('Stop') : t('Run code')}
86
+ </button>
87
  <button
88
  className="flex items-center rounded bg-none p-1 text-xs text-white"
89
  onClick={downloadAsFile}
 
100
  >
101
  {value}
102
  </SyntaxHighlighter>
103
+ {isRunning && (
104
+ <iframe
105
+ srcDoc={value}
106
+ title="live code preview"
107
+ className="w-full h-64 mt-4 border-2 border-gray-300"
108
+ />
109
+ )}
110
  </div>
111
  );
112
  });