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(core-md): fix mentions plugin callback inconsitencies and add tests (#2153)
Seamus31c10cce7628
diff --git a/js/app/packages/core/component/LexicalMarkdown/builder/BuilderTestPage.tsx b/js/app/packages/core/component/LexicalMarkdown/builder/BuilderTestPage.tsxindex 83352511db..fffb1b0e19 100644--- a/js/app/packages/core/component/LexicalMarkdown/builder/BuilderTestPage.tsx+++ b/js/app/packages/core/component/LexicalMarkdown/builder/BuilderTestPage.tsx@@ -36,7 +36,11 @@ function Editor() {.withHistory().withEmojis().withMedia()- .withMentions()+ .withMentions({+ onCreate: (args) => {+ console.log('CREATE MENTION', args);+ },+ }).withActions().onEscape(() => {console.log('ESCAPE');diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/mentions/mentionsPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/mentions/mentionsPlugin.tsindex 533caf6a05..f7fe4544ed 100644--- a/js/app/packages/core/component/LexicalMarkdown/plugins/mentions/mentionsPlugin.ts+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/mentions/mentionsPlugin.ts@@ -630,7 +630,9 @@ function registerMentionsPlugin((mutatedNodes, { prevEditorState }) => {for (const [nodeKey, mutation] of mutatedNodes) {const node = nodeByKey(- editor.getEditorState(),+ mutation === 'destroyed'+ ? prevEditorState+ : editor.getEditorState(),nodeKey) as DocumentMentionNode | null;@@ -656,36 +658,7 @@ function registerMentionsPlugin(}} else if (mutation === 'created') {if (onCreateMention) {- let fileType = '';- const documentName = node.getDocumentName();- const blockName = node.getBlockName();- if (blockName === 'pdf') fileType = 'pdf';- else if (blockName === 'write') fileType = 'docx';- else if (blockName === 'md') fileType = 'md';- else if (blockName === 'canvas') fileType = 'canvas';- else if (blockName === 'code') {- const blockParams = node.getBlockParams();- fileType = blockParams?.fileType || 'txt';- } else if (blockName === 'image') {- fileType = 'png'; // Default to png- } else if (blockName === 'channel') {- fileType = 'channel';- } else if (blockName === 'project') {- fileType = 'project';- } else if (blockName === 'rss') {- fileType = 'rss';- } else if (blockName === 'email') {- fileType = 'email';- } else if (blockName === 'unknown') {- fileType = 'unknown';- }- onCreateMention({- itemType: blockName === 'email' ? 'thread' : itemType,- itemId: node.getDocumentId(),- fileType,- documentName,- channelType: node.getChannelType(),- });+ onCreateMention($mentionItemFromNode(node));}}}@@ -697,26 +670,31 @@ function registerMentionsPlugin(UserMentionNode,(mutatedNodes, { prevEditorState }) => {for (const [nodeKey, mutation] of mutatedNodes) {- const node = nodeByKey(prevEditorState, nodeKey) as UserMentionNode;- if (node && mutation === 'destroyed') {- const mentionUuid = node.getMentionUuid();- if (mentionUuid && sourceDocumentId) {- untrackMention(sourceDocumentId, mentionUuid);- }- if (onRemoveMention) {- onRemoveMention({- itemType: 'user',- itemId: node.getUserId(),- });- }- }- if (node && mutation === 'created') {- if (onCreateMention) {+ if (mutation === 'created') {+ const node = nodeByKey(+ editor.getEditorState(),+ nodeKey+ ) as UserMentionNode;+ if (node && onCreateMention) {onCreateMention({itemType: 'user',itemId: node.getUserId(),});}+ } else if (mutation === 'destroyed') {+ const node = nodeByKey(prevEditorState, nodeKey) as UserMentionNode;+ if (node) {+ const mentionUuid = node.getMentionUuid();+ if (mentionUuid && sourceDocumentId) {+ untrackMention(sourceDocumentId, mentionUuid);+ }+ if (onRemoveMention) {+ onRemoveMention({+ itemType: 'user',+ itemId: node.getUserId(),+ });+ }+ }}}updateMentionsSignal();@@ -727,29 +705,34 @@ function registerMentionsPlugin(ContactMentionNode,(mutatedNodes, { prevEditorState }) => {for (const [nodeKey, mutation] of mutatedNodes) {- const node = nodeByKey(- prevEditorState,- nodeKey- ) as ContactMentionNode;- if (node && mutation === 'destroyed') {- const mentionUuid = node.getMentionUuid();- if (mentionUuid && sourceDocumentId) {- untrackMention(sourceDocumentId, mentionUuid);- }- if (onRemoveMention) {- onRemoveMention({- itemType: 'contact',- itemId: node.getContactId(),- });- }- }- if (node && mutation === 'created') {- if (onCreateMention) {+ if (mutation === 'created') {+ const node = nodeByKey(+ editor.getEditorState(),+ nodeKey+ ) as ContactMentionNode;+ if (node && onCreateMention) {onCreateMention({itemType: 'contact',itemId: node.getContactId(),});}+ } else if (mutation === 'destroyed') {+ const node = nodeByKey(+ prevEditorState,+ nodeKey+ ) as ContactMentionNode;+ if (node) {+ const mentionUuid = node.getMentionUuid();+ if (mentionUuid && sourceDocumentId) {+ untrackMention(sourceDocumentId, mentionUuid);+ }+ if (onRemoveMention) {+ onRemoveMention({+ itemType: 'contact',+ itemId: node.getContactId(),+ });+ }+ }}}updateMentionsSignal();@@ -760,26 +743,31 @@ function registerMentionsPlugin(DateMentionNode,(mutatedNodes, { prevEditorState }) => {for (const [nodeKey, mutation] of mutatedNodes) {- const node = nodeByKey(prevEditorState, nodeKey) as DateMentionNode;- if (node && mutation === 'destroyed') {- const mentionUuid = node.getMentionUuid();- if (mentionUuid && sourceDocumentId) {- untrackMention(sourceDocumentId, mentionUuid);- }- if (onRemoveMention) {- onRemoveMention({- itemType: 'date',- itemId: node.getDate(),- });- }- }- if (node && mutation === 'created') {- if (onCreateMention) {+ if (mutation === 'created') {+ const node = nodeByKey(+ editor.getEditorState(),+ nodeKey+ ) as DateMentionNode;+ if (node && onCreateMention) {onCreateMention({itemType: 'date',itemId: node.getDate(),});}+ } else if (mutation === 'destroyed') {+ const node = nodeByKey(prevEditorState, nodeKey) as DateMentionNode;+ if (node) {+ const mentionUuid = node.getMentionUuid();+ if (mentionUuid && sourceDocumentId) {+ untrackMention(sourceDocumentId, mentionUuid);+ }+ if (onRemoveMention) {+ onRemoveMention({+ itemType: 'date',+ itemId: node.getDate(),+ });+ }+ }}}updateMentionsSignal();@@ -790,19 +778,25 @@ function registerMentionsPlugin(GroupMentionNode,(mutatedNodes, { prevEditorState }) => {for (const [nodeKey, mutation] of mutatedNodes) {- const node = nodeByKey(prevEditorState, nodeKey) as GroupMentionNode;- if (node && mutation === 'destroyed') {- if (onRemoveMention) {- onRemoveMention({+ if (mutation === 'created') {+ const node = nodeByKey(+ editor.getEditorState(),+ nodeKey+ ) as GroupMentionNode;+ if (node && onCreateMention) {+ onCreateMention({itemType: 'group',itemId: node.getGroupAlias(),groupAlias: node.getGroupAlias(),});}- }- if (node && mutation === 'created') {- if (onCreateMention) {- onCreateMention({+ } else if (mutation === 'destroyed') {+ const node = nodeByKey(+ prevEditorState,+ nodeKey+ ) as GroupMentionNode;+ if (node && onRemoveMention) {+ onRemoveMention({itemType: 'group',itemId: node.getGroupAlias(),groupAlias: node.getGroupAlias(),diff --git a/js/app/packages/core/component/LexicalMarkdown/tests/mentionCallbacks.test.ts b/js/app/packages/core/component/LexicalMarkdown/tests/mentionCallbacks.test.tsnew file mode 100644index 0000000000..e58ffe11a8--- /dev/null+++ b/js/app/packages/core/component/LexicalMarkdown/tests/mentionCallbacks.test.ts@@ -0,0 +1,313 @@+import { describe, expect, test, vi } from 'vitest';++vi.hoisted(() => {+ if (typeof globalThis.Worker === 'undefined') {+ (globalThis as any).Worker = class FakeWorker {+ onmessage = null;+ postMessage() {}+ terminate() {}+ addEventListener() {}+ removeEventListener() {}+ };+ }+ if (+ typeof globalThis.window !== 'undefined' &&+ typeof globalThis.window.matchMedia !== 'function'+ ) {+ (globalThis.window as any).matchMedia = () => ({+ matches: false,+ addEventListener: () => {},+ removeEventListener: () => {},+ addListener: () => {},+ removeListener: () => {},+ });+ }+});++// Stub any imports before they import the entire app (sad).+vi.mock('@core/constant/allBlocks', () => ({+ verifyBlockName: (name: string) => name,+}));+vi.mock('@core/signal/mention', () => ({+ untrackMention: vi.fn(),+}));+vi.mock('@service-storage/client', () => ({+ blockNameToItemType: (name: string) => {+ const map: Record<string, string> = {+ write: 'document',+ channel: 'channel',+ project: 'project',+ chat: 'chat',+ email: 'email',+ };+ return map[name] ?? 'document';+ },+}));+vi.mock('../utils', async () => {+ const { $getNodeByKey } = await import('lexical');+ return {+ $collapseSelection: vi.fn(),+ $traverseNodes: vi.fn(),+ nodeByKey: (editorOrState: any, key: string) => {+ let node: any;+ editorOrState.read(() => {+ node = $getNodeByKey(key);+ });+ return node;+ },+ };+});+vi.mock('../plugins/shared', () => ({+ mapRegisterDelete: () => () => {},+}));++import {+ $getRoot,+ $createParagraphNode,+ createEditor,+ type LexicalEditor,+} from 'lexical';+import { SupportedNodeTypes } from '@lexical-core/node-list';+import {+ mentionsPlugin,+ type ItemMention,+ INSERT_DOCUMENT_MENTION_COMMAND,+ INSERT_USER_MENTION_COMMAND,+ INSERT_CONTACT_MENTION_COMMAND,+ INSERT_DATE_MENTION_COMMAND,+ INSERT_GROUP_MENTION_COMMAND,+} from '../plugins/mentions/mentionsPlugin';++function createTestEditor(): LexicalEditor {+ const editor = createEditor({+ namespace: 'test-mentions',+ nodes: [...SupportedNodeTypes],+ onError: (e) => {+ throw e;+ },+ });++ const root = document.createElement('div');+ root.contentEditable = 'true';+ document.body.appendChild(root);+ editor.setRootElement(root);++ editor.update(+ () => {+ $getRoot().clear().append($createParagraphNode());+ },+ { discrete: true }+ );++ return editor;+}++describe('mentionsPlugin callbacks', () => {+ test('onCreateMention fires for each mention type and onRemoveMention fires on clear', async () => {+ const editor = createTestEditor();+ const created: ItemMention[] = [];+ const removed: ItemMention[] = [];++ const flush = () => editor.read(() => {});++ const cleanup = mentionsPlugin({+ onCreateMention: (m) => created.push(m),+ onRemoveMention: (m) => removed.push(m),+ })(editor);++ // Insert one of each mention type.++ editor.dispatchCommand(INSERT_DOCUMENT_MENTION_COMMAND, {+ documentId: 'doc-1',+ documentName: 'Test Doc',+ blockName: 'write',+ mentionUuid: 'uuid-doc',+ });+ flush();++ editor.dispatchCommand(INSERT_USER_MENTION_COMMAND, {+ userId: 'user-1',+ email: '[email protected]',+ mentionUuid: 'uuid-user',+ });+ flush();++ editor.dispatchCommand(INSERT_CONTACT_MENTION_COMMAND, {+ contactId: 'contact-1',+ name: 'Jane',+ emailOrDomain: '[email protected]',+ isCompany: false,+ mentionUuid: 'uuid-contact',+ });+ flush();++ editor.dispatchCommand(INSERT_DATE_MENTION_COMMAND, {+ date: '2026-03-24',+ displayFormat: 'March 24, 2026',+ mentionUuid: 'uuid-date',+ });+ flush();++ editor.dispatchCommand(INSERT_GROUP_MENTION_COMMAND, {+ groupAlias: 'engineering',+ });+ flush();++ expect(created).toHaveLength(5);+ expect(created).toContainEqual(+ expect.objectContaining({ itemType: 'document', itemId: 'doc-1' })+ );+ expect(created).toContainEqual(+ expect.objectContaining({ itemType: 'user', itemId: 'user-1' })+ );+ expect(created).toContainEqual(+ expect.objectContaining({ itemType: 'contact', itemId: 'contact-1' })+ );+ expect(created).toContainEqual(+ expect.objectContaining({ itemType: 'date', itemId: '2026-03-24' })+ );+ expect(created).toContainEqual(+ expect.objectContaining({+ itemType: 'group',+ itemId: 'engineering',+ groupAlias: 'engineering',+ })+ );++ // Clear editor to trigger destroy mutations.+ created.length = 0;++ editor.update(+ () => {+ $getRoot().clear().append($createParagraphNode());+ },+ { discrete: true }+ );+ flush();++ expect(removed).toHaveLength(5);+ expect(removed).toContainEqual(+ expect.objectContaining({ itemType: 'document', itemId: 'doc-1' })+ );+ expect(removed).toContainEqual(+ expect.objectContaining({ itemType: 'user', itemId: 'user-1' })+ );+ expect(removed).toContainEqual(+ expect.objectContaining({ itemType: 'contact', itemId: 'contact-1' })+ );+ expect(removed).toContainEqual(+ expect.objectContaining({ itemType: 'date', itemId: '2026-03-24' })+ );+ expect(removed).toContainEqual(+ expect.objectContaining({+ itemType: 'group',+ itemId: 'engineering',+ groupAlias: 'engineering',+ })+ );++ cleanup();+ });++ test('onCreateMention emits correct fileType and itemType for non-write document mentions', async () => {+ const editor = createTestEditor();+ const created: ItemMention[] = [];++ const flush = () => editor.read(() => {});++ const cleanup = mentionsPlugin({+ onCreateMention: (m) => created.push(m),+ })(editor);++ editor.dispatchCommand(INSERT_DOCUMENT_MENTION_COMMAND, {+ documentId: 'doc-2',+ documentName: 'Team Chat',+ blockName: 'chat',+ mentionUuid: 'uuid-doc-chat',+ });+ flush();++ editor.dispatchCommand(INSERT_DOCUMENT_MENTION_COMMAND, {+ documentId: 'doc-3',+ documentName: 'General',+ blockName: 'channel',+ mentionUuid: 'uuid-doc-channel',+ });+ flush();++ editor.dispatchCommand(INSERT_DOCUMENT_MENTION_COMMAND, {+ documentId: 'doc-4',+ documentName: 'My Project',+ blockName: 'project',+ mentionUuid: 'uuid-doc-project',+ });+ flush();++ editor.dispatchCommand(INSERT_DOCUMENT_MENTION_COMMAND, {+ documentId: 'doc-5',+ documentName: 'Inbox Thread',+ blockName: 'email',+ mentionUuid: 'uuid-doc-email',+ });+ flush();++ expect(created).toHaveLength(4);++ expect(created).toContainEqual(+ expect.objectContaining({+ itemType: 'chat',+ itemId: 'doc-2',+ documentName: 'Team Chat',+ fileType: 'chat',+ })+ );++ expect(created).toContainEqual(+ expect.objectContaining({+ itemType: 'channel',+ itemId: 'doc-3',+ documentName: 'General',+ fileType: 'channel',+ })+ );++ expect(created).toContainEqual(+ expect.objectContaining({+ itemType: 'project',+ itemId: 'doc-4',+ documentName: 'My Project',+ fileType: 'project',+ })+ );++ expect(created).toContainEqual(+ expect.objectContaining({+ itemType: 'thread',+ itemId: 'doc-5',+ documentName: 'Inbox Thread',+ fileType: 'email',+ })+ );++ cleanup();+ });++ test('custom plugin passed to builder runs and cleans up', () => {+ const editor = createTestEditor();+ const pluginInit = vi.fn();+ const pluginCleanup = vi.fn();++ const plugin = (e: LexicalEditor) => {+ pluginInit(e);+ return pluginCleanup;+ };++ const dispose = plugin(editor);++ expect(pluginInit).toHaveBeenCalledOnce();+ expect(pluginInit).toHaveBeenCalledWith(editor);++ dispose();+ expect(pluginCleanup).toHaveBeenCalledOnce();+ });+});