ds-markdown 0.1.9-beta.2 → 0.1.9-beta.4
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.
- package/dist/cjs/i18n/en/index.d.ts +7 -0
- package/dist/cjs/i18n/en/index.js +7 -0
- package/dist/cjs/i18n/en/index.js.map +1 -1
- package/dist/cjs/i18n/zh/index.d.ts +7 -0
- package/dist/cjs/i18n/zh/index.js +7 -0
- package/dist/cjs/i18n/zh/index.js.map +1 -1
- package/dist/cjs/index.d.ts +76 -1
- package/dist/cjs/index.js +280 -75
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/i18n/en/index.d.ts +7 -0
- package/dist/esm/i18n/en/index.js +7 -0
- package/dist/esm/i18n/en/index.js.map +1 -1
- package/dist/esm/i18n/zh/index.d.ts +7 -0
- package/dist/esm/i18n/zh/index.js +7 -0
- package/dist/esm/i18n/zh/index.js.map +1 -1
- package/dist/esm/index.d.ts +76 -1
- package/dist/esm/index.js +229 -33
- package/dist/esm/index.js.map +1 -1
- package/dist/style.css +97 -0
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
-
var
|
|
6
|
+
var React = require('react');
|
|
7
7
|
var Markdown$2 = require('react-markdown');
|
|
8
|
-
var reactSyntaxHighlighter = require('react-syntax-highlighter');
|
|
9
8
|
var gfmPlugin = require('remark-gfm');
|
|
10
9
|
var classNames = require('classnames');
|
|
10
|
+
var reactSyntaxHighlighter = require('react-syntax-highlighter');
|
|
11
|
+
var reactDom = require('react-dom');
|
|
11
12
|
|
|
12
13
|
const replaceMathBracket = (value) => {
|
|
13
14
|
// 1. 提取所有块级公式内容,临时替换为占位符, [...]
|
|
@@ -32,32 +33,18 @@ const replaceMathBracket = (value) => {
|
|
|
32
33
|
return replaced;
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
const CheckMarkIcon = ({ size }) => {
|
|
36
|
-
return (jsxRuntime.jsx("svg", { width: size, height: size, viewBox: `0 0 ${size} ${size}`, fill: "none", xmlns: "http://www.w3.org/2000/svg", children: jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M9.338 21.575a1.058 1.058 0 0 1-.53-.363L2.275 13.17a1.063 1.063 0 0 1 1.65-1.341l5.63 6.928L19.33 3.86a1.064 1.064 0 0 1 1.778 1.167L10.551 21.115a1.065 1.065 0 0 1-1.213.46z", fill: "currentColor" }) }));
|
|
37
|
-
};
|
|
38
|
-
const CopyIcon = ({ size }) => {
|
|
39
|
-
return (jsxRuntime.jsxs("svg", { width: size, height: size, viewBox: `0 0 ${size} ${size}`, fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("path", { d: "M3.65169 12.9243C3.68173 13.1045 3.74181 13.2748 3.80189 13.445C3.87198 13.6052 3.96211 13.7654 4.06225 13.9156C4.16238 14.0658 4.27253 14.206 4.4027 14.3362C4.52286 14.4663 4.66306 14.5765 4.81326 14.6766C4.96346 14.7768 5.11366 14.8569 5.28389 14.927C5.44411 14.9971 5.61434 15.0571 5.79459 15.0872C5.97483 15.1272 6.14506 15.1373 6.3253 15.1373V16.9196C6.30739 16.9196 6.28949 16.9195 6.27159 16.9193C5.9991 16.9158 5.72659 16.8859 5.4541 16.8295C5.16371 16.7694 4.88334 16.6893 4.61298 16.5692C4.3326 16.459 4.08226 16.3188 3.83193 16.1586C3.59161 15.9884 3.3613 15.7981 3.15102 15.5878C2.94074 15.3776 2.7605 15.1473 2.59027 14.9069C2.43006 14.6566 2.28986 14.3962 2.17972 14.1259C2.06957 13.8455 1.97944 13.5651 1.91936 13.2747C1.86929 12.9843 1.83926 12.684 1.83926 12.3936V6.26532C1.83926 5.96492 1.86929 5.67456 1.91936 5.38417C1.97944 5.09378 2.06957 4.80338 2.17972 4.53302C2.28986 4.26265 2.43006 4.0023 2.59027 3.75197C2.7605 3.50163 2.94074 3.27132 3.15102 3.06104C3.3613 2.85076 3.59161 2.67052 3.83193 2.50029C4.08226 2.33006 4.3326 2.19987 4.61298 2.07971C4.88334 1.96956 5.16371 1.87943 5.4541 1.81935C5.74449 1.75927 6.03491 1.73926 6.3253 1.73926H12.3934C12.6838 1.73926 12.9842 1.75927 13.2746 1.81935C13.555 1.87943 13.8354 1.96956 14.1158 2.07971C14.3861 2.19987 14.6465 2.33006 14.8868 2.50029C15.1371 2.67052 15.3574 2.85076 15.5677 3.06104C15.778 3.27132 15.9582 3.50163 16.1284 3.75197C16.2887 4.0023 16.4288 4.26265 16.539 4.53302C16.6592 4.80338 16.7393 5.09378 16.7994 5.38417C16.8558 5.65722 16.8858 5.93024 16.8892 6.21161C16.8894 6.22948 16.8895 6.24739 16.8895 6.26532H15.1271C15.1271 6.08508 15.1071 5.90486 15.067 5.72462C15.037 5.55439 14.9869 5.38415 14.9168 5.21392C14.8467 5.04369 14.7566 4.88347 14.6665 4.73327C14.5664 4.58307 14.4462 4.45289 14.326 4.32271C14.1959 4.19254 14.0557 4.08239 13.9055 3.98226C13.7553 3.88212 13.6051 3.79202 13.4348 3.72193C13.2746 3.65184 13.1044 3.60174 12.9242 3.5717C12.7539 3.53165 12.5737 3.51163 12.3934 3.51163H6.3253C6.14506 3.51163 5.97483 3.53165 5.79459 3.5717C5.61434 3.60174 5.44411 3.65184 5.28389 3.72193C5.11366 3.79202 4.96346 3.88212 4.81326 3.98226C4.66306 4.08239 4.52286 4.19254 4.4027 4.32271C4.27253 4.45289 4.16238 4.58307 4.06225 4.73327C3.96211 4.88347 3.87198 5.04369 3.80189 5.21392C3.74181 5.38415 3.68173 5.55439 3.65169 5.72462C3.61164 5.90486 3.60164 6.08508 3.60164 6.26532V12.3936C3.60164 12.5638 3.61164 12.744 3.65169 12.9243Z", fill: "currentColor" }), jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M9.66972 21.6772C9.39936 21.567 9.13902 21.4268 8.8987 21.2566C8.64836 21.0964 8.42804 20.9061 8.21776 20.6959C8.00748 20.4856 7.81723 20.2553 7.65701 20.015C7.4968 19.7646 7.3566 19.5043 7.24646 19.2239C7.12629 18.9535 7.04621 18.6731 6.98613 18.3727C6.92605 18.0823 6.89601 17.792 6.89601 17.4915V11.3733C6.89601 11.0729 6.92605 10.7825 6.98613 10.4922C7.04621 10.1918 7.12629 9.91137 7.24646 9.64101C7.3566 9.36063 7.4968 9.10028 7.65701 8.85996C7.81723 8.60962 8.00748 8.37931 8.21776 8.16903C8.42804 7.95875 8.64836 7.76849 8.8987 7.60828C9.13902 7.43805 9.39936 7.29785 9.66972 7.1877C9.94009 7.07755 10.2205 6.98745 10.5108 6.92737C10.8012 6.86729 11.0916 6.83725 11.392 6.83725H17.4602C17.7506 6.83725 18.041 6.86729 18.3313 6.92737C18.6217 6.98745 18.9021 7.07755 19.1725 7.1877C19.4529 7.29785 19.7032 7.43805 19.9535 7.60828C20.1938 7.76849 20.4242 7.95875 20.6345 8.16903C20.8447 8.37931 21.025 8.60962 21.1952 8.85996C21.3554 9.10028 21.4956 9.36063 21.6058 9.64101C21.7159 9.91137 21.806 10.1918 21.8661 10.4922C21.9162 10.7825 21.9462 11.0729 21.9462 11.3733V17.4915C21.9462 17.792 21.9162 18.0823 21.8661 18.3727C21.806 18.6731 21.7159 18.9535 21.6058 19.2239C21.4956 19.5043 21.3554 19.7646 21.1952 20.015C21.025 20.2553 20.8447 20.4856 20.6345 20.6959C20.4242 20.9061 20.1938 21.0964 19.9535 21.2566C19.7032 21.4268 19.4529 21.567 19.1725 21.6772C18.9021 21.7973 18.6217 21.8774 18.3313 21.9375C18.041 21.9976 17.7506 22.0276 17.4602 22.0276H11.392C11.0916 22.0276 10.8012 21.9976 10.5108 21.9375C10.2205 21.8774 9.94009 21.7973 9.66972 21.6772ZM10.8613 8.6697C11.0316 8.63966 11.2118 8.61965 11.392 8.61965H17.4602C17.6404 8.61965 17.8107 8.63966 17.9909 8.6697C18.1611 8.70975 18.3314 8.75983 18.5016 8.82992C18.6618 8.90001 18.822 8.98012 18.9722 9.08026C19.1224 9.18039 19.2626 9.30055 19.3828 9.42071C19.513 9.55088 19.6231 9.69109 19.7232 9.84129C19.8234 9.99149 19.9035 10.1517 19.9736 10.3219C20.0437 10.4821 20.0937 10.6624 20.1338 10.8326C20.1638 11.0129 20.1838 11.1931 20.1838 11.3733V17.4915C20.1838 17.6718 20.1638 17.852 20.1338 18.0323C20.0937 18.2125 20.0437 18.3828 19.9736 18.543C19.9035 18.7132 19.8234 18.8734 19.7232 19.0236C19.6231 19.1738 19.513 19.314 19.3828 19.4342C19.2626 19.5643 19.1224 19.6845 18.9722 19.7846C18.822 19.8848 18.6618 19.9649 18.5016 20.035C18.3314 20.1051 18.1611 20.1551 17.9909 20.1952C17.8107 20.2252 17.6404 20.2452 17.4602 20.2452H11.392C11.2118 20.2452 11.0316 20.2252 10.8613 20.1952C10.6811 20.1551 10.5108 20.1051 10.3506 20.035C10.1804 19.9649 10.0202 19.8848 9.87 19.7846C9.72982 19.6845 9.58962 19.5643 9.45945 19.4342C9.33929 19.314 9.21913 19.1738 9.119 19.0236C9.01886 18.8734 8.93875 18.7132 8.86866 18.543C8.79857 18.3828 8.74847 18.2125 8.71843 18.0323C8.67838 17.852 8.65836 17.6718 8.65836 17.4915V11.3733C8.65836 11.1931 8.67838 11.0129 8.71843 10.8326C8.74847 10.6624 8.79857 10.4821 8.86866 10.3219C8.93875 10.1517 9.01886 9.99149 9.119 9.84129C9.21913 9.69109 9.33929 9.55088 9.45945 9.42071C9.58962 9.30055 9.72982 9.18039 9.87 9.08026C10.0202 8.98012 10.1804 8.90001 10.3506 8.82992C10.5108 8.75983 10.6811 8.70975 10.8613 8.6697Z", fill: "currentColor" })] }));
|
|
40
|
-
};
|
|
41
|
-
const DownloadIcon = ({ size }) => {
|
|
42
|
-
return (jsxRuntime.jsxs("svg", { width: size, height: size, viewBox: `0 0 ${size} ${size}`, fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M12 2.55a.97.97 0 0 1 .982.956v13.04a.97.97 0 0 1-.982.957.97.97 0 0 1-.982-.956V3.507A.97.97 0 0 1 12 2.55z", fill: "currentColor" }), jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M19.418 9.808c.382.375.37.971-.027 1.332l-6.7 6.085a1.04 1.04 0 0 1-1.41-.025.905.905 0 0 1 .027-1.332l6.7-6.085a1.04 1.04 0 0 1 1.41.025z", fill: "currentColor" }), jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M4.582 9.808a1.04 1.04 0 0 1 1.41-.025l6.7 6.085c.397.361.409.957.027 1.332a1.04 1.04 0 0 1-1.41.025l-6.7-6.085a.905.905 0 0 1-.027-1.332z", fill: "currentColor" }), jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M3.068 16.46a.97.97 0 0 1 .983.956v1.739c0 .432.36.782.803.782h14.291c.445 0 .804-.35.804-.782v-1.739a.97.97 0 0 1 .983-.956.97.97 0 0 1 .982.956v1.739c0 1.488-1.24 2.695-2.769 2.695H4.855c-1.53 0-2.77-1.207-2.77-2.695v-1.739a.97.97 0 0 1 .983-.956z", fill: "currentColor" })] }));
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const DsButton = ({ className = '', children, icon, onClick, style }) => {
|
|
46
|
-
return (jsxRuntime.jsxs("div", { role: "button", className: classNames({
|
|
47
|
-
'ds-button': true,
|
|
48
|
-
[className]: !!className,
|
|
49
|
-
}), onClick: onClick, style: style, children: [icon && jsxRuntime.jsx("div", { className: "ds-button__icon", children: icon }), children] }));
|
|
50
|
-
};
|
|
51
|
-
|
|
52
36
|
const DEFAULT_THEME = 'light';
|
|
53
37
|
const DEFAULT_ANSWER_TYPE = 'answer';
|
|
54
38
|
const DEFAULT_PLUGINS = [];
|
|
55
|
-
const MarkdownThemeContext =
|
|
56
|
-
state: {
|
|
39
|
+
const MarkdownThemeContext = React.createContext({
|
|
40
|
+
state: {
|
|
41
|
+
theme: DEFAULT_THEME,
|
|
42
|
+
answerType: DEFAULT_ANSWER_TYPE,
|
|
43
|
+
},
|
|
57
44
|
methods: {},
|
|
58
45
|
});
|
|
59
46
|
const MarkdownThemeProvider = ({ value = {}, children }) => {
|
|
60
|
-
const contextValue =
|
|
47
|
+
const contextValue = React.useMemo(() => ({
|
|
61
48
|
state: {
|
|
62
49
|
theme: DEFAULT_THEME,
|
|
63
50
|
answerType: DEFAULT_ANSWER_TYPE,
|
|
@@ -69,7 +56,32 @@ const MarkdownThemeProvider = ({ value = {}, children }) => {
|
|
|
69
56
|
}), [value]);
|
|
70
57
|
return jsxRuntime.jsx(MarkdownThemeContext.Provider, { value: contextValue, children: children });
|
|
71
58
|
};
|
|
72
|
-
const useMarkdownThemeContext = () =>
|
|
59
|
+
const useMarkdownThemeContext = () => React.useContext(MarkdownThemeContext);
|
|
60
|
+
const useThemeState = () => {
|
|
61
|
+
return React.useContext(MarkdownThemeContext).state;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const CodeBlockWrap = ({ children, title }) => {
|
|
65
|
+
const { theme } = useThemeState();
|
|
66
|
+
return (jsxRuntime.jsxs("div", { className: `md-code-block md-code-block-${theme}`, children: [jsxRuntime.jsx("div", { className: "md-code-block-banner-wrap", children: jsxRuntime.jsx("div", { className: "md-code-block-banner md-code-block-banner-lite", children: title }) }), jsxRuntime.jsx("div", { className: "md-code-block-content", children: children })] }));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const Button = ({ className = '', children, icon, onClick, style, ...restProps }) => {
|
|
70
|
+
return (jsxRuntime.jsxs("div", { role: "button", className: classNames({
|
|
71
|
+
'ds-button': true,
|
|
72
|
+
[className]: !!className,
|
|
73
|
+
}), onClick: onClick, style: style, ...restProps, children: [icon && jsxRuntime.jsx("div", { className: "ds-button__icon", children: icon }), children] }));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const CheckMarkIcon = ({ size }) => {
|
|
77
|
+
return (jsxRuntime.jsx("svg", { width: size, height: size, viewBox: `0 0 ${size} ${size}`, fill: "none", xmlns: "http://www.w3.org/2000/svg", children: jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M9.338 21.575a1.058 1.058 0 0 1-.53-.363L2.275 13.17a1.063 1.063 0 0 1 1.65-1.341l5.63 6.928L19.33 3.86a1.064 1.064 0 0 1 1.778 1.167L10.551 21.115a1.065 1.065 0 0 1-1.213.46z", fill: "currentColor" }) }));
|
|
78
|
+
};
|
|
79
|
+
const CopyIcon = ({ size }) => {
|
|
80
|
+
return (jsxRuntime.jsxs("svg", { width: size, height: size, viewBox: `0 0 ${size} ${size}`, fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("path", { d: "M3.65169 12.9243C3.68173 13.1045 3.74181 13.2748 3.80189 13.445C3.87198 13.6052 3.96211 13.7654 4.06225 13.9156C4.16238 14.0658 4.27253 14.206 4.4027 14.3362C4.52286 14.4663 4.66306 14.5765 4.81326 14.6766C4.96346 14.7768 5.11366 14.8569 5.28389 14.927C5.44411 14.9971 5.61434 15.0571 5.79459 15.0872C5.97483 15.1272 6.14506 15.1373 6.3253 15.1373V16.9196C6.30739 16.9196 6.28949 16.9195 6.27159 16.9193C5.9991 16.9158 5.72659 16.8859 5.4541 16.8295C5.16371 16.7694 4.88334 16.6893 4.61298 16.5692C4.3326 16.459 4.08226 16.3188 3.83193 16.1586C3.59161 15.9884 3.3613 15.7981 3.15102 15.5878C2.94074 15.3776 2.7605 15.1473 2.59027 14.9069C2.43006 14.6566 2.28986 14.3962 2.17972 14.1259C2.06957 13.8455 1.97944 13.5651 1.91936 13.2747C1.86929 12.9843 1.83926 12.684 1.83926 12.3936V6.26532C1.83926 5.96492 1.86929 5.67456 1.91936 5.38417C1.97944 5.09378 2.06957 4.80338 2.17972 4.53302C2.28986 4.26265 2.43006 4.0023 2.59027 3.75197C2.7605 3.50163 2.94074 3.27132 3.15102 3.06104C3.3613 2.85076 3.59161 2.67052 3.83193 2.50029C4.08226 2.33006 4.3326 2.19987 4.61298 2.07971C4.88334 1.96956 5.16371 1.87943 5.4541 1.81935C5.74449 1.75927 6.03491 1.73926 6.3253 1.73926H12.3934C12.6838 1.73926 12.9842 1.75927 13.2746 1.81935C13.555 1.87943 13.8354 1.96956 14.1158 2.07971C14.3861 2.19987 14.6465 2.33006 14.8868 2.50029C15.1371 2.67052 15.3574 2.85076 15.5677 3.06104C15.778 3.27132 15.9582 3.50163 16.1284 3.75197C16.2887 4.0023 16.4288 4.26265 16.539 4.53302C16.6592 4.80338 16.7393 5.09378 16.7994 5.38417C16.8558 5.65722 16.8858 5.93024 16.8892 6.21161C16.8894 6.22948 16.8895 6.24739 16.8895 6.26532H15.1271C15.1271 6.08508 15.1071 5.90486 15.067 5.72462C15.037 5.55439 14.9869 5.38415 14.9168 5.21392C14.8467 5.04369 14.7566 4.88347 14.6665 4.73327C14.5664 4.58307 14.4462 4.45289 14.326 4.32271C14.1959 4.19254 14.0557 4.08239 13.9055 3.98226C13.7553 3.88212 13.6051 3.79202 13.4348 3.72193C13.2746 3.65184 13.1044 3.60174 12.9242 3.5717C12.7539 3.53165 12.5737 3.51163 12.3934 3.51163H6.3253C6.14506 3.51163 5.97483 3.53165 5.79459 3.5717C5.61434 3.60174 5.44411 3.65184 5.28389 3.72193C5.11366 3.79202 4.96346 3.88212 4.81326 3.98226C4.66306 4.08239 4.52286 4.19254 4.4027 4.32271C4.27253 4.45289 4.16238 4.58307 4.06225 4.73327C3.96211 4.88347 3.87198 5.04369 3.80189 5.21392C3.74181 5.38415 3.68173 5.55439 3.65169 5.72462C3.61164 5.90486 3.60164 6.08508 3.60164 6.26532V12.3936C3.60164 12.5638 3.61164 12.744 3.65169 12.9243Z", fill: "currentColor" }), jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M9.66972 21.6772C9.39936 21.567 9.13902 21.4268 8.8987 21.2566C8.64836 21.0964 8.42804 20.9061 8.21776 20.6959C8.00748 20.4856 7.81723 20.2553 7.65701 20.015C7.4968 19.7646 7.3566 19.5043 7.24646 19.2239C7.12629 18.9535 7.04621 18.6731 6.98613 18.3727C6.92605 18.0823 6.89601 17.792 6.89601 17.4915V11.3733C6.89601 11.0729 6.92605 10.7825 6.98613 10.4922C7.04621 10.1918 7.12629 9.91137 7.24646 9.64101C7.3566 9.36063 7.4968 9.10028 7.65701 8.85996C7.81723 8.60962 8.00748 8.37931 8.21776 8.16903C8.42804 7.95875 8.64836 7.76849 8.8987 7.60828C9.13902 7.43805 9.39936 7.29785 9.66972 7.1877C9.94009 7.07755 10.2205 6.98745 10.5108 6.92737C10.8012 6.86729 11.0916 6.83725 11.392 6.83725H17.4602C17.7506 6.83725 18.041 6.86729 18.3313 6.92737C18.6217 6.98745 18.9021 7.07755 19.1725 7.1877C19.4529 7.29785 19.7032 7.43805 19.9535 7.60828C20.1938 7.76849 20.4242 7.95875 20.6345 8.16903C20.8447 8.37931 21.025 8.60962 21.1952 8.85996C21.3554 9.10028 21.4956 9.36063 21.6058 9.64101C21.7159 9.91137 21.806 10.1918 21.8661 10.4922C21.9162 10.7825 21.9462 11.0729 21.9462 11.3733V17.4915C21.9462 17.792 21.9162 18.0823 21.8661 18.3727C21.806 18.6731 21.7159 18.9535 21.6058 19.2239C21.4956 19.5043 21.3554 19.7646 21.1952 20.015C21.025 20.2553 20.8447 20.4856 20.6345 20.6959C20.4242 20.9061 20.1938 21.0964 19.9535 21.2566C19.7032 21.4268 19.4529 21.567 19.1725 21.6772C18.9021 21.7973 18.6217 21.8774 18.3313 21.9375C18.041 21.9976 17.7506 22.0276 17.4602 22.0276H11.392C11.0916 22.0276 10.8012 21.9976 10.5108 21.9375C10.2205 21.8774 9.94009 21.7973 9.66972 21.6772ZM10.8613 8.6697C11.0316 8.63966 11.2118 8.61965 11.392 8.61965H17.4602C17.6404 8.61965 17.8107 8.63966 17.9909 8.6697C18.1611 8.70975 18.3314 8.75983 18.5016 8.82992C18.6618 8.90001 18.822 8.98012 18.9722 9.08026C19.1224 9.18039 19.2626 9.30055 19.3828 9.42071C19.513 9.55088 19.6231 9.69109 19.7232 9.84129C19.8234 9.99149 19.9035 10.1517 19.9736 10.3219C20.0437 10.4821 20.0937 10.6624 20.1338 10.8326C20.1638 11.0129 20.1838 11.1931 20.1838 11.3733V17.4915C20.1838 17.6718 20.1638 17.852 20.1338 18.0323C20.0937 18.2125 20.0437 18.3828 19.9736 18.543C19.9035 18.7132 19.8234 18.8734 19.7232 19.0236C19.6231 19.1738 19.513 19.314 19.3828 19.4342C19.2626 19.5643 19.1224 19.6845 18.9722 19.7846C18.822 19.8848 18.6618 19.9649 18.5016 20.035C18.3314 20.1051 18.1611 20.1551 17.9909 20.1952C17.8107 20.2252 17.6404 20.2452 17.4602 20.2452H11.392C11.2118 20.2452 11.0316 20.2252 10.8613 20.1952C10.6811 20.1551 10.5108 20.1051 10.3506 20.035C10.1804 19.9649 10.0202 19.8848 9.87 19.7846C9.72982 19.6845 9.58962 19.5643 9.45945 19.4342C9.33929 19.314 9.21913 19.1738 9.119 19.0236C9.01886 18.8734 8.93875 18.7132 8.86866 18.543C8.79857 18.3828 8.74847 18.2125 8.71843 18.0323C8.67838 17.852 8.65836 17.6718 8.65836 17.4915V11.3733C8.65836 11.1931 8.67838 11.0129 8.71843 10.8326C8.74847 10.6624 8.79857 10.4821 8.86866 10.3219C8.93875 10.1517 9.01886 9.99149 9.119 9.84129C9.21913 9.69109 9.33929 9.55088 9.45945 9.42071C9.58962 9.30055 9.72982 9.18039 9.87 9.08026C10.0202 8.98012 10.1804 8.90001 10.3506 8.82992C10.5108 8.75983 10.6811 8.70975 10.8613 8.6697Z", fill: "currentColor" })] }));
|
|
81
|
+
};
|
|
82
|
+
const DownloadIcon = ({ size }) => {
|
|
83
|
+
return (jsxRuntime.jsxs("svg", { width: size, height: size, viewBox: `0 0 ${size} ${size}`, fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M12 2.55a.97.97 0 0 1 .982.956v13.04a.97.97 0 0 1-.982.957.97.97 0 0 1-.982-.956V3.507A.97.97 0 0 1 12 2.55z", fill: "currentColor" }), jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M19.418 9.808c.382.375.37.971-.027 1.332l-6.7 6.085a1.04 1.04 0 0 1-1.41-.025.905.905 0 0 1 .027-1.332l6.7-6.085a1.04 1.04 0 0 1 1.41.025z", fill: "currentColor" }), jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M4.582 9.808a1.04 1.04 0 0 1 1.41-.025l6.7 6.085c.397.361.409.957.027 1.332a1.04 1.04 0 0 1-1.41.025l-6.7-6.085a.905.905 0 0 1-.027-1.332z", fill: "currentColor" }), jsxRuntime.jsx("path", { "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M3.068 16.46a.97.97 0 0 1 .983.956v1.739c0 .432.36.782.803.782h14.291c.445 0 .804-.35.804-.782v-1.739a.97.97 0 0 1 .983-.956.97.97 0 0 1 .982.956v1.739c0 1.488-1.24 2.695-2.769 2.695H4.855c-1.53 0-2.77-1.207-2.77-2.695v-1.739a.97.97 0 0 1 .983-.956z", fill: "currentColor" })] }));
|
|
84
|
+
};
|
|
73
85
|
|
|
74
86
|
const zhCN = {
|
|
75
87
|
codeBlock: {
|
|
@@ -77,13 +89,20 @@ const zhCN = {
|
|
|
77
89
|
copied: '已复制',
|
|
78
90
|
download: '下载',
|
|
79
91
|
},
|
|
92
|
+
mermaid: {
|
|
93
|
+
diagram: '图表',
|
|
94
|
+
code: '代码',
|
|
95
|
+
zoomOut: '缩小',
|
|
96
|
+
zoomIn: '放大',
|
|
97
|
+
download: '下载',
|
|
98
|
+
},
|
|
80
99
|
};
|
|
81
100
|
|
|
82
|
-
const ConfigContext =
|
|
101
|
+
const ConfigContext = React.createContext({
|
|
83
102
|
locale: zhCN,
|
|
84
103
|
});
|
|
85
104
|
const ConfigProvider = ({ locale, children, mermaidConfig }) => {
|
|
86
|
-
const contextValue =
|
|
105
|
+
const contextValue = React.useMemo(() => {
|
|
87
106
|
const contextValue = {
|
|
88
107
|
locale: locale || zhCN,
|
|
89
108
|
};
|
|
@@ -96,7 +115,7 @@ const ConfigProvider = ({ locale, children, mermaidConfig }) => {
|
|
|
96
115
|
};
|
|
97
116
|
// Hook 用于在组件中使用配置
|
|
98
117
|
const useConfig = () => {
|
|
99
|
-
const context =
|
|
118
|
+
const context = React.useContext(ConfigContext);
|
|
100
119
|
return context;
|
|
101
120
|
};
|
|
102
121
|
// Hook 用于获取当前语言包
|
|
@@ -106,16 +125,12 @@ const useLocale = () => {
|
|
|
106
125
|
};
|
|
107
126
|
|
|
108
127
|
const TIMEOUT = 3000;
|
|
109
|
-
const
|
|
110
|
-
const { state: themeState } = useMarkdownThemeContext();
|
|
128
|
+
const CopyButton = ({ codeContent, style, className }) => {
|
|
111
129
|
const { locale } = useConfig();
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
const [copyText, setCopyText] = react.useState(locale.codeBlock.copy);
|
|
117
|
-
const [showCheckmark, setShowCheckmark] = react.useState(false);
|
|
118
|
-
const [isCopying, setIsCopying] = react.useState(false);
|
|
130
|
+
const [copyText, setCopyText] = React.useState(locale.codeBlock.copy);
|
|
131
|
+
const [showCheckmark, setShowCheckmark] = React.useState(false);
|
|
132
|
+
const [isCopying, setIsCopying] = React.useState(false);
|
|
133
|
+
// 下载文件
|
|
119
134
|
// 复制到剪贴板
|
|
120
135
|
const handleCopy = async () => {
|
|
121
136
|
if (isCopying || !codeContent)
|
|
@@ -154,6 +169,11 @@ const BlockWrap = ({ children, language, codeContent }) => {
|
|
|
154
169
|
document.body.removeChild(textArea);
|
|
155
170
|
}
|
|
156
171
|
};
|
|
172
|
+
return (jsxRuntime.jsx(Button, { style: style, className: className, icon: showCheckmark ? jsxRuntime.jsx(CheckMarkIcon, { size: 24 }) : jsxRuntime.jsx(CopyIcon, { size: 24 }), onClick: handleCopy, children: jsxRuntime.jsx("span", { children: copyText }) }));
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const DownloadButton = ({ codeContent, language, style, className }) => {
|
|
176
|
+
const { locale } = useConfig();
|
|
157
177
|
// 下载文件
|
|
158
178
|
const handleDownload = () => {
|
|
159
179
|
if (!codeContent)
|
|
@@ -204,13 +224,25 @@ const BlockWrap = ({ children, language, codeContent }) => {
|
|
|
204
224
|
document.body.removeChild(link);
|
|
205
225
|
URL.revokeObjectURL(url);
|
|
206
226
|
};
|
|
227
|
+
return (jsxRuntime.jsx(Button, { style: style, className: className, icon: jsxRuntime.jsx(DownloadIcon, { size: 24 }), onClick: handleDownload, children: jsxRuntime.jsx("span", { children: locale.codeBlock.download }) }));
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const CodeBlockActions = ({ codeContent, language }) => {
|
|
231
|
+
return (jsxRuntime.jsxs("div", { className: "md-code-block-header-actions", children: [jsxRuntime.jsx(CopyButton, { codeContent: codeContent, style: { fontSize: 13, padding: '0 4px' } }), jsxRuntime.jsx(DownloadButton, { codeContent: codeContent, language: language, style: { fontSize: 13, padding: '0 4px' } })] }));
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const BlockWrap = ({ children, language, codeContent }) => {
|
|
235
|
+
const { state: themeState } = useMarkdownThemeContext();
|
|
236
|
+
// 从 context 中获取主题配置
|
|
237
|
+
const currentCodeBlock = themeState.codeBlock;
|
|
238
|
+
const { headerActions = true } = currentCodeBlock || {};
|
|
207
239
|
const renderHeaderActions = () => {
|
|
208
240
|
if (headerActions === true) {
|
|
209
|
-
return
|
|
241
|
+
return jsxRuntime.jsx(CodeBlockActions, { codeContent: codeContent, language: language });
|
|
210
242
|
}
|
|
211
243
|
return headerActions;
|
|
212
244
|
};
|
|
213
|
-
return (jsxRuntime.
|
|
245
|
+
return (jsxRuntime.jsx(CodeBlockWrap, { title: jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { className: "md-code-block-language", children: language }), renderHeaderActions()] }), children: children }));
|
|
214
246
|
};
|
|
215
247
|
|
|
216
248
|
const __DEV__ = process.env.NODE_ENV === 'development';
|
|
@@ -218,11 +250,14 @@ const ID_PREFIX__ = '__ds-markdown__';
|
|
|
218
250
|
/** 数学公式插件id */
|
|
219
251
|
const katexId = `${ID_PREFIX__}katex`;
|
|
220
252
|
|
|
221
|
-
const
|
|
253
|
+
const HighlightCode = ({ code, language }) => {
|
|
254
|
+
return (jsxRuntime.jsx(reactSyntaxHighlighter.Prism, { useInlineStyles: false, language: language, style: {}, children: code }));
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const CodeComponent = ({ className, children = '' }) => {
|
|
222
258
|
const match = /language-(\w+)/.exec(className || '');
|
|
223
259
|
const codeContent = String(children).replace(/\n$/, '');
|
|
224
|
-
|
|
225
|
-
return match ? (jsxRuntime.jsx(BlockWrap, { language: match[1], codeContent: codeContent, children: jsxRuntime.jsx(reactSyntaxHighlighter.Prism, { useInlineStyles: false, language: match[1], style: {}, children: codeContent }) })) : (jsxRuntime.jsx("code", { className: className, children: children }));
|
|
260
|
+
return match ? (jsxRuntime.jsx(BlockWrap, { language: match[1], codeContent: codeContent, children: jsxRuntime.jsx(HighlightCode, { code: codeContent, language: match[1] }) })) : (jsxRuntime.jsx("code", { className: className, children: children }));
|
|
226
261
|
};
|
|
227
262
|
const HighReactMarkdown = ({ children: _children, ...props }) => {
|
|
228
263
|
const { state: themeState } = useMarkdownThemeContext();
|
|
@@ -230,7 +265,7 @@ const HighReactMarkdown = ({ children: _children, ...props }) => {
|
|
|
230
265
|
const currentMath = themeState.math;
|
|
231
266
|
const currentPlugins = themeState.plugins;
|
|
232
267
|
const mathSplitSymbol = currentMath?.splitSymbol ?? 'dollar';
|
|
233
|
-
const { remarkPlugins, rehypePlugins, hasKatexPlugin, components } =
|
|
268
|
+
const { remarkPlugins, rehypePlugins, hasKatexPlugin, components } = React.useMemo(() => {
|
|
234
269
|
let hasKatexPlugin = false;
|
|
235
270
|
const components = {};
|
|
236
271
|
const remarkPlugins = [gfmPlugin];
|
|
@@ -266,7 +301,7 @@ const HighReactMarkdown = ({ children: _children, ...props }) => {
|
|
|
266
301
|
components,
|
|
267
302
|
};
|
|
268
303
|
}, [currentPlugins]);
|
|
269
|
-
const children =
|
|
304
|
+
const children = React.useMemo(() => {
|
|
270
305
|
/** 如果存在数学公式插件,并且数学公式分隔符为括号,则替换成 $ 符号 */
|
|
271
306
|
if (hasKatexPlugin && mathSplitSymbol === 'bracket') {
|
|
272
307
|
return replaceMathBracket(_children);
|
|
@@ -281,30 +316,30 @@ const HighReactMarkdown = ({ children: _children, ...props }) => {
|
|
|
281
316
|
...components,
|
|
282
317
|
}, ...props, children: children }));
|
|
283
318
|
};
|
|
284
|
-
var HighReactMarkdown$1 =
|
|
319
|
+
var HighReactMarkdown$1 = React.memo(HighReactMarkdown);
|
|
285
320
|
|
|
286
321
|
const useTypingTask = (options) => {
|
|
287
322
|
const { timerType = 'setTimeout', interval, charsRef, onEnd, onStart, onBeforeTypedChar, onTypedChar, processCharDisplay, wholeContentRef, disableTyping, triggerUpdate, resetWholeContent, } = options;
|
|
288
323
|
/** 是否卸载 */
|
|
289
|
-
const isUnmountRef =
|
|
324
|
+
const isUnmountRef = React.useRef(false);
|
|
290
325
|
/** 是否正在打字 */
|
|
291
|
-
const isTypingRef =
|
|
326
|
+
const isTypingRef = React.useRef(false);
|
|
292
327
|
/** 动画帧ID */
|
|
293
|
-
const animationFrameRef =
|
|
328
|
+
const animationFrameRef = React.useRef(null);
|
|
294
329
|
/** 传统定时器(兼容模式) */
|
|
295
|
-
const timerRef =
|
|
330
|
+
const timerRef = React.useRef(null);
|
|
296
331
|
// 已经打过的字记录
|
|
297
|
-
const typedCharsRef =
|
|
332
|
+
const typedCharsRef = React.useRef(undefined);
|
|
298
333
|
// 是否主动调用 stop 方法
|
|
299
|
-
const typedIsManualStopRef =
|
|
300
|
-
const disableTypingRef =
|
|
334
|
+
const typedIsManualStopRef = React.useRef(false);
|
|
335
|
+
const disableTypingRef = React.useRef(disableTyping);
|
|
301
336
|
disableTypingRef.current = disableTyping;
|
|
302
|
-
const intervalRef =
|
|
337
|
+
const intervalRef = React.useRef(interval);
|
|
303
338
|
intervalRef.current = interval;
|
|
304
339
|
const getChars = () => {
|
|
305
340
|
return charsRef.current;
|
|
306
341
|
};
|
|
307
|
-
|
|
342
|
+
React.useEffect(() => {
|
|
308
343
|
isUnmountRef.current = false;
|
|
309
344
|
return () => {
|
|
310
345
|
isUnmountRef.current = true;
|
|
@@ -617,30 +652,30 @@ const useTypingTask = (options) => {
|
|
|
617
652
|
};
|
|
618
653
|
};
|
|
619
654
|
|
|
620
|
-
const MarkdownContext =
|
|
655
|
+
const MarkdownContext = React.createContext({});
|
|
621
656
|
const MarkdownProvider = ({ value, children }) => {
|
|
622
|
-
const contextValue =
|
|
657
|
+
const contextValue = React.useMemo(() => value, [value]);
|
|
623
658
|
return jsxRuntime.jsx(MarkdownContext.Provider, { value: contextValue, children: children });
|
|
624
659
|
};
|
|
625
660
|
|
|
626
|
-
const MarkdownCMDInner =
|
|
661
|
+
const MarkdownCMDInner = React.forwardRef(({ interval = 30, onEnd, onStart, onTypedChar, onBeforeTypedChar, timerType = 'setTimeout', disableTyping = false, autoStartTyping = true }, ref) => {
|
|
627
662
|
const { state: themeState } = useMarkdownThemeContext();
|
|
628
663
|
// 从 context 中获取主题配置
|
|
629
664
|
const currentTheme = themeState.theme;
|
|
630
665
|
/** 是否自动开启打字动画, 后面发生变化将不会生效 */
|
|
631
|
-
const autoStartTypingRef =
|
|
666
|
+
const autoStartTypingRef = React.useRef(autoStartTyping);
|
|
632
667
|
/** 是否打过字 */
|
|
633
|
-
const isStartedTypingRef =
|
|
668
|
+
const isStartedTypingRef = React.useRef(false);
|
|
634
669
|
/** 当前需要打字的内容 */
|
|
635
|
-
const charsRef =
|
|
670
|
+
const charsRef = React.useRef([]);
|
|
636
671
|
/**
|
|
637
672
|
* 打字是否已经完全结束
|
|
638
673
|
* 如果打字已经完全结束,则不会再触发打字效果
|
|
639
674
|
*/
|
|
640
|
-
const isWholeTypedEndRef =
|
|
641
|
-
const charIndexRef =
|
|
675
|
+
const isWholeTypedEndRef = React.useRef(false);
|
|
676
|
+
const charIndexRef = React.useRef(0);
|
|
642
677
|
/** 整个内容引用 */
|
|
643
|
-
const wholeContentRef =
|
|
678
|
+
const wholeContentRef = React.useRef({
|
|
644
679
|
thinking: {
|
|
645
680
|
content: '',
|
|
646
681
|
length: 0,
|
|
@@ -653,7 +688,7 @@ const MarkdownCMDInner = react.forwardRef(({ interval = 30, onEnd, onStart, onTy
|
|
|
653
688
|
},
|
|
654
689
|
allLength: 0,
|
|
655
690
|
});
|
|
656
|
-
const [, setUpdate] =
|
|
691
|
+
const [, setUpdate] = React.useState(0);
|
|
657
692
|
const triggerUpdate = () => {
|
|
658
693
|
setUpdate((prev) => prev + 1);
|
|
659
694
|
};
|
|
@@ -737,7 +772,7 @@ const MarkdownCMDInner = react.forwardRef(({ interval = 30, onEnd, onStart, onTy
|
|
|
737
772
|
manual: false,
|
|
738
773
|
});
|
|
739
774
|
};
|
|
740
|
-
|
|
775
|
+
React.useImperativeHandle(ref, () => ({
|
|
741
776
|
/**
|
|
742
777
|
* 添加内容
|
|
743
778
|
* @param content 内容 {string}
|
|
@@ -810,7 +845,7 @@ const MarkdownCMDInner = react.forwardRef(({ interval = 30, onEnd, onStart, onTy
|
|
|
810
845
|
if (__DEV__) {
|
|
811
846
|
MarkdownCMDInner.displayName = 'MarkdownCMD';
|
|
812
847
|
}
|
|
813
|
-
const MarkdownCMD =
|
|
848
|
+
const MarkdownCMD = React.forwardRef((props, ref) => {
|
|
814
849
|
const { children = '', answerType = 'answer', isInnerRender, ...reset } = props;
|
|
815
850
|
if (__DEV__) {
|
|
816
851
|
if (!['thinking', 'answer'].includes(answerType)) {
|
|
@@ -820,9 +855,9 @@ const MarkdownCMD = react.forwardRef((props, ref) => {
|
|
|
820
855
|
throw new Error('Markdown组件的子元素必须是一个字符串');
|
|
821
856
|
}
|
|
822
857
|
}
|
|
823
|
-
const contextValue =
|
|
858
|
+
const contextValue = React.useMemo(() => ({ ...reset, answerType }), [reset, answerType]);
|
|
824
859
|
// 分离主题相关的 props
|
|
825
|
-
const themeProps =
|
|
860
|
+
const themeProps = React.useMemo(() => ({
|
|
826
861
|
theme: props.theme || DEFAULT_THEME,
|
|
827
862
|
math: props.math,
|
|
828
863
|
codeBlock: props.codeBlock,
|
|
@@ -837,9 +872,9 @@ const MarkdownCMD = react.forwardRef((props, ref) => {
|
|
|
837
872
|
});
|
|
838
873
|
|
|
839
874
|
const MarkdownInner = ({ children: _children = '', answerType, markdownRef, ...rest }) => {
|
|
840
|
-
const cmdRef =
|
|
841
|
-
const prefixRef =
|
|
842
|
-
const content =
|
|
875
|
+
const cmdRef = React.useRef(null);
|
|
876
|
+
const prefixRef = React.useRef('');
|
|
877
|
+
const content = React.useMemo(() => {
|
|
843
878
|
if (typeof _children === 'string') {
|
|
844
879
|
return _children;
|
|
845
880
|
}
|
|
@@ -848,7 +883,7 @@ const MarkdownInner = ({ children: _children = '', answerType, markdownRef, ...r
|
|
|
848
883
|
}
|
|
849
884
|
return '';
|
|
850
885
|
}, [_children]);
|
|
851
|
-
|
|
886
|
+
React.useEffect(() => {
|
|
852
887
|
if (prefixRef.current !== content) {
|
|
853
888
|
let newContent = '';
|
|
854
889
|
if (prefixRef.current === '') {
|
|
@@ -867,7 +902,7 @@ const MarkdownInner = ({ children: _children = '', answerType, markdownRef, ...r
|
|
|
867
902
|
prefixRef.current = content;
|
|
868
903
|
}
|
|
869
904
|
}, [answerType, content]);
|
|
870
|
-
|
|
905
|
+
React.useImperativeHandle(markdownRef, () => ({
|
|
871
906
|
stop: () => {
|
|
872
907
|
cmdRef.current.stop();
|
|
873
908
|
},
|
|
@@ -885,7 +920,7 @@ const MarkdownInner = ({ children: _children = '', answerType, markdownRef, ...r
|
|
|
885
920
|
const { theme, math, plugins, codeBlock, ...baseProps } = rest;
|
|
886
921
|
return jsxRuntime.jsx(MarkdownCMD, { ref: cmdRef, ...baseProps, isInnerRender: true });
|
|
887
922
|
};
|
|
888
|
-
const Markdown =
|
|
923
|
+
const Markdown = React.forwardRef((props, ref) => {
|
|
889
924
|
const { children = '', answerType = 'answer', ...reset } = props;
|
|
890
925
|
if (__DEV__) {
|
|
891
926
|
if (!['thinking', 'answer'].includes(answerType)) {
|
|
@@ -895,9 +930,9 @@ const Markdown = react.forwardRef((props, ref) => {
|
|
|
895
930
|
throw new Error('Markdown组件的子元素必须是一个字符串');
|
|
896
931
|
}
|
|
897
932
|
}
|
|
898
|
-
const contextValue =
|
|
933
|
+
const contextValue = React.useMemo(() => ({ ...reset, answerType }), [reset, answerType]);
|
|
899
934
|
// 分离主题相关的 props
|
|
900
|
-
const themeProps =
|
|
935
|
+
const themeProps = React.useMemo(() => ({
|
|
901
936
|
theme: props.theme || DEFAULT_THEME,
|
|
902
937
|
math: props.math,
|
|
903
938
|
codeBlock: props.codeBlock,
|
|
@@ -906,14 +941,184 @@ const Markdown = react.forwardRef((props, ref) => {
|
|
|
906
941
|
}), [props.theme, props.math, props.codeBlock, props.plugins, props.answerType]);
|
|
907
942
|
return (jsxRuntime.jsx(MarkdownProvider, { value: contextValue, children: jsxRuntime.jsx(MarkdownThemeProvider, { value: themeProps, children: jsxRuntime.jsx(MarkdownInner, { ...props, answerType: answerType, markdownRef: ref }) }) }));
|
|
908
943
|
});
|
|
909
|
-
var Markdown$1 =
|
|
944
|
+
var Markdown$1 = React.memo(Markdown);
|
|
945
|
+
|
|
946
|
+
const IconButton = ({ icon, style, className = '', onClick, ...restProps }) => {
|
|
947
|
+
return jsxRuntime.jsx(Button, { icon: icon, className: `ds-icon-button ${className}`, style: style, onClick: onClick, ...restProps });
|
|
948
|
+
};
|
|
949
|
+
|
|
950
|
+
const ToolTip = React.forwardRef(({ title, children, position = 'top', className = '', style, showArrow = true, delay = 200 }, ref) => {
|
|
951
|
+
const [isVisible, setIsVisible] = React.useState(false);
|
|
952
|
+
const [tooltipStyle, setTooltipStyle] = React.useState({});
|
|
953
|
+
const triggerRef = React.useRef(null);
|
|
954
|
+
const tooltipRef = React.useRef(null);
|
|
955
|
+
const timeoutRef = React.useRef(undefined);
|
|
956
|
+
// 转发ref到trigger元素
|
|
957
|
+
React.useImperativeHandle(ref, () => triggerRef.current, []);
|
|
958
|
+
const showTooltip = React.useCallback(() => {
|
|
959
|
+
timeoutRef.current = window.setTimeout(() => {
|
|
960
|
+
setIsVisible(true);
|
|
961
|
+
}, delay);
|
|
962
|
+
}, [delay]);
|
|
963
|
+
const hideTooltip = React.useCallback(() => {
|
|
964
|
+
if (timeoutRef.current) {
|
|
965
|
+
clearTimeout(timeoutRef.current);
|
|
966
|
+
}
|
|
967
|
+
setIsVisible(false);
|
|
968
|
+
}, []);
|
|
969
|
+
const updatePosition = React.useCallback(() => {
|
|
970
|
+
if (!triggerRef.current || !tooltipRef.current)
|
|
971
|
+
return;
|
|
972
|
+
const triggerRect = triggerRef.current.getBoundingClientRect();
|
|
973
|
+
const tooltipRect = tooltipRef.current.getBoundingClientRect();
|
|
974
|
+
let top = 0;
|
|
975
|
+
let left = 0;
|
|
976
|
+
switch (position) {
|
|
977
|
+
case 'top':
|
|
978
|
+
top = triggerRect.top - tooltipRect.height - 8;
|
|
979
|
+
left = triggerRect.left + triggerRect.width / 2 - tooltipRect.width / 2;
|
|
980
|
+
break;
|
|
981
|
+
case 'bottom':
|
|
982
|
+
top = triggerRect.bottom + 8;
|
|
983
|
+
left = triggerRect.left + triggerRect.width / 2 - tooltipRect.width / 2;
|
|
984
|
+
break;
|
|
985
|
+
case 'left':
|
|
986
|
+
top = triggerRect.top + triggerRect.height / 2 - tooltipRect.height / 2;
|
|
987
|
+
left = triggerRect.left - tooltipRect.width - 8;
|
|
988
|
+
break;
|
|
989
|
+
case 'right':
|
|
990
|
+
top = triggerRect.top + triggerRect.height / 2 - tooltipRect.height / 2;
|
|
991
|
+
left = triggerRect.right + 8;
|
|
992
|
+
break;
|
|
993
|
+
}
|
|
994
|
+
setTooltipStyle({
|
|
995
|
+
position: 'fixed',
|
|
996
|
+
top: `${top}px`,
|
|
997
|
+
left: `${left}px`,
|
|
998
|
+
zIndex: 1000,
|
|
999
|
+
});
|
|
1000
|
+
}, [position]);
|
|
1001
|
+
React.useEffect(() => {
|
|
1002
|
+
if (isVisible) {
|
|
1003
|
+
updatePosition();
|
|
1004
|
+
}
|
|
1005
|
+
}, [isVisible, position, updatePosition]);
|
|
1006
|
+
React.useEffect(() => {
|
|
1007
|
+
const handleScroll = () => {
|
|
1008
|
+
if (isVisible) {
|
|
1009
|
+
updatePosition();
|
|
1010
|
+
}
|
|
1011
|
+
};
|
|
1012
|
+
const handleResize = () => {
|
|
1013
|
+
if (isVisible) {
|
|
1014
|
+
updatePosition();
|
|
1015
|
+
}
|
|
1016
|
+
};
|
|
1017
|
+
window.addEventListener('scroll', handleScroll, true);
|
|
1018
|
+
window.addEventListener('resize', handleResize);
|
|
1019
|
+
return () => {
|
|
1020
|
+
window.removeEventListener('scroll', handleScroll, true);
|
|
1021
|
+
window.removeEventListener('resize', handleResize);
|
|
1022
|
+
};
|
|
1023
|
+
}, [isVisible, updatePosition]);
|
|
1024
|
+
React.useEffect(() => {
|
|
1025
|
+
return () => {
|
|
1026
|
+
if (timeoutRef.current) {
|
|
1027
|
+
clearTimeout(timeoutRef.current);
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
}, []);
|
|
1031
|
+
const renderTooltip = () => {
|
|
1032
|
+
if (!isVisible)
|
|
1033
|
+
return null;
|
|
1034
|
+
const tooltipElement = (jsxRuntime.jsxs("div", { ref: tooltipRef, className: classNames('ds-tooltip', `ds-tooltip--${position}`), style: tooltipStyle, children: [jsxRuntime.jsx("div", { className: "ds-tooltip__title", children: title }), showArrow && jsxRuntime.jsx("div", { className: classNames('ds-tooltip__arrow', `ds-tooltip__arrow--${position}`) })] }));
|
|
1035
|
+
return reactDom.createPortal(tooltipElement, document.body);
|
|
1036
|
+
};
|
|
1037
|
+
// 合并原有的事件处理器
|
|
1038
|
+
const handleMouseEnter = React.useCallback((e) => {
|
|
1039
|
+
e.stopPropagation(); // 阻止事件冒泡
|
|
1040
|
+
showTooltip();
|
|
1041
|
+
// 只在子元素有onMouseEnter时才调用
|
|
1042
|
+
const props = children.props;
|
|
1043
|
+
if (props.onMouseEnter) {
|
|
1044
|
+
props.onMouseEnter(e);
|
|
1045
|
+
}
|
|
1046
|
+
}, [children.props, showTooltip]);
|
|
1047
|
+
const handleMouseLeave = React.useCallback((e) => {
|
|
1048
|
+
e.stopPropagation(); // 阻止事件冒泡
|
|
1049
|
+
hideTooltip();
|
|
1050
|
+
// 只在子元素有onMouseLeave时才调用
|
|
1051
|
+
const props = children.props;
|
|
1052
|
+
if (props.onMouseLeave) {
|
|
1053
|
+
props.onMouseLeave(e);
|
|
1054
|
+
}
|
|
1055
|
+
}, [children.props, hideTooltip]);
|
|
1056
|
+
const handleFocus = React.useCallback((e) => {
|
|
1057
|
+
e.stopPropagation(); // 阻止事件冒泡
|
|
1058
|
+
showTooltip();
|
|
1059
|
+
// 只在子元素有onFocus时才调用
|
|
1060
|
+
const props = children.props;
|
|
1061
|
+
if (props.onFocus) {
|
|
1062
|
+
props.onFocus(e);
|
|
1063
|
+
}
|
|
1064
|
+
}, [children.props, showTooltip]);
|
|
1065
|
+
const handleBlur = React.useCallback((e) => {
|
|
1066
|
+
e.stopPropagation(); // 阻止事件冒泡
|
|
1067
|
+
hideTooltip();
|
|
1068
|
+
// 只在子元素有onBlur时才调用
|
|
1069
|
+
const props = children.props;
|
|
1070
|
+
if (props.onBlur) {
|
|
1071
|
+
props.onBlur(e);
|
|
1072
|
+
}
|
|
1073
|
+
}, [children.props, hideTooltip]);
|
|
1074
|
+
// 处理onClick事件
|
|
1075
|
+
const handleClick = React.useCallback((e) => {
|
|
1076
|
+
// 调用原有的onClick
|
|
1077
|
+
const props = children.props;
|
|
1078
|
+
if (props.onClick) {
|
|
1079
|
+
props.onClick(e);
|
|
1080
|
+
}
|
|
1081
|
+
}, [children.props]);
|
|
1082
|
+
// 检查组件是否支持ref
|
|
1083
|
+
const isForwardRef = React.isValidElement(children) &&
|
|
1084
|
+
typeof children.type === 'function' &&
|
|
1085
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1086
|
+
children.type.$$typeof === Symbol.for('react.forward_ref');
|
|
1087
|
+
// 如果组件不支持ref,使用包装div的方式
|
|
1088
|
+
if (!isForwardRef && typeof children.type === 'function') {
|
|
1089
|
+
return (jsxRuntime.jsxs("div", { ref: triggerRef, className: classNames('ds-tooltip-trigger', className), style: style, onMouseEnter: showTooltip, onMouseLeave: hideTooltip, onFocus: showTooltip, onBlur: hideTooltip, children: [children, renderTooltip()] }));
|
|
1090
|
+
}
|
|
1091
|
+
// 使用 cloneElement 直接给子元素添加事件监听器和ref
|
|
1092
|
+
const enhancedChild = React.cloneElement(children, {
|
|
1093
|
+
ref: triggerRef,
|
|
1094
|
+
className: classNames(children.props.className, className),
|
|
1095
|
+
style: { ...children.props.style, ...style },
|
|
1096
|
+
onMouseEnter: handleMouseEnter,
|
|
1097
|
+
onMouseLeave: handleMouseLeave,
|
|
1098
|
+
onFocus: handleFocus,
|
|
1099
|
+
onBlur: handleBlur,
|
|
1100
|
+
onClick: handleClick,
|
|
1101
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1102
|
+
});
|
|
1103
|
+
return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [enhancedChild, renderTooltip()] }));
|
|
1104
|
+
});
|
|
1105
|
+
ToolTip.displayName = 'ToolTip';
|
|
910
1106
|
|
|
911
1107
|
/* eslint-disable react-refresh/only-export-components */
|
|
912
1108
|
|
|
1109
|
+
exports.Button = Button;
|
|
1110
|
+
exports.CodeBlockActions = CodeBlockActions;
|
|
1111
|
+
exports.CodeBlockWrap = CodeBlockWrap;
|
|
913
1112
|
exports.ConfigProvider = ConfigProvider;
|
|
1113
|
+
exports.CopyButton = CopyButton;
|
|
1114
|
+
exports.DownloadButton = DownloadButton;
|
|
1115
|
+
exports.HighlightCode = HighlightCode;
|
|
1116
|
+
exports.IconButton = IconButton;
|
|
914
1117
|
exports.Markdown = Markdown$1;
|
|
915
1118
|
exports.MarkdownCMD = MarkdownCMD;
|
|
1119
|
+
exports.ToolTip = ToolTip;
|
|
916
1120
|
exports.default = Markdown$1;
|
|
917
1121
|
exports.useConfig = useConfig;
|
|
918
1122
|
exports.useLocale = useLocale;
|
|
1123
|
+
exports.useThemeState = useThemeState;
|
|
919
1124
|
//# sourceMappingURL=index.js.map
|