2020-06-20 05:29:32 +00:00
|
|
|
describe("Zotero.Annotations", function() {
|
|
|
|
var exampleHighlight = {
|
2021-01-20 12:12:59 +00:00
|
|
|
"libraryID": null,
|
2020-06-20 05:29:32 +00:00
|
|
|
"key": "92JLMCVT",
|
|
|
|
"type": "highlight",
|
2021-01-26 07:24:26 +00:00
|
|
|
"isExternal": false,
|
2021-06-09 15:12:05 +00:00
|
|
|
"readOnly": false,
|
2020-06-20 05:29:32 +00:00
|
|
|
"text": "This is an <b>extracted</b> text with rich-text\nAnd a new line",
|
|
|
|
"comment": "This is a comment with <i>rich-text</i>\nAnd a new line",
|
|
|
|
"color": "#ffec00",
|
|
|
|
"pageLabel": "15",
|
2020-09-09 04:14:11 +00:00
|
|
|
"sortIndex": "00015|002431|00000",
|
2020-06-20 05:29:32 +00:00
|
|
|
"position": {
|
|
|
|
"pageIndex": 1,
|
|
|
|
"rects": [
|
|
|
|
[231.284, 402.126, 293.107, 410.142],
|
|
|
|
[54.222, 392.164, 293.107, 400.18],
|
|
|
|
[54.222, 382.201, 293.107, 390.217],
|
|
|
|
[54.222, 372.238, 293.107, 380.254],
|
|
|
|
[54.222, 362.276, 273.955, 370.292]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"tags": [
|
|
|
|
{
|
|
|
|
"name": "math",
|
|
|
|
"color": "#ff0000"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "chemistry"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"dateModified": "2019-05-14 06:50:40"
|
|
|
|
};
|
2020-09-10 10:13:51 +00:00
|
|
|
var exampleHighlightAlt = jsonPositionToString(exampleHighlight);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
|
|
|
var exampleNote = {
|
2021-01-20 12:12:59 +00:00
|
|
|
"libraryID": null,
|
2020-06-20 05:29:32 +00:00
|
|
|
"key": "5TKU34XX",
|
|
|
|
"type": "note",
|
2021-01-26 07:24:26 +00:00
|
|
|
"isExternal": false,
|
2021-06-09 15:12:05 +00:00
|
|
|
"readOnly": false,
|
2020-06-20 05:29:32 +00:00
|
|
|
"comment": "This is a note",
|
|
|
|
"color": "#ffec00",
|
|
|
|
"pageLabel": "14",
|
2020-09-09 04:14:11 +00:00
|
|
|
"sortIndex": "00014|001491|00283",
|
2020-06-20 05:29:32 +00:00
|
|
|
"position": {
|
|
|
|
"pageIndex": 0,
|
|
|
|
"rects": [
|
|
|
|
[371.395, 266.635, 486.075, 274.651]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"dateModified": "2019-05-14 06:50:54"
|
|
|
|
};
|
2020-09-10 10:13:51 +00:00
|
|
|
var exampleNoteAlt = jsonPositionToString(exampleNote);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
2020-09-04 23:39:19 +00:00
|
|
|
var exampleImage = {
|
2021-01-20 12:12:59 +00:00
|
|
|
"libraryID": null,
|
2020-06-20 05:29:32 +00:00
|
|
|
"key": "QD32MQJF",
|
2020-09-04 23:39:19 +00:00
|
|
|
"type": "image",
|
2021-01-26 07:24:26 +00:00
|
|
|
"isExternal": false,
|
2021-06-09 15:12:05 +00:00
|
|
|
"readOnly": false,
|
2020-12-28 03:18:17 +00:00
|
|
|
"image": "zotero://attachment/library/items/LB417FR4",
|
2020-06-20 05:29:32 +00:00
|
|
|
"comment": "This is a comment",
|
|
|
|
"color": "#ffec00",
|
|
|
|
"pageLabel": "XVI",
|
2020-09-09 04:14:11 +00:00
|
|
|
"sortIndex": "00016|003491|00683",
|
2020-06-20 05:29:32 +00:00
|
|
|
"position": {
|
|
|
|
"pageIndex": 123,
|
|
|
|
"rects": [
|
|
|
|
[314.4, 412.8, 556.2, 609.6]
|
|
|
|
],
|
|
|
|
"width": 400,
|
|
|
|
"height": 200
|
|
|
|
},
|
|
|
|
"dateModified": "2019-05-14 06:51:22"
|
|
|
|
};
|
2020-09-10 10:13:51 +00:00
|
|
|
var exampleImageAlt = jsonPositionToString(exampleImage);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
|
|
|
var exampleGroupHighlight = {
|
2021-01-20 12:12:59 +00:00
|
|
|
"libraryID": null,
|
2020-06-20 05:29:32 +00:00
|
|
|
"key": "PE57YAYH",
|
|
|
|
"type": "highlight",
|
2021-01-26 07:24:26 +00:00
|
|
|
"isExternal": false,
|
2020-06-20 05:29:32 +00:00
|
|
|
"authorName": "Kate Smith",
|
|
|
|
"text": "This is an <b>extracted</b> text with rich-text\nAnd a new line",
|
|
|
|
"comment": "This is a comment with <i>rich-text</i>\nAnd a new line",
|
|
|
|
"color": "#ffec00",
|
|
|
|
"pageLabel": "15",
|
2020-09-09 04:14:11 +00:00
|
|
|
"sortIndex": "00015|002431|00000",
|
2020-06-20 05:29:32 +00:00
|
|
|
"position": {
|
|
|
|
"pageIndex": 1,
|
|
|
|
"rects": [
|
|
|
|
[231.284, 402.126, 293.107, 410.142],
|
|
|
|
[54.222, 392.164, 293.107, 400.18],
|
|
|
|
[54.222, 382.201, 293.107, 390.217],
|
|
|
|
[54.222, 372.238, 293.107, 380.254],
|
|
|
|
[54.222, 362.276, 273.955, 370.292]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"dateModified": "2019-05-14 06:50:40"
|
|
|
|
};
|
2020-09-10 10:13:51 +00:00
|
|
|
var exampleGroupHighlightAlt = jsonPositionToString(exampleGroupHighlight);
|
|
|
|
|
|
|
|
// Item.position is a string, so when using the annotation JSON as input or when comparing we
|
|
|
|
// have to use a version where 'position' has been stringified
|
|
|
|
function jsonPositionToString(json) {
|
|
|
|
var o = Object.assign({}, json);
|
|
|
|
o.position = JSON.stringify(o.position);
|
|
|
|
return o;
|
|
|
|
}
|
2020-06-20 05:29:32 +00:00
|
|
|
|
|
|
|
var item;
|
|
|
|
var attachment;
|
|
|
|
var group;
|
|
|
|
var groupItem;
|
|
|
|
var groupAttachment;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
item = await createDataObject('item');
|
|
|
|
attachment = await importFileAttachment('test.pdf', { parentID: item.id });
|
2021-01-20 12:12:59 +00:00
|
|
|
exampleHighlight.libraryID = item.libraryID;
|
|
|
|
exampleNote.libraryID = item.libraryID;
|
|
|
|
exampleImage.libraryID = item.libraryID;
|
2020-06-20 05:29:32 +00:00
|
|
|
|
2022-01-29 07:13:13 +00:00
|
|
|
group = await getGroup();
|
2021-01-20 12:12:59 +00:00
|
|
|
exampleGroupHighlight.libraryID = group.libraryID;
|
2020-06-20 05:29:32 +00:00
|
|
|
groupItem = await createDataObject('item', { libraryID: group.libraryID });
|
|
|
|
groupAttachment = await importFileAttachment(
|
|
|
|
'test.pdf',
|
|
|
|
{ libraryID: group.libraryID, parentID: groupItem.id }
|
2022-01-29 07:13:13 +00:00
|
|
|
);
|
2020-06-20 05:29:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("#toJSON()", function () {
|
|
|
|
it("should generate an object for a highlight", async function () {
|
|
|
|
var annotation = new Zotero.Item('annotation');
|
|
|
|
annotation.libraryID = attachment.libraryID;
|
|
|
|
annotation.key = exampleHighlight.key;
|
|
|
|
await annotation.loadPrimaryData();
|
|
|
|
annotation.parentID = attachment.id;
|
|
|
|
annotation.annotationType = 'highlight';
|
|
|
|
for (let prop of ['text', 'comment', 'color', 'pageLabel', 'sortIndex', 'position']) {
|
|
|
|
let itemProp = 'annotation' + prop[0].toUpperCase() + prop.substr(1);
|
2020-09-10 10:13:51 +00:00
|
|
|
annotation[itemProp] = exampleHighlightAlt[prop];
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
|
|
|
annotation.addTag("math");
|
|
|
|
annotation.addTag("chemistry");
|
|
|
|
await annotation.saveTx();
|
|
|
|
await Zotero.Tags.setColor(annotation.libraryID, "math", "#ff0000", 0);
|
2020-12-28 03:18:17 +00:00
|
|
|
var json = await Zotero.Annotations.toJSON(annotation);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
|
|
|
assert.sameMembers(Object.keys(json), Object.keys(exampleHighlight));
|
|
|
|
for (let prop of Object.keys(exampleHighlight)) {
|
|
|
|
if (prop == 'dateModified') {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-11 07:37:57 +00:00
|
|
|
assert.deepEqual(json[prop], exampleHighlight[prop], `'${prop}' doesn't match`);
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await annotation.eraseTx();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should generate an object for a note", async function () {
|
|
|
|
var annotation = new Zotero.Item('annotation');
|
|
|
|
annotation.libraryID = attachment.libraryID;
|
|
|
|
annotation.key = exampleNote.key;
|
|
|
|
await annotation.loadPrimaryData();
|
|
|
|
annotation.parentID = attachment.id;
|
|
|
|
annotation.annotationType = 'note';
|
|
|
|
for (let prop of ['comment', 'color', 'pageLabel', 'sortIndex', 'position']) {
|
|
|
|
let itemProp = 'annotation' + prop[0].toUpperCase() + prop.substr(1);
|
2020-09-10 10:13:51 +00:00
|
|
|
annotation[itemProp] = exampleNoteAlt[prop];
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
|
|
|
await annotation.saveTx();
|
2020-12-28 03:18:17 +00:00
|
|
|
var json = await Zotero.Annotations.toJSON(annotation);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
|
|
|
assert.sameMembers(Object.keys(json), Object.keys(exampleNote));
|
|
|
|
for (let prop of Object.keys(exampleNote)) {
|
|
|
|
if (prop == 'dateModified') {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-11 07:37:57 +00:00
|
|
|
assert.deepEqual(json[prop], exampleNote[prop], `'${prop}' doesn't match`);
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await annotation.eraseTx();
|
|
|
|
});
|
|
|
|
|
2020-09-04 23:39:19 +00:00
|
|
|
it("should generate an object for an image", async function () {
|
2020-06-20 05:29:32 +00:00
|
|
|
var annotation = new Zotero.Item('annotation');
|
|
|
|
annotation.libraryID = attachment.libraryID;
|
2020-09-04 23:39:19 +00:00
|
|
|
annotation.key = exampleImage.key;
|
2020-06-20 05:29:32 +00:00
|
|
|
await annotation.loadPrimaryData();
|
|
|
|
annotation.parentID = attachment.id;
|
2020-09-04 23:39:19 +00:00
|
|
|
annotation.annotationType = 'image';
|
2020-06-20 05:29:32 +00:00
|
|
|
for (let prop of ['comment', 'color', 'pageLabel', 'sortIndex', 'position']) {
|
|
|
|
let itemProp = 'annotation' + prop[0].toUpperCase() + prop.substr(1);
|
2020-09-10 10:13:51 +00:00
|
|
|
annotation[itemProp] = exampleImageAlt[prop];
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
|
|
|
await annotation.saveTx();
|
|
|
|
|
|
|
|
// Get Blob from file and attach it
|
|
|
|
var path = OS.Path.join(getTestDataDirectory().path, 'test.png');
|
|
|
|
var imageData = await Zotero.File.getBinaryContentsAsync(path);
|
|
|
|
var array = new Uint8Array(imageData.length);
|
|
|
|
for (let i = 0; i < imageData.length; i++) {
|
|
|
|
array[i] = imageData.charCodeAt(i);
|
|
|
|
}
|
2020-12-28 03:18:17 +00:00
|
|
|
var blob = new Blob([array], { type: 'image/png' });
|
2021-01-20 12:12:59 +00:00
|
|
|
var file = await Zotero.Annotations.saveCacheImage(annotation, blob);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
2020-12-28 03:18:17 +00:00
|
|
|
var json = await Zotero.Annotations.toJSON(annotation);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
2020-09-04 23:39:19 +00:00
|
|
|
assert.sameMembers(Object.keys(json), Object.keys(exampleImage));
|
|
|
|
for (let prop of Object.keys(exampleImage)) {
|
2020-12-28 03:18:17 +00:00
|
|
|
if (prop == 'image'
|
2020-06-20 05:29:32 +00:00
|
|
|
|| prop == 'dateModified') {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-11 07:37:57 +00:00
|
|
|
assert.deepEqual(json[prop], exampleImage[prop], `'${prop}' doesn't match`);
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
2020-12-28 03:18:17 +00:00
|
|
|
|
|
|
|
var imageVal = await new Zotero.Promise((resolve) => {
|
|
|
|
var reader = new FileReader();
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
reader.onloadend = function() {
|
|
|
|
resolve(reader.result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
assert.equal(json.image, imageVal);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
|
|
|
await annotation.eraseTx();
|
|
|
|
});
|
|
|
|
|
2022-01-29 07:13:13 +00:00
|
|
|
it("should generate an object for a highlight by another user in a group library", async function () {
|
|
|
|
await Zotero.Users.setName(12345, 'First Last');
|
2020-06-20 05:29:32 +00:00
|
|
|
|
|
|
|
var annotation = new Zotero.Item('annotation');
|
2022-01-29 07:13:13 +00:00
|
|
|
annotation.libraryID = groupAttachment.libraryID;
|
2020-06-20 05:29:32 +00:00
|
|
|
annotation.key = exampleGroupHighlight.key;
|
|
|
|
await annotation.loadPrimaryData();
|
|
|
|
annotation.createdByUserID = 12345;
|
|
|
|
annotation.parentID = groupAttachment.id;
|
|
|
|
annotation.annotationType = 'highlight';
|
|
|
|
for (let prop of ['text', 'comment', 'color', 'pageLabel', 'sortIndex', 'position']) {
|
|
|
|
let itemProp = 'annotation' + prop[0].toUpperCase() + prop.substr(1);
|
2020-09-10 10:13:51 +00:00
|
|
|
annotation[itemProp] = exampleGroupHighlightAlt[prop];
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
2022-01-29 07:13:13 +00:00
|
|
|
await annotation.saveTx({
|
|
|
|
skipEditCheck: true
|
|
|
|
});
|
2020-12-28 03:18:17 +00:00
|
|
|
var json = await Zotero.Annotations.toJSON(annotation);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
2022-01-29 07:13:13 +00:00
|
|
|
assert.equal(json.authorName, 'First Last');
|
2020-06-20 05:29:32 +00:00
|
|
|
|
2022-01-29 07:13:13 +00:00
|
|
|
await annotation.eraseTx({
|
|
|
|
skipEditCheck: true
|
|
|
|
});
|
2020-06-20 05:29:32 +00:00
|
|
|
});
|
2022-02-08 09:33:33 +00:00
|
|
|
|
|
|
|
it("should generate an object for a highlight by another user modified by the current user in a group library", async function () {
|
|
|
|
await Zotero.Users.setName(1, 'My Name');
|
|
|
|
await Zotero.Users.setName(12345, 'Their Name');
|
|
|
|
|
|
|
|
var annotation = await createAnnotation('highlight', groupAttachment);
|
|
|
|
annotation.createdByUserID = 12345;
|
|
|
|
annotation.lastModifiedByUserID = 1;
|
|
|
|
await annotation.saveTx({
|
|
|
|
skipEditCheck: true
|
|
|
|
});
|
|
|
|
var json = await Zotero.Annotations.toJSON(annotation);
|
|
|
|
|
|
|
|
assert.equal(json.authorName, 'Their Name');
|
|
|
|
assert.equal(json.lastModifiedByUser, 'My Name');
|
|
|
|
|
|
|
|
await annotation.eraseTx({
|
|
|
|
skipEditCheck: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should generate an object for an annotation by another user in a personal library", async function () {
|
|
|
|
var annotation = await createAnnotation('highlight', attachment);
|
|
|
|
annotation.annotationAuthorName = 'First Last';
|
|
|
|
await annotation.saveTx();
|
|
|
|
|
|
|
|
var json = await Zotero.Annotations.toJSON(annotation);
|
|
|
|
|
|
|
|
assert.equal(json.authorName, 'First Last');
|
|
|
|
|
|
|
|
await annotation.eraseTx();
|
|
|
|
});
|
2020-06-20 05:29:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe("#saveFromJSON()", function () {
|
|
|
|
it("should create an item from a highlight", async function () {
|
|
|
|
var annotation = await Zotero.Annotations.saveFromJSON(attachment, exampleHighlight);
|
|
|
|
|
|
|
|
assert.equal(annotation.key, exampleHighlight.key);
|
|
|
|
for (let prop of ['text', 'comment', 'color', 'pageLabel', 'sortIndex', 'position']) {
|
|
|
|
let itemProp = 'annotation' + prop[0].toUpperCase() + prop.substr(1);
|
2020-09-10 10:13:51 +00:00
|
|
|
assert.deepEqual(annotation[itemProp], exampleHighlightAlt[prop], `'${prop}' doesn't match`);
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
|
|
|
var itemTags = annotation.getTags().map(t => t.tag);
|
|
|
|
var jsonTags = exampleHighlight.tags.map(t => t.name);
|
|
|
|
assert.sameMembers(itemTags, jsonTags);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create an item from a note", async function () {
|
|
|
|
var annotation = await Zotero.Annotations.saveFromJSON(attachment, exampleNote);
|
|
|
|
|
|
|
|
assert.equal(annotation.key, exampleNote.key);
|
|
|
|
for (let prop of ['comment', 'color', 'pageLabel', 'sortIndex', 'position']) {
|
|
|
|
let itemProp = 'annotation' + prop[0].toUpperCase() + prop.substr(1);
|
2020-09-10 10:13:51 +00:00
|
|
|
assert.deepEqual(annotation[itemProp], exampleNoteAlt[prop], `'${prop}' doesn't match`);
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-09-04 23:39:19 +00:00
|
|
|
it("should create an item from an image", async function () {
|
|
|
|
var annotation = await Zotero.Annotations.saveFromJSON(attachment, exampleImage);
|
2020-06-20 05:29:32 +00:00
|
|
|
|
2021-01-20 12:12:59 +00:00
|
|
|
// Note: Image is created separately using Zotero.Annotations.saveCacheImage()
|
2020-06-20 05:29:32 +00:00
|
|
|
|
2020-09-04 23:39:19 +00:00
|
|
|
assert.equal(annotation.key, exampleImage.key);
|
2020-06-20 05:29:32 +00:00
|
|
|
for (let prop of ['comment', 'color', 'pageLabel', 'sortIndex', 'position']) {
|
|
|
|
let itemProp = 'annotation' + prop[0].toUpperCase() + prop.substr(1);
|
2020-09-10 10:13:51 +00:00
|
|
|
assert.deepEqual(annotation[itemProp], exampleImageAlt[prop], `'${prop}' doesn't match`);
|
2020-06-20 05:29:32 +00:00
|
|
|
}
|
|
|
|
});
|
2020-12-26 07:33:40 +00:00
|
|
|
|
|
|
|
it("should remove empty fields", async function () {
|
|
|
|
var annotation = await Zotero.Annotations.saveFromJSON(attachment, exampleHighlight);
|
|
|
|
var json = Object.assign({}, exampleHighlight);
|
|
|
|
json.comment = '';
|
|
|
|
json.pageLabel = '';
|
|
|
|
await Zotero.Annotations.saveFromJSON(attachment, json);
|
|
|
|
|
2021-01-26 07:42:51 +00:00
|
|
|
assert.isNull(annotation.annotationComment);
|
|
|
|
assert.isNull(annotation.annotationPageLabel);
|
2020-12-26 07:33:40 +00:00
|
|
|
});
|
2020-06-20 05:29:32 +00:00
|
|
|
});
|
|
|
|
})
|