22 @license
33 Copyright (c) 2015-2026 Lablup Inc. All rights reserved.
44 */
5+ import { loadMonacoEditor } from '../helper/monacoEditor' ;
56import useControllableState_deprecated from '../hooks/useControllableState' ;
67import { useThemeMode } from '../hooks/useThemeMode' ;
7- import { loadLanguage , LanguageName } from '@uiw/codemirror-extensions-langs' ;
8- import CodeMirror , {
9- ReactCodeMirrorProps ,
10- EditorView ,
11- } from '@uiw/react-codemirror' ;
8+ import type { EditorProps } from '@monaco-editor/react' ;
9+ import { Skeleton , theme } from 'antd' ;
10+ import React , { Suspense } from 'react' ;
1211
13- interface BAICodeEditorProps extends Omit < ReactCodeMirrorProps , 'language' > {
12+ const MonacoEditor : React . LazyExoticComponent < React . FC < EditorProps > > =
13+ React . lazy ( ( ) =>
14+ loadMonacoEditor ( ) . then ( ( module ) => ( {
15+ default : module . Editor as React . FC < EditorProps > ,
16+ } ) ) ,
17+ ) ;
18+
19+ // Language alias preserved from the previous codemirror-based API so existing
20+ // call sites keep working. Extend as needed when adding new languages.
21+ export type BAICodeEditorLanguage = 'json' | 'sh' | 'yaml' | 'toml' ;
22+
23+ const MONACO_LANGUAGE_MAP : Record < BAICodeEditorLanguage , string > = {
24+ json : 'json' ,
25+ sh : 'shell' ,
26+ yaml : 'yaml' ,
27+ toml : 'plaintext' ,
28+ } ;
29+
30+ interface BAICodeEditorProps extends Omit <
31+ EditorProps ,
32+ 'language' | 'value' | 'onChange'
33+ > {
1434 value ?: string ;
1535 onChange ?: ( value : string ) => void ;
16- language ?: LanguageName ;
36+ language ?: BAICodeEditorLanguage ;
1737 editable ?: boolean ;
1838 showLineNumbers ?: boolean ;
1939 lineWrapping ?: boolean ;
40+ style ?: React . CSSProperties ;
2041}
2142
2243const BAICodeEditor : React . FC < BAICodeEditorProps > = ( {
@@ -26,34 +47,62 @@ const BAICodeEditor: React.FC<BAICodeEditorProps> = ({
2647 editable = false ,
2748 showLineNumbers = true ,
2849 lineWrapping = false ,
29- ...CodeMirrorProps
50+ height = 200 ,
51+ style,
52+ options,
53+ ...editorProps
3054} ) => {
55+ 'use memo' ;
3156 const { isDarkMode } = useThemeMode ( ) ;
57+ const { token } = theme . useToken ( ) ;
3258
3359 const [ script , setScript ] = useControllableState_deprecated < string > ( {
3460 defaultValue : '' ,
3561 value,
3662 onChange,
3763 } ) ;
38- const languageExtension = loadLanguage ( language ) ;
39- const extensions = languageExtension ? [ languageExtension ] : [ ] ;
4064
41- return (
42- < CodeMirror
43- theme = { isDarkMode ? 'dark' : 'light' }
44- extensions = {
45- lineWrapping ? [ EditorView . lineWrapping , ...extensions ] : extensions
46- }
47- editable = { editable }
48- readOnly = { ! editable }
49- basicSetup = { {
50- lineNumbers : showLineNumbers ,
65+ const loadingFallback = (
66+ < Skeleton
67+ active
68+ style = { {
69+ paddingInline : token . paddingContentHorizontal ,
70+ paddingBlock : token . paddingContentVertical ,
5171 } }
52- value = { script }
53- onChange = { ( value ) => setScript ( value ) }
54- { ...CodeMirrorProps }
5572 />
5673 ) ;
74+
75+ return (
76+ < div
77+ style = { {
78+ border : `1px solid ${ token . colorBorder } ` ,
79+ borderRadius : token . borderRadius ,
80+ overflow : 'hidden' ,
81+ ...style ,
82+ } }
83+ >
84+ < Suspense fallback = { loadingFallback } >
85+ < MonacoEditor
86+ language = { MONACO_LANGUAGE_MAP [ language ] }
87+ height = { height }
88+ theme = { isDarkMode ? 'vs-dark' : 'vs' }
89+ value = { script }
90+ onChange = { ( v : string | undefined ) => setScript ( v ?? '' ) }
91+ loading = { loadingFallback }
92+ options = { {
93+ readOnly : ! editable ,
94+ lineNumbers : showLineNumbers ? 'on' : 'off' ,
95+ wordWrap : lineWrapping ? 'on' : 'off' ,
96+ minimap : { enabled : false } ,
97+ scrollBeyondLastLine : false ,
98+ fixedOverflowWidgets : true ,
99+ ...options ,
100+ } }
101+ { ...editorProps }
102+ />
103+ </ Suspense >
104+ </ div >
105+ ) ;
57106} ;
58107
59108export default BAICodeEditor ;
0 commit comments