2017-04-21 14:05:35 -04:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-23 11:15:17 -04:00
|
|
|
|
2016-11-18 09:36:16 -05:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2023-05-23 11:15:17 -04:00
|
|
|
|
2017-05-28 13:15:35 -04:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2023-05-23 11:15:17 -04:00
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2024-01-25 10:41:31 -05:00
|
|
|
import PhotoLibraryIcon from '@/material-icons/400-24px/photo_library.svg?react';
|
|
|
|
import { IconButton } from 'mastodon/components/icon_button';
|
2016-11-18 09:36:16 -05:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
2020-06-25 18:14:29 -04:00
|
|
|
upload: { id: 'upload_button.label', defaultMessage: 'Add images, a video or an audio file' },
|
2016-11-18 09:36:16 -05:00
|
|
|
});
|
2016-09-07 12:17:15 -04:00
|
|
|
|
2017-05-08 10:49:53 -04:00
|
|
|
const makeMapStateToProps = () => {
|
2017-06-23 10:05:04 -04:00
|
|
|
const mapStateToProps = state => ({
|
2017-05-28 13:15:35 -04:00
|
|
|
acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']),
|
2017-05-08 10:49:53 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return mapStateToProps;
|
2017-05-20 11:31:47 -04:00
|
|
|
};
|
2017-04-23 08:18:58 -04:00
|
|
|
|
|
|
|
const iconStyle = {
|
|
|
|
height: null,
|
2017-05-20 11:31:47 -04:00
|
|
|
lineHeight: '27px',
|
|
|
|
};
|
2017-04-23 08:18:58 -04:00
|
|
|
|
2018-09-14 11:59:48 -04:00
|
|
|
class UploadButton extends ImmutablePureComponent {
|
2016-09-07 12:17:15 -04:00
|
|
|
|
2017-05-12 08:44:10 -04:00
|
|
|
static propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onSelectFile: PropTypes.func.isRequired,
|
|
|
|
style: PropTypes.object,
|
|
|
|
resetFileKey: PropTypes.number,
|
2017-05-28 13:15:35 -04:00
|
|
|
acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
|
2017-05-20 11:31:47 -04:00
|
|
|
intl: PropTypes.object.isRequired,
|
2017-05-12 08:44:10 -04:00
|
|
|
};
|
2016-09-07 12:17:15 -04:00
|
|
|
|
2017-05-12 08:44:10 -04:00
|
|
|
handleChange = (e) => {
|
2016-09-07 12:17:15 -04:00
|
|
|
if (e.target.files.length > 0) {
|
|
|
|
this.props.onSelectFile(e.target.files);
|
|
|
|
}
|
2023-01-29 19:45:35 -05:00
|
|
|
};
|
2016-09-07 12:17:15 -04:00
|
|
|
|
2017-05-12 08:44:10 -04:00
|
|
|
handleClick = () => {
|
2016-11-13 13:08:52 -05:00
|
|
|
this.fileElement.click();
|
2023-01-29 19:45:35 -05:00
|
|
|
};
|
2016-11-13 13:08:52 -05:00
|
|
|
|
2017-05-12 08:44:10 -04:00
|
|
|
setRef = (c) => {
|
2016-11-13 13:08:52 -05:00
|
|
|
this.fileElement = c;
|
2023-01-29 19:45:35 -05:00
|
|
|
};
|
2016-09-07 12:17:15 -04:00
|
|
|
|
|
|
|
render () {
|
2024-01-25 10:41:31 -05:00
|
|
|
const { intl, resetFileKey, disabled, acceptContentTypes } = this.props;
|
2016-11-16 11:20:52 -05:00
|
|
|
|
2020-06-25 18:14:29 -04:00
|
|
|
const message = intl.formatMessage(messages.upload);
|
|
|
|
|
2016-09-07 12:17:15 -04:00
|
|
|
return (
|
2017-04-22 22:26:55 -04:00
|
|
|
<div className='compose-form__upload-button'>
|
2024-01-25 10:41:31 -05:00
|
|
|
<IconButton icon='paperclip' iconComponent={PhotoLibraryIcon} title={message} disabled={disabled} onClick={this.handleClick} className='compose-form__upload-button-icon' size={18} inverted style={iconStyle} />
|
2017-07-27 18:54:48 -04:00
|
|
|
<label>
|
2020-06-25 18:14:29 -04:00
|
|
|
<span style={{ display: 'none' }}>{message}</span>
|
2017-07-27 18:54:48 -04:00
|
|
|
<input
|
|
|
|
key={resetFileKey}
|
|
|
|
ref={this.setRef}
|
|
|
|
type='file'
|
2024-02-08 14:13:44 -05:00
|
|
|
name='file-upload-input'
|
2019-02-02 14:22:05 -05:00
|
|
|
multiple
|
2017-07-27 18:54:48 -04:00
|
|
|
accept={acceptContentTypes.toArray().join(',')}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
disabled={disabled}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
|
|
|
</label>
|
2016-09-07 12:17:15 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-21 14:05:35 -04:00
|
|
|
}
|
2023-03-23 22:17:53 -04:00
|
|
|
|
|
|
|
export default connect(makeMapStateToProps)(injectIntl(UploadButton));
|