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
feat(webv2): add resizable textarea component
joshistoastff698a8ed6a2
diff --git a/invokeai/frontend/webv2/src/workbench/components/ui/ResizableTextarea.tsx b/invokeai/frontend/webv2/src/workbench/components/ui/ResizableTextarea.tsxnew file mode 100644index 0000000000..9d1dc621f8--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/components/ui/ResizableTextarea.tsx@@ -0,0 +1,134 @@+import type {+ ComponentProps,+ KeyboardEvent as ReactKeyboardEvent,+ PointerEvent as ReactPointerEvent,+ Ref,+} from 'react';++import { Box, Textarea } from '@chakra-ui/react';+import { useState } from 'react';++type TextareaProps = ComponentProps<typeof Textarea>;++const DEFAULT_STEP_PX = 12;+const DEFAULT_LARGE_STEP_PX = 48;++const clamp = (value: number, min: number, max: number): number => Math.min(Math.max(value, min), max);++export interface ResizableTextareaProps extends Omit<+ TextareaProps,+ 'h' | 'height' | 'maxH' | 'maxHeight' | 'minH' | 'minHeight' | 'resize'+> {+ defaultHeightPx: number;+ maxHeightPx?: number;+ minHeightPx: number;+ resizeHandleAriaLabel: string;+ largeStepPx?: number;+ stepPx?: number;+ textareaRef?: Ref<HTMLTextAreaElement>;+ onResizeEnd?: (heightPx: number) => void;+}++export const ResizableTextarea = ({+ defaultHeightPx,+ largeStepPx = DEFAULT_LARGE_STEP_PX,+ maxHeightPx = 420,+ minHeightPx,+ onResizeEnd,+ resizeHandleAriaLabel,+ stepPx = DEFAULT_STEP_PX,+ textareaRef,+ ...textareaProps+}: ResizableTextareaProps) => {+ const initialHeightPx = clamp(defaultHeightPx, minHeightPx, maxHeightPx);+ const [heightPx, setHeightPx] = useState(initialHeightPx);+ const [dragHeightPx, setDragHeightPx] = useState<number | null>(null);+ const displayHeightPx = dragHeightPx ?? heightPx;++ const commitHeight = (nextHeightPx: number) => {+ const clampedHeightPx = clamp(nextHeightPx, minHeightPx, maxHeightPx);++ setHeightPx(clampedHeightPx);+ onResizeEnd?.(clampedHeightPx);+ };++ const handlePointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {+ event.preventDefault();++ const startY = event.clientY;+ const startHeightPx = displayHeightPx;+ let nextHeightPx = startHeightPx;++ const handlePointerMove = (moveEvent: PointerEvent) => {+ nextHeightPx = clamp(startHeightPx + moveEvent.clientY - startY, minHeightPx, maxHeightPx);+ setDragHeightPx(nextHeightPx);+ };++ const handlePointerUp = () => {+ window.removeEventListener('pointermove', handlePointerMove);+ window.removeEventListener('pointerup', handlePointerUp);+ window.removeEventListener('pointercancel', handlePointerUp);+ setDragHeightPx(null);+ commitHeight(nextHeightPx);+ };++ window.addEventListener('pointermove', handlePointerMove);+ window.addEventListener('pointerup', handlePointerUp);+ window.addEventListener('pointercancel', handlePointerUp);+ };++ const handleKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {+ const step = event.shiftKey ? largeStepPx : stepPx;+ const heightChanges: Partial<Record<string, number>> = {+ ArrowDown: step,+ ArrowUp: -step,+ End: maxHeightPx - displayHeightPx,+ Home: minHeightPx - displayHeightPx,+ };+ const heightChange = heightChanges[event.key];++ if (heightChange === undefined) {+ return;+ }++ event.preventDefault();+ commitHeight(displayHeightPx + heightChange);+ };++ return (+ <Box position="relative">+ <Textarea ref={textareaRef} h={`${displayHeightPx}px`} resize="none" {...textareaProps} />+ <Box+ aria-label={resizeHandleAriaLabel}+ aria-orientation="horizontal"+ aria-valuemax={maxHeightPx}+ aria-valuemin={minHeightPx}+ aria-valuenow={displayHeightPx}+ bottom="0"+ cursor="ns-resize"+ h="2"+ left="0"+ position="absolute"+ right="0"+ role="separator"+ tabIndex={0}+ transition="background var(--wb-motion-duration-fast) ease, opacity var(--wb-motion-duration-fast) ease"+ _after={{+ bg: 'border.emphasized',+ borderRadius: 'full',+ bottom: '1px',+ content: '""',+ h: '1px',+ left: '25%',+ opacity: 0.55,+ position: 'absolute',+ right: '25%',+ }}+ _focusVisible={{ bg: 'accent.solid/20', outline: '2px solid {colors.accent.solid}' }}+ _hover={{ bg: 'accent.solid/12' }}+ onKeyDown={handleKeyDown}+ onPointerDown={handlePointerDown}+ />+ </Box>+ );+};diff --git a/invokeai/frontend/webv2/src/workbench/components/ui/index.ts b/invokeai/frontend/webv2/src/workbench/components/ui/index.tsindex db4d63c248..48af2a50f7 100644--- a/invokeai/frontend/webv2/src/workbench/components/ui/index.ts+++ b/invokeai/frontend/webv2/src/workbench/components/ui/index.ts@@ -5,6 +5,7 @@ export * from './JsonPreview';export * from './Menu';export * from './Panel';export * from './RenameDialog';+export * from './ResizableTextarea';export * from './Row';export * from './Scrollable';export * from './Tabs';diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts b/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.tsindex 5e56626694..efa69c05b5 100644--- a/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts@@ -6,7 +6,7 @@ import { isInvocationRouteValid, resolveInvocationRoute } from '@workbench/invocimport { ensureModelsLoaded, useModelsSnapshot } from '@workbench/models/modelsStore';import { openWidgetPlacement } from '@workbench/widgetPlacementCommands';import { getWidgetsForRegion } from '@workbench/widgetRegistry';-import { focusPositivePrompt } from '@workbench/widgets/generate/promptFocus';+import { focusPositivePrompt } from '@workbench/widgets/generate/promptFields';import { getProjectWidgetValues } from '@workbench/widgetState';import { useActiveProject, useWorkbenchDispatch } from '@workbench/WorkbenchContext';import { useInvocationTemplatesSnapshot } from '@workbench/workflows/templates';diff --git a/invokeai/frontend/webv2/src/workbench/widgets/generate/GeneratePromptFields.tsx b/invokeai/frontend/webv2/src/workbench/widgets/generate/GeneratePromptFields.tsxdeleted file mode 100644index 2bd01b88fb..0000000000--- a/invokeai/frontend/webv2/src/workbench/widgets/generate/GeneratePromptFields.tsx+++ /dev/null@@ -1,69 +0,0 @@-import type { GenerateModelConfig, GenerateSettings } from '@workbench/generation/types';-import type { ChangeEvent } from 'react';--import { Stack, Textarea } from '@chakra-ui/react';-import { Field } from '@workbench/components/ui';-import { getPromptPolicy } from '@workbench/generation/baseGenerationPolicies';--import { registerPositivePromptElement } from './promptFocus';--interface GeneratePromptFieldsProps {- settings: GenerateSettings;- selectedModel: GenerateModelConfig | undefined;- onCommit: (patch: Partial<GenerateSettings>) => void;-}--interface PromptFieldProps {- helpText?: string;- value: string;- onChange: (value: string) => void;-}--const PositivePromptField = ({ onChange, value }: PromptFieldProps) => (- <Field label="Prompt">- <Textarea- aria-label="Positive prompt"- ref={registerPositivePromptElement}- minH="6rem"- resize="vertical"- size="xs"- fontFamily="mono"- value={value}- onChange={(event: ChangeEvent<HTMLTextAreaElement>) => onChange(event.currentTarget.value)}- />- </Field>-);--const NegativePromptField = ({ helpText, onChange, value }: PromptFieldProps) => (- <Field label="Negative prompt" helpText={helpText}>- <Textarea- aria-label="Negative prompt"- minH="3.5rem"- resize="vertical"- size="xs"- fontFamily="mono"- value={value}- onChange={(event: ChangeEvent<HTMLTextAreaElement>) => onChange(event.currentTarget.value)}- />- </Field>-);--export const GeneratePromptFields = ({ onCommit, selectedModel, settings }: GeneratePromptFieldsProps) => {- const promptPolicy = getPromptPolicy(selectedModel, settings);-- return (- <Stack gap="1">- <PositivePromptField- value={settings.positivePrompt}- onChange={(positivePrompt) => onCommit({ positivePrompt })}- />- {promptPolicy.negativeVisible ? (- <NegativePromptField- helpText={promptPolicy.negativeHelpText}- value={settings.negativePrompt}- onChange={(negativePrompt) => onCommit({ negativePrompt })}- />- ) : null}- </Stack>- );-};diff --git a/invokeai/frontend/webv2/src/workbench/widgets/generate/GenerateSettingsForm.tsx b/invokeai/frontend/webv2/src/workbench/widgets/generate/GenerateSettingsForm.tsxindex 38e5abb416..2cb5fab4e9 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/generate/GenerateSettingsForm.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/generate/GenerateSettingsForm.tsx@@ -12,7 +12,7 @@ import { GenerateComponentsSection } from './GenerateComponentsSection';import { GenerateConceptsSection } from './GenerateConceptsSection';import { GenerateDimensionFields } from './GenerateDimensionFields';import { GenerateModelFields } from './GenerateModelFields';-import { GeneratePromptFields } from './GeneratePromptFields';+import { GeneratePromptFields } from './promptFields';interface GenerateSettingsFormProps {isLoadingModels: boolean;diff --git a/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/GeneratePromptFields.tsx b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/GeneratePromptFields.tsxnew file mode 100644index 0000000000..2496c12df0--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/GeneratePromptFields.tsx@@ -0,0 +1,33 @@+import type { GenerateModelConfig, GenerateSettings } from '@workbench/generation/types';++import { Stack } from '@chakra-ui/react';+import { getPromptPolicy } from '@workbench/generation/baseGenerationPolicies';++import { NegativePromptField } from './NegativePromptField';+import { PositivePromptField } from './PositivePromptField';++interface GeneratePromptFieldsProps {+ settings: GenerateSettings;+ selectedModel: GenerateModelConfig | undefined;+ onCommit: (patch: Partial<GenerateSettings>) => void;+}++export const GeneratePromptFields = ({ onCommit, selectedModel, settings }: GeneratePromptFieldsProps) => {+ const promptPolicy = getPromptPolicy(selectedModel, settings);++ return (+ <Stack gap="1" pt="2">+ <PositivePromptField+ value={settings.positivePrompt}+ onChange={(positivePrompt) => onCommit({ positivePrompt })}+ />+ {promptPolicy.negativeVisible ? (+ <NegativePromptField+ helpText={promptPolicy.negativeHelpText}+ value={settings.negativePrompt}+ onChange={(negativePrompt) => onCommit({ negativePrompt })}+ />+ ) : null}+ </Stack>+ );+};diff --git a/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/NegativePromptField.tsx b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/NegativePromptField.tsxnew file mode 100644index 0000000000..f54b6c0c03--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/NegativePromptField.tsx@@ -0,0 +1,25 @@+import type { ChangeEvent } from 'react';++import { Field, ResizableTextarea } from '@workbench/components/ui';++interface NegativePromptFieldProps {+ helpText?: string;+ value: string;+ onChange: (value: string) => void;+}++export const NegativePromptField = ({ helpText, onChange, value }: NegativePromptFieldProps) => (+ <Field label="Negative prompt" helpText={helpText}>+ <ResizableTextarea+ aria-label="Negative prompt"+ defaultHeightPx={56}+ maxHeightPx={240}+ minHeightPx={56}+ resizeHandleAriaLabel="Resize negative prompt"+ size="xs"+ fontFamily="mono"+ value={value}+ onChange={(event: ChangeEvent<HTMLTextAreaElement>) => onChange(event.currentTarget.value)}+ />+ </Field>+);diff --git a/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/PositivePromptField.tsx b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/PositivePromptField.tsxnew file mode 100644index 0000000000..5bbf99a02f--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/PositivePromptField.tsx@@ -0,0 +1,27 @@+import type { ChangeEvent } from 'react';++import { Field, ResizableTextarea } from '@workbench/components/ui';++import { registerPositivePromptElement } from './promptFocus';++interface PositivePromptFieldProps {+ value: string;+ onChange: (value: string) => void;+}++export const PositivePromptField = ({ onChange, value }: PositivePromptFieldProps) => (+ <Field label="Prompt">+ <ResizableTextarea+ aria-label="Positive prompt"+ defaultHeightPx={96}+ maxHeightPx={360}+ minHeightPx={96}+ resizeHandleAriaLabel="Resize positive prompt"+ size="xs"+ fontFamily="mono"+ textareaRef={registerPositivePromptElement}+ value={value}+ onChange={(event: ChangeEvent<HTMLTextAreaElement>) => onChange(event.currentTarget.value)}+ />+ </Field>+);diff --git a/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/index.ts b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/index.tsnew file mode 100644index 0000000000..7240b6c01a--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/index.ts@@ -0,0 +1,2 @@+export { GeneratePromptFields } from './GeneratePromptFields';+export { focusPositivePrompt } from './promptFocus';diff --git a/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFocus.ts b/invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/promptFocus.tssimilarity index 100%rename from invokeai/frontend/webv2/src/workbench/widgets/generate/promptFocus.tsrename to invokeai/frontend/webv2/src/workbench/widgets/generate/promptFields/promptFocus.ts