2017-09-28 14:31:31 +01:00
|
|
|
import React from 'react';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-10-16 08:36:15 +01:00
|
|
|
import Motion from '../../ui/util/optional_motion';
|
2017-09-28 14:31:31 +01:00
|
|
|
import spring from 'react-motion/lib/spring';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2018-02-21 23:35:46 +00:00
|
|
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
2017-09-28 14:31:31 +01:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
description: { id: 'upload_form.description', defaultMessage: 'Describe for the visually impaired' },
|
|
|
|
});
|
|
|
|
|
2018-09-14 16:59:48 +01:00
|
|
|
export default @injectIntl
|
|
|
|
class Upload extends ImmutablePureComponent {
|
2017-09-28 14:31:31 +01:00
|
|
|
|
2018-10-11 00:31:03 +01:00
|
|
|
static contextTypes = {
|
|
|
|
router: PropTypes.object,
|
|
|
|
};
|
|
|
|
|
2017-09-28 14:31:31 +01:00
|
|
|
static propTypes = {
|
|
|
|
media: ImmutablePropTypes.map.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
onUndo: PropTypes.func.isRequired,
|
|
|
|
onDescriptionChange: PropTypes.func.isRequired,
|
2018-02-21 23:35:46 +00:00
|
|
|
onOpenFocalPoint: PropTypes.func.isRequired,
|
2018-08-18 18:40:35 +01:00
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2017-09-28 14:31:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
hovered: false,
|
|
|
|
focused: false,
|
|
|
|
dirtyDescription: null,
|
|
|
|
};
|
|
|
|
|
2018-08-18 18:40:35 +01:00
|
|
|
handleKeyDown = (e) => {
|
|
|
|
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
|
|
|
|
this.handleSubmit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = () => {
|
|
|
|
this.handleInputBlur();
|
2018-10-11 00:31:03 +01:00
|
|
|
this.props.onSubmit(this.context.router.history);
|
2018-08-18 18:40:35 +01:00
|
|
|
}
|
|
|
|
|
2018-10-24 17:17:15 +01:00
|
|
|
handleUndoClick = e => {
|
|
|
|
e.stopPropagation();
|
2017-09-28 14:31:31 +01:00
|
|
|
this.props.onUndo(this.props.media.get('id'));
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:17:15 +01:00
|
|
|
handleFocalPointClick = e => {
|
|
|
|
e.stopPropagation();
|
2018-02-21 23:35:46 +00:00
|
|
|
this.props.onOpenFocalPoint(this.props.media.get('id'));
|
|
|
|
}
|
|
|
|
|
2017-09-28 14:31:31 +01:00
|
|
|
handleInputChange = e => {
|
|
|
|
this.setState({ dirtyDescription: e.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseEnter = () => {
|
|
|
|
this.setState({ hovered: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseLeave = () => {
|
|
|
|
this.setState({ hovered: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputFocus = () => {
|
|
|
|
this.setState({ focused: true });
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:17:15 +01:00
|
|
|
handleClick = () => {
|
|
|
|
this.setState({ focused: true });
|
|
|
|
}
|
|
|
|
|
2017-09-28 14:31:31 +01:00
|
|
|
handleInputBlur = () => {
|
|
|
|
const { dirtyDescription } = this.state;
|
|
|
|
|
|
|
|
this.setState({ focused: false, dirtyDescription: null });
|
|
|
|
|
|
|
|
if (dirtyDescription !== null) {
|
|
|
|
this.props.onDescriptionChange(this.props.media.get('id'), dirtyDescription);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { intl, media } = this.props;
|
|
|
|
const active = this.state.hovered || this.state.focused;
|
2017-12-12 18:57:22 +00:00
|
|
|
const description = this.state.dirtyDescription || (this.state.dirtyDescription !== '' && media.get('description')) || '';
|
2018-02-21 23:35:46 +00:00
|
|
|
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;
|
2017-09-28 14:31:31 +01:00
|
|
|
|
|
|
|
return (
|
2018-10-24 17:17:15 +01:00
|
|
|
<div className='compose-form__upload' tabIndex='0' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} onClick={this.handleClick} role='button'>
|
2017-09-28 14:31:31 +01:00
|
|
|
<Motion defaultStyle={{ scale: 0.8 }} style={{ scale: spring(1, { stiffness: 180, damping: 12 }) }}>
|
|
|
|
{({ scale }) => (
|
2018-02-21 23:35:46 +00:00
|
|
|
<div className='compose-form__upload-thumbnail' style={{ transform: `scale(${scale})`, backgroundImage: `url(${media.get('preview_url')})`, backgroundPosition: `${x}% ${y}%` }}>
|
|
|
|
<div className={classNames('compose-form__upload__actions', { active })}>
|
2018-09-28 01:11:14 +01:00
|
|
|
<button className='icon-button' onClick={this.handleUndoClick}><i className='fa fa-times' /> <FormattedMessage id='upload_form.undo' defaultMessage='Delete' /></button>
|
|
|
|
{media.get('type') === 'image' && <button className='icon-button' onClick={this.handleFocalPointClick}><i className='fa fa-crosshairs' /> <FormattedMessage id='upload_form.focus' defaultMessage='Crop' /></button>}
|
2018-02-21 23:35:46 +00:00
|
|
|
</div>
|
2017-09-28 14:31:31 +01:00
|
|
|
|
|
|
|
<div className={classNames('compose-form__upload-description', { active })}>
|
|
|
|
<label>
|
|
|
|
<span style={{ display: 'none' }}>{intl.formatMessage(messages.description)}</span>
|
|
|
|
|
|
|
|
<input
|
|
|
|
placeholder={intl.formatMessage(messages.description)}
|
|
|
|
type='text'
|
|
|
|
value={description}
|
2017-09-29 01:30:00 +01:00
|
|
|
maxLength={420}
|
2017-09-28 14:31:31 +01:00
|
|
|
onFocus={this.handleInputFocus}
|
|
|
|
onChange={this.handleInputChange}
|
|
|
|
onBlur={this.handleInputBlur}
|
2018-08-18 18:40:35 +01:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2017-09-28 14:31:31 +01:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Motion>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|