Skip to content

[Feature]: Add Basic live Editor #4034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/pages/LiveEditor/BasicEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { useEffect, useState, useRef } from 'react';

const BasicEditor: React.FC = () => {
const [checker, setChecker] = useState({ html: true, css: false, js: false });
const [html, setHtml] = useState("");
const [css, setCss] = useState("");
const [js, setJs] = useState("");
const iframeRef = useRef<HTMLIFrameElement | null>(null);

useEffect(() => {
if (!iframeRef.current) {
alert("iframeRef is not available");
return;
}

const iframe = iframeRef.current;
const document = iframe.contentDocument;

if (!document) {
alert("iframe contentDocument is not available");
return;
}

const documentContents = `
<!DOCTYPE html>
<html lang="en">
<head>
<style>${css}</style>
</head>
<body>
${html}
<script>${js}</script>
</body>
</html>
`;

document.open();
document.write(documentContents);
document.close();
}, [html, css, js]);

return (
<div className='normal_editor'>
<div className='editor_box'>
<div className='editor_nav'>
<button className={!checker.html ? 'btn_activated html' : 'btn_deactivated'} onClick={() => setChecker({ html: true, css: false, js: false })}>HTML</button>
<button className={!checker.css ? 'btn_activated css' : 'btn_deactivated'} onClick={() => setChecker({ html: false, css: true, js: false })}>CSS</button>
<button className={!checker.js ? 'btn_activated js' : 'btn_deactivated'} onClick={() => setChecker({ html: false, css: false, js: true })}>JS</button>
</div>
<textarea className={checker.html ? 'editor_textarea_activate' : 'editor_textarea_deactivate'} value={html} placeholder="HTML" onChange={(e) => setHtml(e.target.value)}></textarea>
<textarea className={checker.css ? 'editor_textarea_activate' : 'editor_textarea_deactivate'} value={css} placeholder="CSS" onChange={(e) => setCss(e.target.value)}></textarea>
<textarea className={checker.js ? 'editor_textarea_activate' : 'editor_textarea_deactivate'} value={js} placeholder="JavaScript" onChange={(e) => setJs(e.target.value)}></textarea>
</div>
<iframe ref={iframeRef} className='output_container' id="preview"></iframe>
</div>
);
}

export default BasicEditor;
43 changes: 43 additions & 0 deletions src/pages/LiveEditor/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.editor_select_container{display:flex;flex-wrap:wrap;justify-content:center;align-items:center;height:25rem;width:100vw}
.editor_selcet_text{font-family:monospace;position:absolute;top:2%}
.editor_select_imgage_container{display:flex;flex-direction:column;align-items:center}
.editor_select_image{height:10rem}
.editor_container{flex:1 0 20rem;display:flex;gap:1rem;justify-content:center;align-items:center;position:relative;padding:3rem;cursor:pointer}
.editor_container:hover{border: 1px solid;}
.normal_editor{display: flex;flex-wrap: wrap;justify-content: center;align-items: center;width:100vw}
.normal_editor .editor_box,.normal_editor .output_container{
flex: 1 0 30rem;
height: 35rem;
position:relative;
}
.normal_editor .output_container{background: white;}
.editor_box .editor_nav{position: absolute;z-index: 2;gap: 1rem;display: flex;top: 2%;justify-content: center;align-items: center;right: 1%;}
.editor_nav .btn_activated{padding: 0.3rem 1rem;border: none;font-family: monospace;font-weight: 600;cursor: pointer;}
.editor_nav .html{background: rgb(230,81,0);color: white;}
.editor_nav .css{background:rgb(2,119,189);color: white;}
.editor_nav .js{background:rgb(255,214,0);color: rgb(0, 0, 0);}
.editor_nav .btn_deactivated{display: none;}
.editor_textarea_activate{
font-family: "Courier New", Courier, "Lucida Sans Typewriter", "Lucida Typewriter", monospace;
width: 100%;
min-height: 100%;
float: left;
font-size: 10pt;
line-height: 14pt;
font-weight: 200;
padding: 10px;
border: 1px solid;
background: transparent;
box-shadow: inset rgba(0,0,0,.05) 0 3px 10px;
border-radius: 3px;
transition: all 0.2s ease-in-out;
appearance: none;
-moz-appearance: none;
-webkit-appearance: none;
}

.editor_textarea_activate:focus {
border-color: #33dd33;
}
.editor_textarea_deactivate{display: none;}

45 changes: 39 additions & 6 deletions src/pages/LiveEditor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React,{useEffect, useState} from "react";
import React,{useState,useEffect} from "react";
import Layout from "@theme/Layout";

import "./index.css";
import BasicEditor from "./BasicEditor";
const html="/icons/html-5.svg"
const css="/icons/css.svg"
const Js="/icons/js.svg"
const react="/icons/jsx.svg"
export default function LiveEditor() {
const [state,setState]=useState("none")
const [Theme,setNewTheme]=useState("dark")

useEffect(() => {
Expand Down Expand Up @@ -42,8 +48,34 @@ export default function LiveEditor() {
alignItems: "center"
}}
>
<iframe
src={`https://codesandbox.io/embed/github/Ajay-Dhangar/my-react-app/main?fontsize=14&hidenavigation=1&theme=${Theme==="dark"?"dark":"light"}`}
{state==="none"?
<div className="editor_select_container">
<div className="editor_container" onClick={()=>setState("basic")}>
<h2 className="editor_selcet_text">HTML CSS JS Editor</h2>
<div className="editor_select_imgage_container">
<img src={html} alt="html" className="editor_select_image" />
<h2>HTML</h2>
</div>
<div className="editor_select_imgage_container">
<img src={css} alt="css" className="editor_select_image" />
<h2>CSS</h2>
</div>
<div className="editor_select_imgage_container">
<img src={Js} alt="js" className="editor_select_image" />
<h2>JavaScript</h2>
</div>
</div>
<div className="editor_container" onClick={()=>setState("advance")}>
<h2 className="editor_selcet_text">React.Js Editor</h2>
<div className="editor_select_imgage_container">
<img src={react} alt="react" className="editor_select_image"/>
<h2>React.JS</h2>
</div>
</div>
</div>
:state==="advance"?
<iframe
src={`https://codesandbox.io/embed/github/Ajay-Dhangar/my-react-app/main?fontsize=14&hidenavigation=1&theme=${Theme==="dark"?"dark":"light"}`}
style={{
width: "100%",
height: "600px",
Expand All @@ -57,8 +89,9 @@ export default function LiveEditor() {
title="dazzling-swanson-wne32"
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
sandbox="allow-autoplay allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
/>
/> :<BasicEditor />
}
</div>
</Layout>
);
}
}
Loading