Skip to content

Commit 8c3ca76

Browse files
Update: Keep additional classes during a block transform. (WordPress#38964)
1 parent 5c980de commit 8c3ca76

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

packages/block-editor/src/hooks/custom-class-name.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,45 @@ export function addSaveProps( extraProps, blockType, attributes ) {
112112
return extraProps;
113113
}
114114

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+
115154
addFilter(
116155
'blocks.registerBlockType',
117156
'core/custom-class-name/attribute',
@@ -127,3 +166,9 @@ addFilter(
127166
'core/custom-class-name/save-props',
128167
addSaveProps
129168
);
169+
170+
addFilter(
171+
'blocks.switchToBlockType.transformedBlock',
172+
'core/color/addTransforms',
173+
addTransforms
174+
);

0 commit comments

Comments
 (0)