mirror of https://github.com/sgoudham/carbon.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
const { createElement, Component } = require('react')
|
|
const { DropTarget } = require('react-dnd')
|
|
const { NativeTypes } = require('react-dnd-html5-backend')
|
|
|
|
const spec = {
|
|
drop(props, monitor, component) {
|
|
const bundle = monitor.getItem()
|
|
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(),
|
|
monitor,
|
|
isOver: monitor.isOver(),
|
|
canDrop: monitor.canDrop()
|
|
})
|
|
|
|
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,
|
|
})
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
module.exports = exports.default = DropTarget(NativeTypes.FILE, spec, collect)(ReadFileDropContainer)
|