diff --git a/frontend/src/components/settings/OpenCodeConfigEditor.tsx b/frontend/src/components/settings/OpenCodeConfigEditor.tsx index 9b4543bb..6e765bff 100644 --- a/frontend/src/components/settings/OpenCodeConfigEditor.tsx +++ b/frontend/src/components/settings/OpenCodeConfigEditor.tsx @@ -1,8 +1,10 @@ import { useState, useRef, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' -import { Loader2 } from 'lucide-react' +import { Loader2, Search, ChevronUp, ChevronDown } from 'lucide-react' import { Dialog, DialogContent, DialogHeader, DialogFooter, DialogTitle } from '@/components/ui/dialog' +import { Input } from '@/components/ui/input' +import { useFindInText } from '@/lib/useFindInText' import type { OpenCodeConfig } from '@/api/types/settings' import { parseJsonc } from '@/lib/jsonc' import { FetchError } from '@/api/fetchWrapper' @@ -34,6 +36,8 @@ export function OpenCodeConfigEditor({ const [validationIssues, setValidationIssues] = useState([]) const [removedFields, setRemovedFields] = useState([]) const editTextareaRef = useRef(null) + const backdropRef = useRef(null) + const { query, setQuery, matches, currentMatchIndex, hasMatches, next, prev } = useFindInText(editConfigContent) useEffect(() => { if (config && isOpen) { @@ -51,6 +55,63 @@ export function OpenCodeConfigEditor({ } }, [isOpen]) + useEffect(() => { + const textarea = editTextareaRef.current + if (!textarea || matches.length === 0) return + const match = matches[currentMatchIndex] + if (match) { + const textBefore = editConfigContent.substring(0, match.startIndex) + const lineNumber = textBefore.split('\n').length + const lineHeight = textarea.scrollHeight / textarea.value.split('\n').length + textarea.scrollTop = lineHeight * (lineNumber - 1) - textarea.clientHeight / 2 + lineHeight / 2 + syncBackdropScroll() + } + }, [currentMatchIndex, matches, editConfigContent]) + + const syncBackdropScroll = () => { + const textarea = editTextareaRef.current + const backdrop = backdropRef.current + if (textarea && backdrop) { + backdrop.scrollTop = textarea.scrollTop + backdrop.scrollLeft = textarea.scrollLeft + } + } + + const handleFindKeyDown = (e: React.KeyboardEvent) => { + if (e.key === 'Enter') { + e.preventDefault() + if (e.shiftKey) prev() + else next() + } + } + + const renderHighlightedContent = () => { + if (matches.length === 0) { + return editConfigContent + } + const segments: React.ReactNode[] = [] + let lastIndex = 0 + matches.forEach((match, index) => { + if (match.startIndex > lastIndex) { + segments.push(editConfigContent.substring(lastIndex, match.startIndex)) + } + const isCurrent = index === currentMatchIndex + segments.push( + + {editConfigContent.substring(match.startIndex, match.endIndex)} + + ) + lastIndex = match.endIndex + }) + if (lastIndex < editConfigContent.length) { + segments.push(editConfigContent.substring(lastIndex)) + } + return segments + } + const resetErrors = () => { setEditError('') setEditErrorLine(null) @@ -131,17 +192,66 @@ export function OpenCodeConfigEditor({ +
+
+ + setQuery(e.target.value)} + onKeyDown={handleFindKeyDown} + placeholder="Find in config..." + className="pl-9 h-9 text-[16px] sm:text-xs md:text-sm" + autoComplete="off" + name="config-find" + /> +
+ {query && ( + + {hasMatches + ? `${currentMatchIndex + 1} of ${matches.length}` + : '0 matches'} + + )} + + +
+
-