From 131640245cbf62da860b96a59ecbb9d54ba64cf3 Mon Sep 17 00:00:00 2001 From: Mike Fix Date: Sat, 23 Sep 2017 15:40:30 -0700 Subject: [PATCH] Prepare to separate DND to a separate module --- components/ReadFileDropContainer.js | 70 +++++++++++++++++++++-------- pages/editor.js | 16 ++++--- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/components/ReadFileDropContainer.js b/components/ReadFileDropContainer.js index 1b72a9e..c635b38 100644 --- a/components/ReadFileDropContainer.js +++ b/components/ReadFileDropContainer.js @@ -1,28 +1,60 @@ -import React from 'react' -import { DropTarget } from 'react-dnd' -import { NativeTypes } from 'react-dnd-html5-backend' +const { createElement, Component } = require('react') +const { DropTarget } = require('react-dnd') +const { NativeTypes } = require('react-dnd-html5-backend') const spec = { - drop (props, monitor) { + drop(props, monitor, component) { const bundle = monitor.getItem() - const file = bundle.files[0] - const reader = new FileReader() - reader.onload = event => props.onDrop(event.target.result) - reader.readAsText(file, 'UTF-8'); + Promise.all( + bundle.files.map(file => { + const reader = new FileReader() + return new Promise((resolve, reject) => { + reader.onload = event => resolve(event.target.result) + reader.readAsText(file, 'UTF-8'); + }) + }) + ).then(contents => { + bundle.contents = contents + component.setState(state => ({ + lastContent: contents, + history: [bundle, ...state.history] + })) + props.onDrop(contents) + }) } } -const collect = (connect, monitor) => ({ connectDropTarget: connect.dropTarget(), isOver: monitor.isOver() }) +const collect = (connect, monitor) => ({ + connectDropTarget: connect.dropTarget(), + monitor, + isOver: monitor.isOver(), + canDrop: monitor.canDrop() +}) -const ReadFileDropContainer = props => props.connectDropTarget( -
- { - React.Children.only(React.Children.map( - props.children, - child => React.cloneElement(child, { isOver: props.isOver }), - )[0]) +class ReadFileDropContainer extends Component { + constructor(props) { + super(props) + + this.state = { + lastContent: null, + history: [] } -
-) + } + render () { + return this.props.connectDropTarget( + createElement( + 'div', + null, + this.props.children({ + __monitor__: this.props.monitor, + isOver: this.props.isOver, + canDrop: this.props.canDrop, + content: this.state.lastContent, + history: this.state.history, + }) + ) + ) + } +} -export default DropTarget(NativeTypes.FILE, spec, collect)(ReadFileDropContainer) +module.exports = exports.default = DropTarget(NativeTypes.FILE, spec, collect)(ReadFileDropContainer) diff --git a/pages/editor.js b/pages/editor.js index d60dbe0..775c6ac 100644 --- a/pages/editor.js +++ b/pages/editor.js @@ -121,12 +121,16 @@ class Editor extends React.Component { - this.setState({ code: droppedContent })}> - - - {this.state.code} - - + this.setState({ code })}> + { + ({ isOver }) => ( + + + {this.state.code} + + + ) + }