Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(webv2): dismiss the trigger pickers when the page moves
joshistoastba18f5d9f685
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsxindex 538a93e56f..5a0a8392f6 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerAutocomplete.tsx@@ -67,7 +67,14 @@ export const PromptTriggerAutocomplete = ({// Below the caret when there is room, above it when there is not, and never// off the right edge of a narrow window.const spaceBelow = window.innerHeight - (caretRect.y + caretRect.height);- const height = Math.min(MAX_LIST_HEIGHT_PX, Math.max(spaceBelow, caretRect.y) - VIEWPORT_MARGIN_PX - CARET_GAP_PX);+ // Clamped: in a short viewport with the caret near the top there is room on+ // neither side, and the subtraction went negative — `maxH="-2px"` is dropped+ // by the browser, so the list sprang to full height and was then positioned+ // from that negative number.+ const height = Math.max(+ 0,+ Math.min(MAX_LIST_HEIGHT_PX, Math.max(spaceBelow, caretRect.y) - VIEWPORT_MARGIN_PX - CARET_GAP_PX)+ );const opensBelow = spaceBelow >= height + CARET_GAP_PX + VIEWPORT_MARGIN_PX;const left = Math.max(VIEWPORT_MARGIN_PX,diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsxindex 11a004d84c..15742852e7 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTriggerPicker.browser.test.tsx@@ -332,6 +332,34 @@ describe('the caret autocomplete', () => {});});+ // The list is placed once from a viewport rect and never re-measured, and the+ // textarea keeps focus throughout — so without this it stayed welded to where+ // the caret used to be while the prompt scrolled away underneath it.+ it('closes when something scrolls under it', async () => {+ await type('a photo of __');++ expect(listbox()).not.toBeNull();++ await act(() => {+ // Capture-phase, so an element scroll reaches it even though it does not+ // bubble.+ textarea().dispatchEvent(new Event('scroll'));+ });++ expect(listbox()).toBeNull();+ expect(textarea().value).toBe('a photo of __');+ });++ it('closes when the window is resized', async () => {+ await type('a photo of __');++ await act(() => {+ window.dispatchEvent(new Event('resize'));+ });++ expect(listbox()).toBeNull();+ });+it('closes on Escape and leaves the prompt exactly as typed', async () => {await type('a photo of __');await press('{Escape}');diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptCaret.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptCaret.tsindex 185c39915a..204d2091da 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptCaret.ts+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptCaret.ts@@ -52,6 +52,34 @@ const MIRRORED_STYLES = ['wordSpacing',] as const;+/**+ * The one mirror, reused.+ *+ * This runs on every keystroke while a trigger is live, and building it each+ * time meant two `createElement`s, an `append` and a `remove` per character —+ * each insertion and removal dirtying layout for whatever came next. The+ * measurement itself still forces a layout, which is inherent to the technique,+ * but the churn around it is not.+ *+ * It stays in the document between calls. It is `visibility: hidden` and parked+ * off-screen, so it costs one detached-looking subtree and no paint.+ */+let mirrorElements: { marker: HTMLSpanElement; mirror: HTMLDivElement } | null = null;++const getMirror = (): { marker: HTMLSpanElement; mirror: HTMLDivElement } => {+ if (!mirrorElements) {+ const mirror = document.createElement('div');+ const marker = document.createElement('span');++ mirror.setAttribute('aria-hidden', 'true');+ mirror.append(marker);+ document.body.append(mirror);+ mirrorElements = { marker, mirror };+ }++ return mirrorElements;+};+/*** The caret's viewport rectangle, or `null` when the element is not laid out.*@@ -67,8 +95,7 @@ export const getTextareaCaretRect = (textarea: HTMLTextAreaElement, index: numbe}const computed = window.getComputedStyle(textarea);- const mirror = document.createElement('div');- const marker = document.createElement('span');+ const { marker, mirror } = getMirror();for (const property of MIRRORED_STYLES) {mirror.style[property] = computed[property];@@ -88,12 +115,13 @@ export const getTextareaCaretRect = (textarea: HTMLTextAreaElement, index: numbemirror.style.border = '0';mirror.style.width = `${textarea.clientWidth}px`;+ // Setting `textContent` drops the marker along with the old text, so it is put+ // back rather than appended afresh.mirror.textContent = textarea.value.slice(0, index);// A zero-width space so the marker still occupies a line when the caret sits// at the very end of the text, or straight after a newline.marker.textContent = textarea.value.slice(index) || '';mirror.append(marker);- document.body.append(mirror);const lineHeight = Number.parseFloat(computed.lineHeight);// The offsets are measured from the mirror's padding edge, which the field's@@ -107,7 +135,9 @@ export const getTextareaCaretRect = (textarea: HTMLTextAreaElement, index: numbey: textareaRect.top + borderTop + marker.offsetTop - textarea.scrollTop,};- mirror.remove();+ // Emptied rather than removed: keeping the element costs nothing, but keeping+ // a whole prompt's worth of text laid out between keystrokes would.+ mirror.textContent = '';return rect;};diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsxindex 3a4b1b719b..570773f445 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerAutocomplete.tsx@@ -26,7 +26,7 @@ import {getInlineTriggerOptions,usePromptTriggerOptions,} from '@features/generation/ui/promptFields/promptTriggerOptions';-import { useCallback, useId, useMemo, useRef, useState } from 'react';+import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react';interface AutocompleteState {caretRect: CaretRect;@@ -92,6 +92,28 @@ export const usePromptTriggerAutocomplete = ({const isOpen = state !== null && matches.length > 0;const close = useCallback(() => setState(null), []);+ // The list is a fixed-position surface placed from a rect read when it opened,+ // and nothing re-measures it. Scrolling the panel the prompt sits in — or the+ // prompt's own box, or dragging its resize handle — moves the caret out from+ // under it while the textarea keeps focus, so nothing else would close it. A+ // scroll listener in the capture phase sees element scrolls too, which do not+ // bubble.+ useEffect(() => {+ if (!isOpen) {+ return;+ }++ const dismiss = () => setState(null);++ window.addEventListener('scroll', dismiss, { capture: true, passive: true });+ window.addEventListener('resize', dismiss, { passive: true });++ return () => {+ window.removeEventListener('scroll', dismiss, { capture: true });+ window.removeEventListener('resize', dismiss);+ };+ }, [isOpen]);+const refresh = useCallback((textarea: HTMLTextAreaElement | null) => {// Mid-composition the field holds half-committed romaji, which is not adiff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerPicker.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerPicker.tsindex c0a1db2083..61c21fa164 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerPicker.ts+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/usePromptTriggerPicker.ts@@ -12,7 +12,7 @@import type { PromptTextRange } from '@features/generation/ui/promptFields/promptFocus';-import { useCallback, useMemo, useState } from 'react';+import { useCallback, useEffect, useMemo, useState } from 'react';interface AnchorRect {height: number;@@ -55,5 +55,24 @@ export const usePromptTriggerPicker = ({const positioning = useMemo(() => ({ getAnchorRect: () => anchorRect }), [anchorRect]);+ // `getAnchorRect` hands back the rect read when the button was pressed, so the+ // usual re-measuring the popover would do on its own finds the same answer+ // every time. Scroll the panel and it stays where the button used to be.+ useEffect(() => {+ if (anchorRect === null) {+ return;+ }++ const dismiss = () => setAnchorRect(null);++ window.addEventListener('scroll', dismiss, { capture: true, passive: true });+ window.addEventListener('resize', dismiss, { passive: true });++ return () => {+ window.removeEventListener('scroll', dismiss, { capture: true });+ window.removeEventListener('resize', dismiss);+ };+ }, [anchorRect]);+return { close, isOpen: anchorRect !== null, open, positioning, select };};