mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-20 06:20:11 +00:00
## Summary Fix node layout drift from repeated `ensureCorrectLayoutScale` scaling, simplify it to a pure one-time normalizer, and fix links not following Vue nodes during drag. ## Changes - **What**: - `ensureCorrectLayoutScale` simplified to a one-time normalizer: unprojects legacy Vue-scaled coordinates back to canonical LiteGraph coordinates, marks the graph as corrected, and does nothing else. No longer touches the layout store, syncs reroutes, or changes canvas scale. - Removed no-op calls from `useVueNodeLifecycle.ts` (a renderer version string was passed where an `LGraph` was expected). - `layoutStore.finalizeOperation` now calls `notifyChange` synchronously instead of via `setTimeout`. This ensures `useLayoutSync`'s `onChange` callback pushes positions to LiteGraph `node.pos` and calls `canvas.setDirty()` within the same RAF frame as a drag update, fixing links not following Vue nodes during drag. - **Tests**: Added tests for `ensureCorrectLayoutScale` (idempotency, round-trip, unknown-renderer no-op) and `graphRenderTransform` (project/unproject round-trips, anchor caching). ## Review Focus - The `setTimeout(() => this.notifyChange(change), 0)` → `this.notifyChange(change)` change in `layoutStore.ts` is the key fix for the drag-link-sync bug. The listener (`useLayoutSync`) only writes to LiteGraph, not back to the layout store, so synchronous notification is safe. - `ensureCorrectLayoutScale` no longer has any side effects beyond normalizing coordinates and setting `workflowRendererVersion` metadata. --------- Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: Christian Byrne <cbyrne@comfy.org> Co-authored-by: jaeone94 <89377375+jaeone94@users.noreply.github.com> Co-authored-by: AustinMroz <austin@comfy.org> Co-authored-by: Hunter <huntcsg@users.noreply.github.com> Co-authored-by: GitHub Action <action@github.com>
150 lines
4.4 KiB
TypeScript
150 lines
4.4 KiB
TypeScript
import { createSharedComposable, whenever } from '@vueuse/core'
|
|
import { shallowRef, watch } from 'vue'
|
|
|
|
import { useGraphNodeManager } from '@/composables/graph/useGraphNodeManager'
|
|
import type { GraphNodeManager } from '@/composables/graph/useGraphNodeManager'
|
|
import { useVueFeatureFlags } from '@/composables/useVueFeatureFlags'
|
|
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
|
|
import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
|
|
import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations'
|
|
import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
|
|
import { useLayoutSync } from '@/renderer/core/layout/sync/useLayoutSync'
|
|
import { app as comfyApp } from '@/scripts/app'
|
|
|
|
function useVueNodeLifecycleIndividual() {
|
|
const canvasStore = useCanvasStore()
|
|
const layoutMutations = useLayoutMutations()
|
|
const { shouldRenderVueNodes } = useVueFeatureFlags()
|
|
const nodeManager = shallowRef<GraphNodeManager | null>(null)
|
|
const { startSync, stopSync } = useLayoutSync()
|
|
|
|
const initializeNodeManager = () => {
|
|
// Use canvas graph if available (handles subgraph contexts), fallback to app graph
|
|
const activeGraph = comfyApp.canvas?.graph
|
|
if (!activeGraph || nodeManager.value) return
|
|
|
|
// Initialize the core node manager
|
|
const manager = useGraphNodeManager(activeGraph)
|
|
nodeManager.value = manager
|
|
|
|
// Initialize layout system with existing nodes from active graph
|
|
const nodes = activeGraph._nodes.map((node: LGraphNode) => ({
|
|
id: node.id.toString(),
|
|
pos: [node.pos[0], node.pos[1]] as [number, number],
|
|
size: [node.size[0], node.size[1]] as [number, number]
|
|
}))
|
|
layoutStore.initializeFromLiteGraph(nodes)
|
|
|
|
// Seed reroutes into the Layout Store so hit-testing uses the new path
|
|
for (const reroute of activeGraph.reroutes.values()) {
|
|
const [x, y] = reroute.pos
|
|
const parent = reroute.parentId ?? undefined
|
|
const linkIds = Array.from(reroute.linkIds)
|
|
layoutMutations.createReroute(reroute.id, { x, y }, parent, linkIds)
|
|
}
|
|
|
|
// Seed existing links into the Layout Store (topology only)
|
|
for (const link of activeGraph._links.values()) {
|
|
layoutMutations.createLink(
|
|
link.id,
|
|
link.origin_id,
|
|
link.origin_slot,
|
|
link.target_id,
|
|
link.target_slot
|
|
)
|
|
}
|
|
|
|
// Start sync AFTER seeding so bootstrap operations don't trigger
|
|
// the Layout→LiteGraph writeback loop redundantly.
|
|
startSync(canvasStore.canvas)
|
|
}
|
|
|
|
const disposeNodeManagerAndSyncs = () => {
|
|
stopSync()
|
|
if (!nodeManager.value) return
|
|
|
|
try {
|
|
nodeManager.value.cleanup()
|
|
} catch {
|
|
/* empty */
|
|
}
|
|
nodeManager.value = null
|
|
}
|
|
|
|
// Watch for Vue nodes enabled state changes
|
|
watch(
|
|
() => shouldRenderVueNodes.value && Boolean(comfyApp.canvas?.graph),
|
|
(enabled) => {
|
|
if (enabled) {
|
|
initializeNodeManager()
|
|
}
|
|
},
|
|
{ immediate: true }
|
|
)
|
|
|
|
whenever(
|
|
() => !shouldRenderVueNodes.value,
|
|
() => {
|
|
disposeNodeManagerAndSyncs()
|
|
comfyApp.canvas?.setDirty(true, true)
|
|
}
|
|
)
|
|
|
|
// Clear stale slot layouts when switching modes
|
|
watch(
|
|
() => shouldRenderVueNodes.value,
|
|
() => {
|
|
layoutStore.clearAllSlotLayouts()
|
|
}
|
|
)
|
|
|
|
// Handle case where Vue nodes are enabled but graph starts empty
|
|
const setupEmptyGraphListener = () => {
|
|
const activeGraph = comfyApp.canvas?.graph
|
|
if (
|
|
!shouldRenderVueNodes.value ||
|
|
nodeManager.value ||
|
|
activeGraph?._nodes.length !== 0
|
|
) {
|
|
return
|
|
}
|
|
const originalOnNodeAdded = activeGraph.onNodeAdded
|
|
activeGraph.onNodeAdded = function (node: LGraphNode) {
|
|
// Restore original handler
|
|
activeGraph.onNodeAdded = originalOnNodeAdded
|
|
|
|
// Initialize node manager if needed
|
|
if (shouldRenderVueNodes.value && !nodeManager.value) {
|
|
initializeNodeManager()
|
|
}
|
|
|
|
// Call original handler
|
|
if (originalOnNodeAdded) {
|
|
originalOnNodeAdded.call(this, node)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Cleanup function for component unmounting
|
|
const cleanup = () => {
|
|
if (nodeManager.value) {
|
|
nodeManager.value.cleanup()
|
|
nodeManager.value = null
|
|
}
|
|
}
|
|
|
|
return {
|
|
nodeManager,
|
|
|
|
// Lifecycle methods
|
|
initializeNodeManager,
|
|
disposeNodeManagerAndSyncs,
|
|
setupEmptyGraphListener,
|
|
cleanup
|
|
}
|
|
}
|
|
|
|
export const useVueNodeLifecycle = createSharedComposable(
|
|
useVueNodeLifecycleIndividual
|
|
)
|