@yuntijs/ui
Version:
☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps
120 lines • 5.55 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["variant", "fullFeatured", "title", "icon", "contentStyle", "classNames", "className", "collapseStringMode", "collapsed", "dark", "displayArrayIndex", "displaySize", "ignoreLargeArray", "matchesURL", "src", "theme", "style"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { ActionIcon, CopyButton } from '@lobehub/ui';
import { Flex } from 'antd';
import { useThemeMode } from 'antd-style';
import { ChevronDown, ChevronRight } from 'lucide-react';
import React, { useMemo, useState } from 'react';
import JsonView, { stringify } from 'react18-json-view';
import 'react18-json-view/src/dark.css';
import 'react18-json-view/src/style.css';
import Typography from "../Typography";
import { useStyles } from "./style";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export { stringify } from 'react18-json-view';
var Text = Typography.Text;
export var JsonViewer = function JsonViewer(_ref) {
var _ref$variant = _ref.variant,
variant = _ref$variant === void 0 ? 'filled' : _ref$variant,
_ref$fullFeatured = _ref.fullFeatured,
fullFeatured = _ref$fullFeatured === void 0 ? false : _ref$fullFeatured,
_ref$title = _ref.title,
title = _ref$title === void 0 ? 'json' : _ref$title,
icon = _ref.icon,
contentStyle = _ref.contentStyle,
classNames = _ref.classNames,
className = _ref.className,
_ref$collapseStringMo = _ref.collapseStringMode,
collapseStringMode = _ref$collapseStringMo === void 0 ? 'directly' : _ref$collapseStringMo,
_ref$collapsed = _ref.collapsed,
collapsed = _ref$collapsed === void 0 ? 2 : _ref$collapsed,
dark = _ref.dark,
_ref$displayArrayInde = _ref.displayArrayIndex,
displayArrayIndex = _ref$displayArrayInde === void 0 ? false : _ref$displayArrayInde,
_ref$displaySize = _ref.displaySize,
displaySize = _ref$displaySize === void 0 ? 'collapsed' : _ref$displaySize,
_ref$ignoreLargeArray = _ref.ignoreLargeArray,
ignoreLargeArray = _ref$ignoreLargeArray === void 0 ? false : _ref$ignoreLargeArray,
_ref$matchesURL = _ref.matchesURL,
matchesURL = _ref$matchesURL === void 0 ? true : _ref$matchesURL,
src = _ref.src,
_ref$theme = _ref.theme,
theme = _ref$theme === void 0 ? 'default' : _ref$theme,
style = _ref.style,
props = _objectWithoutProperties(_ref, _excluded);
var _useThemeMode = useThemeMode(),
isDarkMode = _useThemeMode.isDarkMode;
var _useState = useState(true),
_useState2 = _slicedToArray(_useState, 2),
expand = _useState2[0],
setExpand = _useState2[1];
var _useStyles = useStyles({
expand: expand,
variant: fullFeatured ? 'filled' : variant
}),
styles = _useStyles.styles,
cx = _useStyles.cx;
var jsonString = useMemo(function () {
return stringify(src);
}, [src]);
var JsonViewEle = /*#__PURE__*/_jsx(JsonView, _objectSpread({
collapseStringMode: collapseStringMode,
collapsed: collapsed,
dark: dark !== null && dark !== void 0 ? dark : isDarkMode,
displayArrayIndex: displayArrayIndex,
displaySize: displaySize,
ignoreLargeArray: ignoreLargeArray,
matchesURL: matchesURL,
src: src,
theme: theme
}, props));
return /*#__PURE__*/_jsxs(Flex, {
className: cx(styles.container, className),
style: style,
vertical: true,
children: [fullFeatured && /*#__PURE__*/_jsxs(Flex, {
align: 'center',
className: cx(styles.header, classNames === null || classNames === void 0 ? void 0 : classNames.header),
justify: 'space-between',
children: [/*#__PURE__*/_jsx(ActionIcon, {
icon: expand ? ChevronDown : ChevronRight,
onClick: function onClick() {
return setExpand(!expand);
},
size: "small"
}), /*#__PURE__*/_jsxs(Flex, {
align: 'center',
className: styles.title,
flex: 1,
gap: 2,
justify: "center",
title: title,
children: [icon, /*#__PURE__*/_jsx(Flex, {
className: styles.titleText,
children: /*#__PURE__*/_jsx(Text, {
ellipsis: true,
children: title
})
})]
}), /*#__PURE__*/_jsx(CopyButton, {
content: jsonString,
size: "small"
})]
}), /*#__PURE__*/_jsx(Flex, {
className: cx(styles.content, classNames === null || classNames === void 0 ? void 0 : classNames.content),
flex: 1,
style: expand ? contentStyle : _objectSpread(_objectSpread({}, contentStyle), {}, {
height: 0,
maxHeight: 0,
padding: 0,
overflow: 'hidden'
}),
children: JsonViewEle
})]
});
};