Skip to content

Commit

Permalink
Merge pull request #2527 from parasharrajat/parasharrajat/quotedText
Browse files Browse the repository at this point in the history
fix inline code blocks
  • Loading branch information
jasperhuangg authored Apr 30, 2021
2 parents 0a30554 + b94e2ba commit a656a1e
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 36 deletions.
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
76 changes: 76 additions & 0 deletions src/components/InlineCodeBlock/WrappedText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import _ from 'underscore';
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 => _.without(row.split(/(\s)/), ''));
}

const propTypes = {
// Required text
children: PropTypes.string.isRequired,

// Style to be applied to Text
textStyles: PropTypes.arrayOf(PropTypes.object),

// Style for each word(Token) in the text,
// remember that token also includes whitespaces among words
wordStyles: PropTypes.arrayOf(PropTypes.object),
};

const defaultProps = {
textStyles: [],
wordStyles: [],
};

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.wordStyles,
colIndex === 0 && styles.codeFirstWordStyle,
colIndex === rowText.length - 1 && styles.codeLastWordStyle,
]}
>
<Text style={props.textStyles}>{colText}</Text>
</View>
</View>
))}
</Fragment>
))}
</>
);
};

WrappedText.propTypes = propTypes;
WrappedText.defaultProps = defaultProps;
WrappedText.displayName = 'WrappedText';

export default WrappedText;
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,24 @@
/* 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={{
...boxModelStyle,
...styles.mbn1,
}}
<WrappedText
textStyles={[textStyle]}
wordStyles={[
boxModelStyle,
styles.codeWordStyle,
]}
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultRendererProps}
>
<TDefaultRenderer style={textStyle} {...defaultRendererProps} />
</View>
{defaultRendererProps.tnode.data}
</WrappedText>
);

InlineCodeBlock.propTypes = inlineCodeBlockPropTypes;
Expand Down
14 changes: 14 additions & 0 deletions src/styles/codeStyles/index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const codeWordWrapper = {
height: 10,
};

const codeWordStyle = {
top: -1,
marginVertical: -2,
};

const codeTextStyle = {
lineHeight: 18,
};

export default {codeWordWrapper, codeWordStyle, codeTextStyle};
16 changes: 16 additions & 0 deletions src/styles/codeStyles/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const codeWordWrapper = {
height: 18,
justifyContent: 'center',
marginTop: 2,
};

const codeWordStyle = {
height: 16,
top: 4,
};

const codeTextStyle = {
lineHeight: 15,
};

export default {codeWordWrapper, codeWordStyle, codeTextStyle};
5 changes: 5 additions & 0 deletions src/styles/codeStyles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// We do not need these on Web/Desktop as their implementation defer from Native devices so just noop them
const codeWordWrapper = {};
const codeWordStyle = {};
const codeTextStyle = {};
export default {codeWordWrapper, codeWordStyle, codeTextStyle};
39 changes: 35 additions & 4 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import wordBreak from './utilities/wordBreak';
import textInputAlignSelf from './utilities/textInputAlignSelf';
import CONST from '../CONST';
import positioning from './utilities/positioning';
import codeStyles from './codeStyles';

const styles = {
// Add all of our utility and helper styles
Expand Down Expand Up @@ -1343,6 +1344,37 @@ const styles = {
scrollbarWidth: 'none',
},

codeWordWrapper: {
...codeStyles.codeWordWrapper,
},

codeWordStyle: {
borderLeftWidth: 0,
borderRightWidth: 0,
borderTopLeftRadius: 0,
borderBottomLeftRadius: 0,
borderTopRightRadius: 0,
borderBottomRightRadius: 0,
paddingLeft: 0,
paddingRight: 0,
justifyContent: 'center',
...codeStyles.codeWordStyle,
},

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

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

fullScreenLoading: {
backgroundColor: themeColors.componentBG,
opacity: 0.8,
Expand All @@ -1363,8 +1395,6 @@ const styles = {
const baseCodeTagStyles = {
borderWidth: 1,
borderRadius: 5,
marginTop: 4,
marginBottom: 4,
borderColor: themeColors.border,
backgroundColor: themeColors.textBackground,
};
Expand Down Expand Up @@ -1419,11 +1449,11 @@ const webViewStyles = {

code: {
...baseCodeTagStyles,
...codeStyles.codeTextStyle,
paddingLeft: 5,
paddingRight: 5,
paddingBottom: 2,
alignSelf: 'flex-start',
fontFamily: fontFamily.MONOSPACE,
fontSize: 13,
},

img: {
Expand All @@ -1436,6 +1466,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 @@ -16,6 +16,7 @@ export default {
fontSizeLarge: 17,
fontSizeh1: 19,
iconSizeExtraSmall: 12,
fontSizeNormalHeight: 20,
iconSizeSmall: 16,
iconSizeNormal: 20,
iconSizeLarge: 24,
Expand Down

0 comments on commit a656a1e

Please sign in to comment.