| 119 | } |
| 120 | |
| 121 | function createMultiGridInstance( |
| 122 | initialLatest: LatestValues, |
| 123 | setScrollbarState: Dispatch<SetStateAction<ScrollbarState>>, |
| 124 | forceUpdate: () => void |
| 125 | ): MultiGridInstance { |
| 126 | const internals: MultiGridInternals = { |
| 127 | bottomLeftGrid: null, |
| 128 | bottomRightGrid: null, |
| 129 | topLeftGrid: null, |
| 130 | topRightGrid: null, |
| 131 | committedSizes: null, |
| 132 | deferredInvalidateColumnIndex: null, |
| 133 | deferredInvalidateRowIndex: null, |
| 134 | latest: initialLatest, |
| 135 | scrollbarState: INITIAL_SCROLLBAR_STATE, |
| 136 | scrollLeft: 0, |
| 137 | scrollTop: 0 |
| 138 | }; |
| 139 | |
| 140 | // Calls recomputeGridSize() on all child Grids (adjusting the indices for the fixed rows and columns) |
| 141 | const recomputeGrids = (columnIndex: number, rowIndex: number) => { |
| 142 | const { fixedColumnCount, fixedRowCount } = internals.latest; |
| 143 | |
| 144 | const adjustedColumnIndex = Math.max(0, columnIndex - fixedColumnCount); |
| 145 | const adjustedRowIndex = Math.max(0, rowIndex - fixedRowCount); |
| 146 | |
| 147 | internals.bottomLeftGrid?.recomputeGridSize({ |
| 148 | columnIndex, |
| 149 | rowIndex: adjustedRowIndex |
| 150 | }); |
| 151 | internals.bottomRightGrid?.recomputeGridSize({ |
| 152 | columnIndex: adjustedColumnIndex, |
| 153 | rowIndex: adjustedRowIndex |
| 154 | }); |
| 155 | internals.topLeftGrid?.recomputeGridSize({ |
| 156 | columnIndex, |
| 157 | rowIndex |
| 158 | }); |
| 159 | internals.topRightGrid?.recomputeGridSize({ |
| 160 | columnIndex: adjustedColumnIndex, |
| 161 | rowIndex |
| 162 | }); |
| 163 | }; |
| 164 | |
| 165 | // Scrolls the fixed Grids to the current scroll offsets (horizontally for the top-right Grid, vertically for the bottom-left Grid). |
| 166 | // Grid's scrollToPosition() is a no-op for a Grid that is already there. |
| 167 | // (The bottom-right Grid is not scrolled here: it reports its own offsets, and may have a pending scroll, e.g. to keep a cell in view.) |
| 168 | const syncFixedGridScrollPositions = () => { |
| 169 | const { scrollLeft, scrollTop } = internals; |
| 170 | |
| 171 | internals.topRightGrid?.scrollToPosition({ scrollLeft }); |
| 172 | internals.bottomLeftGrid?.scrollToPosition({ scrollTop }); |
| 173 | }; |
| 174 | |
| 175 | const handle: MultiGridImperativeAPI = { |
| 176 | forceUpdateGrids() { |
| 177 | internals.bottomLeftGrid?.forceUpdate(); |
| 178 | internals.bottomRightGrid?.forceUpdate(); |