Properly ignore deleted PDF attachments and close pdf-reader tabs

#2006
This commit is contained in:
Martynas Bagdonas 2021-03-17 12:06:58 +02:00
parent 8013a6ce97
commit 1677e957cd
2 changed files with 40 additions and 32 deletions

View file

@ -173,9 +173,11 @@ var ZoteroContextPane = new function () {
})();
var attachment = Zotero.Items.get(reader.itemID);
_selectNotesContext(attachment.libraryID);
var notesContext = _getNotesContext(attachment.libraryID);
notesContext.updateFromCache();
if (attachment) {
_selectNotesContext(attachment.libraryID);
var notesContext = _getNotesContext(attachment.libraryID);
notesContext.updateFromCache();
}
}
_contextPaneSplitter.setAttribute('hidden', false);

View file

@ -133,10 +133,12 @@ class ReaderInstance {
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));
if (item) {
item.setAttachmentLastPageIndex(state.pageIndex);
let file = Zotero.Attachments.getStorageDirectory(item);
file.append(this.pdfStateFileName);
await Zotero.File.putContentsAsync(file, JSON.stringify(state));
}
}
async _getState() {
@ -410,7 +412,7 @@ class ReaderInstance {
}
case 'setState': {
let { state } = message;
this._setState(state);
await this._setState(state);
return;
}
case 'openTagsPopup': {
@ -766,33 +768,37 @@ class Reader {
}
}
}
// Listen for parent item, PDF attachment and its annotations updates
else if (type === 'item') {
// Listen for the parent item, PDF attachment and its annotations updates
for (let reader of this._readers) {
if (event === 'delete') {
let disappearedIDs = reader.annotationItemIDs.filter(x => ids.includes(x));
if (disappearedIDs.length) {
let keys = disappearedIDs.map(id => extraData[id].key);
reader.unsetAnnotations(keys);
}
if (ids.includes(reader._itemID)) {
reader.close();
}
for (let reader of this._readers.slice()) {
if (event === 'delete' && ids.includes(reader._itemID)) {
reader.close();
}
else {
let item = Zotero.Items.get(reader._itemID);
let annotationItems = item.getAnnotations();
reader.annotationItemIDs = annotationItems.map(x => x.id);
let affectedAnnotations = annotationItems.filter(({ id }) => (
ids.includes(id)
&& !(extraData && extraData[id] && extraData[id].instanceID === reader._instanceID)
));
if (affectedAnnotations.length) {
reader.setAnnotations(affectedAnnotations);
// Ignore other notifications if the attachment no longer exists
let item = Zotero.Items.get(reader._itemID);
if (item) {
if (event === 'delete') {
let disappearedIDs = reader.annotationItemIDs.filter(x => ids.includes(x));
if (disappearedIDs.length) {
let keys = disappearedIDs.map(id => extraData[id].key);
reader.unsetAnnotations(keys);
}
}
// Update title if the PDF attachment or the parent item changes
if (ids.includes(reader._itemID) || ids.includes(item.parentItemID)) {
reader.updateTitle();
else {
let annotationItems = item.getAnnotations();
reader.annotationItemIDs = annotationItems.map(x => x.id);
let affectedAnnotations = annotationItems.filter(({ id }) => (
ids.includes(id)
&& !(extraData && extraData[id] && extraData[id].instanceID === reader._instanceID)
));
if (affectedAnnotations.length) {
reader.setAnnotations(affectedAnnotations);
}
// Update title if the PDF attachment or the parent item changes
if (ids.includes(reader._itemID) || ids.includes(item.parentItemID)) {
reader.updateTitle();
}
}
}
}