react/packages/react-devtools-shared/src/backend/utils.js

87 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-08-28 01:54:01 +08:00
/**
* 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.
*
* @flow
*/
2019-01-23 03:04:37 +08:00
import {copy} from 'clipboard-js';
2019-08-14 08:58:03 +08:00
import {dehydrate} from '../hydration';
2019-08-14 08:58:03 +08:00
import type {DehydratedData} from 'react-devtools-shared/src/devtools/views/Components/types';
export function cleanForBridge(
data: Object | null,
2019-06-18 05:30:34 +08:00
isPathWhitelisted: (path: Array<string | number>) => boolean,
2019-08-14 08:58:03 +08:00
path?: Array<string | number> = [],
): DehydratedData | null {
if (data !== null) {
const cleanedPaths = [];
const unserializablePaths = [];
const cleanedData = dehydrate(
data,
cleanedPaths,
unserializablePaths,
path,
isPathWhitelisted,
);
return {
data: cleanedData,
cleaned: cleanedPaths,
unserializable: unserializablePaths,
};
} else {
return null;
}
}
export function copyToClipboard(value: any): void {
const safeToCopy = serializeToString(value);
const text = safeToCopy === undefined ? 'undefined' : safeToCopy;
const {clipboardCopyText} = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
// On Firefox navigator.clipboard.writeText has to be called from
// the content script js code (because it requires the clipboardWrite
// permission to be allowed out of a "user handling" callback),
// clipboardCopyText is an helper injected into the page from.
// injectGlobalHook.
if (typeof clipboardCopyText === 'function') {
clipboardCopyText(text).catch(err => {});
} else {
copy(text);
}
}
export function copyWithSet(
obj: Object | Array<any>,
path: Array<string | number>,
value: any,
2019-08-14 08:58:03 +08:00
index: number = 0,
): Object | Array<any> {
if (index >= path.length) {
return value;
}
const key = path[index];
2019-08-14 08:58:03 +08:00
const updated = Array.isArray(obj) ? obj.slice() : {...obj};
// $FlowFixMe number or string is fine here
updated[key] = copyWithSet(obj[key], path, value, index + 1);
return updated;
}
export function serializeToString(data: any): string {
const cache = new Set();
// Use a custom replacer function to protect against circular references.
return JSON.stringify(data, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.has(value)) {
return;
}
cache.add(value);
}
return value;
});
}