@@ -3,21 +3,26 @@ import CodeMirror from "@uiw/react-codemirror";
33import { javascript } from "@codemirror/lang-javascript" ;
44import { createTheme } from "@uiw/codemirror-themes" ;
55import { EditorView } from "@codemirror/view" ;
6-
76import Head from "next/head" ;
87import Toastr from "../components/Toastr" ;
98import { ToastContainer } from "react-toastify" ;
109import Button from "../components/Button" ;
1110
1211import { CODE_EDITOR_THEME } from "../constants/theme" ;
12+ import { DEMO_JSON_STRING } from "../components/constants" ;
13+ import { Copy } from "../icons/Copy" ;
1314
1415export default function Home ( ) {
15- const [ inputString , setInputString ] = useState ( "" ) ;
16+ const [ inputString , setInputString ] = useState (
17+ JSON . stringify ( DEMO_JSON_STRING )
18+ ) ;
1619 const [ outputString , setOutputString ] = useState ( "" ) ;
1720
21+ let editorExtensions = [ javascript ( { jsx : true } ) , EditorView . lineWrapping ] ;
22+
1823 const handleFormat = ( ) => {
1924 try {
20- setOutputString ( JSON . stringify ( JSON . parse ( inputString ) , null , 4 ) ) ;
25+ setOutputString ( JSON . stringify ( JSON . parse ( inputString ) , null , 2 ) ) ;
2126 } catch ( error ) {
2227 Toastr . error ( "Enter Valid JSON" ) ;
2328 }
@@ -31,14 +36,28 @@ export default function Home() {
3136 const handleDownload = ( ) => {
3237 const element = document . createElement ( "a" ) ;
3338 const file = new Blob ( [ outputString ] , {
34- type : "text/plain;charset=utf-8 " ,
39+ type : "text/json " ,
3540 } ) ;
3641 element . href = URL . createObjectURL ( file ) ;
37- element . download = "json.txt " ;
42+ element . download = "json.json " ;
3843 document . body . appendChild ( element ) ;
3944 element . click ( ) ;
4045 } ;
4146
47+ const handleCopy = ( ) => {
48+ try {
49+ navigator . clipboard . writeText ( outputString ) ;
50+ Toastr . success ( "Copied to clipboard" ) ;
51+ } catch ( error ) {
52+ Toastr . error ( "Could not Copy. Try again" ) ;
53+ }
54+ } ;
55+
56+ const handleDemoJson = ( ) => {
57+ setInputString ( JSON . stringify ( DEMO_JSON_STRING ) ) ;
58+ setOutputString ( "" ) ;
59+ } ;
60+
4261 useEffect ( ( ) => {
4362 inputString === "" && setOutputString ( "" ) ;
4463 } , [ inputString ] ) ;
@@ -56,28 +75,50 @@ export default function Home() {
5675 < p className = "text-2xl text-center" > JSON STYLE</ p >
5776 < div className = "space-y-3 flex-col" >
5877 < div className = "flex space-x-3 h-90" >
59- < CodeMirror
60- autoFocus
61- height = "548px"
62- theme = "dark"
63- value = { inputString }
64- extensions = { [ javascript ( { jsx : true } ) ] }
65- onChange = { ( e ) => setInputString ( e ) }
66- className = "w-1/2 mt-7 h-full"
67- />
68- < CodeMirror
69- height = "548px"
70- theme = { createTheme ( CODE_EDITOR_THEME ) }
71- editable = { false }
72- value = { outputString }
73- extensions = { [ javascript ( { jsx : true } ) , EditorView . lineWrapping ] }
74- className = "w-1/2 mt-7 overflow-auto h-full"
75- />
78+ < div className = "w-full bg-[#282c34]" >
79+ < Button
80+ size = "small"
81+ style = "text"
82+ label = "Demo JSON"
83+ onClick = { handleDemoJson }
84+ />
85+ < CodeMirror
86+ autoFocus
87+ height = "548px"
88+ theme = "dark"
89+ value = { inputString }
90+ extensions = { editorExtensions }
91+ onChange = { ( e ) => setInputString ( e ) }
92+ className = "h-full mt-1"
93+ />
94+ </ div >
95+ < div className = "w-full bg-[#282c34]" >
96+ < Button
97+ size = "small"
98+ style = "text"
99+ onClick = { handleCopy }
100+ icon = { Copy }
101+ className = "float-right"
102+ disabled = { outputString === "" }
103+ />
104+ < CodeMirror
105+ height = "548px"
106+ theme = { createTheme ( CODE_EDITOR_THEME ) }
107+ editable = { false }
108+ value = { outputString }
109+ extensions = { editorExtensions }
110+ className = "overflow-auto h-full mt-7"
111+ />
112+ </ div >
76113 </ div >
77114 < div className = "flex bg-zinc-800 space-x-3 px-3 py-3 rounded spacy-x-2" >
78115 < Button label = "Format" onClick = { handleFormat } />
79116 < Button label = "Clear" onClick = { handleClear } />
80- < Button label = "Download" onClick = { handleDownload } />
117+ < Button
118+ label = "Download"
119+ onClick = { handleDownload }
120+ disabled = { outputString === "" }
121+ />
81122 </ div >
82123 </ div >
83124 { /* <Footer /> */ }
0 commit comments