fix spaces

This commit is contained in:
Anthony Stirling 2025-11-10 17:49:26 +00:00
parent a1b69b4b8b
commit 08e58349dc

View File

@ -706,6 +706,8 @@ const PdfJsonEditorView = ({ data }: PdfJsonEditorViewProps) => {
}, [autoScaleText, visibleGroups, editingGroupId, currentPage, pageHeight, scale, fontFamilies.size, selectedPage]); }, [autoScaleText, visibleGroups, editingGroupId, currentPage, pageHeight, scale, fontFamilies.size, selectedPage]);
useLayoutEffect(() => { useLayoutEffect(() => {
// Only restore caret position during re-renders while already editing
// Don't interfere with initial click-to-position behavior
if (!editingGroupId) { if (!editingGroupId) {
return; return;
} }
@ -714,7 +716,8 @@ const PdfJsonEditorView = ({ data }: PdfJsonEditorViewProps) => {
return; return;
} }
const offset = caretOffsetsRef.current.get(editingGroupId); const offset = caretOffsetsRef.current.get(editingGroupId);
if (offset === undefined) { // Only restore if we have a saved offset (meaning user was already typing)
if (offset === undefined || offset === 0) {
return; return;
} }
setCaretOffset(editor, offset); setCaretOffset(editor, offset);
@ -1378,6 +1381,10 @@ const PdfJsonEditorView = ({ data }: PdfJsonEditorViewProps) => {
} }
event.currentTarget.style.fontFamily = fontFamily; event.currentTarget.style.fontFamily = fontFamily;
}} }}
onClick={(event) => {
// Allow click position to determine cursor placement
event.stopPropagation();
}}
onBlur={(event) => { onBlur={(event) => {
const value = event.currentTarget.innerText.replace(/\u00A0/g, ' '); const value = event.currentTarget.innerText.replace(/\u00A0/g, ' ');
caretOffsetsRef.current.delete(group.id); caretOffsetsRef.current.delete(group.id);
@ -1414,7 +1421,7 @@ const PdfJsonEditorView = ({ data }: PdfJsonEditorViewProps) => {
outline: 'none', outline: 'none',
border: 'none', border: 'none',
display: 'block', display: 'block',
whiteSpace: 'nowrap', whiteSpace: 'pre',
cursor: 'text', cursor: 'text',
overflow: 'visible', overflow: 'visible',
}} }}
@ -1441,7 +1448,7 @@ const PdfJsonEditorView = ({ data }: PdfJsonEditorViewProps) => {
width: '100%', width: '100%',
minHeight: '100%', minHeight: '100%',
padding: 0, padding: 0,
whiteSpace: 'nowrap', whiteSpace: 'pre',
fontSize: `${fontSizePx}px`, fontSize: `${fontSizePx}px`,
fontFamily, fontFamily,
fontWeight, fontWeight,
@ -1464,9 +1471,28 @@ const PdfJsonEditorView = ({ data }: PdfJsonEditorViewProps) => {
{group.text || '\u00A0'} {group.text || '\u00A0'}
</span> </span>
</div>, </div>,
() => { (event: React.MouseEvent) => {
setEditingGroupId(group.id); setEditingGroupId(group.id);
setActiveGroupId(group.id); setActiveGroupId(group.id);
// Store click position for later cursor placement
const editor = document.querySelector<HTMLElement>(`[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
setTimeout(() => {
const range = document.caretRangeFromPoint(event.clientX, event.clientY);
if (range) {
const selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
selection.addRange(range);
}
}
}, 0);
}
}, },
)} )}
</Box> </Box>