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(
const reader = new FileReader() bundle.files.map(file => {
reader.onload = event => props.onDrop(event.target.result) const reader = new FileReader()
reader.readAsText(file, 'UTF-8'); 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( 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"> {
<Carbon config={this.state} updateCode={this.updateCode}> ({ isOver }) => (
{this.state.code} <Overlay isOver={isOver} title="Drop your file here to import">
</Carbon> <Carbon config={this.state} updateCode={this.updateCode}>
</Overlay> {this.state.code}
</Carbon>
</Overlay>
)
}
</ReadFileDropContainer> </ReadFileDropContainer>
</div> </div>
<style jsx>{` <style jsx>{`

Loading…
Cancel
Save