Rename subsequent attachments after their file base names (#4363)

This commit is contained in:
Abe Jellinek 2024-07-09 11:51:25 -04:00 committed by Dan Stillman
parent 13496afeb3
commit 327206f931
6 changed files with 31 additions and 20 deletions

View file

@ -2498,7 +2498,7 @@ var ItemTree = class ItemTree extends LibraryTree {
}
if (item) {
item.setFirstAttachmentTitle();
item.setAutoAttachmentTitle();
await item.saveTx();
addedItems.push(item);
}

View file

@ -3877,23 +3877,34 @@ Zotero.Item.prototype._getDefaultTitleForAttachmentContentType = function () {
};
Zotero.Item.prototype.setFirstAttachmentTitle = function () {
Zotero.Item.prototype.setAutoAttachmentTitle = function () {
if (!this.isAttachment()) {
throw new Error("setFirstAttachmentTitle() can only be called on attachment items");
throw new Error("setAutoAttachmentTitle() can only be called on attachment items");
}
if (!this.isFileAttachment() || !this.parentItemID) {
return;
}
// If this is the only attachment of its type on the parent item, give it
// a default title ("PDF", "Webpage", etc.)
let isFirstOfType = this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1;
if (!isFirstOfType) {
return;
if (isFirstOfType) {
let defaultTitle = this._getDefaultTitleForAttachmentContentType();
if (defaultTitle !== null) {
this.setField('title', defaultTitle);
return;
}
}
let defaultTitle = this._getDefaultTitleForAttachmentContentType();
if (defaultTitle === null) {
// Keep existing title
return;
// If this isn't the only attachment of its type or we don't have a default
// title for this type, name it after its filename, minus the extension
let filename = this.attachmentFilename;
if (filename) {
let title = filename.replace(/\.[^.]+$/, '');
if (title) {
this.setField('title', title);
}
}
this.setField('title', defaultTitle);
};

View file

@ -303,7 +303,7 @@ Zotero.RecognizeDocument = new function () {
}
// Rename attachment title
attachment.setFirstAttachmentTitle();
attachment.setAutoAttachmentTitle();
await attachment.saveTx();
try {

View file

@ -4417,7 +4417,7 @@ var ZoteroPane = new function()
}
try {
item.setFirstAttachmentTitle();
item.setAutoAttachmentTitle();
await item.saveTx();
}
catch (e) {
@ -5349,7 +5349,7 @@ var ZoteroPane = new function()
await Zotero.DB.executeTransaction(async () => {
for (let item of items) {
item.setFirstAttachmentTitle();
item.setAutoAttachmentTitle();
await item.save();
}
});

View file

@ -1570,7 +1570,7 @@ describe("Zotero.ItemTree", function() {
let promise = waitForItemEvent('add');
drop(parentRow, 0, dataTransfer);
// Add a PDF attachment, which will be renamed
// Add a PDF attachment, which will get a default title
let pdfAttachment1 = Zotero.Items.get((await promise)[0]);
assert.equal(pdfAttachment1.parentItemID, parentItem.id);
assert.equal(pdfAttachment1.getField('title'), Zotero.getString('fileTypes.pdf'));
@ -1578,10 +1578,10 @@ describe("Zotero.ItemTree", function() {
promise = waitForItemEvent('add');
drop(parentRow, 0, dataTransfer);
// Add a second, which won't
// Add a second, which will get a title based on its filename
let pdfAttachment2 = Zotero.Items.get((await promise)[0]);
assert.equal(pdfAttachment2.parentItemID, parentItem.id);
assert.equal(pdfAttachment2.getField('title'), 'test.pdf');
assert.equal(pdfAttachment2.getField('title'), 'test');
});
});

View file

@ -1660,19 +1660,19 @@ describe("ZoteroPane", function() {
parentItemID: parentItem.id,
});
// Add a PDF attachment, which will be renamed
// Add a PDF attachment, which will get a default title
let file = getTestDataDirectory();
file.append('test.pdf');
let [pdfAttachment1] = await zp.addAttachmentFromDialog(false, parentItem.id, [file.path]);
assert.equal(parentItem.getAttachments().length, 2);
assert.equal(pdfAttachment1.getField('title'), Zotero.getString('fileTypes.pdf'));
// Add a second, which won't
// Add a second, which will get a title based on its filename
let [pdfAttachment2] = await zp.addAttachmentFromDialog(false, parentItem.id, [file.path]);
assert.equal(parentItem.getAttachments().length, 3);
assert.equal(pdfAttachment2.getField('title'), 'test.pdf');
assert.equal(pdfAttachment2.getField('title'), 'test');
// Add an EPUB attachment, which will be renamed
// Add an EPUB attachment, which will get a default title
file = getTestDataDirectory();
file.append('stub.epub');
let [epubAttachment] = await zp.addAttachmentFromDialog(false, parentItem.id, [file.path]);