Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix(web): preserve partial message selection copy (#493)
Gustavo Ye16b167d79adb
diff --git a/src/web/src/components/community/messages/message.render.test.ts b/src/web/src/components/community/messages/message.render.test.tsindex 581ae110f..7c915ce8a 100644--- a/src/web/src/components/community/messages/message.render.test.ts+++ b/src/web/src/components/community/messages/message.render.test.ts@@ -6,6 +6,7 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query"import {createMessageMenuPointAnchor,Message,+ selectionBelongsToRow,shouldActivateMessageOverlays,shouldSuppressTouchMenuOpen,} from "./message"@@ -270,6 +271,112 @@ describe("Message touch action menu", () => {})})+describe("Message desktop text selection", () => {+ const insideAnchor = {} as Node+ const insideFocus = {} as Node+ const outside = {} as Node+ const rowElement = {+ contains: (node: Node | null) => node === insideAnchor || node === insideFocus,+ }++ it("recognizes a non-collapsed selection with either endpoint inside the row", () => {+ expect(selectionBelongsToRow({+ isCollapsed: false,+ anchorNode: insideAnchor,+ focusNode: outside,+ }, rowElement)).toBe(true)+ expect(selectionBelongsToRow({+ isCollapsed: false,+ anchorNode: outside,+ focusNode: insideFocus,+ }, rowElement)).toBe(true)+ })++ it("rejects collapsed and outside-row selections", () => {+ expect(selectionBelongsToRow({+ isCollapsed: true,+ anchorNode: insideAnchor,+ focusNode: insideFocus,+ }, rowElement)).toBe(false)+ expect(selectionBelongsToRow({+ isCollapsed: false,+ anchorNode: outside,+ focusNode: outside,+ }, rowElement)).toBe(false)+ expect(selectionBelongsToRow(null, rowElement)).toBe(false)+ })++ it("preserves the native context menu for a selection inside the row", () => {+ vi.stubGlobal("window", {+ getSelection: () => ({+ isCollapsed: false,+ anchorNode: insideAnchor,+ focusNode: insideFocus,+ }),+ })+ let renderer: TestRenderer.ReactTestRenderer+ act(() => {+ renderer = TestRenderer.create(makeTree({+ m: baseMsg(),+ onOpenThread: vi.fn(),+ onCopy: vi.fn(),+ }), { createNodeMock: () => genericMock })+ })++ const row = renderer!.root.find(+ (node) => typeof node.props.className === "string"+ && node.props.className.includes("group relative -mx-2"),+ )++ const stopPropagation = vi.fn()+ const preventDefault = vi.fn()+ const preventBaseUIHandler = vi.fn()+ act(() => row.props.onContextMenu({+ currentTarget: rowElement,+ stopPropagation,+ preventDefault,+ preventBaseUIHandler,+ }))++ expect(stopPropagation).toHaveBeenCalledOnce()+ expect(preventDefault).not.toHaveBeenCalled()+ expect(preventBaseUIHandler).toHaveBeenCalledOnce()+ act(() => renderer!.unmount())+ })++ it("keeps Alook's context menu for a row with no active selection", () => {+ vi.stubGlobal("window", { getSelection: () => null })+ let renderer: TestRenderer.ReactTestRenderer+ act(() => {+ renderer = TestRenderer.create(makeTree({+ m: baseMsg(),+ onOpenThread: vi.fn(),+ onCopy: vi.fn(),+ }), { createNodeMock: () => genericMock })+ })++ const row = renderer!.root.find(+ (node) => typeof node.props.className === "string"+ && node.props.className.includes("group relative -mx-2"),+ )++ const stopPropagation = vi.fn()+ const preventDefault = vi.fn()+ const preventBaseUIHandler = vi.fn()+ act(() => row.props.onContextMenu({+ currentTarget: rowElement,+ stopPropagation,+ preventDefault,+ preventBaseUIHandler,+ }))++ expect(stopPropagation).not.toHaveBeenCalled()+ expect(preventDefault).not.toHaveBeenCalled()+ expect(preventBaseUIHandler).not.toHaveBeenCalled()+ act(() => renderer!.unmount())+ })+})+describe("Message image attachment layout", () => {it("keeps a known portrait image intrinsic and constrains it by message width + max height", () => {let renderer: TestRenderer.ReactTestRendererdiff --git a/src/web/src/components/community/messages/message.tsx b/src/web/src/components/community/messages/message.tsxindex 71db4236e..483da9f72 100644--- a/src/web/src/components/community/messages/message.tsx+++ b/src/web/src/components/community/messages/message.tsx@@ -58,6 +58,17 @@ export function shouldSuppressTouchMenuOpen({return nestedControl || selectionInsideRow || longPress}+type RowSelection = Pick<Selection, "isCollapsed" | "anchorNode" | "focusNode">++export function selectionBelongsToRow(+ selection: RowSelection | null,+ row: Pick<HTMLElement, "contains">,+): boolean {+ if (!selection || selection.isCollapsed) return false+ return (!!selection.anchorNode && row.contains(selection.anchorNode))+ || (!!selection.focusNode && row.contains(selection.focusNode))+}+export function createMessageMenuPointAnchor(clientX: number, clientY: number) {const rect = {x: clientX,@@ -209,6 +220,22 @@ function MessageImpl({onPointerEnter={activate}onFocusCapture={activate}onKeyDownCapture={activate}+ onContextMenu={interactive && hoverCapable+ ? (event) => {+ if (!selectionBelongsToRow(window.getSelection(), event.currentTarget)) return++ // Base UI merges the rendered row's handler before its own context-+ // menu handler. Cancel only that library handler, then keep the+ // event away from Base UI's document listener (which otherwise+ // prevents the native menu). Deliberately do not preventDefault:+ // the browser must remain in charge of copying the selection.+ const baseUIEvent = event as typeof event & {+ preventBaseUIHandler?: () => void+ }+ baseUIEvent.preventBaseUIHandler?.()+ event.stopPropagation()+ }+ : undefined}onTouchStart={interactive && !hoverCapable? () => {touchStartedAt.current = performance.now()@@ -234,10 +261,7 @@ function MessageImpl({: interactive && !hoverCapable? (event) => {const selection = window.getSelection()- const selectionInsideRow = !!selection- && !selection.isCollapsed- && !!selection.anchorNode- && event.currentTarget.contains(selection.anchorNode)+ const selectionInsideRow = selectionBelongsToRow(selection, event.currentTarget)const nearestControl = (event.target as Element).closest("button, a, input, textarea, select, [role=button]",)