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

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