From 2c97a7a03e2e1090e9942041e5797a0b41470139 Mon Sep 17 00:00:00 2001 From: Bogdan Abaev Date: Fri, 12 Jan 2024 16:27:45 -0500 Subject: [PATCH] avoid blinking of focused collection row on escape Make sure that on escape, the focus is moved to collectionTree strictly once all filtered rows are rendered. Minor refactoring to achieve it, since setFilter is async. --- chrome/content/zotero/collectionTree.jsx | 7 ++++++- chrome/content/zotero/zoteroPane.js | 20 +++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/chrome/content/zotero/collectionTree.jsx b/chrome/content/zotero/collectionTree.jsx index df4cafff14..e9d517db76 100644 --- a/chrome/content/zotero/collectionTree.jsx +++ b/chrome/content/zotero/collectionTree.jsx @@ -2388,8 +2388,9 @@ var CollectionTree = class CollectionTree extends LibraryTree { * on the currently selected row if any. If the filter is emptied out, the currently selected * row is scrolled to. Otherwise, scroll to the very top. * @param {String} filterText - Text that rows have to contain to match the filter + * @param {Bool} focusTree - Focus the collection tree after the filtering is complete */ - async setFilter(filterText) { + async setFilter(filterText, focusTree = false) { let collectionTable = document.getElementById("collection-tree").firstElementChild; let isEmpty = this._isFilterEmpty(); let willBeEmpty = filterText.length == 0; @@ -2461,6 +2462,10 @@ var CollectionTree = class CollectionTree extends LibraryTree { let scrollPosition = collectionTable.scrollTop + rowMiddle - tableMiddle; collectionTable.scrollTop = scrollPosition; } + // Focus the collection tree + if (focusTree) { + collectionTable.parentNode.focus(); + } } diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 867c61af12..1e287f836c 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -292,18 +292,18 @@ var ZoteroPane = new function() }); let collectionsSearchField = document.getElementById("zotero-collections-search"); - let clearCollectionSearch = (removeFocus) => { - // Clear the search field + let clearCollectionSearch = () => { + // If empty filter - just focus the collectionTree + if (collectionsSearchField.value.length == 0) { + return document.getElementById("collection-tree"); + } + // Clear the search field and focus collection tree if (collectionsSearchField.value.length) { collectionsSearchField.value = ''; - ZoteroPane.collectionsView.setFilter(""); - if (!removeFocus) { - return null; - } + ZoteroPane.collectionsView.setFilter("", true); } ZoteroPane.hideCollectionSearch(); - // If the search field is empty, focus the collection tree - return document.getElementById('collection-tree'); + return null; }; collectionTreeToolbar.addEventListener("keydown", (event) => { let actionsMap = { @@ -404,9 +404,7 @@ var ZoteroPane = new function() // default to focusing on tag selector return false; }, - Escape: () => { - clearCollectionSearch(true); - } + Escape: clearCollectionSearch } }; moveFocus(actionsMap, event);