Skip to content

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.

fix(webv2): mirror the field's direction when measuring the caret

joshistoast91f8547d2869
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.tsx
index 16b750c431..11a004d84c 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
@@ -1,6 +1,7 @@
/* oxlint-disable react-perf/jsx-no-new-array-as-prop, react-perf/jsx-no-new-function-as-prop */
import { ChakraProvider } from '@chakra-ui/react';
import { PositivePromptField } from '@features/generation/ui/promptFields/PositivePromptField';
+import { getTextareaCaretRect } from '@features/generation/ui/promptFields/promptCaret';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { system } from '@theme/system';
import { act } from 'react';
@@ -197,6 +198,29 @@ describe('the caret autocomplete', () => {
expect(list.top - box.top).toBeGreaterThan(60);
});
+ // The rest of the caret coverage uses hard newlines, which any mirror gets
+ // right. Soft wrapping is the case the mirror's own width decides, and nothing
+ // exercised it. Asserted on the measurement rather than the rendered list,
+ // which flips above the caret near the bottom of a full box.
+ it('agrees with the textarea about where soft wrapping put the caret', () => {
+ const element = textarea();
+ // Long enough to overflow the box, so `scrollHeight` reports the real line
+ // count rather than being floored at the visible height.
+ const text = 'lorem ipsum dolor sit amet '.repeat(100);
+
+ element.value = text;
+
+ const style = getComputedStyle(element);
+ const lineHeight = Number.parseFloat(style.lineHeight);
+ const paddingTop = Number.parseFloat(style.paddingTop);
+ const lines = Math.round((element.scrollHeight - paddingTop - Number.parseFloat(style.paddingBottom)) / lineHeight);
+ const rect = getTextareaCaretRect(element, text.length)!;
+ const line = Math.round((rect.y - element.getBoundingClientRect().top - paddingTop) / lineHeight) + 1;
+
+ expect(lines).toBeGreaterThan(2);
+ expect(line).toBe(lines);
+ });
+
it('inserts the highlighted option on Enter', async () => {
await type('a photo of __');
await press('{Enter}');
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptCaret.ts b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptCaret.ts
index 9337f54839..185c39915a 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptCaret.ts
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/promptCaret.ts
@@ -20,13 +20,19 @@ export interface CaretRect {
y: number;
}
-/** Everything that decides where a character lands, copied onto the mirror. */
+/**
+ * Everything that decides where a character lands, copied onto the mirror.
+ *
+ * Width is deliberately absent, and set from `clientWidth` below instead.
+ * Copying the resolved `width` does work — it is the border-box width under
+ * `box-sizing: border-box`, which the copied borders and padding then reduce to
+ * the same content width — but it only works because those three travel
+ * together, and it counts a vertical scrollbar as space for text. `clientWidth`
+ * is the padding box with the scrollbar already taken out, which is the width
+ * the field actually wraps at, stated once.
+ */
const MIRRORED_STYLES = [
- 'borderBottomWidth',
- 'borderLeftWidth',
- 'borderRightWidth',
- 'borderTopWidth',
- 'boxSizing',
+ 'direction',
'fontFamily',
'fontSize',
'fontStretch',
@@ -40,9 +46,9 @@ const MIRRORED_STYLES = [
'paddingRight',
'paddingTop',
'tabSize',
+ 'textAlign',
'textIndent',
'textTransform',
- 'width',
'wordSpacing',
] as const;
@@ -76,6 +82,11 @@ export const getTextareaCaretRect = (textarea: HTMLTextAreaElement, index: numbe
mirror.style.whiteSpace = 'pre-wrap';
mirror.style.overflowWrap = 'break-word';
mirror.style.overflow = 'hidden';
+ // `clientWidth` is the padding box, so with no border of its own the mirror
+ // gets exactly the field's own wrapping width.
+ mirror.style.boxSizing = 'border-box';
+ mirror.style.border = '0';
+ mirror.style.width = `${textarea.clientWidth}px`;
mirror.textContent = textarea.value.slice(0, index);
// A zero-width space so the marker still occupies a line when the caret sits
@@ -85,11 +96,15 @@ export const getTextareaCaretRect = (textarea: HTMLTextAreaElement, index: numbe
document.body.append(mirror);
const lineHeight = Number.parseFloat(computed.lineHeight);
+ // The offsets are measured from the mirror's padding edge, which the field's
+ // own border sits outside of, so it has to be added back by hand.
+ const borderLeft = Number.parseFloat(computed.borderLeftWidth) || 0;
+ const borderTop = Number.parseFloat(computed.borderTopWidth) || 0;
const rect: CaretRect = {
height: Number.isFinite(lineHeight) ? lineHeight : Number.parseFloat(computed.fontSize) * 1.6,
width: 0,
- x: textareaRect.left + marker.offsetLeft - textarea.scrollLeft,
- y: textareaRect.top + marker.offsetTop - textarea.scrollTop,
+ x: textareaRect.left + borderLeft + marker.offsetLeft - textarea.scrollLeft,
+ y: textareaRect.top + borderTop + marker.offsetTop - textarea.scrollTop,
};
mirror.remove();