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): stop layer rows claiming Enter from commands and portals
joshistoastc96cfed81158
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerListItem.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerListItem.tsxindex da0fb99412..fa58deed96 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerListItem.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/LayerListItem.tsx@@ -9,7 +9,7 @@ import { CSS } from '@dnd-kit/utilities';import { IconButton, Row, ToggleDot } from '@platform/ui';import { isHideableLayer, isLayerHidden } from '@workbench/canvas-engine/api';import { EyeIcon, EyeOffIcon, LockIcon, LockOpenIcon } from 'lucide-react';-import { useCallback, useMemo, useState } from 'react';+import { useCallback, useMemo, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';import { ControlLayerWarningIcon } from './ControlLayerWarningIcon';@@ -19,6 +19,7 @@ import {LayerContextMenu,type LayerContextMenuEngine,} from './LayerContextMenu';+import { shouldStartLayerKeyboardDrag } from './layerDndConfig';import { createLayerMenuTargetFromContextEvent } from './layerMenuState';import { applyStructural } from './layerOps';import { LayerPropertiesPopover, type LayerPropertiesEngine } from './LayerPropertiesPopover';@@ -75,6 +76,7 @@ export const LayerListItem = ({disabled: interaction.sortableDisabled,id: layer.id,});+ const rowRef = useRef<HTMLElement | null>(null);const [isEditing, setIsEditing] = useState(false);const [draftName, setDraftName] = useState(layer.name);const [contextMenuTarget, setContextMenuTarget] = useState<CanvasLayerContextMenuTarget | null>(null);@@ -160,11 +162,17 @@ export const LayerListItem = ({const handleNameKeyDown = useCallback((event: KeyboardEvent<HTMLInputElement>) => {- // Keep rename keystrokes from reaching canvas-level shortcuts.- event.stopPropagation();+ // Stop ONLY the two keys the rename owns. A blanket stop would also stop+ // the native event reaching the window, killing every global hotkey while+ // renaming; the hotkey runtime already refuses to fire non-`allowInEditable`+ // bindings for a focused input, so nothing else needs suppressing here.+ // Escape does need it: the engine's own window listener would otherwise+ // take it as a canvas deselect.if (event.key === 'Enter') {+ event.stopPropagation();commitName();} else if (event.key === 'Escape') {+ event.stopPropagation();setIsEditing(false);}},@@ -185,11 +193,43 @@ export const LayerListItem = ({const closeContextMenu = useCallback(() => setContextMenuTarget(null), []);+ // The row's own DOM node, so the drag activator can tell a keystroke made+ // inside it from one that bubbled in through the React tree from a portal.+ const setRowRef = useCallback(+ (node: HTMLElement | null) => {+ rowRef.current = node;+ setNodeRef(node);+ },+ [setNodeRef]+ );++ /**+ * dnd-kit starts a drag on Enter and inspects only the key code, so the+ * activator is gated here — see {@link shouldStartLayerKeyboardDrag}. Without+ * it, `mod+Enter` (Invoke) with a row focused starts an invisible drag that+ * leaves the row at drag opacity, reading as a disabled layer.+ */+ const sortableListeners = useMemo(() => {+ if (interaction.sortableDisabled || !listeners) {+ return {};+ }+ const { onKeyDown, ...rest } = listeners;+ return {+ ...rest,+ onKeyDown: (event: KeyboardEvent<HTMLElement>) => {+ if (!shouldStartLayerKeyboardDrag(event, rowRef.current)) {+ return;+ }+ onKeyDown?.(event);+ },+ };+ }, [interaction.sortableDisabled, listeners]);+return (- <Box ref={setNodeRef} style={dndStyle}>+ <Box ref={setRowRef} style={dndStyle}><Row{...(interaction.sortableDisabled ? {} : attributes)}- {...(interaction.sortableDisabled ? {} : listeners)}+ {...sortableListeners}active={isSelected ? 'muted' : undefined}cursor={isDragging ? 'grabbing' : 'default'}display="flex"diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/RegionalGuidanceSettings.tsx b/invokeai/frontend/webv2/src/workbench/widgets/layers/RegionalGuidanceSettings.tsxindex c97ca3bc38..d09021c232 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/RegionalGuidanceSettings.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/RegionalGuidanceSettings.tsx@@ -9,7 +9,7 @@ import type {RegionalGuidanceReferenceImageAsset,} from '@workbench/canvas-engine/api';import type { CanvasStructuralEngine } from '@workbench/widgets/layers/layerOps';-import type { ChangeEvent, CSSProperties, FocusEvent, KeyboardEvent } from 'react';+import type { ChangeEvent, CSSProperties, FocusEvent } from 'react';import { Box, createListCollection, HStack, IconButton, Input, Stack, Switch, Text } from '@chakra-ui/react';import { useDndMonitor, useDroppable } from '@dnd-kit/core';@@ -160,8 +160,6 @@ export const RegionalGuidanceSettings = ({ engine, layer }: RegionalGuidanceSett[]);- const stopKeyboardPropagation = useCallback((event: KeyboardEvent) => event.stopPropagation(), []);-const handleAutoNegative = useCallback((details: { checked: boolean }) => {commitConfig(@@ -338,7 +336,7 @@ export const RegionalGuidanceSettings = ({ engine, layer }: RegionalGuidanceSettconst styleValue = useMemo(() => [fill.style], [fill.style]);return (- <Stack gap="2" onKeyDown={stopKeyboardPropagation}>+ <Stack gap="2">{isFlux2 && (<Text color="fg.warning" fontSize="xs" role="alert">{t('widgets.layers.regionalGuidance.flux2PositiveOnly')}diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerDndConfig.browser.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerDndConfig.browser.test.tsnew file mode 100644index 0000000000..ded3a08b23--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerDndConfig.browser.test.ts@@ -0,0 +1,60 @@+import { describe, expect, it } from 'vitest';++import { shouldStartLayerKeyboardDrag } from './layerDndConfig';++const keyEvent = (target: EventTarget | null) => ({+ altKey: false,+ ctrlKey: false,+ metaKey: false,+ shiftKey: false,+ target,+});++/** A layer row containing a button, plus a portalled panel outside it. */+const buildTree = () => {+ const row = document.createElement('div');+ const button = document.createElement('button');+ const input = document.createElement('input');+ row.append(button, input);++ const portal = document.createElement('div');+ const portalTextarea = document.createElement('textarea');+ portal.append(portalTextarea);++ document.body.append(row, portal);+ return { button, input, portal, portalTextarea, row };+};++describe('shouldStartLayerKeyboardDrag — DOM rules', () => {+ it('allows a plain Enter on the row itself', () => {+ const { row } = buildTree();+ expect(shouldStartLayerKeyboardDrag(keyEvent(row), row)).toBe(true);+ });++ it('allows a plain Enter on a non-editable child of the row', () => {+ const { button, row } = buildTree();+ expect(shouldStartLayerKeyboardDrag(keyEvent(button), row)).toBe(true);+ });++ it('refuses Enter typed in the row own rename input', () => {+ const { input, row } = buildTree();+ expect(shouldStartLayerKeyboardDrag(keyEvent(input), row)).toBe(false);+ });++ it('refuses Enter from a PORTALLED textarea that bubbled in through the React tree', () => {+ // The layer-properties popover renders through a Portal, so its DOM node is+ // not inside the row even though React events reach the row.+ const { portalTextarea, row } = buildTree();+ expect(shouldStartLayerKeyboardDrag(keyEvent(portalTextarea), row)).toBe(false);+ });++ it('refuses Enter from any node outside the row, editable or not', () => {+ const { portal, row } = buildTree();+ expect(shouldStartLayerKeyboardDrag(keyEvent(portal), row)).toBe(false);+ });++ it('refuses a modified Enter even on the row itself', () => {+ const { row } = buildTree();+ expect(shouldStartLayerKeyboardDrag({ ...keyEvent(row), ctrlKey: true }, row)).toBe(false);+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerDndConfig.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerDndConfig.tsindex f6cec3219e..1d2bc328fd 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/layerDndConfig.ts+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layerDndConfig.ts@@ -10,3 +10,53 @@ export const LAYER_KEYBOARD_SENSOR_OPTIONS = {start: ['Enter'],},} satisfies KeyboardSensorOptions;++/** The shape of a keyboard event this guard inspects (DOM or React synthetic). */+export interface LayerDragKeyEvent {+ target: EventTarget | null;+ altKey: boolean;+ ctrlKey: boolean;+ metaKey: boolean;+ shiftKey: boolean;+}++/**+ * Whether a key event may start a keyboard drag of a layer row.+ *+ * dnd-kit claims `Enter` to start a drag and inspects only `event.code`, and the+ * whole row is the drag handle (no separate activator node, so dnd-kit's own+ * `event.target !== activator` guard never engages). That let two unrelated+ * Enter presses start a drag nobody asked for:+ *+ * - **A MODIFIED Enter.** `mod+Enter` is Invoke. Pressing it with a row focused+ * also began an invisible keyboard drag, leaving the row stuck at drag opacity+ * — which reads as the layer having been disabled, though nothing about the+ * layer changed.+ * - **An Enter from a PORTALLED descendant.** React events bubble through the+ * component tree, not the DOM, so keystrokes in the layer-properties popover+ * (portalled to the body, and holding multi-line prompt textareas where Enter+ * is a newline) reached the row. The popover defended itself by stopping+ * propagation wholesale, which also stopped the event reaching the window —+ * killing every global hotkey inside it, including the prompt-attention keys.+ *+ * Guarding the activator fixes both at the source, so no portalled panel has to+ * remember to stop propagation — and none of them has to break global hotkeys to+ * do it.+ */+export const shouldStartLayerKeyboardDrag = (event: LayerDragKeyEvent, rowNode: HTMLElement | null): boolean => {+ // An unmodified Enter is the drag gesture; every modified one belongs to a+ // command (Invoke, and anything a user rebinds onto mod+Enter).+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {+ return false;+ }+ if (typeof HTMLElement === 'undefined' || !(event.target instanceof HTMLElement)) {+ return true;+ }+ // Typing Enter in a field is never a request to reorder layers.+ if (event.target.closest('input, textarea, select, [contenteditable="true"]')) {+ return false;+ }+ // Portal bleed: the event reached this row through the React tree, but its+ // DOM node lives elsewhere.+ return !rowNode || rowNode.contains(event.target);+};diff --git a/invokeai/frontend/webv2/src/workbench/widgets/layers/layersDnd.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/layers/layersDnd.test.tsindex 6899e75085..25cf00f50f 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/layers/layersDnd.test.ts+++ b/invokeai/frontend/webv2/src/workbench/widgets/layers/layersDnd.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest';-import { LAYER_KEYBOARD_SENSOR_OPTIONS } from './layerDndConfig';+import { LAYER_KEYBOARD_SENSOR_OPTIONS, shouldStartLayerKeyboardDrag } from './layerDndConfig';import { moveItem } from './layersDnd';describe('layer keyboard drag configuration', () => {@@ -33,3 +33,38 @@ describe('moveItem', () => {expect(input).toEqual(['a', 'b', 'c']);});});++describe('shouldStartLayerKeyboardDrag', () => {+ const keyEvent = (+ overrides: Partial<{+ target: EventTarget | null;+ altKey: boolean;+ ctrlKey: boolean;+ metaKey: boolean;+ shiftKey: boolean;+ }> = {}+ ) => ({+ altKey: false,+ ctrlKey: false,+ metaKey: false,+ shiftKey: false,+ target: null,+ ...overrides,+ });++ it('allows a plain Enter with no DOM context (the node-test path)', () => {+ expect(shouldStartLayerKeyboardDrag(keyEvent(), null)).toBe(true);+ });++ it.each([['ctrlKey'], ['metaKey'], ['shiftKey'], ['altKey']] as const)(+ 'refuses a %s-modified Enter — those belong to commands, not reordering',+ (modifier) => {+ expect(shouldStartLayerKeyboardDrag(keyEvent({ [modifier]: true }), null)).toBe(false);+ }+ );++ it('refuses mod+Enter specifically, the Invoke binding that left rows stuck at drag opacity', () => {+ expect(shouldStartLayerKeyboardDrag(keyEvent({ ctrlKey: true }), null)).toBe(false);+ expect(shouldStartLayerKeyboardDrag(keyEvent({ metaKey: true }), null)).toBe(false);+ });+});