From e22decc459ac451e667d9089d9bbd2cea70a403c Mon Sep 17 00:00:00 2001 From: "chris.dumas" Date: Thu, 12 Nov 2026 10:18:04 +0000 Subject: [PATCH] Update js/sequence_prompt_builder.js --- js/sequence_prompt_builder.js | 69 ++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/js/sequence_prompt_builder.js b/js/sequence_prompt_builder.js index 7741d7f..dedcd41 100644 --- a/js/sequence_prompt_builder.js +++ b/js/sequence_prompt_builder.js @@ -1,6 +1,8 @@ import { app } from "../../scripts/app.js"; const CLASS = "DumasH3SequencePromptBuilder"; +// Check the browser console for this line to confirm the patched file loaded. +console.info("[DUMAS H3] Prompt Builder autocomplete v2 (input-triggered) loaded"); const MAX_CLIPS = 16; const FIXED_TEXT_WIDGETS = [ ["shared_directive", 84], @@ -833,7 +835,17 @@ function configure(node) { // by using the panel's origin as the textarea's caret location. const positionAutocomplete = () => { if (!autocomplete || !prompt?.isConnected) { closeAutocomplete(); return; } - const caret = textareaCaretRect(prompt, prompt.selectionStart ?? prompt.value.length); + let caret; + try { + caret = textareaCaretRect(prompt, prompt.selectionStart ?? prompt.value.length); + if (!Number.isFinite(caret.left) || !Number.isFinite(caret.bottom)) throw new Error("invalid caret coordinates"); + } catch (error) { + // Keep autocomplete usable even if a host UI layout cannot be + // mirrored precisely. The fallback anchors at the textarea. + const box = prompt.getBoundingClientRect(); + caret = { left: box.left + 12, top: box.top, bottom: box.top + 22 }; + console.warn("[DUMAS H3] Caret positioning fallback:", error); + } const gutter = 8; const gap = 5; const width = autocomplete.offsetWidth; @@ -947,12 +959,32 @@ function configure(node) { window.removeEventListener("resize", onViewportChange); }; }; - prompt.oninput = () => { + // Trigger on the actual text insertion, not only keydown. On some + // browsers / keyboard layouts an input event follows the @ keydown; + // the old handler immediately CLOSED the menu it had just opened. + // Handling the inserted character works with UK keyboards, IMEs and + // browser-mediated text input without manually inserting duplicates. + prompt.oninput = (event) => { + const before = String(promptText ?? clip.prompt ?? ""); + const after = prompt.value; + const caret = prompt.selectionStart ?? after.length; + const atCaret = caret > 0 && after.charAt(caret - 1) === "@"; + const insertedSingleAt = atCaret && event?.inputType !== "insertFromPaste" && ( + (event?.data === "@") || + (after.length === before.length + 1 && + after.slice(0, caret - 1) === before.slice(0, caret - 1) && + after.slice(caret) === before.slice(caret - 1)) + ); rememberPromptSelection(); - promptText = prompt.value; clip.prompt = prompt.value; - // If they keep typing a literal @R token, don't obstruct typing. - // The menu is shown by @ and stays up for arrow/Enter navigation. - if (autocomplete) closeAutocomplete(); + promptText = after; + clip.prompt = after; + if (insertedSingleAt) { + openAutocomplete(caret - 1); + } else if (autocomplete) { + // Typing anything after the @ lets the user continue with a + // literal token, e.g. @R1, without an obstructing menu. + closeAutocomplete(); + } }; prompt.onpaste = () => { closeAutocomplete(); @@ -976,7 +1008,16 @@ function configure(node) { }, 0); }; prompt.onselect = rememberPromptSelection; - prompt.onkeyup = rememberPromptSelection; + prompt.onkeyup = (event) => { + rememberPromptSelection(); + // Fallback for environments that deliver keyboard text changes + // without a normal InputEvent. Never fabricate the @ character. + if (event.key === "@" && !autocomplete && + promptSelectionStart > 0 && + prompt.value.charAt(promptSelectionStart - 1) === "@") { + openAutocomplete(promptSelectionStart - 1); + } + }; prompt.onmouseup = rememberPromptSelection; prompt.onblur = () => { closeAutocomplete(); @@ -1007,16 +1048,10 @@ function configure(node) { } } if (event.key !== "@" || event.ctrlKey || event.metaKey || event.altKey) return; - event.preventDefault(); event.stopPropagation(); - const start = prompt.selectionStart ?? prompt.value.length; - const end = prompt.selectionEnd ?? start; - prompt.value = prompt.value.slice(0, start) + "@" + prompt.value.slice(end); - prompt.setSelectionRange(start + 1, start + 1); - promptSelectionStart = start; - promptSelectionEnd = start + 1; - promptText = prompt.value; clip.prompt = prompt.value; - syncPrompt(clip, clip.prompt); persistState(); - openAutocomplete(start); + // Let the browser insert @. The input event opens the menu after + // the selection/caret has advanced to its true position. + // Stop ComfyUI graph-level keyboard shortcuts from intercepting it. + event.stopPropagation(); }; promptFrame = el("div"); promptFrame.style.cssText = "position:relative;overflow:visible;width:100%;box-sizing:border-box";