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';
if (this._annotations[cacheKey]) {
return [...this._annotations[cacheKey]];
return Zotero.Items.get([...this._annotations[cacheKey]]);
}
var rows = this._annotations.rows.concat();
@ -3680,7 +3680,7 @@ Zotero.Item.prototype.getAnnotations = function (includeTrashed) {
}
var ids = rows.map(row => row.itemID);
this._annotations[cacheKey] = ids;
return ids;
return Zotero.Items.get(ids);
};

View file

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

View file

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

View file

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