diff --git a/apps/readest-app/src/__tests__/components/page-jump-input.test.tsx b/apps/readest-app/src/__tests__/components/page-jump-input.test.tsx index 04af4343d..77306c234 100644 --- a/apps/readest-app/src/__tests__/components/page-jump-input.test.tsx +++ b/apps/readest-app/src/__tests__/components/page-jump-input.test.tsx @@ -143,6 +143,34 @@ describe('PageJumpInput', () => { expect(mocks.view.goToFraction.mock.calls[0]![0]).toBeCloseTo(119.5 / 251); }); + it('jumps when the field loses focus after an edit (no Enter key on iOS number pads)', () => { + const { input } = setup(); + fireEvent.focus(input); + fireEvent.change(input, { target: { value: '120 / 251' } }); + fireEvent.blur(input); + expect(mocks.view.goToFraction).toHaveBeenCalledTimes(1); + expect(mocks.view.goToFraction.mock.calls[0]![0]).toBeCloseTo(119.5 / 251); + expect(input.value).toBe('94 / 251'); + }); + + it('does not jump when the field loses focus unedited', () => { + const { input } = setup(); + fireEvent.focus(input); + fireEvent.blur(input); + expect(mocks.view.goToFraction).not.toHaveBeenCalled(); + }); + + it('does not jump when the footer bar hides mid-edit', () => { + mocks.state.hoveredBookKey = 'book1'; + const { input, rerender } = setup(); + input.focus(); + fireEvent.change(input, { target: { value: '120 / 251' } }); + mocks.state.hoveredBookKey = ''; + rerender(); + expect(document.activeElement).not.toBe(input); + expect(mocks.view.goToFraction).not.toHaveBeenCalled(); + }); + it('clamps out-of-range pages to the last page', () => { const { input } = setup(); fireEvent.focus(input); diff --git a/apps/readest-app/src/app/reader/components/footerbar/PageJumpInput.tsx b/apps/readest-app/src/app/reader/components/footerbar/PageJumpInput.tsx index 027a3b816..4cfeb2f5c 100644 --- a/apps/readest-app/src/app/reader/components/footerbar/PageJumpInput.tsx +++ b/apps/readest-app/src/app/reader/components/footerbar/PageJumpInput.tsx @@ -17,9 +17,10 @@ interface PageJumpInputProps { /** * The footer bar's progress label, editable in place: clicking it selects the * current page number inside the "94 / 251" text so a target page can be - * typed over it. Enter jumps there; Escape or blur cancels. An invisible - * sizer span reserves the label's width so toggling edit mode never shifts - * the surrounding layout. + * typed over it. Enter or leaving the field jumps there (the iOS number pad + * has no Enter key, only the keyboard bar's Done); Escape cancels. An + * invisible sizer span reserves the label's width so toggling edit mode never + * shifts the surrounding layout. */ const PageJumpInput: React.FC = ({ bookKey, showFraction, className }) => { const _ = useTranslation(); @@ -35,6 +36,8 @@ const PageJumpInput: React.FC = ({ bookKey, showFraction, cl const [draft, setDraft] = useState(''); const [selectLength, setSelectLength] = useState(0); const inputRef = useRef(null); + // Set while the field is blurred on purpose, so onBlur doesn't commit. + const quietBlurRef = useRef(false); const [lastPageInfo, setLastPageInfo] = useState(null); useEffect(() => { @@ -51,7 +54,9 @@ const PageJumpInput: React.FC = ({ bookKey, showFraction, cl useEffect(() => { // The footer bar hid (or another book took focus) mid-edit. if (hoveredBookKey !== bookKey && document.activeElement === inputRef.current) { + quietBlurRef.current = true; inputRef.current?.blur(); + quietBlurRef.current = false; } }, [hoveredBookKey, bookKey]); @@ -113,15 +118,19 @@ const PageJumpInput: React.FC = ({ bookKey, showFraction, cl } }; - const stopEditing = () => { - setEditing(false); - inputRef.current?.blur(); - }; + const initialDraft = `${currentLabel} / ${total}`; const commitDraft = () => { + if (draft === initialDraft) return; const page = parsePageInput(draft); if (page !== null) jumpToPage(page); - stopEditing(); + }; + + const stopEditing = () => { + setEditing(false); + quietBlurRef.current = true; + inputRef.current?.blur(); + quietBlurRef.current = false; }; const text = editing ? draft : displayText; @@ -153,18 +162,23 @@ const PageJumpInput: React.FC = ({ bookKey, showFraction, cl onFocus={() => { // Keep the "current / total" text as-is (percentage style swaps to // it) and pre-select the page portion for type-over editing. - setDraft(`${currentLabel} / ${total}`); + setDraft(initialDraft); setSelectLength(currentLabel.length); setEditing(true); }} - onBlur={() => setEditing(false)} + onBlur={() => { + setEditing(false); + if (!quietBlurRef.current) commitDraft(); + }} onChange={(e) => setDraft(e.target.value)} onKeyDown={(e) => { // Keep keystrokes away from the reader's page-turn shortcuts and // the footer bar's spatial navigation. e.stopPropagation(); - if (e.key === 'Enter') commitDraft(); - else if (e.key === 'Escape') stopEditing(); + if (e.key === 'Enter') { + commitDraft(); + stopEditing(); + } else if (e.key === 'Escape') stopEditing(); }} />