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): respect view mode in the prompt fields
joshistoastbcdfa8bdc06c
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/GeneratePromptFields.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/GeneratePromptFields.tsxindex 7a889facb9..aa596b3c2d 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/GeneratePromptFields.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/GeneratePromptFields.tsx@@ -79,8 +79,12 @@ export const GeneratePromptFields = ({[onCommitImmediate]);+ // View mode is a way of reading one particular template, so it does not carry+ // over to the next one. Left on, applying a template made the prompt box go+ // read-only the instant it was picked, with no interaction to explain it.const applyPromptTemplate = useCallback(- (promptTemplate: PromptTemplateSnapshot | null) => onCommitImmediate({ promptTemplate }),+ (promptTemplate: PromptTemplateSnapshot | null) =>+ onCommitImmediate({ promptTemplate, promptTemplateViewMode: false }),[onCommitImmediate]);diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.browser.test.tsxnew file mode 100644index 0000000000..dbc574846f--- /dev/null+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.browser.test.tsx@@ -0,0 +1,87 @@+/* oxlint-disable react-perf/jsx-no-new-array-as-prop, react-perf/jsx-no-new-function-as-prop */+import { ChakraProvider } from '@chakra-ui/react';+import { NegativePromptField } from '@features/generation/ui/promptFields/NegativePromptField';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import { system } from '@theme/system';+import { act } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, describe, expect, it, vi } from 'vitest';++vi.mock('@features/generation/ui/promptFields/PositivePromptActions', () => ({+ AddPromptTriggerButton: () => null,+ PromptTriggerPopover: () => null,+}));++vi.mock('@features/generation/ui/GenerationUiContext', async (importOriginal) => ({+ ...(await importOriginal<object>()),+ useGenerationUi: () => ({ models: { catalog: [], ensureLoaded: vi.fn() } }),+}));++let host: HTMLDivElement | null = null;+let root: Root | null = null;+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;++const render = async (templateNegativePrompt: string | null) => {+ host = document.createElement('div');+ document.body.append(host);+ root = createRoot(host);++ await act(() => {+ root?.render(+ <QueryClientProvider client={new QueryClient()}>+ <ChakraProvider value={system}>+ <NegativePromptField+ heightPx={56}+ isEnabled+ isTemplateViewMode+ loras={[]}+ projectId="project-1"+ selectedModel={undefined}+ showSyntaxHighlighting={false}+ templateNegativePrompt={templateNegativePrompt}+ value="blurry"+ onChange={vi.fn()}+ onEnabledChange={vi.fn()}+ onResizeEnd={vi.fn()}+ onTemplateViewModeChange={vi.fn()}+ />+ </ChakraProvider>+ </QueryClientProvider>+ );+ });++ return host.querySelector('textarea')!;+};++afterEach(async () => {+ await act(() => root?.unmount());+ host?.remove();+ host = null;+ root = null;+});++describe('the negative prompt in template view mode', () => {+ it('shows the merged text when the template has a negative side', async () => {+ const textarea = await render('lowres, {prompt}');++ expect(textarea.readOnly).toBe(true);+ expect(textarea.value).toBe('lowres, blurry');+ });++ // Most templates carry no negative side at all. Treating that empty string as+ // something to view made this field read-only for no reason, showing the+ // authored text with the merge's trailing space hanging off the end of it.+ it('stays editable when the template has none', async () => {+ const textarea = await render('');++ expect(textarea.readOnly).toBe(false);+ expect(textarea.value).toBe('blurry');+ });++ it('stays editable when no template is applied', async () => {+ const textarea = await render(null);++ expect(textarea.readOnly).toBe(false);+ expect(textarea.value).toBe('blurry');+ });+});diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.tsxindex e90c7a5e83..7c87b00da9 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.tsx@@ -87,10 +87,16 @@ export const NegativePromptField = ({}, []);// Same textarea, read-only, as on the positive side — swapping in a different- // component would reset the resizer's mounted height. The `&&` matters: view- // mode with no template leaves the prompt editable, so gating on the toggle- // alone would kill the autocomplete while the user is still typing.- const isViewingMerged = isTemplateViewMode && templateNegativePrompt !== null;+ // component would reset the resizer's mounted height.+ //+ // An empty string here is a template that carries no negative side, which is+ // most of them: `null` means no template at all. Neither has anything to show,+ // and treating the empty one as something to view turned this field read-only+ // and rendered the authored text with the merge's trailing space hanging off+ // it. Gating on the toggle alone would be worse still — view mode can be on+ // with no template applied, which leaves the prompt perfectly editable.+ const viewedTemplatePrompt = isTemplateViewMode && templateNegativePrompt ? templateNegativePrompt : null;+ const isViewingMerged = viewedTemplatePrompt !== null;const autocomplete = usePromptTriggerAutocomplete({isDisabled: isViewingMerged,@@ -194,8 +200,8 @@ export const NegativePromptField = ({);const templateChunks = useMemo(- () => (isViewingMerged ? getPromptTemplateChunks(draftValue, templateNegativePrompt) : null),- [draftValue, isViewingMerged, templateNegativePrompt]+ () => (viewedTemplatePrompt === null ? null : getPromptTemplateChunks(draftValue, viewedTemplatePrompt)),+ [draftValue, viewedTemplatePrompt]);const exitViewMode = useCallback(() => onTemplateViewModeChange?.(false), [onTemplateViewModeChange]);diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/ResizableTextareaIntegration.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/ResizableTextareaIntegration.browser.test.tsxindex 743d3eab3b..e7c9acdbf1 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/ResizableTextareaIntegration.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/ResizableTextareaIntegration.browser.test.tsx@@ -202,4 +202,47 @@ describe('ResizableTextarea', () => {await expect.poll(() => textarea.value).toBe('hello+ world');});++ // Regression: the write goes through the native value setter and a synthetic+ // `input` event, which React honours even on a read-only textarea. In template+ // view mode that wrote the merged text back as the authored prompt, and the+ // next submit merged it a second time.+ it('leaves a read-only prompt alone', async () => {+ const ReadOnlyHarness = () => (+ <PromptTextarea+ {...PROMPT_ATTENTION_TARGET_PROPS}+ aria-label="Prompt"+ defaultHeightPx={96}+ minHeightPx={56}+ readOnly+ resizeHandleAriaLabel="Resize prompt"+ showSyntaxHighlighting+ value="hello world"+ onChange={vi.fn()}+ />+ );++ host = document.createElement('div');+ document.body.append(host);+ root = createRoot(host);++ await act(() => {+ root?.render(+ <ChakraProvider value={system}>+ <ReadOnlyHarness />+ </ChakraProvider>+ );+ });++ const textarea = host.querySelector<HTMLTextAreaElement>('textarea')!;++ textarea.focus();+ textarea.setSelectionRange(0, 5);++ await act(() => {+ expect(adjustFocusedPromptAttention('increment', false)).toBe(false);+ });++ expect(textarea.value).toBe('hello world');+ });});diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptAttentionHotkeys.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptAttentionHotkeys.tsindex 4e742d1b7d..366572f3ab 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptAttentionHotkeys.ts+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptAttentionHotkeys.ts@@ -5,8 +5,17 @@ import { adjustPromptAttention } from '@features/generation/core/prompt/attentioexport const PROMPT_ATTENTION_TARGET_ATTR = 'data-prompt-attention-target';export const PROMPT_ATTENTION_TARGET_PROPS = { [PROMPT_ATTENTION_TARGET_ATTR]: 'true' } as const;+/**+ * `readOnly` is part of the test, not an afterthought. Writing here goes through+ * the native value setter and a synthetic `input` event, which React accepts on+ * a read-only textarea — so in template view mode the hotkeys wrote the *merged*+ * text back as the authored prompt, and the next submit merged it again.+ * Clicking the box leaves view mode, but tabbing into it does not.+ */const isPromptAttentionTarget = (element: Element | null): element is HTMLTextAreaElement =>- element instanceof HTMLTextAreaElement && element.getAttribute(PROMPT_ATTENTION_TARGET_ATTR) === 'true';+ element instanceof HTMLTextAreaElement &&+ element.getAttribute(PROMPT_ATTENTION_TARGET_ATTR) === 'true' &&+ !element.readOnly;const setTextareaValue = (textarea: HTMLTextAreaElement, value: string): void => {const valueSetter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value')?.set;