|
1 | 1 | import { PREFIX, SEPARATOR } from '@/constants/groupNodeConstants' |
2 | 2 | import { t } from '@/i18n' |
3 | 3 | import { type NodeId } from '@/lib/litegraph/src/LGraphNode' |
| 4 | +import type { IContextMenuValue } from '@/lib/litegraph/src/interfaces' |
4 | 5 | import { |
5 | 6 | type ExecutableLGraphNode, |
6 | 7 | type ExecutionId, |
7 | | - LGraphCanvas, |
8 | 8 | LGraphNode, |
9 | 9 | LiteGraph, |
10 | 10 | SubgraphNode |
@@ -1630,57 +1630,6 @@ export class GroupNodeHandler { |
1630 | 1630 | } |
1631 | 1631 | } |
1632 | 1632 |
|
1633 | | -function addConvertToGroupOptions() { |
1634 | | - // @ts-expect-error fixme ts strict error |
1635 | | - function addConvertOption(options, index) { |
1636 | | - const selected = Object.values(app.canvas.selected_nodes ?? {}) |
1637 | | - const disabled = |
1638 | | - selected.length < 2 || |
1639 | | - selected.find((n) => GroupNodeHandler.isGroupNode(n)) |
1640 | | - options.splice(index, null, { |
1641 | | - content: `Convert to Group Node (Deprecated)`, |
1642 | | - disabled, |
1643 | | - callback: convertSelectedNodesToGroupNode |
1644 | | - }) |
1645 | | - } |
1646 | | - |
1647 | | - // @ts-expect-error fixme ts strict error |
1648 | | - function addManageOption(options, index) { |
1649 | | - const groups = app.graph.extra?.groupNodes |
1650 | | - const disabled = !groups || !Object.keys(groups).length |
1651 | | - options.splice(index, null, { |
1652 | | - content: `Manage Group Nodes`, |
1653 | | - disabled, |
1654 | | - callback: () => manageGroupNodes() |
1655 | | - }) |
1656 | | - } |
1657 | | - |
1658 | | - // Add to canvas |
1659 | | - const getCanvasMenuOptions = LGraphCanvas.prototype.getCanvasMenuOptions |
1660 | | - LGraphCanvas.prototype.getCanvasMenuOptions = function () { |
1661 | | - // @ts-expect-error fixme ts strict error |
1662 | | - const options = getCanvasMenuOptions.apply(this, arguments) |
1663 | | - const index = options.findIndex((o) => o?.content === 'Add Group') |
1664 | | - const insertAt = index === -1 ? options.length - 1 : index + 2 |
1665 | | - addConvertOption(options, insertAt) |
1666 | | - addManageOption(options, insertAt + 1) |
1667 | | - return options |
1668 | | - } |
1669 | | - |
1670 | | - // Add to nodes |
1671 | | - const getNodeMenuOptions = LGraphCanvas.prototype.getNodeMenuOptions |
1672 | | - LGraphCanvas.prototype.getNodeMenuOptions = function (node) { |
1673 | | - // @ts-expect-error fixme ts strict error |
1674 | | - const options = getNodeMenuOptions.apply(this, arguments) |
1675 | | - if (!GroupNodeHandler.isGroupNode(node)) { |
1676 | | - const index = options.findIndex((o) => o?.content === 'Properties') |
1677 | | - const insertAt = index === -1 ? options.length - 1 : index |
1678 | | - addConvertOption(options, insertAt) |
1679 | | - } |
1680 | | - return options |
1681 | | - } |
1682 | | -} |
1683 | | - |
1684 | 1633 | const replaceLegacySeparators = (nodes: ComfyNode[]): void => { |
1685 | 1634 | for (const node of nodes) { |
1686 | 1635 | if (typeof node.type === 'string' && node.type.startsWith('workflow/')) { |
@@ -1718,6 +1667,9 @@ async function convertSelectedNodesToGroupNode() { |
1718 | 1667 | return await GroupNodeHandler.fromNodes(nodes) |
1719 | 1668 | } |
1720 | 1669 |
|
| 1670 | +const convertDisabled = (selected: LGraphNode[]) => |
| 1671 | + selected.length < 2 || !!selected.find((n) => GroupNodeHandler.isGroupNode(n)) |
| 1672 | + |
1721 | 1673 | function ungroupSelectedGroupNodes() { |
1722 | 1674 | const nodes = Object.values(app.canvas.selected_nodes ?? {}) |
1723 | 1675 | for (const node of nodes) { |
@@ -1776,8 +1728,46 @@ const ext: ComfyExtension = { |
1776 | 1728 | } |
1777 | 1729 | } |
1778 | 1730 | ], |
1779 | | - setup() { |
1780 | | - addConvertToGroupOptions() |
| 1731 | + |
| 1732 | + getCanvasMenuItems(canvas): IContextMenuValue[] { |
| 1733 | + const items: IContextMenuValue[] = [] |
| 1734 | + const selected = Object.values(canvas.selected_nodes ?? {}) |
| 1735 | + const convertEnabled = !convertDisabled(selected) |
| 1736 | + |
| 1737 | + items.push({ |
| 1738 | + content: `Convert to Group Node (Deprecated)`, |
| 1739 | + disabled: !convertEnabled, |
| 1740 | + // @ts-expect-error fixme ts strict error - async callback |
| 1741 | + callback: () => convertSelectedNodesToGroupNode() |
| 1742 | + }) |
| 1743 | + |
| 1744 | + const groups = canvas.graph?.extra?.groupNodes |
| 1745 | + const manageDisabled = !groups || !Object.keys(groups).length |
| 1746 | + items.push({ |
| 1747 | + content: `Manage Group Nodes`, |
| 1748 | + disabled: manageDisabled, |
| 1749 | + callback: () => manageGroupNodes() |
| 1750 | + }) |
| 1751 | + |
| 1752 | + return items |
| 1753 | + }, |
| 1754 | + |
| 1755 | + getNodeMenuItems(node): IContextMenuValue[] { |
| 1756 | + if (GroupNodeHandler.isGroupNode(node)) { |
| 1757 | + return [] |
| 1758 | + } |
| 1759 | + |
| 1760 | + const selected = Object.values(app.canvas.selected_nodes ?? {}) |
| 1761 | + const convertEnabled = !convertDisabled(selected) |
| 1762 | + |
| 1763 | + return [ |
| 1764 | + { |
| 1765 | + content: `Convert to Group Node (Deprecated)`, |
| 1766 | + disabled: !convertEnabled, |
| 1767 | + // @ts-expect-error fixme ts strict error - async callback |
| 1768 | + callback: () => convertSelectedNodesToGroupNode() |
| 1769 | + } |
| 1770 | + ] |
1781 | 1771 | }, |
1782 | 1772 | async beforeConfigureGraph( |
1783 | 1773 | graphData: ComfyWorkflowJSON, |
|
0 commit comments