diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message-selection.test.ts b/apps/desktop/src/components/assistant-ui/thread/user-message-selection.test.ts new file mode 100644 index 0000000000..0b766c062a --- /dev/null +++ b/apps/desktop/src/components/assistant-ui/thread/user-message-selection.test.ts @@ -0,0 +1,28 @@ +import { afterEach, describe, expect, it } from 'vitest' + +import { hasTextSelection } from './user-message' + +afterEach(() => { + window.getSelection()?.removeAllRanges() + document.body.replaceChildren() +}) + +describe('hasTextSelection', () => { + it('is false with nothing highlighted', () => { + expect(hasTextSelection()).toBe(false) + }) + + it('is true once the user has a live range', () => { + const node = document.createElement('span') + node.textContent = 'copy me' + document.body.appendChild(node) + + const range = document.createRange() + range.selectNodeContents(node) + const selection = window.getSelection()! + selection.removeAllRanges() + selection.addRange(range) + + expect(hasTextSelection()).toBe(true) + }) +}) diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message.tsx index 16c6963e24..2ea43309df 100644 --- a/apps/desktop/src/components/assistant-ui/thread/user-message.tsx +++ b/apps/desktop/src/components/assistant-ui/thread/user-message.tsx @@ -16,6 +16,13 @@ import { cn } from '@/lib/utils' import { notifyThreadEditOpen } from '@/store/thread-scroll' import { isWatchWindow } from '@/store/windows' +/** True when the user has a live text highlight (drag-select / triple-click). */ +export function hasTextSelection(): boolean { + const selection = window.getSelection() + + return Boolean(selection && !selection.isCollapsed && selection.toString().length > 0) +} + export function StickyHumanMessageContainer({ attachments, children, @@ -279,10 +286,16 @@ export const UserMessage: FC<{
{ + if (hasTextSelection()) { + return + } + event.preventDefault() setPickerOpen(true) } @@ -295,7 +308,9 @@ export const UserMessage: FC<{ aria-expanded={bodyClamped ? expanded : undefined} className={cn(bubbleClassName, !bodyClamped && 'cursor-default')} onClick={() => { - if (!bodyClamped) { + // Drag-select ends on mouseup→click; don't collapse the + // clamp just because the highlight finished. + if (hasTextSelection() || !bodyClamped) { return } @@ -310,12 +325,29 @@ export const UserMessage: FC<{ ) : ( // Always editable — clicking opens the edit composer even while a // turn streams; sending the edit reverts (interrupt + rewind). + // A live text highlight wins: finishing a drag-select must not + // open the editor and throw the selection away.