-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathCodeEditor.tsx
More file actions
47 lines (44 loc) · 1.65 KB
/
CodeEditor.tsx
File metadata and controls
47 lines (44 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Editor from '@monaco-editor/react';
import { useEditorStore } from '../../store/useEditorStore';
import { registerRetroAsm, LANGUAGE_ID as RETRO_ASM_ID } from './retroAsmLanguage';
function getLanguage(filename: string): string {
const ext = filename.split('.').pop()?.toLowerCase() ?? '';
if (ext === 's' || ext === 'asm') return RETRO_ASM_ID;
if (['ino', 'cpp', 'c', 'cc', 'h', 'hpp'].includes(ext)) return 'cpp';
if (ext === 'py') return 'python';
if (ext === 'json') return 'json';
if (ext === 'md') return 'markdown';
if (ext === 'hex') return 'plaintext';
return 'plaintext';
}
export const CodeEditor = () => {
const { files, activeFileId, setFileContent, theme, fontSize } = useEditorStore();
const activeFile = files.find((f) => f.id === activeFileId);
return (
<div style={{ height: '100%', width: '100%' }}>
<Editor
// key forces a fresh editor instance per file (preserves undo/redo per file)
key={activeFileId}
height="100%"
language={activeFile ? getLanguage(activeFile.name) : 'cpp'}
theme={theme}
value={activeFile?.content ?? ''}
beforeMount={(monaco) => {
// Register the 8080/Z80 assembly language once so Monaco knows how
// to tokenize .s / .asm files when they're opened.
registerRetroAsm(monaco);
}}
onChange={(value) => {
if (activeFileId) setFileContent(activeFileId, value || '');
}}
options={{
minimap: { enabled: true },
fontSize,
automaticLayout: true,
scrollBeyondLastLine: false,
wordWrap: 'on',
}}
/>
</div>
);
};