Last active
August 10, 2018 10:28
-
-
Save Sorebit/02635397fd106e03344fc4f1ab14b049 to your computer and use it in GitHub Desktop.
Upload and download JSON scene config file. Will be used in Pen & Paper soon
This file contains 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
<head> | |
<meta charset="utf-8"> | |
</head> | |
<button id="button">Save scene</button> | |
<script> | |
// Original source: http://jsfiddle.net/4ooupev9/126/ | |
function handleDownload() { | |
// Scene object placeholder | |
const scene = { | |
scene: { name: 'Scene name 👍' }, | |
board: { height: 10, width: 15, figures: [] } | |
}; | |
const content = JSON.stringify(scene); | |
const link = document.createElement('a'); | |
const file = new Blob([content], {type: 'application/json'}); | |
link.setAttribute('href', URL.createObjectURL(file)); | |
link.setAttribute('download', 'scene.json'); | |
const event = document.createEvent('MouseEvents'); | |
event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); | |
link.dispatchEvent(event); | |
// link.click(); // Does not work on FF? | |
} | |
document.getElementById('button').addEventListener('click', handleDownload, false); | |
</script> |
This file contains 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
<head> | |
<meta charset="utf-8"> | |
</head> | |
<input type="file" id="file" name="file" /> | |
<output id="list"></output> | |
<script> | |
// Scene parser placeholder | |
function parseScene(scene) { | |
console.log(scene); | |
} | |
function handleFileSelect(evt) { | |
// Get file and return if none | |
let file = evt.target.files[0]; | |
if (!file) return; | |
// Only process JSON files | |
if (!file.type.match('application/json')) { | |
console.error('Only JSON files are supported.'); | |
return; | |
} | |
let reader = new FileReader(); | |
// Capture the file information when loaded | |
// Consider about onerror, onabort, onloadend etc. | |
// Atthendoftheday, it's supposed to be an MVP | |
reader.onload = ((theFile) => { | |
return (e) => { | |
const scene = JSON.parse(e.target.result); | |
parseScene(scene); | |
}; | |
})(file); | |
// Start reading in scene file as text | |
reader.readAsText(file); | |
} | |
document.getElementById('file').addEventListener('change', handleFileSelect, false); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment