Improve file renaming preview in preferences (#3343)

This commit is contained in:
Tom Najdek 2023-08-19 11:06:08 +02:00 committed by GitHub
parent 4dbd52782c
commit 4f19b1d13e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -26,6 +26,7 @@
Zotero_Preferences.FileRenaming = {
mockItem: null,
defaultExt: 'pdf',
init: function () {
this.inputRef = document.getElementById('file-renaming-format-template');
this.updatePreview();
@ -33,11 +34,27 @@ Zotero_Preferences.FileRenaming = {
Zotero.getActiveZoteroPane()?.itemsView.onSelect.addListener(this.updatePreview.bind(this));
},
getActiveTopLevelItem() {
const selectedItem = Zotero.getActiveZoteroPane()?.getSelectedItems()?.[0];
if (selectedItem) {
if (selectedItem.isRegularItem() && !selectedItem.parentKey) {
return [selectedItem, this.defaultExt];
}
if (selectedItem.isFileAttachment() && selectedItem.parentKey) {
const path = selectedItem.getFilePath();
const ext = Zotero.File.getExtension(Zotero.File.pathToFile(path));
return [Zotero.Items.getByLibraryAndKey(selectedItem.libraryID, selectedItem.parentKey), ext ?? this.defaultExt];
}
}
return null;
},
updatePreview() {
const item = Zotero.getActiveZoteroPane()?.getSelectedItems()?.[0] ?? this.mockItem ?? this.makeMockItem();
const [item, ext] = this.getActiveTopLevelItem() ?? [this.mockItem ?? this.makeMockItem(), this.defaultExt];
const tpl = document.getElementById('file-renaming-format-template').value;
const preview = Zotero.Attachments.getFileBaseNameFromItem(item, tpl);
document.getElementById('file-renaming-format-preview').innerText = `${preview}.pdf`;
document.getElementById('file-renaming-format-preview').innerText = `${preview}.${ext}`;
},
makeMockItem() {