Move DataDescription utility to flipper-plugin

Summary:
Another utility component used by DataInspector.

Colors have been hardcoded for now, to decouple from the classic Flipper color palette. Will organize this better later in this stack, and when addressing / adding support for dark mode.

Buttons, checkboxes, selects have been replaced by Antd's counterpart.

Unlike ManagedDataTable, the DataInspector is primarily moved rather than copied & adapted, as the underlying abstraction / API won't change significantly. So the changes here will immediately affect all plugins in Flipper using this component.

Reviewed By: passy

Differential Revision: D27603126

fbshipit-source-id: bacd48c9af2b591033e7f2352627f11acb4df589
This commit is contained in:
Michel Weststrate
2021-04-07 07:52:47 -07:00
committed by Facebook GitHub Bot
parent b6674bf96b
commit 9030a98c6e
12 changed files with 181 additions and 153 deletions

View File

@@ -0,0 +1,67 @@
/**
* 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
*/
export function parseColor(
val: string | number,
):
| {
r: number;
g: number;
b: number;
a: number;
}
| undefined
| null {
if (typeof val === 'number') {
const a = ((val >> 24) & 0xff) / 255;
const r = (val >> 16) & 0xff;
const g = (val >> 8) & 0xff;
const b = val & 0xff;
return {a, b, g, r};
}
if (typeof val !== 'string') {
return;
}
if (val[0] !== '#') {
return;
}
// remove leading hash
val = val.slice(1);
// only allow RGB and ARGB hex values
if (val.length !== 3 && val.length !== 6 && val.length !== 8) {
return;
}
// split every 2 characters
const parts = val.match(/.{1,2}/g);
if (!parts) {
return;
}
// get the alpha value
let a = 1;
// extract alpha if passed AARRGGBB
if (val.length === 8) {
a = parseInt(parts.shift() || '0', 16) / 255;
}
const size = val.length;
const [r, g, b] = parts.map((num) => {
if (size === 3) {
return parseInt(num + num, 16);
} else {
return parseInt(num, 16);
}
});
return {a, b, g, r};
}