Skip to content

Commit 50f6cb7

Browse files
committed
Merge branch 'main' into develop
2 parents c6b8e8e + 85c0f7c commit 50f6cb7

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ All notable changes to this project will be documented in this file. Take a look
3333

3434
### Fixed
3535

36+
#### Navigator
37+
38+
* Fixed a race condition causing EPUB decorations to be applied twice when opening a publication.
3639
* Fixed support of Readium Web Publication packages conforming to the EPUB profile (contributed by [@ddfreiling](https://github.com/readium/kotlin-toolkit/pull/642)).
3740

3841

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorFragment.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,13 @@ public class EpubNavigatorFragment internal constructor(
671671
RunScriptCommand.Scope.LoadedResources -> {
672672
r2PagerAdapter?.mFragments?.forEach { _, fragment ->
673673
(fragment as? R2EpubPageFragment)
674+
?.takeIf { it.isLoaded.value }
674675
?.runJavaScript(command.script)
675676
}
676677
}
677-
is RunScriptCommand.Scope.Resource -> {
678+
is RunScriptCommand.Scope.LoadedResource -> {
678679
loadedFragmentForHref(command.scope.href)
680+
?.takeIf { it.isLoaded.value }
679681
?.runJavaScript(command.script)
680682
}
681683
is RunScriptCommand.Scope.WebView -> {

readium/navigator/src/main/java/org/readium/r2/navigator/epub/EpubNavigatorViewModel.kt

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal class EpubNavigatorViewModel(
6464
sealed class Scope {
6565
object CurrentResource : Scope()
6666
object LoadedResources : Scope()
67-
data class Resource(val href: Url) : Scope()
67+
data class LoadedResource(val href: Url) : Scope()
6868
data class WebView(val webView: R2BasicWebView) : Scope()
6969
}
7070
}
@@ -146,22 +146,39 @@ internal class EpubNavigatorViewModel(
146146
.launchIn(viewModelScope)
147147
}
148148

149-
fun onResourceLoaded(webView: R2BasicWebView, link: Link): RunScriptCommand {
150-
val templates = decorationTemplates.toJSON().toString()
151-
.replace("\\n", " ")
152-
var script = "readium.registerDecorationTemplates($templates);\n"
149+
fun onResourceLoaded(webView: R2BasicWebView, link: Link): List<RunScriptCommand> =
150+
buildList {
151+
val scope = RunScriptCommand.Scope.WebView(webView)
153152

154-
for ((group, decorations) in decorations) {
155-
val changes = decorations
156-
.filter { it.locator.href == link.url() }
157-
.map { DecorationChange.Added(it) }
153+
// Applies the Readium CSS properties in case they changed since they were injected
154+
// in the HTML document.
155+
val properties = css.value.run {
156+
rsProperties.toCssProperties() + userProperties.toCssProperties()
157+
}
158158

159-
val groupScript = changes.javascriptForGroup(group, decorationTemplates) ?: continue
160-
script += "$groupScript\n"
161-
}
159+
add(
160+
RunScriptCommand(
161+
script = "readium.setCSSProperties(${JSONObject(properties.toMap())});",
162+
scope = scope
163+
)
164+
)
162165

163-
return RunScriptCommand(script, scope = RunScriptCommand.Scope.WebView(webView))
164-
}
166+
// Applies the decorations.
167+
val templates = decorationTemplates.toJSON().toString()
168+
.replace("\\n", " ")
169+
var script = "readium.registerDecorationTemplates($templates);\n"
170+
171+
for ((group, decorations) in decorations) {
172+
val changes = decorations
173+
.filter { it.locator.href == link.url() }
174+
.map { DecorationChange.Added(it) }
175+
176+
val groupScript = changes.javascriptForGroup(group, decorationTemplates) ?: continue
177+
script += "$groupScript\n"
178+
}
179+
180+
add(RunScriptCommand(script, scope = scope))
181+
}
165182

166183
// Serving resources
167184

@@ -297,7 +314,9 @@ internal class EpubNavigatorViewModel(
297314
} else {
298315
for ((href, changes) in source.changesByHref(target)) {
299316
val script = changes.javascriptForGroup(group, decorationTemplates) ?: continue
300-
cmds.add(RunScriptCommand(script, scope = RunScriptCommand.Scope.Resource(href)))
317+
cmds.add(
318+
RunScriptCommand(script, scope = RunScriptCommand.Scope.LoadedResource(href))
319+
)
301320
}
302321
}
303322

0 commit comments

Comments
 (0)