2023-07-14 04:46:25 +00:00
|
|
|
import { api } from "./api.js"
|
|
|
|
|
2023-03-02 20:00:06 +00:00
|
|
|
function getNumberDefaults(inputData, defaultStep) {
|
|
|
|
let defaultVal = inputData[1]["default"];
|
|
|
|
let { min, max, step } = inputData[1];
|
|
|
|
|
|
|
|
if (defaultVal == undefined) defaultVal = 0;
|
|
|
|
if (min == undefined) min = 0;
|
|
|
|
if (max == undefined) max = 2048;
|
|
|
|
if (step == undefined) step = defaultStep;
|
|
|
|
|
|
|
|
return { val: defaultVal, config: { min, max, step: 10.0 * step } };
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:57:13 +00:00
|
|
|
export function addValueControlWidget(node, targetWidget, defaultValue = "randomize", values) {
|
|
|
|
const valueControl = node.addWidget("combo", "control_after_generate", defaultValue, function (v) { }, {
|
|
|
|
values: ["fixed", "increment", "decrement", "randomize"],
|
|
|
|
serialize: false, // Don't include this in prompt.
|
|
|
|
});
|
|
|
|
valueControl.afterQueued = () => {
|
|
|
|
|
|
|
|
var v = valueControl.value;
|
|
|
|
|
2023-05-16 07:18:11 +00:00
|
|
|
if (targetWidget.type == "combo" && v !== "fixed") {
|
|
|
|
let current_index = targetWidget.options.values.indexOf(targetWidget.value);
|
|
|
|
let current_length = targetWidget.options.values.length;
|
|
|
|
|
|
|
|
switch (v) {
|
|
|
|
case "increment":
|
|
|
|
current_index += 1;
|
|
|
|
break;
|
|
|
|
case "decrement":
|
|
|
|
current_index -= 1;
|
|
|
|
break;
|
|
|
|
case "randomize":
|
|
|
|
current_index = Math.floor(Math.random() * current_length);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_index = Math.max(0, current_index);
|
|
|
|
current_index = Math.min(current_length - 1, current_index);
|
|
|
|
if (current_index >= 0) {
|
|
|
|
let value = targetWidget.options.values[current_index];
|
|
|
|
targetWidget.value = value;
|
|
|
|
targetWidget.callback(value);
|
|
|
|
}
|
|
|
|
} else { //number
|
|
|
|
let min = targetWidget.options.min;
|
|
|
|
let max = targetWidget.options.max;
|
|
|
|
// limit to something that javascript can handle
|
|
|
|
max = Math.min(1125899906842624, max);
|
|
|
|
min = Math.max(-1125899906842624, min);
|
|
|
|
let range = (max - min) / (targetWidget.options.step / 10);
|
2023-04-13 00:57:13 +00:00
|
|
|
|
2023-05-16 07:18:11 +00:00
|
|
|
//adjust values based on valueControl Behaviour
|
|
|
|
switch (v) {
|
|
|
|
case "fixed":
|
|
|
|
break;
|
|
|
|
case "increment":
|
|
|
|
targetWidget.value += targetWidget.options.step / 10;
|
|
|
|
break;
|
|
|
|
case "decrement":
|
|
|
|
targetWidget.value -= targetWidget.options.step / 10;
|
|
|
|
break;
|
|
|
|
case "randomize":
|
|
|
|
targetWidget.value = Math.floor(Math.random() * range) * (targetWidget.options.step / 10) + min;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*check if values are over or under their respective
|
|
|
|
* ranges and set them to min or max.*/
|
|
|
|
if (targetWidget.value < min)
|
|
|
|
targetWidget.value = min;
|
|
|
|
|
|
|
|
if (targetWidget.value > max)
|
|
|
|
targetWidget.value = max;
|
|
|
|
}
|
2023-04-13 00:57:13 +00:00
|
|
|
}
|
|
|
|
return valueControl;
|
|
|
|
};
|
2023-03-23 21:37:19 +00:00
|
|
|
|
|
|
|
function seedWidget(node, inputName, inputData) {
|
|
|
|
const seed = ComfyWidgets.INT(node, inputName, inputData);
|
2023-04-13 00:57:13 +00:00
|
|
|
const seedControl = addValueControlWidget(node, seed.widget, "randomize");
|
2023-03-02 20:00:06 +00:00
|
|
|
|
2023-04-13 00:57:13 +00:00
|
|
|
seed.widget.linkedWidgets = [seedControl];
|
|
|
|
return seed;
|
2023-03-02 20:00:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 21:34:00 +00:00
|
|
|
const MultilineSymbol = Symbol();
|
2023-03-23 21:37:19 +00:00
|
|
|
const MultilineResizeSymbol = Symbol();
|
2023-03-21 21:34:00 +00:00
|
|
|
|
|
|
|
function addMultilineWidget(node, name, opts, app) {
|
|
|
|
const MIN_SIZE = 50;
|
|
|
|
|
2023-03-22 18:43:43 +00:00
|
|
|
function computeSize(size) {
|
|
|
|
if (node.widgets[0].last_y == null) return;
|
|
|
|
|
|
|
|
let y = node.widgets[0].last_y;
|
|
|
|
let freeSpace = size[1] - y;
|
|
|
|
|
|
|
|
// Compute the height of all non customtext widgets
|
|
|
|
let widgetHeight = 0;
|
|
|
|
const multi = [];
|
|
|
|
for (let i = 0; i < node.widgets.length; i++) {
|
|
|
|
const w = node.widgets[i];
|
|
|
|
if (w.type === "customtext") {
|
|
|
|
multi.push(w);
|
|
|
|
} else {
|
|
|
|
if (w.computeSize) {
|
|
|
|
widgetHeight += w.computeSize()[1] + 4;
|
|
|
|
} else {
|
|
|
|
widgetHeight += LiteGraph.NODE_WIDGET_HEIGHT + 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See how large each text input can be
|
|
|
|
freeSpace -= widgetHeight;
|
2023-06-06 13:27:44 +00:00
|
|
|
freeSpace /= multi.length + (!!node.imgs?.length);
|
2023-03-22 18:43:43 +00:00
|
|
|
|
|
|
|
if (freeSpace < MIN_SIZE) {
|
|
|
|
// There isnt enough space for all the widgets, increase the size of the node
|
|
|
|
freeSpace = MIN_SIZE;
|
2023-06-06 13:27:44 +00:00
|
|
|
node.size[1] = y + widgetHeight + freeSpace * (multi.length + (!!node.imgs?.length));
|
2023-03-22 18:50:45 +00:00
|
|
|
node.graph.setDirtyCanvas(true);
|
2023-03-22 18:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position each of the widgets
|
|
|
|
for (const w of node.widgets) {
|
|
|
|
w.y = y;
|
|
|
|
if (w.type === "customtext") {
|
|
|
|
y += freeSpace;
|
2023-07-07 16:42:33 +00:00
|
|
|
w.computedHeight = freeSpace - multi.length*4;
|
2023-03-22 18:43:43 +00:00
|
|
|
} else if (w.computeSize) {
|
|
|
|
y += w.computeSize()[1] + 4;
|
|
|
|
} else {
|
|
|
|
y += LiteGraph.NODE_WIDGET_HEIGHT + 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.inputHeight = freeSpace;
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:00:06 +00:00
|
|
|
const widget = {
|
|
|
|
type: "customtext",
|
|
|
|
name,
|
|
|
|
get value() {
|
|
|
|
return this.inputEl.value;
|
|
|
|
},
|
|
|
|
set value(x) {
|
|
|
|
this.inputEl.value = x;
|
|
|
|
},
|
|
|
|
draw: function (ctx, _, widgetWidth, y, widgetHeight) {
|
2023-03-22 18:43:43 +00:00
|
|
|
if (!this.parent.inputHeight) {
|
|
|
|
// If we are initially offscreen when created we wont have received a resize event
|
|
|
|
// Calculate it here instead
|
|
|
|
computeSize(node.size);
|
|
|
|
}
|
2023-03-23 21:37:19 +00:00
|
|
|
const visible = app.canvas.ds.scale > 0.5 && this.type === "customtext";
|
2023-03-02 20:00:06 +00:00
|
|
|
const margin = 10;
|
2023-05-19 18:57:15 +00:00
|
|
|
const elRect = ctx.canvas.getBoundingClientRect();
|
|
|
|
const transform = new DOMMatrix()
|
|
|
|
.scaleSelf(elRect.width / ctx.canvas.width, elRect.height / ctx.canvas.height)
|
|
|
|
.multiplySelf(ctx.getTransform())
|
|
|
|
.translateSelf(margin, margin + y);
|
|
|
|
|
2023-07-09 19:41:19 +00:00
|
|
|
const scale = new DOMMatrix().scaleSelf(transform.a, transform.d)
|
2023-03-02 20:00:06 +00:00
|
|
|
Object.assign(this.inputEl.style, {
|
2023-05-19 18:57:15 +00:00
|
|
|
transformOrigin: "0 0",
|
2023-07-09 19:41:19 +00:00
|
|
|
transform: scale,
|
|
|
|
left: `${transform.a + transform.e}px`,
|
|
|
|
top: `${transform.d + transform.f}px`,
|
2023-05-19 18:57:15 +00:00
|
|
|
width: `${widgetWidth - (margin * 2)}px`,
|
|
|
|
height: `${this.parent.inputHeight - (margin * 2)}px`,
|
2023-03-02 20:00:06 +00:00
|
|
|
position: "absolute",
|
2023-05-19 18:57:15 +00:00
|
|
|
background: (!node.color)?'':node.color,
|
2023-04-26 05:22:36 +00:00
|
|
|
color: (!node.color)?'':'white',
|
|
|
|
zIndex: app.graph._nodes.indexOf(node),
|
2023-03-02 20:00:06 +00:00
|
|
|
});
|
|
|
|
this.inputEl.hidden = !visible;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
widget.inputEl = document.createElement("textarea");
|
|
|
|
widget.inputEl.className = "comfy-multiline-input";
|
2023-03-21 21:34:00 +00:00
|
|
|
widget.inputEl.value = opts.defaultVal;
|
|
|
|
widget.inputEl.placeholder = opts.placeholder || "";
|
2023-03-18 18:52:19 +00:00
|
|
|
document.addEventListener("mousedown", function (event) {
|
2023-03-02 20:00:06 +00:00
|
|
|
if (!widget.inputEl.contains(event.target)) {
|
|
|
|
widget.inputEl.blur();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
widget.parent = node;
|
|
|
|
document.body.appendChild(widget.inputEl);
|
|
|
|
|
|
|
|
node.addCustomWidget(widget);
|
|
|
|
|
2023-03-03 20:10:18 +00:00
|
|
|
app.canvas.onDrawBackground = function () {
|
|
|
|
// Draw node isnt fired once the node is off the screen
|
|
|
|
// if it goes off screen quickly, the input may not be removed
|
|
|
|
// this shifts it off screen so it can be moved back if the node is visible.
|
|
|
|
for (let n in app.graph._nodes) {
|
|
|
|
n = graph._nodes[n];
|
|
|
|
for (let w in n.widgets) {
|
|
|
|
let wid = n.widgets[w];
|
|
|
|
if (Object.hasOwn(wid, "inputEl")) {
|
|
|
|
wid.inputEl.style.left = -8000 + "px";
|
|
|
|
wid.inputEl.style.position = "absolute";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-02 20:00:06 +00:00
|
|
|
node.onRemoved = function () {
|
|
|
|
// When removing this node we need to remove the input from the DOM
|
|
|
|
for (let y in this.widgets) {
|
|
|
|
if (this.widgets[y].inputEl) {
|
|
|
|
this.widgets[y].inputEl.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-23 21:37:19 +00:00
|
|
|
widget.onRemove = () => {
|
|
|
|
widget.inputEl?.remove();
|
|
|
|
|
|
|
|
// Restore original size handler if we are the last
|
|
|
|
if (!--node[MultilineSymbol]) {
|
|
|
|
node.onResize = node[MultilineResizeSymbol];
|
|
|
|
delete node[MultilineSymbol];
|
|
|
|
delete node[MultilineResizeSymbol];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (node[MultilineSymbol]) {
|
|
|
|
node[MultilineSymbol]++;
|
|
|
|
} else {
|
|
|
|
node[MultilineSymbol] = 1;
|
|
|
|
const onResize = (node[MultilineResizeSymbol] = node.onResize);
|
2023-03-22 18:52:24 +00:00
|
|
|
|
2023-03-21 21:34:00 +00:00
|
|
|
node.onResize = function (size) {
|
2023-03-22 18:43:43 +00:00
|
|
|
computeSize(size);
|
2023-03-21 21:34:00 +00:00
|
|
|
|
|
|
|
// Call original resizer handler
|
|
|
|
if (onResize) {
|
|
|
|
onResize.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:00:06 +00:00
|
|
|
return { minWidth: 400, minHeight: 200, widget };
|
|
|
|
}
|
|
|
|
|
2023-07-31 06:48:44 +00:00
|
|
|
function isSlider(display_as) {
|
|
|
|
return (display_as==="slider") ? "slider" : "number"
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:00:06 +00:00
|
|
|
export const ComfyWidgets = {
|
|
|
|
"INT:seed": seedWidget,
|
|
|
|
"INT:noise_seed": seedWidget,
|
|
|
|
FLOAT(node, inputName, inputData) {
|
2023-07-31 06:48:44 +00:00
|
|
|
let widgetType = isSlider(inputData[1]["display_as"]);
|
2023-03-02 20:00:06 +00:00
|
|
|
const { val, config } = getNumberDefaults(inputData, 0.5);
|
2023-07-31 06:48:44 +00:00
|
|
|
return { widget: node.addWidget(widgetType, inputName, val, () => {}, config) };
|
2023-03-02 20:00:06 +00:00
|
|
|
},
|
|
|
|
INT(node, inputName, inputData) {
|
2023-07-31 06:48:44 +00:00
|
|
|
let widgetType = isSlider(inputData[1]["display_as"]);
|
2023-03-02 20:00:06 +00:00
|
|
|
const { val, config } = getNumberDefaults(inputData, 1);
|
2023-03-21 21:34:00 +00:00
|
|
|
Object.assign(config, { precision: 0 });
|
2023-03-02 20:00:06 +00:00
|
|
|
return {
|
|
|
|
widget: node.addWidget(
|
2023-07-31 06:48:44 +00:00
|
|
|
widgetType,
|
2023-07-29 12:48:00 +00:00
|
|
|
inputName,
|
|
|
|
val,
|
|
|
|
function (v) {
|
|
|
|
const s = this.options.step / 10;
|
|
|
|
this.value = Math.round(v / s) * s;
|
|
|
|
},
|
|
|
|
config
|
2023-07-31 06:48:44 +00:00
|
|
|
),
|
2023-07-29 12:48:00 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
TOGGLE(node, inputName, inputData) {
|
|
|
|
let defaultVal = inputData[1]["default"];
|
|
|
|
return {
|
|
|
|
widget: node.addWidget(
|
|
|
|
"toggle",
|
|
|
|
inputName,
|
|
|
|
defaultVal,
|
|
|
|
() => {},
|
|
|
|
)
|
|
|
|
};
|
|
|
|
},
|
2023-03-02 20:00:06 +00:00
|
|
|
STRING(node, inputName, inputData, app) {
|
|
|
|
const defaultVal = inputData[1].default || "";
|
|
|
|
const multiline = !!inputData[1].multiline;
|
|
|
|
|
|
|
|
if (multiline) {
|
2023-03-21 21:34:00 +00:00
|
|
|
return addMultilineWidget(node, inputName, { defaultVal, ...inputData[1] }, app);
|
2023-03-02 20:00:06 +00:00
|
|
|
} else {
|
2023-03-03 18:28:34 +00:00
|
|
|
return { widget: node.addWidget("text", inputName, defaultVal, () => {}, {}) };
|
2023-03-02 20:00:06 +00:00
|
|
|
}
|
|
|
|
},
|
2023-03-23 21:37:19 +00:00
|
|
|
COMBO(node, inputName, inputData) {
|
|
|
|
const type = inputData[0];
|
|
|
|
let defaultValue = type[0];
|
|
|
|
if (inputData[1] && inputData[1].default) {
|
|
|
|
defaultValue = inputData[1].default;
|
|
|
|
}
|
|
|
|
return { widget: node.addWidget("combo", inputName, defaultValue, () => {}, { values: type }) };
|
|
|
|
},
|
2023-03-08 22:07:44 +00:00
|
|
|
IMAGEUPLOAD(node, inputName, inputData, app) {
|
|
|
|
const imageWidget = node.widgets.find((w) => w.name === "image");
|
|
|
|
let uploadWidget;
|
|
|
|
|
|
|
|
function showImage(name) {
|
|
|
|
const img = new Image();
|
|
|
|
img.onload = () => {
|
|
|
|
node.imgs = [img];
|
|
|
|
app.graph.setDirtyCanvas(true);
|
|
|
|
};
|
2023-05-08 18:13:06 +00:00
|
|
|
let folder_separator = name.lastIndexOf("/");
|
|
|
|
let subfolder = "";
|
|
|
|
if (folder_separator > -1) {
|
|
|
|
subfolder = name.substring(0, folder_separator);
|
|
|
|
name = name.substring(folder_separator + 1);
|
|
|
|
}
|
2023-07-14 04:46:25 +00:00
|
|
|
img.src = api.apiURL(`/view?filename=${name}&type=input&subfolder=${subfolder}${app.getPreviewFormatParam()}`);
|
2023-05-04 18:42:07 +00:00
|
|
|
node.setSizeForImage?.();
|
2023-03-08 22:07:44 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 18:13:06 +00:00
|
|
|
var default_value = imageWidget.value;
|
|
|
|
Object.defineProperty(imageWidget, "value", {
|
|
|
|
set : function(value) {
|
|
|
|
this._real_value = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
get : function() {
|
|
|
|
let value = "";
|
|
|
|
if (this._real_value) {
|
|
|
|
value = this._real_value;
|
|
|
|
} else {
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.filename) {
|
|
|
|
let real_value = value;
|
|
|
|
value = "";
|
|
|
|
if (real_value.subfolder) {
|
|
|
|
value = real_value.subfolder + "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
value += real_value.filename;
|
|
|
|
|
|
|
|
if(real_value.type && real_value.type !== "input")
|
|
|
|
value += ` [${real_value.type}]`;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-08 22:07:44 +00:00
|
|
|
// Add our own callback to the combo widget to render an image when it changes
|
|
|
|
const cb = node.callback;
|
|
|
|
imageWidget.callback = function () {
|
|
|
|
showImage(imageWidget.value);
|
|
|
|
if (cb) {
|
|
|
|
return cb.apply(this, arguments);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// On load if we have a value then render the image
|
|
|
|
// The value isnt set immediately so we need to wait a moment
|
|
|
|
// No change callbacks seem to be fired on initial setting of the value
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
if (imageWidget.value) {
|
|
|
|
showImage(imageWidget.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-14 21:13:29 +00:00
|
|
|
async function uploadFile(file, updateNode) {
|
|
|
|
try {
|
|
|
|
// Wrap file in formdata so it includes filename
|
|
|
|
const body = new FormData();
|
|
|
|
body.append("image", file);
|
2023-07-14 04:46:25 +00:00
|
|
|
const resp = await api.fetchApi("/upload/image", {
|
2023-03-14 21:13:29 +00:00
|
|
|
method: "POST",
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (resp.status === 200) {
|
|
|
|
const data = await resp.json();
|
|
|
|
// Add the file as an option and update the widget value
|
|
|
|
if (!imageWidget.options.values.includes(data.name)) {
|
|
|
|
imageWidget.options.values.push(data.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateNode) {
|
|
|
|
showImage(data.name);
|
|
|
|
|
|
|
|
imageWidget.value = data.name;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alert(resp.status + " - " + resp.statusText);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
alert(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 22:07:44 +00:00
|
|
|
const fileInput = document.createElement("input");
|
|
|
|
Object.assign(fileInput, {
|
|
|
|
type: "file",
|
2023-04-07 02:22:59 +00:00
|
|
|
accept: "image/jpeg,image/png,image/webp",
|
2023-03-08 22:07:44 +00:00
|
|
|
style: "display: none",
|
|
|
|
onchange: async () => {
|
|
|
|
if (fileInput.files.length) {
|
2023-03-14 21:13:29 +00:00
|
|
|
await uploadFile(fileInput.files[0], true);
|
2023-03-08 22:07:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
document.body.append(fileInput);
|
|
|
|
|
|
|
|
// Create the button widget for selecting the files
|
|
|
|
uploadWidget = node.addWidget("button", "choose file to upload", "image", () => {
|
|
|
|
fileInput.click();
|
|
|
|
});
|
|
|
|
uploadWidget.serialize = false;
|
|
|
|
|
2023-03-14 21:13:29 +00:00
|
|
|
// Add handler to check if an image is being dragged over our node
|
|
|
|
node.onDragOver = function (e) {
|
|
|
|
if (e.dataTransfer && e.dataTransfer.items) {
|
|
|
|
const image = [...e.dataTransfer.items].find((f) => f.kind === "file" && f.type.startsWith("image/"));
|
|
|
|
return !!image;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// On drop upload files
|
|
|
|
node.onDragDrop = function (e) {
|
|
|
|
console.log("onDragDrop called");
|
|
|
|
let handled = false;
|
|
|
|
for (const file of e.dataTransfer.files) {
|
|
|
|
if (file.type.startsWith("image/")) {
|
|
|
|
uploadFile(file, !handled); // Dont await these, any order is fine, only update on first one
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
};
|
|
|
|
|
2023-03-08 22:07:44 +00:00
|
|
|
return { widget: uploadWidget };
|
|
|
|
},
|
2023-03-02 20:00:06 +00:00
|
|
|
};
|