johnfactotum / foliate-js

Render e-books in the browser
https://johnfactotum.github.io/foliate-js/reader.html
MIT License
404 stars 60 forks source link

Unable to navigate to a particular page using epub cfi #40

Open yashspr opened 6 days ago

yashspr commented 6 days ago

Hey John, first of all, thanks for building this amazing project. It is much better than epubjs.

Here is what i am trying to do:

  1. Save the reading position as I read the book, via the cfi, using the relocate event.
  2. On opening the book, open the last read page.

What is happening? I am retrieving the saved cfi and using the this.view.goTo() method to go to that cfi position. But it doesn't take me to the right page. Its usually one or two pages behind.

Here is a small snippet:

this.view = document.createElement("foliate-view");
this.attach_event_handlers();
await this.view.open(this.book);
document.getElementById(renderToElementId).append(this.view);

this.view.renderer.setStyles(styles);
const saved_cfi = "" // get from local storage

if (saved_cfi) {
  await this.view.goTo(saved_cfi);
} else {
  await this.view.next();
}

What I think is happening?

So I am not sure what is happening. Any help here is greatly apprecited. Thanks.

johnfactotum commented 6 days ago

It's hard to say. Some ideas:

yashspr commented 5 days ago

Thanks for the suggestions. Will debug and let you know

yashspr commented 5 days ago

You were right, the coordinates of the range from the cfi were not matching with the current page. And also noticed something strange. The width of the iframe, and the width of its parent div dont match. In my case, the width of the iframe is ~8000px, the width of the parent div is ~10500px.

And when I scrolled the div#container to position (0,0) using scrollTo, there was just empty space. So, there is some issue with iframe's parent div's width which is creating empty space and affecting the pagination.

When the section initially loads, it is somehow scrolled to the right position, which is why next() and prev() work fine i believe.

johnfactotum commented 5 days ago

No, that's expected. In order to slide out the section when transitioning to the next/previous section, there is one extra empty "page" on both sides of the section, which complicates things a little.