import React from 'react'; import { FormattedMessage } from 'react-intl'; import SendMessage from '../widgets/send-message.jsx'; import { REM_SIZE } from '../config.js'; import { fitImageSize } from '../lib/blob-helpers.js'; import { bytesToHumanSize, shortenFileName } from '../lib/strformat.js'; export default class ImagePreview extends React.PureComponent { constructor(props) { super(props); this.state = { width: 0, height: 0 }; this.handleSendImage = this.handleSendImage.bind(this); } assignWidth(node) { if (node && !this.state.width) { const bounds = node.getBoundingClientRect(); this.setState({ width: bounds.width | 0, height: bounds.height | 0 }); } } handleSendImage(caption) { this.props.onClose(); this.props.onSendMessage(caption, this.props.content.blob); } render() { if (!this.props.content) { return null; } const dim = fitImageSize(this.props.content.width, this.props.content.height, this.state.width, this.state.height, false); const size = dim ? { width: dim.dstWidth + 'px', height: dim.dstHeight + 'px' } : ((this.props.content.width > this.props.content.height) ? {width: '100%'} : {height: '100%'}); size.maxWidth = '100%'; size.maxHeight = '100%'; // Average font aspect ratio is ~0.5; File name takes 1/3 of the viewport width. const maxlength = Math.max(((this.state.width / REM_SIZE / 1.5) | 0) - 2, 12); const fname = shortenFileName(this.props.content.name, maxlength) || '-'; const width = this.props.content.width || '-'; const height = this.props.content.height || '-'; return (
{!this.props.onSendMessage ? file_download : {fname} } {e.preventDefault(); this.props.onClose();}}>close
this.assignWidth(node)}> {this.props.content.name}
{this.props.onSendMessage ? : }
); } };