Skip to content

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.

refactor(webv2): give form elements one name and icon each

joshistoastf4d5a52384a4
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/linear/FormBuilderTab.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/linear/FormBuilderTab.tsx
index 50a1f8ceb2..6d2ff3fca2 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/linear/FormBuilderTab.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/linear/FormBuilderTab.tsx
@@ -34,18 +34,7 @@ import { useProjectGraphCommands } from '@features/workflow/ui/useProjectGraphCo
import { useWorkflowHostCommands } from '@features/workflow/ui/WorkflowUiContext';
import { getFormChildren, getResolvedWorkflowEdges, getWorkflowFieldInvalidReason } from '@features/workflow/utility';
import { Button, DropZone, IconButton } from '@platform/ui';
-import {
- Columns2Icon,
- CrosshairIcon,
- GripVerticalIcon,
- HeadingIcon,
- InfoIcon,
- MinusIcon,
- PlusIcon,
- Rows2Icon,
- TextIcon,
- XIcon,
-} from 'lucide-react';
+import { Columns2Icon, CrosshairIcon, GripVerticalIcon, InfoIcon, PlusIcon, Rows2Icon, XIcon } from 'lucide-react';
import {
createContext,
memo,
@@ -68,6 +57,12 @@ import {
resolveFormDrop,
type FormDropTarget,
} from './formBuilderDnd';
+import {
+ ADDABLE_FORM_ELEMENT_KEYS,
+ FORM_ELEMENT_META,
+ getFormElementTitle,
+ type AddableFormElementKey,
+} from './formElementMeta';
import { NodeFieldControl, useNodeFieldBinding } from './NodeFieldControl';
/**
@@ -102,22 +97,6 @@ const BuilderDndContext = createContext<BuilderDndContextValue>({
*/
const BuilderDropTargetContext = createContext<FormDropTarget | null>(null);
-/** Title shown in a card's title bar and the drag ghost. Shared so the two never drift. */
-const getFormElementTitle = (element: WorkflowFormElement): string => {
- switch (element.type) {
- case 'container':
- return `Container (${element.data.layout} layout)`;
- case 'node-field':
- return 'Node Field';
- case 'heading':
- return 'Heading';
- case 'text':
- return 'Text';
- case 'divider':
- return 'Divider';
- }
-};
-
/** The dragged card's `DragOverlay` ghost: a compact title bar following the pointer. */
const BuilderDragGhost = ({ element }: { element: WorkflowFormElement }) => (
<HStack
@@ -506,8 +485,12 @@ const BuilderElement = memo(BuilderElementBase);
const AddElementMenu = () => {
const { editGraph } = useProjectGraphCommands();
- const add = (elementType: 'divider' | 'heading' | 'text' | 'container', layout?: 'row' | 'column') =>
- editGraph({ elementType, layout, type: 'addFormElement' });
+ const add = (key: AddableFormElementKey) =>
+ editGraph(
+ key === 'container-column' || key === 'container-row'
+ ? { elementType: 'container', layout: key === 'container-row' ? 'row' : 'column', type: 'addFormElement' }
+ : { elementType: key, type: 'addFormElement' }
+ );
return (
<Menu.Root positioning={{ placement: 'bottom-start' }}>
@@ -520,26 +503,12 @@ const AddElementMenu = () => {
<Portal>
<Menu.Positioner>
<Menu.Content minW="11rem">
- <Menu.Item value="heading" onClick={() => add('heading')}>
- <Icon as={HeadingIcon} boxSize="3" />
- Heading
- </Menu.Item>
- <Menu.Item value="text" onClick={() => add('text')}>
- <Icon as={TextIcon} boxSize="3" />
- Text
- </Menu.Item>
- <Menu.Item value="divider" onClick={() => add('divider')}>
- <Icon as={MinusIcon} boxSize="3" />
- Divider
- </Menu.Item>
- <Menu.Item value="container-column" onClick={() => add('container', 'column')}>
- <Icon as={Columns2Icon} boxSize="3" />
- Container (column)
- </Menu.Item>
- <Menu.Item value="container-row" onClick={() => add('container', 'row')}>
- <Icon as={Rows2Icon} boxSize="3" />
- Container (row)
- </Menu.Item>
+ {ADDABLE_FORM_ELEMENT_KEYS.map((key) => (
+ <Menu.Item key={key} value={key} onClick={() => add(key)}>
+ <Icon as={FORM_ELEMENT_META[key].icon} boxSize="3" />
+ {FORM_ELEMENT_META[key].label}
+ </Menu.Item>
+ ))}
</Menu.Content>
</Menu.Positioner>
</Portal>
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/linear/WorkflowLinearPanel.browser.test.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/linear/WorkflowLinearPanel.browser.test.tsx
index 7a7b14ae4b..ba329da840 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/linear/WorkflowLinearPanel.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/linear/WorkflowLinearPanel.browser.test.tsx
@@ -290,7 +290,7 @@ describe('Form builder drag and drop (dnd-kit)', () => {
// The container's card content now holds the heading card, and the
// empty-state hint is gone.
- const containerContent = cardContentFor('Container (column layout)');
+ const containerContent = cardContentFor('Container (column)');
expect(containerContent.textContent).toContain('Heading');
expect(containerContent.textContent).not.toContain('Empty container');
@@ -304,7 +304,7 @@ describe('Form builder drag and drop (dnd-kit)', () => {
await dragTo('Divider', headingCardRect.left + headingCardRect.width / 2, headingCardRect.bottom - 2);
// The divider moved into the container, next to the heading.
- const containerContentAfter = cardContentFor('Container (column layout)');
+ const containerContentAfter = cardContentFor('Container (column)');
expect(containerContentAfter.textContent).toContain('Heading');
expect(containerContentAfter.textContent).toContain('Divider');
@@ -339,7 +339,7 @@ describe('Form builder drag and drop (dnd-kit)', () => {
await dragToWithKeyboard('Heading', dropZoneRect.top + dropZoneRect.height / 2);
- const containerContent = cardContentFor('Container (column layout)');
+ const containerContent = cardContentFor('Container (column)');
expect(containerContent.textContent).toContain('Heading');
expect(containerContent.textContent).not.toContain('Empty container');
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/linear/formElementMeta.ts b/invokeai/frontend/webv2/src/features/workflow/ui/linear/formElementMeta.ts
new file mode 100644
index 0000000000..e66ed4b321
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/linear/formElementMeta.ts
@@ -0,0 +1,57 @@
+import type { WorkflowFormElement } from '@features/workflow/contracts';
+import type { LucideIcon } from 'lucide-react';
+
+import { Columns2Icon, CrosshairIcon, HeadingIcon, MinusIcon, Rows2Icon, TextIcon } from 'lucide-react';
+
+/**
+ * One name and one icon per form element kind.
+ *
+ * The builder shows an element's identity in three places — the card title bar,
+ * the drag ghost, and the Add menu — and they were each spelling it out
+ * separately, so the menu could offer "Container (column)" while the card it
+ * produced called itself something else. Naming a kind is one fact; it belongs
+ * in one table.
+ *
+ * Containers are keyed by layout rather than by type alone: a row and a column
+ * are the two things a person is actually choosing between, so they carry
+ * distinct names and distinct icons.
+ */
+export type FormElementMetaKey = 'container-column' | 'container-row' | 'divider' | 'heading' | 'node-field' | 'text';
+
+export interface FormElementMeta {
+ icon: LucideIcon;
+ label: string;
+}
+
+export const FORM_ELEMENT_META: Record<FormElementMetaKey, FormElementMeta> = {
+ 'container-column': { icon: Columns2Icon, label: 'Container (column)' },
+ 'container-row': { icon: Rows2Icon, label: 'Container (row)' },
+ divider: { icon: MinusIcon, label: 'Divider' },
+ heading: { icon: HeadingIcon, label: 'Heading' },
+ 'node-field': { icon: CrosshairIcon, label: 'Node Field' },
+ text: { icon: TextIcon, label: 'Text' },
+};
+
+/**
+ * The kinds the Add menu offers, in the order it offers them.
+ *
+ * `node-field` is absent by construction: a field enters a form by being
+ * dragged off the node that owns it, so there is nothing for a menu entry to
+ * create.
+ */
+export const ADDABLE_FORM_ELEMENT_KEYS = [
+ 'heading',
+ 'text',
+ 'divider',
+ 'container-column',
+ 'container-row',
+] as const satisfies readonly FormElementMetaKey[];
+
+export type AddableFormElementKey = (typeof ADDABLE_FORM_ELEMENT_KEYS)[number];
+
+export const getFormElementMetaKey = (element: WorkflowFormElement): FormElementMetaKey =>
+ element.type === 'container' ? (element.data.layout === 'row' ? 'container-row' : 'container-column') : element.type;
+
+/** Title shown in a card's title bar and the drag ghost. Shared so the two never drift. */
+export const getFormElementTitle = (element: WorkflowFormElement): string =>
+ FORM_ELEMENT_META[getFormElementMetaKey(element)].label;