@@ -112,6 +112,45 @@ export function addSaveProps( extraProps, blockType, attributes ) {
112
112
return extraProps ;
113
113
}
114
114
115
+ export function addTransforms ( result , source , index , results ) {
116
+ if ( ! hasBlockSupport ( result . name , 'customClassName' , true ) ) {
117
+ return result ;
118
+ }
119
+
120
+ // If the condition verifies we are probably in the presence of a wrapping transform
121
+ // e.g: nesting paragraphs in a group or columns and in that case the class should not be kept.
122
+ if ( results . length === 1 && result . innerBlocks . length === source . length ) {
123
+ return result ;
124
+ }
125
+
126
+ // If we are transforming one block to multiple blocks or multiple blocks to one block,
127
+ // we ignore the class during the transform.
128
+ if (
129
+ ( results . length === 1 && source . length > 1 ) ||
130
+ ( results . length > 1 && source . length === 1 )
131
+ ) {
132
+ return result ;
133
+ }
134
+
135
+ // If we are in presence of transform between one or more block in the source
136
+ // that have one or more blocks in the result
137
+ // we apply the class on source N to the result N,
138
+ // if source N does not exists we do nothing.
139
+ if ( source [ index ] ) {
140
+ const originClassName = source [ index ] ?. attributes . className ;
141
+ if ( originClassName ) {
142
+ return {
143
+ ...result ,
144
+ attributes : {
145
+ ...result . attributes ,
146
+ className : originClassName ,
147
+ } ,
148
+ } ;
149
+ }
150
+ }
151
+ return result ;
152
+ }
153
+
115
154
addFilter (
116
155
'blocks.registerBlockType' ,
117
156
'core/custom-class-name/attribute' ,
@@ -127,3 +166,9 @@ addFilter(
127
166
'core/custom-class-name/save-props' ,
128
167
addSaveProps
129
168
) ;
169
+
170
+ addFilter (
171
+ 'blocks.switchToBlockType.transformedBlock' ,
172
+ 'core/color/addTransforms' ,
173
+ addTransforms
174
+ ) ;
0 commit comments