upload/browse: modularize and append the <input> to the document to prevent safari/iOS bug (#264, #238)

This commit is contained in:
jean-emmanuel 2018-04-05 22:44:03 +02:00
parent 22122b5731
commit 4e9193f26a
4 changed files with 46 additions and 48 deletions

View file

@ -1,9 +1,10 @@
# Changelog
## next
## 0.28.3
- bug fixes
- widgets not correctly drawn when hidden/shown with dynamic css
- iOS: file upload failing sometimes
## 0.28.2

View file

@ -3,7 +3,7 @@ var ipc = require('../ipc/'),
state = require('./state'),
editor = require('../editor/'),
lobby = require('../ui/lobby'),
{loading, icon, Popup} = require('../ui/utils'),
{loading, icon, Popup, upload} = require('../ui/utils'),
{saveAs} = require('file-saver'),
widgetManager = require('./widgets')
@ -164,29 +164,12 @@ var SessionManager = class SessionManager {
browse() {
var prompt = DOM.create('<input type="file" accept=".js, .json"/>')
prompt.addEventListener('change',function(e){
var reader = new FileReader(),
file = e.target.files[0],
loader = loading('Uploading file...')
reader.onerror = reader.onabort = function(e) {
loader.close()
new Popup({title:icon('exclamation-triangle')+'&nbsp; Error', content: 'Failed to upload session file.', closable:true})
}
reader.onload = function(e) {
loader.close()
ipc.send('sessionOpen',{file:e.target.result,path:file.path})
}
reader.readAsText(file, 'utf-8')
upload('.json, .js', (path, result)=>{
ipc.send('sessionOpen',{file:result,path:path})
}, ()=>{
new Popup({title:icon('exclamation-triangle')+'&nbsp; Error', content: 'Failed to upload session file.', closable:true})
})
prompt.click()
}
create() {

View file

@ -1,5 +1,5 @@
var widgetManager = require('./widgets'),
{loading, icon, Popup} = require('../ui/utils'),
{loading, icon, Popup, upload} = require('../ui/utils'),
{saveAs} = require('file-saver')
@ -82,31 +82,13 @@ var StateManager = class StateManager {
load() {
var prompt = DOM.create('<input type="file" accept=".state"/>')
prompt.addEventListener('change',(e)=>{
var reader = new FileReader(),
loader = loading('Uploading file...')
reader.onerror = reader.onabort = function(e) {
loader.close()
new Popup({title:icon('exclamation-triangle')+'&nbsp; Error', content: 'Failed to upload state file.', closable:true})
}
reader.onload = (e)=>{
loader.close()
this.set(JSON.parse(e.target.result),true)
this.state = e.target.result
}
reader.readAsText(e.target.files[0],'utf-8')
upload('.state', (path, result)=>{
this.set(JSON.parse(result),true)
this.state = result
}, (e)=>{
new Popup({title:icon('exclamation-triangle')+'&nbsp; Error', content: 'Failed to upload state file.', closable:true})
})
prompt.click()
}
save() {

View file

@ -1,4 +1,5 @@
var popupSingleton = null
var popupSingleton = null,
uploadSingleton = null
module.exports = {
@ -92,6 +93,37 @@ module.exports = {
},
iconify:function(string){
return String(string).replace(/\^[^\s]*/g,(x)=>{return module.exports.icon(x.substring(1))})
},
upload: function(types, ok, error) {
if (uploadSingleton) document.body.removeChild(uploadSingleton)
uploadSingleton = DOM.create('<input type="file" accept="' + types + '" style="position:absolute;opacity:0;pointer-events:none;"/>')
document.body.appendChild(uploadSingleton)
uploadSingleton.addEventListener('change',function(e){
var reader = new FileReader(),
file = e.target.files[0],
loader = module.exports.loading('Uploading file...')
reader.onerror = reader.onabort = function() {
loader.close()
error()
}
reader.onload = function(e) {
loader.close()
ok(file.path, e.target.result)
}
reader.readAsText(file, 'utf-8')
})
uploadSingleton.click()
}
}