@alifd/chat 0.3.28-beta.6 → 0.3.28

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/es/index.js CHANGED
@@ -31,4 +31,4 @@ export { default as RadioGroup } from './radio-group';
31
31
  export { default as CheckboxGroup } from './checkbox-group';
32
32
  export { default as Select } from './select';
33
33
  export { default as Flip } from './flip';
34
- export const version = '0.3.28-beta.6';
34
+ export const version = '0.3.28';
@@ -44,16 +44,20 @@ const Markdown = forwardRef((_a, ref) => {
44
44
  setContentQueue([]);
45
45
  return;
46
46
  }
47
- // 将内容分成两部分:直接显示部分和打字效果部分
48
- if (content.length <= 20) {
49
- // 内容不超过20个字符,全部使用打字效果
50
- setDisplayedContent('');
51
- setContentQueue(content.split(''));
47
+ // 比较传入的content和当前的displayedContent的长度
48
+ const contentLength = content.length;
49
+ const displayedContentLength = displayedContent.length;
50
+ const diffLength = contentLength - displayedContentLength;
51
+ if (diffLength <= 0) {
52
+ // 如果content的长度小于等于displayedContent的长度,则清空contentQueue,直接展示content
53
+ setDisplayedContent(content);
54
+ setContentQueue([]);
52
55
  }
53
56
  else {
54
- // 内容超过20个字符,前面直接显示,后20字符用打字效果
55
- const displayPart = content.slice(0, content.length - 20);
56
- const typingPart = content.slice(content.length - 20);
57
+ // 如果content的长度大于displayedContent的长度,将 content 分为两部分,
58
+ // contentLength - diffLength 个字符直接显示,后 diffLength 个字符使用打字效果
59
+ const displayPart = content.slice(0, contentLength - diffLength);
60
+ const typingPart = content.slice(contentLength - diffLength);
57
61
  setDisplayedContent(displayPart);
58
62
  setContentQueue(typingPart.split(''));
59
63
  }
@@ -65,31 +69,28 @@ const Markdown = forwardRef((_a, ref) => {
65
69
  clearTypeTimer();
66
70
  return;
67
71
  }
68
- // 固定打字速度为50ms一个字
69
- const timePerChar = 50;
72
+ // 固定吐字速度为50ms一次
73
+ const timePerChar = 100;
74
+ // 动态调整一次吐字的字数,确保1s内吐完,最少吐2个字符
75
+ const charsPerTime = Math.max(Math.floor(contentQueue.length / 10), 2);
70
76
  const processNextChar = () => {
71
- // 使用函数式更新避免依赖项问题
72
- setContentQueue(prevQueue => {
73
- if (prevQueue.length === 0) {
74
- return prevQueue;
75
- }
76
- // 获取并移除队列中的第一个字符
77
- const nextChar = prevQueue[0];
78
- const newQueue = prevQueue.slice(1);
79
- // 更新显示内容
80
- setDisplayedContent(prev => prev + nextChar);
81
- // 如果队列还有字符,继续处理下一个
82
- if (newQueue.length > 0) {
83
- typeTimeoutRef.current = setTimeout(processNextChar, timePerChar);
84
- }
85
- return newQueue;
86
- });
77
+ const currentQueue = contentQueue;
78
+ if (currentQueue.length === 0) {
79
+ return;
80
+ }
81
+ // 获取并移除队列中的第charsPerTime个字符
82
+ const nextChars = currentQueue.slice(0, charsPerTime);
83
+ const newQueue = currentQueue.slice(charsPerTime);
84
+ // 更新显示内容
85
+ setDisplayedContent(prev => prev + nextChars.join(''));
86
+ // 更新队列
87
+ setContentQueue(newQueue);
87
88
  };
88
89
  // 开始处理第一个字符
89
90
  clearTypeTimer(); // 确保没有并发定时器
90
91
  typeTimeoutRef.current = setTimeout(processNextChar, timePerChar);
91
92
  return clearTypeTimer;
92
- }, [contentQueue.length, others === null || others === void 0 ? void 0 : others.typewriterEffect]); // 仅依赖队列长度和打字机效果开关
93
+ }, [contentQueue.length, others === null || others === void 0 ? void 0 : others.typewriterEffect]);
93
94
  // 处理 HTML 标签
94
95
  const processedContent = useMemo(() => {
95
96
  // 使用 Record 类型来定义映射关系
package/lib/index.js CHANGED
@@ -68,4 +68,4 @@ var select_1 = require("./select");
68
68
  Object.defineProperty(exports, "Select", { enumerable: true, get: function () { return tslib_1.__importDefault(select_1).default; } });
69
69
  var flip_1 = require("./flip");
70
70
  Object.defineProperty(exports, "Flip", { enumerable: true, get: function () { return tslib_1.__importDefault(flip_1).default; } });
71
- exports.version = '0.3.28-beta.6';
71
+ exports.version = '0.3.28';
@@ -46,16 +46,20 @@ const Markdown = (0, react_1.forwardRef)((_a, ref) => {
46
46
  setContentQueue([]);
47
47
  return;
48
48
  }
49
- // 将内容分成两部分:直接显示部分和打字效果部分
50
- if (content.length <= 20) {
51
- // 内容不超过20个字符,全部使用打字效果
52
- setDisplayedContent('');
53
- setContentQueue(content.split(''));
49
+ // 比较传入的content和当前的displayedContent的长度
50
+ const contentLength = content.length;
51
+ const displayedContentLength = displayedContent.length;
52
+ const diffLength = contentLength - displayedContentLength;
53
+ if (diffLength <= 0) {
54
+ // 如果content的长度小于等于displayedContent的长度,则清空contentQueue,直接展示content
55
+ setDisplayedContent(content);
56
+ setContentQueue([]);
54
57
  }
55
58
  else {
56
- // 内容超过20个字符,前面直接显示,后20字符用打字效果
57
- const displayPart = content.slice(0, content.length - 20);
58
- const typingPart = content.slice(content.length - 20);
59
+ // 如果content的长度大于displayedContent的长度,将 content 分为两部分,
60
+ // contentLength - diffLength 个字符直接显示,后 diffLength 个字符使用打字效果
61
+ const displayPart = content.slice(0, contentLength - diffLength);
62
+ const typingPart = content.slice(contentLength - diffLength);
59
63
  setDisplayedContent(displayPart);
60
64
  setContentQueue(typingPart.split(''));
61
65
  }
@@ -67,31 +71,28 @@ const Markdown = (0, react_1.forwardRef)((_a, ref) => {
67
71
  clearTypeTimer();
68
72
  return;
69
73
  }
70
- // 固定打字速度为50ms一个字
71
- const timePerChar = 50;
74
+ // 固定吐字速度为50ms一次
75
+ const timePerChar = 100;
76
+ // 动态调整一次吐字的字数,确保1s内吐完,最少吐2个字符
77
+ const charsPerTime = Math.max(Math.floor(contentQueue.length / 10), 2);
72
78
  const processNextChar = () => {
73
- // 使用函数式更新避免依赖项问题
74
- setContentQueue(prevQueue => {
75
- if (prevQueue.length === 0) {
76
- return prevQueue;
77
- }
78
- // 获取并移除队列中的第一个字符
79
- const nextChar = prevQueue[0];
80
- const newQueue = prevQueue.slice(1);
81
- // 更新显示内容
82
- setDisplayedContent(prev => prev + nextChar);
83
- // 如果队列还有字符,继续处理下一个
84
- if (newQueue.length > 0) {
85
- typeTimeoutRef.current = setTimeout(processNextChar, timePerChar);
86
- }
87
- return newQueue;
88
- });
79
+ const currentQueue = contentQueue;
80
+ if (currentQueue.length === 0) {
81
+ return;
82
+ }
83
+ // 获取并移除队列中的第charsPerTime个字符
84
+ const nextChars = currentQueue.slice(0, charsPerTime);
85
+ const newQueue = currentQueue.slice(charsPerTime);
86
+ // 更新显示内容
87
+ setDisplayedContent(prev => prev + nextChars.join(''));
88
+ // 更新队列
89
+ setContentQueue(newQueue);
89
90
  };
90
91
  // 开始处理第一个字符
91
92
  clearTypeTimer(); // 确保没有并发定时器
92
93
  typeTimeoutRef.current = setTimeout(processNextChar, timePerChar);
93
94
  return clearTypeTimer;
94
- }, [contentQueue.length, others === null || others === void 0 ? void 0 : others.typewriterEffect]); // 仅依赖队列长度和打字机效果开关
95
+ }, [contentQueue.length, others === null || others === void 0 ? void 0 : others.typewriterEffect]);
95
96
  // 处理 HTML 标签
96
97
  const processedContent = (0, react_1.useMemo)(() => {
97
98
  // 使用 Record 类型来定义映射关系
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alifd/chat",
3
- "version": "0.3.28-beta.6",
3
+ "version": "0.3.28",
4
4
  "description": "A configurable component library for chat built on React.",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",