@@ -5,9 +5,9 @@ import { useCanvasTransformSync } from '@/composables/canvas/useCanvasTransformS
55import { LGraphEventMode , LGraphNode } from '@/lib/litegraph/src/litegraph'
66import type { NodeId } from '@/schemas/comfyWorkflowSchema'
77import { api } from '@/scripts/api'
8- import { app } from '@/scripts/app'
98import { useCanvasStore } from '@/stores/graphStore'
109import { useSettingStore } from '@/stores/settingStore'
10+ import { useWorkflowStore } from '@/stores/workflowStore'
1111import { useColorPaletteStore } from '@/stores/workspace/colorPaletteStore'
1212import { adjustColor } from '@/utils/colorUtil'
1313
@@ -27,6 +27,7 @@ export type MinimapOptionKey =
2727export function useMinimap ( ) {
2828 const settingStore = useSettingStore ( )
2929 const canvasStore = useCanvasStore ( )
30+ const workflowStore = useWorkflowStore ( )
3031 const colorPaletteStore = useColorPaletteStore ( )
3132
3233 const containerRef = ref < HTMLDivElement > ( )
@@ -147,7 +148,11 @@ export function useMinimap() {
147148 }
148149
149150 const canvas = computed ( ( ) => canvasStore . canvas )
150- const graph = ref ( app . canvas ?. graph )
151+ const graph = computed ( ( ) => {
152+ // If we're in a subgraph, use that; otherwise use the canvas graph
153+ const activeSubgraph = workflowStore . activeSubgraph
154+ return activeSubgraph || canvas . value ?. graph
155+ } )
151156
152157 const containerStyles = computed ( ( ) => ( {
153158 width : `${ width } px` ,
@@ -627,7 +632,8 @@ export function useMinimap() {
627632 c . setDirty ( true , true )
628633 }
629634
630- let originalCallbacks : GraphCallbacks = { }
635+ // Map to store original callbacks per graph ID
636+ const originalCallbacksMap = new Map < string , GraphCallbacks > ( )
631637
632638 const handleGraphChanged = useThrottleFn ( ( ) => {
633639 needsFullRedraw . value = true
@@ -641,11 +647,18 @@ export function useMinimap() {
641647 const g = graph . value
642648 if ( ! g ) return
643649
644- originalCallbacks = {
650+ // Check if we've already wrapped this graph's callbacks
651+ if ( originalCallbacksMap . has ( g . id ) ) {
652+ return
653+ }
654+
655+ // Store the original callbacks for this graph
656+ const originalCallbacks : GraphCallbacks = {
645657 onNodeAdded : g . onNodeAdded ,
646658 onNodeRemoved : g . onNodeRemoved ,
647659 onConnectionChange : g . onConnectionChange
648660 }
661+ originalCallbacksMap . set ( g . id , originalCallbacks )
649662
650663 g . onNodeAdded = function ( node ) {
651664 originalCallbacks . onNodeAdded ?. call ( this , node )
@@ -670,15 +683,18 @@ export function useMinimap() {
670683 const g = graph . value
671684 if ( ! g ) return
672685
673- if ( originalCallbacks . onNodeAdded !== undefined ) {
674- g . onNodeAdded = originalCallbacks . onNodeAdded
675- }
676- if ( originalCallbacks . onNodeRemoved !== undefined ) {
677- g . onNodeRemoved = originalCallbacks . onNodeRemoved
678- }
679- if ( originalCallbacks . onConnectionChange !== undefined ) {
680- g . onConnectionChange = originalCallbacks . onConnectionChange
686+ const originalCallbacks = originalCallbacksMap . get ( g . id )
687+ if ( ! originalCallbacks ) {
688+ throw new Error (
689+ 'Attempted to cleanup event listeners for graph that was never set up'
690+ )
681691 }
692+
693+ g . onNodeAdded = originalCallbacks . onNodeAdded
694+ g . onNodeRemoved = originalCallbacks . onNodeRemoved
695+ g . onConnectionChange = originalCallbacks . onConnectionChange
696+
697+ originalCallbacksMap . delete ( g . id )
682698 }
683699
684700 const init = async ( ) => {
@@ -751,6 +767,19 @@ export function useMinimap() {
751767 { immediate : true , flush : 'post' }
752768 )
753769
770+ // Watch for graph changes (e.g., when navigating to/from subgraphs)
771+ watch ( graph , ( newGraph , oldGraph ) => {
772+ if ( newGraph && newGraph !== oldGraph ) {
773+ cleanupEventListeners ( )
774+ setupEventListeners ( )
775+ needsFullRedraw . value = true
776+ updateFlags . value . bounds = true
777+ updateFlags . value . nodes = true
778+ updateFlags . value . connections = true
779+ updateMinimap ( )
780+ }
781+ } )
782+
754783 watch ( visible , async ( isVisible ) => {
755784 if ( isVisible ) {
756785 if ( containerRef . value ) {
0 commit comments