mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Combined search and selection contexts to avoid complicated dependencies
This commit is contained in:
1 parent
9c093ac731
commit
fc924d136a
8 files changed
+444
-402
No files matched your search
@@ -4,8 +4,7 @@ import React, { Fragment, useCallback, useContext, useMemo } from 'react';
|
||||
import { ElementTypeClassOrFunction } from 'src/devtools/types';
|
||||
import { TreeContext } from './context';
|
||||
import { createRegExp } from './utils';
|
||||
import { SearchContext } from './SearchContext';
|
||||
import { SelectedElementContext } from './SelectedElementContext';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import Icon from './Icon';
|
||||
|
||||
import styles from './Element.css';
|
||||
@@ -31,15 +30,15 @@ export default function Element({ index, style }: Props) {
|
||||
|
||||
const { children, depth, displayName, id, key, type } = element;
|
||||
|
||||
const selectedElement = useContext(SelectedElementContext);
|
||||
const { selectedElementID, selectElementWithID } = useContext(
|
||||
SearchAndSelectionContext
|
||||
);
|
||||
const handleClick = useCallback(
|
||||
({ metaKey }) => {
|
||||
selectedElement.id = metaKey ? null : id;
|
||||
},
|
||||
({ metaKey }) => selectElementWithID(metaKey ? null : id),
|
||||
[id]
|
||||
);
|
||||
|
||||
const isSelected = selectedElement.id === id;
|
||||
const isSelected = selectedElementID === id;
|
||||
const showDollarR = isSelected && type === ElementTypeClassOrFunction;
|
||||
|
||||
return (
|
||||
@@ -77,16 +76,14 @@ type DisplayNameProps = {|
|
||||
|};
|
||||
|
||||
function DisplayName({ displayName, id }: DisplayNameProps) {
|
||||
const {
|
||||
currentIndex: currentSearchIndex,
|
||||
ids: searchIDs,
|
||||
text: searchText,
|
||||
} = useContext(SearchContext);
|
||||
const { searchIndex, searchResults, searchText } = useContext(
|
||||
SearchAndSelectionContext
|
||||
);
|
||||
const isSearchResult = useMemo(() => {
|
||||
return searchIDs.includes(id);
|
||||
}, [id, searchIDs]);
|
||||
return searchResults.includes(id);
|
||||
}, [id, searchResults]);
|
||||
const isCurrentResult =
|
||||
currentSearchIndex !== null && id === searchIDs[currentSearchIndex];
|
||||
searchIndex !== null && id === searchResults[searchIndex];
|
||||
|
||||
if (!isSearchResult || displayName === null) {
|
||||
return displayName;
|
||||
|
||||
@@ -5,8 +5,7 @@ import Store from '../store';
|
||||
import Tree from './Tree';
|
||||
import { BridgeContext, StoreContext, TreeContext } from './context';
|
||||
import SelectedElement from './SelectedElement';
|
||||
import { SearchController } from './SearchContext';
|
||||
import { SelectedElementController } from './SelectedElementContext';
|
||||
import { SearchAndSelectionController } from './SearchAndSelectionContext';
|
||||
import styles from './Elements.css';
|
||||
|
||||
import './root.css';
|
||||
@@ -51,18 +50,16 @@ export default function Elements({ bridge, browserName, themeName }: Props) {
|
||||
<BridgeContext.Provider value={bridge}>
|
||||
<StoreContext.Provider value={store}>
|
||||
<TreeContext.Provider value={treeContext}>
|
||||
<SelectedElementController>
|
||||
<SearchController>
|
||||
<div className={styles.Elements}>
|
||||
<div className={styles.TreeWrapper}>
|
||||
<Tree />
|
||||
</div>
|
||||
<div className={styles.SelectedElementWrapper}>
|
||||
<SelectedElement />
|
||||
</div>
|
||||
<SearchAndSelectionController>
|
||||
<div className={styles.Elements}>
|
||||
<div className={styles.TreeWrapper}>
|
||||
<Tree />
|
||||
</div>
|
||||
</SearchController>
|
||||
</SelectedElementController>
|
||||
<div className={styles.SelectedElementWrapper}>
|
||||
<SelectedElement />
|
||||
</div>
|
||||
</div>
|
||||
</SearchAndSelectionController>
|
||||
</TreeContext.Provider>
|
||||
</StoreContext.Provider>
|
||||
</BridgeContext.Provider>
|
||||
|
||||
@@ -0,0 +1,373 @@
|
||||
// @flow
|
||||
|
||||
// This context contains both the current search state and the selected element.
|
||||
// These values are combined into a single context because changes in one often impact the other.
|
||||
// Combining them enables us to avoid cascading renders.
|
||||
|
||||
import React, {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useReducer,
|
||||
} from 'react';
|
||||
import { createRegExp } from './utils';
|
||||
import { StoreContext } from './context';
|
||||
import Store from '../store';
|
||||
|
||||
import type { Element } from 'src/devtools/types';
|
||||
|
||||
type Context = {|
|
||||
// Read current context values:
|
||||
searchIndex: number | null,
|
||||
searchResults: Array<number>,
|
||||
searchText: string,
|
||||
selectedElementID: number | null,
|
||||
selectedElementIndex: number | null,
|
||||
|
||||
// Update current context values:
|
||||
decrementSearchIndex(): void,
|
||||
incrementSearchIndex(): void,
|
||||
selectElementWithID(id: number | null): void,
|
||||
selectElementAtIndex(index: number | null): void,
|
||||
updateSearchText(searchText: string): void,
|
||||
|};
|
||||
|
||||
const SearchAndSelectionContext = createContext<Context>(
|
||||
((null: any): Context)
|
||||
);
|
||||
// $FlowFixMe displayName is a valid attribute of React$ConsearchText
|
||||
SearchAndSelectionContext.displayName = 'SearchAndSelectionContext';
|
||||
|
||||
type State = {|
|
||||
searchIndex: number | null,
|
||||
searchResults: Array<number>,
|
||||
searchText: string,
|
||||
selectedElementID: number | null,
|
||||
selectedElementIndex: number | null,
|
||||
|};
|
||||
|
||||
type Action = {|
|
||||
type:
|
||||
| 'DECREMENT_SEARCH_INDEX'
|
||||
| 'INCREMENT_SEARCH_INDEX'
|
||||
| 'REFINE_SEARCH_RESULTS'
|
||||
| 'SELECT_ELEMENT_WITH_ID'
|
||||
| 'SELECT_ELEMENT_AT_INDEX'
|
||||
| 'UPDATE_SEARCH_TEXT',
|
||||
payload?: any,
|
||||
|};
|
||||
|
||||
type Props = {|
|
||||
children: React$Node,
|
||||
|};
|
||||
|
||||
// TODO Remove this wrapper element once global ConsearchText.write API exists.
|
||||
function SearchAndSelectionController({ children }: Props) {
|
||||
const store = useContext(StoreContext);
|
||||
|
||||
// This reducer is created inline because it needs access to the Store.
|
||||
// The store is mutable, but the Store itself is global and lives for the lifetime of the DevTools,
|
||||
// so it's okay for the reducer to have an empty dependencies array.
|
||||
const reducer = useMemo(
|
||||
() =>
|
||||
function reducer(state: State, action: Action): State {
|
||||
const { searchIndex, searchResults, searchText } = state;
|
||||
const { payload, type } = action;
|
||||
|
||||
let newSearchIndex,
|
||||
newSearchResults,
|
||||
newSearchText,
|
||||
newSelectedElementID;
|
||||
|
||||
switch (type) {
|
||||
case 'DECREMENT_SEARCH_INDEX':
|
||||
if (searchResults.length === 0) return state;
|
||||
|
||||
newSearchIndex =
|
||||
((searchIndex: any): number) > 0
|
||||
? ((searchIndex: any): number) - 1
|
||||
: searchResults.length - 1;
|
||||
newSelectedElementID = searchResults[newSearchIndex];
|
||||
|
||||
return {
|
||||
...state,
|
||||
searchIndex: newSearchIndex,
|
||||
selectedElementID: newSelectedElementID,
|
||||
selectedElementIndex: store.getIndexOfElementID(
|
||||
newSelectedElementID
|
||||
),
|
||||
};
|
||||
|
||||
case 'INCREMENT_SEARCH_INDEX':
|
||||
if (searchResults.length === 0) return state;
|
||||
|
||||
newSearchIndex =
|
||||
((searchIndex: any): number) + 1 < searchResults.length
|
||||
? ((searchIndex: any): number) + 1
|
||||
: 0;
|
||||
newSelectedElementID = searchResults[newSearchIndex];
|
||||
|
||||
return {
|
||||
...state,
|
||||
searchIndex: newSearchIndex,
|
||||
selectedElementID: newSelectedElementID,
|
||||
selectedElementIndex: store.getIndexOfElementID(
|
||||
newSelectedElementID
|
||||
),
|
||||
};
|
||||
|
||||
case 'REFINE_SEARCH_RESULTS':
|
||||
const [
|
||||
addedElementIDs,
|
||||
removedElementIDs,
|
||||
] = ((payload: any): Array<Uint32Array>);
|
||||
|
||||
newSearchResults = searchResults;
|
||||
newSearchIndex = searchIndex;
|
||||
|
||||
removedElementIDs.forEach(id => {
|
||||
// Prune this item from the search results.
|
||||
const index = newSearchResults.indexOf(id);
|
||||
if (index >= 0) {
|
||||
newSearchResults = newSearchResults
|
||||
.slice(0, index)
|
||||
.concat(newSearchResults.slice(index + 1));
|
||||
|
||||
// If the results are now empty, also deselect things.
|
||||
if (newSearchResults.length === 0) {
|
||||
newSearchIndex = null;
|
||||
} else if (
|
||||
((newSearchIndex: any): number) >= newSearchResults.length
|
||||
) {
|
||||
newSearchIndex = newSearchResults.length - 1;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
addedElementIDs.forEach(id => {
|
||||
const { displayName } = ((store.getElementByID(
|
||||
id
|
||||
): any): Element);
|
||||
|
||||
// Add this item to the search results if it matches.
|
||||
const regExp = createRegExp(searchText);
|
||||
if (displayName !== null && regExp.test(displayName)) {
|
||||
const newElementIndex = ((store.getIndexOfElementID(
|
||||
id
|
||||
): any): number);
|
||||
|
||||
let foundMatch = false;
|
||||
for (let index = 0; index < newSearchResults.length; index++) {
|
||||
const id = newSearchResults[index];
|
||||
if (
|
||||
newElementIndex <
|
||||
((store.getIndexOfElementID(id): any): number)
|
||||
) {
|
||||
foundMatch = true;
|
||||
newSearchResults = newSearchResults
|
||||
.slice(0, index)
|
||||
.concat(id)
|
||||
.concat(newSearchResults.slice(index));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
newSearchResults = newSearchResults.concat(id);
|
||||
}
|
||||
|
||||
newSearchIndex = newSearchIndex === null ? 0 : newSearchIndex;
|
||||
}
|
||||
});
|
||||
|
||||
newSelectedElementID =
|
||||
newSearchIndex !== null ? newSearchResults[newSearchIndex] : null;
|
||||
|
||||
return {
|
||||
...state,
|
||||
searchIndex: newSearchIndex,
|
||||
searchResults: newSearchResults,
|
||||
selectedElementID: newSelectedElementID,
|
||||
selectedElementIndex:
|
||||
newSelectedElementID !== null
|
||||
? store.getIndexOfElementID(newSelectedElementID)
|
||||
: null,
|
||||
};
|
||||
|
||||
case 'SELECT_ELEMENT_WITH_ID':
|
||||
newSelectedElementID = ((payload: any): number | null);
|
||||
|
||||
return {
|
||||
...state,
|
||||
selectedElementID: newSelectedElementID,
|
||||
selectedElementIndex:
|
||||
newSelectedElementID !== null
|
||||
? store.getIndexOfElementID(newSelectedElementID)
|
||||
: null,
|
||||
};
|
||||
|
||||
case 'SELECT_ELEMENT_AT_INDEX':
|
||||
newSearchIndex = ((payload: any): number | null);
|
||||
|
||||
const element =
|
||||
newSearchIndex !== null
|
||||
? store.getElementAtIndex(newSearchIndex)
|
||||
: null;
|
||||
|
||||
return {
|
||||
...state,
|
||||
selectedElementID: element !== null ? element.id : null,
|
||||
selectedElementIndex: newSearchIndex,
|
||||
};
|
||||
|
||||
case 'UPDATE_SEARCH_TEXT':
|
||||
newSearchIndex = searchIndex;
|
||||
newSearchResults = [];
|
||||
newSearchText = ((payload: any): string);
|
||||
|
||||
// Find all matching elements.
|
||||
if (newSearchText !== '') {
|
||||
const regExp = createRegExp(newSearchText);
|
||||
store.roots.forEach(rootID => {
|
||||
recursivelySearchTree(store, rootID, regExp, newSearchResults);
|
||||
});
|
||||
|
||||
// If this is a refinement of a previous search, preserve the current index (unless it's no longer valid).
|
||||
// If it's a new search, reset the index.
|
||||
if (newSearchResults.length === 0) {
|
||||
newSearchIndex = null;
|
||||
} else if (
|
||||
searchIndex !== null &&
|
||||
newSearchText.startsWith(searchText)
|
||||
) {
|
||||
newSearchIndex = Math.min(
|
||||
searchIndex,
|
||||
newSearchResults.length - 1
|
||||
);
|
||||
} else {
|
||||
newSearchIndex = 0;
|
||||
}
|
||||
} else {
|
||||
newSearchIndex = null;
|
||||
}
|
||||
|
||||
newSelectedElementID =
|
||||
newSearchIndex !== null ? newSearchResults[newSearchIndex] : null;
|
||||
|
||||
return {
|
||||
...state,
|
||||
searchIndex: newSearchIndex,
|
||||
searchResults: newSearchResults,
|
||||
searchText: newSearchText,
|
||||
selectedElementID: newSelectedElementID,
|
||||
selectedElementIndex:
|
||||
newSelectedElementID !== null
|
||||
? store.getIndexOfElementID(newSelectedElementID)
|
||||
: null,
|
||||
};
|
||||
|
||||
default:
|
||||
throw new Error(`Unrecognized action "${type}"`);
|
||||
}
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
const [state, dispatch] = useReducer(reducer, {
|
||||
searchIndex: null,
|
||||
searchResults: [],
|
||||
searchText: '',
|
||||
selectedElementID: null,
|
||||
selectedElementIndex: null,
|
||||
});
|
||||
|
||||
const decrementSearchIndex = useCallback(
|
||||
() => dispatch({ type: 'DECREMENT_SEARCH_INDEX' }),
|
||||
[dispatch]
|
||||
);
|
||||
const incrementSearchIndex = useCallback(
|
||||
() => dispatch({ type: 'INCREMENT_SEARCH_INDEX' }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectElementWithID = useCallback(
|
||||
(id: number | null) =>
|
||||
dispatch({ type: 'SELECT_ELEMENT_WITH_ID', payload: id }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectElementAtIndex = useCallback(
|
||||
(index: number | null) =>
|
||||
dispatch({ type: 'SELECT_ELEMENT_AT_INDEX', payload: index }),
|
||||
[dispatch]
|
||||
);
|
||||
const updateSearchText = useCallback(
|
||||
(searchText: string) =>
|
||||
dispatch({ type: 'UPDATE_SEARCH_TEXT', payload: searchText }),
|
||||
[dispatch]
|
||||
);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
searchIndex: state.searchIndex,
|
||||
searchResults: state.searchResults,
|
||||
searchText: state.searchText,
|
||||
selectedElementID: state.selectedElementID,
|
||||
selectedElementIndex: state.selectedElementIndex,
|
||||
|
||||
decrementSearchIndex,
|
||||
incrementSearchIndex,
|
||||
selectElementWithID,
|
||||
selectElementAtIndex,
|
||||
updateSearchText,
|
||||
}),
|
||||
[state]
|
||||
);
|
||||
|
||||
// Listen for changes to the tree and incrementally adjust the search results.
|
||||
useLayoutEffect(() => {
|
||||
const handleStoreMutated = ([
|
||||
addedElementIDs,
|
||||
removedElementIDs,
|
||||
]: Array<Uint32Array>) => {
|
||||
if (!state.searchText) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: 'REFINE_SEARCH_RESULTS',
|
||||
payload: [addedElementIDs, removedElementIDs],
|
||||
});
|
||||
};
|
||||
|
||||
store.addListener('mutated', handleStoreMutated);
|
||||
|
||||
return () => store.removeListener('mutated', handleStoreMutated);
|
||||
}, [state, store]);
|
||||
|
||||
return (
|
||||
<SearchAndSelectionContext.Provider value={value}>
|
||||
{children}
|
||||
</SearchAndSelectionContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function recursivelySearchTree(
|
||||
store: Store,
|
||||
elementID: number,
|
||||
regExp: RegExp,
|
||||
searchResults: Array<number>
|
||||
): void {
|
||||
const { children, displayName } = ((store.getElementByID(
|
||||
elementID
|
||||
): any): Element);
|
||||
if (displayName !== null) {
|
||||
if (regExp.test(displayName)) {
|
||||
searchResults.push(elementID);
|
||||
}
|
||||
}
|
||||
children.forEach(childID =>
|
||||
recursivelySearchTree(store, childID, regExp, searchResults)
|
||||
);
|
||||
}
|
||||
|
||||
export { SearchAndSelectionContext, SearchAndSelectionController };
|
||||
@@ -1,241 +0,0 @@
|
||||
// @flow
|
||||
|
||||
import React, {
|
||||
createContext,
|
||||
useContext,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useReducer,
|
||||
} from 'react';
|
||||
import { createRegExp } from './utils';
|
||||
import { StoreContext } from './context';
|
||||
import { SelectedElementContext } from './SelectedElementContext';
|
||||
import Store from '../store';
|
||||
|
||||
import type { Element } from 'src/devtools/types';
|
||||
|
||||
export type SearchContextValue = {|
|
||||
get currentIndex(): number | null,
|
||||
get ids(): Array<number>,
|
||||
get text(): string,
|
||||
updateCurrentIndex(newCurrentIndex: number): void,
|
||||
updateText(newText: string): void,
|
||||
|};
|
||||
|
||||
const SearchContext = createContext<SearchContextValue>(
|
||||
((null: any): SearchContextValue)
|
||||
);
|
||||
// $FlowFixMe displayName is a valid attribute of React$Context
|
||||
SearchContext.displayName = 'SearchContext';
|
||||
|
||||
type State = {|
|
||||
currentIndex: number | null,
|
||||
ids: Array<number>,
|
||||
text: string,
|
||||
|};
|
||||
|
||||
type Action = {|
|
||||
type: 'UPDATE',
|
||||
payload: $Shape<State>,
|
||||
|};
|
||||
|
||||
function reducer(state: State, action: Action) {
|
||||
switch (action.type) {
|
||||
case 'UPDATE':
|
||||
return {
|
||||
...state,
|
||||
...action.payload,
|
||||
};
|
||||
default:
|
||||
throw new Error();
|
||||
}
|
||||
}
|
||||
|
||||
type Props = {|
|
||||
children: React$Node,
|
||||
|};
|
||||
|
||||
// TODO Remove this wrapper element once global Context.write API exists.
|
||||
function SearchController({ children }: Props) {
|
||||
const store = useContext(StoreContext);
|
||||
const selectedElement = useContext(SelectedElementContext);
|
||||
|
||||
const [state, dispatch] = useReducer(reducer, {
|
||||
currentIndex: null,
|
||||
ids: [],
|
||||
text: '',
|
||||
});
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
get currentIndex(): number | null {
|
||||
return state.currentIndex;
|
||||
},
|
||||
|
||||
get ids(): Array<number> {
|
||||
return state.ids;
|
||||
},
|
||||
|
||||
get text(): string {
|
||||
return state.text;
|
||||
},
|
||||
|
||||
updateCurrentIndex(newCurrentIndex: number) {
|
||||
dispatch({
|
||||
type: 'UPDATE',
|
||||
payload: {
|
||||
currentIndex: newCurrentIndex,
|
||||
},
|
||||
});
|
||||
|
||||
// Make sure a new search result is also selected.
|
||||
selectedElement.id =
|
||||
newCurrentIndex !== null ? state.ids[newCurrentIndex] : null;
|
||||
},
|
||||
|
||||
updateText(newText: string) {
|
||||
const { currentIndex: oldCurrentIndex, text: oldText } = state;
|
||||
|
||||
let newCurrentIndex = oldCurrentIndex;
|
||||
let newIDs = [];
|
||||
|
||||
// Find all matching elements.
|
||||
if (newText !== '') {
|
||||
let regExp = createRegExp(newText);
|
||||
store.roots.forEach(rootID => {
|
||||
searchTree(store, rootID, regExp, newIDs);
|
||||
});
|
||||
|
||||
// If this is a refinement of a previous search, preserve the current index (unless it's no longer valid).
|
||||
// If it's a new search, reset the index.
|
||||
if (newIDs.length === 0) {
|
||||
newCurrentIndex = null;
|
||||
} else if (oldCurrentIndex !== null && newText.startsWith(oldText)) {
|
||||
newCurrentIndex = Math.min(oldCurrentIndex, newIDs.length - 1);
|
||||
} else {
|
||||
newCurrentIndex = 0;
|
||||
}
|
||||
} else {
|
||||
newCurrentIndex = null;
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: 'UPDATE',
|
||||
payload: {
|
||||
currentIndex: newCurrentIndex,
|
||||
ids: newIDs,
|
||||
text: newText,
|
||||
},
|
||||
});
|
||||
|
||||
// Make sure a new search result is also selected.
|
||||
selectedElement.id =
|
||||
newCurrentIndex !== null ? newIDs[newCurrentIndex] : null;
|
||||
},
|
||||
}),
|
||||
[state]
|
||||
);
|
||||
|
||||
// Listen for changes to the tree and incrementally adjust the search results.
|
||||
useLayoutEffect(() => {
|
||||
const handleStoreMutated = ([
|
||||
addedElementIDs,
|
||||
removedElementIDs,
|
||||
]: Array<Uint32Array>) => {
|
||||
const { currentIndex, ids, text } = state;
|
||||
|
||||
if (!text) {
|
||||
return;
|
||||
}
|
||||
|
||||
let newIDs = ids;
|
||||
let newCurrentIndex = currentIndex;
|
||||
|
||||
removedElementIDs.forEach(id => {
|
||||
// Prune this item from the search results.
|
||||
const index = newIDs.indexOf(id);
|
||||
if (index >= 0) {
|
||||
newIDs = newIDs.slice(0, index).concat(newIDs.slice(index + 1));
|
||||
|
||||
// If the results are now empty, also deselect things.
|
||||
if (newIDs.length === 0) {
|
||||
newCurrentIndex = null;
|
||||
} else if (((newCurrentIndex: any): number) >= newIDs.length) {
|
||||
newCurrentIndex = newIDs.length - 1;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
addedElementIDs.forEach(id => {
|
||||
const { displayName } = ((store.getElementByID(id): any): Element);
|
||||
|
||||
// Add this item to the search results if it matches.
|
||||
const regExp = createRegExp(text);
|
||||
if (displayName !== null && regExp.test(displayName)) {
|
||||
const newElementIndex = ((store.getIndexOfElementID(
|
||||
id
|
||||
): any): number);
|
||||
|
||||
let foundMatch = false;
|
||||
for (let index = 0; index < newIDs.length; index++) {
|
||||
const id = newIDs[index];
|
||||
if (
|
||||
newElementIndex < ((store.getIndexOfElementID(id): any): number)
|
||||
) {
|
||||
foundMatch = true;
|
||||
newIDs = newIDs
|
||||
.slice(0, index)
|
||||
.concat(id)
|
||||
.concat(newIDs.slice(index));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
newIDs = newIDs.concat(id);
|
||||
}
|
||||
|
||||
newCurrentIndex = newCurrentIndex === null ? 0 : newCurrentIndex;
|
||||
}
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: 'UPDATE',
|
||||
payload: {
|
||||
currentIndex: newCurrentIndex,
|
||||
ids: newIDs,
|
||||
},
|
||||
});
|
||||
|
||||
// Make sure a new search result is also selected.
|
||||
selectedElement.id =
|
||||
newCurrentIndex !== null ? newIDs[newCurrentIndex] : null;
|
||||
};
|
||||
|
||||
store.addListener('mutated', handleStoreMutated);
|
||||
|
||||
return () => store.removeListener('mutated', handleStoreMutated);
|
||||
}, [state, store]);
|
||||
|
||||
return (
|
||||
<SearchContext.Provider value={value}>{children}</SearchContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function searchTree(
|
||||
store: Store,
|
||||
elementID: number,
|
||||
regExp: RegExp,
|
||||
ids: Array<number>
|
||||
): void {
|
||||
const { children, displayName } = ((store.getElementByID(
|
||||
elementID
|
||||
): any): Element);
|
||||
if (displayName !== null) {
|
||||
if (regExp.test(displayName)) {
|
||||
ids.push(elementID);
|
||||
}
|
||||
}
|
||||
children.forEach(childID => searchTree(store, childID, regExp, ids));
|
||||
}
|
||||
|
||||
export { SearchContext, SearchController };
|
||||
@@ -1,7 +1,7 @@
|
||||
// @flow
|
||||
|
||||
import React, { useCallback, useContext, useEffect, useRef } from 'react';
|
||||
import { SearchContext } from './SearchContext';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import ButtonIcon from './ButtonIcon';
|
||||
|
||||
import styles from './SearchInput.css';
|
||||
@@ -9,50 +9,33 @@ import styles from './SearchInput.css';
|
||||
type Props = {||};
|
||||
|
||||
export default function SearchInput(props: Props) {
|
||||
// We use the wrapper searchContext object rather than its individual parts–
|
||||
// (specifically for the useCallback input arrays below)–
|
||||
// because a change in any one value (e.g. ids array) impacts other values (e.g. currentIndex).
|
||||
// It's easier to avoid stale scope issues if we depend on the entire search state.
|
||||
const searchContext = useContext(SearchContext);
|
||||
const {
|
||||
decrementSearchIndex,
|
||||
incrementSearchIndex,
|
||||
searchIndex,
|
||||
searchResults,
|
||||
searchText,
|
||||
updateSearchText,
|
||||
} = useContext(SearchAndSelectionContext);
|
||||
|
||||
const inputRef = useRef();
|
||||
|
||||
const handleTextChange = useCallback(({ currentTarget }) => {
|
||||
searchContext.updateText(currentTarget.value);
|
||||
});
|
||||
|
||||
const selectNext = useCallback(() => {
|
||||
const { currentIndex } = searchContext;
|
||||
if (currentIndex !== null) {
|
||||
searchContext.updateCurrentIndex(currentIndex + 1);
|
||||
}
|
||||
}, [searchContext]);
|
||||
|
||||
const selectPrevious = useCallback(() => {
|
||||
const { currentIndex } = searchContext;
|
||||
if (currentIndex !== null) {
|
||||
searchContext.updateCurrentIndex(currentIndex - 1);
|
||||
}
|
||||
}, [searchContext]);
|
||||
const handleTextChange = useCallback(
|
||||
({ currentTarget }) => updateSearchText(currentTarget.value),
|
||||
[updateSearchText]
|
||||
);
|
||||
|
||||
const resetSearch = useCallback(() => {
|
||||
searchContext.updateText('');
|
||||
}, [searchContext]);
|
||||
updateSearchText('');
|
||||
}, [updateSearchText]);
|
||||
|
||||
const handleInputKeyPress = useCallback(
|
||||
({ key }) => {
|
||||
if (key === 'Enter') {
|
||||
const { currentIndex, ids } = searchContext;
|
||||
if (currentIndex !== null) {
|
||||
if (currentIndex + 1 < ids.length) {
|
||||
searchContext.updateCurrentIndex(currentIndex + 1);
|
||||
} else {
|
||||
searchContext.updateCurrentIndex(0);
|
||||
}
|
||||
}
|
||||
incrementSearchIndex();
|
||||
}
|
||||
},
|
||||
[searchContext]
|
||||
[incrementSearchIndex]
|
||||
);
|
||||
|
||||
// Auto-focus search input
|
||||
@@ -72,8 +55,6 @@ export default function SearchInput(props: Props) {
|
||||
return () => window.removeEventListener('keydown', handleWindowKeyDown);
|
||||
}, [inputRef]);
|
||||
|
||||
const { currentIndex, ids, text } = searchContext;
|
||||
|
||||
return (
|
||||
<div className={styles.SearchInput}>
|
||||
<input
|
||||
@@ -82,33 +63,32 @@ export default function SearchInput(props: Props) {
|
||||
onChange={handleTextChange}
|
||||
placeholder="Search (text or /regex/)"
|
||||
ref={inputRef}
|
||||
value={text}
|
||||
value={searchText}
|
||||
/>
|
||||
{!!text && (
|
||||
{!!searchText && (
|
||||
<span className={styles.IndexLabel}>
|
||||
{Math.min(currentIndex + 1, ids.length)} | {ids.length}
|
||||
{Math.min(searchIndex + 1, searchResults.length)} |{' '}
|
||||
{searchResults.length}
|
||||
</span>
|
||||
)}
|
||||
<div className={styles.LeftVRule} />
|
||||
<button
|
||||
className={styles.IconButton}
|
||||
disabled={currentIndex === null || currentIndex === 0}
|
||||
onClick={selectPrevious}
|
||||
onClick={decrementSearchIndex}
|
||||
title="Scroll to previous search result"
|
||||
>
|
||||
<ButtonIcon type="up" />
|
||||
</button>
|
||||
<button
|
||||
className={styles.IconButton}
|
||||
disabled={currentIndex === null || currentIndex === ids.length - 1}
|
||||
onClick={selectNext}
|
||||
onClick={incrementSearchIndex}
|
||||
title="Scroll to next search result"
|
||||
>
|
||||
<ButtonIcon type="down" />
|
||||
</button>
|
||||
<button
|
||||
className={styles.IconButton}
|
||||
disabled={!text}
|
||||
disabled={!searchText}
|
||||
onClick={resetSearch}
|
||||
title="Reset search"
|
||||
>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// @flow
|
||||
|
||||
import React, { useContext, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { SelectedElementContext } from './SelectedElementContext';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import { BridgeContext, StoreContext } from './context';
|
||||
import ButtonIcon from './ButtonIcon';
|
||||
import HooksTree from './HooksTree';
|
||||
@@ -11,17 +11,16 @@ import styles from './SelectedElement.css';
|
||||
|
||||
import type { InspectedElement } from '../types';
|
||||
import type { DehydratedData } from 'src/devtools/types';
|
||||
import type { SelectedElementContextValue } from './SelectedElementContext';
|
||||
|
||||
export type Props = {||};
|
||||
|
||||
export default function SelectedElement(_: Props) {
|
||||
const selectedElement = useContext(SelectedElementContext);
|
||||
const { id } = selectedElement;
|
||||
const { selectedElementID } = useContext(SearchAndSelectionContext);
|
||||
const store = useContext(StoreContext);
|
||||
const element = id !== null ? store.getElementByID(id) : null;
|
||||
const element =
|
||||
selectedElementID !== null ? store.getElementByID(selectedElementID) : null;
|
||||
|
||||
const inspectedElement = useInspectedElement(id);
|
||||
const inspectedElement = useInspectedElement(selectedElementID);
|
||||
|
||||
// TODO Make "view DOM" and "view source" buttons work
|
||||
|
||||
@@ -65,10 +64,7 @@ export default function SelectedElement(_: Props) {
|
||||
)}
|
||||
|
||||
{inspectedElement !== null && (
|
||||
<InspectedElementView
|
||||
inspectedElement={inspectedElement}
|
||||
selectedElement={selectedElement}
|
||||
/>
|
||||
<InspectedElementView inspectedElement={inspectedElement} />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
@@ -76,13 +72,11 @@ export default function SelectedElement(_: Props) {
|
||||
|
||||
type InspectedElementViewProps = {|
|
||||
inspectedElement: InspectedElement,
|
||||
selectedElement: SelectedElementContextValue,
|
||||
|};
|
||||
|
||||
function InspectedElementView({
|
||||
inspectedElement,
|
||||
selectedElement,
|
||||
}: InspectedElementViewProps) {
|
||||
function InspectedElementView({ inspectedElement }: InspectedElementViewProps) {
|
||||
const { selectElementWithID } = useContext(SearchAndSelectionContext);
|
||||
|
||||
let { context, hooks, owners, props, state } = inspectedElement;
|
||||
|
||||
return (
|
||||
@@ -99,9 +93,7 @@ function InspectedElementView({
|
||||
<div
|
||||
key={owner.id}
|
||||
className={styles.Owner}
|
||||
onClick={() => {
|
||||
selectedElement.id = owner.id;
|
||||
}}
|
||||
onClick={() => selectElementWithID(owner.id)}
|
||||
title={owner.displayName}
|
||||
>
|
||||
{owner.displayName}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
// @flow
|
||||
|
||||
import React, { createContext, useContext, useMemo, useState } from 'react';
|
||||
import { StoreContext } from './context';
|
||||
|
||||
export type SelectedElementContextValue = {|
|
||||
id: number | null,
|
||||
index: number | null,
|
||||
|};
|
||||
|
||||
const SelectedElementContext = createContext<SelectedElementContextValue>(
|
||||
((null: any): SelectedElementContextValue)
|
||||
);
|
||||
// $FlowFixMe displayName is a valid attribute of React$Context
|
||||
SelectedElementContext.displayName = 'SelectedElementContext';
|
||||
|
||||
type Props = {|
|
||||
children: React$Node,
|
||||
|};
|
||||
|
||||
// TODO Remove this wrapper element once global Context.write API exists.
|
||||
function SelectedElementController({ children }: Props) {
|
||||
const store = useContext(StoreContext);
|
||||
|
||||
const [id, setID] = useState<number | null>(null);
|
||||
const [index, setIndex] = useState<number | null>(null);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
get id() {
|
||||
return id;
|
||||
},
|
||||
set id(id: number | null) {
|
||||
setID(id);
|
||||
setIndex(id !== null ? store.getIndexOfElementID(id) : null);
|
||||
},
|
||||
get index() {
|
||||
return index;
|
||||
},
|
||||
set index(index: number | null) {
|
||||
const element = index !== null ? store.getElementAtIndex(index) : null;
|
||||
setID(element !== null ? element.id : null);
|
||||
setIndex(index);
|
||||
},
|
||||
}),
|
||||
[id]
|
||||
);
|
||||
|
||||
return (
|
||||
<SelectedElementContext.Provider value={value}>
|
||||
{children}
|
||||
</SelectedElementContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export { SelectedElementContext, SelectedElementController };
|
||||
+15
-15
@@ -3,7 +3,7 @@
|
||||
import React, { useContext, useEffect, useLayoutEffect, useRef } from 'react';
|
||||
import AutoSizer from 'react-virtualized-auto-sizer';
|
||||
import { FixedSizeList } from 'react-window';
|
||||
import { SelectedElementContext } from './SelectedElementContext';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import ButtonIcon from './ButtonIcon';
|
||||
import Element from './Element';
|
||||
import SearchInput from './SearchInput';
|
||||
@@ -14,37 +14,37 @@ import styles from './Tree.css';
|
||||
type Props = {||};
|
||||
|
||||
export default function Tree(props: Props) {
|
||||
const selectedElementContext = useContext(SelectedElementContext);
|
||||
const { selectedElementIndex, selectElementAtIndex } = useContext(
|
||||
SearchAndSelectionContext
|
||||
);
|
||||
const treeContext = useContext(TreeContext);
|
||||
const listRef = useRef<FixedSizeList<any>>();
|
||||
|
||||
// Make sure a newly selected element is visible in the list.
|
||||
// This is helpful for things like the owners list.
|
||||
useLayoutEffect(() => {
|
||||
const { index } = selectedElementContext;
|
||||
if (index !== null && listRef.current != null) {
|
||||
listRef.current.scrollToItem(index);
|
||||
if (selectedElementIndex !== null && listRef.current != null) {
|
||||
listRef.current.scrollToItem(selectedElementIndex);
|
||||
}
|
||||
}, [listRef, selectedElementContext]);
|
||||
}, [listRef, selectedElementIndex]);
|
||||
|
||||
// Navigate the tree with up/down arrow keys.
|
||||
useEffect(() => {
|
||||
const handleKeyDown = event => {
|
||||
let index;
|
||||
|
||||
// eslint-disable-next-line default-case
|
||||
switch (event.key) {
|
||||
case 'ArrowDown':
|
||||
index = selectedElementContext.index;
|
||||
if (index !== null && index + 1 < treeContext.size) {
|
||||
selectedElementContext.index = ((index: any): number) + 1;
|
||||
if (
|
||||
selectedElementIndex !== null &&
|
||||
selectedElementIndex + 1 < treeContext.size
|
||||
) {
|
||||
selectElementAtIndex(((selectedElementIndex: any): number) + 1);
|
||||
}
|
||||
event.preventDefault();
|
||||
break;
|
||||
case 'ArrowUp':
|
||||
index = selectedElementContext.index;
|
||||
if (index !== null && index > 0) {
|
||||
selectedElementContext.index = ((index: any): number) - 1;
|
||||
if (selectedElementIndex !== null && selectedElementIndex > 0) {
|
||||
selectElementAtIndex(((selectedElementIndex: any): number) - 1);
|
||||
}
|
||||
event.preventDefault();
|
||||
break;
|
||||
@@ -56,7 +56,7 @@ export default function Tree(props: Props) {
|
||||
return () => {
|
||||
window.removeEventListener('keydown', handleKeyDown);
|
||||
};
|
||||
}, [selectedElementContext, treeContext]);
|
||||
}, [selectedElementIndex, selectElementAtIndex, treeContext]);
|
||||
|
||||
return (
|
||||
<div className={styles.Tree}>
|
||||
|
||||
Reference in new issue
Block a user