Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(webv2): leave IME composition alone
joshistoast8b484f16b99e
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsxindex 72da473155..16b750c431 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx@@ -261,6 +261,53 @@ describe('the caret autocomplete', () => {expect(textarea().value).toBe('a photo of __');});+ // An IME sends every key through `keydown` while composing. Taking ArrowDown+ // and Enter for the list leaves the candidate window unusable, so a Japanese+ // or Chinese user cannot commit a word at all.+ describe('while an IME is composing', () => {+ const compose = (type: 'compositionend' | 'compositionstart') =>+ act(() => {+ textarea().dispatchEvent(new CompositionEvent(type, { bubbles: true, data: 'ねこ' }));+ });++ const pressWhileComposing = (key: string) =>+ act(() => {+ textarea().dispatchEvent(+ new KeyboardEvent('keydown', { bubbles: true, cancelable: true, isComposing: true, key })+ );+ });++ it('leaves Enter to the composition instead of inserting', async () => {+ await type('a photo of __');+ await compose('compositionstart');+ await pressWhileComposing('Enter');++ expect(textarea().value).toBe('a photo of __');+ });++ it('leaves the arrow keys to the candidate window', async () => {+ await type('a photo of __');+ await compose('compositionstart');+ await pressWhileComposing('ArrowDown');+ await compose('compositionend');+ await press('{Enter}');++ // Still the first option: the arrow moved an IME candidate, not this list.+ expect(textarea().value).toBe('a photo of __colors__');+ });++ it('reopens against the committed text once composition ends', async () => {+ await type('a photo of __');+ await compose('compositionstart');++ expect(listbox()).toBeNull();++ await compose('compositionend');++ expect(optionLabels()).toEqual(['colors', 'moods']);+ });+ });+it('closes on Escape and leaves the prompt exactly as typed', async () => {await type('a photo of __');await press('{Escape}');diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsxindex 5c1b5588ca..1f1075acd6 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx@@ -17,7 +17,7 @@ import type { GenerateLora, GenerateModelConfig } from '@features/generation/corimport type { CaretRect } from '@features/generation/ui/promptFields/promptCaret';import type { PromptTriggerKey, PromptTriggerQuery } from '@features/generation/ui/promptFields/promptFocus';import type { PromptTriggerOption } from '@features/generation/ui/promptFields/promptTriggerOptions';-import type { KeyboardEvent, ReactNode } from 'react';+import type { CompositionEvent, KeyboardEvent, ReactNode } from 'react';import { getTextareaCaretRect } from '@features/generation/ui/promptFields/promptCaret';import { getActiveTriggerQuery, insertPromptText } from '@features/generation/ui/promptFields/promptFocus';@@ -26,7 +26,7 @@ import {getInlineTriggerOptions,usePromptTriggerOptions,} from '@features/generation/ui/promptFields/promptTriggerOptions';-import { useCallback, useId, useMemo, useState } from 'react';+import { useCallback, useId, useMemo, useRef, useState } from 'react';interface AutocompleteState {caretRect: CaretRect;@@ -42,13 +42,19 @@ interface AutocompleteState {}export interface PromptTriggerAutocompleteApi {- /** Spread onto the textarea so assistive tech sees a combobox. */+ /**+ * Spread onto the textarea so assistive tech sees a combobox, and so the list+ * stays out of the way while an IME is composing.+ */comboboxProps: {'aria-activedescendant': string | undefined;'aria-autocomplete': 'list';'aria-controls': string | undefined;'aria-expanded': boolean;+ 'aria-multiline': true;role: 'combobox';+ onCompositionEnd: (event: CompositionEvent<HTMLTextAreaElement>) => void;+ onCompositionStart: () => void;};element: ReactNode;isOpen: boolean;@@ -77,6 +83,7 @@ export const usePromptTriggerAutocomplete = ({const optionIdPrefix = `${listboxId}-option-`;const [state, setState] = useState<AutocompleteState | null>(null);const [activeIndex, setActiveIndex] = useState(0);+ const isComposingRef = useRef(false);const options = usePromptTriggerOptions(loras, selectedModel);const matches = useMemo(@@ -88,7 +95,10 @@ export const usePromptTriggerAutocomplete = ({const refresh = useCallback((textarea: HTMLTextAreaElement | null) => {- if (!textarea || isDisabled) {+ // Mid-composition the field holds half-committed romaji, which is not a+ // wildcard name being typed. Reading it would re-anchor the list on every+ // keystroke the IME is still working on.+ if (!textarea || isDisabled || isComposingRef.current) {setState(null);return;}@@ -123,7 +133,11 @@ export const usePromptTriggerAutocomplete = ({const handleKeyDown = useCallback((event: KeyboardEvent<HTMLTextAreaElement>): boolean => {- if (!isOpen) {+ // While an IME is composing, the arrows move through its candidate window+ // and Enter commits the composition. Taking either would leave a Japanese+ // or Chinese user unable to pick a candidate at all. `keyCode === 229` is+ // the same signal for the browsers that do not set `isComposing`.+ if (!isOpen || event.nativeEvent.isComposing || event.keyCode === 229) {return false;}@@ -165,6 +179,21 @@ export const usePromptTriggerAutocomplete = ({[activeIndex, isOpen, matches, selectOption]);+ const handleCompositionStart = useCallback(() => {+ isComposingRef.current = true;+ setState(null);+ }, []);++ const handleCompositionEnd = useCallback(+ (event: CompositionEvent<HTMLTextAreaElement>) => {+ isComposingRef.current = false;+ // The composed text is only in the field now, so this is the first point+ // at which a trigger under the caret is worth reading again.+ refresh(event.currentTarget);+ },+ [refresh]+ );+return {close,comboboxProps: {@@ -172,6 +201,12 @@ export const usePromptTriggerAutocomplete = ({'aria-autocomplete': 'list','aria-controls': isOpen ? listboxId : undefined,'aria-expanded': isOpen,+ // `combobox` overrides the textarea's implicit `textbox` role, which is+ // what carries multiline-ness; without this a tall prompt box announces+ // as a single-line field.+ 'aria-multiline': true,+ onCompositionEnd: handleCompositionEnd,+ onCompositionStart: handleCompositionStart,role: 'combobox',},element: