Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(md): flx mention flashing (#7526)

Seamus6b89d5f48a24
diff --git a/docs/AGENT_GUIDE/documents.md b/docs/AGENT_GUIDE/documents.md
index cc7c455b44..e8b4ffdfc9 100644
--- a/docs/AGENT_GUIDE/documents.md
+++ b/docs/AGENT_GUIDE/documents.md
@@ -1618,7 +1618,8 @@ With `ENABLE_GRAPHQL_SOUP` enabled, the popup reuses the reference's live `ItemP
batch, including task properties and viewer permission, without another fetch.
Explicit refreshes may revalidate that batch, but requests must settle while the
pointer stays over the same reference; cache updates must not cause a continuous
-fetch cascade.
+fetch cascade. Adding or changing a direct sibling of a document reference must
+leave the reference mounted without flashing or restarting its preview query.
## Embedded document cards
diff --git a/packages/lexical-core/nodes/DocumentCardNode.ts b/packages/lexical-core/nodes/DocumentCardNode.ts
index c32d471001..76705305e3 100644
--- a/packages/lexical-core/nodes/DocumentCardNode.ts
+++ b/packages/lexical-core/nodes/DocumentCardNode.ts
@@ -96,6 +96,8 @@ export class DocumentCardNode extends DecoratorBlockNode<
__previewBox: PreviewBox;
__previewData: PreviewData | undefined;
__mentionUuid: string | undefined;
+ __cachedDecoratorSignature?: string;
+ __cachedDecoratorComponent?: DecoratorComponent<DocumentCardDecoratorProps>;
static getType() {
return 'document-card';
@@ -106,7 +108,7 @@ export class DocumentCardNode extends DecoratorBlockNode<
}
static clone(node: DocumentCardNode) {
- return new DocumentCardNode(
+ const clone = new DocumentCardNode(
node.__documentId,
node.__documentName,
node.__blockName,
@@ -116,6 +118,9 @@ export class DocumentCardNode extends DecoratorBlockNode<
node.__mentionUuid,
node.__key
);
+ clone.__cachedDecoratorSignature = node.__cachedDecoratorSignature;
+ clone.__cachedDecoratorComponent = node.__cachedDecoratorComponent;
+ return clone;
}
constructor(
@@ -329,28 +334,30 @@ export class DocumentCardNode extends DecoratorBlockNode<
}
decorate(_: LexicalEditor, config: EditorConfig) {
- const key = $getId(this);
- const previewComponent = key
- ? documentCardNodeKeyToPreviewComponent.get(key)?.component
- : undefined;
-
- const decorator =
+ const componentProps = this.exportComponentProps();
+ const signature = JSON.stringify(componentProps);
+ if (
+ this.__cachedDecoratorComponent &&
+ this.__cachedDecoratorSignature === signature
+ )
+ return this.__cachedDecoratorComponent;
+
+ const Component =
getDecorator<DocumentCardDecoratorProps>(DocumentCardNode);
- if (decorator) {
- return () =>
- decorator({
- documentId: this.__documentId,
- documentName: this.__documentName,
- blockName: this.__blockName,
- blockParams: this.__blockParams,
- previewBox: this.__previewBox,
- previewData: this.__previewData,
- mentionUuid: this.__mentionUuid,
- key: this.getKey(),
- theme: config.theme,
- previewComponent,
- });
- }
+ if (!Component) return undefined;
+
+ const nodeId = $getId(this);
+ this.__cachedDecoratorSignature = signature;
+ this.__cachedDecoratorComponent = () =>
+ Component({
+ ...componentProps,
+ key: this.getKey(),
+ theme: config.theme,
+ previewComponent: nodeId
+ ? documentCardNodeKeyToPreviewComponent.get(nodeId)?.component
+ : undefined,
+ });
+ return this.__cachedDecoratorComponent;
}
}
diff --git a/packages/lexical-core/nodes/DocumentMentionNode.ts b/packages/lexical-core/nodes/DocumentMentionNode.ts
index c84d1188a9..f7c4b4257d 100644
--- a/packages/lexical-core/nodes/DocumentMentionNode.ts
+++ b/packages/lexical-core/nodes/DocumentMentionNode.ts
@@ -50,6 +50,8 @@ export class DocumentMentionNode extends DecoratorNode<
__collapsed: boolean;
__channelType: string | undefined;
__createdAt: number | undefined;
+ __cachedDecoratorSignature?: string;
+ __cachedDecoratorComponent?: DecoratorComponent<DocumentMentionDecoratorProps>;
static getType() {
return 'document-mention';
@@ -64,7 +66,7 @@ export class DocumentMentionNode extends DecoratorNode<
}
static clone(node: DocumentMentionNode) {
- return new DocumentMentionNode(
+ const clone = new DocumentMentionNode(
node.__documentId,
node.__documentName,
node.__blockName,
@@ -75,6 +77,9 @@ export class DocumentMentionNode extends DecoratorNode<
node.__createdAt,
node.__key
);
+ clone.__cachedDecoratorSignature = node.__cachedDecoratorSignature;
+ clone.__cachedDecoratorComponent = node.__cachedDecoratorComponent;
+ return clone;
}
constructor(
@@ -310,23 +315,26 @@ export class DocumentMentionNode extends DecoratorNode<
}
decorate(_: LexicalEditor, config: EditorConfig) {
- const decorator =
+ const componentProps = this.exportComponentProps();
+ const signature = JSON.stringify(componentProps);
+ if (
+ this.__cachedDecoratorComponent &&
+ this.__cachedDecoratorSignature === signature
+ )
+ return this.__cachedDecoratorComponent;
+
+ const Component =
getDecorator<DocumentMentionDecoratorProps>(DocumentMentionNode);
- if (decorator) {
- return () =>
- decorator({
- documentId: this.__documentId,
- documentName: this.__documentName,
- blockName: this.__blockName,
- blockParams: this.__blockParams,
- mentionUuid: this.__mentionUuid,
- collapsed: this.__collapsed,
- channelType: this.__channelType,
- createdAt: this.__createdAt,
- key: this.getKey(),
- theme: config.theme,
- });
- }
+ if (!Component) return undefined;
+
+ this.__cachedDecoratorSignature = signature;
+ this.__cachedDecoratorComponent = () =>
+ Component({
+ ...componentProps,
+ key: this.getKey(),
+ theme: config.theme,
+ });
+ return this.__cachedDecoratorComponent;
}
}
diff --git a/packages/lexical-core/tests/document-reference-decorator-stability.test.ts b/packages/lexical-core/tests/document-reference-decorator-stability.test.ts
new file mode 100644
index 0000000000..c297665949
--- /dev/null
+++ b/packages/lexical-core/tests/document-reference-decorator-stability.test.ts
@@ -0,0 +1,90 @@
+// @vitest-environment jsdom
+import { createHeadlessEditor } from '@lexical/headless';
+import {
+ $createParagraphNode,
+ $createTextNode,
+ $getNodeByKey,
+ $getRoot,
+} from 'lexical';
+import { afterEach, expect, it } from 'vitest';
+import { clearDecorators, setDecorator } from '../decoratorRegistry';
+import { NodeReplacements, SupportedNodeTypes } from '../node-list';
+import {
+ $createDocumentCardNode,
+ DocumentCardNode,
+} from '../nodes/DocumentCardNode';
+import {
+ $createDocumentMentionNode,
+ DocumentMentionNode,
+} from '../nodes/DocumentMentionNode';
+
+afterEach(clearDecorators);
+
+it.each(['mention', 'card'] as const)(
+ 'keeps the document %s decorator mounted across direct sibling edits',
+ (kind) => {
+ setDecorator(DocumentMentionNode, () => null);
+ setDecorator(DocumentCardNode, () => null);
+ const editor = createHeadlessEditor({
+ nodes: [...SupportedNodeTypes, ...NodeReplacements],
+ });
+ let key = '';
+ let original: unknown;
+ editor.update(
+ () => {
+ const data = {
+ documentId: 'document-1',
+ documentName: 'Roadmap',
+ blockName: 'md',
+ };
+ const node =
+ kind === 'mention'
+ ? $createDocumentMentionNode({ ...data, createdAt: 1 })
+ : $createDocumentCardNode(data);
+ if (kind === 'mention') {
+ $getRoot().append($createParagraphNode().append(node));
+ } else {
+ $getRoot().append(node);
+ }
+ key = node.getKey();
+ original = node.decorate(editor, editor._config);
+ },
+ { discrete: true }
+ );
+
+ for (const side of ['before', 'after'] as const) {
+ editor.update(
+ () => {
+ const node = $getNodeByKey(key);
+ if (
+ !(
+ node instanceof DocumentMentionNode ||
+ node instanceof DocumentCardNode
+ )
+ )
+ throw new Error('Missing document reference');
+ const sibling =
+ kind === 'mention' ? $createTextNode(side) : $createParagraphNode();
+ if (side === 'before') node.insertBefore(sibling);
+ else node.insertAfter(sibling);
+ expect(node.getLatest().decorate(editor, editor._config)).toBe(
+ original
+ );
+ },
+ { discrete: true }
+ );
+ }
+
+ editor.update(
+ () => {
+ const node = $getNodeByKey<DocumentMentionNode | DocumentCardNode>(key);
+ if (!node) throw new Error('Missing document reference');
+ node.setDocumentName('Updated roadmap');
+ expect(node.getLatest().decorate(editor, editor._config)).not.toBe(
+ original
+ );
+ },
+ { discrete: true }
+ );
+ }
+);