Files
phazei-ComfyUI-Prompt-Stash/prompt_stash_manager_node.py
Adam 52ebe72556 feat: sort lists, meta output, and preserve custom widget labels
- Manager: add "Sort A-Z" button that sorts the selected list's prompts
  case-insensitively and persists the new order (closes #20)
- Saver: add second "meta" STRING output with JSON describing the prompt
  (prompt_name, list_name, source, etc.) for downstream nodes (closes #19)
- Saver: include save_as_key/load_saved/prompt_lists in IS_CHANGED so a
  cached meta output stays accurate
- All nodes: only apply default widget labels when the user hasn't set a
  custom one, so renames survive reload (#13)
- Bump version to 1.7.0
2026-09-13 16:55:58 -07:00

105 lines
3.3 KiB
Python

import os
import json
from server import PromptServer
from .data_utils import init_data_file
class PromptStashManager:
def __init__(self):
self.base_dir = os.path.dirname(os.path.realpath(__file__))
self.data_file = init_data_file(self.base_dir)
self.data = self.load_data()
@classmethod
def INPUT_TYPES(s):
return {
"required": {
},
"optional": {
"new_list_name": ("STRING", {"default": "", "placeholder": "Enter new list name"}),
}
}
RETURN_TYPES = () # No outputs
FUNCTION = "process"
CATEGORY = "utils"
def load_data(self):
if os.path.exists(self.data_file):
try:
with open(self.data_file, 'r', encoding='utf-8') as f:
data = json.load(f)
return data
except Exception as e:
print(f"Error loading prompts: {e}")
return {"lists": {"default": {}}}
def save_data(self):
try:
os.makedirs(os.path.dirname(self.data_file), exist_ok=True)
with open(self.data_file, 'w', encoding='utf-8') as f:
json.dump(self.data, f, indent=2)
return True
except Exception as e:
print(f"Error saving data: {e}")
return False
def add_list(self, list_name):
list_name = list_name.strip()
if not list_name:
return False
if list_name not in self.data["lists"]:
self.data["lists"][list_name] = {}
success = self.save_data()
if success:
# Notify all nodes of the update
PromptServer.instance.send_sync("prompt-stash-update-all", {
"lists": self.data["lists"]
})
return success
return False
def delete_list(self, list_name):
# Prevent deleting default list
# if list_name == "default":
# return False
if len(self.data["lists"]) <= 1:
return False
if list_name in self.data["lists"]:
del self.data["lists"][list_name]
success = self.save_data()
if success:
# Notify all nodes of the update
PromptServer.instance.send_sync("prompt-stash-update-all", {
"lists": self.data["lists"]
})
return success
return False
def sort_list(self, list_name):
"""Sort the prompts in a list alphabetically (case-insensitive) by name.
Rewrites the list's key order in the data file so the new order is
persistent and reflected in exports and every saver node's dropdown.
"""
if list_name not in self.data["lists"]:
return False
prompts = self.data["lists"][list_name]
self.data["lists"][list_name] = dict(
sorted(prompts.items(), key=lambda kv: kv[0].casefold())
)
success = self.save_data()
if success:
# Notify all nodes of the update
PromptServer.instance.send_sync("prompt-stash-update-all", {
"lists": self.data["lists"]
})
return success
def process(self, new_list_name=""):
return ()