Add collectionID option to EditorInstance.createNoteFromAnnotations()

For adding a standalone note to the current collection
This commit is contained in:
Dan Stillman 2022-09-02 01:43:44 +02:00 committed by Dan Stillman
parent b24c7577ec
commit 894309d061
5 changed files with 28 additions and 11 deletions

View file

@ -497,7 +497,12 @@ var ZoteroContextPane = new function () {
if (!annotations.length) { if (!annotations.length) {
return; return;
} }
var note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, child && attachment.parentID); var note = await Zotero.EditorInstance.createNoteFromAnnotations(
annotations,
{
parentID: child && attachment.parentID
}
);
_updateAddToNote(); _updateAddToNote();

View file

@ -1212,10 +1212,12 @@ class EditorInstance {
* Create note from annotations * Create note from annotations
* *
* @param {Zotero.Item[]} annotations * @param {Zotero.Item[]} annotations
* @param {Integer} parentID Creates standalone note if not provided * @param {Object} options
* @param {Integer} options.parentID - Creates standalone note if not provided
* @param {Integer} options.collectionID - Only valid if parentID not provided
* @returns {Promise<Zotero.Item>} * @returns {Promise<Zotero.Item>}
*/ */
static async createNoteFromAnnotations(annotations, parentID) { static async createNoteFromAnnotations(annotations, { parentID, collectionID } = {}) {
if (!annotations.length) { if (!annotations.length) {
throw new Error("No annotations provided"); throw new Error("No annotations provided");
} }
@ -1236,7 +1238,12 @@ class EditorInstance {
let note = new Zotero.Item('note'); let note = new Zotero.Item('note');
note.libraryID = annotations[0].libraryID; note.libraryID = annotations[0].libraryID;
if (parentID) {
note.parentID = parentID; note.parentID = parentID;
}
else if (collectionID) {
note.addToCollection(collectionID);
}
await note.saveTx(); await note.saveTx();
let editorInstance = new EditorInstance(); let editorInstance = new EditorInstance();
editorInstance._item = note; editorInstance._item = note;

View file

@ -4962,7 +4962,12 @@ var ZoteroPane = new function()
if (!annotations.length) { if (!annotations.length) {
return; return;
} }
var note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, attachment.parentID); var note = await Zotero.EditorInstance.createNoteFromAnnotations(
annotations,
{
parentID: attachment.parentID
}
);
if (!skipSelect) { if (!skipSelect) {
await this.selectItem(note.id); await this.selectItem(note.id);
} }

View file

@ -413,7 +413,7 @@ describe("Create a note from annotations from multiple items and attachments", f
annotations.push(annotation1); annotations.push(annotation1);
let annotation2 = await createAnnotation('highlight', attachment); let annotation2 = await createAnnotation('highlight', attachment);
annotations.push(annotation2); annotations.push(annotation2);
let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, null); let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations);
assert.equal(note.note.split('test.pdf').length - 1, 1); assert.equal(note.note.split('test.pdf').length - 1, 1);
assert.equal(note.note.split(annotation1.annotationText).length - 1, 1); assert.equal(note.note.split(annotation1.annotationText).length - 1, 1);
assert.equal(note.note.split(annotation2.annotationText).length - 1, 1); assert.equal(note.note.split(annotation2.annotationText).length - 1, 1);
@ -428,7 +428,7 @@ describe("Create a note from annotations from multiple items and attachments", f
annotations.push(annotation1); annotations.push(annotation1);
let annotation2 = await createAnnotation('highlight', attachment2); let annotation2 = await createAnnotation('highlight', attachment2);
annotations.push(annotation2); annotations.push(annotation2);
let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, null); let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations);
assert.equal(note.note.split('test.pdf').length - 1, 2); assert.equal(note.note.split('test.pdf').length - 1, 2);
assert.equal(note.note.split('>' + item.getField('title') + '<').length - 1, 0); assert.equal(note.note.split('>' + item.getField('title') + '<').length - 1, 0);
assert.equal(note.note.split(annotation1.annotationText).length - 1, 1); assert.equal(note.note.split(annotation1.annotationText).length - 1, 1);
@ -445,7 +445,7 @@ describe("Create a note from annotations from multiple items and attachments", f
annotations.push(annotation1); annotations.push(annotation1);
let annotation2 = await createAnnotation('highlight', attachment2); let annotation2 = await createAnnotation('highlight', attachment2);
annotations.push(annotation2); annotations.push(annotation2);
let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, null); let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations);
assert.equal(note.note.split('test.pdf').length - 1, 0); assert.equal(note.note.split('test.pdf').length - 1, 0);
assert.equal(note.note.split('>' + item1.getField('title') + '<').length - 1, 1); assert.equal(note.note.split('>' + item1.getField('title') + '<').length - 1, 1);
assert.equal(note.note.split('>' + item2.getField('title') + '<').length - 1, 1); assert.equal(note.note.split('>' + item2.getField('title') + '<').length - 1, 1);
@ -468,7 +468,7 @@ describe("Create a note from annotations from multiple items and attachments", f
annotations.push(annotation3); annotations.push(annotation3);
let annotation4 = await createAnnotation('highlight', attachment3); let annotation4 = await createAnnotation('highlight', attachment3);
annotations.push(annotation4); annotations.push(annotation4);
let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations, null); let note = await Zotero.EditorInstance.createNoteFromAnnotations(annotations);
Zotero.debug(note.note); Zotero.debug(note.note);
assert.equal(note.note.split('test.pdf').length - 1, 2); assert.equal(note.note.split('test.pdf').length - 1, 2);
assert.equal(note.note.split('>' + item1.getField('title') + '<').length - 1, 1); assert.equal(note.note.split('>' + item1.getField('title') + '<').length - 1, 1);

View file

@ -608,7 +608,7 @@ describe("Zotero.Items", function () {
await item2.saveTx(); await item2.saveTx();
let attachment2 = await importPDFAttachment(item2); let attachment2 = await importPDFAttachment(item2);
let annotation2 = await createAnnotation('highlight', attachment2); let annotation2 = await createAnnotation('highlight', attachment2);
let annotation2Note = await Zotero.EditorInstance.createNoteFromAnnotations([annotation2], item2.id); let annotation2Note = await Zotero.EditorInstance.createNoteFromAnnotations([annotation2], { parentID: item2.id });
assert.include(annotation2Note.getNote(), attachment2.key); assert.include(annotation2Note.getNote(), attachment2.key);
@ -651,7 +651,7 @@ describe("Zotero.Items", function () {
for (let filename of attachmentFilenames) { for (let filename of attachmentFilenames) {
let attachment = await importFileAttachment(filename, { parentID: item2.id }); let attachment = await importFileAttachment(filename, { parentID: item2.id });
let annotation = await createAnnotation('highlight', attachment); let annotation = await createAnnotation('highlight', attachment);
let note = await Zotero.EditorInstance.createNoteFromAnnotations([annotation], item2.id); let note = await Zotero.EditorInstance.createNoteFromAnnotations([annotation], { parentID: item2.id });
attachments2.push(attachment); attachments2.push(attachment);
annotations2.push(annotation); annotations2.push(annotation);
notes2.push(note); notes2.push(note);