Store PDF state in .zotero-pdf-state

This commit is contained in:
Martynas Bagdonas 2020-09-21 14:35:10 +03:00 committed by Dan Stillman
parent 76e77d4422
commit 43c55594c1

View file

@ -6,6 +6,7 @@ Zotero.PDF = {
class ReaderWindow {
constructor() {
this.pdfStateFileName = '.zotero-pdf-state';
this.annotationItemIDs = [];
this._instanceID = Zotero.Utilities.randomString();
this._window = null;
@ -109,8 +110,8 @@ class ReaderWindow {
item._loaded.childItems = true;
let ids = item.getAnnotations();
let annotations = (await Promise.all(ids.map(id => this._getAnnotation(id)))).filter(x => x);
this.annotationItemIds = ids;
state = state || PDFStates[this._itemID];
this.annotationItemIDs = ids;
state = state || await this._loadState();
this._state = state;
this._postMessage({
action: 'open',
@ -169,6 +170,30 @@ class ReaderWindow {
this._window.close();
}
async _saveState(state) {
let item = Zotero.Items.get(this._itemID);
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
let item = Zotero.Items.get(this._itemID);
let file = Zotero.Attachments.getStorageDirectory(item);
file.append(this.pdfStateFileName);
file = file.path;
try {
if (await OS.File.exists(file)) {
let state = JSON.parse(await Zotero.File.getContentsAsync(file));
return state;
}
} catch (e) {
Zotero.logError(e);
}
return null;
}
_dataURLtoBlob(dataurl) {
let parts = dataurl.split(',');
let mime = parts[0].match(/:(.*?);/)[1];
@ -354,7 +379,7 @@ class ReaderWindow {
}
case 'setState': {
let { state } = message;
PDFStates[this._itemID] = state;
this._saveState(state);
this._state = state;
return;
}