diff --git a/nodes/string_formatter.py b/nodes/string_formatter.py index 000c820..44a0aa6 100644 --- a/nodes/string_formatter.py +++ b/nodes/string_formatter.py @@ -47,6 +47,7 @@ class StringFormatter: # Format string using f-string style with the arguments dictionary result = eval(f"f'''{template}'''", args_dict) - return (result,) + return {"ui": {"formatted_string": (result,)}, "result": (result,)} except Exception as e: - return (f"Format Error: {str(e)}",) \ No newline at end of file + error_msg = f"Format Error: {str(e)}" + return {"ui": {"formatted_string": (error_msg,)}, "result": (error_msg,)} \ No newline at end of file diff --git a/web/js/showFormattedString.js b/web/js/showFormattedString.js new file mode 100644 index 0000000..596a138 --- /dev/null +++ b/web/js/showFormattedString.js @@ -0,0 +1,31 @@ +import { app } from "../../../scripts/app.js"; +import { ComfyWidgets } from "../../../scripts/widgets.js"; + +app.registerExtension({ + name: "ComfyUI-String-Helper.StringFormatter", + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeData.name === "StringFormatter") { + const onExecuted = nodeType.prototype.onExecuted; + nodeType.prototype.onExecuted = function (message) { + onExecuted?.apply(this, arguments); + + if (this.widgets) { + const pos = this.widgets.findIndex((w) => w.name === "formatted_text"); + if (pos !== -1) { + for (let i = pos; i < this.widgets.length; i++) { + this.widgets[i].onRemove?.(); + } + this.widgets.length = pos; + } + } + + const widget = ComfyWidgets["STRING"](this, "formatted_text", ["STRING", { multiline: true }], app).widget; + widget.inputEl.readOnly = true; + widget.inputEl.style.opacity = 0.6; + widget.value = message.formatted_string?.[0] ?? ""; + + this.onResize?.(this.size); + }; + } + } +});