// ComfyUI side of Text Fix: an "Open editor" button on the node. The editor is the same page // the A1111 family shows (ui/), in a window over ComfyUI. The node is the memory: the page reads // the node's widgets on opening and writes every change back, so the next run of the workflow // gives the picture the editor showed. import { app } from "../../scripts/app.js"; const NODE_TYPE = "TextFix"; const PAGE = "/text_fix/ui"; const LAYOUT = "layout"; const BUTTON = "Open editor"; // The look as the page names it -> the node's widget. const LOOK = { font: "font", size: "size", fill: "spread", matchWeight: "match_stroke", uneven: "unevenness", shading: "shading", opacity: "opacity", soften: "soften", seed: "seed", weight: "weight", shiftX: "shift_x", shiftY: "shift_y", }; // The rest of the look has no widgets: it is kept in the layout widget with the lines. const LAYOUT_LOOK = [ "twoColors", "color2", "split", "outline", "outlineColor", "shadow", "shadowColor", "shadowX", "shadowY", "shadowBlur", "effect", "effectStrength", "relief", ]; let overlay = null; let frame = null; let ready = false; let current = null; // the node the editor is open for function widget(node, name) { return (node.widgets || []).find((entry) => entry.name === name); } /** What the node holds, in the page's terms. */ function stateOf(node) { let items = []; const look = {}; try { const data = JSON.parse(widget(node, LAYOUT)?.value || "{}"); if (data && Array.isArray(data.items)) items = data.items; if (data && data.look && typeof data.look === "object") { for (const key of LAYOUT_LOOK) if (key in data.look) look[key] = data.look[key]; } } catch { items = []; } for (const [page, name] of Object.entries(LOOK)) { const entry = widget(node, name); if (entry) look[page] = entry.value; } const color = String(widget(node, "color")?.value ?? "auto").trim(); look.ownColor = !/^#[0-9a-f]{6}$/i.test(color); if (!look.ownColor) look.color = color; return { items, look }; } function setWidget(node, name, value) { const entry = widget(node, name); if (!entry || entry.value === value || value === undefined) return; const choices = entry.options?.values; if (Array.isArray(choices) && !choices.includes(value)) return; if (typeof entry.value === "number" && typeof value !== "number") return; entry.value = value; entry.callback?.(value); } /** Write what the page holds back into the node. */ function applyState(node, state) { if (!node || !state) return; const items = Array.isArray(state.items) ? state.items : []; const look = state.look || {}; const extra = {}; for (const key of LAYOUT_LOOK) if (key in look) extra[key] = look[key]; setWidget(node, LAYOUT, JSON.stringify({ items, look: extra })); for (const [page, name] of Object.entries(LOOK)) setWidget(node, name, look[page]); setWidget(node, "color", look.ownColor ? "auto" : look.color); node.setDirtyCanvas?.(true, true); app.graph?.setDirtyCanvas?.(true, true); } function send() { if (!frame?.contentWindow || !ready || !current) return; frame.contentWindow.postMessage( { type: "TF_INIT", theme: "dark", node: { id: String(current.id), state: stateOf(current) } }, window.location.origin, ); } function close() { if (overlay) overlay.style.display = "none"; current = null; } function build() { overlay = document.createElement("div"); overlay.id = "text-fix-overlay"; overlay.style.cssText = "position:fixed;inset:0;z-index:10000;background:rgba(0,0,0,0.6);display:none;"; frame = document.createElement("iframe"); frame.title = "Text Fix"; frame.style.cssText = "position:absolute;inset:12px;width:calc(100% - 24px);height:calc(100% - 24px);" + "border:1px solid rgba(128,128,128,0.4);border-radius:10px;background:#111317;"; overlay.appendChild(frame); document.body.appendChild(overlay); window.addEventListener("message", (event) => { if (event.source !== frame.contentWindow || event.origin !== window.location.origin) return; const data = event.data || {}; if (data.type === "TF_READY") { ready = true; send(); } else if (data.type === "TF_CLOSE") { close(); } else if (data.type === "TF_STATE") { applyState(current, data.state); } }); document.addEventListener("keydown", (event) => { if (event.key === "Escape" && overlay.style.display !== "none") close(); }); } function open(node) { if (!overlay) build(); current = node; ready = false; frame.src = `${PAGE}?t=${Date.now()}`; // each opening starts clean, with this node's image overlay.style.display = "block"; } /** The layout widget holds JSON written by the editor: it is kept and saved, but not shown. */ function hide(entry) { if (!entry) return; entry.hidden = true; entry.computeSize = () => [0, -4]; if (entry.options) entry.options.hidden = true; } app.registerExtension({ name: "sd-text-fix.editor", async beforeRegisterNodeDef(nodeType, nodeData) { if (nodeData.name !== NODE_TYPE) return; const created = nodeType.prototype.onNodeCreated; nodeType.prototype.onNodeCreated = function () { const result = created?.apply(this, arguments); hide(widget(this, LAYOUT)); if (!widget(this, BUTTON)) { const button = this.addWidget("button", BUTTON, null, () => open(this)); button.serialize = false; button.options = { ...(button.options || {}), serialize: false }; } return result; }; }, });