fix: allow any composer ancestor for cmd+enter submit (#1639)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinjing
2026-05-09 18:17:39 -07:00
committed by GitHub
co-authored by Orca
parent 4e8be92c84
commit 7c51f07858
2 changed files with 21 additions and 28 deletions
@@ -12,19 +12,18 @@ function makeEvent(overrides: Partial<{ isComposing: boolean; shiftKey: boolean
}
class FakeHTMLElement extends EventTarget {
private readonly children = new Set<FakeHTMLElement>()
private readonly descendants = new Set<FakeHTMLElement>()
append(child: FakeHTMLElement): void {
this.children.add(child)
this.descendants.add(child)
}
contains(target: EventTarget): boolean {
return target === this || this.children.has(target as FakeHTMLElement)
return target === this || this.descendants.has(target as FakeHTMLElement)
}
}
let previousHTMLElement: typeof globalThis.HTMLElement | undefined
let previousDocument: typeof globalThis.document | undefined
describe('shouldSuppressEnterSubmit', () => {
it('returns false for a plain Enter with no composition', () => {
@@ -53,17 +52,10 @@ describe('shouldSuppressEnterSubmit', () => {
describe('shouldAllowComposerEnterSubmitTarget', () => {
beforeEach(() => {
previousHTMLElement = globalThis.HTMLElement
previousDocument = globalThis.document
const body = new FakeHTMLElement()
const documentElement = new FakeHTMLElement()
Object.defineProperty(globalThis, 'HTMLElement', {
configurable: true,
value: FakeHTMLElement
})
Object.defineProperty(globalThis, 'document', {
configurable: true,
value: { body, documentElement }
})
})
afterEach(() => {
@@ -71,10 +63,6 @@ describe('shouldAllowComposerEnterSubmitTarget', () => {
configurable: true,
value: previousHTMLElement
})
Object.defineProperty(globalThis, 'document', {
configurable: true,
value: previousDocument
})
})
it('allows targets inside the composer', () => {
@@ -87,21 +75,25 @@ describe('shouldAllowComposerEnterSubmitTarget', () => {
)
})
it('allows body/document targets after a source selection drops focus', () => {
it('allows ancestor targets after a source selection drops focus', () => {
// Why: post-selection focus can land on body, html, or the DialogContent
// root — any ancestor wrapping the composer is a legitimate fallback.
const composer = new FakeHTMLElement()
const dialogContent = new FakeHTMLElement()
const body = new FakeHTMLElement()
dialogContent.append(composer)
body.append(composer)
body.append(dialogContent)
expect(
shouldAllowComposerEnterSubmitTarget(document.body, composer as unknown as HTMLElement)
).toBe(true)
expect(
shouldAllowComposerEnterSubmitTarget(
document.documentElement,
composer as unknown as HTMLElement
)
shouldAllowComposerEnterSubmitTarget(dialogContent, composer as unknown as HTMLElement)
).toBe(true)
expect(shouldAllowComposerEnterSubmitTarget(body, composer as unknown as HTMLElement)).toBe(
true
)
})
it('rejects targets outside the composer', () => {
it('rejects sibling targets outside the composer', () => {
const composer = new FakeHTMLElement()
const outside = new FakeHTMLElement()
@@ -28,8 +28,9 @@ export function shouldAllowComposerEnterSubmitTarget(
if (composer?.contains(target)) {
return true
}
// Why: selecting a PR/issue/Linear row replaces the focused input with a
// source pill, and Chromium can retarget the next global keydown to body.
// Keep the modal shortcut alive for that post-selection focus fallback.
return target === document.body || target === document.documentElement
// Why: selecting a PR/issue/Linear row tears down the focused input and
// Radix's focus restore can land on body/documentElement, the DialogContent
// root, or any other ancestor wrapping the composer. Allow any ancestor so
// the modal's Cmd/Ctrl+Enter shortcut keeps firing post-selection.
return composer ? target.contains(composer) : false
}