mattermost-mobile/app/screens/table_image/table_image.js
Harrison Healey ede4d22f75 ICU-624 Added markdown tables (#1434)
* ICU-624 Added initial table rendering support

* ICU-624 Added full table view

* Added proper pluralization to +X more lines in code blocks

* ICU-624 Added support for images in tables

* Stopped using injectIntl for tables

* ICU-624 Updated commonmark-react-renderer

* ICU-624 Hid scroll indicator in collapsed table view

* Addressed feedback
2018-02-16 19:31:14 -03:00

102 lines
2.3 KiB
JavaScript

// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import PropTypes from 'prop-types';
import React from 'react';
import {
ActivityIndicator,
Image,
ScrollView,
StyleSheet,
View
} from 'react-native';
export default class TableImage extends React.PureComponent {
static propTypes = {
deviceWidth: PropTypes.number.isRequired,
imageSource: PropTypes.string.isRequired
};
constructor(props) {
super(props);
this.state = {
width: -1,
height: -1
};
}
componentWillMount() {
this.getImageSize(this.props.imageSource);
}
componentWillReceiveProps(nextProps) {
if (this.props.imageSource !== nextProps.imageSource) {
this.setState({
width: -1,
height: -1
});
this.getImageSize(nextProps.imageSource);
}
}
getImageSize = (imageSource) => {
Image.getSize(imageSource, (width, height) => {
this.setState({
width,
height
});
});
}
render() {
let {width, height} = this.state;
if (width === -1 || height === -1) {
return (
<View style={style.loadingContainer}>
<ActivityIndicator
animating={true}
size='large'
/>
</View>
);
}
if (width > this.props.deviceWidth) {
height = (height / width) * this.props.deviceWidth;
width = this.props.deviceWidth;
}
return (
<ScrollView
style={style.scrollContainer}
contentContainerStyle={style.container}
>
<Image
style={[style.image, {width, height}]}
source={{uri: this.props.imageSource}}
/>
</ScrollView>
);
}
}
const style = StyleSheet.create({
scrollContainer: {
flex: 1
},
container: {
flex: 1,
flexDirection: 'column'
},
image: {
resizeMode: 'contain'
},
loadingContainer: {
alignItems: 'center',
flex: 1,
justifyContent: 'center'
}
});