From f52c50f055cafc1540e02933246372a77891c9fe Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Fri, 19 Feb 2021 13:00:46 +0200 Subject: [PATCH] Use synced pageIndex --- chrome/content/zotero/xpcom/reader.js | 30 ++++++++++++++++++--------- pdf-reader | 2 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/chrome/content/zotero/xpcom/reader.js b/chrome/content/zotero/xpcom/reader.js index 866b953005..6f7eb51f17 100644 --- a/chrome/content/zotero/xpcom/reader.js +++ b/chrome/content/zotero/xpcom/reader.js @@ -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': { diff --git a/pdf-reader b/pdf-reader index 18b9392a82..16a533308d 160000 --- a/pdf-reader +++ b/pdf-reader @@ -1 +1 @@ -Subproject commit 18b9392a82a1c3b1ed05646c0a6509486d630540 +Subproject commit 16a533308d1333175d728781d52bdfede94fe0c6