Skip to content

fix(web): add reading position to TTS locators, align with native - #376

Open
ddfreiling wants to merge 8 commits into
Notalib:mainfrom
ddfreiling:fix/web-tts-locator-position
Open

ddfreiling wants to merge 8 commits into
Notalib:mainfrom
ddfreiling:fix/web-tts-locator-position

Conversation

@ddfreiling

@ddfreiling ddfreiling commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

During web read-aloud, onTimebasedPlayerStateChanged locators had no position, progression, totalProgression or title, and only the playing state had tocHref. Apps could not track or restore the TTS position, show progress, or skip chapters reliably. Upstream's TS HTMLResourceContentIterator emits locators with none of these, unlike the Kotlin and Swift iterators.

Web TTS also sent onTextLocatorChanged for every paragraph, which native TTS never does. With sync off, the text locator moved while the page stayed put.

Change

  • TTS locators: a ProgressionHTMLResourceContentIterator subclass sets each element's progression to its index over the resource's element count, as kotlin-toolkit does. Every TTS state (playing, paused, ended, failure, word boundary) then gets position from the positions list, plus tocHref, title and totalProgression.
  • No TTS text locators: web TTS reports progress through player states only, as on iOS and Android. onTextLocatorChanged follows the visual reader, as docs/api-reference/streams-events.md already says.
  • totalProgression: now (firstPositionOfResource - 1 + progression * positionsInResource) / total, matching kotlin-toolkit's interpolation. The old formula added resource progression to the current position, so inside chapters spanning several positions it ran up to almost one position ahead. This also applies to EPUB text locators. WebPub and DiViNa have one position per resource, so their values do not change.
  • Chapter title from the ToC: a shared withTocLink(locator, link) sets tocHref and the ToC entry's title, as iOS, Android and swift-toolkit's EPUB navigator do. TTS states and EPUB text locators use it; before, their title came from the positions list, which has none in practice.
  • Skip-back at a chapter's first paragraph: upstream's hasPrevious() wraps index -1 to the resource's last element, so TTS jumped to the same chapter's last paragraph (also on main). The subclass now stops at the first element, so TTS moves to the previous chapter.
  • Cleanup: one readingOrderPositions() fallback replaces three copies (EPUB, DiViNa, TTS).

Review notes

  • The iterator subclass reads upstream's private currentIndex, currentElement, parsedElementRecipes and elements(). jsdom tests drive it over a real parsed resource, so a rename in an @readium/shared upgrade fails CI.
  • enrichWithTocHref is renamed enrichWithToc. fix(web): complete audiobook locator fields after go() #368 rewrites the same function for audiobooks; whichever merges second should build on withTocLink.
  • WebPub has no positions list; TTS uses one position per reading-order item, as the upstream WebPub navigator does.
  • Upgrading apps may see a slightly lower stored totalProgression for positions partway into long chapters, and bookmarks saved with the old formula can sort just after newer ones in the same chapter.
  • Web apps that saved the TTS position from onTextLocatorChanged must switch to onTimebasedPlayerStateChanged.currentLocator, as documented in docs/guides/saving-progress.md.

Verification

  • bin/typecheck, jest (575 tests) and bin/changelog check pass after rebasing onto main.
  • LYT4's web EPUB journey passed on the pre-rebase branch: chapter skip during TTS, pause/play, utterance skip, sleep timer, speed and text settings, and restore after reopening. totalProgression stayed inside each position and only grew.

Not covered

  • No runtime check of chapters spanning several positions; the journey only visits single-position chapters, so that path is covered by unit tests.
  • WebPub and guided-navigation TTS titles were not checked in a browser.
  • The skip-back and ToC title fixes were added after the LYT4 run; they are covered by unit tests only.
  • Web still takes the first ToC entry of a resource, while native picks the section that contains the text. When one file holds several ToC sections, web's tocHref and title are coarser.
  • The web audiobook navigator still emits text locators; whether that should align with native is a separate question.

ddfreiling and others added 6 commits November 30, 2026 16:48
Upstream's HTML content iterator builds element locators with only an
href and a cssSelector, and web TTS sent them to Dart as they were.
Apps that track the reading position from timebased states skip
locators without a position, so on web they lost the position and
chapter skip during read-aloud. iOS and Android emit positions.

Every TTS state now carries the first position of the spoken resource,
the matching totalProgression, and tocHref (which only the playing
state had). WebPub has no positions list, so its reading-order index
is used, as its text locators do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TTS locators carried the first position of the spoken resource, so the
reported progress only moved once per resource and had no chapter
title.

The TTS content iterator now sets each text element's progression to
its index over the resource's element count, as kotlin-toolkit's
HtmlResourceContentIterator does. The position is the last one in the
resource that starts at or before that progression, and the locator
takes its title when it has none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
enrichWithPosition listed every Locator field to add a position and a
title. Spreading the locator does the same and keeps future fields.

A test now checks that the TTS text locator matches the playing
state's locator, position included, as native text locators always
carry one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Web TTS sent a text locator for every spoken paragraph, on top of the
one the visual navigator sends when TTS moves it. With synchronization
off, the text locator moved while the page stayed put. Native TTS
reports progress through player states only, and the text-locator
stream follows the visual reader, as the streams docs describe.

Web now does the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The EPUB, DiViNa and TTS navigators each built one position per
reading-order item when a publication has no positions list. They now
share readingOrderPositions(), which also gives WebPub TTS states the
chapter title the TTS copy left out.

A test now checks that upstream's HTMLResourceContentIterator still
has the private fields ProgressionHTMLResourceContentIterator reads,
so an upstream rename fails CI instead of silently dropping
progression.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
totalProgression was (position - 1 + progression) / total, adding the
resource progression to the current position. In a resource spanning
several positions it ran up to almost one position ahead, then jumped
at each boundary, so it could fall outside the reported position. EPUB
text locators and TTS states both used it.

enrichWithTotalProgression now takes the positions list and computes
(firstPosition - 1 + progression * positionsInResource) / total, which
matches kotlin-toolkit's interpolation over the resource's range. It
falls back to the old formula when the locator has no progression or
its resource is missing from the list. WebPub and DiViNa have one
position per resource, so their values do not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Backward traversal past a resource’s first element emits negative progression and assigns the wrong position to its last paragraph, requiring a boundary guard and a real-iterator regression test.

1 open issue
What changed in this PR

This PR aligns web text-to-speech (TTS) progress reporting with the plugin’s native implementations, adding reading-position metadata while keeping text-locator events tied to the visual reader.

Changes:

  • Enriches TTS player-state locators with position, progression, title, and chapter metadata.
  • Removes TTS-generated text-locator events and corrects total progression across multi-position resources.
  • Shares reading-order fallback logic and adds regression tests and migration notes.
File Description
flutter_readium/​web/​src/​ReadiumReader.ts Passes publication positions to TTS.
flutter_readium/​web/​src/​navigators/​locatorEnrich.ts Adds position enrichment and resource-span interpolation.
flutter_readium/​web/​src/​navigators/​FlutterWebPubNavigator.ts Uses shared reading-order positions for progress.
flutter_readium/​web/​src/​navigators/​FlutterTTSNavigator.ts Enriches player states and removes TTS text-locator events.
flutter_readium/​web/​src/​navigators/​FlutterEpubNavigator.ts Adopts corrected progression and shared fallback logic.
flutter_readium/​web/​src/​navigators/​FlutterDivinaNavigator.ts Reuses shared position and progression helpers.
flutter_readium/​web/​src/​__tests__/​locatorEnrich.test.ts Tests position selection and multi-position interpolation.
flutter_readium/​web/​src/​__tests__/​FlutterTTSNavigator.test.ts Tests TTS metadata, event separation, and iterator progression.
flutter_readium/​changelog.d/​+web-tts-no-text-locator.changed.md Documents the TTS event migration.
flutter_readium/​changelog.d/​+web-tts-locator-position.fixed.md Documents enriched TTS reading positions.
flutter_readium/​changelog.d/​+web-total-progression-span.fixed.md Documents corrected progress within long chapters.

Review effort: Balanced

ddfreiling and others added 2 commits December 1, 2026 12:30
Upstream's HTMLResourceContentIterator.hasPrevious() reads
elements.at(index), which wraps index -1 to the last element. Going
back from a resource's first paragraph therefore returned the same
resource's last paragraph instead of moving to the previous resource,
and with the progression subclass it carried a negative progression.

ProgressionHTMLResourceContentIterator now returns false at the first
element, so the publication iterator moves to the previous resource.
Tests drive the subclass over a real parsed resource, which also
covers the upstream private fields it reads; they replace the
mock-based tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Web TTS states and EPUB text locators took their title from the
positions list, whose entries have none in practice, so apps got no
chapter title. iOS and Android set it from the ToC entry, as does
swift-toolkit's EPUB navigator.

enrichWithTocHref becomes enrichWithToc, built on a shared
withTocLink(locator, link) that sets both tocHref and the entry's
title, keeping the locator's own title when the entry has none. The
positions title stays a fallback for TTS. The audiobook ToC lookup in
Notalib#368 can reuse withTocLink.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ddfreiling added a commit that referenced this pull request Dec 5, 2026
A ToC entry's title is optional in RWPM. The plugin assigned it to the
locator unconditionally, so an untitled entry replaced the title that
the toolkit had already set with nil/null. Keep the existing title in
that case. tocHref handling is unchanged.

Native counterpart of #368 and #376.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants