fix(reading): keep Markdown pagination visible in narrow panels (#1641)

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-08 00:10:24 +08:00
parent 081c9e6d98
commit ecdc146d13
3 changed files with 95 additions and 5 deletions

View File

@@ -504,6 +504,36 @@ def test_unit_text_is_addressed_by_locator(client: TestClient) -> None:
assert "scaled dot-product" in body["text"]
@pytest.mark.parametrize("headings", [True, False])
def test_large_markdown_upload_keeps_every_unit_accessible_through_the_api(client, headings):
"""#1641: heading/fence fallback must never make the end of a document unreachable."""
chunks = ["# Opening\n\n"]
for index in range(24):
if headings:
chunks.append(f"## Section {index}\n\n")
chunks.append(f"Document paragraph {index}. " * 100 + "\n\n")
if index == 5:
chunks.append("````markdown\n```python\n# Code, not an outline heading\n```\n````\n\n")
chunks.append("END-OF-DOCUMENT-SENTINEL")
source = "".join(chunks)
response = client.post(
"/api/reading/materials", files={"file": ("long.md", source.encode(), "text/markdown")}
)
assert response.status_code == 200
material = response.json()
assert material["unit_count"] > 1
detail = client.get(f"/api/reading/materials/{material['material_id']}").json()
assert detail["unit_count"] == material["unit_count"]
units = []
for locator in range(1, material["unit_count"] + 1):
unit = client.get(f"/api/reading/materials/{material['material_id']}/units/{locator}")
assert unit.status_code == 200
units.append(unit.json()["text"])
assert "END-OF-DOCUMENT-SENTINEL" in units[-1]
# Section boundaries may normalize whitespace, but cannot discard source characters.
assert "".join("".join(units).split()) == "".join(source.split())
def test_unit_text_out_of_range_is_a_400_with_the_real_range(client: TestClient) -> None:
material = _upload(client)

View File

@@ -492,11 +492,15 @@ export function TextUnitView({
the new value on press, which the silent spans never did), and the
position is the header's to state, since that is the one line that is
there in every render mode. What is left here is what you can *do*. */}
<div className="flex items-center justify-between gap-2 overflow-x-auto border-b border-[var(--border)] px-2 py-2 sm:px-3">
<ReaderDisplayControls
preferences={{ fontSize, lineWidth, serif, readerTheme, spreadMode }}
onChange={updatePreferences}
/>
<div className="flex shrink-0 items-center justify-between gap-2 border-b border-[var(--border)] px-2 py-2 sm:px-3">
{/* #1641: scroll display controls independently so a narrow reader
never hides the only way to reach the next section. */}
<div className="min-w-0 flex-1 overflow-x-auto">
<ReaderDisplayControls
preferences={{ fontSize, lineWidth, serif, readerTheme, spreadMode }}
onChange={updatePreferences}
/>
</div>
<div className="flex shrink-0 items-center gap-0.5">
<PreferenceButton
label={t("Previous {{unit}}", { unit: t(unitLabel(unit)) })}

View File

@@ -0,0 +1,56 @@
import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { TextUnitView } from "@/components/reading/TextUnitView";
import { getMaterialMedia, getUnitText } from "@/lib/reading-api";
vi.mock("react-i18next", () => ({
useTranslation: () => ({
t: (value: string, options?: { unit?: string }) =>
value.replace("{{unit}}", options?.unit ?? ""),
}),
}));
vi.mock("@/lib/reading-api", () => ({
getUnitText: vi.fn(),
getMaterialMedia: vi.fn(),
materialMediaUrl: vi.fn(),
}));
vi.mock("@/components/common/RichMarkdownRenderer", () => ({ default: () => null }));
vi.mock("@recogito/text-annotator", () => ({
createTextAnnotator: () => ({ destroy: vi.fn(), setAnnotations: vi.fn(), on: vi.fn() }),
}));
describe("Markdown section navigation (#1641)", () => {
beforeEach(() => {
vi.mocked(getMaterialMedia).mockResolvedValue([]);
});
it.each([true, false])("reaches the final unit and returns with headings=%s", async (headings) => {
const units = [
(headings ? "# Opening\n\n" : "") + "Opening paragraph.",
"````markdown\n```python\n# This is code\n```\n````\nMiddle paragraph.",
(headings ? "## Last section\n\n" : "") + "END OF DOCUMENT",
];
vi.mocked(getUnitText).mockImplementation(async (_id, locator) => ({
locator, unit: "section", text: units[locator - 1],
}));
const user = userEvent.setup();
render(<TextUnitView materialId="long-markdown" unit="section" unitCount={units.length}
annotations={[]} jump={null} onSelection={vi.fn()} />);
await screen.findByText("Opening paragraph.");
expect(screen.getByRole("button", { name: "Previous Section" })).toBeDisabled();
for (let locator = 2; locator <= units.length; locator++) {
await user.click(screen.getByRole("button", { name: "Next Section" }));
await waitFor(() => expect(getUnitText).toHaveBeenLastCalledWith("long-markdown", locator));
await waitFor(() => expect(screen.queryByText("Loading…")).not.toBeInTheDocument());
}
await screen.findByText("END OF DOCUMENT");
expect(screen.getByRole("button", { name: "Next Section" })).toBeDisabled();
await user.click(screen.getByRole("button", { name: "Previous Section" }));
await waitFor(() => expect(screen.getByRole("article")).toHaveTextContent("Middle paragraph."));
expect(screen.getByRole("button", { name: "Next Section" })).toBeEnabled();
});
});