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