Skip to content
TypeScript

fix(web): chat text can be cited while file search is open (#27131)

Abhijeet Singh6b3b3b75bdcb
diff --git a/apps/web/src/components/files/fileEditorDismissal.browser.test.tsx b/apps/web/src/components/files/fileEditorDismissal.browser.test.tsx
new file mode 100644
index 00000000000..7f7d6601c68
--- /dev/null
+++ b/apps/web/src/components/files/fileEditorDismissal.browser.test.tsx
@@ -0,0 +1,68 @@
+import { Editor, type EditorFactory } from "@pierre/diffs/edit";
+import { EditProvider, File } from "@pierre/diffs/react";
+import { describe, expect, it, vi } from "vite-plus/test";
+
+import { render } from "../../../test/render";
+
+import { isMacPlatform } from "~/lib/utils";
+import { installFileEditorDismissal } from "./fileEditorDismissal";
+
+describe("installFileEditorDismissal", () => {
+ it("keeps a text selection made outside the file while its search is open", async () => {
+ let editor: Editor<"file" | "file-diff", undefined, undefined> | undefined;
+ const createEditor: EditorFactory<undefined, undefined> = (...args) =>
+ (editor = new Editor(...args));
+ await using screen = await render(
+ <>
+ <p>Assistant reply</p>
+ <div data-testid="file-surface">
+ <EditProvider createEditor={createEditor}>
+ <File file={{ name: "server.ts", contents: "const server = 1;\n" }} edit />
+ </EditProvider>
+ </div>
+ </>,
+ );
+ const shadowRoot = () => screen.container.querySelector("diffs-container")?.shadowRoot;
+ const content = await vi.waitFor(() => {
+ const element = shadowRoot()?.querySelector<HTMLElement>("[data-content]");
+ expect(element).toBeTruthy();
+ return element!;
+ });
+
+ editor!.setSelections([
+ { start: { line: 0, character: 8 }, end: { line: 0, character: 8 }, direction: "none" },
+ ]);
+ const isMac = isMacPlatform(navigator.platform);
+ content.dispatchEvent(
+ new KeyboardEvent("keydown", {
+ key: "f",
+ code: "KeyF",
+ metaKey: isMac,
+ ctrlKey: !isMac,
+ bubbles: true,
+ composed: true,
+ cancelable: true,
+ }),
+ );
+ await vi.waitFor(() =>
+ expect(shadowRoot()?.querySelectorAll("[data-match-range]")).toHaveLength(1),
+ );
+
+ using _dismissal = {
+ [Symbol.dispose]: installFileEditorDismissal({
+ root: screen.getByTestId("file-surface").element() as HTMLElement,
+ editor: editor!,
+ isBlocked: () => false,
+ onDismiss: () => {},
+ }),
+ };
+ const reply = screen.getByText("Assistant reply").element();
+ const range = document.createRange();
+ range.selectNodeContents(reply);
+ window.getSelection()!.removeAllRanges();
+ window.getSelection()!.addRange(range);
+ reply.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, composed: true }));
+
+ expect(window.getSelection()?.toString()).toBe("Assistant reply");
+ });
+});
diff --git a/patches/@pierre%[email protected] b/patches/@pierre%[email protected]
index dba0acc7eb6..c1944083990 100644
--- a/patches/@pierre%[email protected]
+++ b/patches/@pierre%[email protected]
@@ -882,7 +882,18 @@ diff --git a/dist/editor/editor.js b/dist/editor/editor.js
index bf17cea32f..671e77b8cd 100644
--- a/dist/editor/editor.js
+++ b/dist/editor/editor.js
-@@ -1903,7 +1903,11 @@ var Editor = class {
+@@ -436,7 +436,9 @@ var Editor = class {
+ this.#canMountSelectionAction = false;
+ this.#updateSelections(resolvedSelections);
+ const primarySelection = resolvedSelections.at(-1);
+- if (primarySelection !== void 0) this.#revealLineIfCollapsed(getCaretPosition(primarySelection).line);
++ // Clearing keeps the selection while search matches or markers exist; scrolling to it steals focus.
++ if (primarySelection === void 0) return;
++ this.#revealLineIfCollapsed(getCaretPosition(primarySelection).line);
+ this.#scrollToPrimaryCaret();
+ }
+ setMarkers(markers) {
+@@ -1903,7 +1905,11 @@ var Editor = class {
changedDocumentLines: this.#isDiff && !didLineCountChange ? getChangedDocumentLines(textDocument, change) : void 0,
documentLineCount: textDocument.lineCount
});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d690b2b4a36..fa3d8233954 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -367,7 +367,7 @@ patchedDependencies:
'@ff-labs/[email protected]': cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c
'@legendapp/[email protected]': 6cbdacd8c380d9a5afb9f73d4198dc2cc44be6ef28a955b1183c781469307896
'@muse-code/[email protected]': 7b495b607fc5d76b444c7c4d58821e839726a30e6f3c53564f0d59f2d5b42101
- '@pierre/[email protected]': 4645364ddf113e48e8827f3b4966b3b1ff745317011e5a3d904e2d8672dee7dc
+ '@pierre/[email protected]': 322ac5f5db8b5715189c010802047f8235c3a45d8ed5ca0ba60fac266e2eba34
'@react-native-ai/[email protected]': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d
'@react-native-menu/[email protected]': 910ec1bf22aa85a96fcf12299cd823bc30f702dd9b2d77b564d10b9ed709f7f6
'@react-navigation/[email protected]': 990d575444c4af2ce8218b3b7a8280fe244c03abbdb32b822a2e862f791dbf42
@@ -557,7 +557,7 @@ importers:
version: 1.9.1
'@pierre/diffs':
specifier: 1.6.1
- version: 1.6.1(patch_hash=4645364ddf113e48e8827f3b4966b3b1ff745317011e5a3d904e2d8672dee7dc)(@shikijs/[email protected])([email protected]([email protected]))([email protected])
+ version: 1.6.1(patch_hash=322ac5f5db8b5715189c010802047f8235c3a45d8ed5ca0ba60fac266e2eba34)(@shikijs/[email protected])([email protected]([email protected]))([email protected])
'@pierre/highlights':
specifier: 'catalog:'
version: 0.2.0
@@ -1063,7 +1063,7 @@ importers:
version: 1.8.0
'@pierre/diffs':
specifier: 1.6.1
- version: 1.6.1(patch_hash=4645364ddf113e48e8827f3b4966b3b1ff745317011e5a3d904e2d8672dee7dc)(@shikijs/[email protected])([email protected]([email protected]))([email protected])
+ version: 1.6.1(patch_hash=322ac5f5db8b5715189c010802047f8235c3a45d8ed5ca0ba60fac266e2eba34)(@shikijs/[email protected])([email protected]([email protected]))([email protected])
'@pierre/trees':
specifier: 1.0.0-beta.6
@@ -1485,7 +1485,7 @@ importers:
dependencies:
'@pierre/diffs':
specifier: 1.6.1
- version: 1.6.1(patch_hash=4645364ddf113e48e8827f3b4966b3b1ff745317011e5a3d904e2d8672dee7dc)(@shikijs/[email protected])([email protected]([email protected]))([email protected])
+ version: 1.6.1(patch_hash=322ac5f5db8b5715189c010802047f8235c3a45d8ed5ca0ba60fac266e2eba34)(@shikijs/[email protected])([email protected]([email protected]))([email protected])
'@t3tools/contracts':
specifier: workspace:*
version: link:../contracts
@@ -15575,7 +15575,7 @@ snapshots:
tslib: 2.8.1
webcrypto-core: 1.9.2
- '@pierre/[email protected](patch_hash=4645364ddf113e48e8827f3b4966b3b1ff745317011e5a3d904e2d8672dee7dc)(@shikijs/[email protected])([email protected]([email protected]))([email protected])':
+ '@pierre/[email protected](patch_hash=322ac5f5db8b5715189c010802047f8235c3a45d8ed5ca0ba60fac266e2eba34)(@shikijs/[email protected])([email protected]([email protected]))([email protected])':
dependencies:
'@pierre/highlights': 0.2.0
'@pierre/theme': 2.0.0