From 1399994eb6af8576d82fc04100535f2eddc79897 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Thu, 15 Jun 2017 21:55:06 -0700 Subject: [PATCH] Add drag and drop support --- components/codeImage.js | 46 ++++++++++++++++++++++++++++++++++++++--- package.json | 2 ++ pages/index.js | 21 ++++++------------- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/components/codeImage.js b/components/codeImage.js index 21deea7..4561f47 100644 --- a/components/codeImage.js +++ b/components/codeImage.js @@ -1,11 +1,30 @@ +import { EOL } from 'os' import React from 'react' +import { NativeTypes } from 'react-dnd-html5-backend' +import { DropTarget } from 'react-dnd' import domtoimage from 'dom-to-image' const padding = '50px 50px' +const DEFAULT_CODE = ` +const pluckDeep = key => obj => key.split('.').reduce((accum, key) => accum[key], obj) + +const compose = (...fns) => res => fns.reduce((accum, next) => next(accum), res) + +const unfold = (f, seed) => { + const go = (f, seed, acc) => { + const res = f(seed) + return res ? go(f, res[1], acc.concat([res[0]])) : acc + } + return go(f, seed, []) +}` + +const MAX_LINES = 40 + class CodeImage extends React.Component { constructor () { super() + this.state = {} this.save = this.save.bind(this) } @@ -21,12 +40,16 @@ class CodeImage extends React.Component { } render () { - return ( + let code = this.props.children || this.state.droppedContent || DEFAULT_CODE + const split = code.split(EOL) + if (split.length > MAX_LINES) code = [...split.slice(0, MAX_LINES - 1), '', '...'].join(EOL) + + return this.props.connectDropTarget(
{ this.container = container }}>
             
-              {this.props.children}
+              {code}
             
           
@@ -55,4 +78,21 @@ class CodeImage extends React.Component { } } -export default CodeImage +const drop = (props, monitor, component) => { + const bundle = monitor.getItem() + const file = bundle.files[0] + const reader = new FileReader() + reader.onload = function(event) { + component.setState({ + droppedContent: event.target.result + }) + }; + reader.readAsText(file, "UTF-8"); +} + +export default DropTarget(NativeTypes.FILE, { drop }, (connect, monitor) => ({ + connectDropTarget: connect.dropTarget(), + isOver: monitor.isOver(), + canDrop: monitor.canDrop(), + item: monitor.getItem() +}))(CodeImage) diff --git a/package.json b/package.json index 60d0486..58a458f 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,8 @@ "morgan": "^1.8.2", "next": "^2.4.3", "react": "^15.5.4", + "react-dnd": "^2.4.0", + "react-dnd-html5-backend": "^2.4.1", "react-dom": "^15.5.4" } } diff --git a/pages/index.js b/pages/index.js index 3a90997..08b387c 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,23 +1,12 @@ import React from 'react' +import HTML5Backend from 'react-dnd-html5-backend' +import { DragDropContext } from 'react-dnd' import Axios from 'axios' import CodeImage from '../components/codeImage' import api from '../lib/api' -const code = ` -const pluckDeep = key => obj => key.split('.').reduce((accum, key) => accum[key], obj) - -const compose = (...fns) => res => fns.reduce((accum, next) => next(accum), res) - -const unfold = (f, seed) => { - const go = (f, seed, acc) => { - const res = f(seed) - return res ? go(f, res[1], acc.concat([res[0]])) : acc - } - return go(f, seed, []) -}` - -export default class extends React.Component { +class Home extends React.Component { /* pathname, asPath, err, req, res */ static async getInitialProps ({ asPath }) { try { @@ -42,9 +31,11 @@ export default class extends React.Component {

Welcome to Code Image

- {this.props.content || code} + {this.props.content}
) } } + +export default DragDropContext(HTML5Backend)(Home)