@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
132 lines (131 loc) • 6.62 kB
JavaScript
'use client';
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["children", "actionIconSize", "fullFeatured", "copyable", "showLanguage", "language", "style", "variant", "shadow", "enablePanZoom", "defaultExpand", "className", "bodyRender", "fileName", "actionsRender", "theme"];
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; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { cva } from 'class-variance-authority';
import { memo, useMemo } from 'react';
import { Flexbox } from 'react-layout-kit';
import CopyButton from "../CopyButton";
import { useStyles } from "../Highlighter/style";
import Tag from "../Tag";
import FullFeatured from "./FullFeatured";
import SyntaxMermaid from "./SyntaxMermaid";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var Mermaid = /*#__PURE__*/memo(function (_ref) {
var children = _ref.children,
actionIconSize = _ref.actionIconSize,
fullFeatured = _ref.fullFeatured,
_ref$copyable = _ref.copyable,
copyable = _ref$copyable === void 0 ? true : _ref$copyable,
_ref$showLanguage = _ref.showLanguage,
showLanguage = _ref$showLanguage === void 0 ? true : _ref$showLanguage,
_ref$language = _ref.language,
language = _ref$language === void 0 ? 'mermaid' : _ref$language,
style = _ref.style,
_ref$variant = _ref.variant,
variant = _ref$variant === void 0 ? 'filled' : _ref$variant,
shadow = _ref.shadow,
_ref$enablePanZoom = _ref.enablePanZoom,
enablePanZoom = _ref$enablePanZoom === void 0 ? true : _ref$enablePanZoom,
_ref$defaultExpand = _ref.defaultExpand,
defaultExpand = _ref$defaultExpand === void 0 ? true : _ref$defaultExpand,
className = _ref.className,
bodyRender = _ref.bodyRender,
fileName = _ref.fileName,
actionsRender = _ref.actionsRender,
theme = _ref.theme,
rest = _objectWithoutProperties(_ref, _excluded);
var _useStyles = useStyles(),
cx = _useStyles.cx,
styles = _useStyles.styles;
var variants = useMemo(function () {
return cva(styles.root, {
defaultVariants: {
shadow: false,
variant: 'filled',
wrap: false
},
/* eslint-disable sort-keys-fix/sort-keys-fix */
variants: {
variant: {
filled: styles.filled,
outlined: styles.outlined,
borderless: styles.borderless
},
shadow: {
false: null,
true: styles.shadow
},
wrap: {
false: styles.nowrap,
true: null
}
}
/* eslint-enable sort-keys-fix/sort-keys-fix */
});
}, [styles]);
var tirmedChildren = children.trim();
var originalActions = copyable && /*#__PURE__*/_jsx(CopyButton, {
content: tirmedChildren,
size: actionIconSize
});
var actions = actionsRender ? actionsRender({
actionIconSize: actionIconSize,
content: children,
originalNode: originalActions
}) : originalActions;
var defaultBody = /*#__PURE__*/_jsx(SyntaxMermaid, {
enablePanZoom: enablePanZoom,
theme: theme,
variant: variant,
children: tirmedChildren
});
var body = bodyRender ? bodyRender({
content: tirmedChildren,
originalNode: defaultBody
}) : defaultBody;
if (fullFeatured) return /*#__PURE__*/_jsx(FullFeatured, _objectSpread(_objectSpread({
actionsRender: actionsRender,
className: className,
content: tirmedChildren,
copyable: copyable,
defaultExpand: defaultExpand,
fileName: fileName,
language: language,
shadow: shadow,
showLanguage: showLanguage,
style: style,
variant: variant
}, rest), {}, {
children: body
}));
return /*#__PURE__*/_jsxs("div", _objectSpread(_objectSpread({
className: cx(variants({
shadow: shadow,
variant: variant
}), className),
"data-code-type": "mermaid",
style: style
}, rest), {}, {
children: [/*#__PURE__*/_jsx(Flexbox, {
align: 'center',
className: styles.actions,
flex: 'none',
gap: 4,
horizontal: true,
children: actions
}), showLanguage && /*#__PURE__*/_jsx(Tag, {
className: styles.lang,
children: language.toLowerCase()
}), body]
}));
});
Mermaid.displayName = 'Mermaid';
export default Mermaid;