Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit a8d492e

Browse files
mini-code-editor #1
1 parent 4d69c09 commit a8d492e

File tree

14 files changed

+26892
-2
lines changed

14 files changed

+26892
-2
lines changed

code-editor-react-web-app-project/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
![under_construction](https://user-images.githubusercontent.com/37651620/93677983-a7942e00-facc-11ea-8b6d-b57e73dc73bf.png)
1+
## It's Live 🎉 Visit here ==> https://mini-code-editor.netlify.app/
22

3-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
3+
---
4+
5+
![code_editor](https://user-images.githubusercontent.com/37651620/93988236-32647980-fda8-11ea-9052-3238ea4a42b2.png)
6+
7+
<span>Code inside the above image is from <a href="https://codepen.io/nocni_sovac/pen/poyabaB">
8+
Codepen </a> by Zarko Rvovic (<a href="https://codepen.io/nocni_sovac">@nocni_sovac</a>)
9+
on <a href="https://codepen.io">CodePen</a>.</span>
10+
11+
---
412

513
## Available Scripts
614

code-editor-react-web-app-project/package-lock.json

Lines changed: 15780 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "code-editor-react-web-app-project",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^4.2.4",
7+
"@testing-library/react": "^9.3.2",
8+
"@testing-library/user-event": "^7.1.2",
9+
"codemirror": "^5.58.0",
10+
"react": "^16.13.1",
11+
"react-codemirror2": "^7.2.1",
12+
"react-dom": "^16.13.1",
13+
"react-scripts": "3.4.3"
14+
},
15+
"scripts": {
16+
"start": "react-scripts start",
17+
"build": "react-scripts build",
18+
"test": "react-scripts test",
19+
"eject": "react-scripts eject"
20+
},
21+
"eslintConfig": {
22+
"extends": "react-app"
23+
},
24+
"browserslist": {
25+
"production": [
26+
">0.2%",
27+
"not dead",
28+
"not op_mini all"
29+
],
30+
"development": [
31+
"last 1 chrome version",
32+
"last 1 firefox version",
33+
"last 1 safari version"
34+
]
35+
}
36+
}
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
14+
<title>Mini code Editor</title>
15+
</head>
16+
<body>
17+
<noscript>You need to enable JavaScript to run this app.</noscript>
18+
<div id="root"></div>
19+
</body>
20+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React, { useState, useEffect } from "react";
2+
import "../styles/App.css";
3+
import CodeEditor from "./CodeEditor";
4+
import useLocalStorage from "../hooks/useLocalStorage";
5+
6+
function App() {
7+
const [html, setHtml] = useLocalStorage("html", "");
8+
const [css, setCss] = useLocalStorage("css", "");
9+
const [js, setJs] = useLocalStorage("js", "");
10+
const [srcDoc, setSrcDoc] = useState("");
11+
12+
useEffect(() => {
13+
const timeout = setTimeout(() => {}, 20);
14+
setSrcDoc(`
15+
<html>${html}</html>
16+
<style>${css}</style>
17+
<script>${js}</script>
18+
19+
`);
20+
21+
return () => clearTimeout(timeout);
22+
}, [html, css, js]);
23+
24+
return (
25+
<>
26+
<div className="pane top-pane">
27+
<CodeEditor
28+
language="xml"
29+
displayName="HTML"
30+
value={html}
31+
onChange={setHtml}
32+
/>
33+
<CodeEditor
34+
language="css"
35+
displayName="CSS"
36+
value={css}
37+
onChange={setCss}
38+
/>
39+
<CodeEditor
40+
language="javascript"
41+
displayName="JAVASCRIPT"
42+
value={js}
43+
onChange={setJs}
44+
/>
45+
</div>
46+
<div className="pane">
47+
<iframe
48+
srcDoc={srcDoc}
49+
title="output"
50+
sandbox="allow-scripts"
51+
frameBorder="0"
52+
width="100%"
53+
height="100%"
54+
/>
55+
</div>
56+
</>
57+
);
58+
}
59+
60+
export default App;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from "react";
2+
import "codemirror/lib/codemirror.css";
3+
import "codemirror/theme/material.css";
4+
import "codemirror/mode/xml/xml";
5+
import "codemirror/mode/javascript/javascript";
6+
import "codemirror/mode/css/css";
7+
import { Controlled as ControlledEditor } from "react-codemirror2";
8+
9+
export default function Editor(props) {
10+
const { displayName, language, value, onChange } = props;
11+
function handleChange(editor, data, value) {
12+
onChange(value);
13+
}
14+
return (
15+
<div className="code-container">
16+
<div className="code-editor-title">{displayName}</div>
17+
<ControlledEditor
18+
onBeforeChange={handleChange}
19+
value={value}
20+
className="editor-mirror-code-wrapper"
21+
options={{
22+
lineWrapping: true,
23+
lint: true,
24+
mode: language,
25+
theme: "material",
26+
lineNumbers: true,
27+
}}
28+
/>
29+
</div>
30+
);
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useEffect, useState } from "react";
2+
3+
const PREFIX = "mini-code-editor";
4+
export default function useLocalStorage(key, initialValue) {
5+
const prefixedkey = PREFIX + key;
6+
7+
const [value, setValue] = useState(() => {
8+
const jsonValue = localStorage.getItem(prefixedkey);
9+
if (jsonValue != null) return JSON.parse(jsonValue);
10+
if (typeof initialValue === "function") {
11+
return initialValue();
12+
} else {
13+
return initialValue;
14+
}
15+
});
16+
17+
useEffect(() => {
18+
localStorage.setItem(prefixedkey, JSON.stringify(value));
19+
}, [prefixedkey, value]);
20+
21+
return [value, setValue];
22+
}

0 commit comments

Comments
 (0)