diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/node.py b/MKTextNode.py similarity index 84% rename from node.py rename to MKTextNode.py index 3134a94..5390852 100644 --- a/node.py +++ b/MKTextNode.py @@ -5,24 +5,28 @@ sys.path.append( os.path.dirname(os.path.abspath(__file__)) ) +# DisplayText node is forked from AlekPet,ZHO-ZHO-ZHO,thanks to AlekPet,ZHO-ZHO-ZHO! class ShowText: def __init__(self): pass @classmethod def INPUT_TYPES(s): - return { "required": { - "text": ("STRING"), + "text": ("STRING", {"forceInput": True}), }, } + RETURN_TYPES = ("STRING",) + RETURN_NAMES = ("text",) + OUTPUT_NODE = True + FUNCTION = "show_text" CATEGORY = "MK/text" def show_text(self, text): - return {"ui": {"text": [text,]}} + return {"ui": {"string": [text,]}, "result": (text,)} class CombinationText: def __init__(self): @@ -89,8 +93,8 @@ class ReplaceText: return { "required": { "text": ("STRING", {"multiline": True}), - "old_string": ("STRING",), - "new_string": ("STRING",), + "old_string": ("STRING", {"default": ""}), + "new_string": ("STRING", {"default": ""}), }, } diff --git a/__init__.py b/__init__.py index 6f490c8..5c03b52 100644 --- a/__init__.py +++ b/__init__.py @@ -1,10 +1,40 @@ -from .node import * +from .MKTextNode import * from .install import * +import filecmp +import shutil +import __main__ + +python = sys.executable + + +extentions_folder = os.path.join(os.path.dirname(os.path.realpath(__main__.__file__)), + "web" + os.sep + "extensions" + os.sep + "Comfy-MK") +javascript_folder = os.path.join(os.path.dirname(os.path.realpath(__file__)), "js") + +if not os.path.exists(extentions_folder): + os.mkdir(extentions_folder) + +result = filecmp.dircmp(javascript_folder, extentions_folder) + +if result.left_only or result.diff_files: + print('Update to javascripts files detected') + file_list = list(result.left_only) + file_list.extend(x for x in result.diff_files if x not in file_list) + + for file in file_list: + print(f'Copying {file} to extensions folder') + src_file = os.path.join(javascript_folder, file) + dst_file = os.path.join(extentions_folder, file) + if os.path.exists(dst_file): + os.remove(dst_file) + shutil.copy(src_file, dst_file) + -# A dictionary that contains all nodes you want to export with their names -# NOTE: names should be globally unique NODE_CLASS_MAPPINGS = { - "Show Text": ShowText + "ShowText": ShowText, + "CombinationText": CombinationText, + "PlaceholderText": PlaceholderText, + "ReplaceText": ReplaceText } __all__ = ['NODE_CLASS_MAPPINGS'] diff --git a/js/MKTextNode.js b/js/MKTextNode.js new file mode 100644 index 0000000..6eafbf6 --- /dev/null +++ b/js/MKTextNode.js @@ -0,0 +1,71 @@ + +import { app } from "/scripts/app.js"; +import { ComfyWidgets } from "/scripts/widgets.js"; + +app.registerExtension({ + name: "Comfy.MKTextNode", + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeData.name === "ShowText" && nodeData.category === "MK/text") { + // Node Created + const onNodeCreated = nodeType.prototype.onNodeCreated; + nodeType.prototype.onNodeCreated = function () { + const ret = onNodeCreated + ? onNodeCreated.apply(this, arguments) + : undefined; + + let ShowText = app.graph._nodes.filter( + (wi) => wi.type == nodeData.name + ), + nodeName = `${nodeData.name}_${ShowText.length}`; + + const wi = ComfyWidgets.STRING( + this, + nodeName, + [ + "STRING", + { + default: "", + placeholder: "Text output...", + multiline: true, + }, + ], + app + ); + wi.widget.inputEl.readOnly = true; + return ret; + }; + // Function set value + const outSet = function (texts) { + if (texts.length > 0) { + let widget_id = this?.widgets.findIndex( + (w) => w.type == "customtext" + ); + + if (Array.isArray(texts)) + texts = texts + .filter((word) => word.trim() !== "") + .map((word) => word.trim()) + .join(" "); + + this.widgets[widget_id].value = texts; + app.graph.setDirtyCanvas(true); + } + }; + + // onExecuted + const onExecuted = nodeType.prototype.onExecuted; + nodeType.prototype.onExecuted = function (texts) { + onExecuted?.apply(this, arguments); + outSet.call(this, texts?.string); + }; + // onConfigure + const onConfigure = nodeType.prototype.onConfigure; + nodeType.prototype.onConfigure = function (w) { + onConfigure?.apply(this, arguments); + if (w?.widgets_values?.length) { + outSet.call(this, w.widgets_values); + } + }; + } + }, +}); \ No newline at end of file