Skip to content

Commit 64ac21b

Browse files
RtYkkSharzyL
authored andcommitted
fix[frontend]: synchronize line numbers with textarea scroll
1 parent 7166fe1 commit 64ac21b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

frontend/components/CodeEditor.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export function CodeEditor({
7979
}: CodeInputProps) {
8080
const refHighlighting = useRef<HTMLPreElement | null>(null)
8181
const refTextarea = useRef<HTMLTextAreaElement | null>(null)
82+
const refLineNumbers = useRef<HTMLSpanElement | null>(null)
8283

8384
const [heightPx, setHeightPx] = useState<number>(0)
8485
const hljs = useHLJS()
@@ -89,6 +90,9 @@ export function CodeEditor({
8990
function syncScroll() {
9091
refHighlighting.current!.scrollLeft = refTextarea.current!.scrollLeft
9192
refHighlighting.current!.scrollTop = refTextarea.current!.scrollTop
93+
if (refLineNumbers.current) {
94+
refLineNumbers.current.scrollTop = refTextarea.current!.scrollTop
95+
}
9296
}
9397

9498
function handleInput(_: React.FormEvent<HTMLTextAreaElement>) {
@@ -186,10 +190,12 @@ export function CodeEditor({
186190
dangerouslySetInnerHTML={{ __html: highlightHTML(hljs, lang, handleNewLines(content)) }}
187191
></pre>
188192
<span
193+
ref={refLineNumbers}
189194
className={
190195
"line-number-rows font-mono absolute pointer-events-none text-default-500 top-0 left-1 " +
191196
`border-solid border-default-300 border-r-1 ${tst}`
192197
}
198+
style={{ height: `${heightPx}px`, overflow: "hidden" }}
193199
>
194200
{Array.from({ length: lineCount }, (_, idx) => {
195201
return <span key={idx} />

0 commit comments

Comments
 (0)