Skip to content

Commit 1bde608

Browse files
Merge pull request #21 from sreeram-venkitesh/18-added-format-option
Added codemirror and format function
2 parents 060845c + ed03e0f commit 1bde608

File tree

5 files changed

+249
-16
lines changed

5 files changed

+249
-16
lines changed

constants/theme.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { tags as t } from "@lezer/highlight";
2+
3+
export const CODE_EDITOR_THEME = {
4+
theme: "dark",
5+
settings: {
6+
background: "#282c34",
7+
foreground: "#ffffff",
8+
caret: "282c34",
9+
gutters: "#282c34",
10+
selection: "black",
11+
lineHighlight: "#6699ff0b",
12+
gutterBackground: "#282c34",
13+
gutterForeground: "#7d8799",
14+
},
15+
styles: [
16+
{ tag: t.brace, color: "red" },
17+
{ tag: t.squareBracket, color: "#98c379" },
18+
{ tag: t.separator, color: "orange" },
19+
{ tag: t.bool, color: "blue" },
20+
{ tag: t.string, color: "#98c379" },
21+
],
22+
};

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"@codemirror/lang-javascript": "^6.1.2",
13+
"@lezer/highlight": "^1.1.3",
14+
"@uiw/codemirror-themes": "^4.19.4",
15+
"@uiw/react-codemirror": "^4.19.4",
1216
"classnames": "^2.3.2",
1317
"eslint": "8.29.0",
1418
"eslint-config-next": "13.0.6",

pages/index.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
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";
6+
17
import Head from "next/head";
2-
import Footer from "../components/Footer";
38
import Button from "../components/Button";
4-
import Textarea from "../components/Textarea";
9+
10+
import { CODE_EDITOR_THEME } from "../constants/theme";
511

612
export default function Home() {
13+
const [inputString, setInputString] = useState("");
14+
const [outputString, setOutputString] = useState("");
15+
16+
const handleFormat = () => {
17+
setOutputString(JSON.stringify(JSON.parse(inputString), null, 2));
18+
};
19+
20+
const handleClear = () => {
21+
setInputString("");
22+
setOutputString("");
23+
};
24+
725
return (
826
<div className="h-screen flex-col justify-between bg-zinc-900 p-5">
927
<Head>
@@ -14,24 +32,29 @@ export default function Home() {
1432

1533
<main>
1634
<p className="text-2xl text-center">JSON STYLE</p>
17-
<div className="space-y-3">
18-
<div className="flex space-x-3 mt-5">
19-
<Textarea
20-
className="w-1/2"
21-
rows={24}
22-
label="JSON String"
23-
onChange={(e) => console.log(e.target.value)}
35+
<div className="space-y-3 flex-col">
36+
<div className="flex space-x-3 h-90">
37+
<CodeMirror
38+
autoFocus
39+
height="548px"
40+
theme="dark"
41+
value={inputString}
42+
extensions={[javascript({ jsx: true })]}
43+
onChange={(e) => setInputString(e)}
44+
className="w-1/2 mt-7 h-full"
2445
/>
25-
<Textarea
26-
className="w-1/2"
27-
rows={24}
28-
label="Output"
29-
onChange={(e) => console.log(e.target.value)}
46+
<CodeMirror
47+
height="548px"
48+
theme={createTheme(CODE_EDITOR_THEME)}
49+
editable={false}
50+
value={outputString}
51+
extensions={[javascript({ jsx: true }), EditorView.lineWrapping]}
52+
className="w-1/2 mt-7 overflow-auto h-full"
3053
/>
3154
</div>
3255
<div className="flex bg-zinc-800 space-x-3 px-1 py-3 rounded">
33-
<Button className="ml-1" label="Format" />
34-
<Button className="ml-1" label="Reset" />
56+
<Button className="ml-1" label="Format" onClick={handleFormat} />
57+
<Button className="ml-1" label="Clear" onClick={handleClear} />
3558
</div>
3659
</div>
3760
{/* <Footer /> */}

styles/base/_index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ body {
22
color: #e4e4e7;
33
background-color: rgb(24 24 27);
44
}
5+

yarn.lock

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,100 @@
1717
dependencies:
1818
regenerator-runtime "^0.13.11"
1919

20+
"@babel/runtime@^7.18.6":
21+
version "7.20.7"
22+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.20.7.tgz#fcb41a5a70550e04a7b708037c7c32f7f356d8fd"
23+
integrity sha512-UF0tvkUtxwAgZ5W/KrkHf0Rn0fdnLDU9ScxBrEVNUprE/MzirjK4MJUX1/BVDv00Sv8cljtukVK1aky++X1SjQ==
24+
dependencies:
25+
regenerator-runtime "^0.13.11"
26+
27+
"@codemirror/autocomplete@^6.0.0":
28+
version "6.4.0"
29+
resolved "https://registry.yarnpkg.com/@codemirror/autocomplete/-/autocomplete-6.4.0.tgz#76ac9a2a411a4cc6e13103014dba5e0fe601da5a"
30+
integrity sha512-HLF2PnZAm1s4kGs30EiqKMgD7XsYaQ0XJnMR0rofEWQ5t5D60SfqpDIkIh1ze5tiEbyUWm8+VJ6W1/erVvBMIA==
31+
dependencies:
32+
"@codemirror/language" "^6.0.0"
33+
"@codemirror/state" "^6.0.0"
34+
"@codemirror/view" "^6.6.0"
35+
"@lezer/common" "^1.0.0"
36+
37+
"@codemirror/commands@^6.0.0", "@codemirror/commands@^6.1.0":
38+
version "6.1.2"
39+
resolved "https://registry.yarnpkg.com/@codemirror/commands/-/commands-6.1.2.tgz#84fb7d170047c3aeb7b0047ace59510bb19208de"
40+
integrity sha512-sO3jdX1s0pam6lIdeSJLMN3DQ6mPEbM4yLvyKkdqtmd/UDwhXA5+AwFJ89rRXm6vTeOXBsE5cAmlos/t7MJdgg==
41+
dependencies:
42+
"@codemirror/language" "^6.0.0"
43+
"@codemirror/state" "^6.0.0"
44+
"@codemirror/view" "^6.0.0"
45+
"@lezer/common" "^1.0.0"
46+
47+
"@codemirror/lang-javascript@^6.1.2":
48+
version "6.1.2"
49+
resolved "https://registry.yarnpkg.com/@codemirror/lang-javascript/-/lang-javascript-6.1.2.tgz#a11812ca1d21301cdeb80e51b4c007edcf55f813"
50+
integrity sha512-OcwLfZXdQ1OHrLiIcKCn7MqZ7nx205CMKlhe+vL88pe2ymhT9+2P+QhwkYGxMICj8TDHyp8HFKVwpiisUT7iEQ==
51+
dependencies:
52+
"@codemirror/autocomplete" "^6.0.0"
53+
"@codemirror/language" "^6.0.0"
54+
"@codemirror/lint" "^6.0.0"
55+
"@codemirror/state" "^6.0.0"
56+
"@codemirror/view" "^6.0.0"
57+
"@lezer/common" "^1.0.0"
58+
"@lezer/javascript" "^1.0.0"
59+
60+
"@codemirror/language@^6.0.0":
61+
version "6.3.2"
62+
resolved "https://registry.yarnpkg.com/@codemirror/language/-/language-6.3.2.tgz#a3d5796d17a2cd3110bac0f5126db67c7e90a0f3"
63+
integrity sha512-g42uHhOcEMAXjmozGG+rdom5UsbyfMxQFh7AbkeoaNImddL6Xt4cQDL0+JxmG7+as18rUAvZaqzP/TjsciVIrA==
64+
dependencies:
65+
"@codemirror/state" "^6.0.0"
66+
"@codemirror/view" "^6.0.0"
67+
"@lezer/common" "^1.0.0"
68+
"@lezer/highlight" "^1.0.0"
69+
"@lezer/lr" "^1.0.0"
70+
style-mod "^4.0.0"
71+
72+
"@codemirror/lint@^6.0.0":
73+
version "6.1.0"
74+
resolved "https://registry.yarnpkg.com/@codemirror/lint/-/lint-6.1.0.tgz#f006142d3a580fdb8ffc2faa3361b2232c08e079"
75+
integrity sha512-mdvDQrjRmYPvQ3WrzF6Ewaao+NWERYtpthJvoQ3tK3t/44Ynhk8ZGjTSL9jMEv8CgSMogmt75X8ceOZRDSXHtQ==
76+
dependencies:
77+
"@codemirror/state" "^6.0.0"
78+
"@codemirror/view" "^6.0.0"
79+
crelt "^1.0.5"
80+
81+
"@codemirror/search@^6.0.0":
82+
version "6.2.3"
83+
resolved "https://registry.yarnpkg.com/@codemirror/search/-/search-6.2.3.tgz#fab933fef1b1de8ef40cda275c73d9ac7a1ff40f"
84+
integrity sha512-V9n9233lopQhB1dyjsBK2Wc1i+8hcCqxl1wQ46c5HWWLePoe4FluV3TGHoZ04rBRlGjNyz9DTmpJErig8UE4jw==
85+
dependencies:
86+
"@codemirror/state" "^6.0.0"
87+
"@codemirror/view" "^6.0.0"
88+
crelt "^1.0.5"
89+
90+
"@codemirror/state@^6.0.0", "@codemirror/state@^6.1.1", "@codemirror/state@^6.1.4":
91+
version "6.1.4"
92+
resolved "https://registry.yarnpkg.com/@codemirror/state/-/state-6.1.4.tgz#2b654ae233ac4f41ee89ce095509ea35ecdf1031"
93+
integrity sha512-g+3OJuRylV5qsXuuhrc6Cvs1NQluNioepYMM2fhnpYkNk7NgX+j0AFuevKSVKzTDmDyt9+Puju+zPdHNECzCNQ==
94+
95+
"@codemirror/theme-one-dark@^6.0.0":
96+
version "6.1.0"
97+
resolved "https://registry.yarnpkg.com/@codemirror/theme-one-dark/-/theme-one-dark-6.1.0.tgz#6f8b3c7fc22e9fec59edd573f4ba9546db42e007"
98+
integrity sha512-AiTHtFRu8+vWT9wWUWDM+cog6ZwgivJogB1Tm/g40NIpLwph7AnmxrSzWfvJN5fBVufsuwBxecQCNmdcR5D7Aw==
99+
dependencies:
100+
"@codemirror/language" "^6.0.0"
101+
"@codemirror/state" "^6.0.0"
102+
"@codemirror/view" "^6.0.0"
103+
"@lezer/highlight" "^1.0.0"
104+
105+
"@codemirror/view@^6.0.0", "@codemirror/view@^6.6.0":
106+
version "6.7.1"
107+
resolved "https://registry.yarnpkg.com/@codemirror/view/-/view-6.7.1.tgz#370e95d6f001e7f5cadc459807974b4f0a6eb225"
108+
integrity sha512-kYtS+uqYw/q/0ytYxpkqE1JVuK5NsbmBklWYhwLFTKO9gVuTdh/kDEeZPKorbqHcJ+P+ucrhcsS1czVweOpT2g==
109+
dependencies:
110+
"@codemirror/state" "^6.1.4"
111+
style-mod "^4.0.0"
112+
w3c-keyname "^2.2.4"
113+
20114
"@emotion/is-prop-valid@^0.8.2":
21115
version "0.8.8"
22116
resolved "https://registry.yarnpkg.com/@emotion/is-prop-valid/-/is-prop-valid-0.8.8.tgz#db28b1c4368a259b60a97311d6a952d4fd01ac1a"
@@ -63,6 +157,33 @@
63157
resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45"
64158
integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==
65159

160+
"@lezer/common@^1.0.0":
161+
version "1.0.2"
162+
resolved "https://registry.yarnpkg.com/@lezer/common/-/common-1.0.2.tgz#8fb9b86bdaa2ece57e7d59e5ffbcb37d71815087"
163+
integrity sha512-SVgiGtMnMnW3ActR8SXgsDhw7a0w0ChHSYAyAUxxrOiJ1OqYWEKk/xJd84tTSPo1mo6DXLObAJALNnd0Hrv7Ng==
164+
165+
"@lezer/highlight@^1.0.0", "@lezer/highlight@^1.1.3":
166+
version "1.1.3"
167+
resolved "https://registry.yarnpkg.com/@lezer/highlight/-/highlight-1.1.3.tgz#bf5a36c2ee227f526d74997ac91f7777e29bd25d"
168+
integrity sha512-3vLKLPThO4td43lYRBygmMY18JN3CPh9w+XS2j8WC30vR4yZeFG4z1iFe4jXE43NtGqe//zHW5q8ENLlHvz9gw==
169+
dependencies:
170+
"@lezer/common" "^1.0.0"
171+
172+
"@lezer/javascript@^1.0.0":
173+
version "1.4.0"
174+
resolved "https://registry.yarnpkg.com/@lezer/javascript/-/javascript-1.4.0.tgz#fe71474fcadc6112fb0978310faed0788d0af824"
175+
integrity sha512-MQ3oLJGEtpUgZ03LOLI60tDnjSkKO6h9hZSe31qJ1UQV+I9bpv3pwSnPUnX0+e+3E1PBVkox0GB2/MXkxg0M2w==
176+
dependencies:
177+
"@lezer/highlight" "^1.0.0"
178+
"@lezer/lr" "^1.0.0"
179+
180+
"@lezer/lr@^1.0.0":
181+
version "1.2.5"
182+
resolved "https://registry.yarnpkg.com/@lezer/lr/-/lr-1.2.5.tgz#e9088164a711690596f17378665e0554157c9b03"
183+
integrity sha512-f9319YG1A/3ysgUE3bqCHEd7g+3ZZ71MWlwEc42mpnLVYXgfJJgtu1XAyBB4Kz8FmqmnFe9caopDqKeMMMAU6g==
184+
dependencies:
185+
"@lezer/common" "^1.0.0"
186+
66187
"@motionone/animation@^10.15.1":
67188
version "10.15.1"
68189
resolved "https://registry.yarnpkg.com/@motionone/animation/-/animation-10.15.1.tgz#4a85596c31cbc5100ae8eb8b34c459fb0ccf6807"
@@ -287,6 +408,40 @@
287408
"@typescript-eslint/types" "5.46.0"
288409
eslint-visitor-keys "^3.3.0"
289410

411+
"@uiw/codemirror-extensions-basic-setup@4.19.4":
412+
version "4.19.4"
413+
resolved "https://registry.yarnpkg.com/@uiw/codemirror-extensions-basic-setup/-/codemirror-extensions-basic-setup-4.19.4.tgz#faa18c671df937c4f370bbbfdf9ebb454e56f4ae"
414+
integrity sha512-iUni1tTpuSO6j1NShEo01eYGtED+9OYKyQ4Y54fBxluhZCT1Z/25sP05Im2rpt/VHE1fReRaC28FEBR5phBAfA==
415+
dependencies:
416+
"@codemirror/autocomplete" "^6.0.0"
417+
"@codemirror/commands" "^6.0.0"
418+
"@codemirror/language" "^6.0.0"
419+
"@codemirror/lint" "^6.0.0"
420+
"@codemirror/search" "^6.0.0"
421+
"@codemirror/state" "^6.0.0"
422+
"@codemirror/view" "^6.0.0"
423+
424+
"@uiw/codemirror-themes@^4.19.4":
425+
version "4.19.4"
426+
resolved "https://registry.yarnpkg.com/@uiw/codemirror-themes/-/codemirror-themes-4.19.4.tgz#78a443d98ec8261134349163efd01e939e4a3f22"
427+
integrity sha512-n2k1GPH9oTeeXY/Xbu5KQiw2rvrM5wkvhFDNRvoX9N/2e3+/lmyloZZ1C1Pk/OpDfOH2hWns8jiH7BKfn+I82A==
428+
dependencies:
429+
"@codemirror/language" "^6.0.0"
430+
"@codemirror/state" "^6.0.0"
431+
"@codemirror/view" "^6.0.0"
432+
433+
"@uiw/react-codemirror@^4.19.4":
434+
version "4.19.4"
435+
resolved "https://registry.yarnpkg.com/@uiw/react-codemirror/-/react-codemirror-4.19.4.tgz#0e5f2534705c24d166b17780496e2c96bf0991d9"
436+
integrity sha512-FGzrxLrY7t+u7ei7BJ3xlhUuRnSVg5N3mnFHXJzGlk6d1faHyoEv/2W2pLl/7BJPK2hL/tTGSieSNM9O1FxWEQ==
437+
dependencies:
438+
"@babel/runtime" "^7.18.6"
439+
"@codemirror/commands" "^6.1.0"
440+
"@codemirror/state" "^6.1.1"
441+
"@codemirror/theme-one-dark" "^6.0.0"
442+
"@uiw/codemirror-extensions-basic-setup" "4.19.4"
443+
codemirror "^6.0.0"
444+
290445
acorn-jsx@^5.3.2:
291446
version "5.3.2"
292447
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937"
@@ -534,6 +689,19 @@ client-only@0.0.1:
534689
resolved "https://registry.yarnpkg.com/client-only/-/client-only-0.0.1.tgz#38bba5d403c41ab150bff64a95c85013cf73bca1"
535690
integrity sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==
536691

692+
codemirror@^6.0.0:
693+
version "6.0.1"
694+
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-6.0.1.tgz#62b91142d45904547ee3e0e0e4c1a79158035a29"
695+
integrity sha512-J8j+nZ+CdWmIeFIGXEFbFPtpiYacFMDR8GlHK3IyHQJMCaVRfGx9NT+Hxivv1ckLWPvNdZqndbr/7lVhrf/Svg==
696+
dependencies:
697+
"@codemirror/autocomplete" "^6.0.0"
698+
"@codemirror/commands" "^6.0.0"
699+
"@codemirror/language" "^6.0.0"
700+
"@codemirror/lint" "^6.0.0"
701+
"@codemirror/search" "^6.0.0"
702+
"@codemirror/state" "^6.0.0"
703+
"@codemirror/view" "^6.0.0"
704+
537705
color-convert@^2.0.1:
538706
version "2.0.1"
539707
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3"
@@ -556,6 +724,11 @@ core-js-pure@^3.25.1:
556724
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.26.1.tgz#653f4d7130c427820dcecd3168b594e8bb095a33"
557725
integrity sha512-VVXcDpp/xJ21KdULRq/lXdLzQAtX7+37LzpyfFM973il0tWSsDEoyzG38G14AjTpK9VTfiNM9jnFauq/CpaWGQ==
558726

727+
crelt@^1.0.5:
728+
version "1.0.5"
729+
resolved "https://registry.yarnpkg.com/crelt/-/crelt-1.0.5.tgz#57c0d52af8c859e354bace1883eb2e1eb182bb94"
730+
integrity sha512-+BO9wPPi+DWTDcNYhr/W90myha8ptzftZT+LwcmUbbok0rcP/fequmFYCw8NMoH7pkAZQzU78b3kYrlua5a9eA==
731+
559732
cross-spawn@^7.0.2, cross-spawn@^7.0.3:
560733
version "7.0.3"
561734
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
@@ -2034,6 +2207,11 @@ strip-json-comments@^3.1.0, strip-json-comments@^3.1.1:
20342207
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
20352208
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==
20362209

2210+
style-mod@^4.0.0:
2211+
version "4.0.0"
2212+
resolved "https://registry.yarnpkg.com/style-mod/-/style-mod-4.0.0.tgz#97e7c2d68b592975f2ca7a63d0dd6fcacfe35a01"
2213+
integrity sha512-OPhtyEjyyN9x3nhPsu76f52yUGXiZcgvsrFVtvTkyGRQJ0XK+GPc6ov1z+lRpbeabka+MYEQxOYRnt5nF30aMw==
2214+
20372215
styled-jsx@5.1.0:
20382216
version "5.1.0"
20392217
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-5.1.0.tgz#4a5622ab9714bd3fcfaeec292aa555871f057563"
@@ -2189,6 +2367,11 @@ util-deprecate@^1.0.2:
21892367
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
21902368
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==
21912369

2370+
w3c-keyname@^2.2.4:
2371+
version "2.2.6"
2372+
resolved "https://registry.yarnpkg.com/w3c-keyname/-/w3c-keyname-2.2.6.tgz#8412046116bc16c5d73d4e612053ea10a189c85f"
2373+
integrity sha512-f+fciywl1SJEniZHD6H+kUO8gOnwIr7f4ijKA6+ZvJFjeGi1r4PDLl53Ayud9O/rk64RqgoQine0feoeOU0kXg==
2374+
21922375
which-boxed-primitive@^1.0.2:
21932376
version "1.0.2"
21942377
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"

0 commit comments

Comments
 (0)