1- import { useState , useEffect , useRef } from "react" ;
2- import CodeMirror from '@uiw/react-codemirror' ;
3- import { javascript } from '@codemirror/lang-javascript' ;
1+ import { useState } from "react" ;
2+ import CodeMirror from "@uiw/react-codemirror" ;
3+ import { javascript } from "@codemirror/lang-javascript" ;
4+ import { createTheme } from "@uiw/codemirror-themes" ;
5+ import { EditorView } from "@codemirror/view" ;
46
57import Head from "next/head" ;
68import Button from "../components/Button" ;
7- import Textarea from "../components/Textarea" ;
89
9- export default function Home ( ) {
10+ import { CODE_EDITOR_THEME } from "../constants/theme" ;
1011
11- const [ json , setJson ] = useState ( ) ;
12+ export default function Home ( ) {
13+ const [ inputString , setInputString ] = useState ( "" ) ;
14+ const [ outputString , setOutputString ] = useState ( "" ) ;
1215
1316 const handleFormat = ( ) => {
14- console . log ( JSON . stringify ( json , null , 4 ) )
15- }
17+ setOutputString ( JSON . stringify ( JSON . parse ( inputString ) , null , 2 ) ) ;
18+ } ;
19+
20+ const handleClear = ( ) => {
21+ setInputString ( "" ) ;
22+ setOutputString ( "" ) ;
23+ } ;
1624
1725 return (
1826 < div className = "h-screen flex-col justify-between bg-zinc-900 p-5" >
@@ -26,32 +34,27 @@ export default function Home() {
2634 < p className = "text-2xl text-center" > JSON STYLE</ p >
2735 < div className = "space-y-3 flex-col" >
2836 < div className = "flex space-x-3 h-90" >
29- { /* <Textarea
30- className="w-1/2"
31- rows={22}
32- label="JSON String"
33- onChange={(e) => setJson(JSON.parse(e.target.value))}
34- /> */ }
35- < CodeMirror
36- className = "w-1/2 mt-7 overflow-x-auto h-full"
37- value = { JSON . stringify ( json ) }
37+ < CodeMirror
38+ autoFocus
3839 height = "548px"
3940 theme = "dark"
41+ value = { inputString }
4042 extensions = { [ javascript ( { jsx : true } ) ] }
41- onChange = { ( e ) => setJson ( JSON . parse ( e ) ) }
43+ onChange = { ( e ) => setInputString ( e ) }
44+ className = "w-1/2 mt-7 h-full"
4245 />
4346 < CodeMirror
44- className = "w-1/2 mt-7 overflow-auto h-full"
45- value = { JSON . stringify ( json , null , 2 ) }
4647 height = "548px"
47- theme = "dark"
48+ theme = { createTheme ( CODE_EDITOR_THEME ) }
4849 editable = { false }
49- extensions = { [ javascript ( { jsx : true } ) ] }
50+ value = { outputString }
51+ extensions = { [ javascript ( { jsx : true } ) , EditorView . lineWrapping ] }
52+ className = "w-1/2 mt-7 overflow-auto h-full"
5053 />
5154 </ div >
5255 < div className = "flex bg-zinc-800 space-x-3 px-1 py-3 rounded" >
5356 < Button className = "ml-1" label = "Format" onClick = { handleFormat } />
54- < Button className = "ml-1" label = "Reset" />
57+ < Button className = "ml-1" label = "Clear" onClick = { handleClear } />
5558 </ div >
5659 </ div >
5760 { /* <Footer /> */ }
0 commit comments