Line data Source code
1 : import { showNodeOptions } from '@/composables/graph/useMoreOptionsMenu'
2 : import { getCanvasContextMenuTarget } from '@/lib/litegraph/src/canvas/getCanvasContextMenuTarget'
3 : import {
4 : LGraphCanvas,
5 : LGraphNode,
6 : LiteGraph
7 : } from '@/lib/litegraph/src/litegraph'
8 :
9 68 : function shouldUseLegacyContextMenu(
10 : canvas: LGraphCanvas,
11 : node: LGraphNode | undefined
12 : ) {
13 68 : return Boolean(node) || !LiteGraph.vueNodesMode || !canvas.graph
14 : }
15 :
16 : /**
17 : * Routes Nodes 2.0 group right-clicks to Vue while nodes, reroutes,
18 : * background, and legacy mode stay on litegraph.
19 : */
20 2355 : export function useGroupContextMenu() {
21 2355 : const original = LGraphCanvas.prototype.processContextMenu
22 :
23 68 : function processContextMenuWithVueGroupMenu(
24 : this: LGraphCanvas,
25 : ...args: Parameters<typeof original>
26 : ): void {
27 68 : const [node, event] = args
28 :
29 68 : if (shouldUseLegacyContextMenu(this, node)) {
30 56 : original.apply(this, args)
31 56 : return
32 : }
33 :
34 12 : const { reroute, link, group } = getCanvasContextMenuTarget(
35 : this,
36 : event.canvasX,
37 : event.canvasY
38 : )
39 68 : if (reroute || link || !group) {
40 7 : original.apply(this, args)
41 7 : return
42 : }
43 :
44 : const groupIsOnlySelection =
45 : this.selectedItems.size === 1 && this.selectedItems.has(group)
46 :
47 68 : if (!groupIsOnlySelection && !this.selectOnly) {
48 3 : this.deselectAll({ notify: false })
49 3 : this.select(group, { notify: false, selectGroupChildren: false })
50 3 : this.onSelectionChange?.(
51 : Object.fromEntries(
52 : [...this.selectedItems]
53 : .filter((item) => item instanceof LGraphNode)
54 : .map((node) => [node.id, node])
55 : )
56 : )
57 3 : this.setDirty(true)
58 : }
59 5 : showNodeOptions(event)
60 : }
61 :
62 2355 : LGraphCanvas.prototype.processContextMenu = processContextMenuWithVueGroupMenu
63 : }
|