From 87cf336e224aa9530fd632aad3ba3403c6239fe4 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 8 Jan 2018 01:05:28 -0500 Subject: [PATCH] Cancel active Quick Format search if another one starts This fixes inconsistent results (#1349) from async searches finishing out of order. Also adds spinner when search is active --- .../content/zotero/integration/quickFormat.js | 21 +++++++++++++++---- .../zotero/integration/quickFormat.xul | 3 +++ chrome/content/zotero/zoteroPane.xul | 2 +- chrome/skin/default/zotero/integration.css | 5 +++++ chrome/skin/default/zotero/zotero.css | 12 +++++++++-- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/chrome/content/zotero/integration/quickFormat.js b/chrome/content/zotero/integration/quickFormat.js index 5aa1d4c5fe..3b247ba24b 100644 --- a/chrome/content/zotero/integration/quickFormat.js +++ b/chrome/content/zotero/integration/quickFormat.js @@ -37,9 +37,10 @@ var Zotero_QuickFormat = new function () { keepSorted, showEditor, referencePanel, referenceBox, referenceHeight = 0, separatorHeight = 0, currentLocator, currentLocatorLabel, currentSearchTime, dragging, panel, panelPrefix, panelSuffix, panelSuppressAuthor, panelLocatorLabel, panelLocator, - panelLibraryLink, panelInfo, panelRefersToBubble, panelFrameHeight = 0, accepted = false, - searchTimeout; + panelLibraryLink, panelInfo, panelRefersToBubble, panelFrameHeight = 0, accepted = false; + var _searchPromise; + const SEARCH_TIMEOUT = 250; const SHOWN_REFERENCES = 7; /** @@ -1051,8 +1052,20 @@ var Zotero_QuickFormat = new function () { * keypress, since searches can be slow. */ function _resetSearchTimer() { - if(searchTimeout) clearTimeout(searchTimeout); - searchTimeout = setTimeout(_quickFormat, 250); + // Show spinner + var spinner = document.getElementById('quick-format-spinner'); + spinner.style.visibility = ''; + // Cancel current search if active + if (_searchPromise && _searchPromise.isPending()) { + _searchPromise.cancel(); + } + // Start new search + _searchPromise = Zotero.Promise.delay(SEARCH_TIMEOUT) + .then(() => _quickFormat()) + .then(() => { + _searchPromise = null; + spinner.style.visibility = 'hidden'; + }); } /** diff --git a/chrome/content/zotero/integration/quickFormat.xul b/chrome/content/zotero/integration/quickFormat.xul index 594663baec..3419f7bb8f 100644 --- a/chrome/content/zotero/integration/quickFormat.xul +++ b/chrome/content/zotero/integration/quickFormat.xul @@ -62,6 +62,9 @@