From 9064a006d5daa48a75133a79244e9d9dce6a30ca Mon Sep 17 00:00:00 2001 From: Victor Seiji Hariki Date: Tue, 17 Jan 2023 00:03:43 -0300 Subject: [PATCH] add send image to parent interface Signed-off-by: Victor Seiji Hariki --- index.html | 6 +++--- js/global.js | 3 +++ js/ui/tool/select.js | 34 +++++++++++++++++++++++++++++++++- js/webui.js | 39 ++++++++++++++++++++++++++++++++++++--- pages/embed.test.html | 4 ++-- 5 files changed, 77 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index d033f86..96fbc6c 100644 --- a/index.html +++ b/index.html @@ -345,7 +345,7 @@ - + @@ -388,7 +388,7 @@ src="js/ui/tool/colorbrush.js?v=3f8c01a" type="text/javascript"> - + diff --git a/js/global.js b/js/global.js index 91d0f81..446758f 100644 --- a/js/global.js +++ b/js/global.js @@ -53,6 +53,9 @@ const global = { // HRFix compatibility shenanigans isOldHRFix: false, + + // WebUI object to communitate with parent window + webui: null, }; global._firstRun = !localStorage.getItem("openoutpaint/host"); diff --git a/js/ui/tool/select.js b/js/ui/tool/select.js index 6a1095b..2432c8e 100644 --- a/js/ui/tool/select.js +++ b/js/ui/tool/select.js @@ -714,7 +714,7 @@ const selectTransformTool = () => createVisibleResourceButton.disabled = true; }; - // Disable buttons (if something is selected) + // Enable buttons (if something is selected) state.ctxmenu.enableButtons = () => { saveSelectionButton.disabled = ""; createResourceButton.disabled = ""; @@ -723,6 +723,21 @@ const selectTransformTool = () => }; state.ctxmenu.actionArray = actionArray; state.ctxmenu.visibleActionArray = visibleActionArray; + + // Send Selection to Destination + state.ctxmenu.sendSelected = document.createElement("select"); + state.ctxmenu.sendSelected.style.width = "100%"; + state.ctxmenu.sendSelected.addEventListener("change", (evn) => { + const v = evn.target.value; + if (state.selected && v !== "None") + global.webui && global.webui.sendTo(state.selected.canvas, v); + evn.target.value = "None"; + }); + + let opt = document.createElement("option"); + opt.textContent = "Send To..."; + opt.value = "None"; + state.ctxmenu.sendSelected.appendChild(opt); } const array = document.createElement("div"); array.classList.add("checkbox-array"); @@ -733,6 +748,23 @@ const selectTransformTool = () => menu.appendChild(state.ctxmenu.selectionPeekOpacitySlider); menu.appendChild(state.ctxmenu.actionArray); menu.appendChild(state.ctxmenu.visibleActionArray); + if (global.webui && global.webui.destinations) { + while (state.ctxmenu.sendSelected.lastChild.value !== "None") { + state.ctxmenu.sendSelected.removeChild( + state.ctxmenu.sendSelected.lastChild + ); + } + + global.webui.destinations.forEach((dst) => { + const opt = document.createElement("option"); + opt.textContent = dst.name; + opt.value = dst.id; + + state.ctxmenu.sendSelected.appendChild(opt); + }); + + menu.appendChild(state.ctxmenu.sendSelected); + } }, shortcut: "S", } diff --git a/js/webui.js b/js/webui.js index 9995582..de934f6 100644 --- a/js/webui.js +++ b/js/webui.js @@ -2,6 +2,32 @@ * This file should only be actually loaded if we are in a trusted environment. */ (async () => { + let parentWindow = null; + const webui = { + /** @type {{name: string, id: string}[]} */ + destinations: null, + + /** + * Sends a + * + * @param {HTMLCanvas} canvas Canvas to send the data of + * @param {string} destination The ID of the destination + */ + sendTo(canvas, destination) { + if (!this.destinations.find((d) => d.id === destination)) + throw new Error("[webui] Given destination is not available"); + + parentWindow && + parentWindow.postMessage({ + type: "openoutpaint/sendto", + message: { + image: canvas.toDataURL(), + destination, + }, + }); + }, + }; + // Check if key file exists const response = await fetch("key.json"); @@ -48,8 +74,6 @@ } if (data) { - let parentWindow = null; - if (!data.trusted) console.debug(`[webui] Loaded key`); window.addEventListener("message", ({data, origin, source}) => { @@ -65,6 +89,11 @@ console.warn(`[webui] Communication has not been initialized`); } + if (global.debug) { + console.debug("[webui] Received message:"); + console.debug(data); + } + try { switch (data.type) { case "openoutpaint/init": @@ -77,6 +106,7 @@ data.host, `Are you sure you want to modify the host?\nThis configuration was provided by the hosting page\n - ${parentWindow.document.title} (${origin})` ); + if (data.destinations) webui.destinations = data.destinations; break; case "openoutpaint/add-resource": @@ -142,4 +172,7 @@ } }); } -})(); + return webui; +})().then((value) => { + global.webui = value; +}); diff --git a/pages/embed.test.html b/pages/embed.test.html index b4ac0ab..ba38bc4 100644 --- a/pages/embed.test.html +++ b/pages/embed.test.html @@ -8,8 +8,8 @@