|
@@ -1,5 +1,8 @@
|
|
|
import React from 'react';
|
|
import React from 'react';
|
|
|
import PropTypes from 'prop-types';
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
+import loggerFactory from '@alias/logger';
|
|
|
|
|
+import canvasToBlob from 'async-canvas-to-blob';
|
|
|
|
|
+
|
|
|
import Modal from 'react-bootstrap/es/Modal';
|
|
import Modal from 'react-bootstrap/es/Modal';
|
|
|
import Button from 'react-bootstrap/es/Button';
|
|
import Button from 'react-bootstrap/es/Button';
|
|
|
import { withTranslation } from 'react-i18next';
|
|
import { withTranslation } from 'react-i18next';
|
|
@@ -7,6 +10,9 @@ import ReactCrop from 'react-image-crop';
|
|
|
import AppContainer from '../../services/AppContainer';
|
|
import AppContainer from '../../services/AppContainer';
|
|
|
import { createSubscribedElement } from '../UnstatedUtils';
|
|
import { createSubscribedElement } from '../UnstatedUtils';
|
|
|
import 'react-image-crop/dist/ReactCrop.css';
|
|
import 'react-image-crop/dist/ReactCrop.css';
|
|
|
|
|
+import { toastError } from '../../util/apiNotification';
|
|
|
|
|
+
|
|
|
|
|
+const logger = loggerFactory('growi:ImageCropModal');
|
|
|
|
|
|
|
|
class ImageCropModal extends React.Component {
|
|
class ImageCropModal extends React.Component {
|
|
|
|
|
|
|
@@ -42,25 +48,21 @@ class ImageCropModal extends React.Component {
|
|
|
canvas.height = crop.height;
|
|
canvas.height = crop.height;
|
|
|
const ctx = canvas.getContext('2d');
|
|
const ctx = canvas.getContext('2d');
|
|
|
ctx.drawImage(image, crop.x * scaleX, crop.y * scaleY, crop.width * scaleX, crop.height * scaleY, 0, 0, crop.width, crop.height);
|
|
ctx.drawImage(image, crop.x * scaleX, crop.y * scaleY, crop.width * scaleX, crop.height * scaleY, 0, 0, crop.width, crop.height);
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
|
- canvas.toBlob((blob) => {
|
|
|
|
|
- if (!blob) {
|
|
|
|
|
- reject(new Error('Canvas is empty'));
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- blob.name = fileName;
|
|
|
|
|
- window.URL.revokeObjectURL(this.fileUrl);
|
|
|
|
|
- this.fileUrl = window.URL.createObjectURL(blob);
|
|
|
|
|
- resolve(this.fileUrl);
|
|
|
|
|
- }, 'image/jpeg');
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ const blob = await canvasToBlob(canvas);
|
|
|
|
|
+ return blob;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ logger.error(err);
|
|
|
|
|
+ toastError(new Error('Failed to draw image'));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async crop() {
|
|
async crop() {
|
|
|
// crop immages
|
|
// crop immages
|
|
|
if (this.state.imageRef && this.state.crop.width && this.state.crop.height) {
|
|
if (this.state.imageRef && this.state.crop.width && this.state.crop.height) {
|
|
|
- const croppedImageUrl = await this.getCroppedImg(this.state.imageRef, this.state.crop, '/images/icons/user');
|
|
|
|
|
- this.props.onCropCompleted(croppedImageUrl);
|
|
|
|
|
|
|
+ const croppedImage = await this.getCroppedImg(this.state.imageRef, this.state.crop, '/images/icons/user');
|
|
|
|
|
+ this.props.onCropCompleted(croppedImage);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|