Tweaked recent search changes
1. Compare element indices rather than ids (since these don't necessarily correlate) 2. Restored previous behaior when new search text reduces the number of results past the currently-selected element.
Brian Vaughn committed
Jul 27, 2019 at 09:15 UTC
ca4aac5014a8e22d65b94ca7f2546a6c336ac741
1 file changed
+24
-10
src/devtools/views/Components/TreeContext.js
+24
-10
@@ -277,6 +277,7 @@ function reduceSearchState(store: Store, state: State, action: Action): State {
277
selectedElementIndex,
278
} = state;
279
280
+ const prevSearchIndex = searchIndex;
281
const prevSearchText = searchText;
282
const numPrevSearchResults = searchResults.length;
283
@@ -380,10 +381,21 @@ function reduceSearchState(store: Store, state: State, action: Action): State {
381
recursivelySearchTree(store, rootID, regExp, searchResults);
382
});
383
if (searchResults.length > 0) {
383
- if (selectedElementID !== null) {
384
- searchIndex = getNearestResult(searchResults, selectedElementID);
384
+ if (prevSearchIndex === null) {
385
+ if (selectedElementIndex !== null) {
386
+ searchIndex = getNearestResultIndex(
387
+ store,
388
+ searchResults,
389
+ selectedElementIndex
390
+ );
391
+ } else {
392
+ searchIndex = 0;
393
+ }
394
} else {
386
- searchIndex = 0;
395
+ searchIndex = Math.min(
396
+ ((prevSearchIndex: any): number),
397
+ searchResults.length - 1
398
+ );
399
}
400
}
401
}
@@ -774,15 +786,17 @@ function recursivelySearchTree(
786
);
787
}
788
777
-function getNearestResult(
789
+function getNearestResultIndex(
790
+ store: Store,
791
searchResults: Array<number>,
779
- selectedElementID: number | null
780
-) {
781
- const result = searchResults.findIndex(
782
- value => value >= ((selectedElementID: any): number)
783
- );
792
+ selectedElementIndex: number
793
+): number {
794
+ const index = searchResults.findIndex(id => {
795
+ const index = store.getIndexOfElementID(id);
796
+ return index !== null && index >= selectedElementIndex;
797
+ });
798
785
- return result === -1 ? 0 : result;
799
+ return index === -1 ? 0 : index;
800
}
801
802
export { TreeDispatcherContext, TreeStateContext, TreeContextController };