Compare commits

...
Author SHA1 Message Date
l0ng-ai 2532723e32 fix(agents): keep unrelated chat rows stable during text selection 2026-11-23 18:24:44 +08:00
4 changed files with 52 additions and 15 deletions

No files matched your search

+1 -1
View File
@@ -231,7 +231,7 @@
content-visibility: auto;
}
/* WebKit leaves a stale selection highlight on rows that skipped rendering (`useOffscreenRows`). */
.stream[data-selection] > .conversationRow[data-measured] {
.conversationRow[data-measured][data-selection] {
content-visibility: visible;
}
/* Keep turns distinct and the work within a turn close to its author. Not `* + *`: a
+1 -1
View File
@@ -147,7 +147,7 @@ function useOffscreenRows(
}, [ref, visible, enabled, fitWindowWidth, fontZoom]);
// Keep independent of `visible`: when a kept-alive tab hides, `data-selection` must stay until
// the selection leaves and WebKit clears off-screen rows' highlights before skipping again.
useSelectionVisibility(ref, enabled);
useSelectionVisibility(ref, enabled, `.${styles.conversationRow}`);
}
interface StreamProps {
+22 -2
View File
@@ -146,12 +146,14 @@ it("ends Select All in the pane's last selectable text, not on its edge before t
expect(window.getSelection()!.toString()).not.toContain("Reply to the agent");
});
function TrackedVisibilityPane() {
function TrackedVisibilityPane({ rows = false }: { rows?: boolean }) {
const ref = useRef<HTMLDivElement>(null);
useSelectionVisibility(ref);
useSelectionVisibility(ref, true, rows ? "p" : undefined);
return (
<div ref={ref} data-testid="tracked">
<p>Tracked line</p>
<p>Second line</p>
<p>Third line</p>
</div>
);
}
@@ -194,3 +196,21 @@ it("keeps data-selection for one rendered frame after the selection leaves", ()
window.cancelAnimationFrame = prevCancel;
}
});
it("keeps only the selected conversation rows visible and releases rows the selection leaves", async () => {
const view = render(<TrackedVisibilityPane rows />);
const tracked = view.getByTestId("tracked");
const [first, second, third] = [...tracked.children];
const select = (start: Element, end: Element) => {
window.getSelection()!.setBaseAndExtent(start.firstChild!, 0, end.firstChild!, 1);
document.dispatchEvent(new Event("selectionchange"));
};
const marked = () => [...tracked.querySelectorAll("[data-selection]")];
select(first!, second!);
expect(marked()).toEqual([first, second]);
select(third!, third!);
expect(marked()).toEqual([first, second, third]);
await new Promise<void>((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));
expect(marked()).toEqual([third]);
expect(tracked).not.toHaveAttribute("data-selection");
});
+28 -11
View File
@@ -111,32 +111,49 @@ function textEdge(root: HTMLElement, side: "start" | "end"): { node: Node; offse
}
/**
* Marks `ref` with `data-selection` while the document's selection intersects it, and keeps the
* Marks the pane or its intersecting rows with `data-selection` while the document's selection intersects it, and keeps the
* mark until the frame after the selection leaves. WebKit never clears the selection highlight of
* a subtree that skips rendering (`content-visibility: auto` or `hidden`); pairing `[data-selection]`
* with `content-visibility: visible` lets WebKit apply the deselection before skipping again.
*/
export function useSelectionVisibility(ref: RefObject<HTMLElement | null>, enabled = true): void {
export function useSelectionVisibility(ref: RefObject<HTMLElement | null>, enabled = true, rowSelector?: string): void {
useEffect(() => {
const el = ref.current;
if (!el || !enabled) return;
const doc = el.ownerDocument;
const win = doc.defaultView ?? window;
let frame = 0;
const selected = () => {
const marked = new Set<Element>();
const selectionRange = () => {
const sel = doc.getSelection();
return !!sel && !sel.isCollapsed && sel.rangeCount > 0 && sel.getRangeAt(0).intersectsNode(el);
return sel && !sel.isCollapsed && sel.rangeCount ? sel.getRangeAt(0) : null;
};
const update = () => {
win.cancelAnimationFrame(frame);
if (selected()) {
el.setAttribute("data-selection", "");
return;
const range = selectionRange();
if (range?.intersectsNode(el)) {
// Keep unrelated history at its remembered size while selecting visible text.
let targets: Iterable<Element> = [el];
if (rowSelector) {
const node = range.commonAncestorContainer;
const row = (node instanceof Element ? node : node.parentElement)?.closest(rowSelector);
targets = row?.parentElement === el ? [row] : el.querySelectorAll(`:scope > ${rowSelector}`);
}
for (const target of targets) {
if (!range.intersectsNode(target) || marked.has(target)) continue;
marked.add(target);
target.setAttribute("data-selection", "");
}
}
if (!el.hasAttribute("data-selection")) return;
if (![...marked].some((target) => !range?.intersectsNode(target))) return;
frame = win.requestAnimationFrame(() => {
frame = win.requestAnimationFrame(() => {
if (!selected()) el.removeAttribute("data-selection");
const current = selectionRange();
for (const target of marked) {
if (current?.intersectsNode(target)) continue;
target.removeAttribute("data-selection");
marked.delete(target);
}
});
});
};
@@ -145,9 +162,9 @@ export function useSelectionVisibility(ref: RefObject<HTMLElement | null>, enabl
return () => {
win.cancelAnimationFrame(frame);
doc.removeEventListener("selectionchange", update);
el.removeAttribute("data-selection");
for (const target of marked) target.removeAttribute("data-selection");
};
}, [ref, enabled]);
}, [ref, enabled, rowSelector]);
}
/**