@@ -33,25 +33,39 @@ function toggleCodePreview() {
3333 * @param {number } line - The line number to scroll to and highlight
3434 */
3535function scrollToLine ( line ) {
36- if ( ! codeContainer || ! line ) return ;
37-
38- // Wait for SyntaxHighlighter to complete rendering
39- setTimeout ( function ( ) {
40- var selectedLine = codeContainer . querySelector ( ".line.number" + line ) ;
41- if ( selectedLine ) {
42- var codeWrapper = document . querySelector ( '.code-preview' ) ;
43- if ( codeWrapper ) {
44- var top = selectedLine . offsetTop - ( codeWrapper . offsetHeight / 2 ) ;
45- codeWrapper . scrollTop = Math . max ( 0 , top ) ;
46-
47- // Highlight the line temporarily
48- selectedLine . style . backgroundColor = 'rgba(235, 76, 76, 0.3)' ;
49- setTimeout ( function ( ) {
50- selectedLine . style . backgroundColor = '' ;
51- } , 2000 ) ;
52- }
53- }
54- } , 100 ) ;
36+ if ( ! codeContainer || ! line ) return ;
37+
38+ setTimeout ( ( ) => {
39+ // Matches class like "line number24"
40+ const selector = ".line.number" + line ;
41+ const selectedLine = codeContainer . querySelector ( selector ) ;
42+ const scrollContainer = document . getElementById ( "code-container" ) ;
43+
44+ // Guard against missing elements
45+ if ( ! selectedLine || ! scrollContainer ) {
46+ return ;
47+ }
48+
49+ // Approach 1: Using scrollIntoView
50+ selectedLine . scrollIntoView ( {
51+ behavior : "smooth" ,
52+ block : "center" ,
53+ inline : "nearest"
54+ } ) ;
55+
56+ // Approach 2: Direct scroll calculation as fallback
57+ setTimeout ( ( ) => {
58+ const lineRect = selectedLine . getBoundingClientRect ( ) ;
59+ const containerRect = scrollContainer . getBoundingClientRect ( ) ;
60+ const offset = lineRect . top - containerRect . top + scrollContainer . scrollTop ;
61+ const centerOffset = offset - ( scrollContainer . clientHeight / 2 ) ;
62+
63+ scrollContainer . scrollTo ( {
64+ top : Math . max ( 0 , centerOffset ) ,
65+ behavior : "smooth"
66+ } ) ;
67+ } , 100 ) ;
68+ } , 200 ) ;
5569}
5670
5771/**
@@ -98,6 +112,7 @@ function changeCodePanel( id ) {
98112
99113 // Inject the highlighted source to the code container for visualization
100114 codeContainer . innerHTML = code . innerHTML ;
115+ //console.log( "Scroll to line: " + code.getAttribute( "data-highlight-line" ) );
101116 // Scroll to the highlighted error line
102117 scrollToLine ( code . getAttribute ( "data-highlight-line" ) ) ;
103118}
0 commit comments