@@ -139,6 +139,52 @@ const AnnotationsHotTable: React.FC<{ annotationId?: string }> = React.memo((pro
139139 }
140140 } ;
141141
142+ const reorderArray = < T , > ( arr : T [ ] , from : number , to : number ) => {
143+ if ( from === to ) return [ ...arr ] ;
144+ const updated = [ ...arr ] ;
145+ const [ removed ] = updated . splice ( from , 1 ) ;
146+ updated . splice ( to , 0 , removed ) ;
147+ return updated ;
148+ } ;
149+
150+ const handleColumnMove = ( movedColumns : number [ ] , finalIndex : number ) => {
151+ if ( ! canEdit ) return ;
152+ if ( ! movedColumns . length ) return ;
153+ const fromVisualIndex = movedColumns [ 0 ] ;
154+ const toVisualIndex = finalIndex ;
155+
156+ if ( fromVisualIndex < 2 || toVisualIndex < 2 ) return ; // lock study/analysis columns
157+
158+ const from = fromVisualIndex - 2 ;
159+ let to = toVisualIndex - 2 ;
160+
161+ setAnnotationsHotState ( ( prev ) => {
162+ if ( from >= prev . noteKeys . length ) return prev ;
163+ if ( to >= prev . noteKeys . length ) to = prev . noteKeys . length - 1 ;
164+
165+ const updatedNoteKeys = reorderArray ( prev . noteKeys , from , to ) . map ( ( noteKey , index ) => ( {
166+ ...noteKey ,
167+ order : index ,
168+ } ) ) ;
169+
170+ const updatedHotData = prev . hotData . map ( ( row ) => {
171+ const metadataCols = row . slice ( 0 , 2 ) ;
172+ const noteCols = reorderArray ( row . slice ( 2 ) , from , to ) ;
173+ return [ ...metadataCols , ...noteCols ] ;
174+ } ) ;
175+
176+ return {
177+ ...prev ,
178+ isEdited : true ,
179+ noteKeys : updatedNoteKeys ,
180+ hotColumns : createColumns ( updatedNoteKeys , ! canEdit ) ,
181+ hotData : updatedHotData ,
182+ } ;
183+ } ) ;
184+
185+ hotTableRef . current ?. hotInstance ?. getPlugin ( 'manualColumnMove' ) . clearMoves ( ) ;
186+ } ;
187+
142188 /**
143189 * NOTE: there is a bug where fixed, mergedCells (such as the cells showing our studies) get messed up when you scroll to the right. I think that this is
144190 * due to virtualization - as we scroll to the right, the original heights of the cells are no longer in the DOM and so the calculated row heights are lost and
@@ -247,12 +293,14 @@ const AnnotationsHotTable: React.FC<{ annotationId?: string }> = React.memo((pro
247293 mergeCells = { mergeCells }
248294 disableVisualSelection = { ! canEdit }
249295 colHeaders = { hotColumnHeaders }
296+ manualColumnMove = { canEdit }
250297 colWidths = { colWidths }
251298 rowHeights = { rowHeights }
252299 columns = { hotColumns }
253300 data = { JSON . parse ( JSON . stringify ( hotData ) ) }
254301 afterOnCellMouseUp = { handleCellMouseUp }
255302 beforeOnCellMouseDown = { handleCellMouseDown }
303+ afterColumnMove = { handleColumnMove }
256304 />
257305 ) : (
258306 < Typography sx = { { color : 'warning.dark' } } >
0 commit comments