@@ -8,8 +8,8 @@ import { useDrag } from 'react-dnd';
8
8
import { ItemTypes } from '../../constants/ItemTypes' ;
9
9
import StateContext from '../../context/context' ;
10
10
import { combineStyles } from '../../helperFunctions/combineStyles' ;
11
- import IndirectChild from './IndirectChild' ;
12
11
import globalDefaultStyle from '../../public/styles/globalDefaultStyles' ;
12
+ import renderChildren from '../../helperFunctions/renderChildren'
13
13
14
14
function DirectChildComponent ( { childId, type, typeId, style } : ChildElement ) {
15
15
const [ state , dispatch ] = useContext ( StateContext ) ;
@@ -64,115 +64,13 @@ function DirectChildComponent({ childId, type, typeId, style }: ChildElement) {
64
64
interactiveStyle
65
65
) ;
66
66
67
- // helper function to render children of direct child components
68
- // all children of direct child components will be indirect components
69
- // indirect child components are not interactive with the exception of route links which, when clicked, will change the canvas focus
70
- const renderIndirectChildren = (
71
- referencedComponent : Component | ChildElement
72
- ) => {
73
- // iterate through all children of referenced
74
- return referencedComponent . children . map ( child => {
75
- if ( child . type === 'Component' ) {
76
- // If indirect child of referenced component is a component, find the top level component referenced by the child
77
- const childReferencedComponent : Component = state . components . find (
78
- ( elem : Component ) => elem . id === child . typeId
79
- ) ;
80
- // combine the styles of the child with the reference component but give higher priority to the child's styles
81
-
82
- const combinedStyle = combineStyles (
83
- childReferencedComponent . style ,
84
- child . style
85
- ) ;
86
-
87
- // render an IndirectChild component, and also call renderIndirectChildren recursively to render any of the child Component's children
88
- return (
89
- < IndirectChild
90
- // combine styles of instance and referenced component. instance styles have higher priority
91
- key = {
92
- 'indChild' + child . childId . toString ( ) + child . typeId . toString ( )
93
- }
94
- style = { combinedStyle }
95
- placeHolder = ""
96
- linkId = { null }
97
- >
98
- { renderIndirectChildren ( childReferencedComponent ) }
99
- </ IndirectChild >
100
- ) ;
101
- } else if ( child . type === 'HTML Element' ) {
102
- // if indirect child is an HTML element, render an IndirectChild component with no children
103
- // if the HTML element has children, then also render its children
104
- // get the default style/placeholder value for that type of HTML element
105
- // combine the default style of that HTML element and combine in with the custom styles applied to that element
106
- const HTMLType : HTMLType = state . HTMLTypes . find (
107
- ( type : HTMLType ) => type . id === child . typeId
108
- ) ;
109
- const HTMLDefaultStyle = HTMLType . style ;
110
- const HTMLDefaultPlaceholder = HTMLType . placeHolderShort ;
111
- const combinedStyle = combineStyles ( HTMLDefaultStyle , child . style ) ;
112
- return (
113
- < React . Fragment
114
- key = {
115
- 'indChildFrag' +
116
- child . childId . toString ( ) +
117
- child . typeId . toString ( )
118
- }
119
- >
120
- { child . children . length === 0 ? (
121
- < IndirectChild
122
- style = { combinedStyle }
123
- placeHolder = { HTMLDefaultPlaceholder }
124
- linkId = { null }
125
- key = {
126
- 'indChildHTML' +
127
- child . childId . toString ( ) +
128
- child . typeId . toString ( )
129
- }
130
- >
131
- { '' }
132
- </ IndirectChild >
133
- ) : (
134
- < IndirectChild
135
- style = { combinedStyle }
136
- placeHolder = { HTMLDefaultPlaceholder }
137
- linkId = { null }
138
- key = {
139
- 'indChildNest' +
140
- child . childId . toString ( ) +
141
- child . typeId . toString ( )
142
- }
143
- >
144
- { renderIndirectChildren ( child ) }
145
- </ IndirectChild >
146
- ) }
147
- </ React . Fragment >
148
- ) ;
149
- } else if ( child . type === 'Route Link' ) {
150
- // Render a next.js route link
151
- // pass the component id of the component referenced in the link as a prop
152
- // IndirectChild will render the referenced component name as a clickable link
153
- return (
154
- < IndirectChild
155
- key = {
156
- 'RouteLink' + child . childId . toString ( ) + child . typeId . toString ( )
157
- }
158
- style = { combinedStyle }
159
- placeHolder = ""
160
- linkId = { child . typeId }
161
- >
162
- { '' }
163
- </ IndirectChild >
164
- ) ;
165
- }
166
- } ) ;
167
- } ;
168
67
return (
169
68
< div
170
69
onClick = { onClickHandler }
171
- // key={'childComp' + childId}
172
70
style = { combinedStyle }
173
71
ref = { drag }
174
72
>
175
- { renderIndirectChildren ( referencedComponent ) }
73
+ { renderChildren ( referencedComponent . children ) }
176
74
</ div >
177
75
) ;
178
76
}
0 commit comments