Restore copy on text
Summary: Some folks were missing the copy as text ManagedTable used to have, so introduced both the options to either copy as text (visible columns or custom copy handler) or as JSON Changelog: It is now possible to both copy as text or as JSON from data tables Reviewed By: jknoxville Differential Revision: D29712096 fbshipit-source-id: 27bd2e869a247bd0896ce2774c08651123fd531d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
1e93055eb5
commit
d23ccfcd44
@@ -57,6 +57,7 @@ test('Correct top level API exposed', () => {
|
||||
"renderReactRoot",
|
||||
"sleep",
|
||||
"styled",
|
||||
"textContent",
|
||||
"theme",
|
||||
"useLocalStorageState",
|
||||
"useLogger",
|
||||
|
||||
@@ -132,6 +132,8 @@ export {
|
||||
|
||||
export {createTablePlugin} from './utils/createTablePlugin';
|
||||
|
||||
export {textContent} from './utils/textContent';
|
||||
|
||||
// It's not ideal that this exists in flipper-plugin sources directly,
|
||||
// but is the least pain for plugin authors.
|
||||
// Probably we should make sure that testing-library doesn't end up in our final Flipper bundle (which packages flipper-plugin)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {CopyOutlined, FilterOutlined} from '@ant-design/icons';
|
||||
import {CopyOutlined, FilterOutlined, TableOutlined} from '@ant-design/icons';
|
||||
import {Checkbox, Menu} from 'antd';
|
||||
import {
|
||||
DataTableDispatch,
|
||||
@@ -20,20 +20,21 @@ import {tryGetFlipperLibImplementation} from '../../plugin/FlipperLib';
|
||||
import {DataTableColumn} from './DataTable';
|
||||
import {toFirstUpper} from '../../utils/toFirstUpper';
|
||||
import {DataSource} from '../../data-source/index';
|
||||
import {renderColumnValue} from './TableRow';
|
||||
import {textContent} from '../../utils/textContent';
|
||||
|
||||
const {Item, SubMenu} = Menu;
|
||||
|
||||
function defaultOnCopyRows<T>(items: T[]) {
|
||||
return JSON.stringify(items.length > 1 ? items : items[0], null, 2);
|
||||
}
|
||||
|
||||
export function tableContextMenuFactory<T>(
|
||||
datasource: DataSource<T>,
|
||||
dispatch: DataTableDispatch<T>,
|
||||
selection: Selection,
|
||||
columns: DataTableColumn<T>[],
|
||||
visibleColumns: DataTableColumn<T>[],
|
||||
onCopyRows: (rows: T[]) => string = defaultOnCopyRows,
|
||||
onCopyRows: (
|
||||
rows: T[],
|
||||
visibleColumns: DataTableColumn<T>[],
|
||||
) => string = defaultOnCopyRows,
|
||||
onContextMenu?: (selection: undefined | T) => React.ReactElement,
|
||||
) {
|
||||
const lib = tryGetFlipperLibImplementation();
|
||||
@@ -68,6 +69,61 @@ export function tableContextMenuFactory<T>(
|
||||
</Item>
|
||||
))}
|
||||
</SubMenu>
|
||||
<SubMenu
|
||||
key="copy rows"
|
||||
title="Copy row(s)"
|
||||
icon={<TableOutlined />}
|
||||
disabled={!hasSelection}>
|
||||
<Item
|
||||
key="copyToClipboard"
|
||||
disabled={!hasSelection}
|
||||
onClick={() => {
|
||||
const items = getSelectedItems(datasource, selection);
|
||||
if (items.length) {
|
||||
lib.writeTextToClipboard(onCopyRows(items, visibleColumns));
|
||||
}
|
||||
}}>
|
||||
Copy row(s)
|
||||
</Item>
|
||||
{lib.isFB && (
|
||||
<Item
|
||||
key="createPaste"
|
||||
disabled={!hasSelection}
|
||||
onClick={() => {
|
||||
const items = getSelectedItems(datasource, selection);
|
||||
if (items.length) {
|
||||
lib.createPaste(onCopyRows(items, visibleColumns));
|
||||
}
|
||||
}}>
|
||||
Create paste
|
||||
</Item>
|
||||
)}
|
||||
<Item
|
||||
key="copyToClipboardJSON"
|
||||
disabled={!hasSelection}
|
||||
onClick={() => {
|
||||
const items = getSelectedItems(datasource, selection);
|
||||
if (items.length) {
|
||||
lib.writeTextToClipboard(rowsToJson(items));
|
||||
}
|
||||
}}>
|
||||
Copy row(s) (JSON)
|
||||
</Item>
|
||||
{lib.isFB && (
|
||||
<Item
|
||||
key="createPaste"
|
||||
disabled={!hasSelection}
|
||||
onClick={() => {
|
||||
const items = getSelectedItems(datasource, selection);
|
||||
if (items.length) {
|
||||
lib.createPaste(rowsToJson(items));
|
||||
}
|
||||
}}>
|
||||
Create paste (JSON)
|
||||
</Item>
|
||||
)}
|
||||
</SubMenu>
|
||||
|
||||
<SubMenu
|
||||
key="copy cells"
|
||||
title="Copy cell(s)"
|
||||
@@ -88,30 +144,6 @@ export function tableContextMenuFactory<T>(
|
||||
</Item>
|
||||
))}
|
||||
</SubMenu>
|
||||
<Item
|
||||
key="copyToClipboard"
|
||||
disabled={!hasSelection}
|
||||
onClick={() => {
|
||||
const items = getSelectedItems(datasource, selection);
|
||||
if (items.length) {
|
||||
lib.writeTextToClipboard(onCopyRows(items));
|
||||
}
|
||||
}}>
|
||||
Copy row(s)
|
||||
</Item>
|
||||
{lib.isFB && (
|
||||
<Item
|
||||
key="createPaste"
|
||||
disabled={!hasSelection}
|
||||
onClick={() => {
|
||||
const items = getSelectedItems(datasource, selection);
|
||||
if (items.length) {
|
||||
lib.createPaste(onCopyRows(items));
|
||||
}
|
||||
}}>
|
||||
Create paste
|
||||
</Item>
|
||||
)}
|
||||
<Menu.Divider />
|
||||
<SubMenu title="Visible columns" key="visible columns">
|
||||
{columns.map((column, idx) => (
|
||||
@@ -143,3 +175,24 @@ function friendlyColumnTitle(column: DataTableColumn<any>): string {
|
||||
const name = column.title || column.key;
|
||||
return toFirstUpper(name);
|
||||
}
|
||||
|
||||
function defaultOnCopyRows<T>(
|
||||
items: T[],
|
||||
visibleColumns: DataTableColumn<T>[],
|
||||
) {
|
||||
return (
|
||||
visibleColumns.map(friendlyColumnTitle).join('\t') +
|
||||
'\n' +
|
||||
items
|
||||
.map((row, idx) =>
|
||||
visibleColumns
|
||||
.map((col) => textContent(renderColumnValue(col, row, true, idx)))
|
||||
.join('\t'),
|
||||
)
|
||||
.join('\n')
|
||||
);
|
||||
}
|
||||
|
||||
function rowsToJson<T>(items: T[]) {
|
||||
return JSON.stringify(items.length > 1 ? items : items[0], null, 2);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
import React, {CSSProperties, memo} from 'react';
|
||||
import styled from '@emotion/styled';
|
||||
import {theme} from '../theme';
|
||||
import type {TableRowRenderContext} from './DataTable';
|
||||
import {DataTableColumn, TableRowRenderContext} from './DataTable';
|
||||
import {Width} from '../../utils/widthUtils';
|
||||
import {DataFormatter} from '../DataFormatter';
|
||||
import {Dropdown} from 'antd';
|
||||
@@ -121,9 +121,12 @@ export const TableRow = memo(function TableRow<T>({
|
||||
{config.columns
|
||||
.filter((col) => col.visible)
|
||||
.map((col) => {
|
||||
const value = col.onRender
|
||||
? (col as any).onRender(record, highlighted, itemIndex)
|
||||
: DataFormatter.format((record as any)[col.key], col.formatters);
|
||||
const value = renderColumnValue<T>(
|
||||
col,
|
||||
record,
|
||||
highlighted,
|
||||
itemIndex,
|
||||
);
|
||||
|
||||
return (
|
||||
<TableBodyColumnContainer
|
||||
@@ -147,3 +150,14 @@ export const TableRow = memo(function TableRow<T>({
|
||||
return row;
|
||||
}
|
||||
});
|
||||
|
||||
export function renderColumnValue<T>(
|
||||
col: DataTableColumn<any>,
|
||||
record: T,
|
||||
highlighted: boolean,
|
||||
itemIndex: number,
|
||||
) {
|
||||
return col.onRender
|
||||
? col.onRender(record, highlighted, itemIndex)
|
||||
: DataFormatter.format((record as any)[col.key], col.formatters);
|
||||
}
|
||||
|
||||
47
desktop/flipper-plugin/src/utils/textContent.tsx
Normal file
47
desktop/flipper-plugin/src/utils/textContent.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {ReactNode, ReactElement} from 'react';
|
||||
|
||||
function isReactElement(object: any) {
|
||||
return (
|
||||
typeof object === 'object' &&
|
||||
object !== null &&
|
||||
object.$$typeof === Symbol.for('react.element')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively walks through all children of a React element and returns
|
||||
* the string representation of the leafs concatenated.
|
||||
*/
|
||||
export function textContent(node: ReactNode): string {
|
||||
let res = '';
|
||||
const traverse = (node: ReactNode) => {
|
||||
if (typeof node === 'string' || typeof node === 'number') {
|
||||
// this is a leaf, add it to the result string
|
||||
res += node;
|
||||
} else if (Array.isArray(node)) {
|
||||
// traverse all array members and recursively stringify them
|
||||
node.forEach(traverse);
|
||||
} else if (isReactElement(node)) {
|
||||
// node is a react element access its children an recursively stringify them
|
||||
const {children} = (node as ReactElement).props;
|
||||
if (Array.isArray(children)) {
|
||||
children.forEach(traverse);
|
||||
} else {
|
||||
traverse(children);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
traverse(node);
|
||||
|
||||
return res;
|
||||
}
|
||||
Reference in New Issue
Block a user