From 7303f1b0fa1c8f258cd36ba72d7f09a6ec65b759 Mon Sep 17 00:00:00 2001 From: "chris.dumas" Date: Thu, 12 Nov 2026 10:06:40 +0000 Subject: [PATCH] Update js/sequence_prompt_builder.js --- js/sequence_prompt_builder.js | 208 +++++++++++++++++++++++++++++----- 1 file changed, 181 insertions(+), 27 deletions(-) diff --git a/js/sequence_prompt_builder.js b/js/sequence_prompt_builder.js index 35a8cc6..7741d7f 100644 --- a/js/sequence_prompt_builder.js +++ b/js/sequence_prompt_builder.js @@ -178,6 +178,52 @@ function ensureTimelineStyles() { const get = (node, name) => node.widgets?.find((widget) => widget.name === name); const el = (tag, text = "") => { const item = document.createElement(tag); item.textContent = text; return item; }; +/** + * Resolve a textarea caret in viewport pixels, even when the ComfyUI DOM + * widget is zoomed/scaled and its text has wrapped or been scrolled. + * A mirror preserves the textarea's font, padding, line wrapping and tabs. + */ +function textareaCaretRect(textarea, position) { + const style = window.getComputedStyle(textarea); + const mirror = document.createElement("div"); + const properties = [ + "boxSizing", "fontFamily", "fontSize", "fontWeight", "fontStyle", + "fontVariant", "fontStretch", "lineHeight", "letterSpacing", + "textTransform", "textIndent", "textAlign", "direction", + "tabSize", "paddingTop", "paddingRight", "paddingBottom", + "paddingLeft", "borderTopWidth", "borderRightWidth", + "borderBottomWidth", "borderLeftWidth", "borderTopStyle", + "borderRightStyle", "borderBottomStyle", "borderLeftStyle", + ]; + mirror.style.cssText = "position:fixed;left:-100000px;top:0;visibility:hidden;pointer-events:none;white-space:pre-wrap;overflow-wrap:break-word;word-wrap:break-word;"; + for (const property of properties) mirror.style[property] = style[property]; + mirror.style.width = `${textarea.offsetWidth}px`; + mirror.style.height = "auto"; + mirror.style.minHeight = "0"; + mirror.style.maxHeight = "none"; + mirror.style.overflow = "hidden"; + const caret = document.createElement("span"); + caret.textContent = "\u200b"; + mirror.append(document.createTextNode(textarea.value.slice(0, position)), caret); + document.body.append(mirror); + const mirrorBox = mirror.getBoundingClientRect(); + const caretBox = caret.getBoundingClientRect(); + const textareaBox = textarea.getBoundingClientRect(); + // CSS canvas zoom scales the DOM widget's rect but not its layout pixels. + const scaleX = textareaBox.width / Math.max(1, textarea.offsetWidth); + const scaleY = textareaBox.height / Math.max(1, textarea.offsetHeight); + const lineHeight = parseFloat(style.lineHeight) || parseFloat(style.fontSize) * 1.45 || 18; + const result = { + left: textareaBox.left + (caretBox.left - mirrorBox.left - textarea.scrollLeft) * scaleX, + top: textareaBox.top + (caretBox.top - mirrorBox.top - textarea.scrollTop) * scaleY, + height: lineHeight * scaleY, + }; + mirror.remove(); + result.bottom = result.top + result.height; + return result; +} + + function linkedNode(node, inputName) { const input = node.inputs?.find((item) => item.name === inputName); if (input?.link == null) return null; @@ -456,6 +502,7 @@ function configure(node) { const chunkDuration = selectedDuration.seconds; while (state.clips.length < count) state.clips.push({ duration: chunkDuration, enabled: true, slots: [] }); state.clips = state.clips.slice(0, count); active = Math.min(active, count - 1); + node.__dumasCloseAutocomplete?.(); panel.replaceChildren(); const tabs = el("div"); tabs.style.cssText = "display:flex;flex-wrap:wrap;gap:4px;margin-bottom:8px;border-bottom:1px solid #444;padding-bottom:6px"; state.clips.forEach((clip, index) => { @@ -511,7 +558,9 @@ function configure(node) { let autocomplete = null; let autocompleteRange = null; let promptFrame = null; - let suggestionLayer = null; + let autocompleteOptions = []; + let autocompleteSelected = 0; + let autocompleteCleanup = null; let encoder = null; const focusPrompt = () => { if (!prompt) return; @@ -768,60 +817,145 @@ function configure(node) { promptSelectionEnd = prompt.selectionEnd ?? promptSelectionStart; }; const closeAutocomplete = () => { + autocompleteCleanup?.(); + autocompleteCleanup = null; autocomplete?.remove(); autocomplete = null; autocompleteRange = null; + autocompleteOptions = []; + prompt?.removeAttribute("aria-activedescendant"); + prompt?.removeAttribute("aria-controls"); + prompt?.removeAttribute("aria-expanded"); + }; + node.__dumasCloseAutocomplete = closeAutocomplete; + // The overlay lives on document.body rather than inside the ComfyUI + // node. It cannot be clipped by a scaled/transformed node or misplaced + // 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); + const gutter = 8; + const gap = 5; + const width = autocomplete.offsetWidth; + const height = autocomplete.offsetHeight; + const x = Math.min(Math.max(gutter, caret.left), Math.max(gutter, window.innerWidth - width - gutter)); + let y = caret.bottom + gap; + if (y + height > window.innerHeight - gutter) y = caret.top - height - gap; + y = Math.max(gutter, Math.min(y, Math.max(gutter, window.innerHeight - height - gutter))); + autocomplete.style.left = `${Math.round(x)}px`; + autocomplete.style.top = `${Math.round(y)}px`; + }; + const selectAutocompleteOption = (index) => { + if (!autocompleteOptions.length) return; + autocompleteSelected = (index + autocompleteOptions.length) % autocompleteOptions.length; + autocompleteOptions.forEach(({ option }, itemIndex) => { + const active = itemIndex === autocompleteSelected; + option.style.background = active ? "#3a5360" : "transparent"; + option.style.color = active ? "#fff" : "#e7edf0"; + option.setAttribute("aria-selected", String(active)); + }); + const selected = autocompleteOptions[autocompleteSelected].option; + prompt.setAttribute("aria-activedescendant", selected.id); + selected.scrollIntoView?.({ block: "nearest" }); }; const openAutocomplete = (at) => { closeAutocomplete(); autocompleteRange = { start: at, end: at + 1 }; const menu = el("div"); - menu.style.cssText = "display:block;visibility:visible;opacity:1;position:absolute;z-index:100;left:4px;min-width:220px;max-width:310px;padding:5px;background:#25282b;border:1px solid #4b5960;border-radius:8px;box-shadow:0 8px 24px #000b;color:#e7edf0;pointer-events:auto"; - menu.style.top = "0px"; + menu.id = `dumas-h3-autocomplete-${node.id ?? "editor"}-${active}`; + menu.setAttribute("role", "listbox"); + menu.setAttribute("aria-label", "Insert reference image or new shot"); + menu.style.cssText = "position:fixed;z-index:2147483646;min-width:220px;width:max-content;max-width:min(310px,calc(100vw - 16px));max-height:min(360px,calc(100vh - 16px));overflow-y:auto;box-sizing:border-box;padding:5px;background:#252b30;border:1px solid #58646c;border-radius:8px;box-shadow:0 12px 30px #000c;color:#e7edf0;pointer-events:auto"; const addOption = (content, title, action) => { - const option = el("button"); option.type = "button"; option.title = title; - option.style.cssText = "display:flex;align-items:center;gap:9px;width:100%;padding:6px 8px;border:0;border-radius:5px;background:transparent;color:#e7edf0;text-align:left;font:12px monospace;cursor:pointer"; + const option = el("button"); + option.type = "button"; + option.title = title; + option.id = `${menu.id}-option-${autocompleteOptions.length}`; + option.setAttribute("role", "option"); + option.setAttribute("aria-selected", "false"); + option.style.cssText = "display:flex;align-items:center;gap:9px;width:100%;padding:7px 8px;border:0;border-radius:5px;background:transparent;color:#e7edf0;text-align:left;font:12px monospace;cursor:pointer"; + // Keep focus in the textarea: Enter/arrow selection must work. option.onpointerdown = (event) => { event.preventDefault(); event.stopPropagation(); }; - option.onmouseenter = () => { option.style.background = "#3b5661"; }; - option.onmouseleave = () => { option.style.background = "transparent"; }; + option.onmouseenter = () => selectAutocompleteOption(autocompleteOptions.findIndex((entry) => entry.option === option)); option.onclick = (event) => { event.preventDefault(); event.stopPropagation(); action(); }; - option.append(...content); menu.append(option); + option.append(...content); + menu.append(option); + autocompleteOptions.push({ option, action }); }; connectedReferenceSlots.forEach((slot) => { addOption([ - (() => { const icon = el("span", "R"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:50%;background:#7952a8;color:#fff;font-weight:bold"; return icon; })(), + (() => { const icon = el("span", "R"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:50%;background:#7952a8;color:#fff;font-weight:bold;flex:none"; return icon; })(), el("span", `@R${slot} Reference Image ${slot}`), - (() => { const hint = el("span", ""); hint.style.cssText = "margin-left:auto;color:#9bd0a8;font-size:10px"; return hint; })(), + (() => { const hint = el("span", "Image"); hint.style.cssText = "margin-left:auto;color:#9bd0a8;font-size:10px"; return hint; })(), ], `Insert Reference Image ${slot} as @R${slot}`, () => { const start = autocompleteRange?.start ?? prompt.value.length; - const end = autocompleteRange?.end ?? start; + const end = autocompleteRange?.end ?? start + 1; const token = `@R${slot} `; prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end); const caret = start + token.length; - focusPrompt(); prompt.setSelectionRange(caret, caret); + closeAutocomplete(); + focusPrompt(); + prompt.setSelectionRange(caret, caret); promptSelectionStart = caret; promptSelectionEnd = caret; promptText = prompt.value; clip.prompt = prompt.value; - syncPrompt(clip, clip.prompt); persistState(); closeAutocomplete(); + syncPrompt(clip, clip.prompt); persistState(); }); }); addOption([ - (() => { const icon = el("span", "S"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:50%;background:#397a91;color:#dff7ff;font-weight:bold"; return icon; })(), - el("span", "[Shot " + (clip.slots.length + 1) + "]"), - (() => { const hint = el("span", "new shot"); hint.style.cssText = "margin-left:auto;color:#84939a;font-size:10px"; return hint; })(), + (() => { const icon = el("span", "S"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:50%;background:#397a91;color:#dff7ff;font-weight:bold;flex:none"; return icon; })(), + el("span", `[Shot ${clip.slots.length + 1}]`), + (() => { const hint = el("span", "New shot"); hint.style.cssText = "margin-left:auto;color:#a5b7bf;font-size:10px"; return hint; })(), ], "Create the next timed shot", () => { const start = autocompleteRange?.start ?? prompt.value.length; - prompt.value = prompt.value.slice(0, start) + prompt.value.slice(start + 1); + const end = autocompleteRange?.end ?? start + 1; + prompt.value = prompt.value.slice(0, start) + prompt.value.slice(end); promptText = prompt.value; clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); addSlot(clip); rebuildPromptAfterStructureChange(clip); - closeAutocomplete(); setState(); render(); + // The editor is still focused when Enter chooses this item. + // A programmatic redraw will blur the OLD textarea, whose old + // two-shot text would otherwise parse over our added slot. + prompt.onblur = null; + prompt.onchange = null; + closeAutocomplete(); + setState(); // the hidden widget callback renders the new editor + const newPrompt = panel.querySelector("textarea"); + if (newPrompt) { + try { newPrompt.focus({ preventScroll: true }); } + catch (_) { newPrompt.focus(); } + // Put the cursor where the new shot description begins. + newPrompt.setSelectionRange(newPrompt.value.length, newPrompt.value.length); + } }); - panel.append(menu); - menu.style.left = `${Math.max(5, (suggestionLayer?.offsetLeft || 0) + 4)}px`; - menu.style.top = `${Math.max(5, suggestionLayer?.offsetTop || 0)}px`; autocomplete = menu; + document.body.append(menu); + prompt.setAttribute("aria-controls", menu.id); + prompt.setAttribute("aria-expanded", "true"); + selectAutocompleteOption(0); + positionAutocomplete(); + const onOutsidePointer = (event) => { + if (!menu.contains(event.target) && event.target !== prompt) closeAutocomplete(); + }; + const onViewportChange = () => positionAutocomplete(); + document.addEventListener("pointerdown", onOutsidePointer, true); + // Capturing scroll events covers scrolling within the prompt, the + // node viewport and any ComfyUI canvas container. + document.addEventListener("scroll", onViewportChange, true); + window.addEventListener("resize", onViewportChange); + autocompleteCleanup = () => { + document.removeEventListener("pointerdown", onOutsidePointer, true); + document.removeEventListener("scroll", onViewportChange, true); + window.removeEventListener("resize", onViewportChange); + }; + }; + prompt.oninput = () => { + 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(); }; - prompt.oninput = () => { rememberPromptSelection(); promptText = prompt.value; clip.prompt = prompt.value; }; prompt.onpaste = () => { + closeAutocomplete(); // Let the browser insert the pasted text first, then parse its // shot markers and redraw the track without losing the paste. setTimeout(() => { @@ -845,6 +979,7 @@ function configure(node) { prompt.onkeyup = rememberPromptSelection; prompt.onmouseup = rememberPromptSelection; prompt.onblur = () => { + closeAutocomplete(); rememberPromptSelection(); clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); @@ -854,8 +989,25 @@ function configure(node) { prompt.onchange = () => { commitPrompt(clip, prompt.value); }; prompt.onpointerdown = (event) => event.stopPropagation(); prompt.onkeydown = (event) => { - if (event.key !== "@") return; - event.preventDefault(); + if (autocomplete) { + if (event.key === "ArrowDown" || event.key === "ArrowUp") { + event.preventDefault(); event.stopPropagation(); + selectAutocompleteOption(autocompleteSelected + (event.key === "ArrowDown" ? 1 : -1)); + return; + } + if (event.key === "Enter" || event.key === "Tab") { + event.preventDefault(); event.stopPropagation(); + autocompleteOptions[autocompleteSelected]?.action(); + return; + } + if (event.key === "Escape") { + event.preventDefault(); event.stopPropagation(); + closeAutocomplete(); + return; + } + } + 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); @@ -868,10 +1020,7 @@ function configure(node) { }; promptFrame = el("div"); promptFrame.style.cssText = "position:relative;overflow:visible;width:100%;box-sizing:border-box"; - suggestionLayer = el("div"); - suggestionLayer.style.cssText = "position:relative;z-index:100;height:0;width:100%;pointer-events:none;overflow:visible"; promptFrame.append(prompt); - panel.append(suggestionLayer); panel.append(promptFrame); promptSelectionStart = prompt.value.length; promptSelectionEnd = prompt.value.length; @@ -956,6 +1105,11 @@ function configure(node) { render(); return result; }; + const previousRemoved = node.onRemoved; + node.onRemoved = function(...args) { + this.__dumasCloseAutocomplete?.(); + return previousRemoved?.apply(this, args); + }; node.__dumasSequencePromptBuilder = refresh; refresh(); }