@@ -62,7 +62,7 @@ internal class EpubNavigatorViewModel(
6262 sealed class Scope {
6363 object CurrentResource : Scope()
6464 object LoadedResources : Scope()
65- data class Resource (val href : Url ) : Scope()
65+ data class LoadedResource (val href : Url ) : Scope()
6666 data class WebView (val webView : R2BasicWebView ) : Scope()
6767 }
6868 }
@@ -145,22 +145,39 @@ internal class EpubNavigatorViewModel(
145145 .launchIn(viewModelScope)
146146 }
147147
148- fun onResourceLoaded (webView : R2BasicWebView , link : Link ): RunScriptCommand {
149- val templates = decorationTemplates.toJSON().toString()
150- .replace(" \\ n" , " " )
151- var script = " readium.registerDecorationTemplates($templates );\n "
148+ fun onResourceLoaded (webView : R2BasicWebView , link : Link ): List <RunScriptCommand > =
149+ buildList {
150+ val scope = RunScriptCommand .Scope .WebView (webView)
152151
153- for ((group, decorations) in decorations) {
154- val changes = decorations
155- .filter { it.locator.href == link.url() }
156- .map { DecorationChange .Added (it) }
152+ // Applies the Readium CSS properties in case they changed since they were injected
153+ // in the HTML document.
154+ val properties = css.value.run {
155+ rsProperties.toCssProperties() + userProperties.toCssProperties()
156+ }
157157
158- val groupScript = changes.javascriptForGroup(group, decorationTemplates) ? : continue
159- script + = " $groupScript \n "
160- }
158+ add(
159+ RunScriptCommand (
160+ script = " readium.setCSSProperties(${JSONObject (properties.toMap())} );" ,
161+ scope = scope
162+ )
163+ )
161164
162- return RunScriptCommand (script, scope = RunScriptCommand .Scope .WebView (webView))
163- }
165+ // Applies the decorations.
166+ val templates = decorationTemplates.toJSON().toString()
167+ .replace(" \\ n" , " " )
168+ var script = " readium.registerDecorationTemplates($templates );\n "
169+
170+ for ((group, decorations) in decorations) {
171+ val changes = decorations
172+ .filter { it.locator.href == link.url() }
173+ .map { DecorationChange .Added (it) }
174+
175+ val groupScript = changes.javascriptForGroup(group, decorationTemplates) ? : continue
176+ script + = " $groupScript \n "
177+ }
178+
179+ add(RunScriptCommand (script, scope = scope))
180+ }
164181
165182 // Serving resources
166183
@@ -296,7 +313,9 @@ internal class EpubNavigatorViewModel(
296313 } else {
297314 for ((href, changes) in source.changesByHref(target)) {
298315 val script = changes.javascriptForGroup(group, decorationTemplates) ? : continue
299- cmds.add(RunScriptCommand (script, scope = RunScriptCommand .Scope .Resource (href)))
316+ cmds.add(
317+ RunScriptCommand (script, scope = RunScriptCommand .Scope .LoadedResource (href))
318+ )
300319 }
301320 }
302321
0 commit comments