cropperMaker.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ReactDOM from 'react-dom'
  20. import CanvasCropper from './cropper'
  21. // CanvasCropperMaker is the component you'll create if injecting the cropper
  22. // into existing non-react UI (see UploadFileView.coffee for sample usage)
  23. // let cropper = new Cropper(@$('.avatar-preview')[0], {imgFile: @file, width: @avatarSize.w, height: @avatarSize.h})
  24. // cropper.render()
  25. //
  26. class CanvasCropperMaker {
  27. // @param root: DOM node where I want the cropper created
  28. // @param props: properties
  29. // imgFile: the File object returned from the native file open dialog
  30. // width: desired width in px of the final cropped image
  31. // height: desired height in px if the final cropped image
  32. constructor (root, props) {
  33. this.root = root; // DOM node we render into
  34. this.imgFile = props.imgFile;
  35. this.width = props.width || 128;
  36. this.height = props.width || 128;
  37. this.cropper = null;
  38. }
  39. unmount () {
  40. ReactDOM.unmountComponentAtNode(this.root);
  41. }
  42. render () {
  43. ReactDOM.render(
  44. <CanvasCropper ref={(el) => { this.cropper = el }} imgFile={this.imgFile} width={this.width} height={this.height} />,
  45. this.root
  46. )
  47. }
  48. // crop the image.
  49. // returns a promise that resolves with the cropped image as a blob
  50. crop () {
  51. return this.cropper
  52. ? this.cropper.crop()
  53. : null;
  54. }
  55. }
  56. export default CanvasCropperMaker