1+ import hljs from "highlight.js/lib/core" ;
2+ import json from "highlight.js/lib/languages/json" ;
13import { ReactElement } from "react" ;
4+ import Editor from "react-simple-code-editor" ;
5+ import "highlight.js/styles/atom-one-light.css" ;
6+
7+ hljs . registerLanguage ( "json" , json ) ;
28
39export interface CodeEditorProps {
410 value : string ;
@@ -7,13 +13,49 @@ export interface CodeEditorProps {
713 height ?: string ;
814}
915
16+ function highlight ( code : string ) : string {
17+ try {
18+ return hljs . highlight ( code , { language : "json" , ignoreIllegals : true } ) . value ;
19+ } catch ( error ) {
20+ console . warn ( "Syntax highlighting error:" , error ) ;
21+ return code ;
22+ }
23+ }
24+
25+ function jsonError ( code : string ) : string | null {
26+ if ( code . trim ( ) === "" ) {
27+ return null ;
28+ }
29+ try {
30+ JSON . parse ( code ) ;
31+ return null ;
32+ } catch ( error ) {
33+ return error instanceof Error ? error . message : "Invalid JSON" ;
34+ }
35+ }
36+
1037export function CodeEditor ( props : CodeEditorProps ) : ReactElement {
38+ const error = jsonError ( props . value ) ;
39+
1140 return (
12- < textarea
13- value = { props . value }
14- onChange = { e => props . onChange ?.( e . target . value ) }
15- style = { { height : props . height ?? "200px" , width : "100%" , fontFamily : "monospace" } }
16- readOnly = { props . readOnly }
17- />
41+ < div className = "widget-charts-playground-code-editor" >
42+ { error && (
43+ < div className = "widget-charts-playground-code-editor-error" role = "alert" >
44+ { error }
45+ </ div >
46+ ) }
47+ < Editor
48+ value = { props . value }
49+ onValueChange = { value => props . onChange ?.( value ) }
50+ highlight = { highlight }
51+ disabled = { props . readOnly }
52+ padding = { 8 }
53+ tabSize = { 2 }
54+ insertSpaces
55+ ignoreTabKey = { false }
56+ spellCheck = { false }
57+ style = { { height : props . height ?? "200px" , width : "100%" , fontFamily : "monospace" } }
58+ />
59+ </ div >
1860 ) ;
1961}
0 commit comments