From 327206f931685490b08fd5cefbe7da8d77f7b70f Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Tue, 9 Jul 2024 11:51:25 -0400 Subject: [PATCH] Rename subsequent attachments after their file base names (#4363) --- chrome/content/zotero/itemTree.jsx | 2 +- chrome/content/zotero/xpcom/data/item.js | 29 +++++++++++++------ .../content/zotero/xpcom/recognizeDocument.js | 2 +- chrome/content/zotero/zoteroPane.js | 4 +-- test/tests/itemTreeTest.js | 6 ++-- test/tests/zoteroPaneTest.js | 8 ++--- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/chrome/content/zotero/itemTree.jsx b/chrome/content/zotero/itemTree.jsx index 03269fc63f..5577e17382 100644 --- a/chrome/content/zotero/itemTree.jsx +++ b/chrome/content/zotero/itemTree.jsx @@ -2498,7 +2498,7 @@ var ItemTree = class ItemTree extends LibraryTree { } if (item) { - item.setFirstAttachmentTitle(); + item.setAutoAttachmentTitle(); await item.saveTx(); addedItems.push(item); } diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index a9fd2db5eb..96bbd51a60 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -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); }; diff --git a/chrome/content/zotero/xpcom/recognizeDocument.js b/chrome/content/zotero/xpcom/recognizeDocument.js index 5855993b86..e6f95e4c50 100644 --- a/chrome/content/zotero/xpcom/recognizeDocument.js +++ b/chrome/content/zotero/xpcom/recognizeDocument.js @@ -303,7 +303,7 @@ Zotero.RecognizeDocument = new function () { } // Rename attachment title - attachment.setFirstAttachmentTitle(); + attachment.setAutoAttachmentTitle(); await attachment.saveTx(); try { diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 53b451cc8b..0323bd14c2 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -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(); } }); diff --git a/test/tests/itemTreeTest.js b/test/tests/itemTreeTest.js index f9a34c2fcf..f53b73d2ca 100644 --- a/test/tests/itemTreeTest.js +++ b/test/tests/itemTreeTest.js @@ -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'); }); }); diff --git a/test/tests/zoteroPaneTest.js b/test/tests/zoteroPaneTest.js index 13c43ade52..274de09b46 100644 --- a/test/tests/zoteroPaneTest.js +++ b/test/tests/zoteroPaneTest.js @@ -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]);