({ attachment, children }: TextPatchesProps)
| 14 | |
| 15 | // Attachment is actually the comparing candidate. |
| 16 | export function TextPatches({ attachment, children }: TextPatchesProps) { |
| 17 | const { user } = useAuthStore(); |
| 18 | const { selectionRange, setSelectionRange } = useSelectionStore(); |
| 19 | const adapter = useAdapterOptional(); |
| 20 | const preRef = useRef<HTMLPreElement>(null); |
| 21 | |
| 22 | const [insertBtnText, setInsertBtnText] = useState("Insert"); |
| 23 | const [copyBtnText, setCopyBtnText] = useState("Copy"); |
| 24 | const [mappedNodes, setMappedNodes] = useState<React.ReactNode[]>([]); |
| 25 | const [showDiff, setShowDiff] = useState(false); |
| 26 | |
| 27 | const copyText = useCallback(() => { |
| 28 | if (preRef.current) { |
| 29 | navigator.clipboard.writeText(preRef.current.innerText ?? ""); |
| 30 | setCopyBtnText("Copied!"); |
| 31 | setTimeout(() => { |
| 32 | setCopyBtnText("Copy"); |
| 33 | }, 1500); |
| 34 | } |
| 35 | }, [preRef]); |
| 36 | |
| 37 | const applyText = useCallback(async () => { |
| 38 | if (!preRef.current) return; |
| 39 | |
| 40 | const textToInsert = preRef.current.innerText; |
| 41 | |
| 42 | try { |
| 43 | // Prefer adapter-based insertion if available |
| 44 | if (adapter && adapter.isReady()) { |
| 45 | await adapter.replaceSelection(textToInsert); |
| 46 | setSelectionRange(null); |
| 47 | setInsertBtnText("Applied!"); |
| 48 | setTimeout(() => { |
| 49 | setInsertBtnText("Insert"); |
| 50 | }, 1500); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // Fallback to legacy Range-based insertion for Overleaf |
| 55 | if (selectionRange) { |
| 56 | const newText = document.createTextNode(textToInsert); |
| 57 | selectionRange.deleteContents(); |
| 58 | selectionRange.insertNode(newText); |
| 59 | setSelectionRange(null); |
| 60 | setInsertBtnText("Applied!"); |
| 61 | setTimeout(() => { |
| 62 | setInsertBtnText("Insert"); |
| 63 | }, 1500); |
| 64 | } |
| 65 | } catch (error) { |
| 66 | console.error("Failed to apply text:", error); |
| 67 | setInsertBtnText("Failed!"); |
| 68 | setTimeout(() => { |
| 69 | setInsertBtnText("Insert"); |
| 70 | }, 1500); |
| 71 | } |
| 72 | }, [preRef, selectionRange, setSelectionRange, adapter]); |
| 73 |
nothing calls this directly
no test coverage detected