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): stop the line numbers rendering on top of the text

joshistoastcb7826bbe164
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptLineNumbers.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptLineNumbers.browser.test.tsx
index 3c981a22e9..0da4fbca20 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptLineNumbers.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptLineNumbers.browser.test.tsx
@@ -49,6 +49,20 @@ afterEach(async () => {
});
describe('prompt line numbers', () => {
+ it('indents the text clear of the gutter so numbers never overlap it', async () => {
+ // Regression: the gutter offset was a calc() over a Chakra spacing var whose
+ // generated name does not resolve for fractional tokens, so the declaration
+ // was dropped and the numbers rendered on top of the first characters.
+ await render('toyota+\nhonda\nacura--');
+
+ const textarea = host!.querySelector('textarea')!;
+ const gutter = host!.querySelector('[data-line-number]')!.parentElement!.parentElement!;
+ const textStart = textarea.getBoundingClientRect().left + parseFloat(getComputedStyle(textarea).paddingInlineStart);
+
+ expect(parseFloat(getComputedStyle(textarea).paddingInlineStart)).toBeGreaterThan(0);
+ expect(gutter.getBoundingClientRect().right).toBeLessThanOrEqual(textStart);
+ });
+
it('numbers every logical line', async () => {
await render('red\ngreen\nblue');
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptLineNumbers.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptLineNumbers.tsx
index 108db20b6f..0c036a7f96 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptLineNumbers.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptLineNumbers.tsx
@@ -37,6 +37,9 @@ export const PromptLineNumbers = ({ metrics, value }: { metrics: PromptLineNumbe
const [lineHeights, setLineHeights] = useState<number[]>([]);
const lines = useMemo(() => value.split('\n'), [value]);
const gutterWidth = `${getLineNumberGutterCh(lines.length)}ch`;
+ // The numbers occupy the same lane the text is indented by, so they end just
+ // short of where the first character starts.
+ const gutterLaneWidth = `calc(${metrics.paddingInline} + ${gutterWidth})`;
useLayoutEffect(() => {
const mirror = mirrorRef.current;
@@ -93,15 +96,19 @@ export const PromptLineNumbers = ({ metrics, value }: { metrics: PromptLineNumbe
</Box>
</Box>
+ {/* The font metrics have to be on this box too: `ch` in its width resolves
+ against its own font size, not the numbers' inside it. */}
<Box
aria-hidden="true"
bottom="0"
+ fontFamily={metrics.fontFamily}
+ fontSize={metrics.fontSize}
left="0"
overflow="hidden"
pointerEvents="none"
position="absolute"
top="0"
- w={gutterWidth}
+ w={gutterLaneWidth}
zIndex={2}
>
<Box
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTextarea.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTextarea.tsx
index 2c48483b53..80cb44e235 100644
--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTextarea.tsx
+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTextarea.tsx
@@ -7,8 +7,12 @@ import { getLineNumberGutterCh, PromptLineNumbers } from '@features/generation/u
import { ResizableTextarea } from '@platform/ui';
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';
const PROMPT_TEXTAREA_LINE_HEIGHT = '1.6';
-const PROMPT_TEXTAREA_PX = '2.5';
-const PROMPT_TEXTAREA_PY = '2';
+// Literal lengths rather than spacing tokens: the gutter offset is a `calc()`,
+// and Chakra's generated var name for a fractional token does not resolve inside
+// one, which silently invalidates the whole declaration. These are the `2.5` and
+// `2` spacing tokens.
+const PROMPT_TEXTAREA_PX = '0.625rem';
+const PROMPT_TEXTAREA_PY = '0.5rem';
const PROMPT_TEXTAREA_FORCED_COLORS_CSS = { '@media (forced-colors: active)': { display: 'none' } };
const PROMPT_TEXTAREA_HIGHLIGHTED_CSS = {
@@ -70,9 +74,7 @@ export const PromptTextarea = ({
const effectiveLineHeight = lineHeight ?? PROMPT_TEXTAREA_LINE_HEIGHT;
// The gutter widens with the line count, and the text has to start clear of it.
const gutterCh = showLineNumbers ? getLineNumberGutterCh(value.split('\n').length) : 0;
- const paddingInlineStart = showLineNumbers
- ? `calc(var(--chakra-spacing-${PROMPT_TEXTAREA_PX}) + ${gutterCh}ch)`
- : undefined;
+ const paddingInlineStart = showLineNumbers ? `calc(${PROMPT_TEXTAREA_PX} + ${gutterCh}ch)` : undefined;
const highlightOptions = useMemo(
() => ({ dynamicPrompts: highlightDynamicPrompts, knownWildcards }),
@@ -126,8 +128,8 @@ export const PromptTextarea = ({
fontFamily,
fontSize: effectiveFontSize,
lineHeight: effectiveLineHeight,
- paddingBlock: `var(--chakra-spacing-${PROMPT_TEXTAREA_PY})`,
- paddingInline: `var(--chakra-spacing-${PROMPT_TEXTAREA_PX})`,
+ paddingBlock: PROMPT_TEXTAREA_PY,
+ paddingInline: PROMPT_TEXTAREA_PX,
scrollTop: scroll.top,
}),
[effectiveFontSize, effectiveLineHeight, fontFamily, scroll.top, textareaClientWidth]