-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathcode-editor.tsx
More file actions
130 lines (106 loc) · 3.32 KB
/
Copy pathcode-editor.tsx
File metadata and controls
130 lines (106 loc) · 3.32 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { useEffect, useState } from "react";
import { useTheme } from "@/providers/theme-provider/theme-provider";
import { cn } from "@/lib/utils";
import { Spinner } from "./ui/shadcn-io/spinner";
interface CodeEditorProps {
defaultValue?: string;
value?: string;
className?: string | undefined,
readOnly?: boolean,
onChange?: (sql: string) => void
}
const CodeEditor: React.FC<CodeEditorProps> = ({
defaultValue,
value,
className,
readOnly = false,
onChange
}) => {
const [editor, setEditor] = useState<any>(null);
useEffect(() => {
let mounted = true;
Promise.all([
import("@uiw/react-codemirror"),
import("@codemirror/lang-sql"),
import("@codemirror/view"),
]).then(([cm, sqlLang, view]) => {
if (!mounted) return;
const overrideDarkTheme = view.EditorView.theme({
'.cm-content': {
backgroundColor: "#1c2025",
},
".cm-gutter": {
backgroundColor: "#1c2025",
},
".cm-gutterElement": {
color: "#4b515a"
},
".ͼp": {
color: "#A994FF"
},
".cm-line .ͼq": {
color: "#ff6363"
},
".ͼu": {
color: "#B6E672"
},
".ͼv": {
color: "#6cdcc4"
}
}, { dark: true });
const overrideLightTheme = view.EditorView.theme({
".ͼb": {
color: "#2A1D66"
},
".cm-gutterElement": {
color: "#62748e"
},
".cm-gutter": {
backgroundColor: "white",
},
".cm-gutters": {
borderColor: "#f2f4f6"
},
".cm-line": {
color: "#0f172b"
},
})
setEditor({
CodeMirror: cm.default,
oneDark: cm.oneDark,
sql: sqlLang.sql,
overrideDarkTheme,
overrideLightTheme,
});
});
return () => {
mounted = false;
};
}, []);
const { theme } = useTheme();
if (!editor) {
return (
<div
className={cn(
"flex flex-1 w-full min-h-9 rounded-sm border border-border bg-card items-center justify-center",
className
)}
>
<Spinner className="text-primary" />
</div>
);
}
const { CodeMirror, oneDark, sql, overrideDarkTheme, overrideLightTheme } = editor;
return (
<CodeMirror
defaultValue={defaultValue}
value={value}
className={cn("flex flex-1 w-full min-h-9 rounded-sm h-full bg-card border-1 border-border !min-w-0 overflow-hidden", className)}
extensions={[sql()]}
readOnly={readOnly}
theme={theme != "dark" ? overrideLightTheme : [oneDark, overrideDarkTheme]}
onChange={onChange}
/>
)
}
export default React.memo(CodeEditor);