From c0afd6c3fd7fae5c2ce48c064adc4cbccfca87b9 Mon Sep 17 00:00:00 2001 From: liuqianhonga Date: Sun, 15 Dec 2024 16:55:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodes/string_formatter.py | 5 +++-- web/js/showFormattedString.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 web/js/showFormattedString.js 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); + }; + } + } +});