From c3de25535ddac2aa6286944b57a2dae4942270f7 Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Tue, 1 Dec 2026 23:11:00 +0800 Subject: [PATCH] fix(reader): page a book by its own language, not the UI language (#8184) * test(reader): page an English book LTR when the UI language is Arabic The UI language is only meant to set the page direction of a book that names no language. An English book read in the Arabic UI was paged right-to-left, so the right arrow turned back a page. Verified: the new test in foliate-viewer-writing-mode.test.tsx fails on main with "expected true to be false". Co-Authored-By: Claude Code (Claude Opus 5.5) * fix(reader): use the UI language only for books with no language The fallback from #6197 now applies only when the book names no language, so a book that does keeps its own direction. Verified: pnpm test --run (17211 passed, 16 skipped); the new test fails without this change ("expected true to be false") and passes with it; pnpm lint and pnpm format:check pass. Desktop app (macOS), Arabic UI: the right arrow turns to the next page in an English book, and the left arrow still does in an Arabic book. Co-Authored-By: Claude Code (Claude Opus 5.5) * test(reader): page a book with no language by the UI language The fix keeps the UI-language fallback for a book that names no language. This pins that case next to the English-book one, so the narrowing can't drop the fallback altogether. Verified: pnpm test --run src/__tests__/app/reader/foliate-viewer-*.test.tsx (8 files, 35 tests pass). With FoliateViewer.tsx at origin/main, the English-book case fails ("expected true to be false") and the no-language case passes; with the fix both pass. pnpm lint and pnpm format:check pass. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Huda Co-authored-by: Claude Code (Claude Opus 5.5) --- .../foliate-viewer-writing-mode.test.tsx | 41 ++++++++++++++++++- .../app/reader/components/FoliateViewer.tsx | 6 ++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/apps/readest-app/src/__tests__/app/reader/foliate-viewer-writing-mode.test.tsx b/apps/readest-app/src/__tests__/app/reader/foliate-viewer-writing-mode.test.tsx index 8ee22fd71..7c9d55d57 100644 --- a/apps/readest-app/src/__tests__/app/reader/foliate-viewer-writing-mode.test.tsx +++ b/apps/readest-app/src/__tests__/app/reader/foliate-viewer-writing-mode.test.tsx @@ -1,5 +1,5 @@ import { render, waitFor } from '@testing-library/react'; -import { expect, it, vi } from 'vitest'; +import { afterEach, describe, expect, it, vi } from 'vitest'; import type { ComponentProps } from 'react'; import FoliateViewer from '@/app/reader/components/FoliateViewer'; import { useFoliateEvents } from '@/app/reader/hooks/useFoliateEvents'; @@ -167,3 +167,42 @@ it('lets only the section being read set the book writing mode', async () => { load(frontMatter, 0); expect(h.settings).toMatchObject({ vertical: false, rtl: false }); }); + +// The UI language is only the last resort for a book with no direction. An +// English book read in the Arabic UI was paged right-to-left, so the right +// arrow turned back a page. +describe('page direction in an Arabic UI', () => { + // Opens the book and loads its first section, as the renderer would. + const loadFirstSection = async (bookKey: string, metadata: Record) => { + localStorage.setItem('i18nextLng', 'ar'); + h.settings = { writingMode: 'auto', vertical: false, rtl: false }; + const props = { + bookKey, + bookDoc: { metadata }, + config: {}, + gridInsets: { top: 0, right: 0, bottom: 0, left: 0 }, + contentInsets: { top: 0, right: 0, bottom: 0, left: 0 }, + } as unknown as ComponentProps; + vi.mocked(useFoliateEvents).mockClear(); + render(); + await waitFor(() => expect(vi.mocked(useFoliateEvents).mock.calls.some(([v]) => v)).toBe(true)); + const onLoad = vi.mocked(useFoliateEvents).mock.calls.find(([view]) => view)![1]!.onLoad!; + const doc = Object.assign(document.implementation.createHTMLDocument('chapter'), { + isEventListenersAdded: true, + }); + h.contents = []; + onLoad(new CustomEvent('load', { detail: { doc, index: 0 } })); + }; + + afterEach(() => localStorage.removeItem('i18nextLng')); + + it('pages a book by its own language, not the UI language', async () => { + await loadFirstSection('english-book', { language: 'en' }); + expect(h.settings.rtl).toBe(false); + }); + + it('pages a book that names no language by the UI language', async () => { + await loadFirstSection('nameless-book', {}); + expect(h.settings.rtl).toBe(true); + }); +}); diff --git a/apps/readest-app/src/app/reader/components/FoliateViewer.tsx b/apps/readest-app/src/app/reader/components/FoliateViewer.tsx index 4b7f1d030..ac7b25aaa 100644 --- a/apps/readest-app/src/app/reader/components/FoliateViewer.tsx +++ b/apps/readest-app/src/app/reader/components/FoliateViewer.tsx @@ -684,8 +684,10 @@ const FoliateViewer: React.FC<{ writingDir?.vertical || viewSettings.writingMode.includes('vertical') || false; // Fixed-layout books carry no writing mode; their direction may come // from the document itself (PDF ViewerPreferences /Direction /R2L). The - // UI language is the last resort, for a book that says nothing at all. - const documentRtl = writingDir?.rtl || getDirFromUILanguage() === 'rtl' || false; + // UI language is the last resort, for a book that says nothing at all: + // a book that names its language (an English book) is not paged RTL. + const uiRtl = !bookDoc.metadata?.language?.length && getDirFromUILanguage() === 'rtl'; + const documentRtl = writingDir?.rtl || uiRtl; const newRtl = getPageProgressionRTL(viewSettings.writingMode, bookDoc.dir, documentRtl); // Continuous scrolling keeps the neighbouring sections mounted, and they // finish loading *after* the one being read. A horizontal front matter