- 
                Notifications
    You must be signed in to change notification settings 
- Fork 393
Contextmenu Monkeypatch Standardization #5977
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3bc9bcc
              
                feat(contextMenu): add extension API for context menu items
              
              
                Myestery f78cf05
              
                Merge branch 'main' into contextmenu-for-extensions
              
              
                Myestery 3defee1
              
                refactor(contextMenu): use invokeExtensions pattern for menu collection
              
              
                Myestery f858803
              
                test: overhaul context menu extension tests
              
              
                Myestery 9e8c004
              
                fix(tests): update type assertion for separator in context menu tests
              
              
                Myestery File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import { createTestingPinia } from '@pinia/testing' | ||
| import { setActivePinia } from 'pinia' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
|  | ||
| import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces' | ||
| import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph' | ||
| import { useExtensionService } from '@/services/extensionService' | ||
| import { useExtensionStore } from '@/stores/extensionStore' | ||
| import type { ComfyExtension } from '@/types/comfy' | ||
|  | ||
| describe('Context Menu Extension API', () => { | ||
| let mockCanvas: LGraphCanvas | ||
| let mockNode: LGraphNode | ||
| 
      Comment on lines
    
      +12
     to 
      +13
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Could you use a factory to make these per-testcase instead of keeping them in the describe scope? That could also add some type safety via  Also, I don't love things being called mock that aren't using the mocking system, but that's a lesser issue. | ||
| let extensionStore: ReturnType<typeof useExtensionStore> | ||
| let extensionService: ReturnType<typeof useExtensionService> | ||
|  | ||
| // Mock menu items | ||
| const canvasMenuItem1: IContextMenuValue = { | ||
| content: 'Canvas Item 1', | ||
| callback: () => {} | ||
| } | ||
| const canvasMenuItem2: IContextMenuValue = { | ||
| content: 'Canvas Item 2', | ||
| callback: () => {} | ||
| } | ||
| const nodeMenuItem1: IContextMenuValue = { | ||
| content: 'Node Item 1', | ||
| callback: () => {} | ||
| } | ||
| const nodeMenuItem2: IContextMenuValue = { | ||
| content: 'Node Item 2', | ||
| callback: () => {} | ||
| } | ||
|  | ||
| // Mock extensions | ||
| const createCanvasMenuExtension = ( | ||
| name: string, | ||
| items: IContextMenuValue[] | ||
| ): ComfyExtension => ({ | ||
| name, | ||
| getCanvasMenuItems: () => items | ||
| }) | ||
|  | ||
| const createNodeMenuExtension = ( | ||
| name: string, | ||
| items: IContextMenuValue[] | ||
| ): ComfyExtension => ({ | ||
| name, | ||
| getNodeMenuItems: () => items | ||
| }) | ||
|  | ||
| beforeEach(() => { | ||
| setActivePinia(createTestingPinia({ stubActions: false })) | ||
| extensionStore = useExtensionStore() | ||
| extensionService = useExtensionService() | ||
|  | ||
| mockCanvas = { | ||
| graph_mouse: [100, 100], | ||
| selectedItems: new Set() | ||
| } as unknown as LGraphCanvas | ||
|  | ||
| mockNode = { | ||
| id: 1, | ||
| type: 'TestNode', | ||
| pos: [0, 0] | ||
| } as unknown as LGraphNode | ||
| }) | ||
|  | ||
| describe('collectCanvasMenuItems', () => { | ||
| it('should call getCanvasMenuItems and collect into flat array', () => { | ||
| const ext1 = createCanvasMenuExtension('Extension 1', [canvasMenuItem1]) | ||
| const ext2 = createCanvasMenuExtension('Extension 2', [ | ||
| canvasMenuItem2, | ||
| { content: 'Item 3', callback: () => {} } | ||
| ]) | ||
|  | ||
| extensionStore.registerExtension(ext1) | ||
| extensionStore.registerExtension(ext2) | ||
|  | ||
| const items = extensionService | ||
| .invokeExtensions('getCanvasMenuItems', mockCanvas) | ||
| .flat() as IContextMenuValue[] | ||
|  | ||
| expect(items).toHaveLength(3) | ||
| expect(items[0]).toMatchObject({ content: 'Canvas Item 1' }) | ||
| expect(items[1]).toMatchObject({ content: 'Canvas Item 2' }) | ||
| expect(items[2]).toMatchObject({ content: 'Item 3' }) | ||
| }) | ||
|  | ||
| it('should support submenus and separators', () => { | ||
| const extension = createCanvasMenuExtension('Test Extension', [ | ||
| { | ||
| content: 'Menu with Submenu', | ||
| has_submenu: true, | ||
| submenu: { | ||
| options: [ | ||
| { content: 'Submenu Item 1', callback: () => {} }, | ||
| { content: 'Submenu Item 2', callback: () => {} } | ||
| ] | ||
| } | ||
| }, | ||
| null as unknown as IContextMenuValue, | ||
| { content: 'After Separator', callback: () => {} } | ||
| ]) | ||
|  | ||
| extensionStore.registerExtension(extension) | ||
|  | ||
| const items = extensionService | ||
| .invokeExtensions('getCanvasMenuItems', mockCanvas) | ||
| .flat() as IContextMenuValue[] | ||
|  | ||
| expect(items).toHaveLength(3) | ||
| expect(items[0].content).toBe('Menu with Submenu') | ||
| expect(items[0].submenu?.options).toHaveLength(2) | ||
| expect(items[1]).toBeNull() | ||
| expect(items[2].content).toBe('After Separator') | ||
| }) | ||
|  | ||
| it('should skip extensions without getCanvasMenuItems', () => { | ||
| const canvasExtension = createCanvasMenuExtension('Canvas Ext', [ | ||
| canvasMenuItem1 | ||
| ]) | ||
| const extensionWithoutCanvasMenu: ComfyExtension = { | ||
| name: 'No Canvas Menu' | ||
| } | ||
|  | ||
| extensionStore.registerExtension(canvasExtension) | ||
| extensionStore.registerExtension(extensionWithoutCanvasMenu) | ||
|  | ||
| const items = extensionService | ||
| .invokeExtensions('getCanvasMenuItems', mockCanvas) | ||
| .flat() as IContextMenuValue[] | ||
|  | ||
| expect(items).toHaveLength(1) | ||
| expect(items[0].content).toBe('Canvas Item 1') | ||
| }) | ||
| }) | ||
|  | ||
| describe('collectNodeMenuItems', () => { | ||
| it('should call getNodeMenuItems and collect into flat array', () => { | ||
| const ext1 = createNodeMenuExtension('Extension 1', [nodeMenuItem1]) | ||
| const ext2 = createNodeMenuExtension('Extension 2', [ | ||
| nodeMenuItem2, | ||
| { content: 'Item 3', callback: () => {} } | ||
| ]) | ||
|  | ||
| extensionStore.registerExtension(ext1) | ||
| extensionStore.registerExtension(ext2) | ||
|  | ||
| const items = extensionService | ||
| .invokeExtensions('getNodeMenuItems', mockNode) | ||
| .flat() as IContextMenuValue[] | ||
|  | ||
| expect(items).toHaveLength(3) | ||
| expect(items[0]).toMatchObject({ content: 'Node Item 1' }) | ||
| expect(items[1]).toMatchObject({ content: 'Node Item 2' }) | ||
| }) | ||
|  | ||
| it('should support submenus', () => { | ||
| const extension = createNodeMenuExtension('Submenu Extension', [ | ||
| { | ||
| content: 'Node Menu with Submenu', | ||
| has_submenu: true, | ||
| submenu: { | ||
| options: [ | ||
| { content: 'Node Submenu 1', callback: () => {} }, | ||
| { content: 'Node Submenu 2', callback: () => {} } | ||
| ] | ||
| } | ||
| } | ||
| ]) | ||
|  | ||
| extensionStore.registerExtension(extension) | ||
|  | ||
| const items = extensionService | ||
| .invokeExtensions('getNodeMenuItems', mockNode) | ||
| .flat() as IContextMenuValue[] | ||
|  | ||
| expect(items[0].content).toBe('Node Menu with Submenu') | ||
| expect(items[0].submenu?.options).toHaveLength(2) | ||
| }) | ||
|  | ||
| it('should skip extensions without getNodeMenuItems', () => { | ||
| const nodeExtension = createNodeMenuExtension('Node Ext', [nodeMenuItem1]) | ||
| const extensionWithoutNodeMenu: ComfyExtension = { | ||
| name: 'No Node Menu' | ||
| } | ||
|  | ||
| extensionStore.registerExtension(nodeExtension) | ||
| extensionStore.registerExtension(extensionWithoutNodeMenu) | ||
|  | ||
| const items = extensionService | ||
| .invokeExtensions('getNodeMenuItems', mockNode) | ||
| .flat() as IContextMenuValue[] | ||
|  | ||
| expect(items).toHaveLength(1) | ||
| expect(items[0].content).toBe('Node Item 1') | ||
| }) | ||
| }) | ||
| }) | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.