From d790b0b372c0ff2236b24a57f83f9e59a8b76914 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Wed, 12 Apr 2023 23:27:28 +0800 Subject: [PATCH] feat: close #680 lazy load markdown dom --- app/components/chat.tsx | 28 ++++++++--------- app/components/markdown.tsx | 61 +++++++++++++++++++++++++------------ app/global.d.ts | 13 +++----- 3 files changed, 59 insertions(+), 43 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 33ac3ac5..71c8de43 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -672,22 +672,18 @@ export function Chat(props: { )} - {(message.preview || message.content.length === 0) && - !isUser ? ( - - ) : ( -
onRightClick(e, message)} - onDoubleClickCapture={() => { - if (!isMobileScreen()) return; - setUserInput(message.content); - }} - > - -
- )} + onRightClick(e, message)} + onDoubleClickCapture={() => { + if (!isMobileScreen()) return; + setUserInput(message.content); + }} + /> {!isUser && !message.preview && (
diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index c8076640..886956f0 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -8,6 +8,8 @@ import RehypeHighlight from "rehype-highlight"; import { useRef, useState, RefObject, useEffect } from "react"; import { copyToClipboard } from "../utils"; +import LoadingIcon from "../icons/three-dots.svg"; + export function PreCode(props: { children: any }) { const ref = useRef(null); @@ -50,26 +52,47 @@ const useLazyLoad = (ref: RefObject): boolean => { return isIntersecting; }; -export function Markdown(props: { content: string }) { +export function Markdown( + props: { + content: string; + loading?: boolean; + fontSize?: number; + } & React.DOMAttributes, +) { + const mdRef = useRef(null); + const shouldRender = useLazyLoad(mdRef); + const shouldLoading = props.loading || !shouldRender; + return ( - - {props.content} - + {shouldLoading ? ( + + ) : ( + + {props.content} + + )} +
); } diff --git a/app/global.d.ts b/app/global.d.ts index bbe834b3..bd1c062d 100644 --- a/app/global.d.ts +++ b/app/global.d.ts @@ -3,12 +3,9 @@ declare module "*.png"; declare module "*.woff2"; declare module "*.woff"; declare module "*.ttf"; -declare module "*.scss"; - -declare module "*.svg" { - import React = require("react"); - - export const ReactComponent: React.SFC>; - const src: string; - export default src; +declare module "*.scss" { + const content: Record; + export default content; } + +declare module "*.svg";