Mehdi commited on
Commit
e350ad1
·
verified ·
1 Parent(s): 800d872

Upload components/CodeEditor.jsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. components/CodeEditor.jsx +108 -298
components/CodeEditor.jsx CHANGED
@@ -1,316 +1,126 @@
1
- import { useState, useEffect, useRef } from 'react'
2
- import Editor from '@monaco-editor/react'
3
- import { motion } from 'framer-motion'
4
- import {
5
- FaPlay, FaSave, FaDownload, FaUpload, FaCopy, FaPaste,
6
- FaUndo, FaRedo, FaSearch, FaReplace, FaCog, FaExpand,
7
- FaCompress, FaTerminal, FaEye, FaCode, FaFile, FaFolder,
8
- FaPlus, FaMinus, FaTimes, FaCheck, FaSync, FaBug,
9
- FaRocket, FaShieldAlt, FaChartLine, FaDatabase, FaCloud,
10
- FaServer, FaGitAlt, FaLink, FaLock, FaUnlock, FaShare,
11
- FaStar, FaRegStar, FaBookmark, FaFlag, FaTag, FaTags,
12
- FaCalendar, FaClock, FaUser, FaUsers, FaComment, FaComments,
13
- FaHeart, FaRegHeart, FaThumbsUp, FaThumbsDown, FaEyeSlash,
14
- FaEdit, FaTrash, FaFileCode, FaFileAlt, FaFileArchive,
15
- FaFileImage, FaFileVideo, FaFileAudio, FaFilePdf, FaFileExcel,
16
- FaFileWord, FaFilePowerpoint, FaFileCsv, FaFileZip, FaFileMedical
17
- } from 'react-icons/fa'
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 toggleFullscreen = () => {
117
- setIsFullscreen(!isFullscreen)
118
- }
119
-
120
- const clearTerminal = () => {
121
- setTerminalOutput([])
122
- toast.success('Terminal cleared!')
123
  }
124
 
125
  return (
126
- <div className={`flex flex-col h-full ${isFullscreen ? 'fixed inset-0 z-50 bg-gray-900' : ''}`}>
127
- {/* Editor Header */}
128
- <div className="flex items-center justify-between p-3 bg-gray-800 border-b border-gray-700">
129
- <div className="flex items-center space-x-reverse space-x-3">
130
- <FaCode className="w-5 h-5 text-primary-400" />
131
- <select
132
- value={currentLanguage}
133
- onChange={(e) => setCurrentLanguage(e.target.value)}
134
- className="px-3 py-1 bg-gray-700 text-white rounded-lg text-sm focus:outline-none"
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
- <FaSync className="w-4 h-4" />
152
  </button>
153
- <button
154
- onClick={handleSaveCode}
155
- className="p-2 bg-gray-700 text-white rounded-lg hover:bg-gray-600 transition-all"
156
- title="Save"
157
  >
158
- <FaSave className="w-4 h-4" />
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
- {/* Editor Settings Bar */}
194
- <div className="flex items-center justify-between px-3 py-2 bg-gray-750 border-b border-gray-700">
195
- <div className="flex items-center space-x-reverse space-x-3">
196
- <label className="flex items-center space-x-reverse space-x-2 text-xs text-gray-400">
197
- <span>Font Size:</span>
198
- <input
199
- type="number"
200
- value={fontSize}
201
- onChange={(e) => setFontSize(parseInt(e.target.value))}
202
- min="10"
203
- max="24"
204
- className="w-12 px-1 py-0.5 bg-gray-700 text-white rounded"
205
- />
206
- </label>
207
- <label className="flex items-center space-x-reverse space-x-2 text-xs text-gray-400">
208
- <input
209
- type="checkbox"
210
- checked={wordWrap}
211
- onChange={(e) => setWordWrap(e.target.checked)}
212
- className="rounded"
213
- />
214
- <span>Word Wrap</span>
215
- </label>
216
- <label className="flex items-center space-x-reverse space-x-2 text-xs text-gray-400">
217
- <input
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
- {/* Code Editor */}
238
- <div className="flex-1 overflow-hidden">
239
- <Editor
240
- height="100%"
241
- language={currentLanguage}
242
- value={code}
243
- onChange={(value) => setCode(value || '')}
244
- theme={theme}
245
- onMount={(editor) => {
246
- editorRef.current = editor
247
- editor.updateOptions({
248
- fontSize,
249
- wordWrap: wordWrap ? 'on' : 'off',
250
- minimap: { enabled: minimap },
251
- lineNumbers: lineNumbers ? 'on' : 'off',
252
- automaticLayout: true,
253
- scrollBeyondLastLine: false,
254
- renderWhitespace: 'selection',
255
- bracketPairColorization: { enabled: true },
256
- guides: {
257
- bracketPairs: true,
258
- indentation: true
259
- }
260
- })
261
-
262
- options={{
263
- selectOnLineNumbers: true,
264
- roundedSelection: false,
265
- readOnly: false,
266
- cursorStyle: 'line',
267
- automaticLayout: true,
268
- fontFamily: 'JetBrains Mono, Fira Code, Consolas, monospace',
269
- fontLigatures: true
270
-
271
- />
272
- </div>
273
-
274
- {/* Terminal */}
275
- <AnimatePresence>
276
- {showTerminal && (
277
- <motion.div
278
- initial={{ height: 0, opacity: 0 }}
279
- animate={{ height: 200, opacity: 1 }}
280
- exit={{ height: 0, opacity: 0 }}
281
- className="bg-gray-900 border-t border-gray-700"
282
- >
283
- <div className="flex items-center justify-between p-2 bg-gray-800">
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