mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2025-03-25 21:20:38 +08:00
fix: add circular reference handling for object stringification
This commit is contained in:
parent
552e8ba2dd
commit
c69bbfaf1c
@ -36,6 +36,7 @@ import tern, { Def } from 'tern';
|
||||
import { getTypeString } from '../../../utils/type';
|
||||
import ecmascript from '../../../constants/ecmascript';
|
||||
import { PREVENT_POPOVER_WIDGET_CLOSE_CLASS } from '../../../constants';
|
||||
import { stringify } from '../../../utils/object';
|
||||
|
||||
injectGlobal`
|
||||
.CodeMirror-hints {
|
||||
@ -386,7 +387,7 @@ export const ExpressionEditor = React.forwardRef<
|
||||
<Box fontWeight="bold" marginBottom="4px">
|
||||
{error ? 'Error' : getTypeString(evaledValue?.value)}
|
||||
</Box>
|
||||
{error || JSON.stringify(evaledValue?.value, null, 2)}
|
||||
{error || stringify(evaledValue?.value)}
|
||||
</Box>
|
||||
);
|
||||
|
||||
|
17
packages/editor-sdk/src/utils/object.ts
Normal file
17
packages/editor-sdk/src/utils/object.ts
Normal file
@ -0,0 +1,17 @@
|
||||
function stringify(obj: unknown) {
|
||||
const cache: unknown[] = [];
|
||||
|
||||
JSON.stringify(obj, (_, value) => {
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
// Duplicate reference found, discard key
|
||||
if (cache.includes(value)) return;
|
||||
|
||||
// Store value in our collection
|
||||
cache.push(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
});
|
||||
}
|
||||
|
||||
export { stringify };
|
Loading…
x
Reference in New Issue
Block a user