Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(editor): format the first paragraph in backward selections (#9072)

gbirmanfeec6ef4e035
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md
index b042803350..6c5937acaf 100644
--- a/apps/web/AGENTS.md
+++ b/apps/web/AGENTS.md
@@ -92,6 +92,11 @@ Channel composer draft persistence has a backend-free browser regression:
`bunx playwright test --config src/features/channel/Input/browser-test/playwright.config.ts`.
It types a draft, remounts the composer over it, and checks the blank lines survive.
+Block formatting passes a forward copy of the selection to Lexical's
+`$setBlocksType` so backward selections include their first paragraph and keep
+the editor's actual selection direction. The Markdown core browser suite's
+`quotes.browser.e2e.ts` checks Quote through the floating toolbar and after saving.
+
A frontend dev server runs against the **dev** backend, so this needs no local stack, Docker, Rust build, or `sudo`:
```sh
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/README.md b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/README.md
index 48e131f4f2..2fc163f430 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/README.md
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/README.md
@@ -32,3 +32,12 @@ the local fixture are intercepted. Run just this case by adding
Screenshots: [desktop](proof/autolinks-desktop.png) and
[phone](proof/autolinks-mobile.png).
+
+## Message quotes
+
+`quotes.browser.e2e.ts` selects three paragraphs backward and applies Quote
+through the real floating toolbar on desktop/tablet and the composer format
+ribbon on phones. It checks the first paragraph's quote styling and the saved
+message. Run just this case by adding `quotes.browser.e2e.ts` to the command above.
+
+Screenshots: [desktop](proof/quotes-desktop.png) and [phone](proof/quotes-mobile.png).
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/proof/quotes-desktop.png b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/proof/quotes-desktop.png
new file mode 100644
index 0000000000..ac17d28574
Binary files /dev/null and b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/proof/quotes-desktop.png differ
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/proof/quotes-mobile.png b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/proof/quotes-mobile.png
new file mode 100644
index 0000000000..26f237a601
Binary files /dev/null and b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/proof/quotes-mobile.png differ
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.browser.e2e.ts b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.browser.e2e.ts
new file mode 100644
index 0000000000..0409215ec9
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.browser.e2e.ts
@@ -0,0 +1,61 @@
+import { expect, test } from '@playwright/test';
+
+test('quoting a backward selection includes the first paragraph', async ({
+ page,
+ context,
+}, testInfo) => {
+ await context.route('**/*', async (route) => {
+ const url = new URL(route.request().url());
+ if (
+ url.hostname === 'localhost' &&
+ !url.pathname.includes('/__macro_dev/')
+ ) {
+ await route.continue();
+ } else {
+ await route.fulfill({ status: 401, body: '{}' });
+ }
+ });
+ await page.routeWebSocket('**/*', (socket) => socket.close());
+ const errors: string[] = [];
+ page.on('pageerror', (error) => errors.push(error.message));
+ await page.goto(
+ '/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.html'
+ );
+ const editor = page.getByRole('textbox', { name: 'Message' });
+ await expect(editor.locator('p')).toHaveCount(3);
+ await editor.focus();
+ await page.keyboard.press('Control+End');
+ await page.keyboard.press('Control+Shift+Home');
+ if (testInfo.project.name === 'mobile') {
+ // Phones use the composer's format ribbon instead of the floating menu.
+ await page.getByRole('button', { name: 'Blockquote', exact: true }).tap();
+ } else {
+ await page.getByRole('button', { name: 'Body', exact: true }).click();
+ await page
+ .getByRole('menuitemradio', { name: 'Quote', exact: true })
+ .click();
+ }
+
+ await expect(editor.locator('blockquote')).toHaveCount(3);
+ await expect(editor.locator('blockquote').first()).toHaveText(
+ 'First paragraph with enough words to wrap onto several lines.'
+ );
+ const quotes = await editor.locator('blockquote').evaluateAll((elements) =>
+ elements.map((element) => ({
+ border: getComputedStyle(element).borderLeftWidth,
+ fontStyle: getComputedStyle(element).fontStyle,
+ }))
+ );
+ expect(quotes).toEqual([
+ { border: '2px', fontStyle: 'italic' },
+ { border: '2px', fontStyle: 'italic' },
+ { border: '2px', fontStyle: 'italic' },
+ ]);
+ await page.getByRole('button', { name: 'Save message' }).click();
+ await expect(page.getByTestId('saved').locator('blockquote')).toHaveCount(3);
+ expect(errors).toEqual([]);
+ await page.screenshot({
+ path: testInfo.outputPath('quotes.png'),
+ fullPage: true,
+ });
+});
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.html b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.html
new file mode 100644
index 0000000000..4739dfdd9b
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.html
@@ -0,0 +1,5 @@
+<!doctype html>
+<html lang="en">
+ <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Message quotes</title></head>
+ <body><div id="root"></div><script type="module" src="./quotes.tsx"></script></body>
+</html>
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.tsx
new file mode 100644
index 0000000000..5ca2b5481a
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/quotes.tsx
@@ -0,0 +1,55 @@
+import '@app/index.css';
+import { FormatButtons } from '@app/features/channel/Input/FormatButtons';
+import {
+ applyInlineFormat,
+ applyNodeFormat,
+} from '@app/features/channel/Input/utils/formatting';
+import { isMobile } from '@core/mobile/isMobile';
+import { isTouchDevice } from '@core/mobile/isTouchDevice';
+import { createSignal, Show } from 'solid-js';
+import { render } from 'solid-js/web';
+import { buildConfig } from '../../../builder/MarkdownConfigBuilder';
+import { channelTheme } from '../../../theme';
+import { ComposerEditor } from '../../ComposerEditor';
+import { StaticMarkdown, StaticMarkdownContext } from '../StaticMarkdown';
+
+function Fixture() {
+ document.documentElement.dataset.touchDevice = String(isTouchDevice());
+ const [saved, setSaved] = createSignal('');
+ const editor = buildConfig('chat')
+ .namespace('message-quotes')
+ .withFloatingFormatMenu();
+ return (
+ <StaticMarkdownContext>
+ <main class="p-6 bg-page text-ink max-w-2xl space-y-6">
+ <h1 class="text-xl font-medium">Message quotes</h1>
+ <Show when={isMobile()}>
+ <div class="flex flex-wrap">
+ <FormatButtons
+ selectionState={() => editor.selection}
+ onInlineFormat={(format) =>
+ applyInlineFormat(editor.lexical, format)
+ }
+ onNodeFormat={(format) => applyNodeFormat(editor.lexical, format)}
+ />
+ </div>
+ </Show>
+ <ComposerEditor
+ config={editor}
+ initialValue={
+ 'First paragraph with enough words to wrap onto several lines.\n\nSecond paragraph with additional words.\n\nThird paragraph finishes the message.'
+ }
+ ariaLabel="Message"
+ />
+ <button onClick={() => setSaved(editor.controls.getMarkdown())}>
+ Save message
+ </button>
+ <section data-testid="saved">
+ <StaticMarkdown markdown={saved()} theme={channelTheme} />
+ </section>
+ </main>
+ </StaticMarkdownContext>
+ );
+}
+
+render(() => <Fixture />, document.getElementById('root')!);
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin.test.ts
new file mode 100644
index 0000000000..80589e91f0
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin.test.ts
@@ -0,0 +1,72 @@
+import { createHeadlessEditor } from '@lexical/headless';
+import { HeadingNode, QuoteNode } from '@lexical/rich-text';
+import {
+ $createParagraphNode,
+ $createRangeSelection,
+ $createTextNode,
+ $getRoot,
+ $getSelection,
+ $isRangeSelection,
+ $setSelection,
+} from 'lexical';
+import { describe, expect, it } from 'vitest';
+import { NODE_TRANSFORM, nodeTransformPlugin } from './nodeTransformPlugin';
+
+describe('block formatting selection boundaries', () => {
+ for (const backward of [false, true]) {
+ for (const endAtBlockStart of [false, true]) {
+ it.each(['quote', 'heading1'] as const)(
+ `applies %s with backward=${backward}, endAtBlockStart=${endAtBlockStart}`,
+ (format) => {
+ const editor = createHeadlessEditor({
+ nodes: [HeadingNode, QuoteNode],
+ onError: (error) => {
+ throw error;
+ },
+ });
+ const unregister = nodeTransformPlugin()(editor);
+ editor.update(
+ () => {
+ const first = $createTextNode('First paragraph');
+ const middle = $createTextNode('Second paragraph');
+ const last = $createTextNode('Third paragraph');
+ $getRoot().append(
+ $createParagraphNode().append(first),
+ $createParagraphNode().append(middle),
+ $createParagraphNode().append(last)
+ );
+ const selection = $createRangeSelection();
+ const start = backward ? selection.focus : selection.anchor;
+ const end = backward ? selection.anchor : selection.focus;
+ start.set(first.getKey(), 0, 'text');
+ end.set(
+ last.getKey(),
+ endAtBlockStart ? 0 : last.getTextContentSize(),
+ 'text'
+ );
+ $setSelection(selection);
+ const selectedText = selection.getTextContent();
+
+ editor.dispatchCommand(NODE_TRANSFORM, format);
+
+ const type = format === 'quote' ? 'quote' : 'heading';
+ expect(
+ $getRoot()
+ .getChildren()
+ .map((node) => node.getType())
+ ).toEqual([type, type, endAtBlockStart ? 'paragraph' : type]);
+ const updatedSelection = $getSelection();
+ expect($isRangeSelection(updatedSelection)).toBe(true);
+ if ($isRangeSelection(updatedSelection)) {
+ expect(updatedSelection.isBackward()).toBe(backward);
+ expect(updatedSelection.getTextContent()).toBe(selectedText);
+ }
+ },
+ { discrete: true }
+ );
+ unregister();
+ }
+ );
+ }
+ }
+});
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin.ts
index 436dd634b0..2a8f64a03d 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin.ts
@@ -17,7 +17,10 @@ import {
HeadingNode,
QuoteNode,
} from '@lexical/rich-text';
-import { $setBlocksType } from '@lexical/selection';
+import {
+ $ensureForwardRangeSelection,
+ $setBlocksType,
+} from '@lexical/selection';
import type { ElementName } from '@macro-inc/lexical-core';
import { CustomCodeNode } from '@macro-inc/lexical-core';
import type {
@@ -110,7 +113,12 @@ function registerNodeTransform(editor: LexicalEditor) {
return true;
}
- $setBlocksType(selection, NodeTransforms[transform]);
+ // Lexical excludes a focus at a block's start, even for backward ranges.
+ // Normalize a copy so the first selected block is included while the
+ // editor's actual selection keeps its direction.
+ const forwardSelection = selection.clone();
+ $ensureForwardRangeSelection(forwardSelection);
+ $setBlocksType(forwardSelection, NodeTransforms[transform]);
return true;
},
COMMAND_PRIORITY_EDITOR
diff --git a/docs/agent-guide/channels.md b/docs/agent-guide/channels.md
index 14d57e4f83..50a9726ff0 100644
--- a/docs/agent-guide/channels.md
+++ b/docs/agent-guide/channels.md
@@ -33,6 +33,16 @@ Home previews remain display-only as described above. The backend-free browser
check is in
`apps/web/src/lib/core/component/LexicalMarkdown/component/core/browser-test/README.md`.
+## Block formatting
+
+Selecting several paragraphs and choosing Quote or a heading in the floating
+format toolbar applies it to the same paragraphs in either selection direction.
+Verify a selection from the last paragraph back to the start of the first:
+the first paragraph must receive the format too. A range ending at the start of
+the following paragraph leaves that following paragraph unchanged.
+The backend-free quote check is documented in the Markdown browser-test README
+linked above.
+
## Empty direct messages
An empty human DM shows the centered conversations illustration and “Say hello”