Skip to content

Commit a3635b5

Browse files
authored
Optimize scrolling to an EPUB locator with a CSS selector (#435)
1 parent 2fdfea3 commit a3635b5

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

CHANGELOG.md

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

1313
* Support for standalone audio files and their metadata (contributed by [@domkm](https://github.com/readium/swift-toolkit/pull/414)).
1414

15-
1615
### Changed
1716

1817
The Readium Swift toolkit now requires a minimum of iOS 13.
@@ -26,6 +25,12 @@ The Readium Swift toolkit now requires a minimum of iOS 13.
2625

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

28+
### Fixed
29+
30+
#### Navigator
31+
32+
* Optimized scrolling to an EPUB text-based locator if it contains a CSS selector.
33+
2934

3035
## [3.0.0-alpha.1]
3136

Sources/Navigator/EPUB/Assets/Static/scripts/readium-fixed.js

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

Sources/Navigator/EPUB/Assets/Static/scripts/readium-reflowable.js

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

Sources/Navigator/EPUB/EPUBReflowableSpreadView.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ final class EPUBReflowableSpreadView: EPUBSpreadView {
254254
case let .locator(locator):
255255
go(to: locator) { _ in completion() }
256256
case .start:
257-
go(toProgression: 0) { _ in completion() }
257+
scroll(toProgression: 0) { _ in completion() }
258258
case .end:
259-
go(toProgression: 1) { _ in completion() }
259+
scroll(toProgression: 1) { _ in completion() }
260260
}
261261
}
262262

@@ -268,18 +268,18 @@ final class EPUBReflowableSpreadView: EPUBSpreadView {
268268
}
269269

270270
if locator.text.highlight != nil {
271-
go(toText: locator.text, completion: completion)
271+
scroll(toLocator: locator, completion: completion)
272272
// FIXME: find the first fragment matching a tag ID (need a regex)
273273
} else if let id = locator.locations.fragments.first, !id.isEmpty {
274-
go(toTagID: id, completion: completion)
274+
scroll(toTagID: id, completion: completion)
275275
} else {
276276
let progression = locator.locations.progression ?? 0
277-
go(toProgression: progression, completion: completion)
277+
scroll(toProgression: progression, completion: completion)
278278
}
279279
}
280280

281281
/// Scrolls at given progression (from 0.0 to 1.0)
282-
private func go(toProgression progression: Double, completion: @escaping (Bool) -> Void) {
282+
private func scroll(toProgression progression: Double, completion: @escaping (Bool) -> Void) {
283283
guard progression >= 0, progression <= 1 else {
284284
log(.warning, "Scrolling to invalid progression \(progression)")
285285
completion(false)
@@ -301,7 +301,7 @@ final class EPUBReflowableSpreadView: EPUBSpreadView {
301301
}
302302

303303
/// Scrolls at the tag with ID `tagID`.
304-
private func go(toTagID tagID: String, completion: @escaping (Bool) -> Void) {
304+
private func scroll(toTagID tagID: String, completion: @escaping (Bool) -> Void) {
305305
evaluateScript("readium.scrollToId(\'\(tagID)\');") { result in
306306
switch result {
307307
case let .success(value):
@@ -314,12 +314,12 @@ final class EPUBReflowableSpreadView: EPUBSpreadView {
314314
}
315315

316316
/// Scrolls at the snippet matching the given text context.
317-
private func go(toText text: Locator.Text, completion: @escaping (Bool) -> Void) {
318-
guard let json = text.jsonString else {
317+
private func scroll(toLocator locator: Locator, completion: @escaping (Bool) -> Void) {
318+
guard let json = locator.jsonString else {
319319
completion(false)
320320
return
321321
}
322-
evaluateScript("readium.scrollToText(\(json));") { result in
322+
evaluateScript("readium.scrollToLocator(\(json));") { result in
323323
switch result {
324324
case let .success(value):
325325
completion((value as? Bool) ?? false)

Sources/Navigator/EPUB/Scripts/src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
scrollRight,
1616
scrollToId,
1717
scrollToPosition,
18-
scrollToText,
18+
scrollToLocator,
1919
setProperty,
2020
setCSSProperties,
2121
} from "./utils";
@@ -26,7 +26,7 @@ global.readium = {
2626
// utils
2727
scrollToId: scrollToId,
2828
scrollToPosition: scrollToPosition,
29-
scrollToText: scrollToText,
29+
scrollToLocator: scrollToLocator,
3030
scrollLeft: scrollLeft,
3131
scrollRight: scrollRight,
3232
setCSSProperties: setCSSProperties,

Sources/Navigator/EPUB/Scripts/src/utils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ export function scrollToPosition(position, dir) {
172172

173173
// Scrolls to the first occurrence of the given text snippet.
174174
//
175-
// The expected text argument is a Locator Text object, as defined here:
175+
// The expected text argument is a Locator object, as defined here:
176176
// https://readium.org/architecture/models/locators/
177-
export function scrollToText(text) {
178-
let range = rangeFromLocator({ text });
177+
export function scrollToLocator(locator) {
178+
let range = rangeFromLocator(locator);
179179
if (!range) {
180180
return false;
181181
}

0 commit comments

Comments
 (0)