Skip to content

Commit 247e5cf

Browse files
t-hamanojorgefilipecostamcsf
authored
Disable font size when fit text is enabled and the opposite and add e2e tests (#72584)
* Add end to end tests for Fit Text (#72406) Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org> Co-authored-by: mcsf <mcsf@git.wordpress.org> * Update: Disable font size when fit text is enabled and the opposite. (#72533) Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> --------- Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org> Co-authored-by: mcsf <mcsf@git.wordpress.org>
1 parent 6fcd212 commit 247e5cf

File tree

3 files changed

+539
-8
lines changed

3 files changed

+539
-8
lines changed

packages/block-editor/src/hooks/fit-text.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,16 @@ function useFitText( { fitText, name, clientId } ) {
180180
* @param {Function} props.setAttributes Function to set block attributes.
181181
* @param {string} props.name Block name.
182182
* @param {boolean} props.fitText Whether fit text is enabled.
183+
* @param {string} props.fontSize Font size slug.
184+
* @param {Object} props.style Block style object.
183185
*/
184186
export function FitTextControl( {
185187
clientId,
186188
fitText = false,
187189
setAttributes,
188190
name,
191+
fontSize,
192+
style,
189193
} ) {
190194
if ( ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {
191195
return null;
@@ -203,9 +207,28 @@ export function FitTextControl( {
203207
__nextHasNoMarginBottom
204208
label={ __( 'Fit text' ) }
205209
checked={ fitText }
206-
onChange={ () =>
207-
setAttributes( { fitText: ! fitText || undefined } )
208-
}
210+
onChange={ () => {
211+
const newFitText = ! fitText || undefined;
212+
const updates = { fitText: newFitText };
213+
214+
// When enabling fit text, clear font size if it has a value
215+
if ( newFitText ) {
216+
if ( fontSize ) {
217+
updates.fontSize = undefined;
218+
}
219+
if ( style?.typography?.fontSize ) {
220+
updates.style = {
221+
...style,
222+
typography: {
223+
...style?.typography,
224+
fontSize: undefined,
225+
},
226+
};
227+
}
228+
}
229+
230+
setAttributes( updates );
231+
} }
209232
help={
210233
fitText
211234
? __( 'Text will resize to fit its container.' )
@@ -278,7 +301,7 @@ const hasFitTextSupport = ( blockNameOrType ) => {
278301
export default {
279302
useBlockProps,
280303
addSaveProps,
281-
attributeKeys: [ 'fitText' ],
304+
attributeKeys: [ 'fitText', 'fontSize', 'style' ],
282305
hasSupport: hasFitTextSupport,
283306
edit: FitTextControl,
284307
};

packages/block-editor/src/hooks/typography.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,30 @@ function TypographyInspectorControl( { children, resetAllFilter } ) {
116116

117117
export function TypographyPanel( { clientId, name, setAttributes, settings } ) {
118118
function selector( select ) {
119-
const { style, fontFamily, fontSize } =
119+
const { style, fontFamily, fontSize, fitText } =
120120
select( blockEditorStore ).getBlockAttributes( clientId ) || {};
121-
return { style, fontFamily, fontSize };
121+
return { style, fontFamily, fontSize, fitText };
122122
}
123-
const { style, fontFamily, fontSize } = useSelect( selector, [ clientId ] );
123+
const { style, fontFamily, fontSize, fitText } = useSelect( selector, [
124+
clientId,
125+
] );
124126
const isEnabled = useHasTypographyPanel( settings );
125127
const value = useMemo(
126128
() => attributesToStyle( { style, fontFamily, fontSize } ),
127129
[ style, fontSize, fontFamily ]
128130
);
129131

130132
const onChange = ( newStyle ) => {
131-
setAttributes( styleToAttributes( newStyle ) );
133+
const newAttributes = styleToAttributes( newStyle );
134+
135+
// If setting a font size and fitText is currently enabled, disable it
136+
const hasFontSize =
137+
newAttributes.fontSize || newAttributes.style?.typography?.fontSize;
138+
if ( hasFontSize && fitText ) {
139+
newAttributes.fitText = undefined;
140+
}
141+
142+
setAttributes( newAttributes );
132143
};
133144

134145
if ( ! isEnabled ) {

0 commit comments

Comments
 (0)