2024-02-23 06:04:09 +09:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2024-02-24 00:32:13 +09:00
|
|
|
import PhotoLibraryIcon from '@/material-icons/400-20px/photo_library.svg?react';
|
2024-02-28 05:27:51 +09:00
|
|
|
import BrushIcon from '@/material-icons/400-24px/brush.svg?react';
|
|
|
|
import UploadFileIcon from '@/material-icons/400-24px/upload_file.svg?react';
|
|
|
|
|
|
|
|
import { DropdownIconButton } from './dropdown_icon_button';
|
2024-02-23 06:04:09 +09:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
upload: { id: 'upload_button.label', defaultMessage: 'Add images, a video or an audio file' },
|
2024-02-28 05:27:51 +09:00
|
|
|
doodle: { id: 'compose.attach.doodle', defaultMessage: 'Draw something' },
|
2024-02-23 06:04:09 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
const makeMapStateToProps = () => {
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
acceptContentTypes: state.getIn(['media_attachments', 'accept_content_types']),
|
|
|
|
});
|
|
|
|
|
|
|
|
return mapStateToProps;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UploadButton extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onSelectFile: PropTypes.func.isRequired,
|
2024-02-28 05:27:51 +09:00
|
|
|
onDoodleOpen: PropTypes.func.isRequired,
|
2024-02-23 06:04:09 +09:00
|
|
|
style: PropTypes.object,
|
|
|
|
resetFileKey: PropTypes.number,
|
|
|
|
acceptContentTypes: ImmutablePropTypes.listOf(PropTypes.string).isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleChange = (e) => {
|
|
|
|
if (e.target.files.length > 0) {
|
|
|
|
this.props.onSelectFile(e.target.files);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-28 05:27:51 +09:00
|
|
|
handleSelect = (value) => {
|
|
|
|
if (value === 'upload') {
|
|
|
|
this.fileElement.click();
|
|
|
|
} else {
|
|
|
|
this.props.onDoodleOpen();
|
|
|
|
}
|
2024-02-23 06:04:09 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
setRef = (c) => {
|
|
|
|
this.fileElement = c;
|
|
|
|
};
|
|
|
|
|
|
|
|
render () {
|
2024-01-26 00:41:31 +09:00
|
|
|
const { intl, resetFileKey, disabled, acceptContentTypes } = this.props;
|
2024-02-23 06:04:09 +09:00
|
|
|
|
|
|
|
const message = intl.formatMessage(messages.upload);
|
|
|
|
|
2024-02-28 05:27:51 +09:00
|
|
|
const options = [
|
|
|
|
{
|
|
|
|
icon: 'cloud-upload',
|
|
|
|
iconComponent: UploadFileIcon,
|
|
|
|
value: 'upload',
|
|
|
|
text: intl.formatMessage(messages.upload),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: 'paint-brush',
|
|
|
|
iconComponent: BrushIcon,
|
|
|
|
value: 'doodle',
|
|
|
|
text: intl.formatMessage(messages.doodle),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-02-23 06:04:09 +09:00
|
|
|
return (
|
|
|
|
<div className='compose-form__upload-button'>
|
2024-02-28 05:27:51 +09:00
|
|
|
<DropdownIconButton
|
|
|
|
icon='paperclip'
|
|
|
|
iconComponent={PhotoLibraryIcon}
|
|
|
|
title={message}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={this.handleSelect}
|
|
|
|
value='upload'
|
|
|
|
options={options}
|
|
|
|
/>
|
2024-02-23 06:04:09 +09:00
|
|
|
<label>
|
|
|
|
<span style={{ display: 'none' }}>{message}</span>
|
|
|
|
<input
|
|
|
|
key={resetFileKey}
|
|
|
|
ref={this.setRef}
|
|
|
|
type='file'
|
2024-02-09 04:13:44 +09:00
|
|
|
name='file-upload-input'
|
2024-02-23 06:04:09 +09:00
|
|
|
multiple
|
|
|
|
accept={acceptContentTypes.toArray().join(',')}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
disabled={disabled}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(makeMapStateToProps)(injectIntl(UploadButton));
|