Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,906 Bytes
3d5837a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import { ComfyApp, app } from "../../scripts/app.js";
import { ComfyDialog, $el } from "../../scripts/ui.js";
import { api } from "../../scripts/api.js";
async function open_picker(node) {
const resp = await api.fetchApi(`/impact/segs/picker/count?id=${node.id}`);
const body = await resp.text();
let cnt = parseInt(body);
var existingPicker = document.getElementById('impact-picker');
if (existingPicker) {
existingPicker.parentNode.removeChild(existingPicker);
}
var gallery = document.createElement('div');
gallery.id = 'impact-picker';
gallery.style.position = "absolute";
gallery.style.height = "80%";
gallery.style.width = "80%";
gallery.style.top = "10%";
gallery.style.left = "10%";
gallery.style.display = 'flex';
gallery.style.flexWrap = 'wrap';
gallery.style.maxHeight = '600px';
gallery.style.overflow = 'auto';
gallery.style.backgroundColor = 'rgba(0,0,0,0.3)';
gallery.style.padding = '20px';
gallery.draggable = false;
gallery.style.zIndex = 5000;
var doneButton = document.createElement('button');
doneButton.textContent = 'Done';
doneButton.style.padding = '10px 10px';
doneButton.style.border = 'none';
doneButton.style.borderRadius = '5px';
doneButton.style.fontFamily = 'Arial, sans-serif';
doneButton.style.fontSize = '16px';
doneButton.style.fontWeight = 'bold';
doneButton.style.color = '#fff';
doneButton.style.background = 'linear-gradient(to bottom, #0070B8, #003D66)';
doneButton.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.4)';
doneButton.style.margin = "20px";
doneButton.style.height = "40px";
var cancelButton = document.createElement('button');
cancelButton.textContent = 'Cancel';
cancelButton.style.padding = '10px 10px';
cancelButton.style.border = 'none';
cancelButton.style.borderRadius = '5px';
cancelButton.style.fontFamily = 'Arial, sans-serif';
cancelButton.style.fontSize = '16px';
cancelButton.style.fontWeight = 'bold';
cancelButton.style.color = '#fff';
cancelButton.style.background = 'linear-gradient(to bottom, #ff70B8, #ff3D66)';
cancelButton.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.4)';
cancelButton.style.margin = "20px";
cancelButton.style.height = "40px";
const w = node.widgets.find((w) => w.name == 'picks');
let prev_selected = w.value.split(',').map(function(item) {
return parseInt(item, 10);
});
let images = [];
doneButton.onclick = () => {
var result = '';
for(let i in images) {
if(images[i].isSelected) {
if(result != '')
result += ', ';
result += (parseInt(i)+1);
}
}
w.value = result;
gallery.parentNode.removeChild(gallery);
}
cancelButton.onclick = () => {
gallery.parentNode.removeChild(gallery);
}
var panel = document.createElement('div');
panel.style.clear = 'both';
panel.style.width = '100%';
panel.style.height = '40px';
panel.style.justifyContent = 'center';
panel.style.alignItems = 'center';
panel.style.display = 'flex';
panel.appendChild(doneButton);
panel.appendChild(cancelButton);
gallery.appendChild(panel);
var hint = document.createElement('label');
hint.style.position = 'absolute';
hint.innerHTML = 'Click: Toggle Selection<BR>Ctrl-click: Single Selection';
gallery.appendChild(hint);
let max_size = 300;
for(let i=0; i<cnt; i++) {
let image = new Image();
image.src = `/impact/segs/picker/view?id=${node.id}&idx=${i}`;
image.style.margin = '10px';
image.draggable = false;
images.push(image);
image.isSelected = prev_selected.includes(i + 1);
if(image.isSelected) {
image.style.border = '2px solid #006699';
}
image.onload = function() {
var ratio = 1.0;
if(image.naturalWidth > image.naturalHeight) {
ratio = max_size/image.naturalWidth;
}
else {
ratio = max_size/image.naturalHeight;
}
let width = image.naturalWidth * ratio;
let height = image.naturalHeight * ratio;
if(width < height) {
this.style.marginLeft = (200-width)/2+"px";
}
else{
this.style.marginTop = (200-height)/2+"px";
}
this.style.width = width+"px";
this.style.height = height+"px";
this.style.objectFit = 'cover';
}
image.addEventListener('click', function(event) {
if(event.ctrlKey) {
for(let i in images) {
if(images[i].isSelected) {
images[i].style.border = 'none';
images[i].isSelected = false;
}
}
image.style.border = '2px solid #006699';
image.isSelected = true;
return;
}
if(image.isSelected) {
image.style.border = 'none';
image.isSelected = false;
}
else {
image.style.border = '2px solid #006699';
image.isSelected = true;
}
});
gallery.appendChild(image);
}
document.body.appendChild(gallery);
}
app.registerExtension({
name: "Comfy.Impack.Picker",
nodeCreated(node, app) {
if(node.comfyClass == "ImpactSEGSPicker") {
node.addWidget("button", "pick", "image", () => {
open_picker(node);
});
}
}
}); |