Skip to content

Commit 768cf53

Browse files
committed
feat(chat): support render empty prompt
1 parent 07e27b9 commit 768cf53

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/chat/markdown/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,17 @@ export default memo(
4848
hr() {
4949
return <hr color="#ebecf0" className="dtc__aigc__markdown__hr" />;
5050
},
51+
p: (data) => {
52+
// avoid validateDOMNesting error for div as a descendant of p
53+
if (data.node.children.every((child) => child.type === 'text')) {
54+
return <p>{data.children}</p>;
55+
} else {
56+
return <div>{data.children}</div>;
57+
}
58+
},
5159
...components,
5260
}}
61+
includeElementIndex
5362
{...rest}
5463
>
5564
{children}

src/chat/prompt/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export default function Prompt({ data, className }: IPromptProps) {
2828
}, {});
2929
}, [components, data?.id]);
3030

31+
if (!data?.title) return null;
32+
3133
return (
3234
<section className={classNames('dtc__prompt__container', className)}>
3335
<div className="dtc__prompt__wrapper">

src/useTyping/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,29 @@ export default function useTyping({ onTyping }: { onTyping: (post: string) => vo
3535
typingCountOnTime.current = Math.ceil(remainWordsLength / typingTimes);
3636
}
3737

38+
function getNextChunkPosition() {
39+
const rest = queue.current.slice(beginIndex.current);
40+
const chunk = rest.slice(0, typingCountOnTime.current);
41+
const validHTMLTagRegex = /<[a-zA-Z]{0,4}\s[^<]*>/;
42+
// 确保在 typing 的过程中,HTML 标签不被分割
43+
if (validHTMLTagRegex.test(rest) && !validHTMLTagRegex.test(chunk)) {
44+
const match = rest.match(validHTMLTagRegex)!;
45+
const tag = match[0];
46+
const index = rest.indexOf(tag);
47+
return beginIndex.current + index + tag.length;
48+
}
49+
return beginIndex.current + typingCountOnTime.current;
50+
}
51+
3852
function startTyping() {
3953
if (interval.current) return;
4054
interval.current = window.setInterval(() => {
4155
if (beginIndex.current < queue.current.length) {
4256
const str = queue.current;
43-
onTyping(str.slice(0, beginIndex.current + typingCountOnTime.current));
44-
beginIndex.current += typingCountOnTime.current;
57+
const idx = getNextChunkPosition();
58+
const next = str.slice(0, idx);
59+
onTyping(next);
60+
beginIndex.current = next.length;
4561
} else if (!isStart.current) {
4662
// 如果发送了全部的消息且信号关闭,则清空队列
4763
window.clearInterval(interval.current);

0 commit comments

Comments
 (0)