Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ASSISTANT_CITATION_MAX_COMMENT_LENGTH,
type AssistantCitation,
EnvironmentId,
MessageId,
ThreadId,
Expand Down Expand Up @@ -50,12 +51,16 @@ function setup() {
return { sourceAnchor, [Symbol.dispose]: () => source.remove() };
}

function mount(sourceAnchor: AssistantCitationSourceAnchor, onSave: () => boolean) {
function mount(
sourceAnchor: AssistantCitationSourceAnchor | undefined,
onSave: () => boolean,
chipCitation: AssistantCitation = citation,
) {
function Composer() {
const [open, setOpen] = useState(true);
return (
<AssistantCitationChip
citation={citation}
citation={chipCitation}
composer
commentEditor={{ open, sourceAnchor, onOpenChange: setOpen, onSave }}
/>
Expand Down Expand Up @@ -128,6 +133,25 @@ describe("citation comment source disappearance", () => {
});
});

describe("citation comment caret", () => {
it("opens a saved comment with the caret after its end, scrolled into view", async () => {
using _source = setup();
const comment = `${"line\n".repeat(40)}end`;
await using _screen = await mount(
undefined,
vi.fn(() => true),
{ ...citation, comment },
);
await vi.waitFor(() => expect(document.activeElement).toBe(textarea()));
const input = textarea()!;

expect(input.scrollHeight).toBeGreaterThan(input.clientHeight);
expect(input.scrollTop).toBe(input.scrollHeight - input.clientHeight);
await userEvent.keyboard("!");
expect(input.value).toBe(`${comment}!`);
});
});

describe("citation comment popup position", () => {
it("stays on screen when the cited text ends in a code block's hidden overflow", async () => {
using _source = setup();
Expand Down
8 changes: 7 additions & 1 deletion apps/web/src/components/chat/AssistantCitationChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ export function AssistantCitationChip({
align="end"
anchor={popupAnchor}
initialFocus={() => {
commentInputRef.current?.focus({ preventScroll: true });
const input = commentInputRef.current;
if (input) {
input.focus({ preventScroll: true });
input.setSelectionRange(input.value.length, input.value.length);
// Moving the caret doesn't scroll a long comment to it.
input.scrollTop = input.scrollHeight;
}
return false;
}}
finalFocus={
Expand Down
Loading