-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathCodeHighlight.tsx
More file actions
83 lines (73 loc) · 2.34 KB
/
CodeHighlight.tsx
File metadata and controls
83 lines (73 loc) · 2.34 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React, { JSX, useCallback, useEffect, useState } from "react";
import Editor from "react-simple-code-editor";
import hljs from "highlight.js/lib/core";
import javascript from "highlight.js/lib/languages/javascript";
import json from "highlight.js/lib/languages/json";
import "highlight.js/styles/stackoverflow-light.css";
hljs.registerLanguage("json", json);
hljs.registerLanguage("javascript", javascript);
function HighlightedCode({ code, language }: { code: string; language: string }) {
const highlightedCode = hljs.highlight(code, { language }).value;
return (
<pre className={`language-${language} overflow-x-auto text-code`}>
<code dangerouslySetInnerHTML={{ __html: highlightedCode }} />
</pre>
);
}
const codeStyle = {
backgroundColor: "var(--fp-color-background-00)",
border: "1px solid var(--fp-color-decorative-00)",
borderRadius: "4px",
overflowX: "auto",
};
export function CodeHighlight({ code, language = "json" }: { code: string; language?: string }): JSX.Element {
return (
<div className="p-2 text-code" style={codeStyle as React.CSSProperties}>
<HighlightedCode code={code} language={language} />
</div>
);
}
export function EditableCodeHighlight({
code,
onChange,
language = "json",
}: {
code: string;
language?: string;
onChange: (args: { code: string; valid: boolean }) => void;
}) {
const [liveCode, setCode] = useState(code);
const onEditableChange = useCallback(
(liveCode: string) => {
if (language === "json") {
try {
liveCode = JSON.stringify(JSON.parse(liveCode), null, 2);
onChange({ code: liveCode, valid: true });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
onChange({ code: liveCode, valid: false });
}
} else {
onChange({ code: liveCode, valid: true });
}
setCode(liveCode);
},
[language, onChange],
);
useEffect(() => {
setCode(code);
}, [code]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const EditorComponent = Editor as any;
return (
<EditorComponent
value={liveCode}
onValueChange={onEditableChange}
highlight={(code: string) => <HighlightedCode code={code} language={language} />}
padding={10}
style={codeStyle}
autoFocus
className="text-code"
/>
);
}