Prepare to separate DND to a separate module

main
Mike Fix 7 years ago committed by Michael Fix
parent fd0fa897c4
commit 131640245c

@ -1,28 +1,60 @@
import React from 'react' const { createElement, Component } = require('react')
import { DropTarget } from 'react-dnd' const { DropTarget } = require('react-dnd')
import { NativeTypes } from 'react-dnd-html5-backend' const { NativeTypes } = require('react-dnd-html5-backend')
const spec = { const spec = {
drop (props, monitor) { drop(props, monitor, component) {
const bundle = monitor.getItem() const bundle = monitor.getItem()
const file = bundle.files[0] Promise.all(
bundle.files.map(file => {
const reader = new FileReader() const reader = new FileReader()
reader.onload = event => props.onDrop(event.target.result) return new Promise((resolve, reject) => {
reader.onload = event => resolve(event.target.result)
reader.readAsText(file, 'UTF-8'); 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( class ReadFileDropContainer extends Component {
<div> constructor(props) {
{ super(props)
React.Children.only(React.Children.map(
props.children, this.state = {
child => React.cloneElement(child, { isOver: props.isOver }), lastContent: null,
)[0]) history: []
}
} }
</div> 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)

@ -121,12 +121,16 @@ class Editor extends React.Component {
</div> </div>
</Toolbar> </Toolbar>
<ReadFileDropContainer onDrop={droppedContent => this.setState({ code: droppedContent })}> <ReadFileDropContainer onDrop={([code]) => this.setState({ code })}>
<Overlay title="Drop your file here to import"> {
({ isOver }) => (
<Overlay isOver={isOver} title="Drop your file here to import">
<Carbon config={this.state} updateCode={this.updateCode}> <Carbon config={this.state} updateCode={this.updateCode}>
{this.state.code} {this.state.code}
</Carbon> </Carbon>
</Overlay> </Overlay>
)
}
</ReadFileDropContainer> </ReadFileDropContainer>
</div> </div>
<style jsx>{` <style jsx>{`

Loading…
Cancel
Save