Create an online code-editor for HTML, CSS, JS code snippets using only HTML, CSS and React. The code editor’s functionality will be similar to that of codpen.io
Create an online code-editor for HTML, CSS, JS code snippets using only HTML, CSS and React. The code editor’s functionality will be similar to that of codpen.io
Online code-editor is a tool that resides on a remote server and is accessible via browsers. Some online code editors have basic features like syntax highlighting or code completion more similar to text editors while others are like complete IDEs.
For any developer, be it amateur or professional, often the liberty of using a local code editor may be unavailable. As online code-editors are fast, efficient and greatly popular, it is a familiar tool among developers. If you have used one, ever wondered how it can be made? This module will guide you through the process that can be followed to build your own code-editor for HTML, CSS, JS code snippets. Implementing the project will add an immense value to your profile.
This project is a good start for beginners, a new idea for intermediates and a refreshing hobby project for professionals. It involves the basic use of all of HTML, CSS and React languages. Reason for following this tech-stack is that these languages are easy to use and are also fast in terms of execution time.
We can segregate the product architecture based on tools used:
This module consists of the following milestones:
The desired end result of this project is like this:
Some of the applications of online code editors are-
Create an online code-editor for HTML, CSS, JS code snippets using only HTML, CSS and React. The code editor’s functionality will be similar to that of codpen.io
Online code-editor is a tool that resides on a remote server and is accessible via browsers. Some online code editors have basic features like syntax highlighting or code completion more similar to text editors while others are like complete IDEs.
For any developer, be it amateur or professional, often the liberty of using a local code editor may be unavailable. As online code-editors are fast, efficient and greatly popular, it is a familiar tool among developers. If you have used one, ever wondered how it can be made? This module will guide you through the process that can be followed to build your own code-editor for HTML, CSS, JS code snippets. Implementing the project will add an immense value to your profile.
This project is a good start for beginners, a new idea for intermediates and a refreshing hobby project for professionals. It involves the basic use of all of HTML, CSS and React languages. Reason for following this tech-stack is that these languages are easy to use and are also fast in terms of execution time.
We can segregate the product architecture based on tools used:
This module consists of the following milestones:
The desired end result of this project is like this:
Some of the applications of online code editors are-
First validate the idea by doing a low level implementation (Proof of Concept) of the components involved in the project.
This helps you to:
Explore various aspects of the terms mentioned in the preceding sections and also understand the preference to use React over Angular or Vue, XML vs HTML, why use HTML5/CSS3 and not the other versions, etc.
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.31",
"@fortawesome/free-brands-svg-icons": "^5.15.0",
"@fortawesome/free-solid-svg-icons": "^5.15.0",
"@fortawesome/react-fontawesome": "^0.1.11",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"codemirror": "^5.58.1",
"react": "^16.13.1",
"react-codemirror2": "^7.2.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.3"
},
To run the react application simply type npm start
in the terminal and you will get a live server started.
In this task the basic structure of the code-editor is built (excluding the core functionality) using HTML and React.
app.js
to accommodate them.Editor
(preferably) which will be used in taking input of HTML, CSS, JS code snippets in their respective code containers.<div className="pane top-pane">
<Editor /*HTML code editor*//>
<Editor /*CSS code editor*//>
<Editor /*JS code editor*//>
</div>
iframe
element must be used.<div className="bottom-pane">
<iframe
srcDoc={srcDoc}
/*Title option*/
/*sandbox option*/
/*frameBorder option*/
/*width-height option*/
/*Title option*/
></iframe>
</div>
editor.js
and import it to your app.js
to check whether the custom component Editor
is working or not.export default Editor;
import Editor from "./Editor";
Since only a dummy text is being returned by the editor function, at the end of this milestone the app should look something like this (NOTE: The iframe
is not rendering anything now, so it has no effect) -
Start building the code-editor by creating the Editor component that will be taking care of the code fed into respective sections (i.e. HTML, CSS, JS code snippets).
The ‘editor’ component will be responsible for validating the code in their respective sections (code containers) and storing it for further uses.
react-codemirror2
package.import "codemirror/lib/codemirror.css";
import "codemirror/theme/material.css";
import /* codemirror XML (HTML) mode */
import /* codemirror CSS mode */
import /* codemirror JS mode */
<ControlledEditor
onBeforeChange={/* Pass in a custom made function that will handle the change */}
value={/* Give the value */}
className= /* Give an appropriate name for styling reference in CSS */
options={{
/* Give all relevant options */
}}
/>
function handleChange(editor, data, /* An argument that handles the value of this function */) {
onChange(/* The passed in argument that decides this function's value (argument) */);
}
useState
hook as null. This step will be used for the editor tag’s value and onChange event handler’s arguments.Now that the custom component Editor
is in place, the code-editor should look something like the image below. Try inputting some sample code and see if syntax highlighting has been implemented or not. Of course the inputted code won’t be rendered now.
[NOTE: Your output may look different due to styling variations, but it will look same in the end as you keep progressing]
Without styling, the appearance will be bland and not appealing. Thus, this task deals with the required styling needed to beautify the code-editor using only CSS.
.CodeMirror {
height: 100% !important;
}
body {}
.top-pane {}
.top-header {}
.top-header .clear-all-btn {}
.top-header .clear-all-btn:hover {}
.pane {}
.editor-container {}
.editor-title {}
.editor-container.collapsed {}
.editor-container.collapsed .CodeMirror-scroll {
position: absolute;
overflow: hidden !important;
}
.CodeMirror {
height: 100% !important;
}
.code-mirror-wrapper {}
.expand-collapse-btn {}
.ct {}
.ti {}
@font-face {
font-family: "Roboto";
src: local("Roboto"), url(./font/Roboto-Regular.ttf) format("truetype");
}
After implementing the styling the code-editor should look something like the image given below. Keep improving the styling as you wish, but before that move on to the next milestones first to complete the code-editor.
Implement the the core functionality of the code-editor, i.e. render the webpage corresponding to the code inputs (by implementing the code of iframe
element mentioned in milestone 1)
app.js
that needs to be used inside the iframe
element to render the website. To render any website a HTML code is sufficient with all of HTML, CSS, JS codes wrapped properly in it. So, create this variable (which is ideally a template string) such that it understands language specific inputs and accordingly creates a HTML page with the relevant source code of each language wrapped in its tag (like <body> for HTML) .iframe
as one of its arguments you will be able to render pages.source document
variable: The tag that binds HTML code is <body>...</body>
, tag that binds CSS is <style>...</style>
and for JS is <script>...</script>
. Using a template string, the contents of each of tags can be input as variables/expressions.const srcDoc = `
<html>
<body>${html}</body>
/* CSS code */
/* JS code */
</html>
`
useEffect
react componentuseEffect(() => {
const timeout = setTimeout(() => {
/* Source Document Code as function */
}, 250);
return () => clearTimeout(timeout);
}, [html, css, js, /* Source document function call so that the site responds again to new code changes */]);
Now that the core functionalities are implemented the code-editor is good to use now. So the webpage rendered by the code-editor should look something like this
The refresh button feature along with all the icons will look and function as shown in the demo below.
The editor should not lose its data on a direct page reload (i.e. other than the refresh button option). To save them in local storage for one particular instance of code run, using a hook is an easy, straightforward solution. Also the site buttons will look plain as of now. Use fontawesome icons of React library to convert them into edgy icons.
useLocalStorage.js
file.useEffect
hook to make sure that when the value (associated with useState hook) or the key value changes, the key is set as the stringified version of value (JSON).app.js
in place of previously used useState of all language-specific useState’s defined before.import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCompressAlt, faExpandAlt } from "@fortawesome/free-solid-svg-icons";
import { faHtml5, faCss3Alt, faJs } from "@fortawesome/free-brands-svg-icons";
useLocalstorage.js
try completing the below code. Then try your own logic.import { useState, useEffect } from "react";
const PREFIX = /* To prefix your key so that you can easily identify your localhost instance */;
function useLocalStorage(key, initialValue) {
const prefixedKey = PREFIX + key;
const [value, setValue] = useState(() => { /*Use states*/
const jsonValue = /*Get the items from local storage based on the prefix key */;
if (jsonValue != null) return /* Parsed value of jsonValue */;
if (typeof initialValue === "function") {
return /* initialValue as a function*/;
} else {
return /* initialValue as a variable*/;
}
});
useEffect(() => {
if(!value) setValue(/* Set to null */)
localStorage.setItem(prefixedKey, JSON.stringify(value));
/* Set the localstorage's prefixed key with the new stringified json of value */
}, [prefixedKey, value]);
return [value, setValue];
}
export default useLocalStorage;
Your final code-editor should function as shown in the demo below. Do keep improving your code-editor once this stage has been reached.
Finish your work in complete style.
Publish your work on GitHub with proper folder structure and a good README for people to get to know of your work. Make sure you add a .gitignore file and mention all dependency folders like node_modules in it.
Since this is a React application, go one step further by using a hosting service like Heroku, Firebase or Netlify to host your app and get a live URL.
Share this link among your peers and add this project (with the link and proper documentation) to your resume and voila, you have just boosted your resume.