fix(reader): jump to a typed page when the field loses focus (#7080)

The progress panel's page field opens the numeric keyboard. On iPhone
that is a number pad with no Enter key: the only way out is the
keyboard bar's Done, which blurs the field, and blur cancelled the
edit. A typed page could never be reached on iOS.

Leaving the field now jumps to the typed page when the text was
edited. Enter still jumps, Escape and the footer bar hiding mid-edit
still cancel, and leaving an unedited field does nothing (no history
entry). On desktop, clicking away after typing a page now jumps
instead of cancelling.

Verified:
- iOS simulator (iPhone 17, iOS 27.0, pnpm dev-ios-sim build): before
  the fix, typing 5 and tapping Done left the label at 1 / 112. After,
  typing 50 and tapping Done jumped to 50 / 112 and enabled Go Back;
  tapping Done with no input stayed put.
- New test "jumps when the field loses focus after an edit" fails
  without the change: expected "vi.fn()" to be called 1 times, but got
  0 times. Two guard tests (unedited blur, footer hiding) pass both ways.
- On origin/main 1d00df6ed: pnpm test (14146 passed), pnpm lint (one
  pre-existing info in nativePageTurn.ts), pnpm format:check.
- Not run: Android (its number keyboard has a Go key) and the desktop
  app by hand.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Huang Xin 2026-10-25 17:55:04 +08:00 • committed by GitHub
parent 664b7dc342
commit 54fed85c33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 54 additions and 12 deletions

View file

@ -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(<PageJumpInput bookKey='book1' />);
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);

View file

@ -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<PageJumpInputProps> = ({ bookKey, showFraction, className }) => {
const _ = useTranslation();
@ -35,6 +36,8 @@ const PageJumpInput: React.FC<PageJumpInputProps> = ({ bookKey, showFraction, cl
const [draft, setDraft] = useState('');
const [selectLength, setSelectLength] = useState(0);
const inputRef = useRef<HTMLInputElement>(null);
// Set while the field is blurred on purpose, so onBlur doesn't commit.
const quietBlurRef = useRef(false);
const [lastPageInfo, setLastPageInfo] = useState<PageInfo | null>(null);
useEffect(() => {
@ -51,7 +54,9 @@ const PageJumpInput: React.FC<PageJumpInputProps> = ({ 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<PageJumpInputProps> = ({ 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<PageJumpInputProps> = ({ 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();
}}
/>
</div>