From 4312d3962431cf4d55f2a2e458c71ec6198db42d Mon Sep 17 00:00:00 2001 From: j433866 Date: Wed, 3 Jul 2019 11:41:22 +0100 Subject: [PATCH] Show or hide thumbnail when the option is changed. --- src/web/Manager.mjs | 1 + src/web/waiters/InputWaiter.mjs | 47 +++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/web/Manager.mjs b/src/web/Manager.mjs index c64712ac..2486f65d 100755 --- a/src/web/Manager.mjs +++ b/src/web/Manager.mjs @@ -231,6 +231,7 @@ class Manager { this.addDynamicListener(".option-item select", "change", this.options.selectChange, this.options); document.getElementById("theme").addEventListener("change", this.options.themeChange.bind(this.options)); document.getElementById("logLevel").addEventListener("change", this.options.logLevelChange.bind(this.options)); + document.getElementById("imagePreview").addEventListener("change", this.input.renderFileThumb.bind(this.input)); // Misc window.addEventListener("keydown", this.bindings.parseInput.bind(this.bindings)); diff --git a/src/web/waiters/InputWaiter.mjs b/src/web/waiters/InputWaiter.mjs index cf3aa03e..869e5854 100644 --- a/src/web/waiters/InputWaiter.mjs +++ b/src/web/waiters/InputWaiter.mjs @@ -415,6 +415,34 @@ class InputWaiter { this.displayFilePreview(inputData); } + /** + * Render the input thumbnail + */ + async renderFileThumb() { + const activeTab = this.manager.tabs.getActiveInputTab(), + input = await this.getInputValue(activeTab), + fileThumb = document.getElementById("input-file-thumbnail"); + + if (typeof input === "string" || + !this.app.options.imagePreview) { + this.resetFileThumb(); + return; + } + + const inputArr = new Uint8Array(input), + type = isImage(inputArr); + + if (type && type !== "image/tiff" && inputArr.byteLength <= 512000) { + // Most browsers don't support displaying TIFFs, so ignore them + // Don't render images over 512000 bytes + const blob = new Blob([inputArr], {type: type}), + url = URL.createObjectURL(blob); + fileThumb.src = url; + } else { + this.resetFileThumb(); + } + + } /** * Reset the input thumbnail to the default icon @@ -434,28 +462,13 @@ class InputWaiter { displayFilePreview(inputData) { const activeTab = this.manager.tabs.getActiveInputTab(), input = inputData.input, - inputText = document.getElementById("input-text"), - fileThumb = document.getElementById("input-file-thumbnail"); + inputText = document.getElementById("input-text"); if (inputData.inputNum !== activeTab) return; inputText.style.overflow = "hidden"; inputText.classList.add("blur"); inputText.value = Utils.printable(Utils.arrayBufferToStr(input.slice(0, 4096))); - if (this.app.options.imagePreview) { - const inputArr = new Uint8Array(input), - type = isImage(inputArr); - if (type && type !== "image/tiff" && inputArr.byteLength <= 512000) { - // Most browsers don't support displaying TIFFs, so ignore them - // Assume anything over 512000 bytes is truncated - const blob = new Blob([inputArr], {type: type}), - url = URL.createObjectURL(blob); - fileThumb.src = url; - } else { - this.resetFileThumb(); - } - } else { - this.resetFileThumb(); - } + this.renderFileThumb(); }