Add 'Copy expanded child elements' to Layout Inspector

Summary:
As requested in https://fburl.com/dpuz2cew

This diff adds a new popup menu option to copy the info of all the expanded elements in a view/component tree.

It does it by moving the copy function to the layer where all elements are available.

The diff also replaces `Copy` to a call to the same function.

Reviewed By: muraziz

Differential Revision: D23757826

fbshipit-source-id: 3f70c85267f928f7153db75ed8f4eaa3fac669e7
This commit is contained in:
Paco Estevez Garcia
2020-09-17 09:38:20 -07:00
committed by Facebook GitHub Bot
parent 2fbd5f6576
commit 7307ed7014

View File

@@ -221,6 +221,7 @@ type ElementsRowProps = {
| ((key: ElementID | undefined | null) => void) | ((key: ElementID | undefined | null) => void)
| undefined | undefined
| null; | null;
onCopyExpandedTree: (key: Element, maxDepth: number) => string;
style?: Object; style?: Object;
contextMenuExtensions: Array<ContextMenuExtension>; contextMenuExtensions: Array<ContextMenuExtension>;
decorateRow?: DecorateRow; decorateRow?: DecorateRow;
@@ -249,13 +250,14 @@ class ElementsRow extends PureComponent<ElementsRowProps, ElementsRowState> {
{ {
label: 'Copy', label: 'Copy',
click: () => { click: () => {
const attrs = props.element.attributes.reduce( clipboard.writeText(props.onCopyExpandedTree(props.element, 0));
(acc, val) => acc + ` ${val.name}=${val.value}`,
'',
);
clipboard.writeText(`${props.element.name}${attrs}`);
}, },
}, },
{
label: 'Copy expanded child elements',
click: () =>
clipboard.writeText(props.onCopyExpandedTree(props.element, 255)),
},
{ {
label: props.element.expanded ? 'Collapse' : 'Expand', label: props.element.expanded ? 'Collapse' : 'Expand',
click: () => { click: () => {
@@ -637,6 +639,7 @@ export class Elements extends PureComponent<ElementsProps, ElementsState> {
searchResults, searchResults,
contextMenuExtensions, contextMenuExtensions,
decorateRow, decorateRow,
elements,
} = this.props; } = this.props;
const {flatElements} = this.state; const {flatElements} = this.state;
@@ -654,6 +657,30 @@ export class Elements extends PureComponent<ElementsProps, ElementsState> {
if (this.props.alternateRowColor) { if (this.props.alternateRowColor) {
isEven = index % 2 === 0; isEven = index % 2 === 0;
} }
const onCopyExpandedTree = (
maxDepth: number,
element: Element,
depth: number,
): string => {
const shouldIncludeChildren = element.expanded && depth < maxDepth;
const children = shouldIncludeChildren
? element.children.map((childId) => {
const childElement = elements[childId];
return childElement == null
? ''
: onCopyExpandedTree(maxDepth, childElement, depth + 1);
})
: [];
const childrenValue = children.toString().replace(',', '');
const indentation = depth === 0 ? '' : '\n'.padEnd(depth * 2 + 1, ' ');
const attrs = element.attributes.reduce(
(acc, val) => acc + ` ${val.name}=${val.value}`,
'',
);
return `${indentation}${element.name}${attrs}${childrenValue}`;
};
return ( return (
<ElementsRow <ElementsRow
@@ -666,6 +693,9 @@ export class Elements extends PureComponent<ElementsProps, ElementsState> {
onElementHovered && onElementHovered(key); onElementHovered && onElementHovered(key);
}} }}
onElementSelected={onElementSelected} onElementSelected={onElementSelected}
onCopyExpandedTree={(element, maxDepth) =>
onCopyExpandedTree(maxDepth, element, 0)
}
selected={selected === row.key} selected={selected === row.key}
focused={focused === row.key} focused={focused === row.key}
matchingSearchQuery={ matchingSearchQuery={