Created
May 4, 2017 13:21
-
-
Save Bondifrench/63a934d34e6222393ec37b0985befae9 to your computer and use it in GitHub Desktop.
Using Mithril 1.0 and the HTML5 FileReader API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chosenTab = stream(tabs[0]) | |
const rawData = stream(); | |
const load = cb => f => { | |
const r = new FileReader() | |
r.readAsText(f) | |
r.onloadend = data => cb({ success: true, data } ) | |
r.onerror = data => cb({ success: false, data }) | |
} | |
const view = rawData | |
.map( ({ data , success}) => | |
success | |
? m('', [ | |
m('input[type=file][multiple]', { | |
onchange: m.withAttr( | |
'files' | |
, files => upload( | |
Array.from(files) | |
.map(load(rawData)) | |
) | |
) | |
}) | |
,m('pre', { | |
display: 'block', | |
'background-color': '#f5f5f5', | |
border: '1px solid #ccc', | |
padding: '9.5px', | |
color: '#333', | |
style: { | |
width: '100%', | |
'word-break': 'break-all', | |
'word-wrap': 'break-word', | |
height: '200px' | |
} | |
}, data ) | |
]) | |
: m('pre', { style: { color: 'red' }}, 'Something went wrong: ' + data) | |
) | |
// Initialize the view stream | |
view({ success:true, data: ''}) | |
// redraw whenever a file loads | |
rawData | |
.map(m.redraw) | |
module.exports = { | |
chosenTab | |
,view | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In ES5: