Skip to content

Commit

Permalink
Optimize scrolling to an EPUB locator with a CSS selector (readium#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu authored May 22, 2024
1 parent ac6392c commit 8b95d2d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ All notable changes to this project will be documented in this file. Take a look

* 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).

### Fixed

#### Navigator

* Optimized scrolling to an EPUB text-based locator if it contains a CSS selector.


## [3.0.0-beta.1]

Expand Down
4 changes: 2 additions & 2 deletions readium/navigator/src/main/assets/_scripts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
scrollToId,
scrollToPosition,
scrollToStart,
scrollToText,
scrollToLocator,
setProperty,
setCSSProperties,
} from "./utils";
Expand All @@ -29,7 +29,7 @@ window.readium = {
// utils
scrollToId: scrollToId,
scrollToPosition: scrollToPosition,
scrollToText: scrollToText,
scrollToLocator: scrollToLocator,
scrollLeft: scrollLeft,
scrollRight: scrollRight,
scrollToStart: scrollToStart,
Expand Down
9 changes: 4 additions & 5 deletions readium/navigator/src/main/assets/_scripts/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,14 @@ export function scrollToPosition(position) {

// Scrolls to the first occurrence of the given text snippet.
//
// The expected text argument is a Locator Text object, as defined here:
// The expected text argument is a Locator object, as defined here:
// https://readium.org/architecture/models/locators/
export function scrollToText(text) {
let range = rangeFromLocator({ text });
export function scrollToLocator(locator) {
let range = rangeFromLocator(locator);
if (!range) {
return false;
}
scrollToRange(range);
return true;
return scrollToRange(range);
}

function scrollToRange(range) {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,9 @@ internal open class R2BasicWebView(context: Context, attrs: AttributeSet) : WebV
runJavaScript("readium.scrollToPosition(\"$progression\");")
}

suspend fun scrollToText(text: Locator.Text): Boolean {
val json = text.toJSON().toString()
return runJavaScriptSuspend("readium.scrollToText($json);").toBoolean()
suspend fun scrollToLocator(locator: Locator): Boolean {
val json = locator.toJSON().toString()
return runJavaScriptSuspend("readium.scrollToLocator($json);").toBoolean()
}

fun setScrollMode(scrollMode: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,8 @@ internal class R2EpubPageFragment : Fragment() {
readingProgression: ReadingProgression,
locator: Locator
) {
val text = locator.text
if (text.highlight != null) {
if (webView.scrollToText(text)) {
if (locator.text.highlight != null) {
if (webView.scrollToLocator(locator)) {
return
}
}
Expand Down

0 comments on commit 8b95d2d

Please sign in to comment.