2017-05-03 01:04:16 +01:00
|
|
|
import React from 'react';
|
2016-11-13 18:08:52 +00:00
|
|
|
import IconButton from '../../../components/icon_button';
|
2017-04-21 19:05:35 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2016-11-18 14:36:16 +00:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2017-05-08 15:49:53 +01:00
|
|
|
import { connect } from 'react-redux';
|
2017-05-28 18:15:35 +01:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2016-11-18 14:36:16 +00:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
2019-06-19 22:42:38 +01:00
|
|
|
upload: { id: 'upload_button.label', defaultMessage: 'Add media ({formats})' },
|
2016-11-18 14:36:16 +00:00
|
|
|
});
|
2016-09-07 17:17:15 +01:00
|
|
|
|
2019-06-19 22:42:38 +01:00
|
|
|
const SUPPORTED_FORMATS = 'JPEG, PNG, GIF, WebM, MP4, MOV, OGG, WAV, MP3, FLAC';
|
|
|
|
|
2017-05-08 15:49:53 +01:00
|
|
|
const makeMapStateToProps = () => {
|
2017-06-23 15:05:04 +01:00
|
|
|
const mapStateToProps = state => ({
|
2017-05-28 18:15:35 +01:00
|
|
|
acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']),
|
2017-05-08 15:49:53 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return mapStateToProps;
|
2017-05-20 16:31:47 +01:00
|
|
|
};
|
2017-04-23 13:18:58 +01:00
|
|
|
|
|
|
|
const iconStyle = {
|
|
|
|
height: null,
|
2017-05-20 16:31:47 +01:00
|
|
|
lineHeight: '27px',
|
|
|
|
};
|
2017-04-23 13:18:58 +01:00
|
|
|
|
2018-09-14 16:59:48 +01:00
|
|
|
export default @connect(makeMapStateToProps)
|
2017-06-23 18:36:54 +01:00
|
|
|
@injectIntl
|
2018-09-14 16:59:48 +01:00
|
|
|
class UploadButton extends ImmutablePureComponent {
|
2016-09-07 17:17:15 +01:00
|
|
|
|
2017-05-12 13:44:10 +01:00
|
|
|
static propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
2019-03-06 03:53:37 +00:00
|
|
|
unavailable: PropTypes.bool,
|
2017-05-12 13:44:10 +01:00
|
|
|
onSelectFile: PropTypes.func.isRequired,
|
|
|
|
style: PropTypes.object,
|
|
|
|
resetFileKey: PropTypes.number,
|
2017-05-28 18:15:35 +01:00
|
|
|
acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
|
2017-05-20 16:31:47 +01:00
|
|
|
intl: PropTypes.object.isRequired,
|
2017-05-12 13:44:10 +01:00
|
|
|
};
|
2016-09-07 17:17:15 +01:00
|
|
|
|
2017-05-12 13:44:10 +01:00
|
|
|
handleChange = (e) => {
|
2016-09-07 17:17:15 +01:00
|
|
|
if (e.target.files.length > 0) {
|
|
|
|
this.props.onSelectFile(e.target.files);
|
|
|
|
}
|
2017-04-21 19:05:35 +01:00
|
|
|
}
|
2016-09-07 17:17:15 +01:00
|
|
|
|
2017-05-12 13:44:10 +01:00
|
|
|
handleClick = () => {
|
2016-11-13 18:08:52 +00:00
|
|
|
this.fileElement.click();
|
2017-04-21 19:05:35 +01:00
|
|
|
}
|
2016-11-13 18:08:52 +00:00
|
|
|
|
2017-05-12 13:44:10 +01:00
|
|
|
setRef = (c) => {
|
2016-11-13 18:08:52 +00:00
|
|
|
this.fileElement = c;
|
2017-04-21 19:05:35 +01:00
|
|
|
}
|
2016-09-07 17:17:15 +01:00
|
|
|
|
|
|
|
render () {
|
2019-03-06 03:53:37 +00:00
|
|
|
const { intl, resetFileKey, unavailable, disabled, acceptContentTypes } = this.props;
|
2017-04-23 13:18:58 +01:00
|
|
|
|
2019-03-06 03:53:37 +00:00
|
|
|
if (unavailable) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-11-16 16:20:52 +00:00
|
|
|
|
2016-09-07 17:17:15 +01:00
|
|
|
return (
|
2017-04-23 03:26:55 +01:00
|
|
|
<div className='compose-form__upload-button'>
|
2019-06-22 14:29:25 +01:00
|
|
|
<IconButton icon='paperclip' title={intl.formatMessage(messages.upload, { formats: SUPPORTED_FORMATS })} disabled={disabled} onClick={this.handleClick} className='compose-form__upload-button-icon' size={18} inverted style={iconStyle} />
|
2017-07-27 23:54:48 +01:00
|
|
|
<label>
|
2019-06-19 22:42:38 +01:00
|
|
|
<span style={{ display: 'none' }}>{intl.formatMessage(messages.upload, { formats: SUPPORTED_FORMATS })}</span>
|
2017-07-27 23:54:48 +01:00
|
|
|
<input
|
|
|
|
key={resetFileKey}
|
|
|
|
ref={this.setRef}
|
|
|
|
type='file'
|
2019-02-02 19:22:05 +00:00
|
|
|
multiple
|
2017-07-27 23:54:48 +01:00
|
|
|
accept={acceptContentTypes.toArray().join(',')}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
disabled={disabled}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
|
|
|
</label>
|
2016-09-07 17:17:15 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-21 19:05:35 +01:00
|
|
|
}
|