Rename subsequent attachments after their file base names (#4363)
This commit is contained in:
parent
13496afeb3
commit
327206f931
6 changed files with 31 additions and 20 deletions
|
@ -2498,7 +2498,7 @@ var ItemTree = class ItemTree extends LibraryTree {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item) {
|
if (item) {
|
||||||
item.setFirstAttachmentTitle();
|
item.setAutoAttachmentTitle();
|
||||||
await item.saveTx();
|
await item.saveTx();
|
||||||
addedItems.push(item);
|
addedItems.push(item);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3877,23 +3877,34 @@ Zotero.Item.prototype._getDefaultTitleForAttachmentContentType = function () {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Zotero.Item.prototype.setFirstAttachmentTitle = function () {
|
Zotero.Item.prototype.setAutoAttachmentTitle = function () {
|
||||||
if (!this.isAttachment()) {
|
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) {
|
if (!this.isFileAttachment() || !this.parentItemID) {
|
||||||
return;
|
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;
|
let isFirstOfType = this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1;
|
||||||
if (!isFirstOfType) {
|
if (isFirstOfType) {
|
||||||
return;
|
let defaultTitle = this._getDefaultTitleForAttachmentContentType();
|
||||||
|
if (defaultTitle !== null) {
|
||||||
|
this.setField('title', defaultTitle);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let defaultTitle = this._getDefaultTitleForAttachmentContentType();
|
|
||||||
if (defaultTitle === null) {
|
// If this isn't the only attachment of its type or we don't have a default
|
||||||
// Keep existing title
|
// title for this type, name it after its filename, minus the extension
|
||||||
return;
|
let filename = this.attachmentFilename;
|
||||||
|
if (filename) {
|
||||||
|
let title = filename.replace(/\.[^.]+$/, '');
|
||||||
|
if (title) {
|
||||||
|
this.setField('title', title);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.setField('title', defaultTitle);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -303,7 +303,7 @@ Zotero.RecognizeDocument = new function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename attachment title
|
// Rename attachment title
|
||||||
attachment.setFirstAttachmentTitle();
|
attachment.setAutoAttachmentTitle();
|
||||||
await attachment.saveTx();
|
await attachment.saveTx();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -4417,7 +4417,7 @@ var ZoteroPane = new function()
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
item.setFirstAttachmentTitle();
|
item.setAutoAttachmentTitle();
|
||||||
await item.saveTx();
|
await item.saveTx();
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
@ -5349,7 +5349,7 @@ var ZoteroPane = new function()
|
||||||
|
|
||||||
await Zotero.DB.executeTransaction(async () => {
|
await Zotero.DB.executeTransaction(async () => {
|
||||||
for (let item of items) {
|
for (let item of items) {
|
||||||
item.setFirstAttachmentTitle();
|
item.setAutoAttachmentTitle();
|
||||||
await item.save();
|
await item.save();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1570,7 +1570,7 @@ describe("Zotero.ItemTree", function() {
|
||||||
let promise = waitForItemEvent('add');
|
let promise = waitForItemEvent('add');
|
||||||
drop(parentRow, 0, dataTransfer);
|
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]);
|
let pdfAttachment1 = Zotero.Items.get((await promise)[0]);
|
||||||
assert.equal(pdfAttachment1.parentItemID, parentItem.id);
|
assert.equal(pdfAttachment1.parentItemID, parentItem.id);
|
||||||
assert.equal(pdfAttachment1.getField('title'), Zotero.getString('fileTypes.pdf'));
|
assert.equal(pdfAttachment1.getField('title'), Zotero.getString('fileTypes.pdf'));
|
||||||
|
@ -1578,10 +1578,10 @@ describe("Zotero.ItemTree", function() {
|
||||||
promise = waitForItemEvent('add');
|
promise = waitForItemEvent('add');
|
||||||
drop(parentRow, 0, dataTransfer);
|
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]);
|
let pdfAttachment2 = Zotero.Items.get((await promise)[0]);
|
||||||
assert.equal(pdfAttachment2.parentItemID, parentItem.id);
|
assert.equal(pdfAttachment2.parentItemID, parentItem.id);
|
||||||
assert.equal(pdfAttachment2.getField('title'), 'test.pdf');
|
assert.equal(pdfAttachment2.getField('title'), 'test');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1660,19 +1660,19 @@ describe("ZoteroPane", function() {
|
||||||
parentItemID: parentItem.id,
|
parentItemID: parentItem.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add a PDF attachment, which will be renamed
|
// Add a PDF attachment, which will get a default title
|
||||||
let file = getTestDataDirectory();
|
let file = getTestDataDirectory();
|
||||||
file.append('test.pdf');
|
file.append('test.pdf');
|
||||||
let [pdfAttachment1] = await zp.addAttachmentFromDialog(false, parentItem.id, [file.path]);
|
let [pdfAttachment1] = await zp.addAttachmentFromDialog(false, parentItem.id, [file.path]);
|
||||||
assert.equal(parentItem.getAttachments().length, 2);
|
assert.equal(parentItem.getAttachments().length, 2);
|
||||||
assert.equal(pdfAttachment1.getField('title'), Zotero.getString('fileTypes.pdf'));
|
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]);
|
let [pdfAttachment2] = await zp.addAttachmentFromDialog(false, parentItem.id, [file.path]);
|
||||||
assert.equal(parentItem.getAttachments().length, 3);
|
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 = getTestDataDirectory();
|
||||||
file.append('stub.epub');
|
file.append('stub.epub');
|
||||||
let [epubAttachment] = await zp.addAttachmentFromDialog(false, parentItem.id, [file.path]);
|
let [epubAttachment] = await zp.addAttachmentFromDialog(false, parentItem.id, [file.path]);
|
||||||
|
|
Loading…
Reference in a new issue