|
| 1 | +import React, { useState, useMemo, useCallback, useEffect, useRef } from "react"; |
| 2 | +import type { ScriptBlock } from "../types"; |
| 3 | + |
| 4 | +interface FindReplaceProps { |
| 5 | + blocks: ScriptBlock[]; |
| 6 | + onBlocksChange: (blocks: ScriptBlock[]) => void; |
| 7 | + onHighlightBlock: (blockId: string | null) => void; |
| 8 | + visible: boolean; |
| 9 | + onClose: () => void; |
| 10 | +} |
| 11 | + |
| 12 | +export default function FindReplace({ |
| 13 | + blocks, |
| 14 | + onBlocksChange, |
| 15 | + onHighlightBlock, |
| 16 | + visible, |
| 17 | + onClose, |
| 18 | +}: FindReplaceProps) { |
| 19 | + const [searchTerm, setSearchTerm] = useState(""); |
| 20 | + const [replaceTerm, setReplaceTerm] = useState(""); |
| 21 | + const [currentIdx, setCurrentIdx] = useState(0); |
| 22 | + const inputRef = useRef<HTMLInputElement>(null); |
| 23 | + |
| 24 | + const matchIndices = useMemo(() => { |
| 25 | + if (!searchTerm) return []; |
| 26 | + const lower = searchTerm.toLowerCase(); |
| 27 | + return blocks |
| 28 | + .map((b, i) => (b.content.toLowerCase().includes(lower) ? i : -1)) |
| 29 | + .filter((i) => i !== -1); |
| 30 | + }, [blocks, searchTerm]); |
| 31 | + |
| 32 | + // Focus input when opened |
| 33 | + useEffect(() => { |
| 34 | + if (visible) inputRef.current?.focus(); |
| 35 | + }, [visible]); |
| 36 | + |
| 37 | + // Highlight current match |
| 38 | + useEffect(() => { |
| 39 | + if (matchIndices.length > 0 && currentIdx < matchIndices.length) { |
| 40 | + onHighlightBlock(blocks[matchIndices[currentIdx]]?.id ?? null); |
| 41 | + } else { |
| 42 | + onHighlightBlock(null); |
| 43 | + } |
| 44 | + }, [matchIndices, currentIdx, blocks, onHighlightBlock]); |
| 45 | + |
| 46 | + // Reset index when search changes |
| 47 | + useEffect(() => { |
| 48 | + setCurrentIdx(0); |
| 49 | + }, [searchTerm]); |
| 50 | + |
| 51 | + const goNext = useCallback(() => { |
| 52 | + if (matchIndices.length === 0) return; |
| 53 | + setCurrentIdx((prev) => (prev + 1) % matchIndices.length); |
| 54 | + }, [matchIndices]); |
| 55 | + |
| 56 | + const goPrev = useCallback(() => { |
| 57 | + if (matchIndices.length === 0) return; |
| 58 | + setCurrentIdx((prev) => |
| 59 | + prev === 0 ? matchIndices.length - 1 : prev - 1 |
| 60 | + ); |
| 61 | + }, [matchIndices]); |
| 62 | + |
| 63 | + const handleReplace = useCallback(() => { |
| 64 | + if (matchIndices.length === 0 || !searchTerm) return; |
| 65 | + const idx = matchIndices[currentIdx]; |
| 66 | + const block = blocks[idx]; |
| 67 | + const regex = new RegExp(searchTerm.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "i"); |
| 68 | + const newContent = block.content.replace(regex, replaceTerm); |
| 69 | + onBlocksChange( |
| 70 | + blocks.map((b, i) => (i === idx ? { ...b, content: newContent } : b)) |
| 71 | + ); |
| 72 | + }, [blocks, matchIndices, currentIdx, searchTerm, replaceTerm, onBlocksChange]); |
| 73 | + |
| 74 | + const handleReplaceAll = useCallback(() => { |
| 75 | + if (!searchTerm) return; |
| 76 | + const regex = new RegExp( |
| 77 | + searchTerm.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), |
| 78 | + "gi" |
| 79 | + ); |
| 80 | + onBlocksChange( |
| 81 | + blocks.map((b) => ({ |
| 82 | + ...b, |
| 83 | + content: b.content.replace(regex, replaceTerm), |
| 84 | + })) |
| 85 | + ); |
| 86 | + }, [blocks, searchTerm, replaceTerm, onBlocksChange]); |
| 87 | + |
| 88 | + const handleClose = useCallback(() => { |
| 89 | + setSearchTerm(""); |
| 90 | + setReplaceTerm(""); |
| 91 | + onHighlightBlock(null); |
| 92 | + onClose(); |
| 93 | + }, [onClose, onHighlightBlock]); |
| 94 | + |
| 95 | + if (!visible) return null; |
| 96 | + |
| 97 | + return ( |
| 98 | + <div style={containerStyle}> |
| 99 | + <div style={{ display: "flex", gap: 4, alignItems: "center", flex: 1 }}> |
| 100 | + <input |
| 101 | + ref={inputRef} |
| 102 | + value={searchTerm} |
| 103 | + onChange={(e) => setSearchTerm(e.target.value)} |
| 104 | + placeholder="Find..." |
| 105 | + style={inputStyle} |
| 106 | + /> |
| 107 | + <input |
| 108 | + value={replaceTerm} |
| 109 | + onChange={(e) => setReplaceTerm(e.target.value)} |
| 110 | + placeholder="Replace..." |
| 111 | + style={inputStyle} |
| 112 | + /> |
| 113 | + </div> |
| 114 | + <div style={{ display: "flex", gap: 3, alignItems: "center" }}> |
| 115 | + <span style={{ fontSize: 9, color: "#6b7280", minWidth: 30, textAlign: "center" }}> |
| 116 | + {matchIndices.length > 0 |
| 117 | + ? `${currentIdx + 1}/${matchIndices.length}` |
| 118 | + : searchTerm |
| 119 | + ? "0" |
| 120 | + : ""} |
| 121 | + </span> |
| 122 | + <button onClick={goPrev} style={btnStyle} title="Previous"> |
| 123 | + ▲ |
| 124 | + </button> |
| 125 | + <button onClick={goNext} style={btnStyle} title="Next"> |
| 126 | + ▼ |
| 127 | + </button> |
| 128 | + <button onClick={handleReplace} style={btnStyle} title="Replace"> |
| 129 | + R |
| 130 | + </button> |
| 131 | + <button onClick={handleReplaceAll} style={btnStyle} title="Replace All"> |
| 132 | + RA |
| 133 | + </button> |
| 134 | + <button onClick={handleClose} style={btnStyle} title="Close"> |
| 135 | + × |
| 136 | + </button> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +const containerStyle: React.CSSProperties = { |
| 143 | + display: "flex", |
| 144 | + alignItems: "center", |
| 145 | + gap: 6, |
| 146 | + padding: "6px 8px", |
| 147 | + backgroundColor: "#f9fafb", |
| 148 | + border: "1px solid #e5e7eb", |
| 149 | + borderRadius: 6, |
| 150 | +}; |
| 151 | + |
| 152 | +const inputStyle: React.CSSProperties = { |
| 153 | + fontSize: 11, |
| 154 | + padding: "4px 6px", |
| 155 | + border: "1px solid #d1d5db", |
| 156 | + borderRadius: 4, |
| 157 | + outline: "none", |
| 158 | + flex: 1, |
| 159 | + minWidth: 0, |
| 160 | +}; |
| 161 | + |
| 162 | +const btnStyle: React.CSSProperties = { |
| 163 | + fontSize: 10, |
| 164 | + fontWeight: 700, |
| 165 | + color: "#374151", |
| 166 | + backgroundColor: "#e5e7eb", |
| 167 | + border: "none", |
| 168 | + borderRadius: 3, |
| 169 | + padding: "3px 6px", |
| 170 | + cursor: "pointer", |
| 171 | + lineHeight: 1, |
| 172 | +}; |
0 commit comments