Spaces:
Build error
Build error
Mehdi commited on
Upload components/CodeEditor.jsx with huggingface_hub
Browse files- components/CodeEditor.jsx +108 -298
components/CodeEditor.jsx
CHANGED
|
@@ -1,316 +1,126 @@
|
|
| 1 |
-
import { useState
|
| 2 |
-
import
|
| 3 |
-
import {
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
import toast from 'react-hot-toast'
|
| 19 |
-
|
| 20 |
-
export default function CodeEditor({ language, theme }) {
|
| 21 |
-
const [code, setCode] = useState(`// Welcome to AI Dev Studio
|
| 22 |
-
function helloWorld() {
|
| 23 |
-
console.log("Hello, World!");
|
| 24 |
-
return "Welcome to the future of coding!";
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
helloWorld();`)
|
| 28 |
-
const [currentLanguage, setCurrentLanguage] = useState('javascript')
|
| 29 |
-
const [theme, setTheme] = useState('vs-dark')
|
| 30 |
-
const [isFullscreen, setIsFullscreen] = useState(false)
|
| 31 |
-
const [showTerminal, setShowTerminal] = useState(false)
|
| 32 |
-
const [terminalOutput, setTerminalOutput] = useState([])
|
| 33 |
-
const [fontSize, setFontSize] = useState(14)
|
| 34 |
-
const [wordWrap, setWordWrap] = useState(true)
|
| 35 |
-
const [minimap, setMinimap] = useState(true)
|
| 36 |
-
const [lineNumbers, setLineNumbers] = useState(true)
|
| 37 |
-
const editorRef = useRef(null)
|
| 38 |
-
|
| 39 |
-
const languages = [
|
| 40 |
-
{ value: 'javascript', label: 'JavaScript' },
|
| 41 |
-
{ value: 'typescript', label: 'TypeScript' },
|
| 42 |
-
{ value: 'python', label: 'Python' },
|
| 43 |
-
{ value: 'java', label: 'Java' },
|
| 44 |
-
{ value: 'cpp', label: 'C++' },
|
| 45 |
-
{ value: 'csharp', label: 'C#' },
|
| 46 |
-
{ value: 'php', label: 'PHP' },
|
| 47 |
-
{ value: 'ruby', label: 'Ruby' },
|
| 48 |
-
{ value: 'go', label: 'Go' },
|
| 49 |
-
{ value: 'rust', label: 'Rust' },
|
| 50 |
-
{ value: 'sql', label: 'SQL' },
|
| 51 |
-
{ value: 'html', label: 'HTML' },
|
| 52 |
-
{ value: 'css', label: 'CSS' },
|
| 53 |
-
{ value: 'json', label: 'JSON' },
|
| 54 |
-
{ value: 'xml', label: 'XML' },
|
| 55 |
-
{ value: 'yaml', label: 'YAML' },
|
| 56 |
-
{ value: 'markdown', label: 'Markdown' },
|
| 57 |
-
{ value: 'dockerfile', label: 'Dockerfile' },
|
| 58 |
-
{ value: 'shell', label: 'Shell' },
|
| 59 |
-
{ value: 'plaintext', label: 'Plain Text' }
|
| 60 |
-
]
|
| 61 |
-
|
| 62 |
-
const handleRunCode = () => {
|
| 63 |
-
const output = `> Running ${currentLanguage} code...\n> Code executed successfully!\n> Output: Hello, World!`
|
| 64 |
-
setTerminalOutput(prev => [...prev, { type: 'success', message: output, timestamp: new Date() }])
|
| 65 |
-
setShowTerminal(true)
|
| 66 |
-
toast.success('Code executed successfully!')
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
const handleSaveCode = () => {
|
| 70 |
-
toast.success('Code saved successfully!')
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
const handleDownloadCode = () => {
|
| 74 |
-
const blob = new Blob([code], { type: 'text/plain' })
|
| 75 |
-
const url = URL.createObjectURL(blob)
|
| 76 |
-
const a = document.createElement('a')
|
| 77 |
-
a.href = url
|
| 78 |
-
a.download = `code.${getFileExtension()}`
|
| 79 |
-
a.click()
|
| 80 |
-
toast.success('Code downloaded!')
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
const getFileExtension = () => {
|
| 84 |
-
const extensions = {
|
| 85 |
-
javascript: 'js',
|
| 86 |
-
typescript: 'ts',
|
| 87 |
-
python: 'py',
|
| 88 |
-
java: 'java',
|
| 89 |
-
cpp: 'cpp',
|
| 90 |
-
csharp: 'cs',
|
| 91 |
-
php: 'php',
|
| 92 |
-
ruby: 'rb',
|
| 93 |
-
go: 'go',
|
| 94 |
-
rust: 'rs',
|
| 95 |
-
sql: 'sql',
|
| 96 |
-
html: 'html',
|
| 97 |
-
css: 'css',
|
| 98 |
-
json: 'json',
|
| 99 |
-
xml: 'xml',
|
| 100 |
-
yaml: 'yaml',
|
| 101 |
-
markdown: 'md',
|
| 102 |
-
dockerfile: 'dockerfile',
|
| 103 |
-
shell: 'sh',
|
| 104 |
-
plaintext: 'txt'
|
| 105 |
-
}
|
| 106 |
-
return extensions[currentLanguage] || 'txt'
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
const formatCode = () => {
|
| 110 |
-
if (editorRef.current) {
|
| 111 |
-
editorRef.current.getAction('editor.action.formatDocument').run()
|
| 112 |
-
toast.success('Code formatted!')
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
-
const
|
| 117 |
-
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
const clearTerminal = () => {
|
| 121 |
-
setTerminalOutput([])
|
| 122 |
-
toast.success('Terminal cleared!')
|
| 123 |
}
|
| 124 |
|
| 125 |
return (
|
| 126 |
-
<div className=
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
className="px-
|
| 135 |
-
>
|
| 136 |
-
{languages.map(lang => (
|
| 137 |
-
<option key={lang.value} value={lang.value}>{lang.label}</option>
|
| 138 |
-
))}
|
| 139 |
-
</select>
|
| 140 |
-
<span className="text-xs text-gray-400">
|
| 141 |
-
{code.split('\n').length} lines • {code.length} characters
|
| 142 |
-
</span>
|
| 143 |
-
</div>
|
| 144 |
-
|
| 145 |
-
<div className="flex items-center space-x-reverse space-x-2">
|
| 146 |
-
<button
|
| 147 |
-
onClick={formatCode}
|
| 148 |
-
className="p-2 bg-gray-700 text-white rounded-lg hover:bg-gray-600 transition-all"
|
| 149 |
-
title="Format Code"
|
| 150 |
>
|
| 151 |
-
|
| 152 |
</button>
|
| 153 |
-
<button
|
| 154 |
-
onClick={
|
| 155 |
-
className="
|
| 156 |
-
title="Save"
|
| 157 |
>
|
| 158 |
-
|
| 159 |
-
</button>
|
| 160 |
-
<button
|
| 161 |
-
onClick={handleDownloadCode}
|
| 162 |
-
className="p-2 bg-gray-700 text-white rounded-lg hover:bg-gray-600 transition-all"
|
| 163 |
-
title="Download"
|
| 164 |
-
>
|
| 165 |
-
<FaDownload className="w-4 h-4" />
|
| 166 |
-
</button>
|
| 167 |
-
<button
|
| 168 |
-
onClick={handleRunCode}
|
| 169 |
-
className="p-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-all"
|
| 170 |
-
title="Run Code"
|
| 171 |
-
>
|
| 172 |
-
<FaPlay className="w-4 h-4" />
|
| 173 |
-
</button>
|
| 174 |
-
<button
|
| 175 |
-
onClick={() => setShowTerminal(!showTerminal)}
|
| 176 |
-
className={`p-2 rounded-lg transition-all ${
|
| 177 |
-
showTerminal ? 'bg-primary-600 text-white' : 'bg-gray-700 text-white hover:bg-gray-600'
|
| 178 |
-
}`}
|
| 179 |
-
title="Toggle Terminal"
|
| 180 |
-
>
|
| 181 |
-
<FaTerminal className="w-4 h-4" />
|
| 182 |
-
</button>
|
| 183 |
-
<button
|
| 184 |
-
onClick={toggleFullscreen}
|
| 185 |
-
className="p-2 bg-gray-700 text-white rounded-lg hover:bg-gray-600 transition-all"
|
| 186 |
-
title="Toggle Fullscreen"
|
| 187 |
-
>
|
| 188 |
-
{isFullscreen ? <FaCompress className="w-4 h-4" /> : <FaExpand className="w-4 h-4" />}
|
| 189 |
</button>
|
| 190 |
</div>
|
| 191 |
</div>
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
<
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
type="checkbox"
|
| 219 |
-
checked={minimap}
|
| 220 |
-
onChange={(e) => setMinimap(e.target.checked)}
|
| 221 |
-
className="rounded"
|
| 222 |
-
/>
|
| 223 |
-
<span>Minimap</span>
|
| 224 |
-
</label>
|
| 225 |
-
<label className="flex items-center space-x-reverse space-x-2 text-xs text-gray-400">
|
| 226 |
-
<input
|
| 227 |
-
type="checkbox"
|
| 228 |
-
checked={lineNumbers}
|
| 229 |
-
onChange={(e) => setLineNumbers(e.target.checked)}
|
| 230 |
-
className="rounded"
|
| 231 |
-
/>
|
| 232 |
-
<span>Line Numbers</span>
|
| 233 |
-
</label>
|
| 234 |
</div>
|
| 235 |
</div>
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
<div className="flex items-center space-x-reverse space-x-2">
|
| 285 |
-
<FaTerminal className="w-4 h-4 text-primary-400" />
|
| 286 |
-
<span className="text-sm text-gray-300">Terminal</span>
|
| 287 |
-
</div>
|
| 288 |
-
<button
|
| 289 |
-
onClick={clearTerminal}
|
| 290 |
-
className="p-1 bg-gray-700 text-white rounded hover:bg-gray-600 transition-all"
|
| 291 |
-
>
|
| 292 |
-
<FaTrash className="w-3 h-3" />
|
| 293 |
-
</button>
|
| 294 |
-
</div>
|
| 295 |
-
<div className="p-3 overflow-y-auto h-32 font-mono text-xs">
|
| 296 |
-
{terminalOutput.length === 0 ? (
|
| 297 |
-
<div className="text-gray-500">Terminal output will appear here...</div>
|
| 298 |
-
) : (
|
| 299 |
-
terminalOutput.map((output, index) => (
|
| 300 |
-
<div key={index} className="mb-2">
|
| 301 |
-
<span className="text-gray-400">
|
| 302 |
-
[{output.timestamp.toLocaleTimeString()}]
|
| 303 |
-
</span>
|
| 304 |
-
<span className={output.type === 'success' ? 'text-green-400' : 'text-red-400'}>
|
| 305 |
-
{' '}{output.message}
|
| 306 |
-
</span>
|
| 307 |
-
</div>
|
| 308 |
-
))
|
| 309 |
-
)}
|
| 310 |
-
</div>
|
| 311 |
-
</motion.div>
|
| 312 |
-
)}
|
| 313 |
-
</AnimatePresence>
|
| 314 |
</div>
|
| 315 |
)
|
| 316 |
-
}
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState } from 'react'
|
| 2 |
+
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'
|
| 3 |
+
import { atomDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'
|
| 4 |
+
import Modal from 'react-modal'
|
| 5 |
+
|
| 6 |
+
const CodeEditor = ({ language }) => {
|
| 7 |
+
const [code, setCode] = useState('// کد خود را اینجا بنویسید\nconsole.log("سلام دنیا")')
|
| 8 |
+
const [output, setOutput] = useState('')
|
| 9 |
+
const [isModalOpen, setIsModalOpen] = useState(false)
|
| 10 |
+
const [projectType, setProjectType] = useState('nodejs')
|
| 11 |
+
|
| 12 |
+
const runCode = () => {
|
| 13 |
+
try {
|
| 14 |
+
const result = eval(code)
|
| 15 |
+
setOutput(JSON.stringify(result, null, 2))
|
| 16 |
+
} catch (error) {
|
| 17 |
+
setOutput(`خطا: ${error.message}`)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
| 21 |
+
const generateProject = () => {
|
| 22 |
+
setIsModalOpen(true)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
return (
|
| 26 |
+
<div className="bg-white rounded-lg shadow-md p-4">
|
| 27 |
+
<div className="flex justify-between mb-4">
|
| 28 |
+
<h2 className="text-xl font-bold">
|
| 29 |
+
{language === 'fa' ? 'ویرایشگر کد هوشمند' : 'Smart Code Editor'}
|
| 30 |
+
</h2>
|
| 31 |
+
<div className="flex space-x-2">
|
| 32 |
+
<button
|
| 33 |
+
onClick={runCode}
|
| 34 |
+
className="px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
>
|
| 36 |
+
{language === 'fa' ? 'اجرای کد' : 'Run Code'}
|
| 37 |
</button>
|
| 38 |
+
<button
|
| 39 |
+
onClick={generateProject}
|
| 40 |
+
className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700"
|
|
|
|
| 41 |
>
|
| 42 |
+
{language === 'fa' ? 'ساخت پروژه جدید' : 'New Project'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
</button>
|
| 44 |
</div>
|
| 45 |
</div>
|
| 46 |
|
| 47 |
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
| 48 |
+
<div>
|
| 49 |
+
<SyntaxHighlighter
|
| 50 |
+
language="javascript"
|
| 51 |
+
style={atomDark}
|
| 52 |
+
className="rounded-lg h-96"
|
| 53 |
+
showLineNumbers
|
| 54 |
+
>
|
| 55 |
+
{code}
|
| 56 |
+
</SyntaxHighlighter>
|
| 57 |
+
<textarea
|
| 58 |
+
className="w-full mt-2 p-2 border rounded font-mono"
|
| 59 |
+
value={code}
|
| 60 |
+
onChange={(e) => setCode(e.target.value)}
|
| 61 |
+
rows={5}
|
| 62 |
+
dir="ltr"
|
| 63 |
+
/>
|
| 64 |
+
</div>
|
| 65 |
+
<div>
|
| 66 |
+
<h3 className="font-bold mb-2">
|
| 67 |
+
{language === 'fa' ? 'خروجی برنامه' : 'Program Output'}
|
| 68 |
+
</h3>
|
| 69 |
+
<pre className="bg-gray-800 text-green-400 p-4 rounded-lg h-96 overflow-auto">
|
| 70 |
+
{output || (language === 'fa' ? 'خروجی اینجا نمایش داده میشود' : 'Output will appear here')}
|
| 71 |
+
</pre>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
</div>
|
| 73 |
</div>
|
| 74 |
|
| 75 |
+
<Modal
|
| 76 |
+
isOpen={isModalOpen}
|
| 77 |
+
onRequestClose={() => setIsModalOpen(false)}
|
| 78 |
+
contentLabel="Project Generator"
|
| 79 |
+
className="modal"
|
| 80 |
+
overlayClassName="overlay"
|
| 81 |
+
>
|
| 82 |
+
<div className="bg-white p-6 rounded-lg max-w-md mx-auto mt-20">
|
| 83 |
+
<h2 className="text-xl font-bold mb-4">
|
| 84 |
+
{language === 'fa' ? 'ساخت پروژه جدید' : 'Create New Project'}
|
| 85 |
+
</h2>
|
| 86 |
+
|
| 87 |
+
<div className="mb-4">
|
| 88 |
+
<label className="block mb-2">
|
| 89 |
+
{language === 'fa' ? 'نوع پروژه' : 'Project Type'}
|
| 90 |
+
</label>
|
| 91 |
+
<select
|
| 92 |
+
className="w-full p-2 border rounded"
|
| 93 |
+
value={projectType}
|
| 94 |
+
onChange={(e) => setProjectType(e.target.value)}
|
| 95 |
+
>
|
| 96 |
+
<option value="nodejs">Node.js</option>
|
| 97 |
+
<option value="react">React</option>
|
| 98 |
+
<option value="nextjs">Next.js</option>
|
| 99 |
+
<option value="python">Python</option>
|
| 100 |
+
</select>
|
| 101 |
+
</div>
|
| 102 |
+
|
| 103 |
+
<div className="flex justify-end space-x-2">
|
| 104 |
+
<button
|
| 105 |
+
onClick={() => setIsModalOpen(false)}
|
| 106 |
+
className="px-4 py-2 bg-gray-300 rounded hover:bg-gray-400"
|
| 107 |
+
>
|
| 108 |
+
{language === 'fa' ? 'انصراف' : 'Cancel'}
|
| 109 |
+
</button>
|
| 110 |
+
<button
|
| 111 |
+
onClick={() => {
|
| 112 |
+
// Generate project structure
|
| 113 |
+
setIsModalOpen(false)
|
| 114 |
+
|
| 115 |
+
className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700"
|
| 116 |
+
>
|
| 117 |
+
{language === 'fa' ? 'تولید پروژه' : 'Generate Project'}
|
| 118 |
+
</button>
|
| 119 |
+
</div>
|
| 120 |
+
</div>
|
| 121 |
+
</Modal>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
</div>
|
| 123 |
)
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
export default CodeEditor
|