From ba68a5fa356cc7f60948a2a962f4088c9ce628ec Mon Sep 17 00:00:00 2001 From: Igor Semin Date: Wed, 12 Aug 2015 17:37:53 +0300 Subject: [PATCH 1/2] * Add buttons to save\read textarea --- calque.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 12 ++++++++-- style.css | 27 ++++++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/calque.js b/calque.js index 4af2f6f..7f666be 100644 --- a/calque.js +++ b/calque.js @@ -22,6 +22,35 @@ this.expressions = []; this.activeLine = 0; + this.facilitySaveOpen = function (saveFileBtn, openFileBtn) { + var openFileEl = document.createElement("INPUT"); + openFileEl.type = "file"; + openFileEl.onchange = Calque.readFile; + + saveFileBtn.onclick = function () { + Calque.saveToFile(input.value); + }; + + openFileBtn.onclick = openFile; + + window.addEventListener('keypress', + function (e) { + if (e.ctrlKey && e.keyCode == 10) { + Calque.saveToFile(input.value); + } + + if (e.keyCode == 13 && e.shiftKey) { + openFile(); + } + } + ); + + function openFile() { + openFileEl.value = ""; + openFileEl.click(); + } + } + var handler = function () { this.updateActiveLine(); this.input(); @@ -164,6 +193,43 @@ this.outputEl.innerHTML = html; }; + Calque.saveToFile = function (text) { + var textFileAsBlob = new Blob([text], {type:'text/plain'}); + var fileNameToSaveAs ="calque.txt"; + var downloadLink = document.createElement("a"); + downloadLink.download = fileNameToSaveAs; + downloadLink.innerHTML = "Download File"; + + if (window.URL != null) { + downloadLink.href = window.URL.createObjectURL(textFileAsBlob); + } + else { + downloadLink.href = window.URL.createObjectURL(textFileAsBlob); + + downloadLink.onclick = function (e) { + document.body.removeChild(event.target); + }; + downloadLink.style.display = "none"; + + document.body.appendChild(downloadLink); + } + + downloadLink.click(); + }; + + Calque.readFile = function (e) { + var tgt = e.target || window.event.srcElement, + files = tgt.files, + fileReader; + + if (FileReader && files && files.length) { + var fileReader = new FileReader(); + fileReader.onload = function (event) { + calque.inputEl.value = fileReader.result; + } + fileReader.readAsText(files[0], "UTF-8"); + } + }; function Expression(code, scope) { this.code = code; diff --git a/index.html b/index.html index e60dd8b..e5ca62f 100644 --- a/index.html +++ b/index.html @@ -10,9 +10,13 @@ @@ -22,6 +26,10 @@
+
+ Save (ctrl + enter) + Open (shift + enter) +

Calque

diff --git a/style.css b/style.css index 5270a59..af356fe 100644 --- a/style.css +++ b/style.css @@ -101,4 +101,31 @@ body { .info pre:after { content: attr(data-result); color: grey; +} + +.small-btn { + background-color: #f7f7f7; + border: 1px solid #ccc; + color: #333; + font-size: 11px; + line-height: 1.4; + text-shadow: 0 1px 0 #fff; + display: inline-block; + padding: 0.1em 0.6em; + margin: 0 0.1em; + white-space: nowrap; + box-shadow: 0 1px 0px rgba(0,0,0,0.2),0 0 0 2px #fff inset; + border-radius: 3px; + cursor: pointer; +} + +.small-btn:active { + background-color: #f0f0f0; + box-shadow: none; +} + +.file-interface { + position: absolute; + right: 368px; + top: 1px; } \ No newline at end of file From 0599e918746541d486cb5af103f426680acb3b52 Mon Sep 17 00:00:00 2001 From: Igor Semin Date: Wed, 12 Aug 2015 17:43:27 +0300 Subject: [PATCH 2/2] * Refactor changes --- calque.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/calque.js b/calque.js index 7f666be..c7f25a6 100644 --- a/calque.js +++ b/calque.js @@ -23,12 +23,12 @@ this.activeLine = 0; this.facilitySaveOpen = function (saveFileBtn, openFileBtn) { - var openFileEl = document.createElement("INPUT"); + var openFileEl = document.createElement("input"); openFileEl.type = "file"; openFileEl.onchange = Calque.readFile; saveFileBtn.onclick = function () { - Calque.saveToFile(input.value); + Calque.saveToFile(inputEl.value); }; openFileBtn.onclick = openFile; @@ -198,7 +198,6 @@ var fileNameToSaveAs ="calque.txt"; var downloadLink = document.createElement("a"); downloadLink.download = fileNameToSaveAs; - downloadLink.innerHTML = "Download File"; if (window.URL != null) { downloadLink.href = window.URL.createObjectURL(textFileAsBlob);