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): share the prompt field and catalog seams

joshistoast48fae7fa1cb0
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.tsx
index 7c87b00da9..1133052fe7 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/NegativePromptField.tsx
@@ -7,7 +7,7 @@ import { getPromptTemplateChunks } from '@features/generation/core/promptTemplat
import { useRegisterGenerateDraftFlusher } from '@features/generation/ui/generateDraftRegistry';
import { useDebouncedDraftValue } from '@features/generation/ui/useDebouncedDraftValue';
import { Field } from '@platform/ui';
-import { useCallback, useId, useMemo, useRef, useState } from 'react';
+import { useCallback, useId, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { AddPromptTriggerButton, PromptTriggerPopover } from './PositivePromptActions';
@@ -16,6 +16,7 @@ import { insertPromptText } from './promptFocus';
import { promptHistoryNavigation } from './promptHistoryNavigation';
import { PromptTextarea } from './PromptTextarea';
import { usePromptTriggerAutocomplete } from './usePromptTriggerAutocomplete';
+import { usePromptTriggerPicker } from './usePromptTriggerPicker';
const PROMPT_INPUT_DEBOUNCE_MS = 250;
@@ -38,9 +39,6 @@ interface NegativePromptFieldProps {
onResizeEnd: (heightPx: number) => void;
}
-/** The `+` button's browsable list, anchored to the button that opened it. */
-type PromptTriggerPickerState = { anchorRect: { height: number; width: number; x: number; y: number } };
-
/** No `__name__` here: a negative prompt is never expanded, so it has no wildcards. */
const NEGATIVE_PROMPT_TRIGGER_KEYS = ['<'] as const;
@@ -62,7 +60,6 @@ export const NegativePromptField = ({
}: NegativePromptFieldProps) => {
const { t } = useTranslation();
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
- const [triggerPickerState, setTriggerPickerState] = useState<PromptTriggerPickerState | null>(null);
const { draftValue, flushDraftValue, setDraftValue } = useDebouncedDraftValue({
delayMs: PROMPT_INPUT_DEBOUNCE_MS,
onCommit: onChange,
@@ -80,12 +77,6 @@ export const NegativePromptField = ({
[setDraftValue]
);
- const openPromptTriggerPicker = useCallback((anchorElement: HTMLElement) => {
- const rect = anchorElement.getBoundingClientRect();
-
- setTriggerPickerState({ anchorRect: { height: rect.height, width: rect.width, x: rect.x, y: rect.y } });
- }, []);
-
// Same textarea, read-only, as on the positive side — swapping in a different
// component would reset the resizer's mounted height.
//
@@ -106,20 +97,7 @@ export const NegativePromptField = ({
selectedModel,
});
- const handlePromptKeyDown = useCallback(
- (event: KeyboardEvent<HTMLTextAreaElement>) => {
- if (event.altKey || event.ctrlKey || event.metaKey) {
- return;
- }
-
- autocomplete.handleKeyDown(event);
- },
- [autocomplete]
- );
-
- const closePromptTriggerPicker = useCallback(() => setTriggerPickerState(null), []);
-
- const selectPromptTrigger = useCallback(
+ const insertTrigger = useCallback(
(trigger: string) => {
insertPromptText({
onChange: commitPromptChange,
@@ -127,14 +105,20 @@ export const NegativePromptField = ({
text: trigger,
value: draftValue,
});
- closePromptTriggerPicker();
},
- [closePromptTriggerPicker, commitPromptChange, draftValue]
+ [commitPromptChange, draftValue]
);
+ const triggerPicker = usePromptTriggerPicker({ insert: insertTrigger });
- const handleOpenPromptTriggerPicker = useCallback(
- (anchorElement: HTMLElement) => openPromptTriggerPicker(anchorElement),
- [openPromptTriggerPicker]
+ const handlePromptKeyDown = useCallback(
+ (event: KeyboardEvent<HTMLTextAreaElement>) => {
+ if (event.altKey || event.ctrlKey || event.metaKey) {
+ return;
+ }
+
+ autocomplete.handleKeyDown(event);
+ },
+ [autocomplete]
);
const handleEnabledChange = useCallback(
@@ -165,8 +149,8 @@ export const NegativePromptField = ({
<HStack gap="0.5">
{isEnabled ? (
<AddPromptTriggerButton
- isOpen={triggerPickerState !== null || isTemplateViewMode}
- onOpenPromptTriggerPicker={handleOpenPromptTriggerPicker}
+ isOpen={triggerPicker.isOpen || isTemplateViewMode}
+ onOpenPromptTriggerPicker={triggerPicker.open}
/>
) : null}
<Switch.Root
@@ -183,20 +167,7 @@ export const NegativePromptField = ({
</Switch.Root>
</HStack>
),
- [
- enableSwitchInputId,
- handleEnabledChange,
- handleOpenPromptTriggerPicker,
- isEnabled,
- isTemplateViewMode,
- t,
- triggerPickerState,
- ]
- );
-
- const triggerPickerPositioning = useMemo(
- () => ({ getAnchorRect: () => triggerPickerState?.anchorRect ?? null }),
- [triggerPickerState]
+ [enableSwitchInputId, handleEnabledChange, isEnabled, isTemplateViewMode, t, triggerPicker]
);
const templateChunks = useMemo(
@@ -231,14 +202,14 @@ export const NegativePromptField = ({
onResizeEnd={onResizeEnd}
/>
{autocomplete.element}
- {triggerPickerState ? (
+ {triggerPicker.isOpen ? (
<PromptTriggerPopover
loras={loras}
open
- positioning={triggerPickerPositioning}
+ positioning={triggerPicker.positioning}
selectedModel={selectedModel}
- onClose={closePromptTriggerPicker}
- onSelect={selectPromptTrigger}
+ onClose={triggerPicker.close}
+ onSelect={triggerPicker.select}
/>
) : null}
</>
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.tsx
index e01fff30fa..df893392fe 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PositivePromptField.tsx
@@ -8,7 +8,7 @@ import { useRegisterGenerateDraftFlusher } from '@features/generation/ui/generat
import { useDebouncedDraftValue } from '@features/generation/ui/useDebouncedDraftValue';
import { useWildcards } from '@features/generation/ui/useWildcards';
import { Field } from '@platform/ui';
-import { useCallback, useMemo, useRef, useState } from 'react';
+import { useCallback, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import type { DynamicPromptsFieldConfig } from './DynamicPromptsPanel';
@@ -19,6 +19,7 @@ import { insertPromptText, registerPositivePromptElement } from './promptFocus';
import { promptHistoryNavigation } from './promptHistoryNavigation';
import { PromptTextarea } from './PromptTextarea';
import { usePromptTriggerAutocomplete } from './usePromptTriggerAutocomplete';
+import { usePromptTriggerPicker } from './usePromptTriggerPicker';
const PROMPT_INPUT_DEBOUNCE_MS = 250;
@@ -49,9 +50,6 @@ interface PositivePromptFieldProps {
onUsePrompt: (prompt: PromptHistoryItem) => void;
}
-/** The `+` button's browsable list, anchored to the button that opened it. */
-type PromptTriggerPickerState = { anchorRect: { height: number; width: number; x: number; y: number } };
-
/** The positive prompt is the only field whose `__name__` references resolve. */
const POSITIVE_PROMPT_TRIGGER_KEYS = ['<', '_'] as const;
@@ -75,7 +73,6 @@ export const PositivePromptField = ({
}: PositivePromptFieldProps) => {
const { t } = useTranslation();
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
- const [triggerPickerState, setTriggerPickerState] = useState<PromptTriggerPickerState | null>(null);
const { knownNames: knownWildcards } = useWildcards();
const { commitDraftValue, draftValue, flushDraftValue, replaceDraftValue, setDraftValue } = useDebouncedDraftValue({
delayMs: PROMPT_INPUT_DEBOUNCE_MS,
@@ -102,12 +99,6 @@ export const PositivePromptField = ({
[commitDraftValue]
);
- const openPromptTriggerPicker = useCallback((anchorElement: HTMLElement) => {
- const rect = anchorElement.getBoundingClientRect();
-
- setTriggerPickerState({ anchorRect: { height: rect.height, width: rect.width, x: rect.x, y: rect.y } });
- }, []);
-
// View mode keeps the same textarea rather than swapping in a preview
// component: `ResizableTextarea` reads its height once on mount, and the
// element is also what `focusPositivePrompt` and the attention hotkeys target,
@@ -126,6 +117,19 @@ export const PositivePromptField = ({
selectedModel,
});
+ const insertTrigger = useCallback(
+ (trigger: string) => {
+ insertPromptText({
+ onChange: commitPromptChange,
+ textarea: textareaRef.current,
+ text: trigger,
+ value: draftValue,
+ });
+ },
+ [commitPromptChange, draftValue]
+ );
+ const triggerPicker = usePromptTriggerPicker({ insert: insertTrigger });
+
const handlePromptKeyDown = useCallback(
(event: KeyboardEvent<HTMLTextAreaElement>) => {
if (event.altKey || event.ctrlKey || event.metaKey) {
@@ -139,21 +143,6 @@ export const PositivePromptField = ({
[autocomplete]
);
- const closePromptTriggerPicker = useCallback(() => setTriggerPickerState(null), []);
-
- const selectPromptTrigger = useCallback(
- (trigger: string) => {
- insertPromptText({
- onChange: commitPromptChange,
- textarea: textareaRef.current,
- text: trigger,
- value: draftValue,
- });
- closePromptTriggerPicker();
- },
- [closePromptTriggerPicker, commitPromptChange, draftValue]
- );
-
const handleUsePrompt = useCallback(
(prompt: PromptHistoryItem) => {
replaceDraftValue(prompt.positivePrompt);
@@ -167,11 +156,6 @@ export const PositivePromptField = ({
registerPositivePromptElement(element);
}, []);
- const handleOpenPromptTriggerPicker = useCallback(
- (anchorElement: HTMLElement) => openPromptTriggerPicker(anchorElement),
- [openPromptTriggerPicker]
- );
-
const insertTextAtCaret = useCallback(
(text: string) => {
insertPromptText({ onChange: commitPromptChange, textarea: textareaRef.current, text, value: draftValue });
@@ -201,7 +185,7 @@ export const PositivePromptField = ({
<PositivePromptActions
batchCount={batchCount}
dynamicPrompts={dynamicPrompts}
- isPromptTriggerPickerOpen={triggerPickerState !== null}
+ isPromptTriggerPickerOpen={triggerPicker.isOpen}
showSyntaxHighlighting={showSyntaxHighlighting}
onInsertText={insertTextAtCaret}
loras={loras}
@@ -215,7 +199,7 @@ export const PositivePromptField = ({
onFlattenPromptTemplate={flattenPromptTemplate}
projectId={projectId}
selectedModel={selectedModel}
- onOpenPromptTriggerPicker={handleOpenPromptTriggerPicker}
+ onOpenPromptTriggerPicker={triggerPicker.open}
onPositivePromptChangeImmediate={commitPromptChangeImmediately}
onUsePrompt={handleUsePrompt}
/>
@@ -227,7 +211,6 @@ export const PositivePromptField = ({
dynamicPrompts,
effectivePositivePrompt,
flattenPromptTemplate,
- handleOpenPromptTriggerPicker,
handleUsePrompt,
insertTextAtCaret,
isTemplateViewMode,
@@ -238,7 +221,7 @@ export const PositivePromptField = ({
promptTemplate,
selectedModel,
showSyntaxHighlighting,
- triggerPickerState,
+ triggerPicker,
]
);
@@ -261,11 +244,6 @@ export const PositivePromptField = ({
/** Clicking the merged text is the way back to editing, as it is in legacy. */
const exitViewMode = useCallback(() => onTemplateViewModeChange?.(false), [onTemplateViewModeChange]);
- const triggerPickerPositioning = useMemo(
- () => ({ getAnchorRect: () => triggerPickerState?.anchorRect ?? null }),
- [triggerPickerState]
- );
-
return (
<Field label={t('common.prompt')} labelEnd={labelEnd}>
<PromptTextarea
@@ -292,14 +270,14 @@ export const PositivePromptField = ({
onResizeEnd={onResizeEnd}
/>
{autocomplete.element}
- {triggerPickerState ? (
+ {triggerPicker.isOpen ? (
<PromptTriggerPopover
loras={loras}
open
- positioning={triggerPickerPositioning}
+ positioning={triggerPicker.positioning}
selectedModel={selectedModel}
- onClose={closePromptTriggerPicker}
- onSelect={selectPromptTrigger}
+ onClose={triggerPicker.close}
+ onSelect={triggerPicker.select}
/>
) : null}
</Field>
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerPicker.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerPicker.ts
new file mode 100644
index 0000000000..c0a1db2083
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerPicker.ts
@@ -0,0 +1,59 @@
+/**
+ * The `+` button's browsable trigger popover.
+ *
+ * Distinct from the caret autocomplete beside it: this one is opened
+ * deliberately, takes focus, and browses every trigger there is. Both prompt
+ * fields wire it up identically, so it lives here rather than twice.
+ *
+ * The popover is positioned by rect rather than by element so it can anchor to
+ * the button that opened it, wherever that sits, without the field having to
+ * hand its own DOM to a child.
+ */
+
+import type { PromptTextRange } from '@features/generation/ui/promptFields/promptFocus';
+
+import { useCallback, useMemo, useState } from 'react';
+
+interface AnchorRect {
+ height: number;
+ width: number;
+ x: number;
+ y: number;
+}
+
+export interface PromptTriggerPickerApi {
+ isOpen: boolean;
+ positioning: { getAnchorRect: () => AnchorRect | null };
+ close: () => void;
+ open: (anchorElement: HTMLElement) => void;
+ /** Inserts the picked trigger, then closes. */
+ select: (trigger: string) => void;
+}
+
+export const usePromptTriggerPicker = ({
+ insert,
+}: {
+ insert: (trigger: string, range?: PromptTextRange) => void;
+}): PromptTriggerPickerApi => {
+ const [anchorRect, setAnchorRect] = useState<AnchorRect | null>(null);
+
+ const open = useCallback((anchorElement: HTMLElement) => {
+ const rect = anchorElement.getBoundingClientRect();
+
+ setAnchorRect({ height: rect.height, width: rect.width, x: rect.x, y: rect.y });
+ }, []);
+
+ const close = useCallback(() => setAnchorRect(null), []);
+
+ const select = useCallback(
+ (trigger: string) => {
+ insert(trigger);
+ setAnchorRect(null);
+ },
+ [insert]
+ );
+
+ const positioning = useMemo(() => ({ getAnchorRect: () => anchorRect }), [anchorRect]);
+
+ return { close, isOpen: anchorRect !== null, open, positioning, select };
+};
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/useWildcards.ts b/invokeai/frontend/webv2/src/features/generation/ui/useWildcards.ts
index ec23db0fa3..62d58fa23e 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/useWildcards.ts
+++ b/invokeai/frontend/webv2/src/features/generation/ui/useWildcards.ts
@@ -38,39 +38,31 @@ export const useWildcards = (): WildcardCatalog => {
);
// Each mutation captures the identity that started it, so a sign-out mid-flight
- // does not invalidate the next account's caches on the way back.
- const create = useCallback(
- async (wildcard: { name: string; values: string[] }) => {
+ // does not invalidate the next account's caches on the way back. Stated once,
+ // as `usePromptTemplates` does — three copies of it was three chances for the
+ // next mutation added here to forget a step.
+ const runAndInvalidate = useCallback(
+ async (run: () => Promise<unknown>): Promise<void> => {
const owner = captureAccountScope();
- await createWildcard(wildcard);
+ await run();
assertAccountScopeCurrent(owner);
await invalidateWildcardDependents(queryClient);
},
[queryClient]
);
- const update = useCallback(
- async (id: string, changes: { name?: string; values?: string[] }) => {
- const owner = captureAccountScope();
-
- await updateWildcard(id, changes);
- assertAccountScopeCurrent(owner);
- await invalidateWildcardDependents(queryClient);
- },
- [queryClient]
+ const create = useCallback(
+ (wildcard: { name: string; values: string[] }) => runAndInvalidate(() => createWildcard(wildcard)),
+ [runAndInvalidate]
);
- const remove = useCallback(
- async (id: string) => {
- const owner = captureAccountScope();
-
- await deleteWildcard(id);
- assertAccountScopeCurrent(owner);
- await invalidateWildcardDependents(queryClient);
- },
- [queryClient]
+ const update = useCallback(
+ (id: string, changes: { name?: string; values?: string[] }) => runAndInvalidate(() => updateWildcard(id, changes)),
+ [runAndInvalidate]
);
+ const remove = useCallback((id: string) => runAndInvalidate(() => deleteWildcard(id)), [runAndInvalidate]);
+
return { create, isLoading: query.isPending, knownNames, remove, update, wildcards };
};