Add Book -> Book Section and Book Section -> Book to context menu (#2755)

This commit is contained in:
Abe Jellinek 2022-08-11 16:40:27 -04:00 committed by GitHub
parent 864fd03c87
commit d4a7de2e8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 0 deletions

View file

@ -2298,6 +2298,18 @@
</method>
<method name="focusField">
<parameter name="fieldName"/>
<body>
<![CDATA[
let field = document.getAnonymousNodes(this)[0].querySelector(`[fieldname="${fieldName}"][ztabindex]`);
if (!field) return false;
return this._focusNextField(field.getAttribute('ztabindex'));
]]>
</body>
</method>
<!--
Advance the field focus forward or backward

View file

@ -1808,6 +1808,55 @@ var ZoteroPane = new function()
});
this.duplicateAndConvertSelectedItem = async function () {
if (this.getSelectedItems().length != 1
|| !['book', 'bookSection'].includes(this.getSelectedItems()[0].itemType)) {
throw new Error('duplicateAndConvertSelectedItem requires a single book or bookSection to be selected');
}
let authorCreatorType = Zotero.CreatorTypes.getID('author');
let bookAuthorCreatorType = Zotero.CreatorTypes.getID('bookAuthor');
let original = this.getSelectedItems()[0];
let duplicate = await this.duplicateSelectedItem();
if (!duplicate) return null;
if (duplicate.itemType == 'book') {
duplicate.setType(Zotero.ItemTypes.getID('bookSection'));
for (let i = 0; i < duplicate.numCreators(); i++) {
let creator = duplicate.getCreator(i);
if (creator.creatorTypeID == authorCreatorType) {
creator.creatorTypeID = bookAuthorCreatorType;
}
duplicate.setCreator(i, creator);
}
}
else {
duplicate.setField('title', false); // So bookTitle becomes title
duplicate.setType(Zotero.ItemTypes.getID('book'));
// Get creators from the original item because setType() will have changed the types
let creators = original.getCreators()
// Remove authors of the individual book section
.filter(creator => creator.creatorTypeID !== authorCreatorType);
for (let creator of creators) {
if (creator.creatorTypeID == bookAuthorCreatorType) {
creator.creatorTypeID = authorCreatorType;
}
}
duplicate.setCreators(creators);
}
duplicate.addRelatedItem(original);
original.addRelatedItem(duplicate);
await original.saveTx({ skipDateModifiedUpdate: true });
await duplicate.saveTx();
document.getElementById('zotero-editpane-item-box').focusField('title');
return duplicate;
};
/**
* Return whether every selected item can be deleted from the current
* collection context (library, trash, collection, etc.).
@ -3093,6 +3142,7 @@ var ZoteroPane = new function()
'toggleRead',
'addToCollection',
'removeItems',
'duplicateAndConvert',
'duplicateItem',
'restoreToLibrary',
'moveToTrash',
@ -3372,6 +3422,12 @@ var ZoteroPane = new function()
}
}
else if (!collectionTreeRow.isPublications()) {
if (item.itemType == 'book' || item.itemType == 'bookSection') {
menu.childNodes[m.duplicateAndConvert].setAttribute('label', Zotero.getString('pane.items.menu.duplicateAndConvert.'
+ (item.itemType == 'book' ? 'toBookSection' : 'toBook')));
show.add(m.duplicateAndConvert);
}
show.add(m.duplicateItem);
}
}

View file

@ -317,6 +317,7 @@
</menupopup>
</menu>
<menuitem class="menuitem-iconic zotero-menuitem-remove-items" oncommand="ZoteroPane_Local.deleteSelectedItems();"/>
<menuitem class="menuitem-iconic zotero-menuitem-duplicate-and-convert" oncommand="ZoteroPane_Local.duplicateAndConvertSelectedItem();"/>
<menuitem class="menuitem-iconic zotero-menuitem-duplicate-item" label="&zotero.items.menu.duplicateItem;" oncommand="ZoteroPane_Local.duplicateSelectedItem().done();"/>
<menuitem class="menuitem-iconic zotero-menuitem-restore-to-library" label="&zotero.items.menu.restoreToLibrary;" oncommand="ZoteroPane_Local.restoreSelectedItems();"/>
<menuitem class="menuitem-iconic zotero-menuitem-move-to-trash" oncommand="ZoteroPane_Local.deleteSelectedItems(true, true);"/>

View file

@ -361,6 +361,8 @@ pane.items.menu.createParent = Create Parent Item…
pane.items.menu.createParent.multiple = Create Parent Items
pane.items.menu.renameAttachments = Rename File from Parent Metadata
pane.items.menu.renameAttachments.multiple = Rename Files from Parent Metadata
pane.items.menu.duplicateAndConvert.toBookSection = Create Book Section
pane.items.menu.duplicateAndConvert.toBook = Create Book from Book Section
pane.items.showItemInLibrary = Show Item in Library
pane.items.letter.oneParticipant = Letter to %S