Skip to content

Commit e14cbc6

Browse files
authored
Adjust EpubNavigatorFragment.firstVisibleElementLocator() strategy (#331)
1 parent af2f764 commit e14cbc6

File tree

5 files changed

+20
-39
lines changed

5 files changed

+20
-39
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ All notable changes to this project will be documented in this file. Take a look
1515
* The native font size strategy introduced in 2.3.0 uses the Android web view's [`WebSettings.textZoom`](https://developer.android.com/reference/android/webkit/WebSettings#setTextZoom(int)) property to adjust the font size. 2.2.0 was using Readium CSS's [`--USER__fontSize` variable](https://readium.org/readium-css/docs/CSS12-user_prefs.html#font-size).
1616
* `WebSettings.textZoom` will work with more publications than `--USER__fontSize`, even the ones poorly authored. However the page width is not adjusted when changing the font size to keep the optimal line length.
1717

18+
#### Changed
19+
20+
* `EpubNavigatorFragment.firstVisibleElementLocator()` now returns the first *block* element that is visible on the screen, even if it starts on previous pages.
21+
* This is used to make sure the user will not miss any context when restoring a TTS session in the middle of a resource.
22+
1823
## [2.3.0]
1924

2025
### Added

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

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
// available in the top-level LICENSE file of the project.
55
//
66

7-
import { isScrollModeEnabled, pageWidth } from "./utils";
7+
import { isScrollModeEnabled } from "./utils";
88
import { getCssSelector } from "css-selector-generator";
99

1010
export function findFirstVisibleLocator() {
1111
const element = findElement(document.body);
12-
if (!element) {
13-
return undefined;
14-
}
15-
1612
return {
1713
href: "#",
1814
type: "application/xhtml+xml",
@@ -26,59 +22,38 @@ export function findFirstVisibleLocator() {
2622
}
2723

2824
function findElement(rootElement) {
29-
var foundElement = undefined;
30-
for (var i = rootElement.children.length - 1; i >= 0; i--) {
25+
for (var i = 0; i < rootElement.children.length; i++) {
3126
const child = rootElement.children[i];
32-
const position = elementRelativePosition(child, undefined);
33-
if (position == 0) {
34-
if (!shouldIgnoreElement(child)) {
35-
foundElement = child;
36-
}
37-
} else if (position < 0) {
38-
if (!foundElement) {
39-
foundElement = child;
40-
}
41-
break;
27+
if (!shouldIgnoreElement(child) && isElementVisible(child)) {
28+
return findElement(child);
4229
}
4330
}
44-
45-
if (foundElement) {
46-
return findElement(foundElement);
47-
}
4831
return rootElement;
4932
}
5033

51-
// See computeVisibility_() in r2-navigator-js
52-
function elementRelativePosition(element, domRect /* nullable */) {
34+
function isElementVisible(element) {
5335
if (readium.isFixedLayout) return true;
5436

5537
if (element === document.body || element === document.documentElement) {
56-
return -1;
38+
return true;
5739
}
5840
if (!document || !document.documentElement || !document.body) {
59-
return 1;
41+
return false;
6042
}
6143

62-
const rect = domRect || element.getBoundingClientRect();
63-
44+
const rect = element.getBoundingClientRect();
6445
if (isScrollModeEnabled()) {
65-
return rect.top >= 0 && rect.top <= document.documentElement.clientHeight;
46+
return rect.bottom > 0 && rect.top < window.innerHeight;
6647
} else {
67-
if (rect.left >= pageWidth) {
68-
return 1;
69-
} else if (rect.left >= 0) {
70-
return 0;
71-
} else {
72-
return -1;
73-
}
48+
return rect.right > 0 && rect.left < window.innerWidth;
7449
}
7550
}
7651

7752
function shouldIgnoreElement(element) {
7853
const elStyle = getComputedStyle(element);
7954
if (elStyle) {
8055
const display = elStyle.getPropertyValue("display");
81-
if (display === "none") {
56+
if (display != "block") {
8257
return true;
8358
}
8459
// Cannot be relied upon, because web browser engine reports invisible when out of view in

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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/epub/EpubNavigatorFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,8 @@ class EpubNavigatorFragment internal constructor(
903903
)
904904

905905
/**
906-
* Returns the [Locator] to the first HTML element that begins on the current screen.
906+
* Returns the [Locator] to the first HTML *block* element that is visible on the screen, even
907+
* if it begins on previous screen pages.
907908
*/
908909
@ExperimentalReadiumApi
909910
override suspend fun firstVisibleElementLocator(): Locator? {

0 commit comments

Comments
 (0)