Skip to content

Commit

Permalink
fix: copy button doesn't appear on code
Browse files Browse the repository at this point in the history
  • Loading branch information
codegenius2 committed Feb 3, 2024
1 parent 2fa6372 commit 93f8a9c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/home/chat-window/compnent/my-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -73,13 +74,14 @@ export const MyText: React.FC<TextProps> = ({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]);

Expand Down

0 comments on commit 93f8a9c

Please sign in to comment.