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): paste plain text verbatim inside code blocks (#9101)

gbirman97af7878d102
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md
index 5d2ce84d77..35f0c97685 100644
--- a/apps/web/AGENTS.md
+++ b/apps/web/AGENTS.md
@@ -117,6 +117,7 @@ checks project ranking and insertion on desktop and phone.
Channel composer draft persistence has a backend-free browser regression:
`bunx playwright test --config src/features/channel/Input/browser-test/playwright.config.ts`.
It checks draft blank lines across remounts and pending sends on desktop and phone: clear immediately, restore failed sends, and preserve a new draft typed during delivery.
+It also pastes a Macro link and Markdown into a code block, which must keep both as literal text.
Block formatting passes a forward copy of the selection to Lexical's
`$setBlocksType` so backward selections include their first paragraph and keep
diff --git a/apps/web/src/features/channel/Input/browser-test/README.md b/apps/web/src/features/channel/Input/browser-test/README.md
index f61b433791..f0e958a2c0 100644
--- a/apps/web/src/features/channel/Input/browser-test/README.md
+++ b/apps/web/src/features/channel/Input/browser-test/README.md
@@ -19,3 +19,8 @@ line.
The send regression checks desktop and phone: a pending send clears the editor,
a rejected send restores its draft, and a rejection preserves a newer draft
typed while delivery was pending. The fixture controls completion locally.
+
+The code-paste regression types three backticks to open a code block in the
+production composer and pastes a Macro link, then Markdown. Both land as literal
+code text: the link used to become a mention the code block dropped, and the
+Markdown used to lose its syntax.
diff --git a/apps/web/src/features/channel/Input/browser-test/code-paste.browser.e2e.ts b/apps/web/src/features/channel/Input/browser-test/code-paste.browser.e2e.ts
new file mode 100644
index 0000000000..ba2942b185
--- /dev/null
+++ b/apps/web/src/features/channel/Input/browser-test/code-paste.browser.e2e.ts
@@ -0,0 +1,63 @@
+import { expect, test } from '@playwright/test';
+
+const channelUrl =
+ 'https://dev.macro.com/app/channel/0195ceb6-ec2e-7023-80e4-6e084fa6cccd';
+const markdown = '# heading\n- item **bold**';
+
+test('pastes Macro links and Markdown into a code block verbatim', async ({
+ page,
+}) => {
+ await page.route('**/*', async (route) => {
+ const url = new URL(route.request().url());
+ if (
+ url.hostname === 'localhost' &&
+ !url.pathname.startsWith('/__macro_dev/')
+ )
+ await route.continue();
+ else
+ await route.fulfill({
+ status: 401,
+ contentType: 'application/json',
+ body: '{"responses":[]}',
+ });
+ });
+ await page.routeWebSocket('**/*', (socket) => socket.close());
+ const errors: string[] = [];
+ page.on('pageerror', (error) => errors.push(error.message));
+ await page.goto('/src/features/channel/Input/browser-test/draft.html');
+ const editor = page.locator('[data-markdown-editable]');
+ await expect(editor).toBeVisible({ timeout: 60_000 });
+ await editor.click();
+ await page.keyboard.type('``` ');
+ const code = editor.locator('code');
+ await expect(code).toHaveCount(1);
+
+ const paste = (text: string) =>
+ editor.evaluate((element, text) => {
+ const clipboardData = new DataTransfer();
+ clipboardData.setData('text/plain', text);
+ element.dispatchEvent(
+ new ClipboardEvent('paste', {
+ clipboardData,
+ bubbles: true,
+ cancelable: true,
+ })
+ );
+ }, text);
+
+ // Outside code this link becomes a mention, which the code block dropped.
+ await paste(channelUrl);
+ await expect(code).toHaveText(channelUrl);
+ // Parsing this as Markdown stripped the heading, list, and bold syntax.
+ await paste(`\n${markdown}`);
+ await expect.poll(() => code.innerText()).toBe(`${channelUrl}\n${markdown}`);
+
+ await page.getByRole('button', { name: 'Send message', exact: true }).click();
+ // The trailing paragraph after the code block sends as blank lines.
+ await expect
+ .poll(async () =>
+ (await page.getByLabel('Sent message').textContent())?.trimEnd()
+ )
+ .toBe(`\`\`\`plaintext\n${channelUrl}\n${markdown}\n\`\`\``);
+ expect(errors).toEqual([]);
+});
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/text-paste/textPastePlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/text-paste/textPastePlugin.ts
index 7bb16da969..f5ba5a4472 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/text-paste/textPastePlugin.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/text-paste/textPastePlugin.ts
@@ -10,6 +10,7 @@ import {
type BlockName,
BlockRegistry,
} from '@core/block';
+import { shouldUseMobileClipboardImageRecovery } from '@core/mobile/mobileClipboardImageRecovery';
import { fireAndForget } from '@core/util/fireAndForget';
import { isValidMacroAppHostname } from '@core/util/macroAppUrl';
import { $isCodeNode } from '@lexical/code';
@@ -44,6 +45,7 @@ import {
$isRangeSelection,
COMMAND_PRIORITY_HIGH,
type LexicalEditor,
+ type LexicalNode,
PASTE_COMMAND,
} from 'lexical';
import { $insertNodesAndSplitList } from '../../utils';
@@ -182,15 +184,19 @@ export function parsePastedGithubPrUrl(text: string): PrRef | null {
return { owner: match[1], repo: match[2], number: Number(match[3]) };
}
+function $getCodeAncestor(node: LexicalNode) {
+ return $findMatchingParent(
+ node,
+ (node) => $isCodeNode(node) || $isCustomCodeNode(node)
+ );
+}
+
+/** Whether both ends of the selection sit in the same code block. */
function $isSelectionInCode(): boolean {
const selection = $getSelection();
if (!$isRangeSelection(selection)) return false;
- return (
- $findMatchingParent(
- selection.anchor.getNode(),
- (node) => $isCodeNode(node) || $isCustomCodeNode(node)
- ) !== null
- );
+ const code = $getCodeAncestor(selection.anchor.getNode());
+ return code !== null && code.is($getCodeAncestor(selection.focus.getNode()));
}
/**
@@ -271,6 +277,25 @@ function registerTextPastePlugin(
const pastedText: string =
event.clipboardData?.getData('text/plain') || '';
+ // Code blocks hold plain text, so paste it verbatim: the code
+ // highlighter drops mentions and chips inserted there, and parsing
+ // the text as Markdown strips its syntax. Files, including mobile
+ // pasteboard images, still go to the file paste handler.
+ if ($isSelectionInCode()) {
+ const clipboard = event.clipboardData;
+ if (
+ !clipboard ||
+ !pastedText ||
+ clipboard.files.length > 0 ||
+ shouldUseMobileClipboardImageRecovery(clipboard)
+ ) {
+ return false;
+ }
+ event.preventDefault();
+ $getSelection()?.insertRawText(pastedText);
+ return true;
+ }
+
// Check for theme JSON before checking for Macro URL
const themeV3 =
parseThemeV3Json(pastedText) ??
@@ -294,11 +319,7 @@ function registerTextPastePlugin(
const githubPr = parsePastedGithubPrUrl(pastedText);
if (githubPr && editor.hasNode(PullRequestMentionNode)) {
const selection = $getSelection();
- if (
- !$isRangeSelection(selection) ||
- !selection.isCollapsed() ||
- $isSelectionInCode()
- ) {
+ if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
return false;
}
@@ -313,11 +334,7 @@ function registerTextPastePlugin(
editor.hasNode(BookingLinkMentionNode)
) {
const selection = $getSelection();
- if (
- !$isRangeSelection(selection) ||
- !selection.isCollapsed() ||
- $isSelectionInCode()
- ) {
+ if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
return false;
}
diff --git a/docs/agent-guide/ai-chat.md b/docs/agent-guide/ai-chat.md
index ec6af25e2d..10aba8fdd0 100644
--- a/docs/agent-guide/ai-chat.md
+++ b/docs/agent-guide/ai-chat.md
@@ -1600,9 +1600,10 @@ choose **Reply to this** above the selection. The composer inserts a single-line
**Replying to** preview with the same quote-reply styling as channel replies.
Click the preview to open the full **Referenced text** viewer. While editable,
hover the preview for its menu: **Copy**, **Convert to text**, or **Delete**.
-A plain-text paste over 1500 characters collapses into a **pasted** chip that
-opens the same viewer, titled **Pasted text**. Text in either viewer can be
-drag-selected and copied, from the composer and from sent messages.
+A plain-text paste over 1500 characters outside a code block collapses into a
+**pasted** chip that opens the same viewer, titled **Pasted text**. Text in
+either viewer can be drag-selected and copied, from the composer and from sent
+messages.
Selecting text in the composer or outside the transcript must not show the reply
button; clearing the transcript selection dismisses it.
diff --git a/docs/agent-guide/documents.md b/docs/agent-guide/documents.md
index 208f0f37f2..07d21c5a26 100644
--- a/docs/agent-guide/documents.md
+++ b/docs/agent-guide/documents.md
@@ -2394,8 +2394,12 @@ split. A PR mention always stays on one line: a long title truncates with an
ellipsis before the `#N`, so read the full title from the hover card. In a sent
message, a mention an agent wrote by URL alone shows `owner/repo#N` until its
title loads and keeps the same width either way. A PR Macro has not synced
-stays an ordinary link, and pasting inside a code block or over a selection
-keeps the default behavior.
+stays an ordinary link, and pasting over a selection keeps the default
+behavior.
+
+Pasting inside a code block inserts the clipboard's plain text verbatim: links
+stay text instead of becoming mentions, long pastes do not collapse into a
+**pasted** chip, and Markdown syntax is kept. Pasted files still attach.
1. `Create` → `Document D`. The app navigates to `/app/md/<uuid>` with the **title field
focused**.