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.

Fix workflows info copy focus (#9015)

Jonathan41a542552e3a
diff --git a/Makefile b/Makefile
index f1e81429e7..2e452c5cc0 100644
--- a/Makefile
+++ b/Makefile
@@ -12,12 +12,12 @@ help:
@echo "mypy-all Run mypy ignoring the config in pyproject.tom but still ignoring missing imports"
@echo "test Run the unit tests."
@echo "update-config-docstring Update the app's config docstring so mkdocs can autogenerate it correctly."
- @echo "frontend-install Install the pnpm modules needed for the front end"
- @echo "frontend-build Build the frontend in order to run on localhost:9090"
+ @echo "frontend-install Install the pnpm modules needed for the frontend"
+ @echo "frontend-build Build the frontend for localhost:9090"
@echo "frontend-dev Run the frontend in developer mode on localhost:5173"
@echo "frontend-typegen Generate types for the frontend from the OpenAPI schema"
- @echo "frontend-prettier Format the frontend using lint:prettier"
- @echo "wheel Build the wheel for the current version"
+ @echo "frontend-lint Run frontend checks and fixable lint/format steps"
+ @echo "wheel Build the wheel for the current version"
@echo "tag-release Tag the GitHub repository with the current version (use at release time only!)"
@echo "openapi Generate the OpenAPI schema for the app, outputting to stdout"
@echo "docs Serve the mkdocs site with live reload"
diff --git a/invokeai/frontend/web/src/common/hooks/focus.test.ts b/invokeai/frontend/web/src/common/hooks/focus.test.ts
new file mode 100644
index 0000000000..c106fe1cec
--- /dev/null
+++ b/invokeai/frontend/web/src/common/hooks/focus.test.ts
@@ -0,0 +1,13 @@
+import { describe, expect, it } from 'vitest';
+
+import { getFocusedRegion, setFocusedRegion } from './focus';
+
+describe('focus regions', () => {
+ it('supports the workflows region', () => {
+ setFocusedRegion('workflows');
+ expect(getFocusedRegion()).toBe('workflows');
+
+ setFocusedRegion(null);
+ expect(getFocusedRegion()).toBe(null);
+ });
+});
diff --git a/invokeai/frontend/web/src/features/nodes/components/flow/Flow.tsx b/invokeai/frontend/web/src/features/nodes/components/flow/Flow.tsx
index f6474dec74..0c48eddfc6 100644
--- a/invokeai/frontend/web/src/features/nodes/components/flow/Flow.tsx
+++ b/invokeai/frontend/web/src/features/nodes/components/flow/Flow.tsx
@@ -51,7 +51,7 @@ import { selectSelectionMode, selectShouldSnapToGrid } from 'features/nodes/stor
import { NO_DRAG_CLASS, NO_PAN_CLASS, NO_WHEEL_CLASS } from 'features/nodes/types/constants';
import type { AnyEdge, AnyNode } from 'features/nodes/types/invocation';
import { useRegisteredHotkeys } from 'features/system/components/HotkeysModal/useHotkeyData';
-import type { CSSProperties, MouseEvent } from 'react';
+import type { CSSProperties, MouseEvent, RefObject } from 'react';
import { memo, useCallback, useMemo, useRef } from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
@@ -61,6 +61,7 @@ import InvocationDefaultEdge from './edges/InvocationDefaultEdge';
import CurrentImageNode from './nodes/CurrentImage/CurrentImageNode';
import InvocationNodeWrapper from './nodes/Invocation/InvocationNodeWrapper';
import NotesNode from './nodes/Notes/NotesNode';
+import { isWorkflowHotkeyEnabled, shouldIgnoreWorkflowCopyHotkey } from './workflowHotkeys';
const edgeTypes = {
collapsed: InvocationCollapsedEdge,
@@ -248,14 +249,14 @@ export const Flow = memo(() => {
>
<Background gap={snapGrid} offset={snapGrid} />
</ReactFlow>
- <HotkeyIsolator />
+ <HotkeyIsolator flowWrapper={flowWrapper} />
</>
);
});
Flow.displayName = 'Flow';
-const HotkeyIsolator = memo(() => {
+const HotkeyIsolator = memo(({ flowWrapper }: { flowWrapper: RefObject<HTMLDivElement> }) => {
const mayUndo = useAppSelector(selectMayUndo);
const mayRedo = useAppSelector(selectMayRedo);
@@ -270,8 +271,12 @@ const HotkeyIsolator = memo(() => {
id: 'copySelection',
category: 'workflows',
callback: copySelection,
- options: { enabled: isWorkflowsFocused, preventDefault: true },
- dependencies: [copySelection],
+ options: {
+ enabled: isWorkflowHotkeyEnabled(isWorkflowsFocused),
+ preventDefault: true,
+ ignoreEventWhen: () => shouldIgnoreWorkflowCopyHotkey(window.getSelection(), flowWrapper.current),
+ },
+ dependencies: [copySelection, isWorkflowsFocused],
});
const selectAll = useCallback(() => {
@@ -299,7 +304,7 @@ const HotkeyIsolator = memo(() => {
id: 'selectAll',
category: 'workflows',
callback: selectAll,
- options: { enabled: isWorkflowsFocused, preventDefault: true },
+ options: { enabled: isWorkflowHotkeyEnabled(isWorkflowsFocused), preventDefault: true },
dependencies: [selectAll, isWorkflowsFocused],
});
@@ -307,7 +312,7 @@ const HotkeyIsolator = memo(() => {
id: 'pasteSelection',
category: 'workflows',
callback: pasteSelection,
- options: { enabled: isWorkflowsFocused, preventDefault: true },
+ options: { enabled: isWorkflowHotkeyEnabled(isWorkflowsFocused), preventDefault: true },
dependencies: [pasteSelection, isWorkflowsFocused],
});
@@ -315,7 +320,7 @@ const HotkeyIsolator = memo(() => {
id: 'pasteSelectionWithEdges',
category: 'workflows',
callback: pasteSelectionWithEdges,
- options: { enabled: isWorkflowsFocused, preventDefault: true },
+ options: { enabled: isWorkflowHotkeyEnabled(isWorkflowsFocused), preventDefault: true },
dependencies: [pasteSelectionWithEdges, isWorkflowsFocused],
});
@@ -325,7 +330,7 @@ const HotkeyIsolator = memo(() => {
callback: () => {
store.dispatch(undo());
},
- options: { enabled: isWorkflowsFocused && mayUndo, preventDefault: true },
+ options: { enabled: isWorkflowHotkeyEnabled(isWorkflowsFocused) && mayUndo, preventDefault: true },
dependencies: [store, mayUndo, isWorkflowsFocused],
});
@@ -335,7 +340,7 @@ const HotkeyIsolator = memo(() => {
callback: () => {
store.dispatch(redo());
},
- options: { enabled: isWorkflowsFocused && mayRedo, preventDefault: true },
+ options: { enabled: isWorkflowHotkeyEnabled(isWorkflowsFocused) && mayRedo, preventDefault: true },
dependencies: [store, mayRedo, isWorkflowsFocused],
});
@@ -373,7 +378,7 @@ const HotkeyIsolator = memo(() => {
id: 'deleteSelection',
category: 'workflows',
callback: deleteSelection,
- options: { preventDefault: true, enabled: isWorkflowsFocused },
+ options: { preventDefault: true, enabled: isWorkflowHotkeyEnabled(isWorkflowsFocused) },
dependencies: [deleteSelection, isWorkflowsFocused],
});
diff --git a/invokeai/frontend/web/src/features/nodes/components/flow/workflowHotkeys.test.ts b/invokeai/frontend/web/src/features/nodes/components/flow/workflowHotkeys.test.ts
new file mode 100644
index 0000000000..e901683d2d
--- /dev/null
+++ b/invokeai/frontend/web/src/features/nodes/components/flow/workflowHotkeys.test.ts
@@ -0,0 +1,76 @@
+import { describe, expect, it } from 'vitest';
+
+import { isEventTargetWithinElement, isWorkflowHotkeyEnabled, shouldIgnoreWorkflowCopyHotkey } from './workflowHotkeys';
+
+describe('isEventTargetWithinElement', () => {
+ it('returns true when the element contains the event target', () => {
+ const target = new EventTarget();
+ const element = {
+ contains: (node: unknown) => node === target,
+ };
+
+ expect(isEventTargetWithinElement(target, element as never)).toBe(true);
+ });
+
+ it('returns false when the element does not contain the event target', () => {
+ const target = new EventTarget();
+ const element = {
+ contains: () => false,
+ };
+
+ expect(isEventTargetWithinElement(target, element as never)).toBe(false);
+ });
+
+ it('returns false when the element is missing', () => {
+ expect(isEventTargetWithinElement(new EventTarget(), null)).toBe(false);
+ });
+});
+
+describe('isWorkflowHotkeyEnabled', () => {
+ it('enables workflow hotkeys whenever the workflows pane is focused', () => {
+ expect(isWorkflowHotkeyEnabled(true)).toBe(true);
+ });
+
+ it('disables workflow hotkeys when the workflows pane is not focused', () => {
+ expect(isWorkflowHotkeyEnabled(false)).toBe(false);
+ });
+});
+
+describe('shouldIgnoreWorkflowCopyHotkey', () => {
+ const insideNode = new EventTarget() as Node;
+ const outsideNode = new EventTarget() as Node;
+ const element = {
+ contains: (node: Node) => node === insideNode,
+ };
+
+ it('returns false when there is no selection', () => {
+ expect(shouldIgnoreWorkflowCopyHotkey(null, element)).toBe(false);
+ });
+
+ it('returns false for collapsed selections', () => {
+ expect(
+ shouldIgnoreWorkflowCopyHotkey(
+ { isCollapsed: true, toString: () => 'text', anchorNode: outsideNode, focusNode: outsideNode },
+ element
+ )
+ ).toBe(false);
+ });
+
+ it('returns false when the selection is inside the editor element', () => {
+ expect(
+ shouldIgnoreWorkflowCopyHotkey(
+ { isCollapsed: false, toString: () => 'text', anchorNode: insideNode, focusNode: insideNode },
+ element
+ )
+ ).toBe(false);
+ });
+
+ it('returns true when the selection is outside the editor element', () => {
+ expect(
+ shouldIgnoreWorkflowCopyHotkey(
+ { isCollapsed: false, toString: () => 'text', anchorNode: outsideNode, focusNode: outsideNode },
+ element
+ )
+ ).toBe(true);
+ });
+});
diff --git a/invokeai/frontend/web/src/features/nodes/components/flow/workflowHotkeys.ts b/invokeai/frontend/web/src/features/nodes/components/flow/workflowHotkeys.ts
new file mode 100644
index 0000000000..4de23face8
--- /dev/null
+++ b/invokeai/frontend/web/src/features/nodes/components/flow/workflowHotkeys.ts
@@ -0,0 +1,34 @@
+export const isEventTargetWithinElement = (
+ target: EventTarget | null,
+ element: { contains: (node: Node) => boolean } | null
+) => {
+ return Boolean(target && element?.contains(target as Node));
+};
+
+export const isWorkflowHotkeyEnabled = (isWorkflowsFocused: boolean) => {
+ return isWorkflowsFocused;
+};
+
+type SelectionLike = {
+ isCollapsed: boolean;
+ toString(): string;
+ anchorNode: Node | null;
+ focusNode: Node | null;
+};
+
+export const shouldIgnoreWorkflowCopyHotkey = (
+ selection: SelectionLike | null | undefined,
+ element: { contains: (node: Node) => boolean } | null
+) => {
+ if (!selection || !element || selection.isCollapsed || selection.toString().length === 0) {
+ return false;
+ }
+
+ const nodes = [selection.anchorNode, selection.focusNode].filter((node): node is Node => node !== null);
+
+ if (nodes.length === 0) {
+ return false;
+ }
+
+ return nodes.some((node) => !element.contains(node));
+};