feat(app): allow selecting question answer text (#60372)

This commit is contained in:
usrnk1
2026-10-29 22:05:43 +01:00
committed by GitHub
Unverified
parent 53ad857b7a
commit 7ed1f96e29
5 changed files with 152 additions and 17 deletions
@@ -2,6 +2,70 @@ import type { Locator } from "@playwright/test"
import type { MotionKnob } from "@opencode/ui/motion-tuning"
import { expect, sourceURL, story } from "../../stories/playwright/story"
for (const variant of ["classic", "experimental"] as const) {
story(`${variant} answer cards allow text selection without changing answers`, async ({ mount, page }) => {
await page.addInitScript(
(enabled) => localStorage.setItem("settings.v3", JSON.stringify({ general: { newQuestionUI: enabled } })),
variant === "experimental",
)
const component = await mount("app-current-session-surface--question-request")
const question = component.locator('[data-component="dock-prompt"][data-kind="question"]')
const wide = question.getByRole("radio", { name: /Wide/ })
await expect(question.getByRole("radio", { name: /Focused/ })).toBeFocused()
await page.evaluate(() => document.fonts.ready.then(() => undefined))
const drag = async (text: Locator) => {
await text.scrollIntoViewIfNeeded()
const box = await text.boundingBox()
expect(box).not.toBeNull()
await page.mouse.move(box!.x + 1, box!.y + box!.height / 2)
await page.mouse.down()
await page.mouse.move(box!.x + box!.width - 1, box!.y + box!.height / 2, { steps: 12 })
await page.mouse.up()
await expect.poll(() => page.evaluate(() => window.getSelection()?.toString().trim())).toBe(await text.innerText())
}
for (const slot of ["option-label", "option-description"]) {
await drag(wide.locator(`[data-slot="${slot}"] bdi`))
await expect(wide).not.toBeChecked()
}
await wide.locator('[data-slot="option-description"] bdi').click({ modifiers: ["Shift"] })
await expect(wide).not.toBeChecked()
await wide.click({ position: { x: 4, y: 4 } })
await expect(wide).toBeChecked()
await question.getByRole("radio", { name: /Focused/ }).click()
await drag(wide.locator('[data-slot="option-description"] bdi'))
await expect(wide).not.toBeChecked()
const mark = await wide.locator('[data-slot="question-option-check"]').boundingBox()
expect(mark).not.toBeNull()
await page.mouse.move(mark!.x + mark!.width / 2, mark!.y + mark!.height / 2)
await page.mouse.down()
await page.mouse.move(mark!.x + mark!.width / 2 + 1, mark!.y + mark!.height / 2)
await page.mouse.up()
await expect(wide).toBeChecked()
await question.getByRole("button", { name: "Next", exact: true }).click()
const files = question.getByRole("checkbox", { name: "Changed files", exact: true })
await expect(files).toBeFocused()
await files.click()
await expect(files).toBeChecked()
await drag(files.locator('[data-slot="option-label"] bdi'))
await expect(files).toBeChecked()
await files.click({ position: { x: 4, y: 4 } })
await expect(files).not.toBeChecked()
})
}
story(
"uses the original question cards by default and submits selected and custom answers",
async ({ mount, page }) => {
@@ -32,7 +96,9 @@ story(
const submit = question.getByRole("button", { name: "Submit", exact: true })
expect(await submit.evaluate((button) => button.getBoundingClientRect().width)).toBe(width)
await expect(shortcuts).toHaveText(["1", "2", "3", "4"])
await files.click()
await page.keyboard.down(" ")
await page.keyboard.down(" ")
await page.keyboard.up(" ")
await terminal.click()
await expect(files).toBeChecked()
await expect(terminal).toBeChecked()
@@ -47,22 +47,57 @@ function Option(props: {
description?: string
number: number
disabled: boolean
ref?: (el: HTMLButtonElement) => void
ref?: (el: HTMLDivElement) => void
onFocus?: VoidFunction
onClick: VoidFunction
}) {
let pointer: { x: number; y: number } | undefined
return (
<button
type="button"
<div
ref={props.ref}
data-slot="question-option"
data-picked={props.picked}
data-disabled={props.disabled}
role={props.multi ? "checkbox" : "radio"}
aria-checked={props.picked}
aria-keyshortcuts={props.number <= 9 ? String(props.number) : undefined}
disabled={props.disabled}
aria-disabled={props.disabled}
tabindex={props.disabled ? -1 : 0}
onFocus={props.onFocus}
onClick={props.onClick}
onPointerDown={(event) => (pointer = { x: event.clientX, y: event.clientY })}
onClick={(event) => {
const moved = pointer && (event.clientX !== pointer.x || event.clientY !== pointer.y)
pointer = undefined
if (props.disabled) return
const selection = window.getSelection()
// Padding and nonselectable marks can retain an old highlight without selecting text.
if (
event.detail > 0 &&
(moved || event.shiftKey) &&
!(event.target instanceof Element && event.target.closest('[data-slot="question-option-check"]')) &&
selection &&
!selection.isCollapsed &&
selection.getRangeAt(0).intersectsNode(event.currentTarget)
)
return
props.onClick()
}}
onKeyDown={(event) => {
if (event.defaultPrevented || event.isComposing || event.altKey || event.ctrlKey || event.metaKey) return
if (event.key !== " " && event.key !== "Enter") return
event.preventDefault()
if (event.key === " " && event.repeat) return
if (!props.disabled) props.onClick()
}}
>
<Mark multi={props.multi} picked={props.picked} />
<span data-slot="question-option-main">
@@ -83,7 +118,7 @@ function Option(props: {
aria-hidden="true"
/>
</Show>
</button>
</div>
)
}
@@ -126,7 +161,7 @@ export const SessionQuestionDockClassic: Component<{ request: FormInfo; onSubmit
let optionsRef: HTMLDivElement | undefined
let customRef: HTMLButtonElement | undefined
let customInput: HTMLInputElement | undefined
let optsRef: HTMLButtonElement[] = []
let optsRef: HTMLDivElement[] = []
let replied = false
let focusFrame: number | undefined
@@ -194,23 +194,47 @@ function Option(props: {
shortcut?: string
disabled: boolean
tabbable: boolean
ref?: (el: HTMLButtonElement) => void
ref?: (el: HTMLDivElement) => void
onFocus?: VoidFunction
onClick: VoidFunction
}) {
let pointer: { x: number; y: number } | undefined
return (
<button
type="button"
<div
ref={props.ref}
data-slot="question-option"
data-picked={props.picked}
data-disabled={props.disabled}
role={props.multi ? "checkbox" : "radio"}
aria-checked={props.picked}
aria-keyshortcuts={props.shortcut}
tabindex={props.tabbable ? 0 : -1}
disabled={props.disabled}
tabindex={!props.disabled && props.tabbable ? 0 : -1}
aria-disabled={props.disabled}
onFocus={props.onFocus}
onClick={props.onClick}
onPointerDown={(event) => (pointer = { x: event.clientX, y: event.clientY })}
onClick={(event) => {
const moved = pointer && (event.clientX !== pointer.x || event.clientY !== pointer.y)
pointer = undefined
if (props.disabled) return
const selection = window.getSelection()
// Padding and nonselectable marks can retain an old highlight without selecting text.
if (
event.detail > 0 &&
(moved || event.shiftKey) &&
!(event.target instanceof Element && event.target.closest('[data-slot="question-option-check"]')) &&
selection &&
!selection.isCollapsed &&
selection.getRangeAt(0).intersectsNode(event.currentTarget)
)
return
props.onClick()
}}
>
<Mark multi={props.multi} picked={props.picked} shortcut={props.shortcut} />
<span data-slot="question-option-main">
@@ -223,7 +247,7 @@ function Option(props: {
</span>
</Show>
</span>
</button>
</div>
)
}
@@ -270,7 +294,7 @@ export const SessionQuestionDockExperimental: Component<{ request: FormInfo; onS
let customRow: HTMLDivElement | undefined
let highlightRef: HTMLSpanElement | undefined
let customInput: HTMLInputElement | undefined
let optsRef: HTMLButtonElement[] = []
let optsRef: HTMLDivElement[] = []
let replied = false
const question = createMemo(() => questions()[store.tab])
@@ -303,6 +303,8 @@
}
[data-slot="question-option-check"] {
-webkit-user-select: none;
user-select: none;
display: inline-flex;
align-items: center;
justify-content: center;
@@ -358,6 +360,8 @@
}
[data-slot="question-option-main"] {
-webkit-user-select: text;
user-select: text;
display: flex;
flex-direction: column;
gap: 4px;
@@ -246,6 +246,8 @@
}
[data-slot="question-option-check"] {
-webkit-user-select: none;
user-select: none;
display: inline-flex;
align-items: center;
justify-content: center;
@@ -308,6 +310,8 @@
}
[data-slot="question-option-main"] {
-webkit-user-select: text;
user-select: text;
display: flex;
flex-direction: column;
gap: 6px;
@@ -316,6 +320,8 @@
}
[data-slot="question-option-shortcut"] {
-webkit-user-select: none;
user-select: none;
--keybind-font-size: 10px;
--keybind-font-weight: var(--font-weight-regular);
--keybind-transition-duration: var(--question-feedback-duration);
@@ -337,7 +343,7 @@
[data-slot="option-description"] {
min-inline-size: 0;
font-family: var(--v2-font-family-sans);
font-size: 11px;
font-size: 12px;
font-weight: 440;
line-height: var(--line-height-tight);
letter-spacing: 0.05px;