Untitled

 avatar
unknown
plain_text
5 days ago
3.4 kB
2
Indexable

 resizeGroupNode(nodes, groupId) {
        const changes = [];
        const groupNode = nodes.find(node => node.id === groupId);
        if (!groupNode) return changes;

        // Find all children of this group
        const childNodes = nodes.filter(node =>
            node.data.graphNode.getGroupId() === groupId &&
            // Exclude hidden nodes from the calculation
            !node.hidden
        );

        // If there are no visible children, maintain minimum size
        if (childNodes.length === 0) {
            return changes;
        }

        const bounds = this.reactFlowInstance.getNodesBounds(childNodes);

        // Add padding to the calculated dimensions
        const newWidth = bounds.width + (this.padding * 2);
        const newHeight = bounds.height + (this.padding * 2);

        const newPosition = {
            x: bounds.x - this.padding,
            y: bounds.y - this.padding
        };

        // Current dimensions and position
        const currentWidth = groupNode.style?.width || 0;
        const currentHeight = groupNode.style?.height || 0;
        const currentPosition = groupNode.position;

        // Calculate position change
        const xChange = currentPosition.x - newPosition.x;
        const yChange = currentPosition.y - newPosition.y;

        // Only add changes if dimensions or position actually changed
        const dimensionsChanged = currentWidth !== newWidth || currentHeight !== newHeight;
        const positionChanged = xChange !== 0 || yChange !== 0;

        if (dimensionsChanged || positionChanged) {
            // Add position change for the group node
            if (positionChanged) {
                changes.push({
                    id: groupId,
                    type: 'position',
                    position: newPosition
                });
            }

            // Add dimension change for the group node
            if (dimensionsChanged) {
                changes.push({
                    id: groupId,
                    type: 'dimensions',
                    setAttributes: true,
                    dimensions: {
                        width: newWidth,
                        height: newHeight
                    }
                });
            }

            // If the group's position changed and it has other children not involved in this resize,
            // we need to adjust their positions to maintain their relative positions within the group
            if (positionChanged) {
                // Get all children that weren't part of the calculation (those that might be outside the current bounds)
                const otherChildren = nodes.filter(node =>
                    node.data.graphNode.getGroupId() === groupId &&
                    !node.hidden &&
                    !childNodes.some(child => child.id === node.id)
                );

                // Adjust positions of other children
                otherChildren.forEach(child => {
                    changes.push({
                        id: child.id,
                        type: 'position',
                        position: {
                            x: child.position.x + xChange,
                            y: child.position.y + yChange
                        }
                    });
                });
            }
        }

        return changes;
    }
Editor is loading...
Leave a Comment