|
import { app } from "../../scripts/app.js";
|
|
import { api } from "../../scripts/api.js";
|
|
|
|
import { is_UEnode, is_helper, inject, Logger, get_real_node } from "./use_everywhere_utilities.js";
|
|
import { displayMessage, update_input_label, indicate_restriction } from "./use_everywhere_ui.js";
|
|
import { LinkRenderController } from "./use_everywhere_ui.js";
|
|
import { autoCreateMenu } from "./use_everywhere_autocreate.js";
|
|
import { add_autoprompts } from "./use_everywhere_autoprompt.js";
|
|
import { GraphAnalyser } from "./use_everywhere_graph_analysis.js";
|
|
import { main_menu_settings, node_menu_settings, canvas_menu_settings } from "./use_everywhere_settings.js";
|
|
import { add_debug } from "./ue_debug.js";
|
|
|
|
|
|
|
|
|
|
var linkRenderController;
|
|
var graphAnalyser;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function inject_outdating_into_objects(array, methodname, tracetext) {
|
|
if (array) {
|
|
array.forEach((object) => { inject_outdating_into_object_method(object, methodname, tracetext); })
|
|
}
|
|
}
|
|
function inject_outdating_into_object_method(object, methodname, tracetext) {
|
|
if (object) inject(object, methodname, tracetext, linkRenderController.mark_link_list_outdated, linkRenderController);
|
|
}
|
|
|
|
const nodeHandler = {
|
|
set: function(obj, property, value) {
|
|
const oldValue = Reflect.get(obj, property, this);
|
|
const result = Reflect.set(...arguments);
|
|
if (oldValue!=value) {
|
|
if (property==='bgcolor') {
|
|
if (obj.mode!=4) linkRenderController.mark_link_list_outdated();
|
|
}
|
|
if (property==='mode') {
|
|
linkRenderController.mark_link_list_outdated();
|
|
obj.widgets?.forEach((widget) => {widget.onModeChange?.(value)});
|
|
}
|
|
}
|
|
return result;
|
|
},
|
|
}
|
|
|
|
app.registerExtension({
|
|
name: "cg.customnodes.use_everywhere",
|
|
|
|
async beforeRegisterNodeDef(nodeType, nodeData, app) {
|
|
|
|
|
|
|
|
|
|
const onConnectionsChange = nodeType.prototype.onConnectionsChange;
|
|
nodeType.prototype.onConnectionsChange = function (side,slot,connect,link_info,output) {
|
|
Logger.trace("onConnectionsChange", arguments, this);
|
|
if (this.IS_UE && side==1) {
|
|
if (this.type=="Anything Everywhere?" && slot!=0) {
|
|
|
|
} else {
|
|
const type = (connect && link_info) ? get_real_node(link_info?.origin_id)?.outputs[link_info?.origin_slot]?.type : undefined;
|
|
this.input_type[slot] = type;
|
|
if (link_info) link_info.type = type ? type : "*";
|
|
update_input_label(this, slot, app);
|
|
}
|
|
}
|
|
linkRenderController.mark_link_list_outdated();
|
|
onConnectionsChange?.apply(this, arguments);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getExtraMenuOptions = nodeType.prototype.getExtraMenuOptions;
|
|
nodeType.prototype.getExtraMenuOptions = function(_, options) {
|
|
Logger.trace("getExtraMenuOptions", arguments, this);
|
|
getExtraMenuOptions?.apply(this, arguments);
|
|
if (is_UEnode(this)) {
|
|
node_menu_settings(options, this);
|
|
|
|
}
|
|
|
|
inject_outdating_into_objects(options,'callback',`menu option on ${this.id}`);
|
|
}
|
|
|
|
if (is_UEnode(nodeType)) {
|
|
const onNodeCreated = nodeType.prototype.onNodeCreated;
|
|
nodeType.prototype.onNodeCreated = function () {
|
|
const r = onNodeCreated ? onNodeCreated.apply(this, arguments) : undefined;
|
|
if (!this.properties) this.properties = {}
|
|
this.properties.group_restricted = 0;
|
|
this.properties.color_restricted = 0;
|
|
if (this.inputs) {
|
|
if (!this.widgets) this.widgets = [];
|
|
for (const input of this.inputs) {
|
|
if (input.widget && !this.widgets.find((w) => w.name === input.widget.name)) this.widgets.push(input.widget)
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
}
|
|
},
|
|
|
|
async nodeCreated(node) {
|
|
node.IS_UE = is_UEnode(node);
|
|
if (node.IS_UE) {
|
|
node.input_type = [undefined, undefined, undefined];
|
|
node.displayMessage = displayMessage;
|
|
|
|
|
|
inject_outdating_into_objects(node.widgets,'callback',`widget callback on ${node.id}`);
|
|
|
|
|
|
const original_onDrawTitleBar = node.onDrawTitleBar;
|
|
node.onDrawTitleBar = function(ctx, title_height) {
|
|
original_onDrawTitleBar?.apply(this, arguments);
|
|
if (node.properties.group_restricted || node.properties.color_restricted) indicate_restriction(ctx, title_height);
|
|
}
|
|
}
|
|
|
|
if (is_helper(node)) {
|
|
inject_outdating_into_objects(node.widgets,'callback',`widget callback on ${this.id}`);
|
|
}
|
|
|
|
|
|
inject_outdating_into_object_method(node, 'onRemoved', `node ${node.id} removed`)
|
|
|
|
|
|
setTimeout( ()=>{linkRenderController.mark_link_list_outdated()}, 100 );
|
|
},
|
|
|
|
loadedGraphNode(node) { if (node.flags.collapsed && node.loaded_when_collapsed) node.loaded_when_collapsed(); },
|
|
|
|
async setup() {
|
|
const head = document.getElementsByTagName('HEAD')[0];
|
|
const link = document.createElement('link');
|
|
link.rel = 'stylesheet';
|
|
link.type = 'text/css';
|
|
link.href = 'extensions/cg-use-everywhere/ue.css';
|
|
head.appendChild(link);
|
|
|
|
|
|
|
|
|
|
function messageHandler(event) {
|
|
const id = event.detail.id;
|
|
const message = event.detail.message;
|
|
const node = get_real_node(id);
|
|
if (node && node.displayMessage) node.displayMessage(id, message);
|
|
else (console.log(`node ${id} couldn't handle a message`));
|
|
}
|
|
api.addEventListener("ue-message-handler", messageHandler);
|
|
|
|
api.addEventListener("status", ({detail}) => {
|
|
if (linkRenderController) linkRenderController.note_queue_size(detail ? detail.exec_info.queue_remaining : 0)
|
|
});
|
|
|
|
|
|
|
|
|
|
const _original_save_onclick = document.getElementById('comfy-save-button').onclick;
|
|
document.getElementById('comfy-save-button').onclick = function() {
|
|
graphAnalyser.pause();
|
|
_original_save_onclick();
|
|
graphAnalyser.unpause()
|
|
}
|
|
const _original_save_api_onclick = document.getElementById('comfy-dev-save-api-button').onclick;
|
|
document.getElementById('comfy-dev-save-api-button').onclick = function() {
|
|
graphAnalyser.pause();
|
|
_original_save_api_onclick();
|
|
graphAnalyser.unpause();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const original_drawNode = LGraphCanvas.prototype.drawNode;
|
|
LGraphCanvas.prototype.drawNode = function(node, ctx) {
|
|
original_drawNode.apply(this, arguments);
|
|
linkRenderController.highlight_ue_connections(node, ctx);
|
|
}
|
|
|
|
|
|
|
|
|
|
const drawConnections = LGraphCanvas.prototype.drawConnections;
|
|
LGraphCanvas.prototype.drawConnections = function(ctx) {
|
|
drawConnections?.apply(this, arguments);
|
|
linkRenderController.render_all_ue_links(ctx);
|
|
}
|
|
|
|
main_menu_settings();
|
|
|
|
|
|
|
|
|
|
|
|
const original_getCanvasMenuOptions = LGraphCanvas.prototype.getCanvasMenuOptions;
|
|
LGraphCanvas.prototype.getCanvasMenuOptions = function () {
|
|
|
|
const options = original_getCanvasMenuOptions.apply(this, arguments);
|
|
canvas_menu_settings(options);
|
|
|
|
|
|
inject_outdating_into_objects(options,'callback',`menu option on canvas`);
|
|
|
|
return options;
|
|
}
|
|
|
|
|
|
|
|
|
|
const original_showConnectionMenu = LGraphCanvas.prototype.showConnectionMenu;
|
|
LGraphCanvas.prototype.showConnectionMenu = function (optPass) {
|
|
if (optPass.e.shiftKey) {
|
|
autoCreateMenu.apply(this, arguments);
|
|
} else {
|
|
this.use_original_menu = true;
|
|
original_showConnectionMenu.apply(this, arguments);
|
|
this.use_original_menu = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var original_allow_searchbox = app.canvas.allow_searchbox;
|
|
Object.defineProperty(app.canvas, 'allow_searchbox', {
|
|
get : function() {
|
|
if (this.use_original_menu) { return original_allow_searchbox; }
|
|
if(app.ui.settings.getSettingValue('AE.replacesearch', true) && this.connecting_output) {
|
|
return false;
|
|
} else { return original_allow_searchbox; }
|
|
},
|
|
set : function(v) { original_allow_searchbox = v; }
|
|
});
|
|
|
|
|
|
},
|
|
|
|
init() {
|
|
graphAnalyser = GraphAnalyser.instance();
|
|
app.graphToPrompt = async function () {
|
|
return graphAnalyser.analyse_graph(true, true, false);
|
|
}
|
|
|
|
linkRenderController = LinkRenderController.instance(graphAnalyser);
|
|
|
|
add_autoprompts();
|
|
const createNode = LiteGraph.createNode;
|
|
LiteGraph.createNode = function() {
|
|
const nd = createNode.apply(this,arguments);
|
|
if (nd && nd.IS_UE) {
|
|
return new Proxy( nd, nodeHandler );
|
|
} else {
|
|
return nd;
|
|
}
|
|
}
|
|
|
|
if (false) add_debug();
|
|
|
|
}
|
|
|
|
});
|
|
|