From 508afdfbb43b8571c25d21e83e2782903aafa999 Mon Sep 17 00:00:00 2001 From: Michael Fix Date: Fri, 19 Jan 2018 18:10:05 -0800 Subject: [PATCH] Implement drag-n-drop image for backgrounds (#157) - Closes #122 --- components/Carbon.js | 9 +++++++-- package.json | 2 +- pages/editor.js | 24 ++++++++++++++++++++++-- yarn.lock | 6 +++--- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/components/Carbon.js b/components/Carbon.js index f0ee436..2b87c18 100644 --- a/components/Carbon.js +++ b/components/Carbon.js @@ -147,8 +147,13 @@ class Carbon extends React.Component { } #container .bg { - background: ${this.props.config.background || config.background}; - position: absolute; + ${this.props.config.backgroundImage + ? `background: url(${this.props.config.backgroundImage}); + background-size: cover; + background-repeat: no-repeat;` + : `background: ${this.props.config.background || config.background}; + background-size: auto; + background-repeat: repeat;`} position: absolute; top: 0px; right: 0px; bottom: 0px; diff --git a/package.json b/package.json index a51b9bd..7282207 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "codemirror": "^5.31.0", "codemirror-mode-elixir": "^1.1.1", "dom-to-image": "^2.5.2", - "dropperx": "^0.0.3", + "dropperx": "^0.1.0", "express": "^4.16.2", "form-data": "^2.2.0", "highlight.js": "^9.12.0", diff --git a/pages/editor.js b/pages/editor.js index ab03ccc..192ee2d 100644 --- a/pages/editor.js +++ b/pages/editor.js @@ -3,7 +3,7 @@ import React from 'react' import HTML5Backend from 'react-dnd-html5-backend' import { DragDropContext } from 'react-dnd' import domtoimage from 'dom-to-image' -import ReadFileDropContainer from 'dropperx' +import ReadFileDropContainer, { DATA_URL, TEXT } from 'dropperx' // Ours import Page from '../components/Page' @@ -68,6 +68,7 @@ class Editor extends React.Component { paddingVertical: '48px', paddingHorizontal: '32px', uploading: false, + backgroundImage: null, code: props.content, _initialState: this.props.initialState }, @@ -93,6 +94,7 @@ class Editor extends React.Component { updateQueryString(this.state) const s = { ...this.state } delete s.code + delete s.backgroundImage saveState(localStorage, s) } @@ -138,6 +140,10 @@ class Editor extends React.Component { }) } + isImage(file) { + return file.type.split('/')[0] === 'image' + } + render() { return ( @@ -177,7 +183,21 @@ class Editor extends React.Component { - this.setState({ code: file.content })}> + { + if (this.isImage(file)) { + return DATA_URL + } + return TEXT + }} + onDrop={([file]) => { + if (this.isImage(file)) { + this.setState({ backgroundImage: file.content }) + } else { + this.setState({ code: file.content }) + } + }} + > {({ isOver, canDrop }) => ( =15" react-dnd ">=2"