The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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