Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Move copy handling to useCopy composable #2451

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/graph/GraphCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import SideToolbar from '@/components/sidebar/SideToolbar.vue'
import SecondRowWorkflowTabs from '@/components/topbar/SecondRowWorkflowTabs.vue'
import { useCanvasDrop } from '@/composables/useCanvasDrop'
import { useContextMenuTranslation } from '@/composables/useContextMenuTranslation'
import { useCopy } from '@/composables/useCopy'
import { useGlobalLitegraph } from '@/composables/useGlobalLitegraph'
import { useWorkflowPersistence } from '@/composables/useWorkflowPersistence'
import { CORE_SETTINGS } from '@/constants/coreSettings'
Expand Down Expand Up @@ -263,6 +264,7 @@ useCanvasDrop(canvasRef)
onMounted(async () => {
useGlobalLitegraph()
useContextMenuTranslation()
useCopy()

comfyApp.vueAppReady = true

Expand Down
39 changes: 39 additions & 0 deletions src/composables/useCopy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useEventListener } from '@vueuse/core'

import { useCanvasStore } from '@/stores/graphStore'

/**
* Adds a handler on copy that serializes selected nodes to JSON
*/
export const useCopy = () => {
const canvasStore = useCanvasStore()

useEventListener(document, 'copy', (e) => {
if (!(e.target instanceof Element)) {
return
}
if (
(e.target instanceof HTMLTextAreaElement &&
e.target.type === 'textarea') ||
(e.target instanceof HTMLInputElement && e.target.type === 'text')
) {
// Default system copy
return
}
const isTargetInGraph =
e.target.classList.contains('litegraph') ||
e.target.classList.contains('graph-canvas-container') ||
e.target.id === 'graph-canvas'

// copy nodes and clear clipboard
const canvas = canvasStore.canvas
if (isTargetInGraph && canvas?.selectedItems) {
canvas.copyToClipboard()
// clearData doesn't remove images from clipboard
e.clipboardData?.setData('text', ' ')
e.preventDefault()
e.stopImmediatePropagation()
return false
}
})
}
33 changes: 0 additions & 33 deletions src/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,38 +554,6 @@ export class ComfyApp {
})
}

/**
* Adds a handler on copy that serializes selected nodes to JSON
*/
#addCopyHandler() {
document.addEventListener('copy', (e) => {
if (!(e.target instanceof Element)) {
return
}
if (
(e.target instanceof HTMLTextAreaElement &&
e.target.type === 'textarea') ||
(e.target instanceof HTMLInputElement && e.target.type === 'text')
) {
// Default system copy
return
}
const isTargetInGraph =
e.target.classList.contains('litegraph') ||
e.target.classList.contains('graph-canvas-container') ||
e.target.id === 'graph-canvas'

// copy nodes and clear clipboard
if (isTargetInGraph && this.canvas.selected_nodes) {
this.canvas.copyToClipboard()
e.clipboardData.setData('text', ' ') //clearData doesn't remove images from clipboard
e.preventDefault()
e.stopImmediatePropagation()
return false
}
})
}

/**
* Handle mouse
*
Expand Down Expand Up @@ -1010,7 +978,6 @@ export class ComfyApp {
this.#addDrawNodeHandler()
this.#addDrawGroupsHandler()
this.#addDropHandler()
this.#addCopyHandler()
this.#addPasteHandler()

await useExtensionService().invokeExtensionsAsync('setup')
Expand Down