From addaf797cbf734fb5aa9194153e71e05305e1b09 Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Mon, 10 Nov 2025 17:58:49 +0000 Subject: [PATCH] location click --- .../tools/pdfJsonEditor/PdfJsonEditorView.tsx | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/frontend/src/proprietary/components/tools/pdfJsonEditor/PdfJsonEditorView.tsx b/frontend/src/proprietary/components/tools/pdfJsonEditor/PdfJsonEditorView.tsx index 4b9792dcf..a6195ac24 100644 --- a/frontend/src/proprietary/components/tools/pdfJsonEditor/PdfJsonEditorView.tsx +++ b/frontend/src/proprietary/components/tools/pdfJsonEditor/PdfJsonEditorView.tsx @@ -1472,27 +1472,51 @@ const PdfJsonEditorView = ({ data }: PdfJsonEditorViewProps) => { , (event: React.MouseEvent) => { + // Capture click position BEFORE switching to edit mode + const clickX = event.clientX; + const clickY = event.clientY; + setEditingGroupId(group.id); setActiveGroupId(group.id); - // Store click position for later cursor placement - const editor = document.querySelector(`[data-editor-group="${group.id}"]`); - if (editor) { - const rect = editor.getBoundingClientRect(); - const clickX = event.clientX - rect.left; - const clickY = event.clientY - rect.top; - // Use setTimeout to allow the editor to render first + // Clear any stored offset to prevent interference + caretOffsetsRef.current.delete(group.id); + + // Wait for editor to render, then position cursor at click location + requestAnimationFrame(() => { + const editor = document.querySelector(`[data-editor-group="${group.id}"]`); + if (!editor) return; + + // Focus the editor first + editor.focus(); + + // Use caretRangeFromPoint to position cursor at click coordinates setTimeout(() => { - const range = document.caretRangeFromPoint(event.clientX, event.clientY); - if (range) { - const selection = window.getSelection(); - if (selection) { - selection.removeAllRanges(); - selection.addRange(range); + if (document.caretRangeFromPoint) { + const range = document.caretRangeFromPoint(clickX, clickY); + if (range) { + const selection = window.getSelection(); + if (selection) { + selection.removeAllRanges(); + selection.addRange(range); + } + } + } else if ((document as any).caretPositionFromPoint) { + // Firefox fallback + const pos = (document as any).caretPositionFromPoint(clickX, clickY); + if (pos) { + const range = document.createRange(); + range.setStart(pos.offsetNode, pos.offset); + range.collapse(true); + const selection = window.getSelection(); + if (selection) { + selection.removeAllRanges(); + selection.addRange(range); + } } } - }, 0); - } + }, 10); + }); }, )}