From 82f2055df900e2ce7dfb16a040f21716e7abb55e Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Mon, 25 Jul 2022 10:12:58 +0000 Subject: [PATCH] Set longestLineRef once --- .../mdx/src/smooth-code/use-dimensions.tsx | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/packages/mdx/src/smooth-code/use-dimensions.tsx b/packages/mdx/src/smooth-code/use-dimensions.tsx index da8ea15b..217294d1 100644 --- a/packages/mdx/src/smooth-code/use-dimensions.tsx +++ b/packages/mdx/src/smooth-code/use-dimensions.tsx @@ -56,40 +56,43 @@ function useDimensions( code.next, focus.next ) + const lines = (code.prev || code.next!) .trimEnd() .split(newlineRe) const lineCount = lines.length + // avod setting the ref more than once https://github.com/code-hike/codehike/issues/232 + let prevLineRefSet = false const element = (
- {lines.map((line, i) => ( -
- {lineNumbers ? ( - - _{lineCount} - - ) : undefined} -
- {line} + {lines.map((line, i) => { + const ref = + !prevLineRefSet && line === prevLongestLine + ? prevLineRef + : undefined + prevLineRefSet = prevLineRefSet || ref != null + return ( +
+ {lineNumbers ? ( + + _{lineCount} + + ) : undefined} +
+ {line} +
-
- ))} + ) + })}
)