diff --git a/src/web/Manager.js b/src/web/Manager.js index 270e68f8..4b89942a 100755 --- a/src/web/Manager.js +++ b/src/web/Manager.js @@ -142,7 +142,7 @@ Manager.prototype.initialiseEventListeners = function() { document.getElementById("input-text").addEventListener("mouseup", this.highlighter.inputMouseup.bind(this.highlighter)); document.getElementById("input-text").addEventListener("mousemove", this.highlighter.inputMousemove.bind(this.highlighter)); this.addMultiEventListener("#input-text", "mousedown dblclick select", this.highlighter.inputMousedown, this.highlighter); - document.querySelector("#input-file .close").addEventListener("click", this.input.closeFile.bind(this.input)); + document.querySelector("#input-file .close").addEventListener("click", this.input.clearIoClick.bind(this.input)); // Output document.getElementById("save-to-file").addEventListener("click", this.output.saveClick.bind(this.output)); @@ -159,6 +159,8 @@ Manager.prototype.initialiseEventListeners = function() { this.addMultiEventListener("#output-html", "mousedown dblclick select", this.highlighter.outputHtmlMousedown, this.highlighter); this.addDynamicListener(".file-switch", "click", this.output.fileSwitch, this.output); this.addDynamicListener("#output-file-download", "click", this.output.downloadFile, this.output); + this.addDynamicListener("#output-file-slice", "click", this.output.displayFile, this.output); + document.getElementById("show-file-overlay").addEventListener("click", this.output.showFileOverlayClick.bind(this.output)); // Options document.getElementById("options").addEventListener("click", this.options.optionsClick.bind(this.options)); diff --git a/src/web/OutputWaiter.js b/src/web/OutputWaiter.js index d8d028d0..93cc351b 100755 --- a/src/web/OutputWaiter.js +++ b/src/web/OutputWaiter.js @@ -17,7 +17,7 @@ const OutputWaiter = function(app, manager) { this.app = app; this.manager = manager; - this.file = null; + this.dishBuffer = null; }; @@ -37,8 +37,9 @@ OutputWaiter.prototype.get = function() { * @param {string|ArrayBuffer} data - The output string/HTML/ArrayBuffer * @param {string} type - The data type of the output * @param {number} duration - The length of time (ms) it took to generate the output + * @param {boolean} [preserveBuffer=false] - Whether to preserve the dishBuffer */ -OutputWaiter.prototype.set = function(data, type, duration) { +OutputWaiter.prototype.set = function(data, type, duration, preserveBuffer) { const outputText = document.getElementById("output-text"); const outputHtml = document.getElementById("output-html"); const outputFile = document.getElementById("output-file"); @@ -46,7 +47,7 @@ OutputWaiter.prototype.set = function(data, type, duration) { const inputHighlighter = document.getElementById("input-highlighter"); let scriptElements, lines, length; - this.closeFile(); + if (!preserveBuffer) this.closeFile(); switch (type) { case "html": @@ -80,7 +81,7 @@ OutputWaiter.prototype.set = function(data, type, duration) { outputHtml.innerHTML = ""; length = data.byteLength; - this.setFile(new File([data], "output.dat")); + this.setFile(data); break; case "string": default: @@ -106,10 +107,11 @@ OutputWaiter.prototype.set = function(data, type, duration) { /** * Shows file details. * - * @param {File} file + * @param {ArrayBuffer} buf */ -OutputWaiter.prototype.setFile = function(file) { - this.file = file; +OutputWaiter.prototype.setFile = function(buf) { + this.dishBuffer = buf; + const file = new File([buf], "output.dat"); // Display file overlay in output area with details const fileOverlay = document.getElementById("output-file"), @@ -124,7 +126,7 @@ OutputWaiter.prototype.setFile = function(file) { * Removes the output file and nulls its memory. */ OutputWaiter.prototype.closeFile = function() { - this.file = null; + this.dishBuffer = null; document.getElementById("output-file").style.display = "none"; }; @@ -134,8 +136,40 @@ OutputWaiter.prototype.closeFile = function() { */ OutputWaiter.prototype.downloadFile = function() { const filename = window.prompt("Please enter a filename:", "download.dat"); + const file = new File([this.dishBuffer], filename); - if (filename) FileSaver.saveAs(this.file, filename, false); + if (filename) FileSaver.saveAs(file, filename, false); +}; + + +/** + * Handler for file display events. + */ +OutputWaiter.prototype.displayFile = function() { + const startTime = new Date().getTime(), + showFileOverlay = document.getElementById("show-file-overlay"), + sliceFromEl = document.getElementById("output-file-slice-from"), + sliceToEl = document.getElementById("output-file-slice-to"), + sliceFrom = parseInt(sliceFromEl.value, 10), + sliceTo = parseInt(sliceToEl.value, 10), + str = Utils.arrayBufferToStr(this.dishBuffer.slice(sliceFrom, sliceTo)); + + showFileOverlay.style.display = "block"; + this.set(str, "string", new Date().getTime() - startTime, true); +}; + + +/** + * Handler for show file overlay events. + * + * @param {Event} e + */ +OutputWaiter.prototype.showFileOverlayClick = function(e) { + const outputFile = document.getElementById("output-file"), + showFileOverlay = e.target; + + outputFile.style.display = "block"; + showFileOverlay.style.display = "none"; }; @@ -198,8 +232,8 @@ OutputWaiter.prototype.adjustWidth = function() { * Saves the current output to a file. */ OutputWaiter.prototype.saveClick = function() { - if (!this.file) { - this.file = new File([new Uint8Array(Utils.strToCharcode(this.app.dishStr))], ""); + if (!this.dishBuffer) { + this.dishBuffer = new Uint8Array(Utils.strToCharcode(this.app.dishStr)).buffer; } this.downloadFile(); }; @@ -227,7 +261,7 @@ OutputWaiter.prototype.copyClick = function() { let success = false; try { textarea.select(); - success = document.execCommand("copy"); + success = textarea.value && document.execCommand("copy"); } catch (err) { success = false; } diff --git a/src/web/html/index.html b/src/web/html/index.html index cd690869..a132aebd 100755 --- a/src/web/html/index.html +++ b/src/web/html/index.html @@ -184,7 +184,7 @@