-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathBAICodeEditor.tsx
More file actions
108 lines (99 loc) · 2.78 KB
/
BAICodeEditor.tsx
File metadata and controls
108 lines (99 loc) · 2.78 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
/**
@license
Copyright (c) 2015-2026 Lablup Inc. All rights reserved.
*/
import { loadMonacoEditor } from '../helper/monacoEditor';
import useControllableState_deprecated from '../hooks/useControllableState';
import { useThemeMode } from '../hooks/useThemeMode';
import type { EditorProps } from '@monaco-editor/react';
import { Skeleton, theme } from 'antd';
import React, { Suspense } from 'react';
const MonacoEditor: React.LazyExoticComponent<React.FC<EditorProps>> =
React.lazy(() =>
loadMonacoEditor().then((module) => ({
default: module.Editor as React.FC<EditorProps>,
})),
);
// Language alias preserved from the previous codemirror-based API so existing
// call sites keep working. Extend as needed when adding new languages.
export type BAICodeEditorLanguage = 'json' | 'sh' | 'yaml' | 'toml';
const MONACO_LANGUAGE_MAP: Record<BAICodeEditorLanguage, string> = {
json: 'json',
sh: 'shell',
yaml: 'yaml',
toml: 'plaintext',
};
interface BAICodeEditorProps extends Omit<
EditorProps,
'language' | 'value' | 'onChange'
> {
value?: string;
onChange?: (value: string) => void;
language?: BAICodeEditorLanguage;
editable?: boolean;
showLineNumbers?: boolean;
lineWrapping?: boolean;
style?: React.CSSProperties;
}
const BAICodeEditor: React.FC<BAICodeEditorProps> = ({
value,
onChange,
language = 'sh',
editable = false,
showLineNumbers = true,
lineWrapping = false,
height = 200,
style,
options,
...editorProps
}) => {
'use memo';
const { isDarkMode } = useThemeMode();
const { token } = theme.useToken();
const [script, setScript] = useControllableState_deprecated<string>({
defaultValue: '',
value,
onChange,
});
const loadingFallback = (
<Skeleton
active
style={{
paddingInline: token.paddingContentHorizontal,
paddingBlock: token.paddingContentVertical,
}}
/>
);
return (
<div
style={{
border: `1px solid ${token.colorBorder}`,
borderRadius: token.borderRadius,
overflow: 'hidden',
...style,
}}
>
<Suspense fallback={loadingFallback}>
<MonacoEditor
language={MONACO_LANGUAGE_MAP[language]}
height={height}
theme={isDarkMode ? 'vs-dark' : 'vs'}
value={script}
onChange={(v: string | undefined) => setScript(v ?? '')}
loading={loadingFallback}
options={{
readOnly: !editable,
lineNumbers: showLineNumbers ? 'on' : 'off',
wordWrap: lineWrapping ? 'on' : 'off',
minimap: { enabled: false },
scrollBeyondLastLine: false,
fixedOverflowWidgets: true,
...options,
}}
{...editorProps}
/>
</Suspense>
</div>
);
};
export default BAICodeEditor;