mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Refactored tree, search, selection, and owners contexts
This commit is contained in:
1 parent
45641e98e5
commit
0380186501
15 files changed
+772
-767
No files matched your search
@@ -546,7 +546,7 @@ export function attach(
|
||||
const { _debugOwner } = fiber;
|
||||
|
||||
const ownerID =
|
||||
_debugOwner !== null ? getFiberID(getPrimaryFiber(_debugOwner)) : 0;
|
||||
_debugOwner != null ? getFiberID(getPrimaryFiber(_debugOwner)) : 0;
|
||||
|
||||
let encodedDisplayName = ((null: any): Uint8Array);
|
||||
let encodedKey = ((null: any): Uint8Array);
|
||||
|
||||
+11
-3
@@ -107,6 +107,11 @@ export default class Store extends EventEmitter {
|
||||
return ((currentElement: any): Element);
|
||||
}
|
||||
|
||||
getElementIDAtIndex(index: number): number | null {
|
||||
const element: Element | null = this.getElementAtIndex(index);
|
||||
return element === null ? null : element.id;
|
||||
}
|
||||
|
||||
getElementByID(id: number): Element | null {
|
||||
const element = this._idToElement.get(id);
|
||||
|
||||
@@ -286,9 +291,10 @@ export default class Store extends EventEmitter {
|
||||
|
||||
this._idToElement.set(id, element);
|
||||
|
||||
// Track newly addewd items so search results can be updated
|
||||
const oldAddedElementIDs = addedElementIDs;
|
||||
addedElementIDs = new Uint32Array(addedElementIDs.length + 1);
|
||||
addedElementIDs[addedElementIDs.length - 1] = id;
|
||||
addedElementIDs.set(oldAddedElementIDs);
|
||||
addedElementIDs[oldAddedElementIDs.length] = id;
|
||||
|
||||
weightDelta = 1;
|
||||
}
|
||||
@@ -319,8 +325,10 @@ export default class Store extends EventEmitter {
|
||||
}
|
||||
|
||||
// Track removed items so search results can be updated
|
||||
const oldRemovededElementIDs = removedElementIDs;
|
||||
removedElementIDs = new Uint32Array(removedElementIDs.length + 1);
|
||||
removedElementIDs[removedElementIDs.length - 1] = id;
|
||||
removedElementIDs.set(oldRemovededElementIDs);
|
||||
removedElementIDs[oldRemovededElementIDs.length] = id;
|
||||
break;
|
||||
case TREE_OPERATION_RESET_CHILDREN:
|
||||
id = ((operations[i + 1]: any): number);
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
// @flow
|
||||
|
||||
import Store from './Store';
|
||||
|
||||
export const ElementTypeClassOrFunction = 1;
|
||||
export const ElementTypeContext = 2;
|
||||
export const ElementTypeForwardRef = 3;
|
||||
@@ -67,14 +65,6 @@ export type InspectedElement = {|
|
||||
|
||||
// TODO: Add profiling type
|
||||
|
||||
// The frontend Store is mutable.
|
||||
// This wrapper object is recreated whenever the store's tree is mutated.
|
||||
// It is used to trigger a re-render of the virtualized Elements tree.
|
||||
export type TreeMetadataType = {|
|
||||
size: number,
|
||||
store: Store,
|
||||
|};
|
||||
|
||||
export type DehydratedData = {|
|
||||
cleaned: Array<Array<string>>,
|
||||
data: Object,
|
||||
|
||||
@@ -8,6 +8,7 @@ type Props = {|
|
||||
| 'back'
|
||||
| 'close'
|
||||
| 'down'
|
||||
| 'more'
|
||||
| 'search'
|
||||
| 'up'
|
||||
| 'view-dom'
|
||||
@@ -26,6 +27,9 @@ export default function ButtonIcon({ type }: Props) {
|
||||
case 'down':
|
||||
pathData = PATH_DOWN;
|
||||
break;
|
||||
case 'more':
|
||||
pathData = PATH_MORE;
|
||||
break;
|
||||
case 'search':
|
||||
pathData = PATH_SEARCH;
|
||||
break;
|
||||
@@ -68,6 +72,13 @@ const PATH_CLOSE =
|
||||
|
||||
const PATH_DOWN = 'M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z';
|
||||
|
||||
const PATH_MORE = `
|
||||
M22 3H7c-.69 0-1.23.35-1.59.88L0 12l5.41 8.11c.36.53.97.89 1.66.89H22c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9
|
||||
13.5c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm5 0c-.83
|
||||
0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5.67 1.5 1.5-.67 1.5-1.5 1.5zm5 0c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5
|
||||
1.5.67 1.5 1.5-.67 1.5-1.5 1.5z
|
||||
`;
|
||||
|
||||
const PATH_SEARCH = `
|
||||
M20.94 11c-.46-4.17-3.77-7.48-7.94-7.94V1h-2v2.06C6.83 3.52 3.52 6.83 3.06 11H1v2h2.06c.46
|
||||
4.17 3.77 7.48 7.94 7.94V23h2v-2.06c4.17-.46 7.48-3.77 7.94-7.94H23v-2h-2.06zM12 19c-3.87
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
import React, { Fragment, useCallback, useContext, useMemo } from 'react';
|
||||
import { ElementTypeClassOrFunction } from 'src/devtools/types';
|
||||
import { TreeContext } from './context';
|
||||
import { createRegExp } from './utils';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import { TreeContext } from './TreeContext';
|
||||
import Icon from './Icon';
|
||||
|
||||
import type { Element } from '../types';
|
||||
@@ -17,33 +16,30 @@ type Props = {
|
||||
};
|
||||
|
||||
export default function ElementView({ index, style }: Props) {
|
||||
const { store } = useContext(TreeContext);
|
||||
|
||||
const {
|
||||
ownerList,
|
||||
pushOwnerList,
|
||||
baseDepth,
|
||||
getElementAtIndex,
|
||||
selectOwner,
|
||||
selectedElementID,
|
||||
selectElementWithID,
|
||||
} = useContext(SearchAndSelectionContext);
|
||||
selectElementByID,
|
||||
} = useContext(TreeContext);
|
||||
|
||||
const element =
|
||||
ownerList !== null
|
||||
? ((store.getElementByID(ownerList[index]): any): Element)
|
||||
: ((store.getElementAtIndex(index): any): Element);
|
||||
const { children, depth, displayName, id, key, type } = element;
|
||||
const element = getElementAtIndex(index);
|
||||
const {
|
||||
children,
|
||||
depth,
|
||||
displayName,
|
||||
id,
|
||||
key,
|
||||
type,
|
||||
} = ((element: any): Element);
|
||||
|
||||
let calculatedDepth = depth;
|
||||
if (ownerList !== null) {
|
||||
const owner = ((store.getElementByID(ownerList[0]): any): Element);
|
||||
calculatedDepth = depth - owner.depth;
|
||||
}
|
||||
|
||||
const handleDoubleClick = useCallback(() => pushOwnerList(id), [id]);
|
||||
const handleDoubleClick = useCallback(() => selectOwner(id), [id]);
|
||||
|
||||
// TODO Add click and key handlers for toggling element open/close state.
|
||||
|
||||
const handleClick = useCallback(
|
||||
({ metaKey }) => selectElementWithID(metaKey ? null : id),
|
||||
({ metaKey }) => selectElementByID(metaKey ? null : id),
|
||||
[id]
|
||||
);
|
||||
|
||||
@@ -57,7 +53,7 @@ export default function ElementView({ index, style }: Props) {
|
||||
onDoubleClick={handleDoubleClick}
|
||||
style={{
|
||||
...style, // "style" comes from react-window
|
||||
paddingLeft: `${1 + calculatedDepth}rem`,
|
||||
paddingLeft: `${1 + depth - baseDepth}rem`,
|
||||
}}
|
||||
>
|
||||
{children.length > 0 && (
|
||||
@@ -86,9 +82,7 @@ type DisplayNameProps = {|
|
||||
|};
|
||||
|
||||
function DisplayName({ displayName, id }: DisplayNameProps) {
|
||||
const { searchIndex, searchResults, searchText } = useContext(
|
||||
SearchAndSelectionContext
|
||||
);
|
||||
const { searchIndex, searchResults, searchText } = useContext(TreeContext);
|
||||
const isSearchResult = useMemo(() => {
|
||||
return searchResults.includes(id);
|
||||
}, [id, searchResults]);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
.TreeWrapper {
|
||||
flex: 0 0 65%;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
.SelectedElementWrapper {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// @flow
|
||||
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import React, { useMemo } from 'react';
|
||||
import Store from '../store';
|
||||
import Tree from './Tree';
|
||||
import { BridgeContext, StoreContext, TreeContext } from './context';
|
||||
import { BridgeContext, StoreContext } from './context';
|
||||
import SelectedElement from './SelectedElement';
|
||||
import { SearchAndSelectionController } from './SearchAndSelectionContext';
|
||||
import { TreeContextController } from './TreeContext';
|
||||
import styles from './Elements.css';
|
||||
|
||||
import './root.css';
|
||||
@@ -21,46 +21,20 @@ export type Props = {|
|
||||
export default function Elements({ bridge, browserName, themeName }: Props) {
|
||||
const store = useMemo<Store>(() => new Store(bridge), []);
|
||||
|
||||
const [treeContext, setTreeContext] = useState({
|
||||
size: store.numElements,
|
||||
store,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const handler = () => {
|
||||
setTreeContext({
|
||||
size: store.numElements,
|
||||
store,
|
||||
});
|
||||
};
|
||||
|
||||
// Check for changes that happened between async render and passive effect.
|
||||
// (We had not yet subscribed to the store so we would have missed these.)
|
||||
if (treeContext.size !== store.numElements) {
|
||||
handler();
|
||||
}
|
||||
|
||||
store.addListener('mutated', handler);
|
||||
|
||||
return () => store.removeListener('mutated', handler);
|
||||
}, [store]);
|
||||
|
||||
// TODO Flex wrappers below should be user resizable.
|
||||
return (
|
||||
<BridgeContext.Provider value={bridge}>
|
||||
<StoreContext.Provider value={store}>
|
||||
<TreeContext.Provider value={treeContext}>
|
||||
<SearchAndSelectionController>
|
||||
<div className={styles.Elements}>
|
||||
<div className={styles.TreeWrapper}>
|
||||
<Tree />
|
||||
</div>
|
||||
<div className={styles.SelectedElementWrapper}>
|
||||
<SelectedElement />
|
||||
</div>
|
||||
<TreeContextController>
|
||||
<div className={styles.Elements}>
|
||||
<div className={styles.TreeWrapper}>
|
||||
<Tree />
|
||||
</div>
|
||||
</SearchAndSelectionController>
|
||||
</TreeContext.Provider>
|
||||
<div className={styles.SelectedElementWrapper}>
|
||||
<SelectedElement />
|
||||
</div>
|
||||
</div>
|
||||
</TreeContextController>
|
||||
</StoreContext.Provider>
|
||||
</BridgeContext.Provider>
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.IconButton {
|
||||
@@ -29,6 +30,7 @@
|
||||
color: var(--color-component-name);
|
||||
font-family: var(--font-family-monospace);
|
||||
font-size: var(--font-size-normal);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.Component {
|
||||
|
||||
@@ -2,26 +2,26 @@
|
||||
|
||||
import React, { useCallback, useContext } from 'react';
|
||||
import ButtonIcon from './ButtonIcon';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import { TreeContext } from './context';
|
||||
import { TreeContext } from './TreeContext';
|
||||
import { StoreContext } from './context';
|
||||
|
||||
import type { Element } from '../types';
|
||||
|
||||
import styles from './OwnersStack.css';
|
||||
|
||||
export default function OwnerStack() {
|
||||
const { clearOwnerList, ownerIDStack } = useContext(
|
||||
SearchAndSelectionContext
|
||||
);
|
||||
const { ownerStack, resetOwnerStack } = useContext(TreeContext);
|
||||
|
||||
// $FlowFixMe "Missing type annotation for U" whatever that means
|
||||
const elements = ownerIDStack.map(id => <ElementView key={id} id={id} />);
|
||||
const elements = ownerStack.map((id, index) => (
|
||||
<ElementView key={id} id={id} index={index} />
|
||||
));
|
||||
|
||||
return (
|
||||
<div className={styles.OwnerStack}>
|
||||
<button
|
||||
className={styles.IconButton}
|
||||
onClick={clearOwnerList}
|
||||
onClick={resetOwnerStack}
|
||||
title="Back to tree view"
|
||||
>
|
||||
<ButtonIcon type="back" />
|
||||
@@ -34,21 +34,21 @@ export default function OwnerStack() {
|
||||
|
||||
type Props = {
|
||||
id: number,
|
||||
index: number,
|
||||
};
|
||||
|
||||
function ElementView({ id }: Props) {
|
||||
const { ownerIDStack, popToOwnerList } = useContext(
|
||||
SearchAndSelectionContext
|
||||
);
|
||||
const { store } = useContext(TreeContext);
|
||||
function ElementView({ id, index }: Props) {
|
||||
const { ownerStackIndex, selectOwner } = useContext(TreeContext);
|
||||
const store = useContext(StoreContext);
|
||||
const { displayName } = ((store.getElementByID(id): any): Element);
|
||||
|
||||
const isCurrentlyFocusedOwner = ownerIDStack[ownerIDStack.length - 1] === id;
|
||||
const isCurrentlyFocusedOwner = ownerStackIndex === index;
|
||||
|
||||
const handleClick = useCallback(() => popToOwnerList(id), [
|
||||
id,
|
||||
popToOwnerList,
|
||||
]);
|
||||
const handleClick = useCallback(() => {
|
||||
if (!isCurrentlyFocusedOwner) {
|
||||
selectOwner(id);
|
||||
}
|
||||
}, [id, isCurrentlyFocusedOwner, selectOwner]);
|
||||
|
||||
return (
|
||||
<span
|
||||
|
||||
@@ -1,634 +0,0 @@
|
||||
// @flow
|
||||
|
||||
// This context contains both the search, owners stack, and selected element states.
|
||||
// These values are combined into a single context because changes in one often impact the others.
|
||||
// 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
|
||||
ownerIDStack: Array<number>,
|
||||
ownerList: Array<number> | null,
|
||||
searchIndex: number | null,
|
||||
searchResults: Array<number>,
|
||||
searchText: string,
|
||||
selectedElementID: number | null,
|
||||
selectedElementIndex: number | null,
|
||||
|
||||
// Update current context values:
|
||||
clearOwnerList: () => void,
|
||||
decrementSearchIndex(): void,
|
||||
incrementSearchIndex(): void,
|
||||
popToOwnerList(elementID: number): void,
|
||||
pushOwnerList(elementID: number): void,
|
||||
selectElementWithID(id: number | null): void,
|
||||
selectNextElementInTree(): void,
|
||||
selectPreviousElementInTree(): 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 = {|
|
||||
ownerIDStack: Array<number>,
|
||||
ownerList: Array<number> | null,
|
||||
searchIndex: number | null,
|
||||
searchResults: Array<number>,
|
||||
searchText: string,
|
||||
selectedElementID: number | null,
|
||||
selectedElementIndex: number | null,
|
||||
|};
|
||||
|
||||
type Action = {|
|
||||
type:
|
||||
| 'CLEAR_OWNER_LIST'
|
||||
| 'DECREMENT_SEARCH_INDEX'
|
||||
| 'INCREMENT_SEARCH_INDEX'
|
||||
| 'POP_TO_OWNER_LIST'
|
||||
| 'PUSH_OWNER_LIST'
|
||||
| 'REFINE_AFTER_MUTATION'
|
||||
| 'SELECT_ELEMENT_WITH_ID'
|
||||
| 'SELECT_NEXT_ELEMENT_IN_TREE'
|
||||
| 'SELECT_PREVIOUS_ELEMENT_IN_TREE'
|
||||
| '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 {
|
||||
ownerIDStack,
|
||||
ownerList,
|
||||
searchIndex,
|
||||
searchResults,
|
||||
searchText,
|
||||
selectedElementID,
|
||||
selectedElementIndex,
|
||||
} = state;
|
||||
const { payload, type } = action;
|
||||
|
||||
let element,
|
||||
elementID,
|
||||
newOwnerList,
|
||||
newOwnerIDStack,
|
||||
newSearchIndex,
|
||||
newSearchResults,
|
||||
newSearchText,
|
||||
newSelectedElementID,
|
||||
newSelectedElementIndex;
|
||||
|
||||
const updateSelectedElementIndexHelper = (
|
||||
newSelectedElementIndex: number
|
||||
) => {
|
||||
if (ownerList !== null) {
|
||||
newSelectedElementID =
|
||||
newSelectedElementIndex !== null
|
||||
? ownerList[newSelectedElementIndex]
|
||||
: null;
|
||||
|
||||
return {
|
||||
...state,
|
||||
selectedElementID: newSelectedElementID,
|
||||
selectedElementIndex: newSelectedElementIndex,
|
||||
};
|
||||
} else {
|
||||
element =
|
||||
newSelectedElementIndex !== null
|
||||
? store.getElementAtIndex(newSelectedElementIndex)
|
||||
: null;
|
||||
|
||||
return {
|
||||
...state,
|
||||
selectedElementID: element !== null ? element.id : null,
|
||||
selectedElementIndex: newSelectedElementIndex,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
switch (type) {
|
||||
case 'CLEAR_OWNER_LIST':
|
||||
return {
|
||||
...state,
|
||||
ownerList: null,
|
||||
ownerIDStack: [],
|
||||
};
|
||||
|
||||
case 'DECREMENT_SEARCH_INDEX':
|
||||
if (ownerList !== null) {
|
||||
throw Error(
|
||||
`Can't update search index while owner list is active`
|
||||
);
|
||||
}
|
||||
|
||||
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 (ownerList !== null) {
|
||||
throw Error(
|
||||
`Can't update search index while owner list is active`
|
||||
);
|
||||
}
|
||||
|
||||
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 'POP_TO_OWNER_LIST':
|
||||
elementID = ((payload: any): number);
|
||||
return {
|
||||
...state,
|
||||
ownerList: calculateCurrentOwnerList(
|
||||
store,
|
||||
elementID,
|
||||
elementID,
|
||||
[]
|
||||
),
|
||||
ownerIDStack: ownerIDStack.slice(
|
||||
0,
|
||||
ownerIDStack.indexOf(elementID) + 1
|
||||
),
|
||||
selectedElementIndex: null,
|
||||
selectedElementID: null,
|
||||
};
|
||||
|
||||
case 'PUSH_OWNER_LIST':
|
||||
elementID = ((payload: any): number);
|
||||
if (ownerIDStack.includes(elementID)) return state;
|
||||
|
||||
// Add this new owner, and fill in the owners above it as well.
|
||||
const ownerIDsToConcat = [];
|
||||
let currentOwnerID = elementID;
|
||||
while (
|
||||
currentOwnerID !== 0 &&
|
||||
ownerIDStack[ownerIDStack.length - 1] !== currentOwnerID
|
||||
) {
|
||||
ownerIDsToConcat.unshift(currentOwnerID);
|
||||
currentOwnerID = ((store.getElementByID(
|
||||
currentOwnerID
|
||||
): any): Element).ownerID;
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
ownerList: calculateCurrentOwnerList(
|
||||
store,
|
||||
elementID,
|
||||
elementID,
|
||||
[]
|
||||
),
|
||||
ownerIDStack: ownerIDStack.concat(ownerIDsToConcat),
|
||||
selectedElementIndex: null,
|
||||
selectedElementID: null,
|
||||
searchIndex: null,
|
||||
searchResults: [],
|
||||
searchText: '',
|
||||
};
|
||||
|
||||
case 'REFINE_AFTER_MUTATION':
|
||||
const [
|
||||
addedElementIDs,
|
||||
removedElementIDs,
|
||||
] = ((payload: any): Array<Uint32Array>);
|
||||
|
||||
newOwnerList = ownerList;
|
||||
newOwnerIDStack = ownerIDStack;
|
||||
newSearchIndex = searchIndex;
|
||||
newSearchResults = searchResults;
|
||||
newSearchText = searchText;
|
||||
newSelectedElementID = selectedElementID;
|
||||
newSelectedElementIndex = selectedElementIndex;
|
||||
|
||||
if (searchText !== '') {
|
||||
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;
|
||||
newSelectedElementIndex =
|
||||
newSelectedElementID !== null
|
||||
? store.getIndexOfElementID(newSelectedElementID)
|
||||
: null;
|
||||
}
|
||||
|
||||
// If an item in the owner stack has been removed from the tree, unwind it.
|
||||
if (ownerIDStack.length > 0) {
|
||||
let indexOfRemovedItem = -1;
|
||||
for (let i = 0; i < ownerIDStack.length; i++) {
|
||||
if (removedElementIDs.includes(ownerIDStack[i])) {
|
||||
indexOfRemovedItem = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (indexOfRemovedItem >= 0) {
|
||||
newOwnerIDStack = ownerIDStack.slice(0, indexOfRemovedItem);
|
||||
}
|
||||
|
||||
if (newOwnerIDStack.length === 0) {
|
||||
newOwnerList = null;
|
||||
} else {
|
||||
elementID = newOwnerIDStack[newOwnerIDStack.length - 1];
|
||||
|
||||
newOwnerList = calculateCurrentOwnerList(
|
||||
store,
|
||||
elementID,
|
||||
elementID,
|
||||
[]
|
||||
);
|
||||
|
||||
newSelectedElementIndex =
|
||||
newSelectedElementID !== null
|
||||
? newOwnerList.indexOf(newSelectedElementID)
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
ownerIDStack: newOwnerIDStack,
|
||||
ownerList: newOwnerList,
|
||||
searchIndex: newSearchIndex,
|
||||
searchResults: newSearchResults,
|
||||
selectedElementID: newSelectedElementID,
|
||||
selectedElementIndex: newSelectedElementIndex,
|
||||
};
|
||||
|
||||
case 'SELECT_ELEMENT_WITH_ID':
|
||||
newSelectedElementID = ((payload: any): number | null);
|
||||
newSelectedElementIndex = null;
|
||||
|
||||
if (newSelectedElementID !== null) {
|
||||
if (ownerList !== null) {
|
||||
newSelectedElementIndex = ownerList.indexOf(
|
||||
newSelectedElementID
|
||||
);
|
||||
} else {
|
||||
newSelectedElementIndex = store.getIndexOfElementID(
|
||||
newSelectedElementID
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const isInOwnerList =
|
||||
newSelectedElementID !== null &&
|
||||
ownerList !== null &&
|
||||
ownerList.includes(newSelectedElementID);
|
||||
|
||||
return {
|
||||
...state,
|
||||
ownerIDStack: isInOwnerList ? ownerIDStack : [],
|
||||
ownerList: isInOwnerList ? ownerList : null,
|
||||
selectedElementID: newSelectedElementID,
|
||||
selectedElementIndex: newSelectedElementIndex,
|
||||
};
|
||||
|
||||
case 'SELECT_NEXT_ELEMENT_IN_TREE':
|
||||
if (selectedElementIndex === null) {
|
||||
return state;
|
||||
} else if (ownerList !== null) {
|
||||
return updateSelectedElementIndexHelper(
|
||||
selectedElementIndex + 1 < ownerList.length
|
||||
? selectedElementIndex + 1
|
||||
: selectedElementIndex
|
||||
);
|
||||
} else {
|
||||
return updateSelectedElementIndexHelper(
|
||||
selectedElementIndex + 1 < store.numElements
|
||||
? selectedElementIndex + 1
|
||||
: selectedElementIndex
|
||||
);
|
||||
}
|
||||
|
||||
case 'SELECT_PREVIOUS_ELEMENT_IN_TREE':
|
||||
if (selectedElementIndex === null) {
|
||||
return state;
|
||||
} else if (ownerList !== null) {
|
||||
return updateSelectedElementIndexHelper(
|
||||
selectedElementIndex > 0
|
||||
? selectedElementIndex - 1
|
||||
: selectedElementIndex
|
||||
);
|
||||
} else {
|
||||
return updateSelectedElementIndexHelper(
|
||||
selectedElementIndex > 0
|
||||
? selectedElementIndex - 1
|
||||
: selectedElementIndex
|
||||
);
|
||||
}
|
||||
|
||||
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, {
|
||||
ownerIDStack: [],
|
||||
ownerList: null,
|
||||
searchIndex: null,
|
||||
searchResults: [],
|
||||
searchText: '',
|
||||
selectedElementID: null,
|
||||
selectedElementIndex: null,
|
||||
});
|
||||
|
||||
const clearOwnerList = useCallback(
|
||||
() => dispatch({ type: 'CLEAR_OWNER_LIST' }),
|
||||
[dispatch]
|
||||
);
|
||||
const decrementSearchIndex = useCallback(
|
||||
() => dispatch({ type: 'DECREMENT_SEARCH_INDEX' }),
|
||||
[dispatch]
|
||||
);
|
||||
const incrementSearchIndex = useCallback(
|
||||
() => dispatch({ type: 'INCREMENT_SEARCH_INDEX' }),
|
||||
[dispatch]
|
||||
);
|
||||
const popToOwnerList = useCallback(
|
||||
(elementID: number) =>
|
||||
dispatch({ type: 'POP_TO_OWNER_LIST', payload: elementID }),
|
||||
[dispatch]
|
||||
);
|
||||
const pushOwnerList = useCallback(
|
||||
(elementID: number) =>
|
||||
dispatch({ type: 'PUSH_OWNER_LIST', payload: elementID }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectElementWithID = useCallback(
|
||||
(id: number | null) =>
|
||||
dispatch({ type: 'SELECT_ELEMENT_WITH_ID', payload: id }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectNextElementInTree = useCallback(
|
||||
() => dispatch({ type: 'SELECT_NEXT_ELEMENT_IN_TREE' }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectPreviousElementInTree = useCallback(
|
||||
() => dispatch({ type: 'SELECT_PREVIOUS_ELEMENT_IN_TREE' }),
|
||||
[dispatch]
|
||||
);
|
||||
const updateSearchText = useCallback(
|
||||
(searchText: string) =>
|
||||
dispatch({ type: 'UPDATE_SEARCH_TEXT', payload: searchText }),
|
||||
[dispatch]
|
||||
);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
ownerList: state.ownerList,
|
||||
ownerIDStack: state.ownerIDStack,
|
||||
searchIndex: state.searchIndex,
|
||||
searchResults: state.searchResults,
|
||||
searchText: state.searchText,
|
||||
selectedElementID: state.selectedElementID,
|
||||
selectedElementIndex: state.selectedElementIndex,
|
||||
|
||||
clearOwnerList,
|
||||
decrementSearchIndex,
|
||||
incrementSearchIndex,
|
||||
popToOwnerList,
|
||||
pushOwnerList,
|
||||
selectElementWithID,
|
||||
selectNextElementInTree,
|
||||
selectPreviousElementInTree,
|
||||
updateSearchText,
|
||||
}),
|
||||
[state]
|
||||
);
|
||||
|
||||
// Listen for changes to the tree and incrementally adjust the search results.
|
||||
useLayoutEffect(() => {
|
||||
const handleStoreMutated = ([
|
||||
addedElementIDs,
|
||||
removedElementIDs,
|
||||
]: Array<Uint32Array>) => {
|
||||
if (!state.searchText && state.ownerList === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: 'REFINE_AFTER_MUTATION',
|
||||
payload: [addedElementIDs, removedElementIDs],
|
||||
});
|
||||
};
|
||||
|
||||
store.addListener('mutated', handleStoreMutated);
|
||||
|
||||
return () => store.removeListener('mutated', handleStoreMutated);
|
||||
}, [state, store]);
|
||||
|
||||
return (
|
||||
<SearchAndSelectionContext.Provider value={value}>
|
||||
{children}
|
||||
</SearchAndSelectionContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
function calculateCurrentOwnerList(
|
||||
store: Store,
|
||||
rootOwnerID: number,
|
||||
elementID: number,
|
||||
ownerList: Array<number>
|
||||
): Array<number> {
|
||||
if (elementID === rootOwnerID) {
|
||||
ownerList.push(elementID);
|
||||
const { children } = ((store.getElementByID(elementID): any): Element);
|
||||
children.forEach(childID =>
|
||||
calculateCurrentOwnerList(store, rootOwnerID, childID, ownerList)
|
||||
);
|
||||
} else {
|
||||
const { children, ownerID } = ((store.getElementByID(
|
||||
elementID
|
||||
): any): Element);
|
||||
if (ownerID === rootOwnerID) {
|
||||
ownerList.push(elementID);
|
||||
children.forEach(childID =>
|
||||
calculateCurrentOwnerList(store, rootOwnerID, childID, ownerList)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return ownerList;
|
||||
}
|
||||
|
||||
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,7 +1,7 @@
|
||||
// @flow
|
||||
|
||||
import React, { useCallback, useContext, useEffect, useRef } from 'react';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import { TreeContext } from './TreeContext';
|
||||
import ButtonIcon from './ButtonIcon';
|
||||
|
||||
import styles from './SearchInput.css';
|
||||
@@ -10,32 +10,32 @@ type Props = {||};
|
||||
|
||||
export default function SearchInput(props: Props) {
|
||||
const {
|
||||
decrementSearchIndex,
|
||||
incrementSearchIndex,
|
||||
goToNextSearchResult,
|
||||
goToPreviousSearchResult,
|
||||
searchIndex,
|
||||
searchResults,
|
||||
searchText,
|
||||
updateSearchText,
|
||||
} = useContext(SearchAndSelectionContext);
|
||||
setSearchText,
|
||||
} = useContext(TreeContext);
|
||||
|
||||
const inputRef = useRef();
|
||||
|
||||
const handleTextChange = useCallback(
|
||||
({ currentTarget }) => updateSearchText(currentTarget.value),
|
||||
[updateSearchText]
|
||||
({ currentTarget }) => setSearchText(currentTarget.value),
|
||||
[setSearchText]
|
||||
);
|
||||
|
||||
const resetSearch = useCallback(() => {
|
||||
updateSearchText('');
|
||||
}, [updateSearchText]);
|
||||
setSearchText('');
|
||||
}, [setSearchText]);
|
||||
|
||||
const handleInputKeyPress = useCallback(
|
||||
({ key }) => {
|
||||
if (key === 'Enter') {
|
||||
incrementSearchIndex();
|
||||
goToNextSearchResult();
|
||||
}
|
||||
},
|
||||
[incrementSearchIndex]
|
||||
[goToNextSearchResult]
|
||||
);
|
||||
|
||||
// Auto-focus search input
|
||||
@@ -74,14 +74,16 @@ export default function SearchInput(props: Props) {
|
||||
<div className={styles.LeftVRule} />
|
||||
<button
|
||||
className={styles.IconButton}
|
||||
onClick={decrementSearchIndex}
|
||||
disabled={!searchText}
|
||||
onClick={goToPreviousSearchResult}
|
||||
title="Scroll to previous search result"
|
||||
>
|
||||
<ButtonIcon type="up" />
|
||||
</button>
|
||||
<button
|
||||
className={styles.IconButton}
|
||||
onClick={incrementSearchIndex}
|
||||
disabled={!searchText}
|
||||
onClick={goToNextSearchResult}
|
||||
title="Scroll to next search result"
|
||||
>
|
||||
<ButtonIcon type="down" />
|
||||
|
||||
@@ -7,7 +7,7 @@ import React, {
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import { TreeContext } from './TreeContext';
|
||||
import { BridgeContext, StoreContext } from './context';
|
||||
import ButtonIcon from './ButtonIcon';
|
||||
import HooksTree from './HooksTree';
|
||||
@@ -21,7 +21,7 @@ import type { DehydratedData } from 'src/devtools/types';
|
||||
export type Props = {||};
|
||||
|
||||
export default function SelectedElement(_: Props) {
|
||||
const { selectedElementID } = useContext(SearchAndSelectionContext);
|
||||
const { selectedElementID } = useContext(TreeContext);
|
||||
const store = useContext(StoreContext);
|
||||
const element =
|
||||
selectedElementID !== null ? store.getElementByID(selectedElementID) : null;
|
||||
@@ -107,9 +107,9 @@ function InspectedElementView({ inspectedElement }: InspectedElementViewProps) {
|
||||
}
|
||||
|
||||
function OwnerView({ displayName, id }: { displayName: string, id: number }) {
|
||||
const { selectElementWithID } = useContext(SearchAndSelectionContext);
|
||||
const { selectElementByID } = useContext(TreeContext);
|
||||
|
||||
const handleClick = useCallback(() => selectElementWithID(id), [id]);
|
||||
const handleClick = useCallback(() => selectElementByID(id), [id]);
|
||||
|
||||
return (
|
||||
<div
|
||||
|
||||
+16
-15
@@ -9,12 +9,11 @@ import React, {
|
||||
} from 'react';
|
||||
import AutoSizer from 'react-virtualized-auto-sizer';
|
||||
import { FixedSizeList } from 'react-window';
|
||||
import { SearchAndSelectionContext } from './SearchAndSelectionContext';
|
||||
import { TreeContext } from './TreeContext';
|
||||
import ButtonIcon from './ButtonIcon';
|
||||
import Element from './Element';
|
||||
import OwnersStack from './OwnersStack';
|
||||
import SearchInput from './SearchInput';
|
||||
import { TreeContext } from './context';
|
||||
|
||||
import styles from './Tree.css';
|
||||
|
||||
@@ -22,13 +21,14 @@ type Props = {||};
|
||||
|
||||
export default function Tree(props: Props) {
|
||||
const {
|
||||
ownerIDStack,
|
||||
ownerList,
|
||||
baseDepth,
|
||||
getElementAtIndex,
|
||||
numElements,
|
||||
ownerStack,
|
||||
selectedElementIndex,
|
||||
selectNextElementInTree,
|
||||
selectPreviousElementInTree,
|
||||
} = useContext(SearchAndSelectionContext);
|
||||
const treeContext = useContext(TreeContext);
|
||||
} = useContext(TreeContext);
|
||||
const listRef = useRef<FixedSizeList<any>>();
|
||||
|
||||
// Make sure a newly selected element is visible in the list.
|
||||
@@ -66,22 +66,23 @@ export default function Tree(props: Props) {
|
||||
selectedElementIndex,
|
||||
selectNextElementInTree,
|
||||
selectPreviousElementInTree,
|
||||
treeContext,
|
||||
]);
|
||||
|
||||
const itemCount = ownerList !== null ? ownerList.length : treeContext.size;
|
||||
|
||||
// Let react-window know to re-render any time the underlying tree data changes.
|
||||
// This includes the owner context, since it controls a filtered view of the tree.
|
||||
const itemData = useMemo(() => ({ treeContext, ownerList }), [
|
||||
treeContext,
|
||||
ownerList,
|
||||
]);
|
||||
const itemData = useMemo(
|
||||
() => ({
|
||||
baseDepth,
|
||||
numElements,
|
||||
getElementAtIndex,
|
||||
}),
|
||||
[baseDepth, numElements, getElementAtIndex]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={styles.Tree}>
|
||||
<div className={styles.SearchInput}>
|
||||
{ownerIDStack.length > 0 ? <OwnersStack /> : <SearchInput />}
|
||||
{ownerStack.length > 0 ? <OwnersStack /> : <SearchInput />}
|
||||
<button
|
||||
className={styles.IconButton}
|
||||
title="Select an element in the page to inspect it"
|
||||
@@ -95,7 +96,7 @@ export default function Tree(props: Props) {
|
||||
<FixedSizeList
|
||||
className={styles.List}
|
||||
height={height}
|
||||
itemCount={itemCount}
|
||||
itemCount={numElements}
|
||||
itemData={itemData}
|
||||
itemSize={20}
|
||||
ref={listRef}
|
||||
|
||||
@@ -0,0 +1,663 @@
|
||||
// @flow
|
||||
|
||||
// This context combines tree/selection state, search, and the owners stack.
|
||||
// These values are managed together because changes in one often impact the others.
|
||||
// Combining them enables us to avoid cascading renders.
|
||||
//
|
||||
// Changes to search state may impact tree state.
|
||||
// For example, updating the selected search result also updates the tree's selected value.
|
||||
// Search does not fundamanetally change the tree though.
|
||||
// It is also possible to update the selected tree value independently.
|
||||
//
|
||||
// Changes to owners state mask search and tree values.
|
||||
// When owners statck is not empty, search is temporarily disabnled,
|
||||
// and tree values (e.g. num elements, selected element) are masked.
|
||||
// Both tree and search values are restored when the owners stack is cleared.
|
||||
//
|
||||
// For this reason, changes to the tree context are processed in sequence: tree -> search -> owners
|
||||
// This enables each section to potentially override (or mask) previous values.
|
||||
|
||||
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 = {|
|
||||
// Tree
|
||||
baseDepth: number,
|
||||
numElements: number,
|
||||
selectedElementID: number | null,
|
||||
selectedElementIndex: number | null,
|
||||
getElementAtIndex(index: number): Element | null,
|
||||
selectElementAtIndex(index: number): void,
|
||||
selectElementByID(id: number | null): void,
|
||||
selectNextElementInTree(): void,
|
||||
selectPreviousElementInTree(): void,
|
||||
|
||||
// Search
|
||||
searchIndex: number | null,
|
||||
searchResults: Array<number>,
|
||||
searchText: string,
|
||||
setSearchText(text: string): void,
|
||||
goToNextSearchResult(): void,
|
||||
goToPreviousSearchResult(): void,
|
||||
|
||||
// Owners
|
||||
ownerStack: Array<number>,
|
||||
ownerStackIndex: number | null,
|
||||
resetOwnerStack(): void,
|
||||
selectOwner(id: number): void,
|
||||
|};
|
||||
|
||||
const TreeContext = createContext<Context>(((null: any): Context));
|
||||
// $FlowFixMe displayName is a valid attribute of React$ConsearchText
|
||||
TreeContext.displayName = 'TreeContext';
|
||||
|
||||
type State = {|
|
||||
// Tree
|
||||
baseDepth: number,
|
||||
numElements: number,
|
||||
selectedElementID: number | null,
|
||||
selectedElementIndex: number | null,
|
||||
|
||||
// Search
|
||||
searchIndex: number | null,
|
||||
searchResults: Array<number>,
|
||||
searchText: string,
|
||||
|
||||
// Owners
|
||||
ownerStack: Array<number>,
|
||||
ownerStackIndex: number | null,
|
||||
_ownerFlatTree: Array<number> | null,
|
||||
|};
|
||||
|
||||
type Action = {|
|
||||
type:
|
||||
| 'GO_TO_NEXT_SEARCH_RESULT'
|
||||
| 'GO_TO_PREVIOUS_SEARCH_RESULT'
|
||||
| 'HANDLE_STORE_MUTATION'
|
||||
| 'RESET_OWNER_STACK'
|
||||
| 'SELECT_ELEMENT_AT_INDEX'
|
||||
| 'SELECT_ELEMENT_BY_ID'
|
||||
| 'SELECT_NEXT_ELEMENT_IN_TREE'
|
||||
| 'SELECT_PREVIOUS_ELEMENT_IN_TREE'
|
||||
| 'SELECT_OWNER'
|
||||
| 'SET_SEARCH_TEXT',
|
||||
payload?: any,
|
||||
|};
|
||||
|
||||
function reduceTreeState(store: Store, state: State, action: Action): State {
|
||||
const { type, payload } = action;
|
||||
|
||||
let {
|
||||
numElements,
|
||||
ownerStack,
|
||||
selectedElementIndex,
|
||||
selectedElementID,
|
||||
} = state;
|
||||
|
||||
// Base tree should ignore selected element changes when the owner's tree is active.
|
||||
if (ownerStack.length === 0) {
|
||||
switch (type) {
|
||||
case 'HANDLE_STORE_MUTATION':
|
||||
numElements = store.numElements;
|
||||
|
||||
// If the currently-selected Element has been removed from the tree, update selection state.
|
||||
if (selectedElementID !== null) {
|
||||
const removedElementIDs = ((payload: any): Array<Uint32Array>)[1];
|
||||
if (removedElementIDs.includes(((selectedElementID: any): number))) {
|
||||
selectedElementIndex = null;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'SELECT_ELEMENT_AT_INDEX':
|
||||
selectedElementIndex = ((payload: any): number | null);
|
||||
break;
|
||||
case 'SELECT_ELEMENT_BY_ID':
|
||||
selectedElementIndex =
|
||||
payload === null
|
||||
? null
|
||||
: store.getIndexOfElementID(((payload: any): number));
|
||||
break;
|
||||
case 'SELECT_NEXT_ELEMENT_IN_TREE':
|
||||
if (
|
||||
selectedElementIndex !== null &&
|
||||
selectedElementIndex + 1 < numElements
|
||||
) {
|
||||
selectedElementIndex++;
|
||||
}
|
||||
break;
|
||||
case 'SELECT_PREVIOUS_ELEMENT_IN_TREE':
|
||||
if (selectedElementIndex !== null && selectedElementIndex > 0) {
|
||||
selectedElementIndex--;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// React can bailout of no-op updates.
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
// Keep selected item ID and index in sync.
|
||||
if (selectedElementIndex !== state.selectedElementIndex) {
|
||||
if (selectedElementIndex === null) {
|
||||
selectedElementID = null;
|
||||
} else {
|
||||
selectedElementID = store.getElementIDAtIndex(
|
||||
((selectedElementIndex: any): number)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
|
||||
numElements,
|
||||
selectedElementIndex,
|
||||
selectedElementID,
|
||||
};
|
||||
}
|
||||
|
||||
function reduceSearchState(store: Store, state: State, action: Action): State {
|
||||
const { type, payload } = action;
|
||||
|
||||
let {
|
||||
ownerStack,
|
||||
searchIndex,
|
||||
searchResults,
|
||||
searchText,
|
||||
selectedElementID,
|
||||
selectedElementIndex,
|
||||
} = state;
|
||||
|
||||
const prevSearchIndex = searchIndex;
|
||||
const numPrevSearchResults = searchResults.length;
|
||||
|
||||
// Search isn't supported when the owner's tree is active.
|
||||
if (ownerStack.length === 0) {
|
||||
switch (type) {
|
||||
case 'GO_TO_NEXT_SEARCH_RESULT':
|
||||
if (numPrevSearchResults > 0) {
|
||||
searchIndex =
|
||||
searchIndex + 1 < numPrevSearchResults ? searchIndex + 1 : 0;
|
||||
}
|
||||
break;
|
||||
case 'GO_TO_PREVIOUS_SEARCH_RESULT':
|
||||
if (numPrevSearchResults > 0) {
|
||||
searchIndex =
|
||||
((searchIndex: any): number) > 0
|
||||
? ((searchIndex: any): number) - 1
|
||||
: numPrevSearchResults - 1;
|
||||
}
|
||||
break;
|
||||
case 'HANDLE_STORE_MUTATION':
|
||||
if (searchText !== '') {
|
||||
const [
|
||||
addedElementIDs,
|
||||
removedElementIDs,
|
||||
] = ((payload: any): Array<Uint32Array>);
|
||||
|
||||
removedElementIDs.forEach(id => {
|
||||
// Prune this item from the search results.
|
||||
const index = searchResults.indexOf(id);
|
||||
if (index >= 0) {
|
||||
searchResults = searchResults
|
||||
.slice(0, index)
|
||||
.concat(searchResults.slice(index + 1));
|
||||
|
||||
// If the results are now empty, also deselect things.
|
||||
if (searchResults.length === 0) {
|
||||
searchIndex = null;
|
||||
} else if (((searchIndex: any): number) >= searchResults.length) {
|
||||
searchIndex = searchResults.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 < searchResults.length; index++) {
|
||||
const id = searchResults[index];
|
||||
if (
|
||||
newElementIndex <
|
||||
((store.getIndexOfElementID(id): any): number)
|
||||
) {
|
||||
foundMatch = true;
|
||||
searchResults = searchResults
|
||||
.slice(0, index)
|
||||
.concat(id)
|
||||
.concat(searchResults.slice(index));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!foundMatch) {
|
||||
searchResults = searchResults.concat(id);
|
||||
}
|
||||
|
||||
searchIndex = searchIndex === null ? 0 : searchIndex;
|
||||
}
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 'SET_SEARCH_TEXT':
|
||||
searchIndex = null;
|
||||
searchResults = [];
|
||||
searchText = ((payload: any): string);
|
||||
|
||||
if (searchText !== '') {
|
||||
const regExp = createRegExp(searchText);
|
||||
store.roots.forEach(rootID => {
|
||||
recursivelySearchTree(store, rootID, regExp, searchResults);
|
||||
});
|
||||
|
||||
if (searchResults.length > 0) {
|
||||
if (prevSearchIndex === null) {
|
||||
searchIndex = 0;
|
||||
} else {
|
||||
searchIndex = Math.min(
|
||||
((prevSearchIndex: any): number),
|
||||
searchResults.length - 1
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// React can bailout of no-op updates.
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
// Changes in search index should override the selected element.
|
||||
if (searchIndex !== prevSearchIndex) {
|
||||
if (searchIndex === null) {
|
||||
selectedElementIndex = null;
|
||||
selectedElementID = null;
|
||||
} else {
|
||||
selectedElementID = ((searchResults[searchIndex]: any): number);
|
||||
selectedElementIndex = store.getIndexOfElementID(
|
||||
((selectedElementID: any): number)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
|
||||
selectedElementID,
|
||||
selectedElementIndex,
|
||||
|
||||
searchIndex,
|
||||
searchResults,
|
||||
searchText,
|
||||
};
|
||||
}
|
||||
|
||||
function reduceOwnersState(store: Store, state: State, action: Action): State {
|
||||
const { payload, type } = action;
|
||||
|
||||
let {
|
||||
baseDepth,
|
||||
numElements,
|
||||
selectedElementID,
|
||||
selectedElementIndex,
|
||||
ownerStack,
|
||||
ownerStackIndex,
|
||||
searchIndex,
|
||||
searchResults,
|
||||
searchText,
|
||||
_ownerFlatTree,
|
||||
} = state;
|
||||
|
||||
switch (type) {
|
||||
case 'HANDLE_STORE_MUTATION':
|
||||
if (ownerStack.length > 0) {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const [_, removedElementIDs] = ((payload: any): Array<Uint32Array>);
|
||||
|
||||
let indexOfRemovedItem = -1;
|
||||
for (let i = 0; i < ownerStack.length; i++) {
|
||||
if (removedElementIDs.includes(ownerStack[i])) {
|
||||
indexOfRemovedItem = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (indexOfRemovedItem >= 0) {
|
||||
ownerStack = ownerStack.slice(0, indexOfRemovedItem);
|
||||
if (ownerStack.length === 0) {
|
||||
_ownerFlatTree = null;
|
||||
} else {
|
||||
ownerStackIndex = ownerStack.length - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'RESET_OWNER_STACK':
|
||||
ownerStack = [];
|
||||
ownerStackIndex = null;
|
||||
_ownerFlatTree = null;
|
||||
break;
|
||||
case 'SELECT_ELEMENT_AT_INDEX':
|
||||
if (_ownerFlatTree !== null) {
|
||||
selectedElementIndex = ((payload: any): number | null);
|
||||
}
|
||||
break;
|
||||
case 'SELECT_ELEMENT_BY_ID':
|
||||
if (_ownerFlatTree !== null) {
|
||||
selectedElementIndex =
|
||||
payload === null ? null : _ownerFlatTree.indexOf(payload);
|
||||
}
|
||||
break;
|
||||
case 'SELECT_NEXT_ELEMENT_IN_TREE':
|
||||
if (_ownerFlatTree !== null && _ownerFlatTree.length > 0) {
|
||||
if (selectedElementIndex === null) {
|
||||
selectedElementIndex = 0;
|
||||
} else if (selectedElementIndex + 1 < _ownerFlatTree.length) {
|
||||
selectedElementIndex++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'SELECT_PREVIOUS_ELEMENT_IN_TREE':
|
||||
if (_ownerFlatTree !== null && _ownerFlatTree.length > 0) {
|
||||
if (selectedElementIndex !== null && selectedElementIndex > 0) {
|
||||
selectedElementIndex--;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'SELECT_OWNER':
|
||||
ownerStackIndex = ownerStack.indexOf(payload);
|
||||
|
||||
// If this owner is already in the current stack, just select it.
|
||||
// Otherwise, create a new stack.
|
||||
if (ownerStackIndex < 0) {
|
||||
// Add this new owner, and fill in the owners above it as well.
|
||||
ownerStack = [];
|
||||
let currentOwnerID = ((payload: any): number);
|
||||
while (currentOwnerID !== 0) {
|
||||
ownerStack.unshift(currentOwnerID);
|
||||
currentOwnerID = ((store.getElementByID(
|
||||
currentOwnerID
|
||||
): any): Element).ownerID;
|
||||
}
|
||||
ownerStackIndex = ownerStack.length - 1;
|
||||
selectedElementIndex = 0;
|
||||
|
||||
if (searchText !== '') {
|
||||
searchIndex = null;
|
||||
searchResults = [];
|
||||
searchText = '';
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// React can bailout of no-op updates.
|
||||
return state;
|
||||
}
|
||||
|
||||
// Changes in the selected owner require re-calculating the owners tree.
|
||||
if (
|
||||
ownerStackIndex !== state.ownerStackIndex ||
|
||||
ownerStack !== state.ownerStack ||
|
||||
type === 'HANDLE_STORE_MUTATION'
|
||||
) {
|
||||
if (ownerStackIndex === null) {
|
||||
_ownerFlatTree = null;
|
||||
baseDepth = 0;
|
||||
numElements = store.numElements;
|
||||
} else {
|
||||
_ownerFlatTree = calculateCurrentOwnerList(
|
||||
store,
|
||||
ownerStack[ownerStackIndex],
|
||||
ownerStack[ownerStackIndex],
|
||||
[]
|
||||
);
|
||||
|
||||
baseDepth = ((store.getElementByID(_ownerFlatTree[0]): any): Element)
|
||||
.depth;
|
||||
numElements = _ownerFlatTree.length;
|
||||
}
|
||||
}
|
||||
|
||||
// Keep selected item ID and index in sync.
|
||||
if (selectedElementIndex !== state.selectedElementIndex) {
|
||||
if (selectedElementIndex === null) {
|
||||
selectedElementID = null;
|
||||
} else if (_ownerFlatTree !== null) {
|
||||
selectedElementID = _ownerFlatTree[((selectedElementIndex: any): number)];
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
|
||||
baseDepth,
|
||||
numElements,
|
||||
selectedElementID,
|
||||
selectedElementIndex,
|
||||
|
||||
searchIndex,
|
||||
searchResults,
|
||||
searchText,
|
||||
|
||||
ownerStack,
|
||||
ownerStackIndex,
|
||||
_ownerFlatTree,
|
||||
};
|
||||
}
|
||||
|
||||
// TODO Remove TreeContextController wrapper element once global ConsearchText.write API exists.
|
||||
function TreeContextController({ children }: {| children: React$Node |}) {
|
||||
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(
|
||||
() => (state: State, action: Action): State => {
|
||||
const { type } = action;
|
||||
switch (type) {
|
||||
case 'GO_TO_NEXT_SEARCH_RESULT':
|
||||
case 'GO_TO_PREVIOUS_SEARCH_RESULT':
|
||||
case 'HANDLE_STORE_MUTATION':
|
||||
case 'RESET_OWNER_STACK':
|
||||
case 'SELECT_ELEMENT_AT_INDEX':
|
||||
case 'SELECT_ELEMENT_BY_ID':
|
||||
case 'SELECT_NEXT_ELEMENT_IN_TREE':
|
||||
case 'SELECT_PREVIOUS_ELEMENT_IN_TREE':
|
||||
case 'SELECT_OWNER':
|
||||
case 'SET_SEARCH_TEXT':
|
||||
state = reduceTreeState(store, state, action);
|
||||
state = reduceSearchState(store, state, action);
|
||||
state = reduceOwnersState(store, state, action);
|
||||
return state;
|
||||
default:
|
||||
throw new Error(`Unrecognized action "${type}"`);
|
||||
}
|
||||
},
|
||||
[store]
|
||||
);
|
||||
|
||||
const [state, dispatch] = useReducer(reducer, {
|
||||
// Tree
|
||||
baseDepth: 0,
|
||||
numElements: store.numElements,
|
||||
selectedElementIndex: null,
|
||||
selectedElementID: null,
|
||||
|
||||
// Search
|
||||
searchIndex: null,
|
||||
searchResults: [],
|
||||
searchText: '',
|
||||
|
||||
// Owners
|
||||
ownerStack: [],
|
||||
ownerStackIndex: null,
|
||||
_ownerFlatTree: null,
|
||||
});
|
||||
|
||||
const getElementAtIndex = useCallback(
|
||||
(index: number) => {
|
||||
return state._ownerFlatTree === null
|
||||
? store.getElementAtIndex(index)
|
||||
: store.getElementByID(state._ownerFlatTree[index]);
|
||||
},
|
||||
[state]
|
||||
);
|
||||
const selectElementAtIndex = useCallback(
|
||||
(index: number) =>
|
||||
dispatch({ type: 'SELECT_ELEMENT_AT_INDEX', payload: index }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectElementByID = useCallback(
|
||||
(id: number | null) =>
|
||||
dispatch({ type: 'SELECT_ELEMENT_BY_ID', payload: id }),
|
||||
[dispatch]
|
||||
);
|
||||
const setSearchText = useCallback(
|
||||
(text: string) => dispatch({ type: 'SET_SEARCH_TEXT', payload: text }),
|
||||
[dispatch]
|
||||
);
|
||||
const goToNextSearchResult = useCallback(
|
||||
() => dispatch({ type: 'GO_TO_NEXT_SEARCH_RESULT' }),
|
||||
[dispatch]
|
||||
);
|
||||
const goToPreviousSearchResult = useCallback(
|
||||
() => dispatch({ type: 'GO_TO_PREVIOUS_SEARCH_RESULT' }),
|
||||
[dispatch]
|
||||
);
|
||||
const resetOwnerStack = useCallback(
|
||||
() => dispatch({ type: 'RESET_OWNER_STACK' }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectNextElementInTree = useCallback(
|
||||
() => dispatch({ type: 'SELECT_NEXT_ELEMENT_IN_TREE' }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectPreviousElementInTree = useCallback(
|
||||
() => dispatch({ type: 'SELECT_PREVIOUS_ELEMENT_IN_TREE' }),
|
||||
[dispatch]
|
||||
);
|
||||
const selectOwner = useCallback(
|
||||
(id: number) => dispatch({ type: 'SELECT_OWNER', payload: id }),
|
||||
[dispatch]
|
||||
);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
// Tree (derived from Store or owners state)
|
||||
baseDepth: state.baseDepth,
|
||||
numElements: state.numElements,
|
||||
selectedElementID: state.selectedElementID,
|
||||
selectedElementIndex: state.selectedElementIndex,
|
||||
getElementAtIndex,
|
||||
selectElementByID,
|
||||
selectElementAtIndex,
|
||||
selectNextElementInTree,
|
||||
selectPreviousElementInTree,
|
||||
|
||||
// Search
|
||||
searchIndex: state.searchIndex,
|
||||
searchResults: state.searchResults,
|
||||
searchText: state.searchText,
|
||||
setSearchText,
|
||||
goToNextSearchResult,
|
||||
goToPreviousSearchResult,
|
||||
|
||||
// Owners
|
||||
ownerStack: state.ownerStack,
|
||||
ownerStackIndex: state.ownerStackIndex,
|
||||
resetOwnerStack,
|
||||
selectOwner,
|
||||
}),
|
||||
[state]
|
||||
);
|
||||
|
||||
// Mutations to the underlying tree may impact this context (e.g. search results, selection state).
|
||||
useLayoutEffect(() => {
|
||||
const handleStoreMutated = ([
|
||||
addedElementIDs,
|
||||
removedElementIDs,
|
||||
]: Array<Uint32Array>) => {
|
||||
dispatch({
|
||||
type: 'HANDLE_STORE_MUTATION',
|
||||
payload: [addedElementIDs, removedElementIDs],
|
||||
});
|
||||
};
|
||||
|
||||
// TODO Even though we're using layout effect, concurrent rendering may cause us to miss a mutation.
|
||||
// Should the store expose some sort of version number that we could check after mounting?
|
||||
|
||||
store.addListener('mutated', handleStoreMutated);
|
||||
|
||||
return () => store.removeListener('mutated', handleStoreMutated);
|
||||
}, [state, store]);
|
||||
|
||||
return <TreeContext.Provider value={value}>{children}</TreeContext.Provider>;
|
||||
}
|
||||
|
||||
function calculateCurrentOwnerList(
|
||||
store: Store,
|
||||
rootOwnerID: number,
|
||||
elementID: number,
|
||||
ownerList: Array<number>
|
||||
): Array<number> {
|
||||
if (elementID === rootOwnerID) {
|
||||
ownerList.push(elementID);
|
||||
const { children } = ((store.getElementByID(elementID): any): Element);
|
||||
children.forEach(childID =>
|
||||
calculateCurrentOwnerList(store, rootOwnerID, childID, ownerList)
|
||||
);
|
||||
} else {
|
||||
const { children, ownerID } = ((store.getElementByID(
|
||||
elementID
|
||||
): any): Element);
|
||||
if (ownerID === rootOwnerID) {
|
||||
ownerList.push(elementID);
|
||||
children.forEach(childID =>
|
||||
calculateCurrentOwnerList(store, rootOwnerID, childID, ownerList)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return ownerList;
|
||||
}
|
||||
|
||||
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 { TreeContext, TreeContextController };
|
||||
@@ -3,7 +3,6 @@
|
||||
import { createContext } from 'react';
|
||||
|
||||
import type { Bridge } from '../../types';
|
||||
import type { TreeMetadataType } from '../types';
|
||||
|
||||
import Store from '../store';
|
||||
|
||||
@@ -14,9 +13,3 @@ BridgeContext.displayName = 'BridgeContext';
|
||||
export const StoreContext = createContext<Store>(((null: any): Store));
|
||||
// $FlowFixMe displayName is a valid attribute of React$Context
|
||||
StoreContext.displayName = 'StoreContext';
|
||||
|
||||
export const TreeContext = createContext<TreeMetadataType>(
|
||||
((null: any): TreeMetadataType)
|
||||
);
|
||||
// $FlowFixMe displayName is a valid attribute of React$Context
|
||||
TreeContext.displayName = 'TreeMetadataContext';
|
||||
Reference in new issue
Block a user