From 69a2a067ab784b9a56096823c2f18521f69ab6f5 Mon Sep 17 00:00:00 2001 From: Luke Walczak Date: Thu, 30 Apr 2020 11:12:48 +0200 Subject: [PATCH] Correct android font weight (#2141) Co-Authored-By: Drapich Piotr --- src/index.js | 2 + src/text-font-weight-correct.js | 77 +++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/text-font-weight-correct.js diff --git a/src/index.js b/src/index.js index ea05b3719d9ae..2b3edd9c04399 100644 --- a/src/index.js +++ b/src/index.js @@ -22,6 +22,7 @@ import './globals'; import { getTranslation } from '../i18n-cache'; import initialHtml from './initial-html'; import setupApiFetch from './api-fetch-setup'; +import correctTextFontWeight from './text-font-weight-correct'; import setupJetpackEditor from './jetpack-editor-setup'; const gutenbergSetup = () => { @@ -56,6 +57,7 @@ export class RootComponent extends React.Component { super( props ); setupLocale( props.locale, props.translations ); setupApiFetch(); + correctTextFontWeight(); setupJetpackEditor( props.jetpackState || { blogId: 1, isJetpackActive: true } ); require( '@wordpress/edit-post' ).initializeEditor(); diff --git a/src/text-font-weight-correct.js b/src/text-font-weight-correct.js new file mode 100644 index 0000000000000..7c3fd342af90b --- /dev/null +++ b/src/text-font-weight-correct.js @@ -0,0 +1,77 @@ +/** + * External dependencies + * + * @format + */ + +/** + * External dependencies + */ +import { Platform, Text } from 'react-native'; +import { merge } from 'lodash'; +/** + * WordPress dependencies + */ +import { cloneElement } from '@wordpress/element'; + +const textRender = Text.render; + +const getCorrectFontWeight = ( fontWeight ) => { + switch ( fontWeight ) { + case '100': + case '200': + case '300': + return { + fontFamily: 'sans-serif-thin', + fontWeight: 'normal', + }; + case '400': + return { + fontFamily: 'sans-serif-light', + fontWeight: 'normal', + }; + case '500': + case 'normal': + return { + fontFamily: 'sans-serif', + fontWeight: 'normal', + }; + case '600': + return { + fontFamily: 'sans-serif-medium', + fontWeight: 'normal', + }; + case '700': + case 'bold': + return { + fontFamily: 'sans-serif', + fontWeight: 'bold', + }; + case '800': + case '900': + return { + fontFamily: 'sans-serif-medium', + fontWeight: 'bold', + }; + } +}; + +const correctTextFontWeight = ( args ) => { + const baseText = textRender.call( this, args ); + const { style } = baseText.props; + + const flatStyle = Array.isArray( style ) ? merge( {}, ...style ) : style; + const shouldCorrectFontWeight = flatStyle && flatStyle.fontWeight; + + return shouldCorrectFontWeight ? + cloneElement( baseText, { + style: [ flatStyle, getCorrectFontWeight( flatStyle.fontWeight ) ], + } ) : + baseText; +}; + +export default () => { + if ( Platform.OS === 'android' ) { + Text.render = correctTextFontWeight; + } +};