fix add child node resize parent node size

This commit is contained in:
hjlarry 2025-09-26 14:04:50 +08:00
parent 5b40bf6d4e
commit 113aa4ae08
2 changed files with 20 additions and 35 deletions

View File

@ -1,7 +1,6 @@
import { useCallback } from 'react' import { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useStoreApi } from 'reactflow'
import type { import type {
BlockEnum, BlockEnum,
ChildNodeTypeCount, ChildNodeTypeCount,
@ -16,19 +15,16 @@ import {
} from '../../constants' } from '../../constants'
import { CUSTOM_ITERATION_START_NODE } from '../iteration-start/constants' import { CUSTOM_ITERATION_START_NODE } from '../iteration-start/constants'
import { useNodesMetaData } from '@/app/components/workflow/hooks' import { useNodesMetaData } from '@/app/components/workflow/hooks'
import { useCollaborativeWorkflow } from '@/app/components/workflow/hooks/use-collaborative-workflow'
export const useNodeIterationInteractions = () => { export const useNodeIterationInteractions = () => {
const { t } = useTranslation() const { t } = useTranslation()
const store = useStoreApi()
const { nodesMap: nodesMetaDataMap } = useNodesMetaData() const { nodesMap: nodesMetaDataMap } = useNodesMetaData()
const collaborativeWorkflow = useCollaborativeWorkflow()
const handleNodeIterationRerender = useCallback((nodeId: string) => { const handleNodeIterationRerender = useCallback((nodeId: string) => {
const { const { nodes, setNodes } = collaborativeWorkflow.getState()
getNodes,
setNodes,
} = store.getState()
const nodes = getNodes()
const currentNode = nodes.find(n => n.id === nodeId)! const currentNode = nodes.find(n => n.id === nodeId)!
const childrenNodes = nodes.filter(n => n.parentId === nodeId) const childrenNodes = nodes.filter(n => n.parentId === nodeId)
let rightNode: Node let rightNode: Node
@ -72,11 +68,10 @@ export const useNodeIterationInteractions = () => {
setNodes(newNodes) setNodes(newNodes)
} }
}, [store]) }, [collaborativeWorkflow])
const handleNodeIterationChildDrag = useCallback((node: Node) => { const handleNodeIterationChildDrag = useCallback((node: Node) => {
const { getNodes } = store.getState() const { nodes } = collaborativeWorkflow.getState()
const nodes = getNodes()
const restrictPosition: { x?: number; y?: number } = { x: undefined, y: undefined } const restrictPosition: { x?: number; y?: number } = { x: undefined, y: undefined }
@ -98,21 +93,19 @@ export const useNodeIterationInteractions = () => {
return { return {
restrictPosition, restrictPosition,
} }
}, [store]) }, [collaborativeWorkflow])
const handleNodeIterationChildSizeChange = useCallback((nodeId: string) => { const handleNodeIterationChildSizeChange = useCallback((nodeId: string) => {
const { getNodes } = store.getState() const { nodes } = collaborativeWorkflow.getState()
const nodes = getNodes()
const currentNode = nodes.find(n => n.id === nodeId)! const currentNode = nodes.find(n => n.id === nodeId)!
const parentId = currentNode.parentId const parentId = currentNode.parentId
if (parentId) if (parentId)
handleNodeIterationRerender(parentId) handleNodeIterationRerender(parentId)
}, [store, handleNodeIterationRerender]) }, [collaborativeWorkflow, handleNodeIterationRerender])
const handleNodeIterationChildrenCopy = useCallback((nodeId: string, newNodeId: string, idMapping: Record<string, string>) => { const handleNodeIterationChildrenCopy = useCallback((nodeId: string, newNodeId: string, idMapping: Record<string, string>) => {
const { getNodes } = store.getState() const { nodes } = collaborativeWorkflow.getState()
const nodes = getNodes()
const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_ITERATION_START_NODE) const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_ITERATION_START_NODE)
const newIdMapping = { ...idMapping } const newIdMapping = { ...idMapping }
const childNodeTypeCount: ChildNodeTypeCount = {} const childNodeTypeCount: ChildNodeTypeCount = {}
@ -154,7 +147,7 @@ export const useNodeIterationInteractions = () => {
copyChildren, copyChildren,
newIdMapping, newIdMapping,
} }
}, [store, t]) }, [collaborativeWorkflow, t])
return { return {
handleNodeIterationRerender, handleNodeIterationRerender,

View File

@ -1,6 +1,5 @@
import { useCallback } from 'react' import { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import { useStoreApi } from 'reactflow'
import type { import type {
BlockEnum, BlockEnum,
Node, Node,
@ -14,18 +13,14 @@ import {
} from '../../constants' } from '../../constants'
import { CUSTOM_LOOP_START_NODE } from '../loop-start/constants' import { CUSTOM_LOOP_START_NODE } from '../loop-start/constants'
import { useNodesMetaData } from '@/app/components/workflow/hooks' import { useNodesMetaData } from '@/app/components/workflow/hooks'
import { useCollaborativeWorkflow } from '@/app/components/workflow/hooks/use-collaborative-workflow'
export const useNodeLoopInteractions = () => { export const useNodeLoopInteractions = () => {
const store = useStoreApi() const collaborativeWorkflow = useCollaborativeWorkflow()
const { nodesMap: nodesMetaDataMap } = useNodesMetaData() const { nodesMap: nodesMetaDataMap } = useNodesMetaData()
const handleNodeLoopRerender = useCallback((nodeId: string) => { const handleNodeLoopRerender = useCallback((nodeId: string) => {
const { const { nodes, setNodes } = collaborativeWorkflow.getState()
getNodes,
setNodes,
} = store.getState()
const nodes = getNodes()
const currentNode = nodes.find(n => n.id === nodeId)! const currentNode = nodes.find(n => n.id === nodeId)!
const childrenNodes = nodes.filter(n => n.parentId === nodeId) const childrenNodes = nodes.filter(n => n.parentId === nodeId)
let rightNode: Node let rightNode: Node
@ -69,11 +64,10 @@ export const useNodeLoopInteractions = () => {
setNodes(newNodes) setNodes(newNodes)
} }
}, [store]) }, [collaborativeWorkflow])
const handleNodeLoopChildDrag = useCallback((node: Node) => { const handleNodeLoopChildDrag = useCallback((node: Node) => {
const { getNodes } = store.getState() const { nodes } = collaborativeWorkflow.getState()
const nodes = getNodes()
const restrictPosition: { x?: number; y?: number } = { x: undefined, y: undefined } const restrictPosition: { x?: number; y?: number } = { x: undefined, y: undefined }
@ -95,21 +89,19 @@ export const useNodeLoopInteractions = () => {
return { return {
restrictPosition, restrictPosition,
} }
}, [store]) }, [collaborativeWorkflow])
const handleNodeLoopChildSizeChange = useCallback((nodeId: string) => { const handleNodeLoopChildSizeChange = useCallback((nodeId: string) => {
const { getNodes } = store.getState() const { nodes } = collaborativeWorkflow.getState()
const nodes = getNodes()
const currentNode = nodes.find(n => n.id === nodeId)! const currentNode = nodes.find(n => n.id === nodeId)!
const parentId = currentNode.parentId const parentId = currentNode.parentId
if (parentId) if (parentId)
handleNodeLoopRerender(parentId) handleNodeLoopRerender(parentId)
}, [store, handleNodeLoopRerender]) }, [collaborativeWorkflow, handleNodeLoopRerender])
const handleNodeLoopChildrenCopy = useCallback((nodeId: string, newNodeId: string) => { const handleNodeLoopChildrenCopy = useCallback((nodeId: string, newNodeId: string) => {
const { getNodes } = store.getState() const { nodes } = collaborativeWorkflow.getState()
const nodes = getNodes()
const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_LOOP_START_NODE) const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_LOOP_START_NODE)
return childrenNodes.map((child, index) => { return childrenNodes.map((child, index) => {
@ -140,7 +132,7 @@ export const useNodeLoopInteractions = () => {
newNode.id = `${newNodeId}${newNode.id + index}` newNode.id = `${newNodeId}${newNode.id + index}`
return newNode return newNode
}) })
}, [store, nodesMetaDataMap]) }, [collaborativeWorkflow, nodesMetaDataMap])
return { return {
handleNodeLoopRerender, handleNodeLoopRerender,