Reset file renaming preference if input left empty (#4462)

This commit is contained in:
Tom Najdek 2024-07-30 23:02:01 +02:00
parent 1d5971c48f
commit a538a5588e
No known key found for this signature in database
GPG key ID: EEC61A7B4C667D77

View file

@ -31,6 +31,7 @@ Zotero_Preferences.FileRenaming = {
this.inputRef = document.getElementById('file-renaming-format-template');
this.updatePreview();
this.inputRef.addEventListener('input', this.updatePreview.bind(this));
this.inputRef.addEventListener('blur', this.handleInputBlur.bind(this));
this._itemsView = Zotero.getActiveZoteroPane()?.itemsView;
this._updatePreview = this.updatePreview.bind(this);
@ -60,13 +61,22 @@ Zotero_Preferences.FileRenaming = {
return null;
},
async updatePreview() {
updatePreview() {
const [item, ext, attachmentTitle] = this.getActiveItem() ?? [this.mockItem ?? this.makeMockItem(), this.defaultExt, ''];
const formatString = document.getElementById('file-renaming-format-template').value;
const formatString = this.inputRef.value;
const preview = Zotero.Attachments.getFileBaseNameFromItem(item, { formatString, attachmentTitle });
document.getElementById('file-renaming-format-preview').innerText = `${preview}.${ext}`;
},
handleInputBlur() {
const formatString = this.inputRef.value;
const prefKey = this.inputRef.getAttribute('preference');
if (formatString.replace(/\s/g, '') === '') {
Zotero.Prefs.clear(prefKey, true);
this.updatePreview();
}
},
makeMockItem() {
this.mockItem = new Zotero.Item('journalArticle');
this.mockItem.setField('title', 'Example Title: Example Subtitle');