@@ -854,7 +854,11 @@ const ConversationTimeline = memo(function ConversationTimeline({
854854 const onAppFullscreenChange = ( rowId : string , fullscreen : boolean ) => {
855855 setFullscreenAppRowId ( ( current ) => ( fullscreen ? rowId : current === rowId ? null : current ) ) ;
856856 } ;
857- const alwaysRenderIndices = restoringAlwaysRender ?. indices ?? [ ] ;
857+ const selectedRowIds = useSelectedTimelineRowIds ( timelineViewportElement ) ;
858+ const alwaysRenderIndices = [
859+ ...( restoringAlwaysRender ?. indices ?? [ ] ) ,
860+ ...timelineRowSpanIndices ( rows , selectedRowIds ) ,
861+ ] ;
858862 // Screen readers can only reach headings in the DOM. Keeping the first and
859863 // last messages mounted lets heading navigation wrap to either end.
860864 const firstMessageRow = rows . find ( isMessageRow ) ;
@@ -1522,6 +1526,52 @@ function isMessageRow(row: MessagesTimelineRow) {
15221526 return row . kind === "message" && row . message . role !== "system" ;
15231527}
15241528
1529+ // LegendList recycles a row's container once it leaves the draw buffer, which
1530+ // moves a native selection's ends onto other rows. Pin the selected rows.
1531+ function useSelectedTimelineRowIds ( viewport : HTMLElement | null ) {
1532+ const [ rowIds , setRowIds ] = useState < ReadonlyArray < string > > ( [ ] ) ;
1533+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to document selectionchange to track which timeline rows a native selection covers
1534+ useEffect ( ( ) => {
1535+ if ( ! viewport ) return ;
1536+ const update = ( ) => {
1537+ const selection = document . getSelection ( ) ;
1538+ const range =
1539+ selection && ! selection . isCollapsed && selection . rangeCount > 0
1540+ ? selection . getRangeAt ( 0 )
1541+ : null ;
1542+ const next : string [ ] = [ ] ;
1543+ if ( range ?. intersectsNode ( viewport ) ) {
1544+ for ( const row of viewport . querySelectorAll < HTMLElement > ( "[data-timeline-row-id]" ) ) {
1545+ const rowId = row . dataset . timelineRowId ;
1546+ if ( rowId && range . intersectsNode ( row ) ) next . push ( rowId ) ;
1547+ }
1548+ }
1549+ setRowIds ( ( current ) =>
1550+ current . length === next . length && current . every ( ( id , i ) => id === next [ i ] ) ? current : next ,
1551+ ) ;
1552+ } ;
1553+ document . addEventListener ( "selectionchange" , update ) ;
1554+ return ( ) => {
1555+ document . removeEventListener ( "selectionchange" , update ) ;
1556+ setRowIds ( [ ] ) ;
1557+ } ;
1558+ } , [ viewport ] ) ;
1559+ return rowIds ;
1560+ }
1561+
1562+ // Rows between the selected ones are covered too, even if not mounted right now.
1563+ function timelineRowSpanIndices (
1564+ rows : ReadonlyArray < MessagesTimelineRow > ,
1565+ rowIds : ReadonlyArray < string > ,
1566+ ) : number [ ] {
1567+ if ( rowIds . length === 0 ) return [ ] ;
1568+ const selected = new Set ( rowIds ) ;
1569+ const first = rows . findIndex ( ( row ) => selected . has ( row . id ) ) ;
1570+ const last = rows . findLastIndex ( ( row ) => selected . has ( row . id ) ) ;
1571+ if ( first < 0 ) return [ ] ;
1572+ return Array . from ( { length : last - first + 1 } , ( _ , i ) => first + i ) ;
1573+ }
1574+
15251575function keyExtractor ( item : MessagesTimelineRow ) {
15261576 return item . id ;
15271577}
0 commit comments