Set auto attachment title when not renaming (#5748)

This commit is contained in:
Abe Jellinek 2026-01-27 11:44:50 -05:00 • committed by GitHub
parent cc53bc1699
commit 8b446b7fc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 15 additions and 18 deletions

View file

@ -146,7 +146,7 @@ export async function renameFilesFromParent({ userLibrary = true, groupLibrary =
// update the title if it matches the old filename
const newTitleLC = attachmentItem.getField('title').toLowerCase();
if (newTitleLC === oldBaseName.toLowerCase() || newTitleLC === oldFileName.toLowerCase()) {
attachmentItem.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true });
attachmentItem.setAutoAttachmentTitle();
}
await attachmentItem.saveTx();
@ -196,7 +196,7 @@ export async function renameFileFromParent(attachmentItem) {
const newTitleLC = attachmentItem.getField('title').toLowerCase();
if (newTitleLC === oldBaseName.toLowerCase() || newTitleLC === oldName.toLowerCase()) {
attachmentItem.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true });
attachmentItem.setAutoAttachmentTitle();
requiresSave = true;
}

View file

@ -4014,7 +4014,7 @@ Zotero.Item.prototype._getDefaultTitleForAttachmentContentType = function () {
};
Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs, forceFirstOfType } = {}) {
Zotero.Item.prototype.setAutoAttachmentTitle = function ({ forceFirstOfType } = {}) {
if (!this.isAttachment()) {
throw new Error("setAutoAttachmentTitle() can only be called on attachment items");
}
@ -4030,8 +4030,7 @@ Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs
.every(item => item === this)
)
: forceFirstOfType;
let isBeingRenamed = ignoreAutoRenamePrefs || Zotero.Attachments.shouldAutoRenameAttachment(this);
if (isFirstOfType && isBeingRenamed) {
if (isFirstOfType) {
let defaultTitle = this._getDefaultTitleForAttachmentContentType();
if (defaultTitle !== null) {
this.setField('title', defaultTitle);

View file

@ -305,7 +305,7 @@ Zotero.RecognizeDocument = new function () {
if (result !== true) {
throw new Error("Error renaming " + path);
}
attachment.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true });
attachment.setAutoAttachmentTitle();
await attachment.saveTx();
}

View file

@ -5783,7 +5783,7 @@ var ZoteroPane = new function () {
if (result !== true) {
throw new Error('Error renaming ' + path);
}
item.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true });
item.setAutoAttachmentTitle();
await item.saveTx();
}
}

View file

@ -103,7 +103,7 @@ describe("Zotero.Attachments", function () {
await attachment.eraseTx();
});
it("should set a child item's title to the filename, minus its extension", async function () {
it("should set a child item's title based on its type", async function () {
let file = getTestDataDirectory();
file.append('test.pdf');
let parent = await createDataObject('item');
@ -144,7 +144,7 @@ describe("Zotero.Attachments", function () {
await attachment.eraseTx();
});
it("should set a child item's title to the filename, minus its extension", async function () {
it("should set a child item's title based on its type", async function () {
let file = getTestDataDirectory();
file.append('test.pdf');
let parent = await createDataObject('item');
@ -152,7 +152,7 @@ describe("Zotero.Attachments", function () {
file: file,
parentItemID: parent.id,
});
assert.equal(attachment.getField('title'), 'test');
assert.equal(attachment.getField('title'), Zotero.getString('file-type-pdf'));
await parent.eraseTx();
});
})
@ -2309,17 +2309,15 @@ describe("Zotero.Attachments", function () {
var attachment = await importFileAttachment('test.png', {
parentItemID: item.id,
// Use default setAutoAttachmentTitle() behavior -- the file isn't going to be
// renamed because autoRenameFiles.fileTypes doesn't match image/, so the title
// becomes the filename minus extension, i.e., "test"
title: null
// Pretend this was, e.g., a secondary attachment before, and it got titled
// after the file basename
title: 'test'
});
assert.equal(attachment.getField('title'), 'test');
await renameFileFromParent(attachment);
assert.equal(attachment.attachmentFilename, 'Title.png');
// After a manual rename, the title becomes the default for this type
assert.equal(attachment.getField('title'), Zotero.getString('file-type-image'));
});
it("should change attachment title if file basename matches the title", async function () {

View file

@ -108,7 +108,7 @@ describe("Document Recognition", function () {
assert.lengthOf(addedIDs, 2);
var item = Zotero.Items.get(addedIDs[0]);
assert.equal(item.getField("title"), "Scaling study of an improved fermion action on quenched lattices");
assert.lengthOf(modifiedIDs, 1);
assert.lengthOf(modifiedIDs, 2);
await waitForProgressWindow();
@ -139,7 +139,7 @@ describe("Document Recognition", function () {
// Item and note
assert.lengthOf(addedIDs, 2);
var item = Zotero.Items.get(addedIDs[0]);
assert.lengthOf(modifiedIDs, 1);
assert.lengthOf(modifiedIDs, 2);
await waitForProgressWindow();
@ -167,7 +167,7 @@ describe("Document Recognition", function () {
// Item and note
assert.lengthOf(addedIDs, 2);
var item = Zotero.Items.get(addedIDs[0]);
assert.lengthOf(modifiedIDs, 1);
assert.lengthOf(modifiedIDs, 2);
await waitForProgressWindow();