forked from Ivasoft/mattermost-mobile
* update eslint's `comma-dangle` rule to `always-multiline` * add check and fix scripts to package.json * Invoke `yarn fix` to adopt the updated eslint rules. No other changes are included.
119 lines
3.4 KiB
JavaScript
119 lines
3.4 KiB
JavaScript
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
import PropTypes from 'prop-types';
|
|
import React from 'react';
|
|
import {
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
} from 'react-native';
|
|
|
|
import {getCodeFont} from 'app/utils/markdown';
|
|
import {changeOpacity, makeStyleSheetFromTheme, setNavigatorStyles} from 'app/utils/theme';
|
|
|
|
export default class Code extends React.PureComponent {
|
|
static propTypes = {
|
|
navigator: PropTypes.object.isRequired,
|
|
theme: PropTypes.object.isRequired,
|
|
content: PropTypes.string.isRequired,
|
|
};
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
if (this.props.theme !== nextProps.theme) {
|
|
setNavigatorStyles(this.props.navigator, nextProps.theme);
|
|
}
|
|
}
|
|
|
|
countLines = (content) => {
|
|
return content.split('\n').length;
|
|
};
|
|
|
|
render() {
|
|
const style = getStyleSheet(this.props.theme);
|
|
|
|
const numberOfLines = this.countLines(this.props.content);
|
|
let lineNumbers = '1';
|
|
for (let i = 1; i < numberOfLines; i++) {
|
|
const line = (i + 1).toString();
|
|
|
|
lineNumbers += '\n' + line;
|
|
}
|
|
|
|
let lineNumbersStyle;
|
|
if (numberOfLines >= 10) {
|
|
lineNumbersStyle = [style.lineNumbers, style.lineNumbersRight];
|
|
} else {
|
|
lineNumbersStyle = style.lineNumbers;
|
|
}
|
|
|
|
return (
|
|
<ScrollView
|
|
style={style.scrollContainer}
|
|
contentContainerStyle={style.container}
|
|
>
|
|
<View style={lineNumbersStyle}>
|
|
<Text style={style.lineNumbersText}>
|
|
{lineNumbers}
|
|
</Text>
|
|
</View>
|
|
<ScrollView
|
|
style={style.codeContainer}
|
|
contentContainerStyle={style.code}
|
|
horizontal={true}
|
|
>
|
|
<Text style={style.codeText}>
|
|
{this.props.content}
|
|
</Text>
|
|
</ScrollView>
|
|
</ScrollView>
|
|
);
|
|
}
|
|
}
|
|
|
|
const getStyleSheet = makeStyleSheetFromTheme((theme) => {
|
|
return {
|
|
scrollContainer: {
|
|
flex: 1,
|
|
},
|
|
container: {
|
|
minHeight: '100%',
|
|
flexDirection: 'row',
|
|
},
|
|
lineNumbers: {
|
|
alignItems: 'center',
|
|
backgroundColor: changeOpacity(theme.centerChannelColor, 0.05),
|
|
borderRightColor: changeOpacity(theme.centerChannelColor, 0.15),
|
|
borderRightWidth: StyleSheet.hairlineWidth,
|
|
flexDirection: 'column',
|
|
justifyContent: 'flex-start',
|
|
paddingHorizontal: 6,
|
|
paddingVertical: 4,
|
|
},
|
|
lineNumbersRight: {
|
|
alignItems: 'flex-end',
|
|
},
|
|
lineNumbersText: {
|
|
color: changeOpacity(theme.centerChannelColor, 0.5),
|
|
fontSize: 12,
|
|
lineHeight: 18,
|
|
},
|
|
codeContainer: {
|
|
flexGrow: 0,
|
|
flexShrink: 1,
|
|
width: '100%',
|
|
},
|
|
code: {
|
|
paddingHorizontal: 6,
|
|
paddingVertical: 4,
|
|
},
|
|
codeText: {
|
|
color: changeOpacity(theme.centerChannelColor, 0.65),
|
|
fontFamily: getCodeFont(),
|
|
fontSize: 12,
|
|
lineHeight: 18,
|
|
},
|
|
};
|
|
});
|