From 93f8a9c0c31e3bcb3f1d659b62b1877518fd2c5b Mon Sep 17 00:00:00 2001 From: codegenius2 Date: Sun, 4 Feb 2024 03:40:14 +0800 Subject: [PATCH] fix: copy button doesn't appear on code --- src/home/chat-window/compnent/my-text.tsx | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/home/chat-window/compnent/my-text.tsx b/src/home/chat-window/compnent/my-text.tsx index 664581c..7800d96 100644 --- a/src/home/chat-window/compnent/my-text.tsx +++ b/src/home/chat-window/compnent/my-text.tsx @@ -26,7 +26,7 @@ import mifoot from "markdown-it-footnote" import './widget/highlightjs-plugins/copy-button-plugin.css' import {LanguageLabelPlugin} from "./widget/highlightjs-plugins/language-label-plugin.tsx"; import {CopyButtonPlugin} from "./widget/highlightjs-plugins/copy-button-plugin.tsx"; -import {throttle} from "lodash"; +import {debounce, throttle} from "lodash"; import {useSnapshot} from "valtio/react"; import {appState} from "../../../state/app-state.ts"; import {controlState} from "../../../state/control-state.ts"; @@ -37,10 +37,11 @@ hljs.configure({ hljs.addPlugin(new LanguageLabelPlugin()); hljs.addPlugin(new CopyButtonPlugin()); -// to improve performance -const ha: () => void = throttle(() => { - hljs.highlightAll() -}, 500) +const ha = throttle(debounce(() => hljs.highlightAll(), 1000, { + 'trailing': true +}), 1000); + +const haNow = () => hljs.highlightAll() interface TextProps { messageSnap: Message @@ -73,13 +74,14 @@ export const MyText: React.FC = ({messageSnap, theme}) => { useEffect(() => { if (!controlState.isTextPending) { setText(messageSnap.text) + ha() } }, [messageSnap]); useEffect(() => { - // apply plugins only if message is fully received to improve performance + // apply highlight plugin immediately after message is fully received if (messageSnap.status === 'received') { - ha() + haNow() } }, [messageSnap.status]);