Skip to content

Commit 8bfda58

Browse files
Added syntax highlighter and formatting options
1 parent 277adcb commit 8bfda58

File tree

5 files changed

+59
-25
lines changed

5 files changed

+59
-25
lines changed

constants/theme.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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: "#282c34;",
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.separator, color: "orange" },
18+
{ tag: t.bool, color: "blue" },
19+
{ tag: t.string, color: "#98c379" },
20+
],
21+
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
},
1111
"dependencies": {
1212
"@codemirror/lang-javascript": "^6.1.2",
13+
"@lezer/highlight": "^1.1.3",
14+
"@uiw/codemirror-themes": "^4.19.4",
1315
"@uiw/react-codemirror": "^4.19.4",
1416
"classnames": "^2.3.2",
1517
"eslint": "8.29.0",

pages/_app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import "../styles/application.scss";
2-
// import 'codemirror/lib/codemirror.css'
32

43
function MyApp({ Component, pageProps }) {
54
return <Component {...pageProps} />;

pages/index.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
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

57
import Head from "next/head";
68
import 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 /> */}

yarn.lock

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
resolved "https://registry.yarnpkg.com/@lezer/common/-/common-1.0.2.tgz#8fb9b86bdaa2ece57e7d59e5ffbcb37d71815087"
163163
integrity sha512-SVgiGtMnMnW3ActR8SXgsDhw7a0w0ChHSYAyAUxxrOiJ1OqYWEKk/xJd84tTSPo1mo6DXLObAJALNnd0Hrv7Ng==
164164

165-
"@lezer/highlight@^1.0.0":
165+
"@lezer/highlight@^1.0.0", "@lezer/highlight@^1.1.3":
166166
version "1.1.3"
167167
resolved "https://registry.yarnpkg.com/@lezer/highlight/-/highlight-1.1.3.tgz#bf5a36c2ee227f526d74997ac91f7777e29bd25d"
168168
integrity sha512-3vLKLPThO4td43lYRBygmMY18JN3CPh9w+XS2j8WC30vR4yZeFG4z1iFe4jXE43NtGqe//zHW5q8ENLlHvz9gw==
@@ -421,6 +421,15 @@
421421
"@codemirror/state" "^6.0.0"
422422
"@codemirror/view" "^6.0.0"
423423

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+
424433
"@uiw/react-codemirror@^4.19.4":
425434
version "4.19.4"
426435
resolved "https://registry.yarnpkg.com/@uiw/react-codemirror/-/react-codemirror-4.19.4.tgz#0e5f2534705c24d166b17780496e2c96bf0991d9"

0 commit comments

Comments
 (0)