@lobehub/ui 1.168.17 → 1.168.18

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.
@@ -9,29 +9,118 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
9
9
  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
10
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
11
11
  import { useTheme } from 'antd-style';
12
- import mermaid from 'mermaid';
13
- import { useCallback, useEffect, useState } from 'react';
14
- import { Center } from 'react-layout-kit';
15
- import { TransformComponent, TransformWrapper } from 'react-zoom-pan-pinch';
16
- import Controls from "./Controls";
17
- import { jsx as _jsx } from "react/jsx-runtime";
18
- import { jsxs as _jsxs } from "react/jsx-runtime";
19
- export var useMermaid = function useMermaid(content) {
20
- var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
21
- enablePanZoom = _ref.enablePanZoom;
22
- var _useState = useState(),
12
+ import { useEffect, useMemo, useState } from 'react';
13
+ import useSWR from 'swr';
14
+ import { Md5 } from 'ts-md5';
15
+
16
+ // 缓存已验证的图表内容
17
+ var mermaidCache = new Map();
18
+ var MD5_LENGTH_THRESHOLD = 10000;
19
+
20
+ // 懒加载 mermaid 实例
21
+ var loadMermaid = function loadMermaid() {
22
+ if (typeof window === 'undefined') return Promise.resolve(null);
23
+ return import('mermaid').then(function (mod) {
24
+ return mod.default;
25
+ });
26
+ };
27
+ var mermaidPromise = loadMermaid();
28
+
29
+ /**
30
+ * 验证并处理 Mermaid 图表内容
31
+ */
32
+ export var useMermaid = function useMermaid(id, content) {
33
+ // 用于存储最近一次有效的内容
34
+ var _useState = useState(''),
23
35
  _useState2 = _slicedToArray(_useState, 2),
24
- mermaidContent = _useState2[0],
25
- setMermaidContent = _useState2[1];
36
+ validContent = _useState2[0],
37
+ setValidContent = _useState2[1];
38
+ console.log(id, content);
39
+
40
+ // 为长内容生成哈希键
41
+ var cacheKey = useMemo(function () {
42
+ var hash = content.length < MD5_LENGTH_THRESHOLD ? content : Md5.hashStr(content);
43
+ return "mermaid-".concat(hash);
44
+ }, [content]);
45
+ return useSWR(cacheKey, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
46
+ var mermaidInstance, isValid, _yield$mermaidInstanc, svg;
47
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
48
+ while (1) switch (_context.prev = _context.next) {
49
+ case 0:
50
+ if (!mermaidCache.has(cacheKey)) {
51
+ _context.next = 2;
52
+ break;
53
+ }
54
+ return _context.abrupt("return", validContent);
55
+ case 2:
56
+ _context.prev = 2;
57
+ _context.next = 5;
58
+ return mermaidPromise;
59
+ case 5:
60
+ mermaidInstance = _context.sent;
61
+ if (mermaidInstance) {
62
+ _context.next = 8;
63
+ break;
64
+ }
65
+ return _context.abrupt("return", content);
66
+ case 8:
67
+ _context.next = 10;
68
+ return mermaidInstance.parse(content);
69
+ case 10:
70
+ isValid = _context.sent;
71
+ if (!isValid) {
72
+ _context.next = 22;
73
+ break;
74
+ }
75
+ _context.next = 14;
76
+ return mermaidInstance.render(id, content);
77
+ case 14:
78
+ _yield$mermaidInstanc = _context.sent;
79
+ svg = _yield$mermaidInstanc.svg;
80
+ console.log(svg);
81
+ setValidContent(svg);
82
+ // 缓存验证结果
83
+ mermaidCache.set(cacheKey, true);
84
+ return _context.abrupt("return", svg);
85
+ case 22:
86
+ throw new Error('Mermaid 语法无效');
87
+ case 23:
88
+ _context.next = 29;
89
+ break;
90
+ case 25:
91
+ _context.prev = 25;
92
+ _context.t0 = _context["catch"](2);
93
+ console.error('Mermaid 解析错误:', _context.t0);
94
+ // 返回最近一次有效的内容,或空字符串
95
+ return _context.abrupt("return", validContent || '');
96
+ case 29:
97
+ case "end":
98
+ return _context.stop();
99
+ }
100
+ }, _callee, null, [[2, 25]]);
101
+ })), {
102
+ dedupingInterval: 3000,
103
+ errorRetryCount: 2,
104
+ revalidateOnFocus: false,
105
+ revalidateOnReconnect: false
106
+ });
107
+ };
108
+
109
+ /**
110
+ * 初始化 Mermaid 配置
111
+ */
112
+ export var useMermaidInit = function useMermaidInit() {
26
113
  var theme = useTheme();
27
- useEffect(function () {
28
- mermaid.initialize({
114
+
115
+ // 提取主题相关配置到 useMemo 中
116
+ var mermaidConfig = useMemo(function () {
117
+ return {
29
118
  fontFamily: theme.fontFamilyCode,
30
119
  gantt: {
31
120
  useWidth: 1920
32
121
  },
33
122
  securityLevel: 'loose',
34
- startOnLoad: true,
123
+ startOnLoad: false,
35
124
  theme: theme.isDarkMode ? 'dark' : 'neutral',
36
125
  themeVariables: {
37
126
  errorBkgColor: theme.colorTextDescription,
@@ -57,63 +146,38 @@ export var useMermaid = function useMermaid(content) {
57
146
  tertiaryTextColor: theme.colorSuccessText,
58
147
  textColor: theme.colorText
59
148
  }
60
- });
61
- mermaid.contentLoaded();
62
- }, [mermaidContent, theme.isDarkMode]);
63
- var checkSyntax = /*#__PURE__*/function () {
64
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(textStr) {
65
- return _regeneratorRuntime().wrap(function _callee$(_context) {
66
- while (1) switch (_context.prev = _context.next) {
67
- case 0:
68
- _context.prev = 0;
69
- _context.next = 3;
70
- return mermaid.parse(textStr);
71
- case 3:
72
- if (!_context.sent) {
73
- _context.next = 5;
74
- break;
75
- }
76
- setMermaidContent(textStr);
77
- case 5:
78
- _context.next = 9;
79
- break;
80
- case 7:
81
- _context.prev = 7;
82
- _context.t0 = _context["catch"](0);
83
- case 9:
84
- case "end":
85
- return _context.stop();
86
- }
87
- }, _callee, null, [[0, 7]]);
88
- }));
89
- return function checkSyntax(_x) {
90
- return _ref2.apply(this, arguments);
91
149
  };
92
- }();
150
+ }, [theme.isDarkMode]);
151
+
152
+ // 初始化 mermaid 配置
93
153
  useEffect(function () {
94
- checkSyntax(content);
95
- }, [content]);
96
- return useCallback(function () {
97
- if (enablePanZoom) {
98
- return /*#__PURE__*/_jsxs(TransformWrapper, {
99
- children: [/*#__PURE__*/_jsx(Controls, {}), /*#__PURE__*/_jsx(TransformComponent, {
100
- contentClass: 'mermaid',
101
- contentStyle: {
102
- padding: 16
103
- },
104
- wrapperStyle: {
105
- minHeight: 240,
106
- width: '100%'
107
- },
108
- children: mermaidContent
109
- })]
110
- });
111
- }
112
- return /*#__PURE__*/_jsx(Center, {
113
- as: 'pre',
114
- className: 'mermaid',
115
- padding: 16,
116
- children: mermaidContent
117
- });
118
- }, [mermaidContent, theme.isDarkMode, enablePanZoom]);
154
+ var initMermaid = /*#__PURE__*/function () {
155
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
156
+ var mermaidInstance;
157
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
158
+ while (1) switch (_context2.prev = _context2.next) {
159
+ case 0:
160
+ _context2.next = 2;
161
+ return mermaidPromise;
162
+ case 2:
163
+ mermaidInstance = _context2.sent;
164
+ if (mermaidInstance) {
165
+ _context2.next = 5;
166
+ break;
167
+ }
168
+ return _context2.abrupt("return");
169
+ case 5:
170
+ mermaidInstance.initialize(mermaidConfig);
171
+ case 6:
172
+ case "end":
173
+ return _context2.stop();
174
+ }
175
+ }, _callee2);
176
+ }));
177
+ return function initMermaid() {
178
+ return _ref2.apply(this, arguments);
179
+ };
180
+ }();
181
+ initMermaid();
182
+ }, [mermaidConfig]);
119
183
  };
@@ -31,7 +31,7 @@ import remarkGfm from 'remark-gfm';
31
31
  import remarkMath from 'remark-math';
32
32
  import Alert from "../../Alert";
33
33
  import ImageGallery from "../../Image/ImageGallery";
34
- import { Typography } from "../../Markdown/Typography";
34
+ import Typography from "../../Markdown/Typography";
35
35
  import { useStyles } from "../../Markdown/style";
36
36
  import { escapeBrackets, escapeMhchem, fixMarkdownBold } from "../../Markdown/utils";
37
37
  import mdxComponents from "../mdxComponents";
@@ -1,3 +1,5 @@
1
+ declare const UNPKG_API = "https://unpkg.com";
2
+ declare const ALIYUN_API = "https://registry.npmmirror.com";
1
3
  export type CDN = 'aliyun' | 'unpkg';
2
4
  export interface CdnApi {
3
5
  path: string;
@@ -6,3 +8,4 @@ export interface CdnApi {
6
8
  version?: string;
7
9
  }
8
10
  export declare const genCdnUrl: ({ pkg, version, path, proxy }: CdnApi) => string;
11
+ export { ALIYUN_API, UNPKG_API };
@@ -39,4 +39,6 @@ export var genCdnUrl = function genCdnUrl(_ref) {
39
39
  // return urlJoin(LOBEHUB_API, 'gh', repo, path);
40
40
  // }
41
41
  // }
42
- // };
42
+ // };
43
+
44
+ export { ALIYUN_API, UNPKG_API };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/ui",
3
- "version": "1.168.17",
3
+ "version": "1.168.18",
4
4
  "description": "Lobe UI is an open-source UI component library for building AIGC web apps",
5
5
  "keywords": [
6
6
  "lobehub",
@@ -55,7 +55,7 @@
55
55
  "@floating-ui/react": "^0.27.5",
56
56
  "@giscus/react": "^3.1.0",
57
57
  "@lobehub/fluent-emoji": "^1.2.0",
58
- "@lobehub/icons": "^1.93.0",
58
+ "@lobehub/icons": "^1.94.0",
59
59
  "@mdx-js/mdx": "^3.1.0",
60
60
  "@mdx-js/react": "^3.1.0",
61
61
  "@radix-ui/react-slot": "^1.1.2",
@@ -69,10 +69,10 @@
69
69
  "emoji-mart": "^5.6.0",
70
70
  "fast-deep-equal": "^3.1.3",
71
71
  "immer": "^10.1.1",
72
- "leva": "^0.9.36",
72
+ "leva": "^0.10.0",
73
73
  "lodash-es": "^4.17.21",
74
- "lucide-react": "^0.475.0",
75
- "mermaid": "^11.5.0",
74
+ "lucide-react": "^0.484.0",
75
+ "mermaid": "^11.6.0",
76
76
  "numeral": "^2.0.6",
77
77
  "polished": "^4.3.1",
78
78
  "query-string": "^9.1.1",
@@ -82,7 +82,7 @@
82
82
  "react-error-boundary": "^5.0.0",
83
83
  "react-hotkeys-hook": "^4.6.1",
84
84
  "react-layout-kit": "^1.9.1",
85
- "react-markdown": "^9.1.0",
85
+ "react-markdown": "^10.1.0",
86
86
  "react-merge-refs": "^2.1.1",
87
87
  "react-rnd": "^10.5.2",
88
88
  "react-zoom-pan-pinch": "^3.7.0",
@@ -1,3 +0,0 @@
1
- export declare const useMermaid: (content: string, { enablePanZoom }?: {
2
- enablePanZoom?: boolean | undefined;
3
- }) => () => import("react/jsx-runtime").JSX.Element;