Use synced pageIndex

This commit is contained in:
Martynas Bagdonas 2021-02-19 13:00:46 +02:00 committed by Dan Stillman
parent 77ea19277d
commit f52c50f055
2 changed files with 21 additions and 11 deletions

View file

@ -32,7 +32,6 @@ class ReaderInstance {
this._window = null;
this._iframeWindow = null;
this._itemID = null;
this._state = null;
this._isReaderInitialized = false;
this._showItemPaneToggle = false;
this._initPromise = new Promise((resolve, reject) => {
@ -57,8 +56,7 @@ class ReaderInstance {
let annotationItems = item.getAnnotations();
let annotations = (await Promise.all(annotationItems.map(x => this._getAnnotation(x)))).filter(x => x);
this.annotationItemIDs = annotationItems.map(x => x.id);
state = state || await this._loadState();
this._state = state;
state = state || await this._getState();
this._postMessage({
action: 'open',
buf,
@ -132,28 +130,41 @@ class ReaderInstance {
this._postMessage({ action: 'setToolbarPlaceholderWidth', width });
}
async _saveState(state) {
async _setState(state) {
let item = Zotero.Items.get(this._itemID);
item.setAttachmentLastPageIndex(state.pageIndex);
let file = Zotero.Attachments.getStorageDirectory(item);
file.append(this.pdfStateFileName);
await Zotero.File.putContentsAsync(file, JSON.stringify(state));
}
async _loadState() {
// TODO: Validate data to make sure the older format doesn't crash the future pdf-reader
async _getState() {
let item = Zotero.Items.get(this._itemID);
let file = Zotero.Attachments.getStorageDirectory(item);
file.append(this.pdfStateFileName);
file = file.path;
let state;
try {
if (await OS.File.exists(file)) {
let state = JSON.parse(await Zotero.File.getContentsAsync(file));
return state;
state = JSON.parse(await Zotero.File.getContentsAsync(file));
}
}
catch (e) {
Zotero.logError(e);
}
let pageIndex = item.getAttachmentLastPageIndex();
if (state) {
if (Number.isInteger(pageIndex) && state.pageIndex !== pageIndex) {
state.pageIndex = pageIndex;
delete state.top;
delete state.left;
}
return state;
}
else if (Number.isInteger(pageIndex)) {
return { pageIndex };
}
return null;
}
@ -329,8 +340,7 @@ class ReaderInstance {
}
case 'setState': {
let { state } = message;
this._saveState(state);
this._state = state;
this._setState(state);
return;
}
case 'openTagsPopup': {

@ -1 +1 @@
Subproject commit 18b9392a82a1c3b1ed05646c0a6509486d630540
Subproject commit 16a533308d1333175d728781d52bdfede94fe0c6