fix(desktop): drag-select and ⌘C work on user bubbles again
User bubbles are buttons, so the global user-select:none rule killed text selection. Right-click-to-react and click-to-edit also ate a live highlight. Prefer selection when one exists.
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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<{
|
||||
<div
|
||||
className="relative w-full"
|
||||
onContextMenu={
|
||||
// Right-click is the desktop stand-in for iOS touch-and-hold.
|
||||
// Right-click is the desktop stand-in for iOS touch-and-hold —
|
||||
// but only when there's nothing selected. A live highlight
|
||||
// keeps the native Copy menu (and ⌘C) instead of the picker.
|
||||
readOnly || !reactionsEnabled
|
||||
? undefined
|
||||
: event => {
|
||||
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.
|
||||
<ActionBarPrimitive.Edit asChild>
|
||||
<button
|
||||
aria-label={copy.editMessage}
|
||||
className={bubbleClassName}
|
||||
onClick={() => triggerHaptic('selection')}
|
||||
onPointerDown={() => notifyThreadEditOpen()}
|
||||
onClick={event => {
|
||||
if (hasTextSelection()) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
triggerHaptic('selection')
|
||||
}}
|
||||
onPointerDown={() => {
|
||||
if (hasTextSelection()) {
|
||||
return
|
||||
}
|
||||
|
||||
notifyThreadEditOpen()
|
||||
}}
|
||||
title={copy.editMessage}
|
||||
type="button"
|
||||
>
|
||||
|
||||
@@ -1008,6 +1008,15 @@ button,
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
/* User bubbles are <button>s (click → edit), so the rule above would kill
|
||||
drag-to-select and leave ⌘C with nothing to grab. Message text stays
|
||||
selectable; chrome controls outside the bubble keep user-select: none. */
|
||||
[data-slot='aui_user-message-root'] button,
|
||||
[data-slot='aui_user-message-root'] [role='button'] {
|
||||
-webkit-user-select: text;
|
||||
user-select: text;
|
||||
}
|
||||
|
||||
img,
|
||||
picture,
|
||||
video,
|
||||
|
||||
Reference in New Issue
Block a user