@@ -101,7 +101,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
101101 } , [ showDropdown ] )
102102
103103 // Handle enhanced prompt response and search results.
104- // --- 必须严格修改消息监听 useEffect --- (根据规划修改)
104+
105105 useEffect ( ( ) => {
106106 const messageHandler = ( event : MessageEvent ) => {
107107 const message = event . data // The JSON data our extension sent
@@ -131,23 +131,20 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
131131 }
132132 break
133133
134- // 必须添加此 case (根据规划添加)
135134 case "mentionPathsResponse" : {
136- const validPaths = message . mentionPaths ?. filter ( ( path : string ) : path is string => ! ! path ) || [ ] // 添加显式类型 string (修复 TS 错误)
135+ const validPaths = message . mentionPaths ?. filter ( ( path : string ) : path is string => ! ! path ) || [ ]
137136 if ( validPaths . length > 0 ) {
138- // 必须更新 pendingInsertions 状态
139137 setPendingInsertions ( ( prev ) => [ ...prev , ...validPaths ] )
140138 }
141139 break
142140 }
143- // ... 其他 case ...
144141 }
145142 }
146143
147144 window . addEventListener ( "message" , messageHandler )
148145 // Clean up
149146 return ( ) => window . removeEventListener ( "message" , messageHandler )
150- } , [ setInputValue , searchRequestId /* , 确保其他依赖项完整 */ ] ) // 确保依赖项完整
147+ } , [ setInputValue , searchRequestId ] )
151148 const [ isDraggingOver , setIsDraggingOver ] = useState ( false )
152149 const [ textAreaBaseHeight , setTextAreaBaseHeight ] = useState < number | undefined > ( undefined )
153150 const [ showContextMenu , setShowContextMenu ] = useState ( false )
@@ -159,9 +156,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
159156 const [ selectedMenuIndex , setSelectedMenuIndex ] = useState ( - 1 )
160157 const [ selectedType , setSelectedType ] = useState < ContextMenuOptionType | null > ( null )
161158 const [ justDeletedSpaceAfterMention , setJustDeletedSpaceAfterMention ] = useState ( false )
162- // 必须定义此状态 (根据规划添加)
163159 const [ pendingInsertions , setPendingInsertions ] = useState < string [ ] > ( [ ] )
164- // 必须定义此状态 (已存在)
165160 const [ intendedCursorPosition , setIntendedCursorPosition ] = useState < number | null > ( null )
166161 const contextMenuContainerRef = useRef < HTMLDivElement > ( null )
167162 const [ isEnhancingPrompt , setIsEnhancingPrompt ] = useState ( false )
@@ -431,15 +426,12 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
431426 ] ,
432427 )
433428
434- // --- 必须添加用于应用光标位置的 useLayoutEffect --- (已存在,符合规划)
435429 useLayoutEffect ( ( ) => {
436430 if ( intendedCursorPosition !== null && textAreaRef . current ) {
437- // 必须应用光标位置
438431 textAreaRef . current . setSelectionRange ( intendedCursorPosition , intendedCursorPosition )
439- // 必须重置,防止重复应用
440432 setIntendedCursorPosition ( null )
441433 }
442- } , [ inputValue , intendedCursorPosition ] ) // 必须包含正确的依赖项
434+ } , [ inputValue , intendedCursorPosition ] )
443435 // Ref to store the search timeout
444436 const searchTimeoutRef = useRef < NodeJS . Timeout | null > ( null )
445437
@@ -625,27 +617,22 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
625617 [ updateCursorPosition ] ,
626618 )
627619
628- // --- 必须严格修改 onDrop (或 handleDrop) 函数 --- (根据规划重写)
629620 const handleDrop = useCallback (
630621 async ( e : React . DragEvent < HTMLDivElement > ) => {
631622 e . preventDefault ( )
632- setIsDraggingOver ( false ) // 假设有此状态
623+ setIsDraggingOver ( false )
633624
634- // --- 1. 核心:处理 VSCode 拖拽 ---
635625 let uris : string [ ] = [ ]
636- const vscodeUriListData = e . dataTransfer . getData ( "application/vnd.code.uri-list" ) // 必须检查这个
637- const resourceUrlsData = e . dataTransfer . getData ( "resourceurls" ) // 也要检查这个
626+ const vscodeUriListData = e . dataTransfer . getData ( "application/vnd.code.uri-list" )
627+ const resourceUrlsData = e . dataTransfer . getData ( "resourceurls" )
638628
639- // 优先使用 application/vnd.code.uri-list
640629 if ( vscodeUriListData ) {
641630 uris = vscodeUriListData
642631 . split ( "\n" )
643632 . map ( ( uri ) => uri . trim ( ) )
644633 . filter ( ( uri ) => uri )
645634 } else if ( resourceUrlsData ) {
646- // 回退到 resourceurls
647635 try {
648- // 注意:resourceUrlsData 是 JSON 字符串数组
649636 const parsedUris = JSON . parse ( resourceUrlsData ) as string [ ]
650637 uris = parsedUris . map ( ( uri ) => decodeURIComponent ( uri ) ) . filter ( ( uri ) => uri )
651638 } catch ( error ) {
@@ -654,15 +641,12 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
654641 }
655642 }
656643
657- // 过滤有效的 URI (file: 或 vscode-file:)
658644 const validUris = uris . filter (
659645 ( uri ) => uri && ( uri . startsWith ( "vscode-file:" ) || uri . startsWith ( "file:" ) ) ,
660646 )
661647
662648 if ( validUris . length > 0 ) {
663- // 必须清空待插入项
664649 setPendingInsertions ( [ ] )
665- // 必须记录初始光标位置
666650 let initialCursorPos = inputValue . length
667651 if ( textAreaRef . current ) {
668652 initialCursorPos =
@@ -672,25 +656,13 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
672656 }
673657 setIntendedCursorPosition ( initialCursorPos )
674658
675- // 必须发送此消息
676659 vscode . postMessage ( {
677660 type : "getMentionPathsFromUris" ,
678661 uris : validUris ,
679662 } )
680- return // 处理完毕,不再执行后续逻辑
681- }
682-
683- // --- 2. 移除或注释掉无关逻辑 ---
684- // 🚨 下面的 text/plain 处理逻辑与本次任务无关,必须移除或注释掉!
685- /*
686- const text = e.dataTransfer.getData("text")
687- if (text) {
688- // handleTextDrop(text) // 移除或注释掉这部分
689663 return
690664 }
691- */
692665
693- // --- 3. 其他拖拽处理 (例如图片) ---
694666 const files = Array . from ( e . dataTransfer . files )
695667 if ( ! textAreaDisabled && files . length > 0 ) {
696668 const acceptedTypes = [ "png" , "jpeg" , "webp" ]
@@ -736,7 +708,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
736708 [
737709 inputValue ,
738710 setIntendedCursorPosition ,
739- setPendingInsertions , // 添加依赖
711+ setPendingInsertions ,
740712 textAreaDisabled ,
741713 shouldDisableImages ,
742714 setSelectedImages ,
@@ -756,50 +728,31 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
756728 }
757729 } )
758730
759- // --- 必须严格修改此 useEffect 以处理多个插入 --- (根据规划重写)
760731 useEffect ( ( ) => {
761- // 确保 textAreaRef.current 存在且 pendingInsertions 有内容
762732 if ( pendingInsertions . length > 0 && textAreaRef . current ) {
763- const currentTextArea = textAreaRef . current // 引用当前文本区域
764- // 将所有待插入路径用空格连接成一个字符串
733+ const currentTextArea = textAreaRef . current
765734 const textToInsert = pendingInsertions . join ( " " )
766- // 获取当前光标位置,若无则取输入值末尾
767735 const currentCursorPos = currentTextArea . selectionStart ?? inputValue . length
768- // 确定插入起始位置:优先使用记录的拖放初始位置,否则使用当前光标位置
769736 const startPos = intendedCursorPosition ?? currentCursorPos
770737
771- // 构建插入后的新输入值
772- const newValue =
773- inputValue . substring ( 0 , startPos ) + // 插入点之前的部分
774- textToInsert + // 要插入的所有路径字符串
775- " " + // 在所有路径后追加一个空格,以便继续输入
776- inputValue . substring ( startPos ) // 插入点之后的部分
738+ const newValue = inputValue . substring ( 0 , startPos ) + textToInsert + " " + inputValue . substring ( startPos )
777739
778- // 调用回调函数更新父组件或全局状态中的输入值
779- // 注意:这里直接调用 setInputValue,因为它是 props 传入的 state setter
780740 setInputValue ( newValue )
781- // 一次性清空待插入项数组
782741 setPendingInsertions ( [ ] )
783742
784- // 计算插入后的新光标位置(位于插入内容和末尾空格之后)
785- const newCursorPos = startPos + textToInsert . length + 1 // +1 是因为加了空格
743+ const newCursorPos = startPos + textToInsert . length + 1
786744
787- // 使用 requestAnimationFrame 确保在 DOM 更新后设置光标和焦点
788745 requestAnimationFrame ( ( ) => {
789746 if ( textAreaRef . current ) {
790747 textAreaRef . current . selectionStart = newCursorPos
791748 textAreaRef . current . selectionEnd = newCursorPos
792- // 确保文本区域获得焦点,以便用户可以立即输入
793749 textAreaRef . current . focus ( )
794750 }
795751 } )
796752
797- // 重置记录的初始光标位置
798753 setIntendedCursorPosition ( null )
799754 }
800- // 依赖项数组:当这些值变化时,此 effect 会重新运行
801- // 需要包含所有在 effect 内部使用的、可能变化的外部变量/状态/回调
802- } , [ pendingInsertions , inputValue , setInputValue , intendedCursorPosition , setIntendedCursorPosition ] ) // 确保依赖项完整
755+ } , [ pendingInsertions , inputValue , setInputValue , intendedCursorPosition , setIntendedCursorPosition ] )
803756
804757 const placeholderBottomText = `\n(${ t ( "chat:addContext" ) } ${ shouldDisableImages ? `, ${ t ( "chat:dragFiles" ) } ` : `, ${ t ( "chat:dragFilesImages" ) } ` } )`
805758
0 commit comments