Return Zotero.Item objects from getAnnotations() instead of ids

Returning ids mirrors getAttachments() and getNotes(), but I think we
want to move towards a world where those return actual objects, even if
we need async load calls on the returned objects.
This commit is contained in:
Dan Stillman 2020-12-26 02:32:11 -05:00
parent 60cb299f40
commit 69958d4356
4 changed files with 20 additions and 23 deletions

View file

@ -3670,7 +3670,7 @@ Zotero.Item.prototype.getAnnotations = function (includeTrashed) {
var cacheKey = 'with' + (includeTrashed ? '' : 'out') + 'Trashed'; var cacheKey = 'with' + (includeTrashed ? '' : 'out') + 'Trashed';
if (this._annotations[cacheKey]) { if (this._annotations[cacheKey]) {
return [...this._annotations[cacheKey]]; return Zotero.Items.get([...this._annotations[cacheKey]]);
} }
var rows = this._annotations.rows.concat(); var rows = this._annotations.rows.concat();
@ -3680,7 +3680,7 @@ Zotero.Item.prototype.getAnnotations = function (includeTrashed) {
} }
var ids = rows.map(row => row.itemID); var ids = rows.map(row => row.itemID);
this._annotations[cacheKey] = ids; this._annotations[cacheKey] = ids;
return ids; return Zotero.Items.get(ids);
}; };

View file

@ -157,10 +157,9 @@ class PDFWorker {
if (!this.isPDFAttachment(attachment)) { if (!this.isPDFAttachment(attachment)) {
throw new Error('not a valid attachment'); throw new Error('not a valid attachment');
} }
let ids = attachment.getAnnotations(); let items = attachment.getAnnotations();
let annotations = []; let annotations = [];
for (let id of ids) { for (let item of items) {
let item = await Zotero.Items.getAsync(id);
annotations.push({ annotations.push({
id: item.key, id: item.key,
type: item.annotationType, type: item.annotationType,
@ -222,10 +221,9 @@ class PDFWorker {
} }
// TODO: Remove when fixed // TODO: Remove when fixed
attachment._loaded.childItems = true; attachment._loaded.childItems = true;
let ids = attachment.getAnnotations(); let items = attachment.getAnnotations();
let existingAnnotations = []; let existingAnnotations = [];
for (let id of ids) { for (let item of items) {
let item = await Zotero.Items.getAsync(id);
existingAnnotations.push({ existingAnnotations.push({
id: item.key, id: item.key,
type: item.annotationType, type: item.annotationType,

View file

@ -37,9 +37,9 @@ class ReaderInstance {
buf = new Uint8Array(buf).buffer; buf = new Uint8Array(buf).buffer;
// TODO: Remove when fixed // TODO: Remove when fixed
item._loaded.childItems = true; item._loaded.childItems = true;
let ids = item.getAnnotations(); let annotationItems = item.getAnnotations();
let annotations = (await Promise.all(ids.map(id => this._getAnnotation(id)))).filter(x => x); let annotations = (await Promise.all(annotationItems.map(x => this._getAnnotation(x)))).filter(x => x);
this.annotationItemIDs = ids; this.annotationItemIDs = annotationItems.map(x => x.id);
state = state || await this._loadState(); state = state || await this._loadState();
this._state = state; this._state = state;
this._postMessage({ this._postMessage({
@ -439,9 +439,8 @@ class ReaderInstance {
* @param itemID * @param itemID
* @returns {Object|null} * @returns {Object|null}
*/ */
async _getAnnotation(itemID) { async _getAnnotation(item) {
try { try {
let item = Zotero.Items.get(itemID);
if (!item || !item.isAnnotation()) { if (!item || !item.isAnnotation()) {
return null; return null;
} }
@ -704,10 +703,10 @@ class Reader {
let item = Zotero.Items.get(readerWindow._itemID); let item = Zotero.Items.get(readerWindow._itemID);
// TODO: Remove when fixed // TODO: Remove when fixed
item._loaded.childItems = true; item._loaded.childItems = true;
let annotationItemIDs = item.getAnnotations(); let annotationItems = item.getAnnotations();
readerWindow.annotationItemIDs = annotationItemIDs; readerWindow.annotationItemIDs = annotationItems.map(x => x.id);
let affectedAnnotationIds = annotationItemIDs.filter(annotationID => { let affectedAnnotationIds = annotationItems.filter(annotation => {
let annotation = Zotero.Items.get(annotationID); let annotationID = annotation.id;
let imageAttachmentID = null; let imageAttachmentID = null;
annotation._loaded.childItems = true; annotation._loaded.childItems = true;
let annotationAttachments = annotation.getAttachments(); let annotationAttachments = annotation.getAttachments();

View file

@ -1341,14 +1341,14 @@ describe("Zotero.Item", function () {
await annotation2.saveTx(); await annotation2.saveTx();
}); });
it("should return ids of annotations not in trash", async function () { it("should return annotations not in trash", async function () {
var ids = attachment.getAnnotations(); var items = attachment.getAnnotations();
assert.sameMembers(ids, [annotation1.id]); assert.sameMembers(items, [annotation1]);
}); });
it("should return ids of annotations in trash if includeTrashed=true", async function () { it("should return annotations in trash if includeTrashed=true", async function () {
var ids = attachment.getAnnotations(true); var items = attachment.getAnnotations(true);
assert.sameMembers(ids, [annotation1.id, annotation2.id]); assert.sameMembers(items, [annotation1, annotation2]);
}); });
}); });
}); });