From ce16e61e63382d525d8ba3cac0999867d9d772cf Mon Sep 17 00:00:00 2001 From: Marc-Andre Ferland Date: Mon, 31 Oct 2022 01:02:23 -0400 Subject: [PATCH] Adds a copy as JSON button. --- ui/media/js/dnd.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/ui/media/js/dnd.js b/ui/media/js/dnd.js index a2d51211..4966a313 100644 --- a/ui/media/js/dnd.js +++ b/ui/media/js/dnd.js @@ -209,6 +209,18 @@ function restoreTaskToUI(task) { } } +function readUIasJSON() { + const reqBody = {} + for (const key in TASK_MAPPING) { + reqBody[key] = TASK_MAPPING[key].readUI() + } + return { + 'numOutputsTotal': parseInt(numOutputsTotalField.value), + 'seed': TASK_MAPPING['seed'].readUI(), + 'reqBody': reqBody + } +} + const TASK_TEXT_MAPPING = { width: 'Width', height: 'Height', @@ -331,3 +343,41 @@ function dragOverHandler(ev) { document.addEventListener("drop", dropHandler) document.addEventListener("dragover", dragOverHandler) + +// Adds a copy icon if the browser grants permission to write to clipboard. +function checkWriteToClipboardPermission (result) { + if (result.state == "granted" || result.state == "prompt") { + const style = document.createElement('style'); + style.textContent = ` + #copy-image-settings { + cursor: pointer; + padding: 8px; + opacity: 1; + transition: opacity 0.5; + } + .collapsible:not(.active) #copy-image-settings { + display: none; + } + #copy-image-settings.hidden { + opacity: 0; + pointer-events: none; + }`; + document.head.append(style); + const resetSettings = document.getElementById('reset-image-settings') + const copyIcon = document.createElement('i') + copyIcon.id = 'copy-image-settings' + copyIcon.className = 'fa-solid fa-clipboard' + copyIcon.innerHTML = `Copy Image Settings` + copyIcon.addEventListener('click', (event) => { + event.stopPropagation() + navigator.clipboard.writeText(JSON.stringify(readUIasJSON())) + }) + resetSettings.parentNode.insertBefore(copyIcon, resetSettings) + } +} +navigator.permissions.query({ name: "clipboard-write" }).then(checkWriteToClipboardPermission, (e) => { + if (e instanceof TypeError && typeof navigator?.clipboard?.writeText === 'function') { + // Fix for firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1560373 + checkWriteToClipboardPermission({state:"granted"}) + } +})