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<{