| import { ControlNetUnit } from "./controlnet_unit.mjs"; |
|
|
| export class OpenposeEditor { |
| |
| |
| |
| |
| constructor(unit) { |
| this.unit = unit; |
| this.iframe = this.unit.generatedImageGroup.querySelector('.cnet-modal iframe'); |
| this.closeModalButton = this.unit.generatedImageGroup.querySelector('.cnet-modal .cnet-modal-close'); |
| this.uploadButton = this.unit.inputImageGroup.querySelector('.cnet-upload-pose input'); |
| this.editorURL = null; |
| this.unit.poseEditButton.addEventListener('click', this.trigger.bind(this)); |
| |
| window.addEventListener('message', ((event) => { |
| const message = event.data; |
| const modalId = this.unit.poseEditButton.id.replace('cnet-modal-open-', ''); |
| if (message.modalId !== modalId) return; |
| this.updatePreviewPose(message.poseURL); |
| this.closeModalButton.click(); |
| }).bind(this)); |
| |
| this.uploadButton.addEventListener('change', ((event) => { |
| const file = event.target.files[0]; |
| if (!file) |
| return; |
|
|
| const reader = new FileReader(); |
| reader.onload = (e) => { |
| const contents = e.target.result; |
| const poseURL = `data:application/json;base64,${btoa(contents)}`; |
| this.updatePreviewPose(poseURL); |
| }; |
| reader.readAsText(file); |
| |
| event.target.value = ''; |
| }).bind(this)); |
| } |
|
|
| async checkEditorAvailable() { |
| const LOCAL_EDITOR_PATH = '/openpose_editor_index'; |
| const REMOTE_EDITOR_PATH = 'https://huchenlei.github.io/sd-webui-openpose-editor/'; |
|
|
| async function testEditorPath(path) { |
| const res = await fetch(path); |
| return res.status === 200 ? path : null; |
| } |
| |
| |
| |
| |
| return await testEditorPath(LOCAL_EDITOR_PATH) || await testEditorPath(REMOTE_EDITOR_PATH); |
| } |
|
|
| navigateIframe() { |
| const iframe = this.iframe; |
| const editorURL = this.editorURL; |
|
|
| function getPathname(rawURL) { |
| try { |
| return new URL(rawURL).pathname; |
| } catch (e) { |
| return rawURL; |
| } |
| } |
|
|
| return new Promise((resolve) => { |
| const darkThemeParam = document.body.classList.contains('dark') ? |
| new URLSearchParams({ theme: 'dark' }).toString() : |
| ''; |
|
|
| window.addEventListener('message', (event) => { |
| const message = event.data; |
| if (message['ready']) resolve(); |
| }, { once: true }); |
|
|
| if ((editorURL.startsWith("http") ? iframe.src : getPathname(iframe.src)) !== editorURL) { |
| iframe.src = `${editorURL}?${darkThemeParam}`; |
| |
| |
| setTimeout(resolve, 5000); |
| } else { |
| |
| resolve(); |
| } |
| }); |
| } |
|
|
| |
| async trigger() { |
| const inputImageGroup = this.unit.tab.querySelector('.cnet-input-image-group'); |
| const inputImage = inputImageGroup.querySelector('.cnet-image img'); |
| const downloadLink = this.unit.generatedImageGroup.querySelector('.cnet-download-pose a'); |
| const modalId = this.unit.poseEditButton.id.replace('cnet-modal-open-', ''); |
|
|
| if (!this.editorURL) { |
| this.editorURL = await this.checkEditorAvailable(); |
| if (!this.editorURL) { |
| alert("No openpose editor available.") |
| } |
| } |
|
|
| await this.navigateIframe(); |
| this.iframe.contentWindow.postMessage({ |
| modalId, |
| imageURL: inputImage ? inputImage.src : undefined, |
| poseURL: downloadLink.href, |
| }, '*'); |
| |
| |
| |
| this.iframe.contentWindow.focus(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| updatePreviewPose(poseURL) { |
| const downloadLink = this.unit.generatedImageGroup.querySelector('.cnet-download-pose a'); |
| const renderButton = this.unit.generatedImageGroup.querySelector('.cnet-render-pose'); |
| const poseTextbox = this.unit.generatedImageGroup.querySelector('.cnet-pose-json textarea'); |
| const allowPreviewCheckbox = this.unit.allowPreviewCheckbox; |
|
|
| if (!allowPreviewCheckbox.checked) |
| allowPreviewCheckbox.click(); |
|
|
| |
| |
| |
| if (downloadLink !== null) |
| downloadLink.href = poseURL; |
|
|
| poseTextbox.value = poseURL; |
| |
| |
| function updateInput(target) { |
| let e = new Event("input", { bubbles: true }) |
| Object.defineProperty(e, "target", { value: target }) |
| target.dispatchEvent(e); |
| } |
| updateInput(poseTextbox); |
| renderButton.click(); |
| } |
| } |
|
|