@@ -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 - z A - 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