@lobehub/ui 1.170.0 → 1.170.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,6 +60,7 @@ export interface ActionIconProps extends LucideIconProps, FlexboxProps {
60
60
  * @default 0.5
61
61
  */
62
62
  tooltipDelay?: number;
63
+ variant?: 'default' | 'block' | 'ghost';
63
64
  }
64
65
  declare const ActionIcon: import("react").ForwardRefExoticComponent<ActionIconProps & import("react").RefAttributes<HTMLDivElement>>;
65
66
  export default ActionIcon;
@@ -1,7 +1,7 @@
1
1
  'use client';
2
2
 
3
3
  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); }
4
- var _excluded = ["color", "fill", "className", "active", "icon", "size", "style", "glass", "title", "placement", "arrow", "spotlight", "onClick", "children", "loading", "tooltipDelay", "fillOpacity", "fillRule", "focusable", "disable", "spin", "styles"];
4
+ var _excluded = ["color", "fill", "className", "active", "icon", "size", "variant", "style", "glass", "title", "placement", "arrow", "spotlight", "onClick", "children", "loading", "tooltipDelay", "fillOpacity", "fillRule", "focusable", "disable", "spin", "styles"];
5
5
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
6
6
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7
7
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
@@ -33,6 +33,8 @@ var ActionIcon = /*#__PURE__*/forwardRef(function (_ref, ref) {
33
33
  icon = _ref.icon,
34
34
  _ref$size = _ref.size,
35
35
  size = _ref$size === void 0 ? 'normal' : _ref$size,
36
+ _ref$variant = _ref.variant,
37
+ variant = _ref$variant === void 0 ? 'default' : _ref$variant,
36
38
  style = _ref.style,
37
39
  glass = _ref.glass,
38
40
  title = _ref.title,
@@ -54,7 +56,8 @@ var ActionIcon = /*#__PURE__*/forwardRef(function (_ref, ref) {
54
56
  rest = _objectWithoutProperties(_ref, _excluded);
55
57
  var _useStyles = useStyles({
56
58
  active: Boolean(active),
57
- glass: Boolean(glass)
59
+ glass: Boolean(glass),
60
+ variant: variant
58
61
  }),
59
62
  s = _useStyles.styles,
60
63
  cx = _useStyles.cx;
@@ -1,6 +1,7 @@
1
1
  export declare const useStyles: (props?: {
2
2
  active: boolean;
3
3
  glass: boolean;
4
+ variant: 'default' | 'block' | 'ghost';
4
5
  } | undefined) => import("antd-style").ReturnStyles<{
5
6
  block: string;
6
7
  disabled: import("antd-style").SerializedStyles;
@@ -1,4 +1,4 @@
1
- var _templateObject, _templateObject2, _templateObject3, _templateObject4;
1
+ var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5;
2
2
  function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
3
3
  import { createStyles } from 'antd-style';
4
4
  export var useStyles = createStyles(function (_ref, _ref2) {
@@ -7,11 +7,14 @@ export var useStyles = createStyles(function (_ref, _ref2) {
7
7
  stylish = _ref.stylish,
8
8
  cx = _ref.cx;
9
9
  var active = _ref2.active,
10
- glass = _ref2.glass;
10
+ glass = _ref2.glass,
11
+ variant = _ref2.variant;
12
+ var isBlock = variant === 'block';
13
+ var isGhost = variant === 'ghost';
11
14
  return {
12
- block: cx(glass && stylish.blur, css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n position: relative;\n\n flex: none;\n\n color: ", ";\n\n background: ", ";\n\n transition:\n color 600ms ", ",\n scale 400ms ", ",\n background-color 100ms ", ";\n "])), active ? token.colorText : token.colorTextTertiary, active ? token.colorFillTertiary : 'transparent', token.motionEaseOut, token.motionEaseOut, token.motionEaseOut)),
13
- disabled: css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n cursor: not-allowed;\n opacity: 0.5;\n "]))),
14
- icon: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n transition: scale 400ms ", ";\n\n &:active {\n scale: 0.8;\n }\n "])), token.motionEaseOut),
15
- normal: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n cursor: pointer;\n\n &:hover {\n color: ", ";\n background-color: ", ";\n }\n\n &:active {\n color: ", ";\n background-color: ", ";\n }\n "])), token.colorText, token.colorFillSecondary, token.colorText, token.colorFill)
15
+ block: cx(glass && stylish.blur, isGhost && css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n border: 1px solid ", ";\n "])), token.colorBorderSecondary), css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n position: relative;\n\n flex: none;\n\n color: ", ";\n\n background: ", ";\n\n transition:\n color 600ms ", ",\n scale 400ms ", ",\n background-color 100ms ", ";\n "])), active ? token.colorText : token.colorTextTertiary, active || isBlock ? token.colorFillTertiary : 'transparent', token.motionEaseOut, token.motionEaseOut, token.motionEaseOut)),
16
+ disabled: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n cursor: not-allowed;\n opacity: 0.5;\n "]))),
17
+ icon: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n transition: scale 400ms ", ";\n\n &:active {\n scale: 0.8;\n }\n "])), token.motionEaseOut),
18
+ normal: css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n cursor: pointer;\n\n &:hover {\n color: ", ";\n background-color: ", ";\n }\n\n &:active {\n color: ", ";\n background-color: ", ";\n }\n "])), token.colorText, token.colorFillSecondary, token.colorText, token.colorFill)
16
19
  };
17
20
  });
@@ -28,7 +28,7 @@ export var useStyles = createStyles(function (_ref) {
28
28
  blockStyle: css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n background: ", ";\n border: none;\n border-radius: ", "px;\n "])), token.colorFillQuaternary, token.borderRadiusLG),
29
29
  defaultStyle: css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n background: ", ";\n border: 1px solid ", ";\n border-radius: ", "px;\n "])), token.colorFillQuaternary, token.colorBorderSecondary, token.borderRadiusLG),
30
30
  flatGroup: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n overflow: hidden;\n padding-inline: 16px;\n "]))),
31
- ghostStyle: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n background: transparent;\n border: 1px solid ", ";\n "])), token.colorBorderSecondary),
31
+ ghostStyle: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n background: transparent;\n border: 1px solid ", ";\n border-radius: ", "px;\n "])), token.colorBorderSecondary, token.borderRadiusLG),
32
32
  icon: css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n transition: all 100ms ", ";\n "])), token.motionEaseOut),
33
33
  mobileFlatGroup: css(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["\n border-radius: 0;\n "]))),
34
34
  mobileGroupBody: css(_templateObject7 || (_templateObject7 = _taggedTemplateLiteral(["\n padding-block: 0;\n padding-inline: 16px;\n background: ", ";\n "])), token.colorBgContainer),
@@ -1,5 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { FormItemProps as AntdFormItemProps } from 'antd';
3
+ import { FormVariant } from "./FormGroup";
3
4
  import { type FormTitleProps } from './FormTitle';
4
5
  export declare const useStyles: (props?: {
5
6
  minWidth?: string | number | undefined;
@@ -16,6 +17,7 @@ export interface FormItemProps extends AntdFormItemProps {
16
17
  hidden?: boolean;
17
18
  minWidth?: string | number;
18
19
  tag?: FormTitleProps['tag'];
20
+ variant?: FormVariant;
19
21
  }
20
22
  declare const FormItem: import("react").NamedExoticComponent<FormItemProps>;
21
23
  export default FormItem;
@@ -1,7 +1,7 @@
1
1
  'use client';
2
2
 
3
3
  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); }
4
- var _excluded = ["desc", "tag", "minWidth", "avatar", "className", "label", "children", "divider", "layout"];
4
+ var _excluded = ["desc", "tag", "minWidth", "avatar", "className", "label", "children", "divider", "layout", "variant"];
5
5
  var _templateObject, _templateObject2, _templateObject3, _templateObject4;
6
6
  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; }
7
7
  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; }
@@ -43,6 +43,7 @@ var FormItem = /*#__PURE__*/memo(function (_ref3) {
43
43
  children = _ref3.children,
44
44
  divider = _ref3.divider,
45
45
  layout = _ref3.layout,
46
+ variant = _ref3.variant,
46
47
  rest = _objectWithoutProperties(_ref3, _excluded);
47
48
  var _useStyles = useStyles({
48
49
  minWidth: minWidth
@@ -51,7 +52,11 @@ var FormItem = /*#__PURE__*/memo(function (_ref3) {
51
52
  styles = _useStyles.styles;
52
53
  var isVertical = layout === 'vertical';
53
54
  return /*#__PURE__*/_jsxs(_Fragment, {
54
- children: [divider && /*#__PURE__*/_jsx(FormDivider, {}), /*#__PURE__*/_jsx(Item, _objectSpread(_objectSpread({
55
+ children: [divider && /*#__PURE__*/_jsx(FormDivider, {
56
+ style: {
57
+ opacity: variant === 'pure' ? 0 : undefined
58
+ }
59
+ }), /*#__PURE__*/_jsx(Item, _objectSpread(_objectSpread({
55
60
  className: cx(styles.item, Boolean(minWidth) && styles.itemMinWidth, !divider && styles.itemNoDivider, isVertical && styles.verticalLayout, className),
56
61
  label: /*#__PURE__*/_jsx(FormTitle, {
57
62
  avatar: avatar,
@@ -73,19 +73,20 @@ var FormSubmitFooter = /*#__PURE__*/memo(function (_ref2) {
73
73
  var v = merge({}, initialValues, values);
74
74
  setHasUnsavedChanges(!isEqual(v, initialValues));
75
75
  }, [values, initialValues, submitLoading]);
76
+ var fn = function fn(e) {
77
+ if (hasUnsavedChanges) {
78
+ e.returnValue = (texts === null || texts === void 0 ? void 0 : texts.unSavedWarning) || 'You have unsaved changes. Are you sure you want to leave?';
79
+ } else {
80
+ delete e.returnValue;
81
+ }
82
+ };
76
83
  useEffect(function () {
77
84
  if (!enableUnsavedWarning) return;
78
85
  if (typeof window === 'undefined' || !hasUnsavedChanges) return;
79
86
  // 添加离开页面的提示
80
- window.addEventListener('beforeunload', function (e) {
81
- if (hasUnsavedChanges) {
82
- e.preventDefault();
83
- e.returnValue = (texts === null || texts === void 0 ? void 0 : texts.unSavedWarning) || 'You have unsaved changes. Are you sure you want to leave?';
84
- }
85
- });
87
+ window.addEventListener('beforeunload', fn);
86
88
  return function () {
87
- // eslint-disable-next-line unicorn/no-invalid-remove-event-listener
88
- window.removeEventListener('beforeunload', function () {});
89
+ return window.removeEventListener('beforeunload', fn);
89
90
  };
90
91
  }, [enableUnsavedWarning, hasUnsavedChanges]);
91
92
  var content = /*#__PURE__*/_jsxs(_Fragment, {
package/es/Form/index.js CHANGED
@@ -64,7 +64,8 @@ var FormParent = /*#__PURE__*/forwardRef(function (_ref, ref) {
64
64
  var mapFlat = function mapFlat(item, itemIndex) {
65
65
  return /*#__PURE__*/_jsx(FormItem, _objectSpread({
66
66
  divider: itemIndex !== 0,
67
- minWidth: itemMinWidth
67
+ minWidth: itemMinWidth,
68
+ variant: variant
68
69
  }, item), itemIndex);
69
70
  };
70
71
  var mapTree = function mapTree(group, groupIndex) {
@@ -3,7 +3,7 @@ import { Loader2 } from 'lucide-react';
3
3
  import { memo, useId } from 'react';
4
4
  import { Center, Flexbox } from 'react-layout-kit';
5
5
  import Icon from "../../Icon";
6
- import { useMermaid, useMermaidInit } from "../../hooks/useMermaid";
6
+ import { useMermaid } from "../../hooks/useMermaid";
7
7
  import { useStyles } from "./style";
8
8
  import { jsx as _jsx } from "react/jsx-runtime";
9
9
  import { Fragment as _Fragment } from "react/jsx-runtime";
@@ -15,7 +15,6 @@ var MermaidContainer = /*#__PURE__*/memo(function (_ref) {
15
15
  styles = _useStyles.styles;
16
16
  var id = useId();
17
17
  var mermaidId = kebabCase("mermaid-".concat(id));
18
- useMermaidInit();
19
18
  var _useMermaid = useMermaid(mermaidId, children),
20
19
  data = _useMermaid.data,
21
20
  isLoading = _useMermaid.isLoading;
@@ -4,5 +4,5 @@ import { createStyles } from 'antd-style';
4
4
  export var useStyles = createStyles(function (_ref) {
5
5
  var css = _ref.css,
6
6
  token = _ref.token;
7
- return css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n width: 64px;\n height: 100%;\n min-height: 640px;\n padding-block: 16px;\n padding-inline: 0;\n\n background: ", ";\n border-inline-end: 1px solid ", ";\n "])), token.colorBgContainer, token.colorBorder);
7
+ return css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n width: 64px;\n height: 100%;\n min-height: 640px;\n padding-block: 16px;\n padding-inline: 0;\n\n background: ", ";\n border-inline-end: 1px solid ", ";\n "])), token.colorBgContainer, token.colorBorderSecondary);
8
8
  });
@@ -1,10 +1,9 @@
1
1
  import { SWRResponse } from 'swr';
2
2
  export declare const FALLBACK_LANG = "txt";
3
- declare const highlightCache: Map<string, string>;
4
3
  declare const MD5_LENGTH_THRESHOLD = 10000;
5
4
  declare const loadShiki: () => Promise<(code: string, options: import("@shikijs/types").CodeToHastOptions<import("shiki/dist/langs.mjs").BundledLanguage, import("shiki/dist/themes.mjs").BundledTheme>) => Promise<string>>;
6
5
  declare const shikiPromise: Promise<(code: string, options: import("@shikijs/types").CodeToHastOptions<import("shiki/dist/langs.mjs").BundledLanguage, import("shiki/dist/themes.mjs").BundledTheme>) => Promise<string>>;
7
6
  declare const escapeHtml: (str: string) => string;
8
7
  export declare const useHighlight: (text: string, lang: string, enableTransformer?: boolean) => SWRResponse<string, Error>;
9
8
  export { default as languageMap } from './languageMap';
10
- export { escapeHtml, highlightCache, loadShiki, MD5_LENGTH_THRESHOLD, shikiPromise };
9
+ export { escapeHtml, loadShiki, MD5_LENGTH_THRESHOLD, shikiPromise };
@@ -11,7 +11,6 @@ import languageMap from "./languageMap";
11
11
  export var FALLBACK_LANG = 'txt';
12
12
 
13
13
  // 应用级缓存,避免重复计算
14
- var highlightCache = new Map();
15
14
  var MD5_LENGTH_THRESHOLD = 10000; // 超过该长度使用异步MD5
16
15
 
17
16
  // 颜色替换映射类型
@@ -88,59 +87,49 @@ export var useHighlight = function useHighlight(text, lang, enableTransformer) {
88
87
  return _regeneratorRuntime().wrap(function _callee$(_context) {
89
88
  while (1) switch (_context.prev = _context.next) {
90
89
  case 0:
91
- if (!(cacheKey && highlightCache.has(cacheKey))) {
92
- _context.next = 2;
93
- break;
94
- }
95
- return _context.abrupt("return", highlightCache.get(cacheKey));
96
- case 2:
97
- _context.prev = 2;
98
- _context.next = 5;
90
+ _context.prev = 0;
91
+ _context.next = 3;
99
92
  return shikiPromise;
100
- case 5:
93
+ case 3:
101
94
  codeToHtml = _context.sent;
102
- _context.next = 8;
95
+ _context.next = 6;
103
96
  return codeToHtml(text, {
104
97
  colorReplacements: colorReplacements,
105
98
  lang: matchedLanguage,
106
99
  theme: isDarkMode ? 'slack-dark' : 'slack-ochin',
107
100
  transformers: transformers
108
101
  });
109
- case 8:
102
+ case 6:
110
103
  html = _context.sent;
111
- // 缓存结果
112
- if (cacheKey) highlightCache.set(cacheKey, html);
113
104
  return _context.abrupt("return", html);
114
- case 13:
115
- _context.prev = 13;
116
- _context.t0 = _context["catch"](2);
105
+ case 10:
106
+ _context.prev = 10;
107
+ _context.t0 = _context["catch"](0);
117
108
  console.error('高级渲染失败:', _context.t0);
118
- _context.prev = 16;
119
- _context.next = 19;
109
+ _context.prev = 13;
110
+ _context.next = 16;
120
111
  return shikiPromise;
121
- case 19:
112
+ case 16:
122
113
  _codeToHtml = _context.sent;
123
- _context.next = 22;
114
+ _context.next = 19;
124
115
  return _codeToHtml(text, {
125
116
  lang: matchedLanguage,
126
117
  theme: isDarkMode ? 'dark-plus' : 'light-plus'
127
118
  });
128
- case 22:
119
+ case 19:
129
120
  _html = _context.sent;
130
- if (cacheKey) highlightCache.set(cacheKey, _html);
131
121
  return _context.abrupt("return", _html);
132
- case 27:
133
- _context.prev = 27;
134
- _context.t1 = _context["catch"](16);
122
+ case 23:
123
+ _context.prev = 23;
124
+ _context.t1 = _context["catch"](13);
135
125
  // 最终降级到纯文本
136
126
  fallbackHtml = "<pre class=\"fallback\"><code>".concat(escapeHtml(text), "</code></pre>");
137
- if (cacheKey) highlightCache.set(cacheKey, fallbackHtml);
138
127
  return _context.abrupt("return", fallbackHtml);
139
- case 32:
128
+ case 27:
140
129
  case "end":
141
130
  return _context.stop();
142
131
  }
143
- }, _callee, null, [[2, 13], [16, 27]]);
132
+ }, _callee, null, [[0, 10], [13, 23]]);
144
133
  })), {
145
134
  dedupingInterval: 3000,
146
135
  // 3秒内相同请求只执行一次
@@ -151,4 +140,4 @@ export var useHighlight = function useHighlight(text, lang, enableTransformer) {
151
140
  });
152
141
  };
153
142
  export { default as languageMap } from "./languageMap";
154
- export { escapeHtml, highlightCache, loadShiki, MD5_LENGTH_THRESHOLD, shikiPromise };
143
+ export { escapeHtml, loadShiki, MD5_LENGTH_THRESHOLD, shikiPromise };
@@ -1,9 +1,9 @@
1
1
  import { SWRResponse } from 'swr';
2
- /**
3
- * 验证并处理 Mermaid 图表内容
4
- */
5
- export declare const useMermaid: (id: string, content: string) => SWRResponse<string, Error>;
6
2
  /**
7
3
  * 初始化 Mermaid 配置
8
4
  */
9
5
  export declare const useMermaidInit: () => void;
6
+ /**
7
+ * 验证并处理 Mermaid 图表内容
8
+ */
9
+ export declare const useMermaid: (id: string, content: string) => SWRResponse<string, Error>;
@@ -1,20 +1,19 @@
1
1
  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); }
2
- function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
2
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
6
3
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7
4
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
5
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
9
6
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
10
7
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
8
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
10
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
11
11
  import { useTheme } from 'antd-style';
12
12
  import { useEffect, useMemo, useState } from 'react';
13
13
  import useSWR from 'swr';
14
14
  import { Md5 } from 'ts-md5';
15
15
 
16
16
  // 缓存已验证的图表内容
17
- var mermaidCache = new Map();
18
17
  var MD5_LENGTH_THRESHOLD = 10000;
19
18
 
20
19
  // 懒加载 mermaid 实例
@@ -26,84 +25,6 @@ var loadMermaid = function loadMermaid() {
26
25
  };
27
26
  var mermaidPromise = loadMermaid();
28
27
 
29
- /**
30
- * 验证并处理 Mermaid 图表内容
31
- */
32
- export var useMermaid = function useMermaid(id, content) {
33
- // 用于存储最近一次有效的内容
34
- var _useState = useState(''),
35
- _useState2 = _slicedToArray(_useState, 2),
36
- validContent = _useState2[0],
37
- setValidContent = _useState2[1];
38
-
39
- // 为长内容生成哈希键
40
- var cacheKey = useMemo(function () {
41
- var hash = content.length < MD5_LENGTH_THRESHOLD ? content : Md5.hashStr(content);
42
- return "mermaid-".concat(id, "-").concat(hash);
43
- }, [content, id]);
44
- return useSWR(cacheKey, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
45
- var mermaidInstance, isValid, _yield$mermaidInstanc, svg;
46
- return _regeneratorRuntime().wrap(function _callee$(_context) {
47
- while (1) switch (_context.prev = _context.next) {
48
- case 0:
49
- if (!mermaidCache.has(cacheKey)) {
50
- _context.next = 2;
51
- break;
52
- }
53
- return _context.abrupt("return", validContent);
54
- case 2:
55
- _context.prev = 2;
56
- _context.next = 5;
57
- return mermaidPromise;
58
- case 5:
59
- mermaidInstance = _context.sent;
60
- if (mermaidInstance) {
61
- _context.next = 8;
62
- break;
63
- }
64
- return _context.abrupt("return", content);
65
- case 8:
66
- _context.next = 10;
67
- return mermaidInstance.parse(content);
68
- case 10:
69
- isValid = _context.sent;
70
- if (!isValid) {
71
- _context.next = 21;
72
- break;
73
- }
74
- _context.next = 14;
75
- return mermaidInstance.render(id, content);
76
- case 14:
77
- _yield$mermaidInstanc = _context.sent;
78
- svg = _yield$mermaidInstanc.svg;
79
- setValidContent(svg);
80
- // 缓存验证结果
81
- mermaidCache.set(cacheKey, true);
82
- return _context.abrupt("return", svg);
83
- case 21:
84
- throw new Error('Mermaid 语法无效');
85
- case 22:
86
- _context.next = 28;
87
- break;
88
- case 24:
89
- _context.prev = 24;
90
- _context.t0 = _context["catch"](2);
91
- console.error('Mermaid 解析错误:', _context.t0);
92
- // 返回最近一次有效的内容,或空字符串
93
- return _context.abrupt("return", validContent || '');
94
- case 28:
95
- case "end":
96
- return _context.stop();
97
- }
98
- }, _callee, null, [[2, 24]]);
99
- })), {
100
- dedupingInterval: 3000,
101
- errorRetryCount: 2,
102
- revalidateOnFocus: false,
103
- revalidateOnReconnect: false
104
- });
105
- };
106
-
107
28
  /**
108
29
  * 初始化 Mermaid 配置
109
30
  */
@@ -150,32 +71,143 @@ export var useMermaidInit = function useMermaidInit() {
150
71
  // 初始化 mermaid 配置
151
72
  useEffect(function () {
152
73
  var initMermaid = /*#__PURE__*/function () {
153
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
74
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
154
75
  var mermaidInstance;
155
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
156
- while (1) switch (_context2.prev = _context2.next) {
76
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
77
+ while (1) switch (_context.prev = _context.next) {
157
78
  case 0:
158
- _context2.next = 2;
79
+ _context.next = 2;
159
80
  return mermaidPromise;
160
81
  case 2:
161
- mermaidInstance = _context2.sent;
82
+ mermaidInstance = _context.sent;
162
83
  if (mermaidInstance) {
163
- _context2.next = 5;
84
+ _context.next = 5;
164
85
  break;
165
86
  }
166
- return _context2.abrupt("return");
87
+ return _context.abrupt("return");
167
88
  case 5:
168
89
  mermaidInstance.initialize(mermaidConfig);
169
90
  case 6:
170
91
  case "end":
171
- return _context2.stop();
92
+ return _context.stop();
172
93
  }
173
- }, _callee2);
94
+ }, _callee);
174
95
  }));
175
96
  return function initMermaid() {
176
- return _ref2.apply(this, arguments);
97
+ return _ref.apply(this, arguments);
177
98
  };
178
99
  }();
179
100
  initMermaid();
180
101
  }, [mermaidConfig]);
102
+ };
103
+
104
+ /**
105
+ * 验证并处理 Mermaid 图表内容
106
+ */
107
+ export var useMermaid = function useMermaid(id, content) {
108
+ var theme = useTheme();
109
+ // 用于存储最近一次有效的内容
110
+ var _useState = useState(''),
111
+ _useState2 = _slicedToArray(_useState, 2),
112
+ validContent = _useState2[0],
113
+ setValidContent = _useState2[1];
114
+
115
+ // 提取主题相关配置到 useMemo 中
116
+ var mermaidConfig = useMemo(function () {
117
+ return {
118
+ fontFamily: theme.fontFamilyCode,
119
+ gantt: {
120
+ useWidth: 1920
121
+ },
122
+ securityLevel: 'loose',
123
+ startOnLoad: false,
124
+ theme: theme.isDarkMode ? 'dark' : 'neutral',
125
+ themeVariables: {
126
+ errorBkgColor: theme.colorTextDescription,
127
+ errorTextColor: theme.colorTextDescription,
128
+ fontFamily: theme.fontFamily,
129
+ fontSize: 14,
130
+ lineColor: theme.colorTextSecondary,
131
+ mainBkg: theme.colorBgContainer,
132
+ noteBkgColor: theme.colorInfoBg,
133
+ noteTextColor: theme.colorInfoText,
134
+ pie1: theme.geekblue,
135
+ pie2: theme.colorWarning,
136
+ pie3: theme.colorSuccess,
137
+ pie4: theme.colorError,
138
+ primaryBorderColor: theme.colorBorder,
139
+ primaryColor: theme.colorBgContainer,
140
+ primaryTextColor: theme.colorText,
141
+ secondaryBorderColor: theme.colorInfoBorder,
142
+ secondaryColor: theme.colorInfoBg,
143
+ secondaryTextColor: theme.colorInfoText,
144
+ tertiaryBorderColor: theme.colorSuccessBorder,
145
+ tertiaryColor: theme.colorSuccessBg,
146
+ tertiaryTextColor: theme.colorSuccessText,
147
+ textColor: theme.colorText
148
+ }
149
+ };
150
+ }, [theme.isDarkMode]);
151
+
152
+ // 为长内容生成哈希键
153
+ var cacheKey = useMemo(function () {
154
+ var hash = content.length < MD5_LENGTH_THRESHOLD ? content : Md5.hashStr(content);
155
+ return "".concat(id, "-").concat(theme.isDarkMode ? 'd' : 'l', "-").concat(hash);
156
+ }, [content, id, theme.isDarkMode]);
157
+ return useSWR(cacheKey, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
158
+ var mermaidInstance, isValid, _yield$mermaidInstanc, svg;
159
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
160
+ while (1) switch (_context2.prev = _context2.next) {
161
+ case 0:
162
+ _context2.prev = 0;
163
+ _context2.next = 3;
164
+ return mermaidPromise;
165
+ case 3:
166
+ mermaidInstance = _context2.sent;
167
+ if (mermaidInstance) {
168
+ _context2.next = 6;
169
+ break;
170
+ }
171
+ return _context2.abrupt("return", content);
172
+ case 6:
173
+ _context2.next = 8;
174
+ return mermaidInstance.parse(content);
175
+ case 8:
176
+ isValid = _context2.sent;
177
+ if (!isValid) {
178
+ _context2.next = 19;
179
+ break;
180
+ }
181
+ // 更新有效内容状态
182
+ mermaidInstance.initialize(mermaidConfig);
183
+ _context2.next = 13;
184
+ return mermaidInstance.render(id, content);
185
+ case 13:
186
+ _yield$mermaidInstanc = _context2.sent;
187
+ svg = _yield$mermaidInstanc.svg;
188
+ setValidContent(svg);
189
+ // 缓存验证结果
190
+ return _context2.abrupt("return", svg);
191
+ case 19:
192
+ throw new Error('Mermaid 语法无效');
193
+ case 20:
194
+ _context2.next = 26;
195
+ break;
196
+ case 22:
197
+ _context2.prev = 22;
198
+ _context2.t0 = _context2["catch"](0);
199
+ console.error('Mermaid 解析错误:', _context2.t0);
200
+ // 返回最近一次有效的内容,或空字符串
201
+ return _context2.abrupt("return", validContent || '');
202
+ case 26:
203
+ case "end":
204
+ return _context2.stop();
205
+ }
206
+ }, _callee2, null, [[0, 22]]);
207
+ })), {
208
+ dedupingInterval: 3000,
209
+ errorRetryCount: 2,
210
+ revalidateOnFocus: false,
211
+ revalidateOnReconnect: false
212
+ });
181
213
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/ui",
3
- "version": "1.170.0",
3
+ "version": "1.170.1",
4
4
  "description": "Lobe UI is an open-source UI component library for building AIGC web apps",
5
5
  "keywords": [
6
6
  "lobehub",