@@ -62,7 +62,7 @@ internal class EpubNavigatorViewModel(
62
62
sealed class Scope {
63
63
object CurrentResource : Scope()
64
64
object LoadedResources : Scope()
65
- data class Resource (val href : Url ) : Scope()
65
+ data class LoadedResource (val href : Url ) : Scope()
66
66
data class WebView (val webView : R2BasicWebView ) : Scope()
67
67
}
68
68
}
@@ -145,22 +145,39 @@ internal class EpubNavigatorViewModel(
145
145
.launchIn(viewModelScope)
146
146
}
147
147
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)
152
151
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
+ }
157
157
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
+ )
161
164
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
+ }
164
181
165
182
// Serving resources
166
183
@@ -296,7 +313,9 @@ internal class EpubNavigatorViewModel(
296
313
} else {
297
314
for ((href, changes) in source.changesByHref(target)) {
298
315
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
+ )
300
319
}
301
320
}
302
321
0 commit comments