|
1 | 1 | import { useState, useRef } from 'react'; |
2 | 2 | import { DiffEditor, type MonacoDiffEditor } from '@monaco-editor/react'; |
3 | 3 | import { useTheme } from '../../hooks/useTheme'; |
| 4 | +import { useDragAndDrop } from '../../hooks/useDragAndDrop'; |
4 | 5 |
|
5 | 6 | interface TextDiffProps { |
6 | 7 | className?: string; |
7 | 8 | } |
8 | 9 |
|
| 10 | +type DropZone = 'original' | 'modified'; |
| 11 | + |
| 12 | +const DROP_ZONE = { |
| 13 | + ORIGINAL: 'original' as const, |
| 14 | + MODIFIED: 'modified' as const, |
| 15 | +} satisfies Record<string, DropZone>; |
| 16 | + |
9 | 17 | export const TextDiff = ({ className = '' }: TextDiffProps) => { |
10 | 18 | const [originalText, setOriginalText] = useState('function hello() {\n console.log("Hello World");\n}'); |
11 | 19 | const [modifiedText, setModifiedText] = useState('function hello() {\n console.log("Hello, World!");\n return "Hello";\n}'); |
12 | 20 | const editorRef = useRef<MonacoDiffEditor | null>(null); |
| 21 | + const originalDropZoneRef = useRef<HTMLElement | null>(null); |
| 22 | + const modifiedDropZoneRef = useRef<HTMLElement | null>(null); |
13 | 23 | const { theme } = useTheme(); |
14 | 24 |
|
| 25 | + const { isDragging, activeDropZone, registerDropZone } = useDragAndDrop<DropZone>({ |
| 26 | + onFilesDrop: (files, dropZone) => { |
| 27 | + const file = files[0]; |
| 28 | + readFile(file, dropZone); |
| 29 | + }, |
| 30 | + }); |
| 31 | + |
15 | 32 | const handleEditorDidMount = (editor: MonacoDiffEditor) => { |
16 | 33 | editorRef.current = editor; |
| 34 | + originalDropZoneRef.current = editor.getOriginalEditor().getDomNode(); |
| 35 | + modifiedDropZoneRef.current = editor.getModifiedEditor().getDomNode(); |
| 36 | + if (originalDropZoneRef.current) { |
| 37 | + registerDropZone(originalDropZoneRef.current, DROP_ZONE.ORIGINAL); |
| 38 | + } |
| 39 | + if (modifiedDropZoneRef.current) { |
| 40 | + registerDropZone(modifiedDropZoneRef.current, DROP_ZONE.MODIFIED); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + const readFile = (file: File, side: DropZone) => { |
| 45 | + const reader = new FileReader(); |
| 46 | + reader.onload = (e) => { |
| 47 | + const content = e.target?.result as string; |
| 48 | + if (side === DROP_ZONE.ORIGINAL) { |
| 49 | + setOriginalText(content); |
| 50 | + } else { |
| 51 | + setModifiedText(content); |
| 52 | + } |
| 53 | + }; |
| 54 | + reader.readAsText(file); |
17 | 55 | }; |
18 | 56 |
|
19 | 57 | const refreshEditor = () => { |
@@ -64,27 +102,74 @@ export const TextDiff = ({ className = '' }: TextDiffProps) => { |
64 | 102 | </div> |
65 | 103 |
|
66 | 104 | {/* Diff Editor */} |
67 | | - <DiffEditor |
68 | | - wrapperProps={{ |
69 | | - className: 'flex-1', |
70 | | - }} |
71 | | - theme={theme === 'dark' ? 'vs-dark' : 'vs'} |
72 | | - original={originalText} |
73 | | - modified={modifiedText} |
74 | | - onMount={handleEditorDidMount} |
75 | | - options={{ |
76 | | - readOnly: false, |
77 | | - minimap: { enabled: false }, |
78 | | - scrollBeyondLastLine: false, |
79 | | - fontSize: 14, |
80 | | - lineNumbers: 'on', |
81 | | - renderSideBySide: true, |
82 | | - enableSplitViewResizing: true, |
83 | | - ignoreTrimWhitespace: false, |
84 | | - renderIndicators: true, |
85 | | - originalEditable: true, |
86 | | - }} |
87 | | - /> |
| 105 | + <div className="flex-1 flex flex-col relative"> |
| 106 | + <DiffEditor |
| 107 | + wrapperProps={{ |
| 108 | + className: 'flex-1', |
| 109 | + }} |
| 110 | + theme={theme === 'dark' ? 'vs-dark' : 'vs'} |
| 111 | + original={originalText} |
| 112 | + modified={modifiedText} |
| 113 | + onMount={handleEditorDidMount} |
| 114 | + options={{ |
| 115 | + readOnly: false, |
| 116 | + minimap: { enabled: false }, |
| 117 | + scrollBeyondLastLine: false, |
| 118 | + fontSize: 14, |
| 119 | + lineNumbers: 'on', |
| 120 | + enableSplitViewResizing: false, |
| 121 | + ignoreTrimWhitespace: false, |
| 122 | + renderIndicators: true, |
| 123 | + originalEditable: true, |
| 124 | + }} |
| 125 | + /> |
| 126 | + |
| 127 | + {isDragging && ( |
| 128 | + <> |
| 129 | + <div |
| 130 | + className={`absolute top-0 left-0 w-1/2 h-full pointer-events-none transition-all duration-200 ${ |
| 131 | + activeDropZone === DROP_ZONE.ORIGINAL |
| 132 | + ? 'bg-blue-500/20 border-2 border-blue-500 border-dashed' |
| 133 | + : 'bg-gray-500/10' |
| 134 | + }`} |
| 135 | + > |
| 136 | + {activeDropZone === DROP_ZONE.ORIGINAL && ( |
| 137 | + <div className="flex items-center justify-center h-full"> |
| 138 | + <div className="bg-blue-500 text-white px-4 py-2 rounded-lg shadow-lg"> |
| 139 | + <div className="flex items-center space-x-2"> |
| 140 | + <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 141 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M9 19l3 3m0 0l3-3m-3 3V10" /> |
| 142 | + </svg> |
| 143 | + <span>Drag to update original content</span> |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + )} |
| 148 | + </div> |
| 149 | + |
| 150 | + <div |
| 151 | + className={`absolute top-0 right-0 w-1/2 h-full pointer-events-none transition-all duration-200 ${ |
| 152 | + activeDropZone === DROP_ZONE.MODIFIED |
| 153 | + ? 'bg-green-500/20 border-2 border-green-500 border-dashed' |
| 154 | + : 'bg-gray-500/10' |
| 155 | + }`} |
| 156 | + > |
| 157 | + {activeDropZone === DROP_ZONE.MODIFIED && ( |
| 158 | + <div className="flex items-center justify-center h-full"> |
| 159 | + <div className="bg-green-500 text-white px-4 py-2 rounded-lg shadow-lg"> |
| 160 | + <div className="flex items-center space-x-2"> |
| 161 | + <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 162 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M9 19l3 3m0 0l3-3m-3 3V10" /> |
| 163 | + </svg> |
| 164 | + <span>Drag to update modified content</span> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + )} |
| 169 | + </div> |
| 170 | + </> |
| 171 | + )} |
| 172 | + </div> |
88 | 173 | </div> |
89 | 174 | ); |
90 | 175 | }; |
0 commit comments