127 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-03-02 17:26:25 -05:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
import { defineMessages, injectIntl } from 'react-intl'
import classNames from 'classnames'
import Button from '../../../../components/button'
2020-03-06 23:53:28 -05:00
import Image from '../../../../components/image'
2019-07-02 03:10:25 -04:00
const messages = defineMessages({
description: { id: 'upload_form.description', defaultMessage: 'Describe for the visually impaired' },
delete: { id: 'upload_form.undo', defaultMessage: 'Delete' },
2020-03-02 17:26:25 -05:00
})
2019-07-02 03:10:25 -04:00
2020-02-25 11:04:44 -05:00
export default
@injectIntl
2019-07-02 03:10:25 -04:00
class Upload extends ImmutablePureComponent {
static contextTypes = {
router: PropTypes.object,
2020-03-02 17:26:25 -05:00
}
2019-07-02 03:10:25 -04:00
static propTypes = {
media: ImmutablePropTypes.map.isRequired,
intl: PropTypes.object.isRequired,
onUndo: PropTypes.func.isRequired,
onDescriptionChange: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
2020-03-02 17:26:25 -05:00
}
2019-07-02 03:10:25 -04:00
state = {
hovered: false,
focused: false,
dirtyDescription: null,
2020-03-02 17:26:25 -05:00
}
2019-07-02 03:10:25 -04:00
handleKeyDown = (e) => {
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
2020-03-02 17:26:25 -05:00
this.handleSubmit()
2019-07-02 03:10:25 -04:00
}
}
handleSubmit = () => {
2020-03-02 17:26:25 -05:00
this.handleInputBlur()
this.props.onSubmit(this.context.router.history)
2019-07-02 03:10:25 -04:00
}
handleUndoClick = e => {
2020-03-02 17:26:25 -05:00
e.stopPropagation()
this.props.onUndo(this.props.media.get('id'))
2019-07-02 03:10:25 -04:00
}
handleInputChange = e => {
2020-03-02 17:26:25 -05:00
this.setState({ dirtyDescription: e.target.value })
2019-07-02 03:10:25 -04:00
}
handleMouseEnter = () => {
2020-03-02 17:26:25 -05:00
this.setState({ hovered: true })
2019-07-02 03:10:25 -04:00
}
handleMouseLeave = () => {
2020-03-02 17:26:25 -05:00
this.setState({ hovered: false })
2019-07-02 03:10:25 -04:00
}
handleInputFocus = () => {
2020-03-02 17:26:25 -05:00
this.setState({ focused: true })
2019-07-02 03:10:25 -04:00
}
handleClick = () => {
2020-03-02 17:26:25 -05:00
this.setState({ focused: true })
2019-07-02 03:10:25 -04:00
}
handleInputBlur = () => {
2020-03-02 17:26:25 -05:00
const { dirtyDescription } = this.state
2019-07-02 03:10:25 -04:00
2020-03-06 23:53:28 -05:00
this.setState({
focused: false,
dirtyDescription: null,
})
2019-07-02 03:10:25 -04:00
if (dirtyDescription !== null) {
2020-03-02 17:26:25 -05:00
this.props.onDescriptionChange(this.props.media.get('id'), dirtyDescription)
2019-07-02 03:10:25 -04:00
}
}
2020-03-02 17:26:25 -05:00
render() {
const { intl, media } = this.props
const active = this.state.hovered || this.state.focused
const description = this.state.dirtyDescription || (this.state.dirtyDescription !== '' && media.get('description')) || ''
const focusX = media.getIn(['meta', 'focus', 'x'])
const focusY = media.getIn(['meta', 'focus', 'y'])
const x = ((focusX / 2) + .5) * 100
const y = ((focusY / -2) + .5) * 100
2019-07-02 03:10:25 -04:00
return (
<div className='compose-form-upload' tabIndex='0' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} onClick={this.handleClick} role='button'>
2020-03-06 23:53:28 -05:00
<div className='compose-form__upload-thumbnail' style={{ backgroundImage: `url(${media.get('preview_url')})`, backgroundPosition: `${x}% ${y}%` }}>
<div className={classNames('compose-form__upload__actions', { active })}>
<Button
title={intl.formatMessage(messages.delete)}
onClick={this.handleUndoClick}
icon='cancel'
/>
</div>
<div className={classNames('compose-form-upload__description', { active })}>
<label>
<span style={{ display: 'none' }}>
{intl.formatMessage(messages.description)}
</span>
<textarea
placeholder={intl.formatMessage(messages.description)}
value={description}
maxLength={420}
onFocus={this.handleInputFocus}
onChange={this.handleInputChange}
onBlur={this.handleInputBlur}
onKeyDown={this.handleKeyDown}
/>
</label>
</div>
</div>
2019-07-02 03:10:25 -04:00
</div>
2020-03-02 17:26:25 -05:00
)
2019-07-02 03:10:25 -04:00
}
}