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(md): fix mention->card node split, fix list number merge (#4458)
Seamus4973d4f20d92
diff --git a/js/lexical-core/nodes/DocumentCardNode.ts b/js/lexical-core/nodes/DocumentCardNode.tsindex 8a397d6001..5dc6a37c4f 100644--- a/js/lexical-core/nodes/DocumentCardNode.ts+++ b/js/lexical-core/nodes/DocumentCardNode.ts@@ -1,7 +1,9 @@import {$applyNodeReplacement,$createParagraphNode,+ $isElementNode,$isRootOrShadowRoot,+ $splitNode,type DOMConversionMap,type EditorConfig,type EditorThemeClasses,@@ -366,6 +368,29 @@ export function $isDocumentCardNode(return node instanceof DocumentCardNode;}+function $removeEmptyElementChain(+ node: ElementNode | null,+ stopNode?: ElementNode | null+): void {+ let current: ElementNode | null = node;++ while (current && !$isRootOrShadowRoot(current)) {+ const parent = current.getParent();++ if (current.getChildrenSize() > 0) {+ break;+ }++ current.remove();++ if (current === stopNode) {+ break;+ }++ current = $isElementNode(parent) ? parent : null;+ }+}+/*** Convert a DocumentMentionNode (inline) to a DocumentCardNode (block)* The mention node will be replaced with a card node, properly handling the block-level insertion@@ -397,36 +422,22 @@ export function $convertMentionToCard(return cardNode;}- let topLevelBlock: LexicalNode = mentionNode;- let currentParent: ElementNode | null = parent;-- while (currentParent && !$isRootOrShadowRoot(currentParent)) {- topLevelBlock = currentParent;- currentParent = currentParent.getParent();- }-- const siblings = parent.getChildren();- const mentionIndex = siblings.indexOf(mentionNode);-- if (siblings.length === 1) {- topLevelBlock.replace(cardNode);- } else {- const nodesAfterMention = siblings.slice(mentionIndex + 1);-- mentionNode.remove();-- topLevelBlock.insertAfter(cardNode);+ const mentionIndex = mentionNode.getIndexWithinParent();+ const [blockBeforeMention, blockAfterMention] = $splitNode(+ parent,+ mentionIndex+ );+ const parentBeforeMention = parent;+ const parentAfterMention = mentionNode.getParent();- if (nodesAfterMention.length > 0) {- const newParagraph = $createParagraphNode();- cardNode.insertAfter(newParagraph);+ blockAfterMention.insertBefore(cardNode);+ mentionNode.remove();- nodesAfterMention.forEach((node) => {- node.remove();- newParagraph.append(node);- });- }- }+ $removeEmptyElementChain(parentBeforeMention, blockBeforeMention);+ $removeEmptyElementChain(+ $isElementNode(parentAfterMention) ? parentAfterMention : null,+ blockAfterMention+ );return cardNode;}@@ -446,7 +457,7 @@ export function $convertCardToMention(mentionUuid: cardNode.getMentionUuid(),});- let targetParagraph = $createParagraphNode();+ const targetParagraph = $createParagraphNode();targetParagraph.append(mentionNode);cardNode.replace(targetParagraph);return mentionNode;diff --git a/js/lexical-core/tests/document-card-conversion.test.ts b/js/lexical-core/tests/document-card-conversion.test.tsnew file mode 100644index 0000000000..22a0b9f2d0--- /dev/null+++ b/js/lexical-core/tests/document-card-conversion.test.ts@@ -0,0 +1,166 @@+import { $createLinkNode } from '@lexical/link';+import {+ $createListItemNode,+ $createListNode,+ $isListNode,+} from '@lexical/list';+import {+ $createParagraphNode,+ $createTextNode,+ $getRoot,+ createEditor,+ type LexicalEditor,+} from 'lexical';+import { describe, expect, it } from 'vitest';+import { SupportedNodeTypes } from '../node-list';+import {+ $convertMentionToCard,+ $isDocumentCardNode,+} from '../nodes/DocumentCardNode';+import { $createDocumentMentionNode } from '../nodes/DocumentMentionNode';++function makeEditor() {+ return createEditor({+ nodes: SupportedNodeTypes,+ onError: console.error,+ });+}++function update(editor: LexicalEditor, fn: () => void): Promise<void> {+ return new Promise((resolve) => {+ editor.update(fn, { onUpdate: () => resolve() });+ });+}++function createMention() {+ return $createDocumentMentionNode({+ documentId: 'doc-1',+ documentName: 'Doc',+ blockName: 'md',+ });+}++describe('$convertMentionToCard', () => {+ it('splits a paragraph around a mention nested in an inline parent', async () => {+ const editor = makeEditor();++ await update(editor, () => {+ const root = $getRoot();+ const paragraph = $createParagraphNode();+ const link = $createLinkNode('https://example.com');+ const mention = createMention();++ root.clear();+ link.append(mention);+ paragraph.append(+ $createTextNode('before '),+ link,+ $createTextNode(' after')+ );+ root.append(paragraph);++ $convertMentionToCard(mention);+ });++ editor.getEditorState().read(() => {+ const children = $getRoot().getChildren();++ expect(children).toHaveLength(3);+ expect(children[0].getTextContent()).toBe('before ');+ expect($isDocumentCardNode(children[1])).toBe(true);+ expect(children[2].getTextContent()).toBe(' after');+ });+ });++ it('splits a list around a mention-only list item', async () => {+ const editor = makeEditor();++ await update(editor, () => {+ const root = $getRoot();+ const list = $createListNode('bullet');+ const beforeItem = $createListItemNode();+ const mentionItem = $createListItemNode();+ const afterItem = $createListItemNode();+ const mention = createMention();++ root.clear();+ beforeItem.append($createTextNode('before'));+ mentionItem.append(mention);+ afterItem.append($createTextNode('after'));+ list.append(beforeItem, mentionItem, afterItem);+ root.append(list);++ $convertMentionToCard(mention);+ });++ editor.getEditorState().read(() => {+ const children = $getRoot().getChildren();+ const beforeList = children[0];+ const afterList = children[2];++ expect(children).toHaveLength(3);+ expect($isListNode(beforeList)).toBe(true);+ expect($isDocumentCardNode(children[1])).toBe(true);+ expect($isListNode(afterList)).toBe(true);++ if (!$isListNode(beforeList) || !$isListNode(afterList)) {+ throw new Error('Expected lists around converted card');+ }++ expect(+ beforeList.getChildren().map((item) => item.getTextContent())+ ).toEqual(['before']);+ expect(+ afterList.getChildren().map((item) => item.getTextContent())+ ).toEqual(['after']);+ });+ });++ it('keeps list-item text before and after the converted mention in lists', async () => {+ const editor = makeEditor();++ await update(editor, () => {+ const root = $getRoot();+ const list = $createListNode('bullet');+ const beforeItem = $createListItemNode();+ const mentionItem = $createListItemNode();+ const afterItem = $createListItemNode();+ const mention = createMention();++ root.clear();+ beforeItem.append($createTextNode('before'));+ mentionItem.append(+ $createTextNode('item before '),+ mention,+ $createTextNode(' item after')+ );+ afterItem.append($createTextNode('after'));+ list.append(beforeItem, mentionItem, afterItem);+ root.append(list);++ $convertMentionToCard(mention);+ });++ editor.getEditorState().read(() => {+ const children = $getRoot().getChildren();+ const beforeList = children[0];+ const afterList = children[2];++ expect(children).toHaveLength(3);+ expect($isListNode(beforeList)).toBe(true);+ expect($isDocumentCardNode(children[1])).toBe(true);+ expect($isListNode(afterList)).toBe(true);++ if (!$isListNode(beforeList) || !$isListNode(afterList)) {+ throw new Error('Expected lists around converted card');+ }++ expect(+ beforeList.getChildren().map((item) => item.getTextContent())+ ).toEqual(['before', 'item before ']);+ expect(+ afterList.getChildren().map((item) => item.getTextContent())+ ).toEqual([' item after', 'after']);+ });+ });+});diff --git a/js/lexical-core/tests/list-transformers.test.ts b/js/lexical-core/tests/list-transformers.test.tsnew file mode 100644index 0000000000..e223bc219a--- /dev/null+++ b/js/lexical-core/tests/list-transformers.test.ts@@ -0,0 +1,89 @@+import {+ $createListItemNode,+ $createListNode,+ $isListNode,+} from '@lexical/list';+import { type ElementTransformer, ORDERED_LIST } from '@lexical/markdown';+import {+ $createParagraphNode,+ $createTextNode,+ $getRoot,+ createEditor,+ type LexicalEditor,+} from 'lexical';+import { describe, expect, it } from 'vitest';+import { SupportedNodeTypes } from '../node-list';+import { CUSTOM_TRANSFORMERS } from '../transformers/customTransformers';++function makeEditor() {+ return createEditor({+ nodes: SupportedNodeTypes,+ onError: console.error,+ });+}++function update(editor: LexicalEditor, fn: () => void): Promise<void> {+ return new Promise((resolve) => {+ editor.update(fn, { onUpdate: () => resolve() });+ });+}++function getCustomOrderedListTransformer(): ElementTransformer {+ const transformer = CUSTOM_TRANSFORMERS.find(+ (transformer): transformer is ElementTransformer =>+ transformer.type === 'element' &&+ transformer.regExp.source === ORDERED_LIST.regExp.source+ );++ if (!transformer) {+ throw new Error('Missing custom ordered list transformer');+ }++ return transformer;+}++describe('custom ordered-list transformer', () => {+ it('uses the incoming marker as the start when inserting before an ordered list', async () => {+ const editor = makeEditor();++ await update(editor, () => {+ const root = $getRoot();+ const paragraph = $createParagraphNode();+ const list = $createListNode('number', 3);+ const existingItem = $createListItemNode();+ const transformer = getCustomOrderedListTransformer();+ const match = '1. '.match(transformer.regExp);++ if (!match) {+ throw new Error('Expected ordered list marker to match');+ }++ root.clear();+ existingItem.append($createTextNode('Existing'));+ list.append(existingItem);+ root.append(paragraph, list);++ transformer.replace(+ paragraph,+ [$createTextNode('Incoming')],+ match,+ false+ );+ });++ editor.getEditorState().read(() => {+ const [list] = $getRoot().getChildren();++ expect($isListNode(list)).toBe(true);+ if (!$isListNode(list)) {+ throw new Error('Expected converted node to be an ordered list');+ }++ expect(list.getListType()).toBe('number');+ expect(list.getStart()).toBe(1);+ expect(+ list.getChildren().map((item) => item.getTextContent())+ ).toEqual(['Incoming', 'Existing']);+ });+ });+});diff --git a/js/lexical-core/transformers/customTransformers.ts b/js/lexical-core/transformers/customTransformers.tsindex 8a3a520c6a..048f1a2279 100644--- a/js/lexical-core/transformers/customTransformers.ts+++ b/js/lexical-core/transformers/customTransformers.ts@@ -6,6 +6,7 @@ import {type Transformer,UNORDERED_LIST,} from '@lexical/markdown';+import { $isListNode } from '@lexical/list';import { $isHeadingNode } from '@lexical/rich-text';import { $isElementNode, type ElementNode, type LexicalNode } from 'lexical';@@ -27,6 +28,18 @@ const customTransformer = () {return false;}+ if (original === ORDERED_LIST) {+ const nextNode = parentNode.getNextSibling();+ const start = Number(match[2]);++ if (+ Number.isFinite(start) &&+ $isListNode(nextNode) &&+ nextNode.getListType() === 'number'+ ) {+ nextNode.setStart(start);+ }+ }original.replace(parentNode, children, match, isImport);},};