ds-markdown 0.1.4-beta.0 → 0.1.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/README.en.md +3 -1
- package/README.md +3 -1
- package/dist/cjs/index.js +669 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/plugins/index.js +24 -1
- package/dist/cjs/plugins/index.js.map +1 -1
- package/dist/esm/index.js +663 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/plugins/index.js +21 -1
- package/dist/esm/plugins/index.js.map +1 -1
- package/package.json +2 -2
- package/README.ja.md +0 -942
- package/README.ko.md +0 -805
package/dist/esm/index.js
CHANGED
|
@@ -1,2 +1,664 @@
|
|
|
1
|
-
import{jsxs
|
|
1
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { memo, useMemo, useRef, useEffect, forwardRef, useState, useImperativeHandle } from 'react';
|
|
3
|
+
import Markdown$2 from 'react-markdown';
|
|
4
|
+
import { Prism } from 'react-syntax-highlighter';
|
|
5
|
+
import gfmPlugin from 'remark-gfm';
|
|
6
|
+
import classNames from 'classnames';
|
|
7
|
+
|
|
8
|
+
const replaceMathBracket = (value) => {
|
|
9
|
+
// 1. 提取所有块级公式内容,临时替换为占位符, [...]
|
|
10
|
+
const blockMatches = [];
|
|
11
|
+
let replaced = value.replace(/\\*\[([\s\S]+?)\\*\]/g, (_m, p1) => {
|
|
12
|
+
blockMatches.push(p1);
|
|
13
|
+
return `__BLOCK_MATH_${blockMatches.length - 1}__`;
|
|
14
|
+
});
|
|
15
|
+
// 也需要兼容 $$ xxxx $$ 这种写法
|
|
16
|
+
replaced = replaced.replace(/\$\$([\s\S]+?)\$\$/g, (_m, p1) => {
|
|
17
|
+
blockMatches.push(p1);
|
|
18
|
+
return `__BLOCK_MATH_${blockMatches.length - 1}__`;
|
|
19
|
+
});
|
|
20
|
+
// 2. 替换块级公式外部的 ( ... ) 为 $...$
|
|
21
|
+
replaced = replaced.replace(/\\*\(([^)]+?)\\*\)/g, (_m, p1) => {
|
|
22
|
+
return '$' + p1 + '$';
|
|
23
|
+
});
|
|
24
|
+
// 3. 还原块级公式内容,保持其内部小括号原样
|
|
25
|
+
replaced = replaced.replace(/__BLOCK_MATH_(\d+)__/g, (_m, idx) => {
|
|
26
|
+
return '$$' + blockMatches[Number(idx)] + '$$';
|
|
27
|
+
});
|
|
28
|
+
return replaced;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const BlockWrap = ({ children, language, theme = 'light' }) => {
|
|
32
|
+
return (jsxs("div", { className: `md-code-block md-code-block-${theme}`, children: [jsx("div", { className: "md-code-block-banner-wrap", children: jsx("div", { className: "md-code-block-banner md-code-block-banner-lite", children: jsx("div", { className: "md-code-block-banner-content", children: jsx("div", { className: "md-code-block-language", children: language }) }) }) }), jsx("div", { className: "md-code-block-content", children: children })] }));
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const __DEV__ = process.env.NODE_ENV === 'development';
|
|
36
|
+
/** 数学公式插件id */
|
|
37
|
+
const katexId = Symbol('katex');
|
|
38
|
+
|
|
39
|
+
const HighReactMarkdown = ({ theme = 'light', children: _children, math, plugins, ...props }) => {
|
|
40
|
+
const mathSplitSymbol = math?.splitSymbol ?? 'dollar';
|
|
41
|
+
const { remarkPlugins, rehypePlugins, hasKatexPlugin } = useMemo(() => {
|
|
42
|
+
let hasKatexPlugin = false;
|
|
43
|
+
const remarkPlugins = [gfmPlugin];
|
|
44
|
+
const rehypePlugins = [];
|
|
45
|
+
if (!plugins) {
|
|
46
|
+
return {
|
|
47
|
+
remarkPlugins,
|
|
48
|
+
rehypePlugins,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
plugins.forEach((plugin) => {
|
|
52
|
+
if (plugin.id === katexId) {
|
|
53
|
+
hasKatexPlugin = true;
|
|
54
|
+
remarkPlugins.push(plugin.remarkPlugin);
|
|
55
|
+
rehypePlugins.push(plugin.rehypePlugin);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
remarkPlugins,
|
|
60
|
+
rehypePlugins,
|
|
61
|
+
hasKatexPlugin,
|
|
62
|
+
};
|
|
63
|
+
}, [plugins]);
|
|
64
|
+
const children = useMemo(() => {
|
|
65
|
+
/** 如果存在数学公式插件,并且数学公式分隔符为括号,则替换成 $ 符号 */
|
|
66
|
+
if (hasKatexPlugin && mathSplitSymbol === 'bracket') {
|
|
67
|
+
return replaceMathBracket(_children);
|
|
68
|
+
}
|
|
69
|
+
return _children;
|
|
70
|
+
}, [hasKatexPlugin, mathSplitSymbol, _children]);
|
|
71
|
+
return (jsx(Markdown$2, { remarkPlugins: remarkPlugins, rehypePlugins: rehypePlugins, components: {
|
|
72
|
+
code: ({ className, children, ...props }) => {
|
|
73
|
+
const match = /language-(\w+)/.exec(className || '');
|
|
74
|
+
return match ? (jsx(BlockWrap, { language: match[1], theme: theme, children: jsx(Prism, { useInlineStyles: false, language: match[1], style: {}, children: String(children).replace(/\n$/, '') }) })) : (jsx("code", { className: className, ...props, children: children }));
|
|
75
|
+
},
|
|
76
|
+
table: ({ children, ...props }) => {
|
|
77
|
+
return (jsx("div", { className: "markdown-table-wrapper", children: jsx("table", { className: "ds-markdown-table", children: children }) }));
|
|
78
|
+
},
|
|
79
|
+
}, ...props, children: children }));
|
|
80
|
+
};
|
|
81
|
+
var HighReactMarkdown$1 = memo(HighReactMarkdown);
|
|
82
|
+
|
|
83
|
+
const useTypingTask = (options) => {
|
|
84
|
+
const { timerType = 'setTimeout', interval, charsRef, onEnd, onStart, onBeforeTypedChar, onTypedChar, processCharDisplay, wholeContentRef, disableTyping, triggerUpdate, resetWholeContent, } = options;
|
|
85
|
+
/** 是否卸载 */
|
|
86
|
+
const isUnmountRef = useRef(false);
|
|
87
|
+
/** 是否正在打字 */
|
|
88
|
+
const isTypingRef = useRef(false);
|
|
89
|
+
/** 动画帧ID */
|
|
90
|
+
const animationFrameRef = useRef(null);
|
|
91
|
+
/** 传统定时器(兼容模式) */
|
|
92
|
+
const timerRef = useRef(null);
|
|
93
|
+
// 已经打过的字记录
|
|
94
|
+
const typedCharsRef = useRef(undefined);
|
|
95
|
+
// 是否主动调用 stop 方法
|
|
96
|
+
const typedIsManualStopRef = useRef(false);
|
|
97
|
+
const disableTypingRef = useRef(disableTyping);
|
|
98
|
+
disableTypingRef.current = disableTyping;
|
|
99
|
+
const intervalRef = useRef(interval);
|
|
100
|
+
intervalRef.current = interval;
|
|
101
|
+
const getChars = () => {
|
|
102
|
+
return charsRef.current;
|
|
103
|
+
};
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
isUnmountRef.current = false;
|
|
106
|
+
return () => {
|
|
107
|
+
isUnmountRef.current = true;
|
|
108
|
+
clearTimer();
|
|
109
|
+
};
|
|
110
|
+
}, []);
|
|
111
|
+
/**
|
|
112
|
+
* 触发打字开始回调
|
|
113
|
+
* @param char 当前字符
|
|
114
|
+
*/
|
|
115
|
+
const triggerOnStart = (char) => {
|
|
116
|
+
if (!onStart) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const prevStr = wholeContentRef.current[char.answerType].content;
|
|
120
|
+
onStart({
|
|
121
|
+
currentIndex: prevStr.length,
|
|
122
|
+
currentChar: char.content,
|
|
123
|
+
answerType: char.answerType,
|
|
124
|
+
prevStr,
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* 触发打字结束回调
|
|
129
|
+
*/
|
|
130
|
+
const triggerOnEnd = (data) => {
|
|
131
|
+
if (!onEnd) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
onEnd({
|
|
135
|
+
str: wholeContentRef.current.answer.content,
|
|
136
|
+
answerStr: wholeContentRef.current.answer.content,
|
|
137
|
+
thinkingStr: wholeContentRef.current.thinking.content,
|
|
138
|
+
manual: data?.manual ?? false,
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* 触发打字过程中回调
|
|
143
|
+
* @param char 当前字符
|
|
144
|
+
*/
|
|
145
|
+
const triggerOnBeforeTypedChar = async (char) => {
|
|
146
|
+
if (!onBeforeTypedChar) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const { answerType, content, index } = char;
|
|
150
|
+
const allLength = wholeContentRef.current.allLength;
|
|
151
|
+
// 计算之前字符的百分比
|
|
152
|
+
const percent = (char.index / allLength) * 100;
|
|
153
|
+
await onBeforeTypedChar({
|
|
154
|
+
currentIndex: index,
|
|
155
|
+
currentChar: content,
|
|
156
|
+
answerType: answerType,
|
|
157
|
+
prevStr: wholeContentRef.current[answerType].content,
|
|
158
|
+
percent,
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
/** 打字完成回调 */
|
|
162
|
+
const triggerOnTypedChar = async (char) => {
|
|
163
|
+
if (!onTypedChar) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const { answerType, content, index } = char;
|
|
167
|
+
const allLength = wholeContentRef.current.allLength;
|
|
168
|
+
const percent = ((char.index + 1) / allLength) * 100;
|
|
169
|
+
onTypedChar({
|
|
170
|
+
currentIndex: index,
|
|
171
|
+
currentChar: content,
|
|
172
|
+
answerType: answerType,
|
|
173
|
+
prevStr: wholeContentRef.current[answerType].content.slice(0, index),
|
|
174
|
+
currentStr: wholeContentRef.current[answerType].content,
|
|
175
|
+
percent,
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
/** 清除定时器 */
|
|
179
|
+
const clearTimer = () => {
|
|
180
|
+
// 清理 requestAnimationFrame
|
|
181
|
+
if (animationFrameRef.current) {
|
|
182
|
+
cancelAnimationFrame(animationFrameRef.current);
|
|
183
|
+
animationFrameRef.current = null;
|
|
184
|
+
}
|
|
185
|
+
// 清理 setTimeout (可能被 timestamp 模式使用)
|
|
186
|
+
if (timerRef.current) {
|
|
187
|
+
clearTimeout(timerRef.current);
|
|
188
|
+
timerRef.current = null;
|
|
189
|
+
}
|
|
190
|
+
isTypingRef.current = false;
|
|
191
|
+
typedCharsRef.current = undefined;
|
|
192
|
+
};
|
|
193
|
+
/** 开始打字任务 */
|
|
194
|
+
const startTypedTask = () => {
|
|
195
|
+
/** 如果手动调用 stop 方法,则不重新开始打字 */
|
|
196
|
+
if (typedIsManualStopRef.current) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (isTypingRef.current) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (timerType === 'requestAnimationFrame') {
|
|
203
|
+
startAnimationFrameMode();
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
startTimeoutMode();
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
/** 打字机打完所有字符 */
|
|
210
|
+
async function typingRemainAll() {
|
|
211
|
+
const chars = getChars();
|
|
212
|
+
const thinkingCharsStr = chars
|
|
213
|
+
.filter((char) => char.answerType === 'thinking')
|
|
214
|
+
.map((char) => char.content)
|
|
215
|
+
.join('');
|
|
216
|
+
const answerCharsStr = chars
|
|
217
|
+
.filter((char) => char.answerType === 'answer')
|
|
218
|
+
.map((char) => char.content)
|
|
219
|
+
.join('');
|
|
220
|
+
if (thinkingCharsStr) {
|
|
221
|
+
await onBeforeTypedChar?.({
|
|
222
|
+
currentIndex: wholeContentRef.current.thinking.length,
|
|
223
|
+
currentChar: thinkingCharsStr,
|
|
224
|
+
answerType: 'thinking',
|
|
225
|
+
prevStr: wholeContentRef.current.thinking.content,
|
|
226
|
+
percent: 100,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
if (answerCharsStr) {
|
|
230
|
+
await onBeforeTypedChar?.({
|
|
231
|
+
currentIndex: wholeContentRef.current.answer.length,
|
|
232
|
+
currentChar: answerCharsStr,
|
|
233
|
+
answerType: 'answer',
|
|
234
|
+
prevStr: wholeContentRef.current.answer.content,
|
|
235
|
+
percent: 100,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
wholeContentRef.current.thinking.content += thinkingCharsStr;
|
|
239
|
+
wholeContentRef.current.thinking.length += thinkingCharsStr.length;
|
|
240
|
+
wholeContentRef.current.answer.content += answerCharsStr;
|
|
241
|
+
wholeContentRef.current.answer.length += answerCharsStr.length;
|
|
242
|
+
wholeContentRef.current.allLength += thinkingCharsStr.length + answerCharsStr.length;
|
|
243
|
+
charsRef.current = [];
|
|
244
|
+
isTypingRef.current = false;
|
|
245
|
+
triggerOnEnd();
|
|
246
|
+
triggerUpdate();
|
|
247
|
+
}
|
|
248
|
+
/** requestAnimationFrame 模式 */
|
|
249
|
+
const startAnimationFrameMode = () => {
|
|
250
|
+
let lastFrameTime = performance.now();
|
|
251
|
+
const frameLoop = async (currentTime) => {
|
|
252
|
+
// 如果关闭打字机效果,则打完所有字符
|
|
253
|
+
if (disableTypingRef.current) {
|
|
254
|
+
await typingRemainAll();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const chars = getChars();
|
|
258
|
+
if (isUnmountRef.current)
|
|
259
|
+
return;
|
|
260
|
+
if (chars.length === 0) {
|
|
261
|
+
stopAnimationFrame();
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const deltaTime = currentTime - lastFrameTime;
|
|
265
|
+
let needToTypingCharsLength = Math.max(0, Math.floor(deltaTime / intervalRef.current));
|
|
266
|
+
needToTypingCharsLength = Math.min(needToTypingCharsLength, chars.length);
|
|
267
|
+
if (needToTypingCharsLength > 0) {
|
|
268
|
+
// 处理字符
|
|
269
|
+
for (let i = 0; i < needToTypingCharsLength; i++) {
|
|
270
|
+
const char = chars.shift();
|
|
271
|
+
if (char === undefined)
|
|
272
|
+
break;
|
|
273
|
+
if (!isTypingRef.current) {
|
|
274
|
+
isTypingRef.current = true;
|
|
275
|
+
triggerOnStart(char);
|
|
276
|
+
}
|
|
277
|
+
/** 打字前回调 */
|
|
278
|
+
await triggerOnBeforeTypedChar(char);
|
|
279
|
+
processCharDisplay(char);
|
|
280
|
+
/** 打字完成回调 */
|
|
281
|
+
triggerOnTypedChar(char);
|
|
282
|
+
}
|
|
283
|
+
lastFrameTime = performance.now();
|
|
284
|
+
// 继续下一帧
|
|
285
|
+
if (chars.length > 0) {
|
|
286
|
+
animationFrameRef.current = requestAnimationFrame(frameLoop);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
isTypingRef.current = false;
|
|
290
|
+
triggerOnEnd();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
// 本次你不需要打字,继续下一帧
|
|
295
|
+
animationFrameRef.current = requestAnimationFrame(frameLoop);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
animationFrameRef.current = requestAnimationFrame(frameLoop);
|
|
299
|
+
};
|
|
300
|
+
/** 停止动画帧模式 */
|
|
301
|
+
const stopAnimationFrame = (manual = false) => {
|
|
302
|
+
isTypingRef.current = false;
|
|
303
|
+
if (animationFrameRef.current) {
|
|
304
|
+
cancelAnimationFrame(animationFrameRef.current);
|
|
305
|
+
animationFrameRef.current = null;
|
|
306
|
+
}
|
|
307
|
+
if (!manual) {
|
|
308
|
+
triggerOnEnd({ manual });
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
/** setTimeout 模式 */
|
|
312
|
+
const startTimeoutMode = () => {
|
|
313
|
+
const nextTyped = () => {
|
|
314
|
+
const chars = getChars();
|
|
315
|
+
if (chars.length === 0) {
|
|
316
|
+
stopTimeout();
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
timerRef.current = setTimeout(startTyped, intervalRef.current);
|
|
320
|
+
};
|
|
321
|
+
const startTyped = async (isStartPoint = false) => {
|
|
322
|
+
// 如果关闭打字机效果,则打完所有字符
|
|
323
|
+
if (disableTypingRef.current) {
|
|
324
|
+
typingRemainAll();
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
const chars = getChars();
|
|
328
|
+
if (isUnmountRef.current)
|
|
329
|
+
return;
|
|
330
|
+
isTypingRef.current = true;
|
|
331
|
+
const char = chars.shift();
|
|
332
|
+
if (char === undefined) {
|
|
333
|
+
stopTimeout();
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
if (isStartPoint) {
|
|
337
|
+
triggerOnStart(char);
|
|
338
|
+
}
|
|
339
|
+
/** 打字前回调 */
|
|
340
|
+
await triggerOnBeforeTypedChar(char);
|
|
341
|
+
processCharDisplay(char);
|
|
342
|
+
/** 打字完成回调 */
|
|
343
|
+
triggerOnTypedChar(char);
|
|
344
|
+
nextTyped();
|
|
345
|
+
};
|
|
346
|
+
startTyped(true);
|
|
347
|
+
};
|
|
348
|
+
/** 停止超时模式 */
|
|
349
|
+
const stopTimeout = () => {
|
|
350
|
+
isTypingRef.current = false;
|
|
351
|
+
if (timerRef.current) {
|
|
352
|
+
clearTimeout(timerRef.current);
|
|
353
|
+
timerRef.current = null;
|
|
354
|
+
}
|
|
355
|
+
triggerOnEnd();
|
|
356
|
+
};
|
|
357
|
+
const cancelTask = () => {
|
|
358
|
+
if (timerType === 'requestAnimationFrame') {
|
|
359
|
+
stopAnimationFrame();
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
stopTimeout();
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
/** 暂时停止 */
|
|
366
|
+
const stopTask = () => {
|
|
367
|
+
typedIsManualStopRef.current = true;
|
|
368
|
+
cancelTask();
|
|
369
|
+
};
|
|
370
|
+
/** 停止打字任务 */
|
|
371
|
+
const endTask = () => {
|
|
372
|
+
cancelTask();
|
|
373
|
+
};
|
|
374
|
+
function restartTypedTask() {
|
|
375
|
+
endTask();
|
|
376
|
+
// 将wholeContentRef的内容放到charsRef中
|
|
377
|
+
charsRef.current.unshift(...wholeContentRef.current.thinking.content.split('').map((charUnit) => {
|
|
378
|
+
const char = {
|
|
379
|
+
content: charUnit,
|
|
380
|
+
answerType: 'thinking',
|
|
381
|
+
tokenId: 0,
|
|
382
|
+
index: 0,
|
|
383
|
+
};
|
|
384
|
+
return char;
|
|
385
|
+
}));
|
|
386
|
+
charsRef.current.unshift(...wholeContentRef.current.answer.content.split('').map((charUnit) => {
|
|
387
|
+
const char = {
|
|
388
|
+
content: charUnit,
|
|
389
|
+
answerType: 'answer',
|
|
390
|
+
tokenId: 0,
|
|
391
|
+
index: 0,
|
|
392
|
+
};
|
|
393
|
+
return char;
|
|
394
|
+
}));
|
|
395
|
+
resetWholeContent();
|
|
396
|
+
triggerUpdate();
|
|
397
|
+
startTypedTask();
|
|
398
|
+
}
|
|
399
|
+
function clear() {
|
|
400
|
+
clearTimer();
|
|
401
|
+
}
|
|
402
|
+
function resume() {
|
|
403
|
+
typedIsManualStopRef.current = false;
|
|
404
|
+
startTypedTask();
|
|
405
|
+
}
|
|
406
|
+
return {
|
|
407
|
+
start: startTypedTask,
|
|
408
|
+
restart: restartTypedTask,
|
|
409
|
+
stop: stopTask,
|
|
410
|
+
resume: resume,
|
|
411
|
+
clear: clear,
|
|
412
|
+
isTyping: () => isTypingRef.current,
|
|
413
|
+
typedIsManualStopRef,
|
|
414
|
+
};
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
const MarkdownCMD = forwardRef(({ interval = 30, onEnd, onStart, onTypedChar, onBeforeTypedChar, timerType = 'setTimeout', theme = 'light', math, plugins, disableTyping = false, autoStartTyping = true }, ref) => {
|
|
418
|
+
/** 是否自动开启打字动画, 后面发生变化将不会生效 */
|
|
419
|
+
const autoStartTypingRef = useRef(autoStartTyping);
|
|
420
|
+
/** 是否打过字 */
|
|
421
|
+
const isStartedTypingRef = useRef(false);
|
|
422
|
+
/** 当前需要打字的内容 */
|
|
423
|
+
const charsRef = useRef([]);
|
|
424
|
+
/**
|
|
425
|
+
* 打字是否已经完全结束
|
|
426
|
+
* 如果打字已经完全结束,则不会再触发打字效果
|
|
427
|
+
*/
|
|
428
|
+
const isWholeTypedEndRef = useRef(false);
|
|
429
|
+
const charIndexRef = useRef(0);
|
|
430
|
+
/** 整个内容引用 */
|
|
431
|
+
const wholeContentRef = useRef({
|
|
432
|
+
thinking: {
|
|
433
|
+
content: '',
|
|
434
|
+
length: 0,
|
|
435
|
+
prevLength: 0,
|
|
436
|
+
},
|
|
437
|
+
answer: {
|
|
438
|
+
content: '',
|
|
439
|
+
length: 0,
|
|
440
|
+
prevLength: 0,
|
|
441
|
+
},
|
|
442
|
+
allLength: 0,
|
|
443
|
+
});
|
|
444
|
+
const [, setUpdate] = useState(0);
|
|
445
|
+
const triggerUpdate = () => {
|
|
446
|
+
setUpdate((prev) => prev + 1);
|
|
447
|
+
};
|
|
448
|
+
/**
|
|
449
|
+
* 处理字符显示逻辑
|
|
450
|
+
*/
|
|
451
|
+
const processCharDisplay = (char) => {
|
|
452
|
+
if (!isStartedTypingRef.current) {
|
|
453
|
+
isStartedTypingRef.current = true;
|
|
454
|
+
}
|
|
455
|
+
if (char.answerType === 'thinking') {
|
|
456
|
+
wholeContentRef.current.thinking.content += char.content;
|
|
457
|
+
wholeContentRef.current.thinking.length += 1;
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
wholeContentRef.current.answer.content += char.content;
|
|
461
|
+
wholeContentRef.current.answer.length += 1;
|
|
462
|
+
}
|
|
463
|
+
triggerUpdate();
|
|
464
|
+
};
|
|
465
|
+
const resetWholeContent = () => {
|
|
466
|
+
wholeContentRef.current.thinking.content = '';
|
|
467
|
+
wholeContentRef.current.thinking.length = 0;
|
|
468
|
+
wholeContentRef.current.thinking.prevLength = 0;
|
|
469
|
+
wholeContentRef.current.answer.content = '';
|
|
470
|
+
wholeContentRef.current.answer.length = 0;
|
|
471
|
+
wholeContentRef.current.answer.prevLength = 0;
|
|
472
|
+
wholeContentRef.current.allLength = 0;
|
|
473
|
+
};
|
|
474
|
+
// 使用新的打字任务 hook
|
|
475
|
+
const typingTask = useTypingTask({
|
|
476
|
+
timerType,
|
|
477
|
+
interval,
|
|
478
|
+
charsRef,
|
|
479
|
+
onEnd,
|
|
480
|
+
onStart,
|
|
481
|
+
onTypedChar,
|
|
482
|
+
onBeforeTypedChar,
|
|
483
|
+
processCharDisplay,
|
|
484
|
+
wholeContentRef,
|
|
485
|
+
disableTyping,
|
|
486
|
+
triggerUpdate,
|
|
487
|
+
resetWholeContent,
|
|
488
|
+
});
|
|
489
|
+
/**
|
|
490
|
+
* 内部推送处理逻辑
|
|
491
|
+
*/
|
|
492
|
+
const processHasTypingPush = (content, answerType) => {
|
|
493
|
+
if (content.length === 0) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
charsRef.current.push(...content.split('').map((chatStr) => {
|
|
497
|
+
const index = charIndexRef.current++;
|
|
498
|
+
const charObj = {
|
|
499
|
+
content: chatStr,
|
|
500
|
+
answerType,
|
|
501
|
+
tokenId: 0,
|
|
502
|
+
index,
|
|
503
|
+
};
|
|
504
|
+
return charObj;
|
|
505
|
+
}));
|
|
506
|
+
wholeContentRef.current.allLength += content.length;
|
|
507
|
+
// 如果关闭了自动打字, 并且没有打过字, 则不开启打字动画
|
|
508
|
+
if (!autoStartTypingRef.current && !isStartedTypingRef.current) {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (!typingTask.isTyping()) {
|
|
512
|
+
typingTask.start();
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
const processNoTypingPush = (content, answerType) => {
|
|
516
|
+
wholeContentRef.current[answerType].content += content;
|
|
517
|
+
// 记录打字前的长度
|
|
518
|
+
wholeContentRef.current[answerType].prevLength = wholeContentRef.current[answerType].length;
|
|
519
|
+
wholeContentRef.current[answerType].length += content.length;
|
|
520
|
+
triggerUpdate();
|
|
521
|
+
onEnd?.({
|
|
522
|
+
str: content,
|
|
523
|
+
answerStr: wholeContentRef.current.answer.content,
|
|
524
|
+
thinkingStr: wholeContentRef.current.thinking.content,
|
|
525
|
+
manual: false,
|
|
526
|
+
});
|
|
527
|
+
};
|
|
528
|
+
useImperativeHandle(ref, () => ({
|
|
529
|
+
/**
|
|
530
|
+
* 添加内容
|
|
531
|
+
* @param content 内容 {string}
|
|
532
|
+
* @param answerType 回答类型 {AnswerType}
|
|
533
|
+
*/
|
|
534
|
+
push: (content, answerType = 'answer') => {
|
|
535
|
+
if (disableTyping) {
|
|
536
|
+
processNoTypingPush(content, answerType);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
processHasTypingPush(content, answerType);
|
|
540
|
+
},
|
|
541
|
+
/**
|
|
542
|
+
* 清除打字任务
|
|
543
|
+
*/
|
|
544
|
+
clear: () => {
|
|
545
|
+
typingTask.stop();
|
|
546
|
+
typingTask.typedIsManualStopRef.current = false;
|
|
547
|
+
charsRef.current = [];
|
|
548
|
+
resetWholeContent();
|
|
549
|
+
isWholeTypedEndRef.current = false;
|
|
550
|
+
charIndexRef.current = 0;
|
|
551
|
+
isStartedTypingRef.current = false;
|
|
552
|
+
triggerUpdate();
|
|
553
|
+
},
|
|
554
|
+
/** 开启打字,只有在关闭了自动打字才生效 */
|
|
555
|
+
start: () => {
|
|
556
|
+
if (!autoStartTypingRef.current) {
|
|
557
|
+
typingTask.start();
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
/** 停止打字任务 */
|
|
561
|
+
stop: () => {
|
|
562
|
+
typingTask.stop();
|
|
563
|
+
},
|
|
564
|
+
/** 重新开始打字任务 */
|
|
565
|
+
resume: () => {
|
|
566
|
+
typingTask.resume();
|
|
567
|
+
},
|
|
568
|
+
/**
|
|
569
|
+
* 主动触发打字结束
|
|
570
|
+
*/
|
|
571
|
+
triggerWholeEnd: () => {
|
|
572
|
+
isWholeTypedEndRef.current = true;
|
|
573
|
+
if (!typingTask.isTyping()) {
|
|
574
|
+
// 这里需要手动触发结束回调,因为 hook 中的 triggerOnEnd 不能直接调用
|
|
575
|
+
onEnd?.({
|
|
576
|
+
str: wholeContentRef.current.answer.content,
|
|
577
|
+
answerStr: wholeContentRef.current.answer.content,
|
|
578
|
+
thinkingStr: wholeContentRef.current.thinking.content,
|
|
579
|
+
manual: true,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
},
|
|
583
|
+
/** 重新开始打字任务 */
|
|
584
|
+
restart: () => {
|
|
585
|
+
typingTask.restart();
|
|
586
|
+
},
|
|
587
|
+
}));
|
|
588
|
+
const getParagraphs = (answerType) => {
|
|
589
|
+
const content = wholeContentRef.current[answerType].content || '';
|
|
590
|
+
return (jsx("div", { className: `ds-markdown-paragraph ds-typed-${answerType}`, children: jsx(HighReactMarkdown$1, { theme: theme, math: math, plugins: plugins, children: content }) }));
|
|
591
|
+
};
|
|
592
|
+
return (jsxs("div", { className: classNames({
|
|
593
|
+
'ds-markdown': true,
|
|
594
|
+
apple: true,
|
|
595
|
+
'ds-markdown-dark': theme === 'dark',
|
|
596
|
+
}), children: [jsx("div", { className: "ds-markdown-thinking", children: getParagraphs('thinking') }), jsx("div", { className: "ds-markdown-answer", children: getParagraphs('answer') })] }));
|
|
597
|
+
});
|
|
598
|
+
if (__DEV__) {
|
|
599
|
+
MarkdownCMD.displayName = 'MarkdownCMD';
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const MarkdownInner = ({ children: _children = '', answerType, markdownRef, ...rest }) => {
|
|
603
|
+
const cmdRef = useRef(null);
|
|
604
|
+
const prefixRef = useRef('');
|
|
605
|
+
const content = useMemo(() => {
|
|
606
|
+
if (typeof _children === 'string') {
|
|
607
|
+
return _children;
|
|
608
|
+
}
|
|
609
|
+
if (__DEV__) {
|
|
610
|
+
console.error('Markdown组件的子元素必须是一个字符串');
|
|
611
|
+
}
|
|
612
|
+
return '';
|
|
613
|
+
}, [_children]);
|
|
614
|
+
useEffect(() => {
|
|
615
|
+
if (prefixRef.current !== content) {
|
|
616
|
+
let newContent = '';
|
|
617
|
+
if (prefixRef.current === '') {
|
|
618
|
+
newContent = content;
|
|
619
|
+
}
|
|
620
|
+
else {
|
|
621
|
+
if (content.startsWith(prefixRef.current)) {
|
|
622
|
+
newContent = content.slice(prefixRef.current.length);
|
|
623
|
+
}
|
|
624
|
+
else {
|
|
625
|
+
newContent = content;
|
|
626
|
+
cmdRef.current.clear();
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
cmdRef.current.push(newContent, answerType);
|
|
630
|
+
prefixRef.current = content;
|
|
631
|
+
}
|
|
632
|
+
}, [answerType, content]);
|
|
633
|
+
useImperativeHandle(markdownRef, () => ({
|
|
634
|
+
stop: () => {
|
|
635
|
+
cmdRef.current.stop();
|
|
636
|
+
},
|
|
637
|
+
resume: () => {
|
|
638
|
+
cmdRef.current.resume();
|
|
639
|
+
},
|
|
640
|
+
start: () => {
|
|
641
|
+
cmdRef.current.start();
|
|
642
|
+
},
|
|
643
|
+
restart: () => {
|
|
644
|
+
cmdRef.current.restart();
|
|
645
|
+
},
|
|
646
|
+
}));
|
|
647
|
+
return jsx(MarkdownCMD, { ref: cmdRef, ...rest });
|
|
648
|
+
};
|
|
649
|
+
const Markdown = forwardRef((props, ref) => {
|
|
650
|
+
const { children = '', answerType = 'answer' } = props;
|
|
651
|
+
if (__DEV__) {
|
|
652
|
+
if (!['thinking', 'answer'].includes(answerType)) {
|
|
653
|
+
throw new Error('Markdown组件的answerType必须是thinking或answer');
|
|
654
|
+
}
|
|
655
|
+
if (typeof children !== 'string') {
|
|
656
|
+
throw new Error('Markdown组件的子元素必须是一个字符串');
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
return jsx(MarkdownInner, { ...props, answerType: answerType, markdownRef: ref });
|
|
660
|
+
});
|
|
661
|
+
var Markdown$1 = memo(Markdown);
|
|
662
|
+
|
|
663
|
+
export { Markdown$1 as Markdown, MarkdownCMD, Markdown$1 as default };
|
|
2
664
|
//# sourceMappingURL=index.js.map
|