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(docs): reject container text writes and preserve table-cell text (#6468)
Wolf Mermelstein173fb6833f58
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.tsindex 1b245f10bd..65092517de 100644--- 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.ts@@ -1,3 +1,4 @@+import { $createLinkNode } from '@lexical/link';import {$createTableNode,$createTableRowNode,@@ -11,7 +12,7 @@ import {$createVideoNode,$isVideoNode,} from '@macro-inc/lexical-core/nodes/VideoNode';-import type { LexicalEditor } from 'lexical';+import { $createTextNode, $isElementNode, type LexicalEditor } from 'lexical';import { describe, expect, it } from 'vitest';import {$createTextCell,@@ -26,6 +27,44 @@ function createTableEditor(): LexicalEditor {}describe('table cell allowed content', () => {+ it('preserves stray text and inline links in paragraphs, in order', async () => {+ const editor = createTableEditor();+ await buildTable(editor, textGrid([['hello']]));+ let textKey = '';+ let linkKey = '';+ editor.update(+ () => {+ const text = $createTextNode('stray text');+ const link = $createLinkNode('https://example.com');+ link.append($createTextNode('linked text'));+ textKey = text.getKey();+ linkKey = link.getKey();+ $getCell(0, 0).append(text, link);+ },+ { discrete: true }+ );++ editor.read(() => {+ const children = $getCell(0, 0).getChildren();+ expect(children.map((child) => child.getType())).toEqual([+ 'paragraph',+ 'paragraph',+ 'paragraph',+ ]);+ expect(children.map((child) => child.getTextContent())).toEqual([+ 'hello',+ 'stray text',+ 'linked text',+ ]);+ expect(+ $isElementNode(children[1]) && children[1].getFirstChild()?.getKey()+ ).toBe(textKey);+ expect(+ $isElementNode(children[2]) && children[2].getFirstChild()?.getKey()+ ).toBe(linkKey);+ });+ });+it('keeps an image appended to a table cell', async () => {const editor = createTableEditor();await buildTable(editor, textGrid([['hello']]));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 7c905b8c29..5da815022a 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@@ -6,7 +6,7 @@ import {TableCellNode,} from '@lexical/table';import { mergeRegister } from '@lexical/utils';-import type { LexicalEditor } from 'lexical';+import { $createParagraphNode, type LexicalEditor } from 'lexical';import { registerTableListTab } from './tableListTab';import { registerTableSelectAll } from './tableSelectAll';import { registerTableTabInsertRow } from './tableTabInsertRow';@@ -68,7 +68,7 @@ function _registerTablePlugin(editor: LexicalEditor, props: TablePluginProps) {})(),// Restrict table cells to block content that renders sanely inside them- // (notably: no nested tables).+ // (notably: no nested tables). Preserve stray text/inlines in a paragraph.(() => {const allowedNodesInTableCellNode = ['paragraph',@@ -86,7 +86,13 @@ function _registerTablePlugin(editor: LexicalEditor, props: TablePluginProps) {for (let i = 0; i < children.length; i++) {const child = children[i];if (!allowedNodesInTableCellNode.includes(child.__type)) {- child.remove();+ if (child.isInline()) {+ const paragraph = $createParagraphNode();+ child.replace(paragraph);+ paragraph.append(child);+ } else {+ child.remove();+ }}}});diff --git a/docs/AGENT_GUIDE/documents.md b/docs/AGENT_GUIDE/documents.mdindex d811461cea..065164d8dc 100644--- a/docs/AGENT_GUIDE/documents.md+++ b/docs/AGENT_GUIDE/documents.md@@ -312,6 +312,11 @@ nodes — use the snapshot itself to verify content. For formatting checks, runBody placeholder advertises: `/` for block commands, `@` to reference files, `;` for snippets.Markdown auto-format works while typing (`#` heading, `[]` checklist, `>` quote).+AI text-writing operations require a paragraph/list-item or text-run ID. A+table, row, cell, or list-container ID is rejected with guidance to choose a+content block or use `setCell`. Existing stray inline content directly inside+table cells is preserved in paragraphs when the editor opens the document.+`@` opens the mention menu wherever the caret starts a word, including directlyin front of existing text — the menu opens empty there instead of searching forthe word ahead of the caret. Typed inside a word (`he@llo`) it stays literal text.diff --git a/services/ai-editing-worker/src/ai-editing/doc/cell-id-shape.test.ts b/services/ai-editing-worker/src/ai-editing/doc/cell-id-shape.test.tsnew file mode 100644index 0000000000..00516a7c39--- /dev/null+++ b/services/ai-editing-worker/src/ai-editing/doc/cell-id-shape.test.ts@@ -0,0 +1,113 @@+import { $getId } from '@macro-inc/lexical-core/plugins/nodeIdPlugin';+import { $getRoot, $isElementNode } from 'lexical';+import { describe, expect, it, vi } from 'vitest';+import { read, setup } from '../ai-toolkit/_test-helpers';+import { type DocumentOp, EditError } from '../editor';+import { Doc } from './doc';++const textWrites: DocumentOp[] = [+ { kind: 'setText', node: '', text: 'X' },+ { kind: 'appendText', node: '', text: 'X' },+ { kind: 'prependText', node: '', text: 'X' },+ { kind: 'insertText', node: '', at: 0, text: 'X' },+ {+ kind: 'insertInline',+ node: '',+ at: 0,+ ref: 'break',+ spec: { inline: 'linebreak' },+ },+ { kind: 'setBlockType', node: '', block: 'heading', level: 2 },+];++function tableFixture(text = 'original') {+ const { session, ids } = setup('intro\n\n- one\n- two');+ const propagate = vi.fn();+ const doc = new Doc(session, propagate);+ doc.apply({+ kind: 'insertNode',+ ref: 'table',+ spec: { block: 'table', rows: [[text]] },+ at: { after: ids[0]! },+ });+ const targets = read(session, () => {+ const table = $getRoot()+ .getChildren()+ .find((n) => n.getType() === 'table');+ if (!$isElementNode(table)) throw new Error('missing table');+ const row = table.getFirstChild();+ if (!$isElementNode(row)) throw new Error('missing row');+ const cell = row.getFirstChild();+ if (!$isElementNode(cell)) throw new Error('missing cell');+ const paragraph = cell.getFirstChild()!;+ const list = $getRoot()+ .getChildren()+ .find((n) => n.getType() === 'list');+ if (!$isElementNode(list)) throw new Error('missing list');+ return {+ table: 'table',+ row: $getId(row)!,+ cell: $getId(cell)!,+ list: $getId(list)!,+ paragraph: $getId(paragraph)!,+ item: $getId(list.getFirstChild()!)!,+ };+ });+ propagate.mockClear();+ return { session, doc, targets, propagate, intro: ids[0]! };+}++describe('text writers reject container ids before changing content', () => {+ for (const target of ['table', 'row', 'cell', 'list'] as const) {+ for (const op of textWrites) {+ it(`${op.kind} rejects a ${target} id, including when empty`, () => {+ for (const text of ['original', '']) {+ const { session, doc, targets, propagate } = tableFixture(text);+ const before = session.editor.getEditorState().toJSON();+ const apply = () =>+ doc.apply({ ...op, node: targets[target] } as DocumentOp);+ expect(apply).toThrow(EditError);+ expect(apply).toThrow('target a paragraph or list item id');+ expect(session.editor.getEditorState().toJSON()).toEqual(before);+ expect(propagate).not.toHaveBeenCalled();+ }+ });+ }+ }++ for (const target of ['paragraph', 'item'] as const) {+ for (const op of textWrites) {+ it(`${op.kind} still accepts a ${target} id`, () => {+ const { doc, targets, propagate } = tableFixture();+ expect(() =>+ doc.apply({ ...op, node: targets[target] } as DocumentOp)+ ).not.toThrow();+ expect(propagate).toHaveBeenCalledOnce();+ });+ }+ }++ it('keeps setCell, container search/replace, and table moves working', () => {+ const { session, doc, targets, intro } = tableFixture();+ doc.apply({+ kind: 'setCell',+ table: targets.table,+ row: 0,+ col: 0,+ text: 'updated',+ });+ expect(doc.locate(targets.cell, 'updated')).toHaveLength(1);+ doc.apply({+ kind: 'replaceText',+ node: targets.table,+ find: 'updated',+ to: 'replaced',+ scope: { kind: 'all' },+ });+ doc.apply({ kind: 'moveNode', node: targets.table, at: { before: intro } });+ read(session, () => {+ expect($getRoot().getFirstChild()?.getType()).toBe('table');+ expect($getRoot().getTextContent()).toContain('replaced');+ });+ });+});diff --git a/services/ai-editing-worker/src/ai-editing/doc/doc.ts b/services/ai-editing-worker/src/ai-editing/doc/doc.tsindex c8047aca3a..844fa359b9 100644--- a/services/ai-editing-worker/src/ai-editing/doc/doc.ts+++ b/services/ai-editing-worker/src/ai-editing/doc/doc.ts@@ -237,6 +237,24 @@ export class Doc implements DocReader, DocWriter {return locate.$blockById(this.session, node);}+ /** Text writers need a content block, not a table/list container. Reject+ * ambiguous container ids instead of choosing one of their descendants. */+ private textBlock(id: NodeRef): ElementNode {+ const block = this.block(id);+ if (+ $isTableNode(block) ||+ $isTableRowNode(block) ||+ $isTableCellNode(block) ||+ $isListNode(block) ||+ block.getType() === 'root'+ ) {+ throw new EditError(+ `id "${id}" is a ${block.getType()} container — target a paragraph or list item id; use setCell to replace a table cell.`+ );+ }+ return block;+ }+/** Resolve a table by id, rejecting non-table ids. `setCell`/cell resolution* otherwise walk up to the enclosing table, so a <tr>/<td> id "works" but* reindexes row/col against the table — silently clobbering the wrong cell@@ -293,7 +311,7 @@ export class Doc implements DocReader, DocWriter {const c = target.getTextContent();target.setTextContent(c.slice(0, at) + text + c.slice(at));} else {- insertTextAt(this.block(node), at, text);+ insertTextAt(this.textBlock(node), at, text);}});}@@ -332,16 +350,16 @@ export class Doc implements DocReader, DocWriter {else target.setTextContent(text);return;}- blocks.$setText(this.block(node), text);+ blocks.$setText(this.textBlock(node), text);});}private appendText(node: NodeRef, text: string): void {- this.tx(() => inline.$appendText(this.block(node), text));+ this.tx(() => inline.$appendText(this.textBlock(node), text));}private prependText(node: NodeRef, text: string): void {- this.tx(() => inline.$prependText(this.block(node), text));+ this.tx(() => inline.$prependText(this.textBlock(node), text));}private insertTextAfterInline(@@ -489,12 +507,13 @@ export class Doc implements DocReader, DocWriter {: block === 'code'? { type: 'code', language: opts.language }: { type: block };- this.tx(() =>+ this.tx(() => {+ this.textBlock(node);modify.$modifyNode(this.session, node, {op: 'blockType',block: data,- })- );+ });+ });}private setListType(nodes: NodeRef[], list: ListKind): void {@@ -586,7 +605,7 @@ export class Doc implements DocReader, DocWriter {spec: NodeSpec): void {this.tx(() => {- const block = this.block(node);+ const block = this.textBlock(node);const inline = buildNode(spec);insertInlineAt(block, at, inline);this.assignRef(ref, inline);