From 220c622f8f472311f4d3787e1293998c475ad0e3 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Tue, 2 May 2023 02:37:15 +0800 Subject: [PATCH] fixup --- app/components/markdown.tsx | 43 ++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index 2909293c7..11fc249df 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -71,22 +71,35 @@ export function Markdown( const parent = props.parentRef.current; const md = mdRef.current; - if (parent && md) { - const parentBounds = parent.getBoundingClientRect(); - const twoScreenHeight = Math.max(500, parentBounds.height * 2); - const mdBounds = md.getBoundingClientRect(); - const isInRange = (x: number) => - x <= parentBounds.bottom + twoScreenHeight && - x >= parentBounds.top - twoScreenHeight; - inView.current = isInRange(mdBounds.top) || isInRange(mdBounds.bottom); - } + const checkInView = () => { + if (parent && md) { + const parentBounds = parent.getBoundingClientRect(); + const twoScreenHeight = Math.max(500, parentBounds.height * 2); + const mdBounds = md.getBoundingClientRect(); + const isInRange = (x: number) => + x <= parentBounds.bottom + twoScreenHeight && + x >= parentBounds.top - twoScreenHeight; + inView.current = isInRange(mdBounds.top) || isInRange(mdBounds.bottom); + } - if (inView.current && md) { - renderedHeight.current = Math.max( - renderedHeight.current, - md.getBoundingClientRect().height, - ); - } + if (inView.current && md) { + renderedHeight.current = Math.max( + renderedHeight.current, + md.getBoundingClientRect().height, + ); + } + }; + + useEffect(() => { + setTimeout(() => { + if (!inView.current) { + checkInView(); + } + }, 10); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + checkInView(); return (