Compare commits

..

4 Commits

Author SHA1 Message Date
Ethanfel d6d2c98a58 Merge fix/textgate-prompt-weighting: prompt weighting in text gate editor 2026-07-03 10:17:58 +02:00
Ethanfel 36dd5c91ee fix: text gate supports prompt weighting (Ctrl/Cmd+↑/↓) in the editor
ComfyUI's "edit attention" (wrap selection in (token:weight)) is a global
window keydown listener that acts when a <textarea> is focused. The text
gate editor is a textarea, but its keydown handler called stopPropagation
on EVERY key, so the event never bubbled to window and weighting never
fired — notably when using the node as a prompt text node in protected mode.

Now stopPropagation is skipped for the weighting shortcut (Ctrl/Cmd + ↑/↓)
so it reaches the global handler; all other keys are still stopped so
typing/space can't trigger litegraph canvas shortcuts. The weighting edit
goes through execCommand, which fires our oninput -> stored_text stays synced.

Verified against the verbatim editAttention from the shipped frontend:
whole-word weighting, existing-weight decrement, and no-selection word
expansion all round-trip; plain keys stay stopped.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 10:17:58 +02:00
Ethanfel 954b9ec2e6 Merge fix/textgate-persist-editor: text gate editor survives reload 2026-07-03 00:22:33 +02:00
Ethanfel 1881aa727f fix: text gate persists editor text across refresh/reload
The editor content was only restored on reload in protected mode, and
stored_text was only synced on keystroke (oninput). So in the default
pause mode edited text came back empty after refresh/reload-workflow,
and upstream text passed without a keystroke was never captured.

Now applyPersistedMode restores the editor from stored_text in BOTH
modes, and syncStored also fires when upstream text arrives (socket)
and on Pass — so whatever text is shown/edited survives a reload.
Verified against the shipped litegraph serialize()/configure() widget
semantics: default-mode + pass-without-typing round-trips now restore.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 00:22:28 +02:00
+20 -9
View File
@@ -32,7 +32,9 @@ const MARGIN = 10; // ComfyUI DOM-widget inset, matches the other nodes
// `protected` (BOOLEAN toggle) + `stored_text` (hidden STRING) are real backend // `protected` (BOOLEAN toggle) + `stored_text` (hidden STRING) are real backend
// widgets. When protected, the node acts as a plain text node: it outputs // widgets. When protected, the node acts as a plain text node: it outputs
// stored_text and ignores upstream (no pause). The DOM textarea is the visible // stored_text and ignores upstream (no pause). The DOM textarea is the visible
// editor and mirrors its value into stored_text so it persists and reaches run(). // editor and mirrors its value into stored_text on EVERY change (typing, upstream
// arrival, Pass) — so the editor content survives refresh / workflow reload in
// BOTH modes (stored_text also reaches run() when protected).
function widgetByName(node, name) { function widgetByName(node, name) {
return node.widgets?.find((w) => w.name === name); return node.widgets?.find((w) => w.name === name);
@@ -59,15 +61,13 @@ function hideStoredWidget(node) {
w.computeSize = () => [0, -4]; w.computeSize = () => [0, -4];
} }
// reflect the persisted protected/stored_text state into the editor + UI // reflect the persisted stored_text + mode into the editor + UI. The editor text
// is restored in BOTH modes so it survives a refresh / workflow reload; the mode
// only selects the UI state (protected vs idle waiting-for-a-run).
function applyPersistedMode(node) { function applyPersistedMode(node) {
if (!node._tg) return; if (!node._tg) return;
if (isProtected(node)) {
node._tg.area.value = widgetByName(node, "stored_text")?.value ?? ""; node._tg.area.value = widgetByName(node, "stored_text")?.value ?? "";
setState(node, "protected"); setState(node, isProtected(node) ? "protected" : "idle");
} else {
setState(node, "idle");
}
} }
// ---- server call ------------------------------------------------------------ // ---- server call ------------------------------------------------------------
@@ -170,8 +170,17 @@ function setupTextGateNode(node) {
const area = document.createElement("textarea"); const area = document.createElement("textarea");
area.className = "tgate-area"; area.className = "tgate-area";
area.placeholder = "waiting for a run…"; area.placeholder = "waiting for a run…";
// don't let typing/space toggle node selection or graph shortcuts // Stop keys from reaching litegraph (so typing/space can't toggle node
area.onkeydown = (e) => e.stopPropagation(); // selection or fire canvas shortcuts) — EXCEPT ComfyUI's prompt-weighting
// shortcut (Ctrl/Cmd+↑/↓). That handler is a global `window` keydown listener
// that wraps the selection in (token:weight); a blanket stopPropagation here
// kept it from ever bubbling up, so weighting didn't work in this editor.
// Its execCommand edit fires our oninput, so the weighted text still syncs.
area.onkeydown = (e) => {
const isWeight = (e.ctrlKey || e.metaKey) &&
(e.key === "ArrowUp" || e.key === "ArrowDown");
if (!isWeight) e.stopPropagation();
};
// keep the hidden stored_text widget mirrored so edits persist + reach run() // keep the hidden stored_text widget mirrored so edits persist + reach run()
area.oninput = () => syncStored(node); area.oninput = () => syncStored(node);
@@ -182,6 +191,7 @@ function setupTextGateNode(node) {
pass.className = "tgate-pass"; pass.className = "tgate-pass";
pass.textContent = "▶ Pass"; pass.textContent = "▶ Pass";
pass.onclick = async () => { pass.onclick = async () => {
syncStored(node); // persist the passed text so a reload keeps it
await postPass(node, area.value); await postPass(node, area.value);
setState(node, "passed"); setState(node, "passed");
}; };
@@ -265,6 +275,7 @@ app.registerExtension({
} else { } else {
node._tg.area.value = d.text || ""; node._tg.area.value = d.text || "";
} }
syncStored(node); // persist the shown text so a refresh/reload keeps it
setState(node, "paused"); setState(node, "paused");
try { node._tg.area.focus(); } catch (err) { /* ignore */ } try { node._tg.area.focus(); } catch (err) { /* ignore */ }
}); });