Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mobile] - Enable font size and line height support and addresses Android issues #38205

Merged
merged 14 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default function LineHeightControl( { value: lineHeight, onChange } ) {
return (
<UnitControl
label={ __( 'Line Height' ) }
min={ 0 }
// Set minimun value of 1 since lower values break on Android
min={ 1 }
max={ 5 }
step={ STEP }
value={ value }
Expand Down
4 changes: 1 addition & 3 deletions packages/block-editor/src/hooks/typography.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export function TypographyPanel( props ) {
const isDisabled = useIsTypographyDisabled( props );
const isSupported = hasTypographySupport( props.name );

// only enable TypographyPanel for development
// eslint-disable-next-line no-undef
if ( isDisabled || ! isSupported || ! __DEV__ ) return null;
if ( isDisabled || ! isSupported ) return null;

return (
<InspectorControls>
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/post-title/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class PostTitle extends Component {
style={ titleStyles }
styles={ styles }
fontSize={ 24 }
lineHeight={ 1 }
fontWeight={ 'bold' }
deleteEnter={ true }
onChange={ ( value ) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-aztec/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
jSoupVersion = '1.10.3'
wordpressUtilsVersion = '1.22'
espressoVersion = '3.0.1'
aztecVersion = 'v1.5.3'
aztecVersion = '952-52a7e6bfccea37fcdfc1652918bb94163db87723'
willPublishReactNativeAztecBinary = properties["willPublishReactNativeAztecBinary"]?.toBoolean() ?: false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReactAztecManager extends BaseViewManager<ReactAztecText, LayoutShadowNode> {

Expand All @@ -80,6 +82,7 @@ public class ReactAztecManager extends BaseViewManager<ReactAztecText, LayoutSha
private static final String BLOCK_TYPE_TAG_KEY = "tag";
private static final String LINK_TEXT_COLOR_KEY = "linkTextColor";

private float mCurrentFontSize = 0;
private float mCurrentLineHeight = 0;

@Nullable private final Consumer<Exception> exceptionLogger;
Expand Down Expand Up @@ -267,6 +270,34 @@ private void updateSelectionIfNeeded(ReactAztecText view, @Nullable ReadableMap
}
}

private boolean isHeadingBlock(ReactAztecText view) {
String tag = view.getTagName();
final String regex = "h([1-6])";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(tag);

return matcher.find();
}

private float getHeadingScale(String scale) {
geriux marked this conversation as resolved.
Show resolved Hide resolved
switch (scale) {
case "h1":
return 1.73f;
case "h2":
return 1.32f;
case "h3":
return 1.02f;
case "h4":
return 0.87f;
case "h5":
return 0.72f;
case "h6":
return 0.60f;
}

return 1.0f;
}

@ReactProp(name = "activeFormats", defaultBoolean = false)
public void setActiveFormats(final ReactAztecText view, @Nullable ReadableArray activeFormats) {
if (activeFormats != null) {
Expand All @@ -285,19 +316,35 @@ public void setActiveFormats(final ReactAztecText view, @Nullable ReadableArray
*/
@ReactProp(name = ViewProps.FONT_SIZE, defaultFloat = ViewDefaults.FONT_SIZE_SP)
public void setFontSize(ReactAztecText view, float fontSize) {
float scale = 1;
boolean isLineHeightSet = mCurrentLineHeight != 0;
mCurrentFontSize = fontSize;

// Since Aztec applies a scale to the heading's font size
// we subtract it before applying the new font size.
if (isHeadingBlock(view) && isLineHeightSet) {
scale = getHeadingScale(view.getTagName());
}

view.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
(int) Math.ceil(PixelUtil.toPixelFromSP(fontSize)));
(int) Math.ceil(PixelUtil.toPixelFromSP(fontSize / scale)));

if (mCurrentLineHeight != 0) {
if (isLineHeightSet) {
setLineHeight(view, mCurrentLineHeight);
}
}

@ReactProp(name = ViewProps.LINE_HEIGHT)
public void setLineHeight(ReactAztecText view, float lineHeight) {
mCurrentLineHeight = lineHeight;
float textSize = view.getTextSize();
float scale = 1;

if (isHeadingBlock(view)) {
scale = getHeadingScale(view.getTagName());
}

float textSize = view.getTextSize() * scale;
view.setLineSpacing(textSize * lineHeight, (float) (lineHeight / textSize));
}

Expand Down Expand Up @@ -464,6 +511,12 @@ public void setSelectionColor(ReactAztecText view, @Nullable Integer color) {
public void setBlockType(ReactAztecText view, ReadableMap inputMap) {
if (inputMap.hasKey(BLOCK_TYPE_TAG_KEY)) {
view.setTagName(inputMap.getString(BLOCK_TYPE_TAG_KEY));

// Check if it's a heading block, this is needed to set the
// right font size scale.
if (isHeadingBlock(view)) {
setFontSize(view, mCurrentFontSize);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/react-native-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For each user feature we should also add a importance categorization label to i
## Unreleased

- [**] [Gallery block] Fix crash when adding images and selecting a gallery item [#38238]
- [***] Font size and line-height support for text-based blocks [#38205]

## 1.70.0

Expand Down
53 changes: 39 additions & 14 deletions packages/rich-text/src/component/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const gutenbergFormatNamesToAztec = {

const EMPTY_PARAGRAPH_TAGS = '<p></p>';
const DEFAULT_FONT_SIZE = 16;
const MIN_LINE_HEIGHT = 1;

export class RichText extends Component {
constructor( {
Expand Down Expand Up @@ -919,7 +920,7 @@ export class RichText extends Component {
let newFontSize = DEFAULT_FONT_SIZE;

// For block-based themes, get the default editor font size.
if ( baseGlobalStyles?.typography?.fontSize ) {
if ( baseGlobalStyles?.typography?.fontSize && tagName === 'p' ) {
newFontSize = baseGlobalStyles?.typography?.fontSize;
}

Expand Down Expand Up @@ -949,29 +950,54 @@ export class RichText extends Component {
}

getLineHeight() {
const { baseGlobalStyles, tagName } = this.props;
const { baseGlobalStyles, tagName, lineHeight, style } = this.props;
const tagNameLineHeight =
baseGlobalStyles?.elements?.[ tagName ]?.typography?.lineHeight;
let lineHeight;
let newLineHeight;

// eslint-disable-next-line no-undef
if ( ! __DEV__ ) {
if ( ! this.getIsBlockBasedTheme() ) {
return;
}

if ( baseGlobalStyles?.typography?.lineHeight ) {
lineHeight = parseFloat( baseGlobalStyles?.typography?.lineHeight );
// For block-based themes, get the default editor line height.
if ( baseGlobalStyles?.typography?.lineHeight && tagName === 'p' ) {
newLineHeight = parseFloat(
baseGlobalStyles?.typography?.lineHeight
);
}

// For block-based themes, get the default element line height
// e.g h1, h2.
if ( tagNameLineHeight ) {
lineHeight = parseFloat( tagNameLineHeight );
newLineHeight = parseFloat( tagNameLineHeight );
}

// For line height values provided from the styles,
// usually from values set from the line height picker.
if ( style?.lineHeight ) {
newLineHeight = parseFloat( style.lineHeight );
}

// Fall-back to a line height provided from its props (if there's any)
// and there are no other default values to use.
if ( lineHeight && ! tagNameLineHeight && ! style?.lineHeight ) {
newLineHeight = lineHeight;
}

if ( this.props.style?.lineHeight ) {
lineHeight = parseFloat( this.props.style.lineHeight );
// Check the final value is not over the minimum supported value.
if ( newLineHeight && newLineHeight < MIN_LINE_HEIGHT ) {
newLineHeight = MIN_LINE_HEIGHT;
}

return lineHeight;
return newLineHeight;
}

getIsBlockBasedTheme() {
const { baseGlobalStyles } = this.props;

return (
baseGlobalStyles && Object.entries( baseGlobalStyles ).length !== 0
);
}

getBlockUseDefaultFont() {
Expand All @@ -982,9 +1008,8 @@ export class RichText extends Component {
return;
}

const { baseGlobalStyles, tagName } = this.props;
const isBlockBasedTheme =
baseGlobalStyles && Object.entries( baseGlobalStyles ).length !== 0;
const { tagName } = this.props;
const isBlockBasedTheme = this.getIsBlockBasedTheme();
const tagsToMatch = /pre|h([1-6])$/gm;

return isBlockBasedTheme && tagsToMatch.test( tagName );
Expand Down
33 changes: 31 additions & 2 deletions packages/rich-text/src/test/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,28 @@ describe( '<RichText/>', () => {
expect( actualFontSize ).toBe( expectedFontSize );
} );

it( `should display rich text with the default editor font size value and not use the
\`default font size value from the global styles for a tag different than (p)`, () => {
// Arrange
const defaultFontSize = 16;
mockGlobalSettings( { fontSize: 'min(2em, 3em)' } );
// Act
const { getByA11yLabel } = render(
<RichText accessibilityLabel={ 'editor' } tagName="div" />
);
// Assert
const actualFontSize = getByA11yLabel( 'editor' ).props.fontSize;
expect( actualFontSize ).toBe( defaultFontSize );
} );

it( `should display rich text at the PROVIDED font size computed from the selected GLOBAL
\`__experimentalGlobalStylesBaseStyles.typography.fontSize\` CSS.`, () => {
// Arrange
const expectedFontSize = 32;
mockGlobalSettings( { fontSize: 'min(2em, 3em)' } );
// Act
const { getByA11yLabel } = render(
<RichText accessibilityLabel={ 'editor' } />
<RichText accessibilityLabel={ 'editor' } tagName="p" />
);
// Assert
const actualFontSize = getByA11yLabel( 'editor' ).props.fontSize;
Expand All @@ -144,7 +158,7 @@ describe( '<RichText/>', () => {
mockGlobalSettings( { fontSize: unit } );
// Act
const { getByA11yLabel } = render(
<RichText accessibilityLabel={ 'editor' } />
<RichText accessibilityLabel={ 'editor' } tagName="p" />
);
// Assert
const actualFontSize = getByA11yLabel( 'editor' ).props
Expand All @@ -164,6 +178,7 @@ describe( '<RichText/>', () => {
accessibilityLabel={ 'editor' }
style={ { fontSize: '1' } }
fontSize={ '2' }
tagName="p"
/>
);
// Assert
Expand All @@ -181,6 +196,7 @@ describe( '<RichText/>', () => {
<RichText
accessibilityLabel={ 'editor' }
style={ { fontSize: '1' } }
tagName="p"
/>
);
// Assert
Expand Down Expand Up @@ -246,5 +262,18 @@ describe( '<RichText/>', () => {
// Assert
expect( screen.toJSON() ).toMatchSnapshot();
} );

it( 'should set the default minimum line height value if the provided value from the styles is lower', () => {
// Arrange
const expectedLineHeight = 1;
const style = { lineHeight: 0.2 };
// Act
const { getByA11yLabel } = render(
<RichText accessibilityLabel={ 'editor' } style={ style } />
);
// Assert
const actualFontSize = getByA11yLabel( 'editor' ).props.lineHeight;
expect( actualFontSize ).toBe( expectedLineHeight );
} );
} );
} );