-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathjavascript-editor.tsx
More file actions
114 lines (108 loc) · 3.37 KB
/
javascript-editor.tsx
File metadata and controls
114 lines (108 loc) · 3.37 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import CodeMirror, { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { forwardRef, useMemo } from "react";
import { tags as t } from "@lezer/highlight";
import createTheme from "@uiw/codemirror-themes";
import { useTheme } from "@/context/theme-provider";
import { indentationMarkers } from "@replit/codemirror-indentation-markers";
interface JsonEditorProps {
value: string;
readOnly?: boolean;
onChange?: (value: string) => void;
}
function useJavascriptTheme() {
const { theme } = useTheme();
return useMemo(() => {
if (theme === "light") {
return createTheme({
theme: "light",
settings: {
background: "#FFFFFF",
foreground: "#000000",
caret: "#FBAC52",
selection: "#FFD420",
selectionMatch: "#FFD420",
gutterBackground: "#fff",
gutterForeground: "#4D4D4C",
gutterBorder: "transparent",
lineHighlight: "#00000012",
fontFamily:
'Consolas, "Andale Mono", "Ubuntu Mono", "Courier New", monospace',
},
styles: [
{
tag: [t.propertyName, t.function(t.variableName)],
color: "#e67e22",
},
{ tag: [t.keyword], color: "#0000FF" },
{ tag: [t.comment, t.blockComment], color: "#95a5a6" },
{ tag: [t.bool, t.null], color: "#696C77" },
{ tag: [t.number], color: "#FF0080" },
{ tag: [t.string], color: "#50A14F" },
],
});
} else {
return createTheme({
theme: "dark",
settings: {
background: "var(--background)",
foreground: "#9cdcfd",
caret: "#c6c6c6",
selection: "#6199ff2f",
selectionMatch: "#72a1ff59",
lineHighlight: "#ffffff0f",
gutterBackground: "var(--background)",
gutterForeground: "#838383",
gutterActiveForeground: "#fff",
fontFamily:
'Consolas, "Andale Mono", "Ubuntu Mono", "Courier New", monospace',
},
styles: [
{
tag: [t.propertyName, t.function(t.variableName)],
color: "#dcdcaa",
},
{ tag: [t.keyword], color: "#c586c0" },
{ tag: [t.comment, t.blockComment], color: "#95a5a6" },
{ tag: [t.bool, t.null], color: "#696C77" },
{ tag: [t.number], color: "#b5cea8" },
{ tag: [t.string], color: "#ce9178" },
{ tag: [t.paren], color: "#9cdcfd" },
{ tag: [t.bracket], color: "#da70d6" },
],
});
}
}, [theme]);
}
const JavascriptEditor = forwardRef<ReactCodeMirrorRef, JsonEditorProps>(
function JavascriptEditor(
{ value, onChange, readOnly }: JsonEditorProps,
ref
) {
const theme = useJavascriptTheme();
return (
<CodeMirror
className="border p-1 rounded"
ref={ref}
autoFocus
readOnly={readOnly}
basicSetup={{
drawSelection: false,
highlightActiveLine: false,
highlightActiveLineGutter: false,
foldGutter: false,
}}
theme={theme}
value={value}
height="100%"
onChange={onChange}
style={{
fontSize: 20,
height: "100%",
}}
extensions={[javascript(), indentationMarkers()]}
/>
);
}
);
export default JavascriptEditor;