From 737cb979ae4b011cf7db260beb4647e48865cb47 Mon Sep 17 00:00:00 2001 From: Michael Fix Date: Tue, 20 Nov 2018 09:43:06 -0800 Subject: [PATCH] implement background url support (#579) * implement background url support - Closes #490 * fix lint * WIP: move input into background picker * fix cors issue * keep background select as is * update UI to select between URL and file * fetch https url instead of http --- components/ImagePicker.js | 104 ++++++++++++++++++++++++++++++++++---- components/RandomImage.js | 4 +- 2 files changed, 97 insertions(+), 11 deletions(-) diff --git a/components/ImagePicker.js b/components/ImagePicker.js index d740554..f044a10 100644 --- a/components/ImagePicker.js +++ b/components/ImagePicker.js @@ -1,9 +1,10 @@ import React from 'react' import ReactCrop, { makeAspectCrop } from 'react-image-crop' -import RandomImage from './RandomImage' +import RandomImage, { downloadThumbnailImage } from './RandomImage' import PhotoCredit from './PhotoCredit' import { fileToDataURL } from '../lib/util' +import { COLORS } from '../lib/constants' const getCroppedImg = (imageDataURL, pixelCrop) => { const canvas = document.createElement('canvas') @@ -32,17 +33,25 @@ const getCroppedImg = (imageDataURL, pixelCrop) => { }) } -const INITIAL_STATE = { crop: null, imageAspectRatio: null, pixelCrop: null, photographer: null } +const INITIAL_STATE = { + mode: 'file', + crop: null, + imageAspectRatio: null, + pixelCrop: null, + photographer: null +} export default class extends React.Component { constructor(props) { super(props) this.state = INITIAL_STATE + this.handleURLInput = this.handleURLInput.bind(this) this.selectImage = this.selectImage.bind(this) this.removeImage = this.removeImage.bind(this) this.onImageLoaded = this.onImageLoaded.bind(this) this.onCropChange = this.onCropChange.bind(this) this.onDragEnd = this.onDragEnd.bind(this) + this.selectMode = this.selectMode.bind(this) } static getDerivedStateFromProps(nextProps, state) { @@ -90,6 +99,22 @@ export default class extends React.Component { }) } + handleURLInput(e) { + e.preventDefault() + const url = e.target[0].value + return downloadThumbnailImage({ url }).then(({ dataURL }) => + this.props.onChange({ + backgroundImage: dataURL, + backgroundImageSelection: null, + photographer: null + }) + ) + } + + selectMode(mode) { + this.setState({ mode }) + } + selectImage(e, { photographer } = {}) { const file = e.target ? e.target.files[0] : e @@ -117,12 +142,31 @@ export default class extends React.Component { let content = (
- Click the button below to upload a background image: - + Upload a background image: + + + {this.state.mode === 'file' ? ( + + ) : ( +
+ + +
+ )}

@@ -133,14 +177,55 @@ export default class extends React.Component {
diff --git a/components/RandomImage.js b/components/RandomImage.js index ffcd111..aa322e3 100644 --- a/components/RandomImage.js +++ b/components/RandomImage.js @@ -6,9 +6,9 @@ import api from '../lib/api' import PhotoCredit from './PhotoCredit' import { fileToDataURL } from '../lib/util' -const downloadThumbnailImage = img => { +export const downloadThumbnailImage = img => { return api.client - .get(img.url, { responseType: 'blob' }) + .get(img.url.replace('http://', 'https://'), { responseType: 'blob' }) .then(res => res.data) .then(fileToDataURL) .then(dataURL => Object.assign(img, { dataURL }))