diff --git a/src/web/Manager.js b/src/web/Manager.js index 1d32758..5b8c98f 100755 --- a/src/web/Manager.js +++ b/src/web/Manager.js @@ -142,6 +142,7 @@ Manager.prototype.initialiseEventListeners = function() { // Output document.getElementById("save-to-file").addEventListener("click", this.output.saveClick.bind(this.output)); + document.getElementById("copy-output").addEventListener("click", this.output.copyClick.bind(this.output)); document.getElementById("switch").addEventListener("click", this.output.switchClick.bind(this.output)); document.getElementById("undo-switch").addEventListener("click", this.output.undoSwitchClick.bind(this.output)); document.getElementById("maximise-output").addEventListener("click", this.output.maximiseOutputClick.bind(this.output)); diff --git a/src/web/OutputWaiter.js b/src/web/OutputWaiter.js index 0b16c0f..a5576f3 100755 --- a/src/web/OutputWaiter.js +++ b/src/web/OutputWaiter.js @@ -105,17 +105,20 @@ OutputWaiter.prototype.setOutputInfo = function(length, lines, duration) { OutputWaiter.prototype.adjustWidth = function() { const output = document.getElementById("output"); const saveToFile = document.getElementById("save-to-file"); + const copyOutput = document.getElementById("copy-output"); const switchIO = document.getElementById("switch"); const undoSwitch = document.getElementById("undo-switch"); const maximiseOutput = document.getElementById("maximise-output"); if (output.clientWidth < 680) { saveToFile.childNodes[1].nodeValue = ""; + copyOutput.childNodes[1].nodeValue = ""; switchIO.childNodes[1].nodeValue = ""; undoSwitch.childNodes[1].nodeValue = ""; maximiseOutput.childNodes[1].nodeValue = ""; } else { saveToFile.childNodes[1].nodeValue = " Save to file"; + copyOutput.childNodes[1].nodeValue = " Copy output"; switchIO.childNodes[1].nodeValue = " Move output to input"; undoSwitch.childNodes[1].nodeValue = " Undo"; maximiseOutput.childNodes[1].nodeValue = @@ -147,6 +150,44 @@ OutputWaiter.prototype.saveClick = function() { }; +/** + * Handler for copy click events. + * Copies the output to the clipboard. + */ +OutputWaiter.prototype.copyClick = function() { + // Create invisible textarea to populate with the raw dishStr (not the printable version that + // contains dots instead of the actual bytes) + const textarea = document.createElement("textarea"); + textarea.style.position = "fixed"; + textarea.style.top = 0; + textarea.style.left = 0; + textarea.style.width = 0; + textarea.style.height = 0; + textarea.style.border = "none"; + + textarea.value = this.app.dishStr; + document.body.appendChild(textarea); + + // Select and copy the contents of this textarea + let success = false; + try { + textarea.select(); + success = document.execCommand("copy"); + } catch (err) { + success = false; + } + + if (success) { + this.app.alert("Copied raw output successfully.", "success", 2000); + } else { + this.app.alert("Sorry, the output could not be copied.", "danger", 2000); + } + + // Clean up + document.body.removeChild(textarea); +}; + + /** * Handler for switch click events. * Moves the current output into the input textarea. diff --git a/src/web/html/index.html b/src/web/html/index.html index 5df6682..6f7c878 100755 --- a/src/web/html/index.html +++ b/src/web/html/index.html @@ -189,6 +189,7 @@