logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

focal_point_modal.js (3408B)


  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import ImmutablePureComponent from 'react-immutable-pure-component';
  4. import { connect } from 'react-redux';
  5. import ImageLoader from './image_loader';
  6. import classNames from 'classnames';
  7. import { changeUploadCompose } from '../../../actions/compose';
  8. import { getPointerPosition } from '../../video';
  9. const mapStateToProps = (state, { id }) => ({
  10. media: state.getIn(['compose', 'media_attachments']).find(item => item.get('id') === id),
  11. });
  12. const mapDispatchToProps = (dispatch, { id }) => ({
  13. onSave: (x, y) => {
  14. dispatch(changeUploadCompose(id, { focus: `${x.toFixed(2)},${y.toFixed(2)}` }));
  15. },
  16. });
  17. @connect(mapStateToProps, mapDispatchToProps)
  18. export default class FocalPointModal extends ImmutablePureComponent {
  19. static propTypes = {
  20. media: ImmutablePropTypes.map.isRequired,
  21. };
  22. state = {
  23. x: 0,
  24. y: 0,
  25. focusX: 0,
  26. focusY: 0,
  27. dragging: false,
  28. };
  29. componentWillMount () {
  30. this.updatePositionFromMedia(this.props.media);
  31. }
  32. componentWillReceiveProps (nextProps) {
  33. if (this.props.media.get('id') !== nextProps.media.get('id')) {
  34. this.updatePositionFromMedia(nextProps.media);
  35. }
  36. }
  37. componentWillUnmount () {
  38. document.removeEventListener('mousemove', this.handleMouseMove);
  39. document.removeEventListener('mouseup', this.handleMouseUp);
  40. }
  41. handleMouseDown = e => {
  42. document.addEventListener('mousemove', this.handleMouseMove);
  43. document.addEventListener('mouseup', this.handleMouseUp);
  44. this.updatePosition(e);
  45. this.setState({ dragging: true });
  46. }
  47. handleMouseMove = e => {
  48. this.updatePosition(e);
  49. }
  50. handleMouseUp = () => {
  51. document.removeEventListener('mousemove', this.handleMouseMove);
  52. document.removeEventListener('mouseup', this.handleMouseUp);
  53. this.setState({ dragging: false });
  54. this.props.onSave(this.state.focusX, this.state.focusY);
  55. }
  56. updatePosition = e => {
  57. const { x, y } = getPointerPosition(this.node, e);
  58. const focusX = (x - .5) * 2;
  59. const focusY = (y - .5) * -2;
  60. this.setState({ x, y, focusX, focusY });
  61. }
  62. updatePositionFromMedia = media => {
  63. const focusX = media.getIn(['meta', 'focus', 'x']);
  64. const focusY = media.getIn(['meta', 'focus', 'y']);
  65. if (focusX && focusY) {
  66. const x = (focusX / 2) + .5;
  67. const y = (focusY / -2) + .5;
  68. this.setState({ x, y, focusX, focusY });
  69. } else {
  70. this.setState({ x: 0.5, y: 0.5, focusX: 0, focusY: 0 });
  71. }
  72. }
  73. setRef = c => {
  74. this.node = c;
  75. }
  76. render () {
  77. const { media } = this.props;
  78. const { x, y, dragging } = this.state;
  79. const width = media.getIn(['meta', 'original', 'width']) || null;
  80. const height = media.getIn(['meta', 'original', 'height']) || null;
  81. return (
  82. <div className='modal-root__modal video-modal focal-point-modal'>
  83. <div className={classNames('focal-point', { dragging })} ref={this.setRef}>
  84. <ImageLoader
  85. previewSrc={media.get('preview_url')}
  86. src={media.get('url')}
  87. width={width}
  88. height={height}
  89. />
  90. <div className='focal-point__reticle' style={{ top: `${y * 100}%`, left: `${x * 100}%` }} />
  91. <div className='focal-point__overlay' onMouseDown={this.handleMouseDown} />
  92. </div>
  93. </div>
  94. );
  95. }
  96. }