@@ -16,6 +16,7 @@ const middleRowFocusOption = 'middleRowFocus' as const;
1616export class MiddleRowFocus extends Module {
1717 static moduleName = 'middleRowFocus' ;
1818
19+ tableHolder : HTMLElement | null = null ;
1920 middleRow : RowComponent | null = null ;
2021 constructor ( table : Tabulator ) {
2122 super ( table ) ;
@@ -25,6 +26,8 @@ export class MiddleRowFocus extends Module {
2526 initialize ( ) {
2627 // @ts -expect-error not in types
2728 if ( this . options ( middleRowFocusOption ) ) {
29+ this . tableHolder = this . table . element . querySelector ( '.tabulator-tableholder' ) ;
30+
2831 this . table . on ( 'dataTreeRowExpanded' , ( ) => {
2932 this . _clearFocusRow ( ) ;
3033 } ) ;
@@ -34,8 +37,8 @@ export class MiddleRowFocus extends Module {
3437 } ) ;
3538
3639 this . table . on ( 'renderStarted' , ( ) => {
37- if ( this . table && ! this . middleRow ) {
38- this . middleRow = this . _findMiddleVisibleRow ( this . table ) ;
40+ if ( this . table && this . tableHolder && ! this . middleRow ) {
41+ this . middleRow = this . _findMiddleVisibleRow ( this . tableHolder ) ;
3942 }
4043 } ) ;
4144
@@ -55,33 +58,32 @@ export class MiddleRowFocus extends Module {
5558 if ( ! row ) {
5659 return ;
5760 }
58- const timeoutId = window . setTimeout ( ( ) => {
59- let rowToScrollTo : RowComponent | null = row ;
60- if ( rowToScrollTo ) {
61- //@ts -expect-error This is private to tabulator, but we have no other choice atm.
62- const internalRow = rowToScrollTo . _getSelf ( ) ;
63- const displayRows = internalRow . table . rowManager . getDisplayRows ( ) ;
64- const canScroll = displayRows . indexOf ( internalRow ) !== - 1 ;
65- if ( ! canScroll ) {
66- const rowData = rowToScrollTo . getData ( ) as TimedNodeProp ;
67- const node = rowData . originalData ;
68-
69- rowToScrollTo = this . _findClosestActive ( this . table . getRows ( 'active' ) , node . timestamp ) ;
70- }
7161
72- if ( rowToScrollTo ) {
73- this . table . scrollToRow ( rowToScrollTo , 'center' , true ) . then ( ( ) => {
62+ let rowToScrollTo : RowComponent | null = row ;
63+ if ( rowToScrollTo ) {
64+ const displayRows = this . table . rowManager . getDisplayRows ( ) ;
65+ //@ts -expect-error This is private to tabulator, but we have no other choice atm.
66+ const internalRow = rowToScrollTo . _getSelf ( ) ;
67+ const canScroll = displayRows . indexOf ( internalRow ) !== - 1 ;
68+ if ( ! canScroll ) {
69+ const rowData = rowToScrollTo . getData ( ) as TimedNodeProp ;
70+ const node = rowData . originalData ;
71+
72+ rowToScrollTo = this . _findClosestActive ( this . table . getRows ( 'active' ) , node . timestamp ) ;
73+ }
74+
75+ if ( rowToScrollTo ) {
76+ this . table . scrollToRow ( rowToScrollTo , 'center' , true ) . then ( ( ) => {
77+ setTimeout ( ( ) => {
7478 if ( rowToScrollTo ) {
7579 rowToScrollTo
7680 ?. getElement ( )
7781 . scrollIntoView ( { behavior : 'auto' , block : 'center' , inline : 'start' } ) ;
7882 }
7983 } ) ;
80- }
84+ } ) ;
8185 }
82-
83- window . clearTimeout ( timeoutId ) ;
84- } ) ;
86+ }
8587 }
8688
8789 private _findClosestActive ( rows : RowComponent [ ] , timeStamp : number ) : RowComponent | null {
@@ -93,6 +95,7 @@ export class MiddleRowFocus extends Module {
9395 end = rows . length - 1 ;
9496
9597 // Iterate as long as the beginning does not encounter the end.
98+ const displayRows = this . table . rowManager . getDisplayRows ( ) ;
9699 while ( start <= end ) {
97100 // find out the middle index
98101 const mid = Math . floor ( ( start + end ) / 2 ) ;
@@ -102,13 +105,11 @@ export class MiddleRowFocus extends Module {
102105 break ;
103106 }
104107 const node = ( row . getData ( ) as TimedNodeProp ) . originalData ;
105-
106- //@ts -expect-error This is private to tabulator, but we have no other choice atm.
107- const internalRow = row . _getSelf ( ) ;
108- const displayRows = internalRow . table . rowManager . getDisplayRows ( ) ;
109108 const endTime = node . exitStamp ?? node . timestamp ;
110109
111110 if ( timeStamp === node . timestamp ) {
111+ //@ts -expect-error This is private to tabulator, but we have no other choice atm.
112+ const internalRow = row . _getSelf ( ) ;
112113 const isActive = displayRows . indexOf ( internalRow ) !== - 1 ;
113114 if ( isActive ) {
114115 return row ;
@@ -190,13 +191,16 @@ export class MiddleRowFocus extends Module {
190191 return closestIndex ? rows [ closestIndex ] || null : null ;
191192 }
192193
193- private _findMiddleVisibleRow ( table : Tabulator ) {
194- const visibleRows = table . getRows ( 'visible' ) ;
195- if ( visibleRows . length === 1 ) {
196- return visibleRows [ 0 ] || null ;
194+ private _findMiddleVisibleRow ( tableHolder : HTMLElement ) {
195+ const visibleRows = this . table . getRows ( 'visible' ) ;
196+ const len = visibleRows . length ;
197+ if ( len === 0 ) {
198+ return null ;
199+ } else if ( len === 1 ) {
200+ return visibleRows [ 0 ] ?? null ;
197201 }
198202
199- const tableRect = table . element . getBoundingClientRect ( ) ;
203+ const tableRect = tableHolder . getBoundingClientRect ( ) ;
200204 const totalHeight = Math . round ( tableRect . height / 2 ) ;
201205
202206 let currentHeight = 0 ;
0 commit comments