TypeScript
fix(web): keep the folder path caret in place while editing (#31642)
Julius Marmingefb9f1c6f336d
diff --git a/apps/web/src/components/CommandPalette.composition.browser.test.tsx b/apps/web/src/components/CommandPalette.composition.browser.test.tsxindex 2eb58411808..c0b5b3269ce 100644--- a/apps/web/src/components/CommandPalette.composition.browser.test.tsx+++ b/apps/web/src/components/CommandPalette.composition.browser.test.tsx@@ -133,3 +133,17 @@ describe.each(["repository", "folder", "destination"] as const)("add-project %s"}});});++it("keeps the caret in place when an edit briefly makes the folder path not a path", async () => {+ await using _screen = await render(<CommandPalette>{null}</CommandPalette>);+ await act(() => openCommandPalette({ open: "add-project" }));+ await page.getByText("Local folder", { exact: true }).click();+ const input = () => page.getByRole("combobox");+ await input().fill("/workspace/project");+ await userEvent.keyboard("{Home}{ArrowRight}{Backspace}");+ await expect.element(input()).toHaveValue("workspace/project");+ await userEvent.keyboard("/");+ await expect.element(input()).toHaveValue("/workspace/project");+ await userEvent.keyboard("w");+ await expect.element(input()).toHaveValue("/wworkspace/project");+});diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsxindex 196cb0215cc..7c0821678e5 100644--- a/apps/web/src/components/CommandPalette.tsx+++ b/apps/web/src/components/CommandPalette.tsx@@ -149,6 +149,7 @@ import {hasTrailingPathSeparator,inferProjectTitleFromPath,isExplicitRelativeProjectPath,+ isFilesystemBrowseQuery,isScratchProject,isUnsupportedWindowsProjectPath,resolveProjectPathForDispatch,@@ -215,7 +216,7 @@ import {} from "./CommandPalette.logic";import { resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";import { isSidebarHiddenThread } from "./Sidebar.logic";-import { CommandPaletteContent } from "./CommandPaletteContent";+import { CommandPaletteContent, type PendingInputSelection } from "./CommandPaletteContent";import {selectCommandPaletteClientSettings,useEnvironmentLookups,@@ -429,6 +430,8 @@ function OpenCommandPaletteDialog(props: {const resultListRef = useRef<LegendListRef | null>(null);// A pushed view drops the typed highlight until the user moves it again.const highlightClearedRef = useRef(viewStack.length > 0);+ // Entering or leaving browse mode remounts the input; carry the caret across it.+ const remountSelectionRef = useRef<PendingInputSelection | null>(null);function clearTypedHighlight(): void {highlightClearedRef.current = true;setHighlightedItemValue(null);@@ -923,10 +926,25 @@ function OpenCommandPaletteDialog(props: {setHighlightedItemValue(null);}- function handleQueryChange(nextQuery: string): void {+ function handleQueryChange(nextQuery: string, source: EventTarget | null): void {browseNavigation.invalidate();clearTypedHighlight();- setQuery(canBrowsePaths ? resolvePastedPath(query, nextQuery) : nextQuery);+ const resolvedQuery = canBrowsePaths ? resolvePastedPath(query, nextQuery) : nextQuery;+ // A rewritten paste has no matching offsets, so its caret lands at the end.+ if (+ source instanceof HTMLInputElement &&+ resolvedQuery === nextQuery &&+ (canBrowsePaths && isFilesystemBrowseQuery(resolvedQuery, browseEnvironmentPlatform)) !==+ isBrowsing+ ) {+ remountSelectionRef.current = {+ value: resolvedQuery,+ start: source.selectionStart,+ end: source.selectionEnd,+ direction: source.selectionDirection,+ };+ }+ setQuery(resolvedQuery);if (nextQuery === "" && currentView?.initialQuery) {popView();}@@ -2861,7 +2879,8 @@ function OpenCommandPaletteDialog(props: {scrollCommandPaletteRowIntoView(resultListRef.current, rowIndex);}}}- onValueChange={handleQueryChange}+ onValueChange={(nextQuery, { event }) => handleQueryChange(nextQuery, event.target)}+ pendingSelectionRef={remountSelectionRef}showBackHint={isSubmenu}value={query}>diff --git a/apps/web/src/components/CommandPaletteContent.tsx b/apps/web/src/components/CommandPaletteContent.tsxindex 811d091bf3e..25a72d151b6 100644--- a/apps/web/src/components/CommandPaletteContent.tsx+++ b/apps/web/src/components/CommandPaletteContent.tsx@@ -1,5 +1,5 @@import { ArrowDownIcon, ArrowUpIcon } from "lucide-react";-import { type ComponentProps, type ReactNode } from "react";+import { type ComponentProps, type ReactNode, type RefObject } from "react";import { Command, CommandFooter, CommandInput, CommandPanel } from "./ui/command";import { Kbd, KbdGroup } from "./ui/kbd";@@ -9,8 +9,7 @@ import { Kbd, KbdGroup } from "./ui/kbd";* left there by the removed input. Claim it so typing cannot reach the composer* behind the modal. On first open the dialog's own initial focus handles it.*/-function claimFocusInOpenDialog(input: HTMLInputElement | null) {- if (!input) return;+function claimFocusInOpenDialog(input: HTMLInputElement) {const active = input.ownerDocument.activeElement;if (active === null ||@@ -21,6 +20,24 @@ function claimFocusInOpenDialog(input: HTMLInputElement | null) {}}+export interface PendingInputSelection {+ readonly value: string;+ readonly start: number | null;+ readonly end: number | null;+ readonly direction: "forward" | "backward" | "none" | null;+}++function restorePendingSelection(+ input: HTMLInputElement,+ pendingSelectionRef: RefObject<PendingInputSelection | null> | undefined,+) {+ const selection = pendingSelectionRef?.current;+ if (!pendingSelectionRef || !selection) return;+ pendingSelectionRef.current = null;+ if (input.value !== selection.value) return;+ input.setSelectionRange(selection.start, selection.end, selection.direction ?? undefined);+}+type CommandPaletteContentProps = Omit<ComponentProps<typeof Command>, "children"> & {readonly children: ReactNode;readonly escapeLabel?: ReactNode;@@ -28,6 +45,7 @@ type CommandPaletteContentProps = Omit<ComponentProps<typeof Command>, "childrenreadonly footerTrailing?: ReactNode;readonly inputAccessory?: ReactNode;readonly inputProps: ComponentProps<typeof CommandInput>;+ readonly pendingSelectionRef?: RefObject<PendingInputSelection | null>;readonly panelSize?: "list" | "tall-list" | "fill";readonly showBackHint?: boolean;readonly testId?: string;@@ -40,6 +58,7 @@ export function CommandPaletteContent({footerTrailing,inputAccessory,inputProps,+ pendingSelectionRef,panelSize = "list",showBackHint,testId,@@ -49,7 +68,15 @@ export function CommandPaletteContent({<div className="contents" data-testid={testId}><Command {...commandProps}><div className="relative">- <CommandInput autoFocus={false} {...inputProps} ref={claimFocusInOpenDialog} />+ <CommandInput+ autoFocus={false}+ {...inputProps}+ ref={(input) => {+ if (!input) return;+ restorePendingSelection(input, pendingSelectionRef);+ claimFocusInOpenDialog(input);+ }}+ />{inputAccessory}</div><CommandPanel