2022-08-30 13:37:16 +03:00
|
|
|
window.SD = (() => {
|
2022-08-30 16:33:27 +03:00
|
|
|
/*
|
|
|
|
* Painterro is made a field of the SD global object
|
|
|
|
* To provide convinience when using w() method in css_and_js.py
|
|
|
|
*/
|
2022-08-30 13:37:16 +03:00
|
|
|
class PainterroClass {
|
2022-08-30 16:33:27 +03:00
|
|
|
static isOpen = false;
|
2022-09-01 13:11:22 +03:00
|
|
|
static async init ({ x, toId }) {
|
2022-09-08 15:43:08 +03:00
|
|
|
console.log(x)
|
|
|
|
|
|
|
|
const originalImage = x[2] === 'Mask' ? x[1]?.image : x[0];
|
2022-08-30 16:33:27 +03:00
|
|
|
|
|
|
|
if (window.Painterro === undefined) {
|
|
|
|
try {
|
|
|
|
await this.load();
|
|
|
|
} catch (e) {
|
|
|
|
SDClass.error(e);
|
|
|
|
|
|
|
|
return this.fallback(originalImage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isOpen) {
|
|
|
|
return this.fallback(originalImage);
|
|
|
|
}
|
|
|
|
this.isOpen = true;
|
|
|
|
|
|
|
|
let resolveResult;
|
|
|
|
const paintClient = Painterro({
|
2022-08-30 13:37:16 +03:00
|
|
|
hiddenTools: ['arrow'],
|
2022-08-30 16:33:27 +03:00
|
|
|
onHide: () => {
|
|
|
|
resolveResult?.(null);
|
|
|
|
},
|
|
|
|
saveHandler: (image, done) => {
|
|
|
|
const data = image.asDataURL();
|
|
|
|
|
|
|
|
// ensures stable performance even
|
|
|
|
// when the editor is in interactive mode
|
|
|
|
SD.clearImageInput(SD.el.get(`#${toId}`));
|
|
|
|
|
|
|
|
resolveResult(data);
|
|
|
|
|
2022-08-30 13:37:16 +03:00
|
|
|
done(true);
|
2022-08-30 16:33:27 +03:00
|
|
|
paintClient.hide();
|
2022-08-30 13:37:16 +03:00
|
|
|
},
|
2022-08-30 16:33:27 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const result = await new Promise((resolve) => {
|
|
|
|
resolveResult = resolve;
|
|
|
|
paintClient.show(originalImage);
|
|
|
|
});
|
|
|
|
this.isOpen = false;
|
|
|
|
|
|
|
|
return result ? this.success(result) : this.fallback(originalImage);
|
2022-08-30 13:37:16 +03:00
|
|
|
}
|
2022-09-08 15:43:08 +03:00
|
|
|
static success (result) { return [result, { image: result, mask: result }] };
|
|
|
|
static fallback (image) { return [image, { image: image, mask: image }] };
|
2022-08-30 16:33:27 +03:00
|
|
|
static load () {
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-30 22:56:20 +03:00
|
|
|
const scriptId = '__painterro-script';
|
|
|
|
if (document.getElementById(scriptId)) {
|
|
|
|
reject(new Error('Tried to load painterro script, but script tag already exists.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const styleId = '__painterro-css-override';
|
|
|
|
if (!document.getElementById(styleId)) {
|
|
|
|
/* Ensure Painterro window is always on top */
|
|
|
|
const style = document.createElement('style');
|
|
|
|
style.id = styleId;
|
|
|
|
style.setAttribute('type', 'text/css');
|
|
|
|
style.appendChild(document.createTextNode(`
|
|
|
|
.ptro-holder-wrapper {
|
|
|
|
z-index: 100;
|
|
|
|
}
|
|
|
|
`));
|
|
|
|
document.head.appendChild(style);
|
|
|
|
}
|
2022-08-30 13:37:16 +03:00
|
|
|
|
2022-08-30 16:33:27 +03:00
|
|
|
const script = document.createElement('script');
|
2022-08-30 22:56:20 +03:00
|
|
|
script.id = scriptId;
|
2022-08-30 16:33:27 +03:00
|
|
|
script.src = 'https://unpkg.com/painterro@1.2.78/build/painterro.min.js';
|
|
|
|
script.onload = () => resolve(true);
|
2022-08-30 22:56:20 +03:00
|
|
|
script.onerror = (e) => {
|
|
|
|
// remove self on error to enable reattempting load
|
|
|
|
document.head.removeChild(script);
|
|
|
|
reject(e);
|
|
|
|
};
|
2022-08-30 16:33:27 +03:00
|
|
|
document.head.appendChild(script);
|
|
|
|
});
|
2022-08-30 13:37:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turns out caching elements doesn't actually work in gradio
|
|
|
|
* As elements in tabs might get recreated
|
|
|
|
*/
|
|
|
|
class ElementCache {
|
|
|
|
#el;
|
|
|
|
constructor () {
|
|
|
|
this.root = document.querySelector('gradio-app').shadowRoot;
|
|
|
|
}
|
|
|
|
get (selector) {
|
|
|
|
return this.root.querySelector(selector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The main helper class to incapsulate functions
|
|
|
|
* that change gradio ui functionality
|
|
|
|
*/
|
2022-08-30 16:33:27 +03:00
|
|
|
class SDClass {
|
2022-08-30 13:37:16 +03:00
|
|
|
el = new ElementCache();
|
|
|
|
Painterro = PainterroClass;
|
2022-09-01 13:11:22 +03:00
|
|
|
moveImageFromGallery ({ x, fromId, toId }) {
|
2022-09-08 15:43:08 +03:00
|
|
|
x = x[0];
|
2022-09-01 13:11:22 +03:00
|
|
|
if (!Array.isArray(x) || x.length === 0) return;
|
2022-08-30 13:37:16 +03:00
|
|
|
|
|
|
|
this.clearImageInput(this.el.get(`#${toId}`));
|
|
|
|
|
|
|
|
const i = this.#getGallerySelectedIndex(this.el.get(`#${fromId}`));
|
|
|
|
|
2022-09-01 13:11:22 +03:00
|
|
|
return [x[i].replace('data:;','data:image/png;')];
|
2022-08-30 13:37:16 +03:00
|
|
|
}
|
2022-09-01 13:11:22 +03:00
|
|
|
async copyImageFromGalleryToClipboard ({ x, fromId }) {
|
2022-09-08 15:43:08 +03:00
|
|
|
x = x[0];
|
2022-09-01 13:11:22 +03:00
|
|
|
if (!Array.isArray(x) || x.length === 0) return;
|
2022-08-30 13:37:16 +03:00
|
|
|
|
|
|
|
const i = this.#getGallerySelectedIndex(this.el.get(`#${fromId}`));
|
|
|
|
|
2022-09-01 13:11:22 +03:00
|
|
|
const data = x[i];
|
2022-08-30 13:37:16 +03:00
|
|
|
const blob = await (await fetch(data.replace('data:;','data:image/png;'))).blob();
|
|
|
|
const item = new ClipboardItem({'image/png': blob});
|
|
|
|
|
2022-09-01 13:11:22 +03:00
|
|
|
await this.copyToClipboard([item]);
|
2022-08-30 13:37:16 +03:00
|
|
|
}
|
2022-09-01 13:11:22 +03:00
|
|
|
clickFirstVisibleButton({ rowId }) {
|
2022-08-30 22:18:29 +03:00
|
|
|
const generateButtons = this.el.get(`#${rowId}`).querySelectorAll('.gr-button-primary');
|
|
|
|
|
|
|
|
if (!generateButtons) return;
|
|
|
|
|
|
|
|
for (let i = 0, arr = [...generateButtons]; i < arr.length; i++) {
|
|
|
|
const cs = window.getComputedStyle(arr[i]);
|
|
|
|
|
|
|
|
if (cs.display !== 'none' && cs.visibility !== 'hidden') {
|
|
|
|
console.log(arr[i]);
|
|
|
|
|
|
|
|
arr[i].click();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 15:43:08 +03:00
|
|
|
async gradioInputToClipboard ({ x }) { return this.copyToClipboard(x[0]); }
|
2022-09-01 13:11:22 +03:00
|
|
|
async copyToClipboard (value) {
|
|
|
|
if (!value || typeof value === 'boolean') return;
|
|
|
|
try {
|
|
|
|
if (Array.isArray(value) &&
|
|
|
|
value.length &&
|
|
|
|
value[0] instanceof ClipboardItem) {
|
|
|
|
await navigator.clipboard.write(value);
|
|
|
|
} else {
|
|
|
|
await navigator.clipboard.writeText(value);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
SDClass.error(e);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 16:33:27 +03:00
|
|
|
static error (e) {
|
|
|
|
console.error(e);
|
|
|
|
if (typeof e === 'string') {
|
|
|
|
alert(e);
|
|
|
|
} else if(typeof e === 'object' && Object.hasOwn(e, 'message')) {
|
|
|
|
alert(e.message);
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 13:11:22 +03:00
|
|
|
clearImageInput (imageEditor) {
|
|
|
|
imageEditor?.querySelector('.modify-upload button:last-child')?.click();
|
|
|
|
}
|
2022-08-30 13:37:16 +03:00
|
|
|
#getGallerySelectedIndex (gallery) {
|
|
|
|
const selected = gallery.querySelector(`.\\!ring-2`);
|
|
|
|
return selected ? [...selected.parentNode.children].indexOf(selected) : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 16:33:27 +03:00
|
|
|
return new SDClass();
|
2022-08-30 13:37:16 +03:00
|
|
|
})();
|