Improve note creation from annotations

This commit is contained in:
Martynas Bagdonas 2021-02-17 12:01:45 +02:00 committed by Dan Stillman
parent d23e0e30bb
commit 16650b4ddf
2 changed files with 14 additions and 13 deletions

View file

@ -926,27 +926,27 @@ class EditorInstance {
wrappedJSObject: io
});
}
static canCreateNoteFromAnnotations(item) {
return item.parentID && item.isAttachment() && item.attachmentContentType === 'application/pdf';
}
static async createNoteFromAnnotations(attachmentItem) {
if (!this.canCreateNoteFromAnnotations(attachmentItem)) {
return;
}
let annotations = attachmentItem.getAnnotations();
/**
* Create note from annotations
*
* @param {Zotero.Item[]} annotations
* @param {Integer} parentID Creates standalone note if not provided
* @returns {Promise<Zotero.Item|undefined>}
*/
static async createNoteFromAnnotations(annotations, parentID) {
if (!annotations.length) {
return;
}
let note = new Zotero.Item('note');
note.libraryID = attachmentItem.libraryID;
note.parentID = attachmentItem.parentID;
note.libraryID = annotations[0].libraryID;
note.parentID = parentID;
await note.saveTx();
let editorInstance = new EditorInstance();
editorInstance._item = note;
let jsonAnnotations = [];
for (let annotation of annotations) {
let attachmentItem = Zotero.Items.get(annotation.parentID);
let jsonAnnotation = await Zotero.Annotations.toJSON(annotation);
jsonAnnotation.attachmentItemID = attachmentItem.id;
jsonAnnotations.push(jsonAnnotation);
@ -955,6 +955,7 @@ class EditorInstance {
html += await editorInstance._digestAnnotations(jsonAnnotations);
note.setNote(html);
await note.saveTx();
return note;
}
}

View file

@ -2975,7 +2975,7 @@ var ZoteroPane = new function()
}
if (Zotero.EditorInstance.canCreateNoteFromAnnotations(item)) {
if (item.isPDFAttachment()) {
show.push(m.createNoteFromAnnotations);
}
}
@ -4687,7 +4687,7 @@ var ZoteroPane = new function()
}
let item = this.getSelectedItems()[0];
Zotero.EditorInstance.createNoteFromAnnotations(item);
Zotero.EditorInstance.createNoteFromAnnotations(item.getAnnotations(), item.parentID);
};
this.createEmptyParent = async function (item) {