|
| 1 | +import React, { useRef, useEffect } from 'react'; |
| 2 | +import Editor, { Monaco } from '@monaco-editor/react'; |
| 3 | +import { clsx } from 'clsx'; |
| 4 | + |
| 5 | +export interface CodeChange { |
| 6 | + type: 'added' | 'removed' | 'modified'; |
| 7 | + startLine: number; |
| 8 | + endLine: number; |
| 9 | + content?: string; |
| 10 | +} |
| 11 | + |
| 12 | +interface CodeEditorProps { |
| 13 | + value: string; |
| 14 | + language: string; |
| 15 | + theme?: 'light' | 'dark'; |
| 16 | + readOnly?: boolean; |
| 17 | + showLineNumbers?: boolean; |
| 18 | + changes?: CodeChange[]; |
| 19 | + onSelectionChange?: (selection: any) => void; |
| 20 | + className?: string; |
| 21 | + height?: string; |
| 22 | +} |
| 23 | + |
| 24 | +export const CodeEditor: React.FC<CodeEditorProps> = ({ |
| 25 | + value, |
| 26 | + language, |
| 27 | + theme = 'light', |
| 28 | + readOnly = true, |
| 29 | + showLineNumbers = true, |
| 30 | + changes = [], |
| 31 | + onSelectionChange, |
| 32 | + className, |
| 33 | + height = '400px', |
| 34 | +}) => { |
| 35 | + const editorRef = useRef<any>(null); |
| 36 | + const monacoRef = useRef<Monaco | null>(null); |
| 37 | + |
| 38 | + const handleEditorDidMount = (editor: any, monaco: Monaco) => { |
| 39 | + editorRef.current = editor; |
| 40 | + monacoRef.current = monaco; |
| 41 | + |
| 42 | + // Configure editor options |
| 43 | + editor.updateOptions({ |
| 44 | + readOnly, |
| 45 | + lineNumbers: showLineNumbers ? 'on' : 'off', |
| 46 | + minimap: { enabled: false }, |
| 47 | + scrollBeyondLastLine: false, |
| 48 | + automaticLayout: true, |
| 49 | + wordWrap: 'on', |
| 50 | + fontSize: 14, |
| 51 | + fontFamily: 'JetBrains Mono, Fira Code, Monaco, Consolas, monospace', |
| 52 | + }); |
| 53 | + |
| 54 | + // Apply change decorations |
| 55 | + if (changes.length > 0) { |
| 56 | + applyChangeDecorations(editor, monaco, changes); |
| 57 | + } |
| 58 | + |
| 59 | + // Handle selection changes |
| 60 | + if (onSelectionChange) { |
| 61 | + editor.onDidChangeCursorSelection((e: any) => { |
| 62 | + onSelectionChange(e.selection); |
| 63 | + }); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + const applyChangeDecorations = (editor: any, monaco: Monaco, changes: CodeChange[]) => { |
| 68 | + const decorations = changes.map(change => { |
| 69 | + let className = ''; |
| 70 | + let glyphMarginClassName = ''; |
| 71 | + |
| 72 | + switch (change.type) { |
| 73 | + case 'added': |
| 74 | + className = 'code-line-added'; |
| 75 | + glyphMarginClassName = 'code-glyph-added'; |
| 76 | + break; |
| 77 | + case 'removed': |
| 78 | + className = 'code-line-removed'; |
| 79 | + glyphMarginClassName = 'code-glyph-removed'; |
| 80 | + break; |
| 81 | + case 'modified': |
| 82 | + className = 'code-line-modified'; |
| 83 | + glyphMarginClassName = 'code-glyph-modified'; |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + range: new monaco.Range(change.startLine, 1, change.endLine, 1), |
| 89 | + options: { |
| 90 | + isWholeLine: true, |
| 91 | + className, |
| 92 | + glyphMarginClassName, |
| 93 | + glyphMarginHoverMessage: { |
| 94 | + value: `**${change.type.toUpperCase()}**${change.content ? `\n\n${change.content}` : ''}`, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }; |
| 98 | + }); |
| 99 | + |
| 100 | + editor.deltaDecorations([], decorations); |
| 101 | + }; |
| 102 | + |
| 103 | + useEffect(() => { |
| 104 | + if (editorRef.current && monacoRef.current && changes.length > 0) { |
| 105 | + applyChangeDecorations(editorRef.current, monacoRef.current, changes); |
| 106 | + } |
| 107 | + }, [changes]); |
| 108 | + |
| 109 | + // Define custom CSS for change highlighting |
| 110 | + useEffect(() => { |
| 111 | + const style = document.createElement('style'); |
| 112 | + style.textContent = ` |
| 113 | + .code-line-added { |
| 114 | + background-color: rgba(34, 197, 94, 0.1) !important; |
| 115 | + border-left: 3px solid #22c55e !important; |
| 116 | + } |
| 117 | + .code-line-removed { |
| 118 | + background-color: rgba(239, 68, 68, 0.1) !important; |
| 119 | + border-left: 3px solid #ef4444 !important; |
| 120 | + } |
| 121 | + .code-line-modified { |
| 122 | + background-color: rgba(251, 191, 36, 0.1) !important; |
| 123 | + border-left: 3px solid #fbbf24 !important; |
| 124 | + } |
| 125 | + .code-glyph-added::before { |
| 126 | + content: '+'; |
| 127 | + color: #22c55e; |
| 128 | + font-weight: bold; |
| 129 | + } |
| 130 | + .code-glyph-removed::before { |
| 131 | + content: '-'; |
| 132 | + color: #ef4444; |
| 133 | + font-weight: bold; |
| 134 | + } |
| 135 | + .code-glyph-modified::before { |
| 136 | + content: '~'; |
| 137 | + color: #fbbf24; |
| 138 | + font-weight: bold; |
| 139 | + } |
| 140 | + `; |
| 141 | + document.head.appendChild(style); |
| 142 | + |
| 143 | + return () => { |
| 144 | + document.head.removeChild(style); |
| 145 | + }; |
| 146 | + }, []); |
| 147 | + |
| 148 | + return ( |
| 149 | + <div className={clsx('border border-gray-300 rounded-lg overflow-hidden', className)}> |
| 150 | + <Editor |
| 151 | + height={height} |
| 152 | + language={language} |
| 153 | + value={value} |
| 154 | + theme={theme === 'dark' ? 'vs-dark' : 'vs'} |
| 155 | + onMount={handleEditorDidMount} |
| 156 | + options={{ |
| 157 | + readOnly, |
| 158 | + lineNumbers: showLineNumbers ? 'on' : 'off', |
| 159 | + minimap: { enabled: false }, |
| 160 | + scrollBeyondLastLine: false, |
| 161 | + automaticLayout: true, |
| 162 | + wordWrap: 'on', |
| 163 | + fontSize: 14, |
| 164 | + fontFamily: 'JetBrains Mono, Fira Code, Monaco, Consolas, monospace', |
| 165 | + glyphMargin: true, |
| 166 | + folding: true, |
| 167 | + lineDecorationsWidth: 10, |
| 168 | + lineNumbersMinChars: 3, |
| 169 | + renderLineHighlight: 'line', |
| 170 | + selectOnLineNumbers: true, |
| 171 | + roundedSelection: false, |
| 172 | + cursorStyle: 'line', |
| 173 | + automaticLayout: true, |
| 174 | + }} |
| 175 | + /> |
| 176 | + </div> |
| 177 | + ); |
| 178 | +}; |
0 commit comments