Skip to content

Commit 4d23f7b

Browse files
committed
feat(contextMenu): implement context menu item collection for extensions
1 parent e19778d commit 4d23f7b

File tree

4 files changed

+438
-2
lines changed

4 files changed

+438
-2
lines changed

src/scripts/app.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { shallowRef } from 'vue'
77
import { useCanvasPositionConversion } from '@/composables/element/useCanvasPositionConversion'
88
import { registerProxyWidgets } from '@/core/graph/subgraph/proxyWidget'
99
import { st, t } from '@/i18n'
10+
import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces'
1011
import {
1112
LGraph,
1213
LGraphCanvas,
@@ -1667,6 +1668,56 @@ export class ComfyApp {
16671668
useExtensionService().registerExtension(extension)
16681669
}
16691670

1671+
/**
1672+
* Collects context menu items from all extensions for canvas menus
1673+
* @param canvas The canvas instance
1674+
* @returns Array of context menu items from all extensions
1675+
*/
1676+
collectCanvasMenuItems(canvas: LGraphCanvas): IContextMenuValue[] {
1677+
const items: IContextMenuValue[] = []
1678+
1679+
for (const ext of this.extensions) {
1680+
if (ext.getCanvasMenuItems) {
1681+
try {
1682+
const extItems = ext.getCanvasMenuItems(canvas)
1683+
items.push(...extItems)
1684+
} catch (error) {
1685+
console.error(
1686+
`[Context Menu] Extension "${ext.name}" failed to provide canvas menu items:`,
1687+
error
1688+
)
1689+
}
1690+
}
1691+
}
1692+
1693+
return items
1694+
}
1695+
1696+
/**
1697+
* Collects context menu items from all extensions for node menus
1698+
* @param node The node being right-clicked
1699+
* @returns Array of context menu items from all extensions
1700+
*/
1701+
collectNodeMenuItems(node: LGraphNode): IContextMenuValue[] {
1702+
const items: IContextMenuValue[] = []
1703+
1704+
for (const ext of this.extensions) {
1705+
if (ext.getNodeMenuItems) {
1706+
try {
1707+
const extItems = ext.getNodeMenuItems(node)
1708+
items.push(...extItems)
1709+
} catch (error) {
1710+
console.error(
1711+
`[Context Menu] Extension "${ext.name}" failed to provide node menu items:`,
1712+
error
1713+
)
1714+
}
1715+
}
1716+
}
1717+
1718+
return items
1719+
}
1720+
16701721
/**
16711722
* Refresh combo list on whole nodes
16721723
*/

src/types/comfy.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import type { Positionable } from '@/lib/litegraph/src/interfaces'
2-
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
1+
import type {
2+
IContextMenuValue,
3+
Positionable
4+
} from '@/lib/litegraph/src/interfaces'
5+
import type { LGraphCanvas, LGraphNode } from '@/lib/litegraph/src/litegraph'
36
import type { SettingParams } from '@/platform/settings/types'
47
import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema'
58
import type { Keybinding } from '@/schemas/keyBindingSchema'
@@ -106,6 +109,20 @@ export interface ComfyExtension {
106109
*/
107110
getSelectionToolboxCommands?(selectedItem: Positionable): string[]
108111

112+
/**
113+
* Allows the extension to add context menu items to canvas right-click menus
114+
* @param canvas The canvas instance
115+
* @returns An array of context menu items to add
116+
*/
117+
getCanvasMenuItems?(canvas: LGraphCanvas): IContextMenuValue[]
118+
119+
/**
120+
* Allows the extension to add context menu items to node right-click menus
121+
* @param node The node being right-clicked
122+
* @returns An array of context menu items to add
123+
*/
124+
getNodeMenuItems?(node: LGraphNode): IContextMenuValue[]
125+
109126
/**
110127
* Allows the extension to add additional handling to the node before it is registered with **LGraph**
111128
* @param nodeType The node class (not an instance)

0 commit comments

Comments
 (0)