Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop the first page of a chapter counting as the last page of the one before

Barnabas7bd8db17b67e
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index c6ef89a2..0642de23 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -853,13 +853,15 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
source: '''
(function() {
rendition.on('relocated', function(location) {
- if (location && location.start && location.start.displayed) {
- window.flutter_inappwebview.callHandler('pageInfo', {
+ var info = (typeof readerPageInfo === 'function') ? readerPageInfo() : null;
+ if (!info && location && location.start && location.start.displayed) {
+ info = {
page: location.start.displayed.page,
total: location.start.displayed.total,
href: location.start.href || ''
- });
+ };
}
+ if (info) window.flutter_inappwebview.callHandler('pageInfo', info);
});
})();
''',
@@ -3066,6 +3068,12 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
out.page = loc.start.displayed.page;
out.total = loc.start.displayed.total;
}
+ var info = (typeof readerPageInfo === 'function') ? readerPageInfo() : null;
+ if (info) {
+ out.page = info.page;
+ out.total = info.total;
+ out.href = info.href || out.href;
+ }
out.percentage = (typeof loc.start.percentage === 'number') ? loc.start.percentage : null;
if ((out.percentage == null || out.percentage === 0) && book.locations && loc.start.cfi) {
try {
diff --git a/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js b/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
index bd8fdb64..61b94e9b 100644
--- a/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
+++ b/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
@@ -2626,6 +2626,38 @@ function blindPageIdOf(r) {
return null;
}
+// The page the live rendition is showing within its chapter, read off the
+// manager's geometry the same way the blind is placed. The reported
+// location is one page behind at every chapter boundary: the previous view
+// still touches the container's edge with zero width, the location mapping
+// picks it up, and the new chapter's first page reports as the old one's
+// last while its second page reports as page 1. Pages count columns like
+// epub.js does, so a spread advances by two.
+function readerPageInfo() {
+ try {
+ var m = rendition.manager;
+ var c = m.container.getBoundingClientRect();
+ var rtl = m.settings && m.settings.direction === "rtl";
+ var probe = rtl ? c.right - 1 : c.left + 1;
+ var views = m.views.all();
+ for (var i = 0; i < views.length; i++) {
+ var v = views[i];
+ if (!v || !v.element || !v.section) continue;
+ var b = v.element.getBoundingClientRect();
+ if (b.left <= probe && b.right > probe) {
+ var delta = m.layout.delta || b.width || 1;
+ var pageWidth = m.layout.pageWidth || delta;
+ var divisor = m.layout.divisor || 1;
+ var into = rtl ? b.right - probe : probe - b.left;
+ var total = Math.max(1, Math.ceil(b.width / delta) * divisor);
+ var page = Math.min(total, Math.floor(into / pageWidth) + 1);
+ return { page: page, total: total, href: v.section.href || '', index: v.section.index };
+ }
+ }
+ } catch (e) {}
+ return null;
+}
+
// 1 = the blind is past the live page, 0 = same page, -1 = behind it,
// null = one of them has nothing laid out yet.
function blindPageCmp() {