Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(sheets): show formula help only while typing and make it compact (#7682)
teo479b560273df
diff --git a/apps/web/src/features/block-spreadsheet/browser-test/editor-interactions.browser.e2e.ts b/apps/web/src/features/block-spreadsheet/browser-test/editor-interactions.browser.e2e.tsindex 31df5434b7..535f26714e 100644--- a/apps/web/src/features/block-spreadsheet/browser-test/editor-interactions.browser.e2e.ts+++ b/apps/web/src/features/block-spreadsheet/browser-test/editor-interactions.browser.e2e.ts@@ -137,13 +137,13 @@ test('picks a range on a second sheet and commits the formula back to its originawait page.getByRole('button', { name: 'Save name' }).click();await page.getByRole('tab', { name: 'Sheet1', exact: true }).click();await page.locator('[data-address="C2"]').dblclick();- await page- .getByRole('textbox', { name: 'Edit C2', exact: true })- .fill('=SUM(');+ const editor = page.getByRole('textbox', { name: 'Edit C2', exact: true });+ await editor.fill('=SUM(');+ await expect(editor).toHaveAttribute('aria-describedby', /help/);await page.getByRole('tab', { name: 'Forecast 2027', exact: true }).click();- await expect(- page.getByRole('textbox', { name: 'Formula bar', exact: true })- ).toHaveValue('=SUM(');+ const bar = page.getByRole('textbox', { name: 'Formula bar', exact: true });+ await expect(bar).toHaveValue('=SUM(');+ await expect(bar).not.toHaveAttribute('aria-describedby');await drag(page,page.locator('[data-address="A1"]'),diff --git a/apps/web/src/features/block-spreadsheet/components/FormulaInput.test.tsx b/apps/web/src/features/block-spreadsheet/components/FormulaInput.test.tsxindex af0d7e36ac..25ef33515e 100644--- a/apps/web/src/features/block-spreadsheet/components/FormulaInput.test.tsx+++ b/apps/web/src/features/block-spreadsheet/components/FormulaInput.test.tsx@@ -36,7 +36,7 @@ it('accepts completion without committing, keeps caret/focus, and dismisses help}) as HTMLTextAreaElement;input.focus();input.setSelectionRange(3, 3);- fireEvent.select(input);+ fireEvent.input(input);await screen.findByRole('listbox', { name: 'Formula suggestions' });fireEvent.keyDown(input, { key: 'Tab' });await waitFor(() => expect(input.value).toBe('=SUM('));@@ -54,6 +54,51 @@ it('accepts completion without committing, keeps caret/focus, and dismisses helpexpect(key).toHaveBeenCalledTimes(1);});+it('opens help only after typing and closes it on an outside press or Escape until the next keystroke', async () => {+ const complete = vi.fn(async () => ({+ expecting: [{ FunctionName: 'SU' }],+ replace_from: 0,+ }));+ const view = render(() => (+ <>+ <button type="button">Sheet2</button>+ <FormulaInput+ label="Formula"+ value="=SU"+ class=""+ autoFocus+ complete={complete}+ onInput={() => {}}+ onKeyDown={() => {}}+ onBlur={() => {}}+ />+ </>+ ));+ const input = view.getByRole('textbox') as HTMLTextAreaElement;+ input.setSelectionRange(3, 3);+ fireEvent.select(input);+ fireEvent.click(input);+ await Promise.resolve();+ expect(complete).not.toHaveBeenCalled();+ expect(screen.queryByRole('listbox')).toBeNull();++ fireEvent.input(input);+ await screen.findByRole('listbox');+ fireEvent.pointerDown(view.getByRole('button', { name: 'Sheet2' }));+ expect(screen.queryByRole('listbox')).toBeNull();+ fireEvent.select(input);+ await Promise.resolve();+ expect(screen.queryByRole('listbox')).toBeNull();++ fireEvent.input(input);+ await screen.findByRole('listbox');+ fireEvent.keyDown(input, { key: 'Escape' });+ expect(screen.queryByRole('listbox')).toBeNull();+ fireEvent.select(input);+ await Promise.resolve();+ expect(screen.queryByRole('listbox')).toBeNull();+});+it('does not request or display suggestions for a read-only formula', async () => {const complete = vi.fn();const view = render(() => (@@ -97,7 +142,7 @@ it.each(['readonly', 'pickingReference'] as const)(const input = view.getByRole('textbox') as HTMLTextAreaElement;input.focus();input.setSelectionRange(3, 3);- fireEvent.select(input);+ fireEvent.input(input);await screen.findByRole('listbox');expect(input.getAttribute('aria-controls')).toBeTruthy();expect(input.getAttribute('aria-activedescendant')).toBeTruthy();diff --git a/apps/web/src/features/block-spreadsheet/components/FormulaInput.tsx b/apps/web/src/features/block-spreadsheet/components/FormulaInput.tsxindex 35cd095eb7..f8d8688fcd 100644--- a/apps/web/src/features/block-spreadsheet/components/FormulaInput.tsx+++ b/apps/web/src/features/block-spreadsheet/components/FormulaInput.tsx@@ -60,6 +60,9 @@ export function FormulaInput(props: FormulaInputProps) {get autoFocus() {return props.autoFocus || transferFocus;},+ get typed() {+ return transferFocus;+ },onFocus: () => {transferFocus = false;props.onFocus?.();@@ -84,11 +87,14 @@ export function FormulaInput(props: FormulaInputProps) {);}-function FormulaTextarea(props: FormulaInputProps) {+function FormulaTextarea(props: FormulaInputProps & { typed?: boolean }) {let input!: HTMLTextAreaElement;let popup: HTMLDivElement | undefined;const id = createUniqueId();const [focused, setFocused] = createSignal(false);+ // Help opens only after the user types, so focusing, clicking, or switching+ // sheets with a formula draft never covers the grid.+ let typing = !!props.typed;const assistance = createFormulaAssistance((text, cursor) =>props.complete?.(text, cursor) ?? Promise.resolve(undefined),@@ -124,10 +130,14 @@ function FormulaTextarea(props: FormulaInputProps) {if (argument() < 0 || !args.length) return -1;return Math.min(argument(), args.length - 1);};+ const dismiss = () => {+ typing = false;+ assistance.dismiss();+ };const update = () => {if (!focused() || props.readonly) return;props.onSelectionChange?.(input.selectionStart, input.selectionEnd);- if (props.pickingReference) assistance.dismiss();+ if (props.pickingReference || !typing) dismiss();elseassistance.update(input.value, input.selectionStart, input.selectionEnd);};@@ -193,6 +203,7 @@ function FormulaTextarea(props: FormulaInputProps) {update();}}onInput={() => {+ typing = true;props.onInput(input.value);update();}}@@ -205,6 +216,7 @@ function FormulaTextarea(props: FormulaInputProps) {onKeyDown={(event) => {event.stopPropagation();if (!props.readonly && assistance.keyDown(event)) {+ if (event.key === 'Escape') typing = false;if (event.key === 'ArrowDown' || event.key === 'ArrowUp')popup?.querySelector('[aria-selected="true"]')@@ -215,7 +227,7 @@ function FormulaTextarea(props: FormulaInputProps) {}}onBlur={() => {setFocused(false);- assistance.dismiss();+ dismiss();props.onBlur();}}/>@@ -236,13 +248,14 @@ function FormulaTextarea(props: FormulaInputProps) {onReady={(element) => {popup = element;}}+ onOutsidePress={dismiss}><Show when={choices().length}><divid={`${id}-list`}role="listbox"aria-label="Formula suggestions"- class="min-h-0 max-h-48 shrink overflow-y-auto overscroll-contain p-1"+ class="min-h-0 max-h-40 shrink overflow-y-auto overscroll-contain p-1"><For each={choices()}>{(choice, index) => {@@ -256,7 +269,7 @@ function FormulaTextarea(props: FormulaInputProps) {id={`${id}-${index()}`}role="option"aria-selected={assistance.selected() === index()}- class="flex items-center gap-3 rounded px-2.5 py-2 text-xs touch:min-h-[44px] touch:text-sm"+ class="flex items-center gap-2 rounded px-2 py-1 text-xs touch:min-h-[44px] touch:text-sm"classList={{'bg-accent-bg text-accent':assistance.selected() === index(),@@ -280,10 +293,12 @@ function FormulaTextarea(props: FormulaInputProps) {</Show><divid={`${id}-help`}- class="min-h-0 overflow-y-auto border-t border-edge-muted px-3 py-3 text-xs touch:text-sm"- classList={{ 'touch:hidden': choices().length > 0 }}+ class="min-h-0 overflow-y-auto px-2.5 py-1.5 text-xs touch:text-sm"+ classList={{+ 'border-t border-edge-muted touch:hidden': choices().length > 0,+ }}>- <div class="mb-2 break-words font-mono leading-5 text-ink">+ <div class="break-words font-mono leading-5 text-ink"><span class="font-semibold text-accent">{name()}</span>(<For each={info().args}>{(arg, index) => (@@ -302,22 +317,14 @@ function FormulaTextarea(props: FormulaInputProps) {</For>)</div>- <p class="leading-5 text-ink-muted">- {activeArgument() >= 0- ? info().args[activeArgument()][2]- : functionSummary(info())}- </p>- <Show when={info().examples[0]}>- <p class="mt-2 break-words font-mono text-[11px] leading-4 text-ink-subtle">- {info().examples[0]}+ <Show when={!choices().length}>+ <p class="text-[11px] leading-4 text-ink-muted">+ {activeArgument() >= 0+ ? info().args[activeArgument()][2]+ : functionSummary(info())}</p></Show></div>- <Show when={choices().length}>- <div class="shrink-0 border-t border-edge-muted px-3 py-2 text-[10px] text-ink-subtle touch:hidden">- ↑↓ Navigate · Tab or Enter to insert · Esc to dismiss- </div>- </Show></FormulaPopup></Portal></Show>@@ -417,10 +424,19 @@ function FormulaPopup(props: {interactive: boolean;children: import('solid-js').JSX.Element;onReady: (element: HTMLDivElement) => void;+ onOutsidePress: () => void;}) {let element!: HTMLDivElement;onMount(() => {props.onReady(element);+ // Pressing a cell, sheet tab, or toolbar keeps the draft but closes help.+ const press = (event: PointerEvent) => {+ const target = event.target as Node;+ if (!element.contains(target) && !props.anchor.contains(target))+ props.onOutsidePress();+ };+ document.addEventListener('pointerdown', press, true);+ onCleanup(() => document.removeEventListener('pointerdown', press, true));let alive = true;const update = async () => {const position = await computePosition(props.anchor, element, {@@ -457,7 +473,7 @@ function FormulaPopup(props: {<divref={element}style={{ visibility: 'hidden' }}- class="fixed z-[100] flex w-96 max-w-[calc(100vw-24px)] flex-col overflow-hidden rounded-lg border border-edge bg-panel text-ink shadow-xl"+ class="fixed z-[100] flex w-72 max-w-[calc(100vw-24px)] flex-col overflow-hidden rounded-lg border border-edge bg-panel text-ink shadow-xl"classList={{ 'pointer-events-none': !props.interactive }}onPointerDown={(event) => {event.preventDefault();diff --git a/apps/web/src/features/block-spreadsheet/core/formula-completion.test.ts b/apps/web/src/features/block-spreadsheet/core/formula-completion.test.tsindex 1e9bb32197..00f0ab725b 100644--- a/apps/web/src/features/block-spreadsheet/core/formula-completion.test.ts+++ b/apps/web/src/features/block-spreadsheet/core/formula-completion.test.ts@@ -43,6 +43,7 @@ describe('formula help using the actual IronCalc parser', () => {kind: 'list',names: ['NORM.DIST'],});+ expect(complete('=')).toBeUndefined();expect(formulaFunctions.TODAY).toBeDefined();expect(formulaFunctions.HYPERLINK).toBeUndefined();});diff --git a/apps/web/src/features/block-spreadsheet/core/formula-completion.ts b/apps/web/src/features/block-spreadsheet/core/formula-completion.tsindex e32d305d40..4720293715 100644--- a/apps/web/src/features/block-spreadsheet/core/formula-completion.ts+++ b/apps/web/src/features/block-spreadsheet/core/formula-completion.ts@@ -40,8 +40,6 @@ export function formulaCompletion(text: string,context: CompletionContext): FormulaCompletion {- if (text.trim() === '=')- return { kind: 'list', names: names.slice(0, 40), from: 1 };const argument = context.expecting.find((token) => typeof token === 'object' && 'Argument' in token);@@ -53,7 +51,12 @@ export function formulaCompletion(const partial = context.expecting.find((token) => typeof token === 'object' && 'FunctionName' in token);- if (!partial || typeof partial !== 'object' || !('FunctionName' in partial))+ if (+ !partial ||+ typeof partial !== 'object' ||+ !('FunctionName' in partial) ||+ !partial.FunctionName+ )return;const matches = names.filter((name) =>name.startsWith(partial.FunctionName.toUpperCase())diff --git a/docs/AGENT_GUIDE/documents.md b/docs/AGENT_GUIDE/documents.mdindex 78b4c09ca3..69decce2af 100644--- a/docs/AGENT_GUIDE/documents.md+++ b/docs/AGENT_GUIDE/documents.md@@ -159,12 +159,17 @@ and **Cancel edit** while editing, so a software keyboard is sufficient. Swipe tformatting ribbon horizontally to reach more controls. On narrow screens, **Add rows**is in the active sheet's actions menu; **Import and export** stays at the bottom right.-Both editors offer formula autocomplete. Type `=` or a function prefix such as-`=SU`, use Up/Down to choose a suggestion, and Tab or Enter to insert it. Clicking-a suggestion also keeps focus in the editor. The popup shows a description,-signature, and example; after `(` it highlights the current argument, including-inside nested formulas. Escape dismisses help first, then cancels editing on a-second press. Suggestions do not appear inside quoted text or in view-only mode.+Both editors offer formula autocomplete. Type a function prefix such as `=SU`+(a bare `=` shows nothing), use Up/Down to choose a suggestion, and Tab or Enter+to insert it. Clicking a suggestion also keeps focus in the editor. The compact+popup lists each function with a one-line description and the highlighted+function's signature; after `(` it shows the signature with the current argument+highlighted, including inside nested formulas. Help opens only after typing:+focusing or clicking into an existing formula, switching sheet tabs, or picking a+reference leaves it closed. Pressing outside the editor closes it, and Escape+dismisses help first, then cancels editing on a second press; either keeps help+closed until the next keystroke. Suggestions do not appear inside quoted text or+in view-only mode.While editing a formula, click a cell or drag across cells to insert a referenceat the caret (for example, type `=SUM(`, then drag B4 through B7). The draft updates