diff options
author | IWANABETHATGUY <974153916@qq.com> | 2021-02-24 09:34:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-23 17:34:46 -0800 |
commit | 6d6c37b9a0fb29ee469ef8561395ea9a44fa6bb0 (patch) | |
tree | c56358a6ce847ba4a26e7bbba6a9abbbe82bfca7 /docs/demo | |
parent | ffb22e3c900e8246d806d2e9a765dc14251b33e8 (diff) | |
download | wabt-6d6c37b9a0fb29ee469ef8561395ea9a44fa6bb0.tar.gz wabt-6d6c37b9a0fb29ee469ef8561395ea9a44fa6bb0.tar.bz2 wabt-6d6c37b9a0fb29ee469ef8561395ea9a44fa6bb0.zip |
feat: 🎸 drag file to editor trigger recompile (#1598)
* feat: 🎸 drag file to editor trigger recompile
* fix: 🐛 code style, fix reviewd problem
* style: 💄 semicolon
✅ Closes: g
Diffstat (limited to 'docs/demo')
-rw-r--r-- | docs/demo/wasm2wat/demo.js | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/docs/demo/wasm2wat/demo.js b/docs/demo/wasm2wat/demo.js index afe2aa62..d7e19d75 100644 --- a/docs/demo/wasm2wat/demo.js +++ b/docs/demo/wasm2wat/demo.js @@ -14,7 +14,17 @@ * limitations under the License. */ -var features = {}; + +var features = getLocalStorageFeatures(); + +function getLocalStorageFeatures() { + try { + return JSON.parse(localStorage && localStorage.getItem("features")) || defaultFeatures; + } catch (e) { + console.log(e); + return { mutable_globals: true }; + } +} WabtModule().then(function(wabt) { @@ -32,6 +42,7 @@ var FEATURES = [ 'reference_types', ]; +var defaultFeatures = { mutable_globals: true }; var editorEl = document.querySelector('.editor'); var uploadEl = document.getElementById('upload'); var selectEl = document.getElementById('select'); @@ -40,19 +51,30 @@ var generateNamesEl = document.getElementById('generateNames'); var foldExprsEl = document.getElementById('foldExprs'); var inlineExportEl = document.getElementById('inlineExport'); var readDebugNamesEl = document.getElementById('readDebugNames'); - var options = {mode: 'wast', lineNumbers: true}; var editor = CodeMirror.fromTextArea(editorEl, options); -var fileBuffer = null; +var editorContainer = document.querySelector('.CodeMirror.cm-s-default'); +editorContainer.ondrop = function(e) { + e.preventDefault(); + let file = e.dataTransfer.files[0]; + if (!file) { + return; + } + readAndCompileFile(file); +} +var fileBuffer = null; for (var feature of FEATURES) { var featureEl = document.getElementById(feature); - features[feature] = featureEl.checked; + featureEl.checked = !!features[feature]; featureEl.addEventListener('change', event => { var feature = event.target.id; features[feature] = event.target.checked; compile(fileBuffer); + if (localStorage) { + localStorage.setItem('features', JSON.stringify(features)) + } }); } @@ -96,6 +118,10 @@ function onUploadClicked(e) { function onUploadedFile(e) { var file = e.target.files[0]; + readAndCompileFile(file); +} +// extract common util function +function readAndCompileFile(file) { var reader = new FileReader(); reader.onload = function(e) { fileBuffer = new Uint8Array(e.target.result); |