@dr33m/react-native-readium 5.0.0-rc.21 → 5.0.0-rc.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -274,26 +274,33 @@ abstract class BaseReaderFragment : Fragment() {
274
274
  fun ttsSkipPrevious() { ttsManager?.skipPrevious() }
275
275
 
276
276
  private fun applyTTSDecoration(locator: Locator) {
277
- // Route through the public applyDecorations so the "tts" group is registered
278
- // via addDecorationListener before the first applyDecorations call — Readium
279
- // silently ignores decorations for groups that have no registered listener.
280
- applyDecorations(
281
- mapOf(
282
- "tts" to listOf(
277
+ if (!isNavigatorReady) return
278
+ val decorableNavigator = navigator as? DecorableNavigator ?: return
279
+ // applyDecorations is suspend in Readium 3.x must be called from a coroutine.
280
+ // viewLifecycleOwner.lifecycleScope dispatches to Dispatchers.Main, matching
281
+ // the observeWhenStarted pattern used by the Readium test app.
282
+ viewLifecycleOwner.lifecycleScope.launch {
283
+ decorableNavigator.applyDecorations(
284
+ listOf(
283
285
  Decoration(
284
- id = "tts-current",
286
+ id = "tts",
285
287
  locator = locator,
286
288
  style = Decoration.Style.Highlight(
287
289
  tint = android.graphics.Color.argb(89, 0xF2, 0xCA, 0x50) // #f2ca50 @ 35%
288
290
  )
289
291
  )
290
- )
292
+ ),
293
+ "tts"
291
294
  )
292
- )
295
+ }
293
296
  }
294
297
 
295
298
  private fun clearTTSDecoration() {
296
- applyDecorations(mapOf("tts" to emptyList()))
299
+ if (!isNavigatorReady) return
300
+ val decorableNavigator = navigator as? DecorableNavigator ?: return
301
+ viewLifecycleOwner.lifecycleScope.launch {
302
+ decorableNavigator.applyDecorations(emptyList(), "tts")
303
+ }
297
304
  }
298
305
 
299
306
  override fun onDestroyView() {
@@ -3,7 +3,9 @@ package com.reactnativereadium.reader
3
3
  import android.app.Application
4
4
  import kotlinx.coroutines.CoroutineScope
5
5
  import kotlinx.coroutines.Job
6
+ import kotlinx.coroutines.flow.distinctUntilChanged
6
7
  import kotlinx.coroutines.flow.launchIn
8
+ import kotlinx.coroutines.flow.map
7
9
  import kotlinx.coroutines.flow.onEach
8
10
  import kotlinx.coroutines.launch
9
11
  import org.readium.navigator.media.common.MediaNavigator
@@ -108,8 +110,14 @@ class TTSManager(
108
110
  }
109
111
  .launchIn(this)
110
112
 
111
- // Observe current locator
112
- ttsNavigator.currentLocator
113
+ // Observe utterance-level locator (sentence, not word).
114
+ // TtsNavigator.currentLocator returns tokenLocator ?: utteranceLocator — the
115
+ // word-level locator — which has incomplete CSS selectors and cannot be rendered
116
+ // by the EPUB decorator. utteranceLocator is the sentence-level locator and
117
+ // matches what the Readium test app (TtsViewModel.highlight) uses.
118
+ ttsNavigator.location
119
+ .map { it.utteranceLocator }
120
+ .distinctUntilChanged()
113
121
  .onEach { locator ->
114
122
  val text = locator.text.highlight ?: ""
115
123
  onUtterance(locator, text)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dr33m/react-native-readium",
3
- "version": "5.0.0-rc.21",
3
+ "version": "5.0.0-rc.23",
4
4
  "description": "A react-native wrapper for https://readium.org/",
5
5
  "main": "lib/src/index",
6
6
  "types": "lib/src/index.d.ts",