Skip to content

Commit af3aadb

Browse files
fix(web): chat text selection stays continuous while scrolling
LegendList recycled the containers of rows a drag selection covered once they left the draw buffer, so the selection's start moved onto other messages and rows in between dropped out. Keep every row between the first and last selected rows mounted through alwaysRender until the selection collapses. Fixes #12943 AI-assisted; verified with the tests and tools listed above.
1 parent b68aedf commit af3aadb

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,11 @@ const ConversationTimeline = memo(function ConversationTimeline({
851851
const onAppFullscreenChange = (rowId: string, fullscreen: boolean) => {
852852
setFullscreenAppRowId((current) => (fullscreen ? rowId : current === rowId ? null : current));
853853
};
854-
const alwaysRenderIndices = restoringAlwaysRender?.indices ?? [];
854+
const selectedRowIds = useSelectedTimelineRowIds(timelineViewportElement);
855+
const alwaysRenderIndices = [
856+
...(restoringAlwaysRender?.indices ?? []),
857+
...timelineRowSpanIndices(rows, selectedRowIds),
858+
];
855859
// Screen readers can only reach headings in the DOM. Keeping the first and
856860
// last messages mounted lets heading navigation wrap to either end.
857861
const firstMessageRow = rows.find(isMessageRow);
@@ -1518,6 +1522,52 @@ function isMessageRow(row: MessagesTimelineRow) {
15181522
return row.kind === "message" && row.message.role !== "system";
15191523
}
15201524

1525+
// LegendList recycles a row's container once it leaves the draw buffer, which
1526+
// moves a native selection's ends onto other rows. Pin the selected rows.
1527+
function useSelectedTimelineRowIds(viewport: HTMLElement | null) {
1528+
const [rowIds, setRowIds] = useState<ReadonlyArray<string>>([]);
1529+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to document selectionchange to track which timeline rows a native selection covers
1530+
useEffect(() => {
1531+
if (!viewport) return;
1532+
const update = () => {
1533+
const selection = document.getSelection();
1534+
const range =
1535+
selection && !selection.isCollapsed && selection.rangeCount > 0
1536+
? selection.getRangeAt(0)
1537+
: null;
1538+
const next: string[] = [];
1539+
if (range?.intersectsNode(viewport)) {
1540+
for (const row of viewport.querySelectorAll<HTMLElement>("[data-timeline-row-id]")) {
1541+
const rowId = row.dataset.timelineRowId;
1542+
if (rowId && range.intersectsNode(row)) next.push(rowId);
1543+
}
1544+
}
1545+
setRowIds((current) =>
1546+
current.length === next.length && current.every((id, i) => id === next[i]) ? current : next,
1547+
);
1548+
};
1549+
document.addEventListener("selectionchange", update);
1550+
return () => {
1551+
document.removeEventListener("selectionchange", update);
1552+
setRowIds([]);
1553+
};
1554+
}, [viewport]);
1555+
return rowIds;
1556+
}
1557+
1558+
// Rows between the selected ones are covered too, even if not mounted right now.
1559+
function timelineRowSpanIndices(
1560+
rows: ReadonlyArray<MessagesTimelineRow>,
1561+
rowIds: ReadonlyArray<string>,
1562+
): number[] {
1563+
if (rowIds.length === 0) return [];
1564+
const selected = new Set(rowIds);
1565+
const first = rows.findIndex((row) => selected.has(row.id));
1566+
const last = rows.findLastIndex((row) => selected.has(row.id));
1567+
if (first < 0) return [];
1568+
return Array.from({ length: last - first + 1 }, (_, i) => first + i);
1569+
}
1570+
15211571
function keyExtractor(item: MessagesTimelineRow) {
15221572
return item.id;
15231573
}

0 commit comments

Comments
 (0)