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(markdown): keep node ids when undoing a list-to-table conversion (#8984)
Wolf Mermelstein215df290c876
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.tsindex fceb41e528..a9a8c15fad 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/reconcile.ts@@ -1,3 +1,4 @@+import { $isListItemNode } from '@lexical/list';import {$getNodeById,$getPeerId,@@ -54,25 +55,31 @@ function getComparableNodeValue(url: node.srcType === 'dss' ? undefined : node.url,};}- return {- ...node,- children: undefined,- };+ const value: Record<string, any> = { ...node, children: undefined };+ // A list item's indent is its nesting depth, which the tree already+ // carries; applying it would re-nest the item (ListItemNode.setIndent).+ if (node.type === 'listitem') value.indent = undefined;+ return value;}-// Map of a nodeId -> parent nodeId. If parent is undefined, it means the node is the root node-type ParentMap = Map<string, string | undefined>;+/** What one reconcile pass reads while it walks the new tree. */+type Reconcile = {+ editor: LexicalEditor;+ mapping: NodeIdMappings;+ /** The old serialized node for each id, wherever it sat in the old tree. */+ oldNodes: Map<string, SerializedLexicalNode>;+ peerId: () => string;+};-function buildParentMap(+function indexNodesById(node: SerializedLexicalNode,- parentId: string | undefined,- out: ParentMap+ out: Map<string, SerializedLexicalNode>) {const id = getNodeId(node);- if (id) out.set(id, parentId);+ if (id) out.set(id, node);if (hasChildren(node)) {for (const child of node.children) {- buildParentMap(child, id, out);+ indexNodesById(child, out);}}}@@ -91,8 +98,6 @@ export function $reconcileLexicalState(mapping: NodeIdMappings,peerId: () => string): void {- const editor = $getEditor();-// The local draft comment mark is never synced, so it is lifted out while// the tree is compared with the shared state. Drafts that arrive in the// shared state were left behind by sessions that ended mid-draft.@@ -100,20 +105,15 @@ export function $reconcileLexicalState(const oldRoot = stripDraftCommentMarks(oldState).root;const newRoot = stripDraftCommentMarks(newState).root;- const newParentMap = new Map<string, string | undefined>();- const oldParentMap = new Map<string, string | undefined>();- buildParentMap(newRoot, undefined, newParentMap);- buildParentMap(oldRoot, undefined, oldParentMap);-- $applyChildren(- editor,+ const reconcile: Reconcile = {+ editor: $getEditor(),mapping,- oldRoot,- newRoot,- oldParentMap,- newParentMap,- peerId- );+ oldNodes: new Map(),+ peerId,+ };+ indexNodesById(oldRoot, reconcile.oldNodes);++ $applyChildren(reconcile, oldRoot, newRoot);$restoreDraftCommentMarks(drafts);}@@ -128,13 +128,15 @@ function $applyLocalStatus(node: LexicalNode, peerId: () => string) {// Apply state changes to a specific nodefunction $applyNodeState(- editor: LexicalEditor,- mapping: NodeIdMappings,+ reconcile: Reconcile,nodeId: string,- state: Partial<SerializedLexicalNode>,- peerId: () => string+ state: ReturnType<typeof getComparableNodeValue>): void {- const node = $getNodeById(editor, mapping.idToNodeKeyMap, nodeId);+ const node = $getNodeById(+ reconcile.editor,+ reconcile.mapping.idToNodeKeyMap,+ nodeId+ );if (!node) {console.error('Node not found for ID:', nodeId);return;@@ -142,63 +144,120 @@ function $applyNodeState(try {// Update the node with new state and mark it as dirty to trigger re-render- const newNode = node.updateFromJSON(state);- $applyLocalStatus(newNode, peerId);+ const newNode = node.updateFromJSON(+ $isListItemNode(node) ? { ...state, indent: node.getIndent() } : state+ );+ $applyLocalStatus(newNode, reconcile.peerId);newNode.markDirty();} catch (error) {console.error(`Failed to update node ${nodeId}:`, error);}}-function moveNodeIfNeeded(- parent: ElementNode,- node: LexicalNode,- targetIndex: number+/** The live node holding this id, if it has the serialized node's type. */+function $getExistingNode(+ reconcile: Reconcile,+ id: string | undefined,+ type: string+): LexicalNode | null {+ if (!id) return null;+ const node = $getNodeById(+ reconcile.editor,+ reconcile.mapping.idToNodeKeyMap,+ id+ );+ return node?.getType() === type ? node : null;+}++/** Brings an existing node's own state and children up to its new serialized form. */+function $updateExistingNode(+ reconcile: Reconcile,+ id: string,+ newNode: SerializedLexicalNode): void {- const currentIndex = parent.getChildren().indexOf(node);- if (currentIndex !== -1 && currentIndex !== targetIndex) {- parent.splice(currentIndex, 1, []);- parent.splice(targetIndex, 0, [node]);+ const oldNode = reconcile.oldNodes.get(id);+ if (!oldNode) return;+ const newValue = getComparableNodeValue(newNode);+ if (!deepEqual(getComparableNodeValue(oldNode), newValue)) {+ $applyNodeState(reconcile, id, newValue);+ }+ if (hasChildren(oldNode) && hasChildren(newNode)) {+ $applyChildren(reconcile, oldNode, newNode);}}-function moveNodeBetweenParents(- editor: LexicalEditor,- mapping: NodeIdMappings,- parent: ElementNode,- node: LexicalNode,- oldParentId: string,- targetIndex: number-) {- const oldParent = oldParentId- ? $getNodeById(editor, mapping.idToNodeKeyMap, oldParentId)- : null;- if (oldParent && $isElementNode(oldParent)) {- const oldIndex = oldParent.getChildren().indexOf(node);- if (oldIndex !== -1) {- oldParent.splice(oldIndex, 1, []);+/**+ * Creates a node that is new to the editor. Descendants that already exist+ * elsewhere (a move into a new parent, such as list items a list-to-table+ * undo puts back into a recreated list) are moved in rather than parsed again,+ * so they keep their ids and stay mapped to the shared document.+ */+function $createNode(+ reconcile: Reconcile,+ serialized: SerializedLexicalNode+): LexicalNode {+ if (!hasChildren(serialized)) return $parseSerializedNode(serialized);+ const shell: NodeWithChildren = { ...serialized, children: [] };+ const node = $parseSerializedNode(shell);+ if (!$isElementNode(node)) return $parseSerializedNode(serialized);+ for (const child of serialized.children) {+ const id = getNodeId(child);+ const existing = $getExistingNode(reconcile, id, child.type);+ if (!existing || !id) {+ node.append($createNode(reconcile, child));+ continue;}+ node.append(existing);+ $updateExistingNode(reconcile, id, child);}+ return node;+}- parent.splice(targetIndex, 0, [node]);+/**+ * Removes the children of `parent` that the new state no longer lists there.+ * Nodes with ids are found by id: one may already have moved to a new parent.+ */+function $removeDroppedChildren(+ reconcile: Reconcile,+ parent: ElementNode,+ oldChildren: SerializedLexicalNode[],+ newChildren: SerializedLexicalNode[]+): void {+ const newIds = new Set<string>();+ for (const child of newChildren) {+ const id = getNodeId(child);+ if (id) newIds.add(id);+ }+ for (let i = oldChildren.length - 1; i >= 0; i--) {+ const id = getNodeId(oldChildren[i]);+ if (id && newIds.has(id)) continue;+ if (!id) {+ if (i < parent.getChildrenSize()) parent.splice(i, 1, []);+ continue;+ }+ const node = $getNodeById(+ reconcile.editor,+ reconcile.mapping.idToNodeKeyMap,+ id+ );+ if (node?.getParent()?.is(parent)) {+ parent.splice(node.getIndexWithinParent(), 1, []);+ }+ }}/** Diff and apply changes between two sets of nodes*- * @param editor - Lexical editor instance- * @param mapping - LoroNodeMapping instance+ * @param reconcile - The pass's editor, mappings and indexes* @param oldNode - Old node to diff against* @param newNode - New node to diff against*/function $applyChildren(- editor: LexicalEditor,- mapping: NodeIdMappings,+ reconcile: Reconcile,oldNode: NodeWithChildren,- newNode: NodeWithChildren,- oldParentMap: ParentMap,- newParentMap: ParentMap,- peerId: () => string+ newNode: NodeWithChildren): void {+ const { editor, mapping } = reconcile;const oldChildren = oldNode.children;const newChildren = newNode.children;const parentId = getNodeId(oldNode as any);@@ -213,87 +272,26 @@ function $applyChildren(return;}- const oldIdToIndex = new Map<string, number>();- const newIdToIndex = new Map<string, number>();-- for (let i = 0; i < oldChildren.length; i++) {- const id = getNodeId(oldChildren[i]);- if (id) oldIdToIndex.set(id, i);- }- for (let i = 0; i < newChildren.length; i++) {- const id = getNodeId(newChildren[i]);- if (id) newIdToIndex.set(id, i);- }-- // Remove nodes that arent present in the newChildren array first- for (let i = oldChildren.length - 1; i >= 0; i--) {- const id = getNodeId(oldChildren[i]);- if (!id || !newIdToIndex.has(id)) {- const oldSize = parent.getChildrenSize();- if (i < oldSize) parent.splice(i, 1, []);- }- }+ $removeDroppedChildren(reconcile, parent, oldChildren, newChildren);for (let newIndex = 0; newIndex < newChildren.length; newIndex++) {const newChild = newChildren[newIndex];const id = getNodeId(newChild);+ const node = id ? $getNodeById(editor, mapping.idToNodeKeyMap, id) : null;- let node = id ? $getNodeById(editor, mapping.idToNodeKeyMap, id) : null;-- // Node already exists, check for move / update- if (node && id) {- const oldParentId = oldParentMap.get(id);- const newParentId = newParentMap.get(id);-- // Handle cross-parent move- if (oldParentId && newParentId && oldParentId !== newParentId) {- moveNodeBetweenParents(- editor,- mapping,- parent,- node,- oldParentId,- newIndex- );- continue;- }-- let nodeIndex = parent.getChildren().indexOf(node);-- if (nodeIndex !== -1 && nodeIndex !== newIndex) {- moveNodeIfNeeded(parent, node, newIndex);- }-- // Node exists in old and new children- const oldIdx = oldIdToIndex.get(id!);- if (oldIdx !== undefined) {- const oldChildValue = getComparableNodeValue(oldChildren[oldIdx]);- const newChildValue = getComparableNodeValue(newChild);- // Node has a value change- if (!deepEqual(oldChildValue, newChildValue)) {- $applyNodeState(editor, mapping, id!, newChildValue, peerId);- }- }+ if (!node || !id) {+ parent.splice(newIndex, 0, [$createNode(reconcile, newChild)]);+ continue;+ }- // Recurse all children if they exist- if (hasChildren(newChild)) {- const oldIdx = oldIdToIndex.get(id!);- if (oldIdx !== undefined && hasChildren(oldChildren[oldIdx])) {- $applyChildren(- editor,- mapping,- oldChildren[oldIdx] as NodeWithChildren,- newChild as NodeWithChildren,- oldParentMap,- newParentMap,- peerId- );- }- }- } else {- // Insert new node- const newNodeInstance = $parseSerializedNode(newChild);- parent.splice(newIndex, 0, [newNodeInstance]);+ if (!node.getParent()?.is(parent)) {+ // Moved here from another parent: splice detaches it from that one.+ parent.splice(newIndex, 0, [node]);+ } else if (node.getIndexWithinParent() !== newIndex) {+ parent.splice(node.getIndexWithinParent(), 1, []);+ parent.splice(newIndex, 0, [node]);}++ $updateExistingNode(reconcile, id, newChild);}}diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/listToTableUndo.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/listToTableUndo.test.tsnew file mode 100644index 0000000000..d720ac401a--- /dev/null+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/tables/listToTableUndo.test.ts@@ -0,0 +1,161 @@+import { FROM_LORO_TAG } from '@core/component/LexicalMarkdown/collaboration/CollabProvider';+import { $reconcileLexicalState } from '@core/component/LexicalMarkdown/collaboration/reconcile';+import { registerLoroHistory } from '@core/component/LexicalMarkdown/collaboration/undo';+import { loroSyncState } from '@core/component/LexicalMarkdown/utils';+import { $isListNode, registerList } from '@lexical/list';+import { $convertFromMarkdownString } from '@lexical/markdown';+import { registerRichText } from '@lexical/rich-text';+import { $isTableNode } from '@lexical/table';+import { SyncDirection } from '@loro-mirror/core';+import {+ createLoroManager,+ type LoroManager,+} from '@macro-inc/collaboration/collab/manager';+import {+ $updateAllNodeIds,+ ALL_TRANSFORMERS,+ COLLABORATION_TAG,+ type NodeIdMappings,+ nodeIdPlugin,+} from '@macro-inc/lexical-core';+import { MARKDOWN_LORO_SCHEMA } from '@macro-inc/lexical-core/markdown-loro-schema';+import {+ NodeReplacements,+ SupportedNodeTypes,+} from '@macro-inc/lexical-core/node-list';+import {+ $getRoot,+ CLEAR_HISTORY_COMMAND,+ createEditor,+ type SerializedEditorState,+ UNDO_COMMAND,+} from 'lexical';+import { LoroDoc } from 'loro-crdt';+import { createRoot } from 'solid-js';+import { describe, expect, it, vi } from 'vitest';+import { trailingParagraphPlugin } from '../trailing-paragraph/trailingParagraphPlugin';+import {+ LIST_TO_TABLE_COMMAND,+ registerListToTableCommand,+} from './listToTable';+import { tablePlugin } from './tablePlugin';++// The editor utilities pull in the plugin barrel, whose leaves open the+// storage and connection-gateway sockets on import.+vi.mock('@service-storage/websocket', () => ({+ storageWS: { reconnectIfDisconnected: vi.fn() },+ createWebSocketJob: vi.fn(),+}));+vi.mock('@service-connection/websocket', () => ({+ ws: { addEventListener: vi.fn(), send: vi.fn() },+ state: () => 'closed',+ createConnectionBlockWebsocketEffect: vi.fn(),+ createConnectionWebsocketEffect: vi.fn(),+}));++describe('list to table conversion in a collaborative document', () => {+ it('undoes back to the nested list, with the ids the shared document has', async () => {+ const editor = createEditor({+ namespace: 'list-to-table-undo',+ nodes: [...SupportedNodeTypes, ...NodeReplacements],+ onError: (error) => {+ throw error;+ },+ });+ const mappings: NodeIdMappings = {+ idToNodeKeyMap: new Map(),+ nodeKeyToIdMap: new Map(),+ };+ editor.setRootElement(document.createElement('div'));+ registerRichText(editor);+ registerList(editor);+ tablePlugin({+ hasCellMerge: true,+ hasCellBackgroundColor: true,+ hasTabHandler: true,+ })(editor);+ registerListToTableCommand(editor);+ trailingParagraphPlugin()(editor);+ nodeIdPlugin({ nodes: SupportedNodeTypes, mappings })(editor);++ let manager!: LoroManager<typeof MARKDOWN_LORO_SCHEMA>;+ createRoot(() => {+ manager = createLoroManager(MARKDOWN_LORO_SCHEMA, { documentId: 'doc' });+ });+ await manager.initializeFromSnapshot(+ new LoroDoc().export({ mode: 'snapshot' })+ );++ // Wired as CollabProvider wires them: local updates sync to Loro, and+ // Loro changes (here, the undo) are reconciled onto the editor by node id.+ const syncs: Promise<unknown>[] = [];+ editor.registerUpdateListener(({ editorState, tags, mutatedNodes }) => {+ if (tags.has(FROM_LORO_TAG) || !mutatedNodes?.size) return;+ syncs.push(manager.syncToLoro(loroSyncState(editorState) as never));+ });+ manager.onStateChange((update) => {+ if (update.metadata.direction === SyncDirection.TO_LORO) return;+ editor.update(+ () => {+ $reconcileLexicalState(+ editor.getEditorState().toJSON(),+ update.state as unknown as SerializedEditorState,+ mappings,+ () => manager.peerIdStr+ );+ },+ { discrete: true, tag: [FROM_LORO_TAG, COLLABORATION_TAG] }+ );+ queueMicrotask(() => editor.update(() => $updateAllNodeIds(mappings)));+ });+ registerLoroHistory(editor, manager, 300);++ editor.update(+ () => {+ $convertFromMarkdownString(+ [+ 'intro',+ '',+ '- column 1',+ ' - a1',+ ' - deeper',+ ' - a2',+ '- column 2',+ ' - b1',+ '',+ 'outro',+ ].join('\n'),+ ALL_TRANSFORMERS+ );+ $updateAllNodeIds(mappings);+ },+ { discrete: true }+ );+ await Promise.all(syncs);+ editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);+ const before = editor.getEditorState().toJSON();++ const listKey = editor.read(() => {+ const list = $getRoot().getChildAtIndex(1);+ if (!$isListNode(list)) throw new Error('expected the list');+ return list.getKey();+ });+ let converted = false;+ editor.update(+ () => {+ converted = editor.dispatchCommand(LIST_TO_TABLE_COMMAND, listKey);+ },+ { discrete: true }+ );+ expect(converted).toBe(true);+ await Promise.all(syncs);+ expect(editor.read(() => $isTableNode($getRoot().getChildAtIndex(1)))).toBe(+ true+ );++ editor.dispatchCommand(UNDO_COMMAND, undefined);+ await Promise.all(syncs);++ expect(editor.getEditorState().toJSON()).toEqual(before);+ });+});diff --git a/packages/lexical-core/plugins/nodeIdPlugin.ts b/packages/lexical-core/plugins/nodeIdPlugin.tsindex 55905c2c19..1c5a535afb 100644--- a/packages/lexical-core/plugins/nodeIdPlugin.ts+++ b/packages/lexical-core/plugins/nodeIdPlugin.ts@@ -148,7 +148,6 @@ function registerNodeIdPlugin(editor: LexicalEditor, props: NodeIdProps) {idToNodeKeyMap.delete(id);nodeKeyToIdMap.delete(node!.getKey());}- return;}}});diff --git a/packages/lexical-core/tests/node-id-plugin.test.ts b/packages/lexical-core/tests/node-id-plugin.test.tsnew file mode 100644index 0000000000..e4483d3eb5--- /dev/null+++ b/packages/lexical-core/tests/node-id-plugin.test.ts@@ -0,0 +1,58 @@+import {+ $createParagraphNode,+ $createTextNode,+ $getRoot,+ createEditor,+} from 'lexical';+import { describe, expect, it } from 'vitest';+import { SupportedNodeTypes } from '../node-list';+import {+ $getId,+ type NodeIdMappings,+ nodeIdPlugin,+} from '../plugins/nodeIdPlugin';++describe('nodeIdPlugin', () => {+ it('unmaps every node destroyed in one update', () => {+ const editor = createEditor({+ nodes: SupportedNodeTypes,+ onError: (error) => {+ throw error;+ },+ });+ editor.setRootElement(document.createElement('div'));+ const mappings: NodeIdMappings = {+ idToNodeKeyMap: new Map(),+ nodeKeyToIdMap: new Map(),+ };+ nodeIdPlugin({ nodes: SupportedNodeTypes, mappings })(editor);++ editor.update(+ () => {+ $getRoot().append(+ $createParagraphNode().append($createTextNode('one')),+ $createParagraphNode().append($createTextNode('two')),+ $createParagraphNode().append($createTextNode('three'))+ );+ },+ { discrete: true }+ );+ const ids = editor.read(() =>+ $getRoot()+ .getChildren()+ .flatMap((paragraph) => [paragraph, ...paragraph.getChildren()])+ .map((node) => $getId(node))+ );+ expect(ids).toHaveLength(6);+ for (const id of ids) expect(mappings.idToNodeKeyMap.has(id!)).toBe(true);++ editor.update(+ () => {+ $getRoot().clear().append($createParagraphNode());+ },+ { discrete: true }+ );++ for (const id of ids) expect(mappings.idToNodeKeyMap.has(id!)).toBe(false);+ });+});