Don't set default attachment title if not renaming file (#4459)

Except from Rename File from Parent Metadata.
This commit is contained in:
Abe Jellinek 2024-07-31 01:39:25 -04:00 committed by GitHub
parent 06f359df23
commit dd1601793c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 30 additions and 13 deletions

View file

@ -3895,7 +3895,7 @@ Zotero.Item.prototype._getDefaultTitleForAttachmentContentType = function () {
}; };
Zotero.Item.prototype.setAutoAttachmentTitle = function () { Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs } = {}) {
if (!this.isAttachment()) { if (!this.isAttachment()) {
throw new Error("setAutoAttachmentTitle() can only be called on attachment items"); throw new Error("setAutoAttachmentTitle() can only be called on attachment items");
} }
@ -3903,11 +3903,14 @@ Zotero.Item.prototype.setAutoAttachmentTitle = function () {
return; return;
} }
// If this is the only attachment of its type on the parent item, give it // If this is the only attachment of its type on the parent item and the
// a default title ("PDF", "Webpage", etc.) // file is being renamed, give it a default title ("PDF", "Webpage", etc.)
let isFirstOfType = this.parentItemID let isFirstOfType = this.parentItemID
&& this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1; && this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1;
if (isFirstOfType) { let isBeingRenamed = ignoreAutoRenamePrefs
|| (Zotero.Attachments.shouldAutoRenameFile(this.isLinkedFileAttachment())
&& Zotero.Attachments.isRenameAllowedForType(this.attachmentContentType));
if (isFirstOfType && isBeingRenamed) {
let defaultTitle = this._getDefaultTitleForAttachmentContentType(); let defaultTitle = this._getDefaultTitleForAttachmentContentType();
if (defaultTitle !== null) { if (defaultTitle !== null) {
this.setField('title', defaultTitle); this.setField('title', defaultTitle);

View file

@ -5667,6 +5667,7 @@ var ZoteroPane = new function()
let parentItemID = item.parentItemID; let parentItemID = item.parentItemID;
let parentItem = await Zotero.Items.getAsync(parentItemID); let parentItem = await Zotero.Items.getAsync(parentItemID);
var oldBaseName = item.attachmentFilename.replace(/\.[^.]+$/, '');
var newName = Zotero.Attachments.getFileBaseNameFromItem(parentItem, { attachmentTitle: item.getField('title') }); var newName = Zotero.Attachments.getFileBaseNameFromItem(parentItem, { attachmentTitle: item.getField('title') });
let extRE = /\.[^\.]+$/; let extRE = /\.[^\.]+$/;
@ -5682,6 +5683,11 @@ var ZoteroPane = new function()
continue; continue;
} }
if (item.getField('title') === oldBaseName) {
item.setAutoAttachmentTitle({ ignoreAutoRenamePrefs: true });
await item.saveTx();
}
let str = await document.l10n.formatValue('file-renaming-file-renamed-to', { filename: newName }); let str = await document.l10n.formatValue('file-renaming-file-renamed-to', { filename: newName });
progressWin.addLines(str, item.getItemTypeIconName()); progressWin.addLines(str, item.getItemTypeIconName());
progressWin.show(); progressWin.show();

View file

@ -1007,7 +1007,9 @@ function importFileAttachment(filename, options = {}) {
Object.assign(importOptions, options); Object.assign(importOptions, options);
// Override default behavior - don't set title based on type, // Override default behavior - don't set title based on type,
// just use extension-less leafName // just use extension-less leafName
importOptions.title ??= file.leafName.replace(/\.[^.]+$/, ''); if (!('title' in importOptions)) {
importOptions.title = file.leafName.replace(/\.[^.]+$/, '');
}
return Zotero.Attachments.importFromFile(importOptions); return Zotero.Attachments.importFromFile(importOptions);
} }

View file

@ -152,7 +152,7 @@ describe("Zotero.Attachments", function() {
file: file, file: file,
parentItemID: parent.id, parentItemID: parent.id,
}); });
assert.equal(attachment.getField('title'), Zotero.getString('file-type-pdf')); assert.equal(attachment.getField('title'), 'test');
await parent.eraseTx(); await parent.eraseTx();
}); });
}) })

View file

@ -662,28 +662,34 @@ describe("ZoteroPane", function() {
await item.saveTx(); await item.saveTx();
var attachment = await importFileAttachment('test.png', { parentItemID: item.id }); var attachment = await importFileAttachment('test.png', { parentItemID: item.id });
attachment.setField('title', 'Image'); attachment.setField('title', 'Title');
await attachment.saveTx(); await attachment.saveTx();
await zp.selectItem(attachment.id); await zp.selectItem(attachment.id);
await zp.renameSelectedAttachmentsFromParents(); await zp.renameSelectedAttachmentsFromParents();
assert.equal(attachment.attachmentFilename, 'Title.png'); assert.equal(attachment.attachmentFilename, 'Title.png');
assert.equal(attachment.getField('title'), 'Image') assert.equal(attachment.getField('title'), 'Title');
}); });
it("should not change attachment title even if the same as filename", async function () { it("should change attachment title if previously set to the file basename by setAutoAttachmentTitle()", async function () {
var item = createUnsavedDataObject('item'); var item = createUnsavedDataObject('item');
item.setField('title', 'Title'); item.setField('title', 'Title');
await item.saveTx(); await item.saveTx();
var attachment = await importFileAttachment('test.png', { parentItemID: item.id }); var attachment = await importFileAttachment('test.png', {
attachment.setField('title', 'test.png'); parentItemID: item.id,
await attachment.saveTx(); // 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: undefined
});
assert.equal(attachment.getField('title'), 'test');
await zp.selectItem(attachment.id); await zp.selectItem(attachment.id);
await zp.renameSelectedAttachmentsFromParents(); await zp.renameSelectedAttachmentsFromParents();
assert.equal(attachment.attachmentFilename, 'Title.png'); assert.equal(attachment.attachmentFilename, 'Title.png');
assert.equal(attachment.getField('title'), 'test.png') // After a manual rename, the title becomes the default for this type
assert.equal(attachment.getField('title'), Zotero.getString('file-type-image'));
}); });
}); });