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

fix inline code blocks for native platforms #2307

Merged
merged 10 commits into from
Apr 20, 2021
7 changes: 4 additions & 3 deletions src/components/AnchorForCommentsOnly/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {StyleSheet} from 'react-native';
import {StyleSheet, Text} from 'react-native';
import anchorForCommentsOnlyPropTypes from './anchorForCommentsOnlyPropTypes';

const defaultProps = {
Expand All @@ -18,16 +18,17 @@ const AnchorForCommentsOnly = ({
style,
...props
}) => (
<a
<Text
style={StyleSheet.flatten(style)}
accessibilityRole="link"
href={href}
rel={rel}
target={target}
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
>
{children}
</a>
</Text>
);

AnchorForCommentsOnly.propTypes = anchorForCommentsOnlyPropTypes;
Expand Down
19 changes: 0 additions & 19 deletions src/components/InlineCodeBlock/index.android.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
/* eslint-disable react/jsx-props-no-spreading */
import React from 'react';
import {View} from 'react-native';
import styles from '../../styles/styles';
import WrappedText from './wrappedText';
import inlineCodeBlockPropTypes from './inlineCodeBlockPropTypes';

const InlineCodeBlock = ({
TDefaultRenderer,
defaultRendererProps,
boxModelStyle,
textStyle,
}) => (
<View
style={{
<WrappedText
textStyle={textStyle}
wordStyle={{
...boxModelStyle,
...styles.mbn1,
...styles.codeWordStyle,
}}
firstWordStyle={styles.codeFirstWordStyle}
lastWordStyle={styles.codeLastWordStyle}
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultRendererProps}
>
<TDefaultRenderer style={textStyle} {...defaultRendererProps} />
</View>
{defaultRendererProps.tnode.data}
</WrappedText>
);

InlineCodeBlock.propTypes = inlineCodeBlockPropTypes;
Expand Down
83 changes: 83 additions & 0 deletions src/components/InlineCodeBlock/wrappedText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React, {Fragment} from 'react';
import {Text, View} from 'react-native';
import PropTypes from 'prop-types';
import styles from '../../styles/styles';

/**
* Breaks the text into matrix
* for eg: My Name is Rajat
* [
* [My,'',Name,'','',is,'',Rajat],
* ]
*
* @param {String} text
* @returns {Array<String[]>}
*/
function getTextMatrix(text) {
return text.split('\n').map(row => row.split(/(\s)/));
}
parasharrajat marked this conversation as resolved.
Show resolved Hide resolved
const propTypes = {
// Required text
children: PropTypes.string.isRequired,

// Style to be applied to Text
// eslint-disable-next-line react/forbid-prop-types
textStyle: PropTypes.object,

// Style for each word(Token) in the text,
// remember that token also includes the following spaces before next word break
// eslint-disable-next-line react/forbid-prop-types
wordStyle: PropTypes.object,

// Style for first word
// eslint-disable-next-line react/forbid-prop-types
firstWordStyle: PropTypes.object,

// Style for last word
// eslint-disable-next-line react/forbid-prop-types
lastWordStyle: PropTypes.object,
};
parasharrajat marked this conversation as resolved.
Show resolved Hide resolved
const defaultProps = {
textStyle: {},
wordStyle: {},
firstWordStyle: {},
lastWordStyle: {},
};
parasharrajat marked this conversation as resolved.
Show resolved Hide resolved
const WrappedText = (props) => {
const textMatrix = getTextMatrix(props.children);
return (
<>
{textMatrix.map((rowText, rowIndex) => (
<Fragment
// eslint-disable-next-line react/no-array-index-key
key={`${rowText}-${rowIndex}`}
>
{rowText.map((colText, colIndex) => (

// Outer View is important to vertically center the Text
<View
// eslint-disable-next-line react/no-array-index-key
key={`${colText}-${colIndex}`}
style={styles.codeWordWrapper}
>
<View
style={[
props.wordStyle,
colIndex === 0 && props.firstWordStyle,
colIndex === rowText.length - 1 && props.lastWordStyle,
]}
>
<Text style={props.textStyle}>{colText}</Text>
</View>
</View>
))}
</Fragment>
))}
</>
);
};
parasharrajat marked this conversation as resolved.
Show resolved Hide resolved
WrappedText.propTypes = propTypes;
WrappedText.defaultProps = defaultProps;
WrappedText.displayName = 'WrappedText';

export default WrappedText;
41 changes: 37 additions & 4 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,39 @@ const styles = {
scrollbarWidth: 'none',
},

codeWordWrapper: {
height: 10,
},

codeWordStyle: {
borderLeftWidth: 0,
borderRightWidth: 0,
borderTopLeftRadius: 0,
borderBottomLeftRadius: 0,
borderTopRightRadius: 0,
borderBottomRightRadius: 0,
flexBasis: 'auto',
paddingLeft: 0,
paddingRight: 0,
justifyContent: 'center',
marginVertical: -2,
top: -1,
},

codeFirstWordStyle: {
borderLeftWidth: 1,
borderTopLeftRadius: 4,
borderBottomLeftRadius: 4,
paddingLeft: 5,
},

codeLastWordStyle: {
borderRightWidth: 1,
borderTopRightRadius: 4,
borderBottomRightRadius: 4,
paddingRight: 5,
},

fullScreenLoading: {
backgroundColor: themeColors.modalBackdrop,
opacity: 0.8,
Expand All @@ -1271,12 +1304,11 @@ const styles = {
const baseCodeTagStyles = {
borderWidth: 1,
borderRadius: 5,
marginTop: 4,
marginBottom: 4,
borderColor: themeColors.border,
backgroundColor: themeColors.textBackground,
};


parasharrajat marked this conversation as resolved.
Show resolved Hide resolved
const webViewStyles = {
// As of react-native-render-html v6, don't declare distinct styles for
// custom renderers, the API for custom renderers has changed. Declare the
Expand Down Expand Up @@ -1329,9 +1361,9 @@ const webViewStyles = {
...baseCodeTagStyles,
paddingLeft: 5,
paddingRight: 5,
paddingBottom: 2,
alignSelf: 'flex-start',
fontFamily: fontFamily.MONOSPACE,
lineHeight: 18,
fontSize: 13,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inline code block font size was being inherited. But after explicitly setting it to 13px, it's always rendered at 13px even if the parent is a H1 where the font size is expected to be a little bigger. (Coming from #30203)

},

img: {
Expand All @@ -1344,6 +1376,7 @@ const webViewStyles = {
baseFontStyle: {
color: themeColors.text,
fontSize: variables.fontSizeNormal,
lineHeight: variables.fontSizeNormalHeight,
fontFamily: fontFamily.GTA,
},
};
Expand Down
1 change: 1 addition & 0 deletions src/styles/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
fontSizeExtraSmall: 9,
fontSizeLabel: 13,
fontSizeNormal: 15,
fontSizeNormalHeight: 20,
fontSizeLarge: 17,
fontSizeh1: 19,
iconSizeExtraSmall: 12,
Expand Down