cropper.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2016 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import React from 'react'
  19. import PropTypes from 'prop-types'
  20. import Cropper from 'react-crop'
  21. //
  22. // The react-crop component requires a wrapper component for interacting
  23. // with the outside world.
  24. // The main thing you have to do is set style for .CanvasCropper
  25. // For example, the style for profile avatars looks like
  26. // .avatar-preview .CanvasCropper {
  27. // max-width: 270px;
  28. // max-height: 270px;
  29. // overflow: hidden;
  30. // }
  31. //
  32. class CanvasCropper extends React.Component {
  33. static propTypes = {
  34. imgFile: PropTypes.object, // eslint-disable-line react/forbid-prop-types
  35. // selected image file object
  36. width: PropTypes.number, // desired cropped width
  37. height: PropTypes.number, // desired cropped height
  38. onImageLoaded: PropTypes.func // if you care when the image is loaded
  39. };
  40. static defaultProps = {
  41. imgFile: null,
  42. width: 270,
  43. height: 270,
  44. onImageLoaded: null
  45. };
  46. constructor (/* props */) {
  47. super();
  48. this.onImageLoaded = this.imageLoaded.bind(this);
  49. this.wrapper = null;
  50. this.cropper = null;
  51. }
  52. componentDidMount () {
  53. if (this.wrapper) {
  54. this.wrapper.focus()
  55. }
  56. }
  57. // called when the image is loaded in the DOM
  58. // @param img: the img DOM element
  59. imageLoaded (img) {
  60. if (typeof this.props.onImageLoaded === 'function') {
  61. this.props.onImageLoaded(img);
  62. }
  63. }
  64. // @returns a Promise that resolves with cropped image as a blob
  65. crop () {
  66. return this.cropper.cropImage();
  67. }
  68. render () {
  69. return (
  70. <div className="CanvasCropper" ref={(el) => { this.wrapper = el }} tabIndex="0">
  71. {this.props.imgFile &&
  72. <div>
  73. <Cropper
  74. ref={(el) => { this.cropper = el }}
  75. image={this.props.imgFile}
  76. width={this.props.width}
  77. height={this.props.height}
  78. minConstraints={[16, 16]}
  79. onImageLoaded={this.onImageLoaded}
  80. />
  81. </div>}
  82. </div>
  83. )
  84. }
  85. }
  86. export default CanvasCropper