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
chore: allow images inside table cells (#5928)
Wolf Mermelsteind8f88a991909
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableCellContent.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableCellContent.test.tsnew file mode 100644index 0000000000..6310c632f6--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tableCellContent.test.ts@@ -0,0 +1,73 @@+import {+ $createTableNode,+ $createTableRowNode,+ $isTableNode,+} from '@lexical/table';+import {+ $createImageNode,+ $isImageNode,+} from '@macro-inc/lexical-core/nodes/ImageNode';+import type { LexicalEditor } from 'lexical';+import { describe, expect, it } from 'vitest';+import {+ $createTextCell,+ $getCell,+ buildTable,+ createTableTestEditor,+ textGrid,+} from './tableTestUtils';++function createTableEditor(): LexicalEditor {+ return createTableTestEditor();+}++describe('table cell allowed content', () => {+ it('keeps an image appended to a table cell', async () => {+ const editor = createTableEditor();+ await buildTable(editor, textGrid([['hello']]));++ await new Promise<void>((resolve) => {+ editor.update(+ () => {+ $getCell(0, 0).append(+ $createImageNode({+ srcType: 'url',+ url: 'https://example.com/cat.png',+ alt: 'cat',+ })+ );+ },+ { onUpdate: () => resolve() }+ );+ });++ editor.read(() => {+ const image = $getCell(0, 0).getChildren().find($isImageNode);+ expect(image).toBeDefined();+ expect(image?.getUrl()).toBe('https://example.com/cat.png');+ expect(image?.getAlt()).toBe('cat');+ });+ });++ it('still strips nested tables from cells', async () => {+ const editor = createTableEditor();+ await buildTable(editor, textGrid([['hello']]));++ await new Promise<void>((resolve) => {+ editor.update(+ () => {+ const nested = $createTableNode();+ const row = $createTableRowNode();+ row.append($createTextCell('inner'));+ nested.append(row);+ $getCell(0, 0).append(nested);+ },+ { onUpdate: () => resolve() }+ );+ });++ editor.read(() => {+ expect($getCell(0, 0).getChildren().some($isTableNode)).toBe(false);+ });+ });+});diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tablePlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tablePlugin.tsindex 2773869c8a..966835e601 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tablePlugin.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/tablePlugin.ts@@ -72,6 +72,7 @@ function _registerTablePlugin(editor: LexicalEditor, props: TablePluginProps) {'quote','code','custom-code',+ 'image',];return editor.registerNodeTransform(TableCellNode, (node) => {const children = node.getChildren();diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/styles.css b/apps/web/src/lib/core/component/LexicalMarkdown/styles.cssindex 3faca6af08..0829077e01 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/styles.css+++ b/apps/web/src/lib/core/component/LexicalMarkdown/styles.css@@ -386,6 +386,11 @@ in a cell — move the indent inside the box so markers stay visible.padding-left: 1.5em;}+/* Keep images from stretching the cell wider than the column. */+.md-table-cell img {+ max-width: 100%;+}+.md-table-cell-resizer {position: absolute;right: -4px;diff --git a/packages/lexical-core/tests/tables.test.ts b/packages/lexical-core/tests/tables.test.tsindex 3b8aab9c8e..81c6fbeab4 100644--- a/packages/lexical-core/tests/tables.test.ts+++ b/packages/lexical-core/tests/tables.test.ts@@ -19,10 +19,12 @@ import {$createParagraphNode,$createTextNode,$getRoot,+ $isElementNode,createEditor,} from 'lexical';import { describe, expect, it } from 'vitest';import { SupportedNodeTypes } from '../node-list';+import { $createImageNode, $isImageNode } from '../nodes/ImageNode';import { EXTERNAL_TRANSFORMERS, INTERNAL_TRANSFORMERS } from '../transformers';function createTestEditor() {@@ -293,6 +295,104 @@ describe('lists inside table cells', () => {});});+describe('images inside table cells', () => {+ function $getFirstCellImage() {+ const table = $getFirstTable();+ const [row] = table.getChildren().filter($isTableRowNode);+ const [cell] = (row as TableRowNode).getChildren().filter($isTableCellNode);+ const image = cell+ .getChildren()+ .flatMap((child) =>+ $isImageNode(child)+ ? [child]+ : $isElementNode(child)+ ? child.getChildren().filter($isImageNode)+ : []+ )[0];+ expect(image).toBeDefined();+ return image!;+ }++ it('round-trips a markdown image in a cell through internal markdown', async () => {+ const editor = createTestEditor();+ await importMarkdown(+ editor,+ '<m-table><m-table-row><m-table-cell></m-table-cell></m-table-row></m-table>'+ );++ const markdown = exportMarkdown(editor);+ expect(markdown).toBe(+ '<m-table><m-table-row><m-table-cell></m-table-cell></m-table-row></m-table>'+ );++ await importMarkdown(editor, markdown);+ editor.getEditorState().read(() => {+ const image = $getFirstCellImage();+ expect(image.getUrl()).toBe('https://example.com/cat.png');+ expect(image.getAlt()).toBe('cat');+ });+ });++ it('round-trips a constrained image in a cell', async () => {+ const editor = createTestEditor();++ await buildEditorState(editor, () => {+ const table = $createTableNode();+ const row = $createTableRowNode();+ const cell = $createTableCellNode(TableCellHeaderStates.NO_STATUS);+ cell.append(+ $createImageNode({+ srcType: 'url',+ url: 'https://example.com/cat.png',+ alt: 'cat',+ width: 400,+ height: 300,+ constrainedWidth: 200,+ constrainedHeight: 150,+ })+ );+ row.append(cell);+ table.append(row);+ $getRoot().append(table);+ });++ const markdown = exportMarkdown(editor);+ expect(markdown).toContain('<m-image>');+ expect(markdown).toContain('"constrainedWidth":200');+ expect(markdown).toContain('"constrainedHeight":150');++ await importMarkdown(editor, markdown);+ editor.getEditorState().read(() => {+ const image = $getFirstCellImage();+ expect(image.getUrl()).toBe('https://example.com/cat.png');+ expect(image.getAlt()).toBe('cat');+ expect(image.getConstrainedWidth()).toBe(200);+ expect(image.getConstrainedHeight()).toBe(150);+ });+ });++ it('round-trips a markdown image in a cell through external pipe-table markdown', async () => {+ const editor = createTestEditor();+ await importMarkdown(+ editor,+ '<m-table><m-table-row><m-table-cell></m-table-cell><m-table-cell>plain</m-table-cell></m-table-row></m-table>'+ );++ const markdown = exportMarkdown(editor, EXTERNAL_TRANSFORMERS);+ expect(markdown).toBe('|  | plain |');++ await buildEditorState(editor, () => {+ $getRoot().clear();+ $convertFromMarkdownString(markdown, EXTERNAL_TRANSFORMERS);+ });+ editor.getEditorState().read(() => {+ const image = $getFirstCellImage();+ expect(image.getUrl()).toBe('https://example.com/cat.png');+ expect(image.getAlt()).toBe('cat');+ });+ });+});+describe('pipe table external transformer', () => {it('pads merged cells so exported rows stay rectangular', async () => {const editor = createTestEditor();diff --git a/packages/lexical-core/transformers/image.ts b/packages/lexical-core/transformers/image.tsindex cc5b7dadb0..afbb713981 100644--- a/packages/lexical-core/transformers/image.ts+++ b/packages/lexical-core/transformers/image.ts@@ -10,7 +10,7 @@ import {export const I_IMAGE_CONSTRAINED: ElementTransformer = {dependencies: [ImageNode, UnknownMentionNode],type: 'element',- regExp: /<m-image>(.*?)<\/m-image>/,+ regExp: /^<m-image>(.*?)<\/m-image>\s*$/,export: (node: LexicalNode) => {if (!$isImageNode(node)) return null;if (node.getSrcType() === 'local') return null;diff --git a/packages/lexical-core/transformers/tables.ts b/packages/lexical-core/transformers/tables.tsindex bc8e8b6e6b..43787c854e 100644--- a/packages/lexical-core/transformers/tables.ts+++ b/packages/lexical-core/transformers/tables.ts@@ -21,6 +21,7 @@ import {import { $isParagraphNode, $isTextNode, type LexicalNode } from "lexical";import { z } from "zod";import { CUSTOM_TRANSFORMERS } from "./customTransformers";+import { I_IMAGE_CONSTRAINED, IMAGE } from "./image";import { E_BLOCK_EQUATION_NODE, I_EQUATION_NODE } from "./katex";import {E_CONTACT_MENTION,@@ -97,6 +98,8 @@ const internalTransformersWithinTables: Transformer[] = [I_DOCUMENT_MENTION,I_CONTACT_MENTION,I_EQUATION_NODE,+ I_IMAGE_CONSTRAINED,+ IMAGE,...TRANSFORMERS,];@@ -243,6 +246,7 @@ const externalTransformersWithinTables: Transformer[] = [E_DOCUMENT_MENTION,E_CONTACT_MENTION,E_BLOCK_EQUATION_NODE,+ IMAGE,...HTML_ENTITY_TRANSFORMERS,...TRANSFORMERS,];@@ -257,7 +261,7 @@ function createTableCell(cellContent: string): TableCellNode {// Export escapes cell newlines as `\n` so rows stay on one line; restore// them so multi-line content (lists, multiple blocks) survives the trip.$convertFromMarkdownString(- cellContent.replace(/\\n/g, "\n"),+ cellContent.replace(/\\n/g, "\n").trim(),externalTransformersWithinTables,cellNode,true,