|
| 1 | +import React, { useEffect, useState, useRef } from 'react'; |
| 2 | + |
| 3 | +const BasicEditor: React.FC= () => { |
| 4 | + const [checker, setChecker] = useState({ html: true, css: false, js: false,settings:false,fontSize:10,fontColor:false,backcolor:false }); |
| 5 | + const [html, setHtml] = useState(""); |
| 6 | + const [css, setCss] = useState(""); |
| 7 | + const [js, setJs] = useState(""); |
| 8 | + const iframeRef = useRef<HTMLIFrameElement | null>(null); |
| 9 | + |
| 10 | + useEffect(() => { |
| 11 | + if (!iframeRef.current) { |
| 12 | + alert("iframeRef is not available"); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + const iframe = iframeRef.current; |
| 17 | + const document = iframe.contentDocument; |
| 18 | + |
| 19 | + if (!document) { |
| 20 | + alert("iframe contentDocument is not available"); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const documentContents = ` |
| 25 | + <!DOCTYPE html> |
| 26 | + <html lang="en"> |
| 27 | + <head> |
| 28 | + <style>${css}</style> |
| 29 | + </head> |
| 30 | + <body> |
| 31 | + ${html} |
| 32 | + <script>${js}</script> |
| 33 | + </body> |
| 34 | + </html> |
| 35 | + `; |
| 36 | + |
| 37 | + document.open(); |
| 38 | + document.write(documentContents); |
| 39 | + document.close(); |
| 40 | + }, [html, css, js]); |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className='normal_editor'> |
| 44 | + <div className='editor_box'> |
| 45 | + <div className='editor_nav'> |
| 46 | + <button className={!checker.html ? 'btn_activated html' : 'btn_deactivated'} onClick={() => setChecker({ html: true, css: false, js: false })}>HTML</button> |
| 47 | + <button className={!checker.css ? 'btn_activated css' : 'btn_deactivated'} onClick={() => setChecker({ html: false, css: true, js: false })}>CSS</button> |
| 48 | + <button className={!checker.js ? 'btn_activated js' : 'btn_deactivated'} onClick={() => setChecker({ html: false, css: false, js: true })}>JS</button> |
| 49 | + </div> |
| 50 | + <div className='editor_settings_nav'> |
| 51 | + <div className={`editor_settings_menu_con ${checker.settings?"activate_menu":"deactivate_menu"}`}> |
| 52 | + <div className='editor_settings_menu'> |
| 53 | + <p>Font Size</p> |
| 54 | + <button onClick={()=>setChecker(pre=>({...pre,fontSize:pre.fontSize!==30?pre.fontSize+1:pre.fontSize}))}>+</button> |
| 55 | + <p>{checker.fontSize}</p> |
| 56 | + <button onClick={()=>setChecker(pre=>({...pre,fontSize:pre.fontSize!==10?pre.fontSize-1:pre.fontSize}))}>-</button> |
| 57 | + </div> |
| 58 | + <div className='editor_settings_menu'> |
| 59 | + <p>color</p> |
| 60 | + <input |
| 61 | + type="color" |
| 62 | + value={checker.fontColor} |
| 63 | + onChange={(e)=>setChecker(pre=>({...pre,fontColor:e.target.value}))} |
| 64 | + /> |
| 65 | + </div> |
| 66 | + <div className='editor_settings_menu'> |
| 67 | + <p>Bakcground color</p> |
| 68 | + <input |
| 69 | + type="color" |
| 70 | + value={checker.backcolor} |
| 71 | + onChange={(e)=>setChecker(pre=>({...pre,backcolor:e.target.value}))} |
| 72 | + /> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + <div className='editor_setting_icon'> |
| 76 | + <img src="https://cdn-icons-png.flaticon.com/128/9333/9333993.png" onClick={()=>setChecker(pre => ({...pre, settings: !pre.settings}))} alt='settings'/> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + <textarea style={{fontSize:`${checker.fontSize}pt`,color:checker.fontColor==false?"unset":checker.fontColor,background:checker.backcolor==false?"transparent":checker.backcolor}} className={checker.html ? 'editor_textarea_activate' : 'editor_textarea_deactivate'} value={html} placeholder="HTML" onChange={(e) => setHtml(e.target.value)}></textarea> |
| 80 | + <textarea style={{fontSize:`${checker.fontSize}pt`,color:checker.fontColor==false?"unset":checker.fontColor,background:checker.backcolor==false?"transparent":checker.backcolor}} className={checker.css ? 'editor_textarea_activate' : 'editor_textarea_deactivate'} value={css} placeholder="CSS" onChange={(e) => setCss(e.target.value)}></textarea> |
| 81 | + <textarea style={{fontSize:`${checker.fontSize}pt`,color:checker.fontColor==false?"unset":checker.fontColor,background:checker.backcolor==false?"transparent":checker.backcolor}} className={checker.js ? 'editor_textarea_activate' : 'editor_textarea_deactivate'} value={js} placeholder="JavaScript" onChange={(e) => setJs(e.target.value)}></textarea> |
| 82 | + </div> |
| 83 | + <iframe ref={iframeRef} className='output_container' id="preview"></iframe> |
| 84 | + </div> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +export default BasicEditor; |
0 commit comments