Skip to content

Commit 6b3b3b7

Browse files
fix(web): chat text can be cited while file search is open (#27131)
Co-authored-by: maria-rcks <[email protected]>
1 parent e4d1eda commit 6b3b3b7

3 files changed

Lines changed: 85 additions & 6 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Editor, type EditorFactory } from "@pierre/diffs/edit";
2+
import { EditProvider, File } from "@pierre/diffs/react";
3+
import { describe, expect, it, vi } from "vite-plus/test";
4+
5+
import { render } from "../../../test/render";
6+
7+
import { isMacPlatform } from "~/lib/utils";
8+
import { installFileEditorDismissal } from "./fileEditorDismissal";
9+
10+
describe("installFileEditorDismissal", () => {
11+
it("keeps a text selection made outside the file while its search is open", async () => {
12+
let editor: Editor<"file" | "file-diff", undefined, undefined> | undefined;
13+
const createEditor: EditorFactory<undefined, undefined> = (...args) =>
14+
(editor = new Editor(...args));
15+
await using screen = await render(
16+
<>
17+
<p>Assistant reply</p>
18+
<div data-testid="file-surface">
19+
<EditProvider createEditor={createEditor}>
20+
<File file={{ name: "server.ts", contents: "const server = 1;\n" }} edit />
21+
</EditProvider>
22+
</div>
23+
</>,
24+
);
25+
const shadowRoot = () => screen.container.querySelector("diffs-container")?.shadowRoot;
26+
const content = await vi.waitFor(() => {
27+
const element = shadowRoot()?.querySelector<HTMLElement>("[data-content]");
28+
expect(element).toBeTruthy();
29+
return element!;
30+
});
31+
32+
editor!.setSelections([
33+
{ start: { line: 0, character: 8 }, end: { line: 0, character: 8 }, direction: "none" },
34+
]);
35+
const isMac = isMacPlatform(navigator.platform);
36+
content.dispatchEvent(
37+
new KeyboardEvent("keydown", {
38+
key: "f",
39+
code: "KeyF",
40+
metaKey: isMac,
41+
ctrlKey: !isMac,
42+
bubbles: true,
43+
composed: true,
44+
cancelable: true,
45+
}),
46+
);
47+
await vi.waitFor(() =>
48+
expect(shadowRoot()?.querySelectorAll("[data-match-range]")).toHaveLength(1),
49+
);
50+
51+
using _dismissal = {
52+
[Symbol.dispose]: installFileEditorDismissal({
53+
root: screen.getByTestId("file-surface").element() as HTMLElement,
54+
editor: editor!,
55+
isBlocked: () => false,
56+
onDismiss: () => {},
57+
}),
58+
};
59+
const reply = screen.getByText("Assistant reply").element();
60+
const range = document.createRange();
61+
range.selectNodeContents(reply);
62+
window.getSelection()!.removeAllRanges();
63+
window.getSelection()!.addRange(range);
64+
reply.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, composed: true }));
65+
66+
expect(window.getSelection()?.toString()).toBe("Assistant reply");
67+
});
68+
});

‎patches/@pierre%[email protected]‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,18 @@ diff --git a/dist/editor/editor.js b/dist/editor/editor.js
882882
index bf17cea32f..671e77b8cd 100644
883883
--- a/dist/editor/editor.js
884884
+++ b/dist/editor/editor.js
885-
@@ -1903,7 +1903,11 @@ var Editor = class {
885+
@@ -436,7 +436,9 @@ var Editor = class {
886+
this.#canMountSelectionAction = false;
887+
this.#updateSelections(resolvedSelections);
888+
const primarySelection = resolvedSelections.at(-1);
889+
- if (primarySelection !== void 0) this.#revealLineIfCollapsed(getCaretPosition(primarySelection).line);
890+
+ // Clearing keeps the selection while search matches or markers exist; scrolling to it steals focus.
891+
+ if (primarySelection === void 0) return;
892+
+ this.#revealLineIfCollapsed(getCaretPosition(primarySelection).line);
893+
this.#scrollToPrimaryCaret();
894+
}
895+
setMarkers(markers) {
896+
@@ -1903,7 +1905,11 @@ var Editor = class {
886897
changedDocumentLines: this.#isDiff && !didLineCountChange ? getChangedDocumentLines(textDocument, change) : void 0,
887898
documentLineCount: textDocument.lineCount
888899
});

‎pnpm-lock.yaml‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)