Skip to content

Commit 8b95d2d

Browse files
authored
Optimize scrolling to an EPUB locator with a CSS selector (readium#520)
1 parent ac6392c commit 8b95d2d

File tree

7 files changed

+19
-15
lines changed

7 files changed

+19
-15
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ All notable changes to this project will be documented in this file. Take a look
1212

1313
* EPUB: The `scroll` preference is now forced to `true` when rendering vertical text (e.g. CJK vertical). [See this discussion for the rationale](https://github.com/readium/swift-toolkit/discussions/370).
1414

15+
### Fixed
16+
17+
#### Navigator
18+
19+
* Optimized scrolling to an EPUB text-based locator if it contains a CSS selector.
20+
1521

1622
## [3.0.0-beta.1]
1723

readium/navigator/src/main/assets/_scripts/src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
scrollToId,
1717
scrollToPosition,
1818
scrollToStart,
19-
scrollToText,
19+
scrollToLocator,
2020
setProperty,
2121
setCSSProperties,
2222
} from "./utils";
@@ -29,7 +29,7 @@ window.readium = {
2929
// utils
3030
scrollToId: scrollToId,
3131
scrollToPosition: scrollToPosition,
32-
scrollToText: scrollToText,
32+
scrollToLocator: scrollToLocator,
3333
scrollLeft: scrollLeft,
3434
scrollRight: scrollRight,
3535
scrollToStart: scrollToStart,

readium/navigator/src/main/assets/_scripts/src/utils.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,14 @@ export function scrollToPosition(position) {
129129

130130
// Scrolls to the first occurrence of the given text snippet.
131131
//
132-
// The expected text argument is a Locator Text object, as defined here:
132+
// The expected text argument is a Locator object, as defined here:
133133
// https://readium.org/architecture/models/locators/
134-
export function scrollToText(text) {
135-
let range = rangeFromLocator({ text });
134+
export function scrollToLocator(locator) {
135+
let range = rangeFromLocator(locator);
136136
if (!range) {
137137
return false;
138138
}
139-
scrollToRange(range);
140-
return true;
139+
return scrollToRange(range);
141140
}
142141

143142
function scrollToRange(range) {

readium/navigator/src/main/assets/readium/scripts/readium-fixed.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readium/navigator/src/main/assets/readium/scripts/readium-reflowable.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readium/navigator/src/main/java/org/readium/r2/navigator/R2BasicWebView.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,9 @@ internal open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebV
509509
runJavaScript("readium.scrollToPosition(\"$progression\");")
510510
}
511511

512-
suspend fun scrollToText(text: Locator.Text): Boolean {
513-
val json = text.toJSON().toString()
514-
return runJavaScriptSuspend("readium.scrollToText($json);").toBoolean()
512+
suspend fun scrollToLocator(locator: Locator): Boolean {
513+
val json = locator.toJSON().toString()
514+
return runJavaScriptSuspend("readium.scrollToLocator($json);").toBoolean()
515515
}
516516

517517
fun setScrollMode(scrollMode: Boolean) {

readium/navigator/src/main/java/org/readium/r2/navigator/pager/R2EpubPageFragment.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,8 @@ internal class R2EpubPageFragment : Fragment() {
435435
readingProgression: ReadingProgression,
436436
locator: Locator
437437
) {
438-
val text = locator.text
439-
if (text.highlight != null) {
440-
if (webView.scrollToText(text)) {
438+
if (locator.text.highlight != null) {
439+
if (webView.scrollToLocator(locator)) {
441440
return
442441
}
443442
}

0 commit comments

Comments
 (0)