You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.2 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import Motion from '../../ui/util/optional_motion';
  5. import spring from 'react-motion/lib/spring';
  6. import ImmutablePureComponent from 'react-immutable-pure-component';
  7. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  8. import classNames from 'classnames';
  9. const messages = defineMessages({
  10. description: { id: 'upload_form.description', defaultMessage: 'Describe for the visually impaired' },
  11. });
  12. export default @injectIntl
  13. class Upload extends ImmutablePureComponent {
  14. static contextTypes = {
  15. router: PropTypes.object,
  16. };
  17. static propTypes = {
  18. media: ImmutablePropTypes.map.isRequired,
  19. intl: PropTypes.object.isRequired,
  20. onUndo: PropTypes.func.isRequired,
  21. onDescriptionChange: PropTypes.func.isRequired,
  22. onOpenFocalPoint: PropTypes.func.isRequired,
  23. onSubmit: PropTypes.func.isRequired,
  24. };
  25. state = {
  26. hovered: false,
  27. focused: false,
  28. dirtyDescription: null,
  29. };
  30. handleKeyDown = (e) => {
  31. if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
  32. this.handleSubmit();
  33. }
  34. }
  35. handleSubmit = () => {
  36. this.handleInputBlur();
  37. this.props.onSubmit(this.context.router.history);
  38. }
  39. handleUndoClick = () => {
  40. this.props.onUndo(this.props.media.get('id'));
  41. }
  42. handleFocalPointClick = () => {
  43. this.props.onOpenFocalPoint(this.props.media.get('id'));
  44. }
  45. handleInputChange = e => {
  46. this.setState({ dirtyDescription: e.target.value });
  47. }
  48. handleMouseEnter = () => {
  49. this.setState({ hovered: true });
  50. }
  51. handleMouseLeave = () => {
  52. this.setState({ hovered: false });
  53. }
  54. handleInputFocus = () => {
  55. this.setState({ focused: true });
  56. }
  57. handleInputBlur = () => {
  58. const { dirtyDescription } = this.state;
  59. this.setState({ focused: false, dirtyDescription: null });
  60. if (dirtyDescription !== null) {
  61. this.props.onDescriptionChange(this.props.media.get('id'), dirtyDescription);
  62. }
  63. }
  64. render () {
  65. const { intl, media } = this.props;
  66. const active = this.state.hovered || this.state.focused;
  67. const description = this.state.dirtyDescription || (this.state.dirtyDescription !== '' && media.get('description')) || '';
  68. const focusX = media.getIn(['meta', 'focus', 'x']);
  69. const focusY = media.getIn(['meta', 'focus', 'y']);
  70. const x = ((focusX / 2) + .5) * 100;
  71. const y = ((focusY / -2) + .5) * 100;
  72. return (
  73. <div className='compose-form__upload' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  74. <Motion defaultStyle={{ scale: 0.8 }} style={{ scale: spring(1, { stiffness: 180, damping: 12 }) }}>
  75. {({ scale }) => (
  76. <div className='compose-form__upload-thumbnail' style={{ transform: `scale(${scale})`, backgroundImage: `url(${media.get('preview_url')})`, backgroundPosition: `${x}% ${y}%` }}>
  77. <div className={classNames('compose-form__upload__actions', { active })}>
  78. <button className='icon-button' onClick={this.handleUndoClick}><i className='fa fa-times' /> <FormattedMessage id='upload_form.undo' defaultMessage='Delete' /></button>
  79. {media.get('type') === 'image' && <button className='icon-button' onClick={this.handleFocalPointClick}><i className='fa fa-crosshairs' /> <FormattedMessage id='upload_form.focus' defaultMessage='Crop' /></button>}
  80. </div>
  81. <div className={classNames('compose-form__upload-description', { active })}>
  82. <label>
  83. <span style={{ display: 'none' }}>{intl.formatMessage(messages.description)}</span>
  84. <input
  85. placeholder={intl.formatMessage(messages.description)}
  86. type='text'
  87. value={description}
  88. maxLength={420}
  89. onFocus={this.handleInputFocus}
  90. onChange={this.handleInputChange}
  91. onBlur={this.handleInputBlur}
  92. onKeyDown={this.handleKeyDown}
  93. />
  94. </label>
  95. </div>
  96. </div>
  97. )}
  98. </Motion>
  99. </div>
  100. );
  101. }
  102. }