Skip to content

fix(web): complete audiobook locator fields after go() - #368

Open
ddfreiling wants to merge 4 commits into
Notalib:mainfrom
ddfreiling:fix/web-audio-locator-title
Open

ddfreiling wants to merge 4 commits into
Notalib:mainfrom
ddfreiling:fix/web-audio-locator-title

Conversation

@ddfreiling

@ddfreiling ddfreiling commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Summary

On web, plain audiobook locators lose their title after a jump to a locator (a ToC jump or a restored position). Apps that validate bookmarks then drop them. iOS and Android emit a title in the same flow.

Cause: upstream AudioNavigator.go() makes the caller's locator the current one, and position polling copies it forward, so an untitled locator stays untitled. tocHref was never emitted on web audio either: the plugin looked up timelineItemChanged items (from pub.timeline) in a separately built Timeline, so the lookup always missed.

Change

  • Title and tocHref: each plain audiobook locator gets title and locations.tocHref from the ToC entry playing at the current time, the last #t= entry for the track at or before t (the iOS currentTocLinkFromTimeLocator lookup). If no timed entry matches, the file's first ToC entry is used (one entry per file, no #t=), then the reading-order track title. ToC hrefs are matched by resolved path. The broken timeline tracking is removed.
  • Time and progression: a locator reached by go() without a t= fragment or progression (for example one built from a ToC link during a chapter skip while paused) now gets them from playback. Before, no position poll ran while paused, so the emitted locator had neither.
  • Stopped audiobook: goTo while the audiobook is stopped (after stop()) now reports the same filled-in locator. All paths share makePlainAudioLocatorEnricher.
  • Recovery state: the pinned "loading" state during stream recovery now goes through the same enrichment as other states. Before, it sent the raw navigator locator without title, tocHref or totalProgression.

Review notes

  • ToC order is taken as time order, as on iOS and Android. Web additionally falls back to a per-file ToC entry without #t=; iOS and Android skip such entries, so for those books web now has a ToC title where native has the track title.
  • Platforms differ on the title source: iOS uses the ToC title, Android only the reading-order title. Web now follows iOS.
  • Media Overlay sessions are unchanged; their items already carry the ToC entry.

Verification

  • New FlutterAudioNavigatorLocatorTitle.test.ts drives create() with a fake upstream navigator that behaves like go(). Its title and time tests each fail before their fix.
  • New recovery test in FlutterAudioNavigatorTrackFailures.test.ts and stopped-goTo test in ReadiumReader.test.ts fail without their fix.
  • bin/typecheck, full Jest suite (551 tests), bin/update_web_example, bin/changelog check.
  • LYT4 web audiobook journey with bookmarks enabled passes against bf336bc0: two ToC jumps and a paused chapter skip, both bookmarks validated.

Not covered

  • Android's audio title still comes from the reading order, not the ToC.
  • LYT4 has not rerun against the review follow-up commit.
  • timelineItemChanged and the other no-op listeners stay: the AudioNavigator constructor requires a full AudioNavigatorListeners.

ddfreiling and others added 3 commits November 30, 2026 01:14
Upstream AudioNavigator.go() makes the caller's locator the current
one, and position polling copies it forward. A restored position or
ToC jump has no title, so every later web audio locator had title
null, and apps that validate bookmarks dropped them.

tocHref was never set either: the plugin resolved timelineItemChanged
items from pub.timeline against a separately built Timeline, so the
lookup always missed.

Plain audiobook locators now get title and tocHref from the ToC entry
playing at the current time (last timed entry for the track at or
before t), falling back to the reading-order track title. This mirrors
the iOS timebased enrichment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
During stream recovery, plain audiobooks pinned the "loading" state
with the raw navigator locator, so it lacked title, tocHref and
totalProgression. Route it through emit(), as Media Overlay already
did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Upstream go() keeps the caller's locations. A locator built from a
ToC link has no t= fragment and no progression, and while paused no
position poll replaces it, so a chapter skip followed by a bookmark
emitted progression null. LYT4 rejects such bookmarks.

Fill a missing t= fragment from playback and a missing progression
from the time and track duration, as iOS and Android always emit both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ddfreiling ddfreiling changed the title fix(web): keep chapter title on audiobook locators after go() fix(web): complete audiobook locator fields after go() Nov 30, 2026

@m-abs m-abs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the diff plus the flow it touches, and ran the web gates against this branch in a throwaway worktree.

What this change does: On web, every plain-audiobook locator now gets its title and locations.tocHref filled in from the table-of-contents entry playing at the locator's time, the same lookup iOS does. The old Timeline-based tracking is deleted (it never worked), and the recovery "loading"/"failure" state now goes through the same fill-in as every other state.

Verified on this head: tsc clean, eslint clean, full Jest suite 546/546 pass, production rollup bundle builds. The 4 new tests do fail on main, so they really pin the bug. Both cause claims check out against upstream source: AudioNavigator.go() sets currentLocation = locator.copyWithLocations(...) (keeps the caller's missing title forever), and Timeline.build() makes its own item objects, so the old timeline.linkFor(item) lookup on navigator-supplied items missed by identity — tocHref really was always undefined. createLocator() uses the manifest-relative href plus a t= fragment, so the new lookup matches real locators, not just the test fake.

Load assumed: one reader in one browser tab, state emitted once per poll interval. Nothing here scales badly.

Should fix

1. The same broken bookmark still ships from one call site over (flutter_readium/web/src/ReadiumReader.ts:438-449)

  • What this is: When you jump to a locator (ToC tap, restored position) and no audio navigator is live, goTo remembers the locator and tells Flutter about it.
  • Problem: That report hand-builds its JSON instead of going through the fill-in path, so it carries no title, no tocHref and no totalProgression. This is reachable: stop() (ReadiumReader.ts:1110-1133) destroys the audio navigator but keeps the publication open. Tap a chapter after that and the app gets an untitled locator — exactly the symptom this PR set out to kill — and may store it as a bookmark.
  • Fix: Lift the closure body at FlutterAudioNavigator.ts:797-806 into an exported makeAudioChapterEnricher(publication) (it only needs the publication and the flattened ToC), use it in create() and wrap audioLocator with it before serialize() here. About 3 net lines, no new concepts.
  • If we skip it: Say so in the PR description as a known remaining gap. Silent is the bad outcome.

2. Audiobooks whose ToC points at bare files still get no title (flutter_readium/web/src/navigators/locatorEnrich.ts:90-108)

  • What this is: The lookup only accepts ToC entries whose href carries a #t= start time.
  • Problem: A common audiobook shape is one file per chapter with ToC entries like track1.mp3 and no time fragment. Every entry is then skipped, so tocHref stays unset and the title falls back to the reading-order link's title — often absent for those manifests, so the locator still reaches Dart with no title and the reported bug survives for that publication shape.
  • Fix: When the timed lookup finds nothing, fall back to the first ToC entry for the same file. locatorEnrich.ts already has that in findCurrentTocHref (used by EPUB and TTS); reuse it for both tocHref and the title. Two lines.
  • If we skip it: It is a deliberate parity call — iOS does the same (FlutterReadiumPlugin.swift:992-1012, and its second fallback needs a cssSelector audio locators never have). Then keep it, but the changelog line "audiobook locators now carry the chapter title" reads as a promise for those books. My recommendation: add the fallback on web, file the iOS/Android gap separately.

3. Two near-copies of the same locator-copy code (FlutterAudioNavigator.ts:173-190 vs locatorEnrich.ts:62-83)

  • What this is: withTocHref and enrichWithTocHref both rebuild a Locator and set otherLocations.tocHref, field by field.
  • Problem: This PR makes the audio path depend on the copy in the navigator file while EPUB and TTS use the one in locatorEnrich.ts. They already differ: the locatorEnrich one skips the work when the value is unchanged, the navigator one rebuilds a Map on every poll. Any future change to how tocHref is carried has to be made twice.
  • Fix: Keep one in locatorEnrich.ts, have enrichWithTocHref call it, import it in the audio navigator, drop withTocHref and its __testing__ entry (the 5 unit tests in FlutterAudioNavigator.test.ts:365-399 move with it).
  • If we skip it: Nothing breaks today; it is duplication that must be edited in step later.

Nice to have

4. The ToC href match is a raw string compare (locatorEnrich.ts:99)

  • Problem: A ToC entry written as ./track1.mp3#t=300, or percent-encoded differently from the reading-order href, matches nothing and the enrichment silently no-ops. iOS compares paths ($0.hrefPath == timeLocator.href.path), and this repo already has a tolerant matcher — findLinkByHref (utils/ReadiumExtensions.ts:32-48) — which the title fallback two lines away uses.
  • Fix: Compare pathnames the same way in both branches, or reuse findLinkByHref on the ToC list. Low impact, but the inconsistency is confusing to read.

5. timelineItemChanged: () => {} is now an empty listener (FlutterAudioNavigator.ts:1035)

  • What this is: Upstream treats every listener as optional (listeners.timelineItemChanged ?? (() => {})), so the no-op exists only to satisfy the plugin's own AudioNavigatorListeners annotation.
  • Fix: Delete it and the six sibling no-ops by typing listeners as Partial<AudioNavigatorListeners>. Roughly -7 lines. Fine to leave, since the file already does this for other events.

Checked and good

  • Removing Timeline is a real deletion of broken code, not a rewrite: the old lookup could never match, and nothing tested the wiring — which is why the helper had 5 passing unit tests while the feature never worked. The new test drives create() end to end instead.
  • The pinned-state change deletes a special case and routes everything through one chokepoint. Its guards (!nav || isDisposed()) are equivalent to what it replaced, so post-close stragglers stay suppressed.
  • Routing every plain emission through new Locator({...locator, title}) loses nothing: Locator's fields are all own properties (upstream itself copies locators with new Locator({...locator, ...})), and LocatorLocations' five fields are preserved by both copy helpers.
  • Reusing getTime rather than hand-parsing #t= is right — it matches iOS on clock formats (1:02:03) and ranges.
  • Media Overlay sessions keep their own ToC source and are untouched.

Verdict: ship after 1 (or state it as a follow-up in the description), and decide 2 on purpose.

Lean: -25 lines possible.

Not checked: bin/changelog check (needs uv/pipx, not installed here — the fragment name and type match the documented pattern), bin/update_web_example's example copy (I built the bundle with --skip-example-copy), and the LYT4 web audiobook journey, which the description already flags.

Review follow-ups for the audiobook locator fill-in:

- goTo on a stopped audiobook now reports the same title, tocHref,
  time and progression as a live navigator. The fill-in moves into
  makePlainAudioLocatorEnricher so both paths share it.
- A ToC with one entry per file and no t= start now supplies the
  chapter title and tocHref instead of falling back to the track
  title.
- ToC hrefs are matched by resolved path, so ./track.mp3 and
  percent-encoding variants match.
- withTocHref has one home in locatorEnrich.ts, shared with EPUB/TTS.

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

Copy link
Copy Markdown
Member Author

Thanks. All addressed in 085b8b6 except 5:

  1. Fixed. The fill-in is now makePlainAudioLocatorEnricher(publication), used by create() and by the stopped-audiobook goTo report, which now also gets t=0, progression and totalProgression. New test in ReadiumReader.test.ts fails without it.
  2. Added the fallback: with no timed match, the file's first ToC entry supplies title and tocHref, then the track title. This now goes beyond iOS/Android; noted in the description.
  3. One withTocHref in locatorEnrich.ts (keeps the no-op when unchanged); enrichWithTocHref calls it, and its tests moved to locatorEnrich.test.ts.
  4. The audio lookup now compares resolved paths, with a test for ./track1.mp3#t=300.
  5. Not changed: the upstream AudioNavigator constructor is typed listeners: AudioNavigatorListeners, so Partial<…> would need a cast.

🤖 Addressed by Claude Code

ddfreiling added a commit to ddfreiling/flutter_readium that referenced this pull request Dec 1, 2026
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

Copy link
Copy Markdown
Member Author

Heads-up from #376: it renames enrichWithTocHref to enrichWithToc in locatorEnrich.ts and adds withTocLink(locator, link), which sets both tocHref and the ToC entry's title (keeping the locator's own title when the entry has none). The audiobook path here could become withTocLink(locator, findAudioTocLink(...)) instead of withTocHref plus a separate title. Whichever PR merges second will conflict in locatorEnrich.ts and should build on withTocLink.

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