Open History to see the last commit
1import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
2import type { ComposerContextId } from "@t3tools/contracts";
3import {
4 $applyNodeReplacement,
5 DecoratorNode,
6 type NodeKey,
7 type SerializedLexicalNode,
8 type Spread,
9} from "lexical";
10import type { ReactElement } from "react";
11
12import { randomUUID } from "~/lib/utils";
13import { COMPOSER_INLINE_CHIP_DECORATOR_CLASS_NAME } from "./composerInlineChip";
14import { ComposerContextReferenceChip } from "./composerContextPresentation";
15
16export interface ComposerContextReference {
17 kind: string;
18 contextId: string;
19 label: string;
20}
21
22export type SerializedComposerContextReferenceNode = Spread<
23 ComposerContextReference & {
24 referenceId: string;
25 type: "composer-context-reference";
26 version: 1;
27 },
28 SerializedLexicalNode
29>;
30
31/**
32 * One inline occurrence of a context payload. The node's text is the canonical link, so the
33 * prompt string carries kind and id and rebuilding the editor from it restores the same chip.
34 * `referenceId` identifies this occurrence; duplicating the node mints a new one.
35 */
36export class ComposerContextReferenceNode extends DecoratorNode<ReactElement> {
37 __kind: string;
38 __contextId: string;
39 __label: string;
40 __referenceId: string;
41
42 static override getType(): "composer-context-reference" {
43 return "composer-context-reference";
44 }
45
46 static override clone(node: ComposerContextReferenceNode): ComposerContextReferenceNode {
47 return new ComposerContextReferenceNode(
48 { kind: node.__kind, contextId: node.__contextId, label: node.__label },
49 node.__referenceId,
50 node.__key,
51 );
52 }
53
54 static override importJSON(
55 serializedNode: SerializedComposerContextReferenceNode,
56 ): ComposerContextReferenceNode {
57 return $createComposerContextReferenceNode(
58 {
59 kind: serializedNode.kind,
60 contextId: serializedNode.contextId,
61 label: serializedNode.label,
62 },
63 serializedNode.referenceId,
64 ).updateFromJSON(serializedNode);
65 }
66
67 constructor(reference: ComposerContextReference, referenceId: string, key?: NodeKey) {
68 super(key);
69 this.__kind = reference.kind;
70 this.__contextId = reference.contextId;
71 this.__label = reference.label;
72 this.__referenceId = referenceId;
73 }
74
75 override exportJSON(): SerializedComposerContextReferenceNode {
76 const latest = this.getLatest();
77 return {
78 ...super.exportJSON(),
79 kind: latest.__kind,
80 contextId: latest.__contextId,
81 label: latest.__label,
82 referenceId: latest.__referenceId,
83 type: "composer-context-reference",
84 version: 1,
85 };
86 }
87
88 override createDOM(): HTMLElement {
89 const dom = document.createElement("span");
90 dom.className = `${COMPOSER_INLINE_CHIP_DECORATOR_CLASS_NAME} max-w-full`;
91 return dom;
92 }
93
94 override updateDOM(): false {
95 return false;
96 }
97
98 override getTextContent(): string {
99 const latest = this.getLatest();
100 return formatComposerContextReference({
101 kind: latest.__kind,
102 contextId: latest.__contextId as ComposerContextId,
103 label: latest.__label,
104 });
105 }
106
107 override isInline(): true {
108 return true;
109 }
110
111 getReference(): ComposerContextReference {
112 const latest = this.getLatest();
113 return { kind: latest.__kind, contextId: latest.__contextId, label: latest.__label };
114 }
115
116 override decorate(): ReactElement {
117 const latest = this.getLatest();
118 return (
119 <ComposerContextReferenceChip
120 kind={latest.__kind}
121 contextId={latest.__contextId}
122 label={latest.__label}
123 />
124 );
125 }
126}
127
128export function $createComposerContextReferenceNode(
129 reference: ComposerContextReference,
130 referenceId: string = randomUUID(),
131): ComposerContextReferenceNode {
132 return $applyNodeReplacement(new ComposerContextReferenceNode(reference, referenceId));
133}