From 12bdf8971e84108e85a8587701a2d2f060e34270 Mon Sep 17 00:00:00 2001 From: bymyself Date: Fri, 17 Oct 2025 01:38:04 -0700 Subject: [PATCH] [refactor] extract clipboard target check to shared helper - addresses review feedback Co-authored-by: Myestery --- src/composables/useCopy.ts | 15 +++++---------- src/composables/usePaste.ts | 17 ++++------------- src/workbench/eventHelpers.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 src/workbench/eventHelpers.ts diff --git a/src/composables/useCopy.ts b/src/composables/useCopy.ts index 64adce6cf..20da5c65d 100644 --- a/src/composables/useCopy.ts +++ b/src/composables/useCopy.ts @@ -1,6 +1,7 @@ import { useEventListener } from '@vueuse/core' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' +import { isEventTargetInGraph } from '@/workbench/eventHelpers' /** * Adds a handler on copy that serializes selected nodes to JSON @@ -21,19 +22,13 @@ export const useCopy = () => { return } // Check if target is graph canvas or within graph UI (minimap, controls, etc.) - const isTargetInGraph = - e.target.id === 'graph-canvas' || - e.target.id === 'comfy-minimap' || - e.target.id === 'graph-canvas-controls' || - e.target.classList.contains('graph-canvas-container') || - e.target.classList.contains('litegraph') || - e.target.closest('#comfy-minimap') !== null || - e.target.closest('#graph-canvas-controls') !== null || - e.target.closest('#graph-canvas-container') !== null + if (!isEventTargetInGraph(e.target)) { + return + } // copy nodes and clear clipboard const canvas = canvasStore.canvas - if (isTargetInGraph && canvas?.selectedItems) { + if (canvas?.selectedItems) { canvas.copyToClipboard() // clearData doesn't remove images from clipboard e.clipboardData?.setData('text', ' ') diff --git a/src/composables/usePaste.ts b/src/composables/usePaste.ts index 69b87ecde..c322d5940 100644 --- a/src/composables/usePaste.ts +++ b/src/composables/usePaste.ts @@ -7,6 +7,7 @@ import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' import { app } from '@/scripts/app' import { useWorkspaceStore } from '@/stores/workspaceStore' import { isAudioNode, isImageNode, isVideoNode } from '@/utils/litegraphUtil' +import { isEventTargetInGraph } from '@/workbench/eventHelpers' /** * Adds a handler on paste that extracts and loads images or workflows from pasted JSON data @@ -39,19 +40,9 @@ export const usePaste = () => { useEventListener(document, 'paste', async (e) => { // Check if target is graph canvas or within graph UI (minimap, controls, etc.) - const isTargetInGraph = - e.target instanceof Element && - (e.target.id === 'graph-canvas' || - e.target.id === 'comfy-minimap' || - e.target.id === 'graph-canvas-controls' || - e.target.classList.contains('graph-canvas-container') || - e.target.classList.contains('litegraph') || - e.target.closest('#comfy-minimap') !== null || - e.target.closest('#graph-canvas-controls') !== null || - e.target.closest('#graph-canvas-container') !== null) - - // If the target is not in the graph, we don't want to handle the paste event - if (!isTargetInGraph) return + if (!isEventTargetInGraph(e.target)) { + return + } // ctrl+shift+v is used to paste nodes with connections // this is handled by litegraph diff --git a/src/workbench/eventHelpers.ts b/src/workbench/eventHelpers.ts new file mode 100644 index 000000000..44e1a9bb2 --- /dev/null +++ b/src/workbench/eventHelpers.ts @@ -0,0 +1,31 @@ +/** + * Utility functions for handling workbench events + */ + +/** + * Checks if an event target is within the graph canvas or related UI elements + * (minimap, canvas controls, etc.) + * + * Used by clipboard handlers to determine if copy/paste events should be + * intercepted for graph operations vs. allowing default browser behavior + * for text inputs and other UI elements. + * + * @param target - The event target to check + * @returns true if the target is within graph-related UI elements + */ +export function isEventTargetInGraph(target: EventTarget | null): boolean { + if (!(target instanceof Element)) { + return false + } + + return ( + target.id === 'graph-canvas' || + target.id === 'comfy-minimap' || + target.id === 'graph-canvas-controls' || + target.classList.contains('graph-canvas-container') || + target.classList.contains('litegraph') || + target.closest('#comfy-minimap') !== null || + target.closest('#graph-canvas-controls') !== null || + target.closest('#graph-canvas-container') !== null + ) +}