@@ -17,32 +17,62 @@ export function normalIdx(idx: number, linesLen: number): number {
1717 return Math . min ( Math . max ( 0 , idx ) , linesLen - 1 ) ;
1818}
1919
20- export function getVisibleIdxRange ( linesLen : number ) : {
20+ function getVisibleRange (
21+ containerEl : HTMLElement ,
22+ contentEl : HTMLElement ,
23+ linesLen : number ,
24+ ) : {
2125 min : number ;
2226 max : number ;
2327} {
24- const formattedLogLines = document . getElementById ( "formatted-log-lines" ) ;
25- const logContainer = document . getElementById ( "log-container" ) ;
26- if ( ! formattedLogLines || ! logContainer ) {
27- return { min : 0 , max : 0 } ; // Throw here
28- }
29- const linesRect = formattedLogLines . getBoundingClientRect ( ) ;
30- const containerRect = logContainer . getBoundingClientRect ( ) ;
28+ const contentRect = contentEl . getBoundingClientRect ( ) ;
29+ const containerRect = containerEl . getBoundingClientRect ( ) ;
3130
32- if ( containerRect . height > linesRect . height ) {
31+ if ( containerRect . height > contentRect . height ) {
3332 return { min : 0 , max : linesLen - 1 } ;
3433 }
3534
36- const relativeStart = ( containerRect . top - linesRect . top ) / linesRect . height ;
37- const relativeEnd = relativeStart + containerRect . height / linesRect . height ;
35+ const relativeStart =
36+ ( containerRect . top - contentRect . top ) / contentRect . height ;
37+ const relativeEnd = relativeStart + containerRect . height / contentRect . height ;
3838 const min = Math . floor ( relativeStart * linesLen ) ;
3939 const max = Math . ceil ( relativeEnd * linesLen ) ;
4040
4141 return { min, max } ;
4242}
4343
44- function scrollToLine ( el : HTMLElement , idx : number , linesLen : number ) {
45- const { min, max } = getVisibleIdxRange ( linesLen ) ;
44+ export function getVisibleLogLineRange ( linesLen : number ) : {
45+ min : number ;
46+ max : number ;
47+ } {
48+ const formattedLogLines = document . getElementById ( "formatted-log-lines" ) ;
49+ const logContainer = document . getElementById ( "log-container" ) ;
50+ if ( ! formattedLogLines || ! logContainer ) {
51+ throw new Error ( "Missing formattedLogLines or logContainer" ) ;
52+ }
53+ return getVisibleRange ( logContainer , formattedLogLines , linesLen ) ;
54+ }
55+
56+ export function getVisibleCSourceRange ( linesLen : number ) : {
57+ min : number ;
58+ max : number ;
59+ } {
60+ const csourceContent = document . getElementById ( "c-source-content" ) ;
61+ const csourceContainer = document . getElementById ( "c-source-container" ) ;
62+ if ( ! csourceContent || ! csourceContainer ) {
63+ // Don't throw the container might be collapsed
64+ return { min : 0 , max : 0 } ;
65+ }
66+ return getVisibleRange ( csourceContainer , csourceContent , linesLen ) ;
67+ }
68+
69+ function scrollToLine (
70+ el : HTMLElement ,
71+ idx : number ,
72+ max : number ,
73+ min : number ,
74+ linesLen : number ,
75+ ) {
4676 const page = max - min + 1 ;
4777 const relativePosition = normalIdx ( idx - page * 0.618 , linesLen ) / linesLen ;
4878 el . scrollTop = relativePosition * el . scrollHeight ;
@@ -53,12 +83,12 @@ export function scrollToLogLine(idx: number, linesLen: number) {
5383 if ( ! logContainer ) {
5484 throw new Error ( "Log line container is not in the DOM" ) ;
5585 }
56- const logRange = getVisibleIdxRange ( linesLen ) ;
86+ const logRange = getVisibleLogLineRange ( linesLen ) ;
5787 if (
5888 ( idx < logRange . min + 8 || idx > logRange . max - 8 ) &&
5989 ! ( idx < 0 || idx >= linesLen )
6090 ) {
61- scrollToLine ( logContainer , idx , linesLen ) ;
91+ scrollToLine ( logContainer , idx , logRange . max , logRange . min , linesLen ) ;
6292 }
6393}
6494
@@ -68,7 +98,19 @@ export function scrollToCLine(idx: number, linesLen: number) {
6898 // This won't exist if the container is collapsed
6999 return ;
70100 }
71- scrollToLine ( cSourceContainer , idx , linesLen ) ;
101+ const cLinesRange = getVisibleCSourceRange ( linesLen ) ;
102+ if (
103+ ( idx < cLinesRange . min + 8 || idx > cLinesRange . max - 8 ) &&
104+ ! ( idx < 0 || idx >= linesLen )
105+ ) {
106+ scrollToLine (
107+ cSourceContainer ,
108+ idx ,
109+ cLinesRange . max ,
110+ cLinesRange . min ,
111+ linesLen ,
112+ ) ;
113+ }
72114}
73115
74116export function siblingInsLine (
0 commit comments