Skip to content

Optimize scrolling to an EPUB locator with a CSS selector #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading