From 130a72af1bd5737c754e321d818bae47f23736bf Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Wed, 21 Feb 2024 06:42:59 -0500 Subject: [PATCH] Fix empty Quick Copy locale dropdown (without a freeze) (#3720) --- .../content/zotero/preferences/preferences.js | 32 +++++++++++++++---- .../zotero/preferences/preferences_export.jsx | 7 ++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/chrome/content/zotero/preferences/preferences.js b/chrome/content/zotero/preferences/preferences.js index 45c3f239fe..2ff21cc82f 100644 --- a/chrome/content/zotero/preferences/preferences.js +++ b/chrome/content/zotero/preferences/preferences.js @@ -31,6 +31,8 @@ var Zotero_Preferences = { _firstPaneLoadDeferred: Zotero.Promise.defer(), _observerSymbols: new Map(), + + _mutationObservers: new Map(), init: function () { this.navigation = document.getElementById('prefs-navigation'); @@ -442,14 +444,29 @@ ${str} this._observerSymbols.set(elem, symbol); if (elem.tagName === 'menulist') { - // Set up an observer to resync if this menulist has items added/removed later + // Set up an observer to resync if this menulist has items added later // (If we set elem.value before the corresponding item is added, the label won't be updated when it // does get added, unless we do this) - new MutationObserver(() => this._syncFromPref(elem, preference)) - .observe(elem, { - childList: true, - subtree: true - }); + let mutationObserver = new MutationObserver((mutations) => { + let value = Zotero.Prefs.get(preference, true); + for (let mutation of mutations) { + for (let node of mutation.addedNodes) { + if (node.tagName === 'menuitem' && node.value === value) { + Zotero.debug(`Preferences: menulist attached to ${preference} has new item matching current pref value '${value}'`); + // Set selectedItem so the menulist updates its label, icon, and description + // The selectedItem setter fires select and ValueChange, but we don't listen to either + // of those events + elem.selectedItem = node; + return; + } + } + } + }); + mutationObserver.observe(elem, { + childList: true, + subtree: true + }); + this._mutationObservers.set(elem, mutationObserver); } elem.addEventListener('command', this._syncToPrefOnModify.bind(this)); @@ -468,6 +485,9 @@ ${str} Zotero.Prefs.unregisterObserver(this._observerSymbols.get(elem)); this._observerSymbols.delete(elem); } + if (this._mutationObservers.has(elem)) { + this._mutationObservers.get(elem).disconnect(); + } }; let awaitBeforeShowing = []; diff --git a/chrome/content/zotero/preferences/preferences_export.jsx b/chrome/content/zotero/preferences/preferences_export.jsx index 2a6db92ebf..a76e13aa90 100644 --- a/chrome/content/zotero/preferences/preferences_export.jsx +++ b/chrome/content/zotero/preferences/preferences_export.jsx @@ -69,11 +69,12 @@ Zotero_Preferences.Export = { // Initialize locale drop-down var localeMenulist = document.getElementById("zotero-quickCopy-locale-menu"); Zotero.Styles.populateLocaleList(localeMenulist); + localeMenulist.addEventListener('syncfrompreference', () => { + this._lastSelectedLocale = Zotero.Prefs.get("export.quickCopy.locale"); + this.updateQuickCopyUI(); + }); localeMenulist.setAttribute('preference', "extensions.zotero.export.quickCopy.locale"); - this._lastSelectedLocale = Zotero.Prefs.get("export.quickCopy.locale"); - this.updateQuickCopyUI(); - yield this.refreshQuickCopySiteList(); }),