@duskmoon-dev/components 0.5.6 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +85 -2
  2. package/dist/chunk-b1jnq16e.js +727 -0
  3. package/dist/chunk-kg46s2vk.js +238 -0
  4. package/dist/components/chat/Chat.types.d.ts +8 -0
  5. package/dist/components/chat/ChatAuthoritativeQueue.d.ts +4 -0
  6. package/dist/components/chat/ChatSessionComposer.d.ts +4 -0
  7. package/dist/components/chat/ChatSessionComposer.types.d.ts +42 -0
  8. package/dist/components/chat/ComposerAttachment.types.d.ts +80 -0
  9. package/dist/components/chat/ComposerDraft.types.d.ts +89 -0
  10. package/dist/components/chat/ComposerProvider.types.d.ts +66 -0
  11. package/dist/components/chat/ComposerQueue.types.d.ts +64 -0
  12. package/dist/components/chat/ComposerSession.types.d.ts +78 -0
  13. package/dist/components/chat/ComposerSettings.d.ts +4 -0
  14. package/dist/components/chat/composer/CompletionList.d.ts +10 -0
  15. package/dist/components/chat/composer/ComposerShell.d.ts +19 -4
  16. package/dist/components/chat/composer/DraftLifecycle.d.ts +17 -0
  17. package/dist/components/chat/composer/EditorAdapter.types.d.ts +36 -0
  18. package/dist/components/chat/composer/PlainEditor.d.ts +2 -0
  19. package/dist/components/chat/composer/PlainSemanticEditor.d.ts +6 -0
  20. package/dist/components/chat/composer/PreservedDraft.d.ts +7 -0
  21. package/dist/components/chat/composer/SemanticComposer.d.ts +10 -0
  22. package/dist/components/chat/composer/attachments.d.ts +4 -0
  23. package/dist/components/chat/composer/document.d.ts +3 -1
  24. package/dist/components/chat/composer/drafts.d.ts +2 -0
  25. package/dist/components/chat/composer/providers.d.ts +40 -0
  26. package/dist/components/chat/composer/session.d.ts +30 -0
  27. package/dist/components/chat/composer/useComposerAttachments.d.ts +3 -0
  28. package/dist/components/chat/composer/useComposerDraft.d.ts +3 -0
  29. package/dist/components/chat/composer/useComposerProviders.d.ts +7 -0
  30. package/dist/components/chat/composer/useComposerSession.d.ts +16 -0
  31. package/dist/components/chat/index.d.ts +17 -0
  32. package/dist/components/chat/index.js +4386 -1074
  33. package/dist/components/chat/rich/LexicalEditor.d.ts +10 -0
  34. package/dist/components/chat/rich/RichChatComposer.d.ts +4 -0
  35. package/dist/components/chat/rich/index.d.ts +2 -0
  36. package/dist/components/chat/rich/index.js +3437 -0
  37. package/dist/components/chat/rich/nodes.d.ts +49 -0
  38. package/dist/generated/version.d.ts +1 -1
  39. package/dist/index.js +7107 -4252
  40. package/dist/infrastructure.js +1 -1
  41. package/package.json +25 -4
@@ -0,0 +1,727 @@
1
+ "use client";
2
+ import {
3
+ readComposerDocument,
4
+ snapshotComposerDocument,
5
+ exportComposerText
6
+ } from "./chunk-kg46s2vk.js";
7
+
8
+ // src/components/chat/rich/LexicalEditor.tsx
9
+ import {
10
+ forwardRef,
11
+ useId,
12
+ useImperativeHandle,
13
+ useLayoutEffect,
14
+ useRef
15
+ } from "react";
16
+ import {
17
+ $getRoot as $getRoot2,
18
+ $getSelection as $getSelection2,
19
+ $isRangeSelection as $isRangeSelection2,
20
+ CLEAR_HISTORY_COMMAND,
21
+ COMMAND_PRIORITY_CRITICAL,
22
+ COMMAND_PRIORITY_HIGH,
23
+ COPY_COMMAND,
24
+ createEditor,
25
+ CUT_COMMAND,
26
+ CUT_TAG,
27
+ HISTORY_PUSH_TAG,
28
+ KEY_DOWN_COMMAND,
29
+ mergeRegister,
30
+ PASTE_COMMAND,
31
+ PASTE_TAG,
32
+ RootNode,
33
+ SELECTION_CHANGE_COMMAND
34
+ } from "lexical";
35
+ import {
36
+ createEmptyHistoryState,
37
+ registerHistory
38
+ } from "@lexical/history";
39
+ import { registerPlainText } from "@lexical/plain-text";
40
+
41
+ // src/components/chat/rich/nodes.ts
42
+ import {
43
+ $applyNodeReplacement,
44
+ $createLineBreakNode,
45
+ $createParagraphNode,
46
+ $createRangeSelection,
47
+ $createTextNode,
48
+ $getRoot,
49
+ $getSelection,
50
+ $isElementNode,
51
+ $isLineBreakNode,
52
+ $isRangeSelection,
53
+ $isTextNode,
54
+ $setSelection,
55
+ TextNode
56
+ } from "lexical";
57
+ function atomText(atom) {
58
+ return atom.kind === "command" ? atom.source : atom.kind === "reference" ? atom.value.label || atom.value.plainText || atom.value.refId : atom.value.text || "";
59
+ }
60
+
61
+ class ComposerAtomNode extends TextNode {
62
+ __atom;
63
+ static getType() {
64
+ return "duskmoon-composer-atom";
65
+ }
66
+ static clone(node) {
67
+ const clone = new ComposerAtomNode(node.__atom, node.__key);
68
+ clone.__mode = node.__mode;
69
+ clone.__format = node.__format;
70
+ clone.__style = node.__style;
71
+ clone.__detail = node.__detail;
72
+ return clone;
73
+ }
74
+ static importJSON(serialized) {
75
+ return $createComposerAtom(serialized.atom);
76
+ }
77
+ constructor(atom, key) {
78
+ super(atomText(atom), key);
79
+ this.__atom = atom;
80
+ }
81
+ exportJSON() {
82
+ return {
83
+ ...super.exportJSON(),
84
+ type: ComposerAtomNode.getType(),
85
+ version: 1,
86
+ atom: this.getLatest().__atom
87
+ };
88
+ }
89
+ createDOM() {
90
+ const atom = this.__atom;
91
+ const element = document.createElement("span");
92
+ element.dataset.composerKind = atom.kind;
93
+ if (atom.kind === "quote") {
94
+ element.className = "chat-composer-quote";
95
+ element.dataset.nodeId = atom.value.nodeId;
96
+ const text = document.createElement("span");
97
+ text.className = "chat-composer-quote-text";
98
+ text.textContent = atom.value.text;
99
+ element.append(text);
100
+ if (atom.value.source?.label) {
101
+ const source = document.createElement("span");
102
+ source.contentEditable = "false";
103
+ source.textContent = atom.value.source.label;
104
+ element.append(source);
105
+ }
106
+ element.setAttribute("aria-label", atom.value.source?.label ? `Quote from ${atom.value.source.label}` : "Quote");
107
+ } else {
108
+ element.className = "chip chat-composer-reference";
109
+ const label = document.createElement("span");
110
+ label.className = "chat-composer-label";
111
+ label.textContent = atomText(atom);
112
+ element.append(label);
113
+ if (atom.kind === "reference") {
114
+ element.dataset.nodeId = atom.value.nodeId;
115
+ element.title = atom.value.plainText;
116
+ element.setAttribute("aria-label", `Reference: ${atom.value.label || atom.value.refId}`);
117
+ } else
118
+ element.setAttribute("aria-label", `Command: ${atom.source}`);
119
+ }
120
+ return element;
121
+ }
122
+ updateDOM(previous) {
123
+ return previous.__atom !== this.__atom;
124
+ }
125
+ getDOMSlot(element) {
126
+ return super.getDOMSlot(element.firstElementChild);
127
+ }
128
+ isTextEntity() {
129
+ return true;
130
+ }
131
+ canInsertTextBefore() {
132
+ return false;
133
+ }
134
+ canInsertTextAfter() {
135
+ return false;
136
+ }
137
+ }
138
+ function $createComposerAtom(atom) {
139
+ if (atom.kind === "command") {
140
+ if (!atom.source || /\s/u.test(atom.source))
141
+ throw new TypeError("A command token needs one nonempty source word");
142
+ const document2 = snapshotComposerDocument({
143
+ schemaVersion: 1,
144
+ content: [],
145
+ command: atom.value
146
+ });
147
+ atom = { ...atom, value: document2.command };
148
+ } else {
149
+ const document2 = snapshotComposerDocument({
150
+ schemaVersion: 1,
151
+ content: [atom.value]
152
+ });
153
+ atom = { ...atom, value: document2.content[0] };
154
+ }
155
+ return $applyNodeReplacement(new ComposerAtomNode(Object.freeze(atom))).setMode("token");
156
+ }
157
+ var nextOccurrence = 0;
158
+ function occurrenceId() {
159
+ return `composer-node-${globalThis.crypto?.randomUUID?.() ?? ++nextOccurrence}`;
160
+ }
161
+ function textNodes(text) {
162
+ const result = [];
163
+ text.split(`
164
+ `).forEach((line, index) => {
165
+ if (index > 0)
166
+ result.push($createLineBreakNode());
167
+ if (line)
168
+ result.push($createTextNode(line));
169
+ });
170
+ return result;
171
+ }
172
+ function $nodesForDocument(document2, fresh = false) {
173
+ const result = [];
174
+ document2.content.forEach((node, index) => {
175
+ if (node.type === "text") {
176
+ let text = node.text;
177
+ if (index === 0 && document2.command) {
178
+ const source = /^\S+/u.exec(text)?.[0];
179
+ if (!source)
180
+ throw new TypeError("A claimed command requires a leading source token");
181
+ result.push($createComposerAtom({
182
+ kind: "command",
183
+ value: document2.command,
184
+ source
185
+ }));
186
+ text = text.slice(source.length);
187
+ }
188
+ result.push(...textNodes(text));
189
+ } else {
190
+ const value = fresh ? { ...node, nodeId: occurrenceId() } : node;
191
+ result.push($createComposerAtom(value.type === "reference" ? { kind: "reference", value } : { kind: "quote", value }));
192
+ }
193
+ });
194
+ if (document2.command && document2.content[0]?.type !== "text")
195
+ throw new TypeError("A claimed command requires a leading source token");
196
+ return result;
197
+ }
198
+ function $replaceComposerDocument(value) {
199
+ const document2 = snapshotComposerDocument(value);
200
+ const children = $nodesForDocument(document2);
201
+ const paragraph = $createParagraphNode();
202
+ paragraph.append(...children);
203
+ $getRoot().clear().append(paragraph);
204
+ }
205
+ function $layout() {
206
+ const content = [];
207
+ const segments = [];
208
+ let position = 0;
209
+ let command;
210
+ function appendText(text, node, claim) {
211
+ const previous = content.at(-1);
212
+ const offset = previous?.type === "text" ? previous.text.length : 0;
213
+ const value = { type: "text", text };
214
+ if (previous?.type === "text")
215
+ content[content.length - 1] = {
216
+ type: "text",
217
+ text: previous.text + text
218
+ };
219
+ else
220
+ content.push(value);
221
+ segments.push({
222
+ node,
223
+ start: position,
224
+ end: position + text.length,
225
+ documentIndex: content.length - 1,
226
+ textOffset: offset,
227
+ value,
228
+ command: claim
229
+ });
230
+ position += text.length;
231
+ }
232
+ function visit(node) {
233
+ if (node instanceof ComposerAtomNode) {
234
+ const atom = node.getLatest().__atom;
235
+ if (atom.kind === "command") {
236
+ if (position === 0)
237
+ command = atom.value;
238
+ appendText(atom.source, node, atom.value);
239
+ } else {
240
+ content.push(atom.value);
241
+ segments.push({
242
+ node,
243
+ start: position,
244
+ end: position + 1,
245
+ documentIndex: content.length - 1,
246
+ textOffset: 0,
247
+ value: atom.value
248
+ });
249
+ position++;
250
+ }
251
+ } else if ($isTextNode(node))
252
+ appendText(node.getTextContent(), node);
253
+ else if ($isLineBreakNode(node))
254
+ appendText(`
255
+ `, node);
256
+ else if ($isElementNode(node))
257
+ node.getChildren().forEach(visit);
258
+ else
259
+ throw new TypeError("Unsupported node in the Composer editing model");
260
+ }
261
+ $getRoot().getChildren().forEach((node, index) => {
262
+ if (index > 0)
263
+ appendText(`
264
+ `);
265
+ visit(node);
266
+ });
267
+ if (!content.length)
268
+ content.push({ type: "text", text: "" });
269
+ return {
270
+ document: snapshotComposerDocument({
271
+ schemaVersion: 1,
272
+ content,
273
+ ...command ? { command } : {}
274
+ }),
275
+ segments,
276
+ length: position
277
+ };
278
+ }
279
+ function $exportComposerDocument() {
280
+ return $layout().document;
281
+ }
282
+ function pointPosition(point, layout) {
283
+ const node = point.getNode();
284
+ const segment = layout.segments.find((entry) => entry.node?.getKey() === node.getKey());
285
+ if (segment)
286
+ return segment.start + (node instanceof ComposerAtomNode ? point.offset === 0 ? 0 : segment.end - segment.start : Math.min(point.offset, segment.end - segment.start));
287
+ if (!$isElementNode(node))
288
+ return null;
289
+ const child = node.getChildAtIndex(point.offset);
290
+ if (child) {
291
+ const first = layout.segments.find((entry) => entry.node && (entry.node.is(child) || child.isParentOf(entry.node)));
292
+ if (first)
293
+ return first.start;
294
+ }
295
+ const descendants = layout.segments.filter((entry) => entry.node && node.isParentOf(entry.node));
296
+ return descendants.at(-1)?.end ?? 0;
297
+ }
298
+ function $composerSelection() {
299
+ const selection = $getSelection();
300
+ if (!$isRangeSelection(selection))
301
+ return null;
302
+ const layout = $layout();
303
+ const anchor = pointPosition(selection.anchor, layout);
304
+ const focus = pointPosition(selection.focus, layout);
305
+ if (anchor === null || focus === null)
306
+ return null;
307
+ if (!layout.length)
308
+ return { nodeIndex: 0, start: 0, end: 0 };
309
+ const find = (position) => layout.segments.find((entry) => entry.value.type === "text" && !(entry.node instanceof ComposerAtomNode) && position >= entry.start && position <= entry.end);
310
+ const start = find(anchor);
311
+ const end = find(focus);
312
+ if (!start || !end || start.documentIndex !== end.documentIndex)
313
+ return null;
314
+ return {
315
+ nodeIndex: start.documentIndex,
316
+ start: Math.min(start.textOffset + anchor - start.start, end.textOffset + focus - end.start),
317
+ end: Math.max(start.textOffset + anchor - start.start, end.textOffset + focus - end.start)
318
+ };
319
+ }
320
+ function $normalizeComposerCommands() {
321
+ const { segments } = $layout();
322
+ for (const segment of segments)
323
+ if (segment.command && segment.start !== 0 && segment.node)
324
+ segment.node.replace($createTextNode(segment.value.type === "text" ? segment.value.text : ""));
325
+ }
326
+ function $selectionOrEnd() {
327
+ const selection = $getSelection();
328
+ return $isRangeSelection(selection) ? selection : $getRoot().selectEnd();
329
+ }
330
+ function $insertComposerDocument(value, fresh = true) {
331
+ const document2 = snapshotComposerDocument(value);
332
+ const nodes = $nodesForDocument(document2, fresh);
333
+ const selection = $selectionOrEnd();
334
+ selection.insertNodes(nodes);
335
+ }
336
+ function $insertComposerReference(reference) {
337
+ const document2 = $exportComposerDocument();
338
+ const duplicate = document2.content.some((node) => node.type !== "text" && node.nodeId === reference.nodeId);
339
+ $insertComposerDocument({ schemaVersion: 1, content: [reference] }, duplicate);
340
+ }
341
+ function $insertComposerQuote(quote) {
342
+ const duplicate = $exportComposerDocument().content.some((node) => node.type !== "text" && node.nodeId === quote.nodeId);
343
+ $insertComposerDocument({ schemaVersion: 1, content: [quote] }, duplicate);
344
+ }
345
+ function $claimComposerCommand(command, source) {
346
+ const token = $createComposerAtom({
347
+ kind: "command",
348
+ value: command,
349
+ source
350
+ });
351
+ const root = $getRoot();
352
+ const firstBlock = root.getFirstChild();
353
+ const paragraph = $isElementNode(firstBlock) ? firstBlock : $createParagraphNode();
354
+ if (!firstBlock)
355
+ root.append(paragraph);
356
+ const first = paragraph.getFirstChild();
357
+ if (first instanceof ComposerAtomNode && first.__atom.kind === "command")
358
+ first.replace(token);
359
+ else if ($isTextNode(first) && first.getTextContent().startsWith(source) && (first.getTextContent().length === source.length || /\s/u.test(first.getTextContent()[source.length]))) {
360
+ const [leading] = first.splitText(source.length);
361
+ leading.replace(token);
362
+ } else if (first) {
363
+ first.insertBefore(token);
364
+ if (!$isTextNode(first) || !/^\s/u.test(first.getTextContent()))
365
+ token.insertAfter($createTextNode(" "));
366
+ } else
367
+ paragraph.append(token);
368
+ token.selectEnd();
369
+ }
370
+ function $applyComposerProvider(effect) {
371
+ if (effect.action.type !== "insert")
372
+ return false;
373
+ const current = $composerSelection();
374
+ const trigger = effect.request.trigger;
375
+ if (!current || current.start !== current.end || current.nodeIndex !== trigger.nodeIndex || current.end !== trigger.caret)
376
+ return false;
377
+ const layout = $layout();
378
+ const node = layout.document.content[trigger.nodeIndex];
379
+ if (node?.type !== "text" || trigger.start < 0 || trigger.end > node.text.length || trigger.start > trigger.end || node.text.slice(trigger.start, trigger.end) !== trigger.text)
380
+ return false;
381
+ if (effect.action.command && (trigger.nodeIndex !== 0 || trigger.start !== 0))
382
+ return false;
383
+ function point(offset, end) {
384
+ const entries = layout.segments.filter((entry) => entry.documentIndex === trigger.nodeIndex && entry.node && $isTextNode(entry.node) && !(entry.node instanceof ComposerAtomNode));
385
+ const order = end ? entries : [...entries].reverse();
386
+ const entry = order.find((candidate) => offset >= candidate.textOffset && offset <= candidate.textOffset + candidate.end - candidate.start);
387
+ return entry && $isTextNode(entry.node) ? { node: entry.node, offset: offset - entry.textOffset } : undefined;
388
+ }
389
+ const start = point(trigger.start, false);
390
+ const end = point(trigger.end, true);
391
+ if (!start || !end)
392
+ return false;
393
+ const replacement = snapshotComposerDocument({
394
+ schemaVersion: 1,
395
+ content: effect.action.content,
396
+ ...effect.action.command ? { command: effect.action.command } : {}
397
+ });
398
+ const inserted = $nodesForDocument(replacement, true);
399
+ const selection = $createRangeSelection();
400
+ selection.setTextNodeRange(start.node, start.offset, end.node, end.offset);
401
+ $setSelection(selection);
402
+ selection.insertNodes(inserted);
403
+ return true;
404
+ }
405
+ function $composerClipboardDocument() {
406
+ const selection = $getSelection();
407
+ if (!$isRangeSelection(selection) || selection.isCollapsed())
408
+ return null;
409
+ const layout = $layout();
410
+ const a = pointPosition(selection.anchor, layout);
411
+ const b = pointPosition(selection.focus, layout);
412
+ if (a === null || b === null)
413
+ return null;
414
+ const start = Math.min(a, b);
415
+ const end = Math.max(a, b);
416
+ const content = [];
417
+ let command;
418
+ for (const segment of layout.segments) {
419
+ if (segment.start >= end || segment.end <= start)
420
+ continue;
421
+ if (segment.value.type !== "text")
422
+ content.push(segment.value);
423
+ else {
424
+ const text = segment.value.text.slice(Math.max(0, start - segment.start), Math.min(segment.value.text.length, end - segment.start));
425
+ const previous = content.at(-1);
426
+ if (previous?.type === "text")
427
+ content[content.length - 1] = {
428
+ type: "text",
429
+ text: previous.text + text
430
+ };
431
+ else
432
+ content.push({ type: "text", text });
433
+ if (segment.command && start <= segment.start && end >= segment.end)
434
+ command = segment.command;
435
+ }
436
+ }
437
+ return snapshotComposerDocument({
438
+ schemaVersion: 1,
439
+ content,
440
+ ...command ? { command } : {}
441
+ });
442
+ }
443
+
444
+ // src/components/chat/rich/LexicalEditor.tsx
445
+ import { jsx } from "react/jsx-runtime";
446
+ var COMPOSER_CLIPBOARD_TYPE = "application/x-duskmoon-composer+json";
447
+ var EXTERNAL_REPLACE = "duskmoon-composer-external";
448
+ var LexicalEditor = forwardRef((props, ref) => {
449
+ const rootRef = useRef(null);
450
+ const editorRef = useRef(null);
451
+ const historyRef = useRef(null);
452
+ const latest = useRef(props);
453
+ const composing = useRef(false);
454
+ const ready = useRef(false);
455
+ const revision = useRef(props.value.revision);
456
+ const originId = `${useId()}-editor`;
457
+ const applied = useRef(undefined);
458
+ const emitted = useRef(undefined);
459
+ const documentSignature = useRef("");
460
+ const selectionSignature = useRef("");
461
+ useLayoutEffect(() => {
462
+ latest.current = props;
463
+ });
464
+ function report(error) {
465
+ const cause = error instanceof Error ? error : new Error(String(error));
466
+ if (latest.current.onError)
467
+ latest.current.onError(cause);
468
+ else
469
+ throw cause;
470
+ }
471
+ useImperativeHandle(ref, () => {
472
+ function transact(action) {
473
+ const editor = editorRef.current;
474
+ if (!editor || !ready.current || latest.current.disabled || composing.current || editor.isComposing())
475
+ return;
476
+ editor.update(action, { discrete: true, tag: HISTORY_PUSH_TAG });
477
+ }
478
+ return {
479
+ focus: () => editorRef.current?.focus(),
480
+ insertReference: (reference) => transact(() => $insertComposerReference(reference)),
481
+ insertQuote: (quote) => transact(() => $insertComposerQuote(quote)),
482
+ claimCommand: (command, source) => transact(() => $claimComposerCommand(command, source)),
483
+ insertText: (text) => transact(() => {
484
+ const selection = $getSelection2();
485
+ ($isRangeSelection2(selection) ? selection : $getRoot2().selectEnd()).insertRawText(text);
486
+ }),
487
+ selection: () => editorRef.current?.getEditorState().read($composerSelection) ?? null,
488
+ applyProvider: (effect) => {
489
+ if (effect.request.documentRevision !== revision.current || effect.request.instanceGeneration !== latest.current.instanceGeneration)
490
+ return false;
491
+ let applied = false;
492
+ transact(() => {
493
+ applied = $applyComposerProvider(effect);
494
+ });
495
+ return applied;
496
+ }
497
+ };
498
+ }, []);
499
+ useLayoutEffect(() => {
500
+ const root = rootRef.current;
501
+ if (!root)
502
+ return;
503
+ let alive = true;
504
+ let initializationError;
505
+ const editor = createEditor({
506
+ namespace: "DuskMoonComposer",
507
+ nodes: [ComposerAtomNode],
508
+ editable: !latest.current.disabled,
509
+ onError: (error) => {
510
+ initializationError = error;
511
+ report(error);
512
+ }
513
+ });
514
+ editorRef.current = editor;
515
+ const history = createEmptyHistoryState();
516
+ historyRef.current = history;
517
+ function notifySelection() {
518
+ const selection = $composerSelection();
519
+ const signature = JSON.stringify(selection);
520
+ if (signature !== selectionSignature.current) {
521
+ selectionSignature.current = signature;
522
+ latest.current.onSelectionChange(selection);
523
+ }
524
+ }
525
+ const clipboard = (event, cut) => {
526
+ if (!event || !("clipboardData" in event) || !event.clipboardData || event.defaultPrevented)
527
+ return false;
528
+ const document2 = $composerClipboardDocument();
529
+ if (!document2)
530
+ return false;
531
+ event.clipboardData.setData(COMPOSER_CLIPBOARD_TYPE, JSON.stringify(document2));
532
+ event.clipboardData.setData("text/plain", exportComposerText(document2));
533
+ event.preventDefault();
534
+ if (cut && editor.isEditable()) {
535
+ editor.update(() => {
536
+ const selection = $getSelection2();
537
+ if ($isRangeSelection2(selection))
538
+ selection.removeText();
539
+ }, { tag: CUT_TAG });
540
+ }
541
+ return true;
542
+ };
543
+ const cleanup = mergeRegister(registerPlainText(editor), registerHistory(editor, history, 300), editor.registerNodeTransform(RootNode, $normalizeComposerCommands), editor.registerCommand(KEY_DOWN_COMMAND, (event) => event.defaultPrevented || composing.current || event.isComposing || event.keyCode === 229, COMMAND_PRIORITY_CRITICAL), editor.registerCommand(COPY_COMMAND, (event) => clipboard(event, false), COMMAND_PRIORITY_HIGH), editor.registerCommand(CUT_COMMAND, (event) => clipboard(event, true), COMMAND_PRIORITY_HIGH), editor.registerCommand(PASTE_COMMAND, (event) => {
544
+ if (event.defaultPrevented)
545
+ return true;
546
+ if (!("clipboardData" in event) || !event.clipboardData)
547
+ return false;
548
+ event.preventDefault();
549
+ if (!editor.isEditable() || composing.current || editor.isComposing())
550
+ return true;
551
+ const portable = event.clipboardData.getData(COMPOSER_CLIPBOARD_TYPE);
552
+ try {
553
+ if (portable) {
554
+ const document2 = readComposerDocument(JSON.parse(portable));
555
+ if (document2.status !== "compatible")
556
+ throw new TypeError(document2.reason);
557
+ editor.update(() => $insertComposerDocument(document2.value), {
558
+ tag: PASTE_TAG
559
+ });
560
+ } else {
561
+ const text = event.clipboardData.getData("text/plain");
562
+ if (text)
563
+ editor.update(() => {
564
+ const selection = $getSelection2();
565
+ if ($isRangeSelection2(selection))
566
+ selection.insertRawText(text);
567
+ }, { tag: PASTE_TAG });
568
+ }
569
+ } catch (cause) {
570
+ report(cause);
571
+ }
572
+ return true;
573
+ }, COMMAND_PRIORITY_HIGH), editor.registerCommand(SELECTION_CHANGE_COMMAND, () => {
574
+ notifySelection();
575
+ return false;
576
+ }, COMMAND_PRIORITY_HIGH), editor.registerUpdateListener(({ editorState, tags }) => {
577
+ if (!alive)
578
+ return;
579
+ editorState.read(() => {
580
+ const document2 = $exportComposerDocument();
581
+ const signature = JSON.stringify(document2);
582
+ root.dataset.empty = String(!document2.command && document2.content.every((node) => node.type === "text" && !node.text));
583
+ notifySelection();
584
+ if (signature === documentSignature.current)
585
+ return;
586
+ documentSignature.current = signature;
587
+ if (!ready.current || tags.has(EXTERNAL_REPLACE))
588
+ return;
589
+ const update = {
590
+ document: document2,
591
+ revision: Math.max(revision.current, latest.current.value.revision) + 1,
592
+ origin: "edit",
593
+ originId
594
+ };
595
+ revision.current = update.revision;
596
+ emitted.current = update;
597
+ latest.current.onChange(update);
598
+ });
599
+ }));
600
+ try {
601
+ editor.setRootElement(root);
602
+ const value = latest.current.value;
603
+ revision.current = value.revision;
604
+ editor.update(() => $replaceComposerDocument(value.document), {
605
+ discrete: true,
606
+ tag: EXTERNAL_REPLACE
607
+ });
608
+ if (initializationError)
609
+ throw initializationError;
610
+ editor.update(() => {
611
+ editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
612
+ }, { discrete: true });
613
+ history.current = { editor, editorState: editor.getEditorState() };
614
+ applied.current = {
615
+ originId: value.originId,
616
+ revision: value.revision,
617
+ generation: latest.current.instanceGeneration
618
+ };
619
+ ready.current = true;
620
+ latest.current.onReady?.(true);
621
+ } catch (cause) {
622
+ report(cause);
623
+ }
624
+ return () => {
625
+ alive = false;
626
+ ready.current = false;
627
+ composing.current = false;
628
+ cleanup();
629
+ editor.setRootElement(null);
630
+ if (editorRef.current === editor)
631
+ editorRef.current = null;
632
+ if (historyRef.current === history)
633
+ historyRef.current = null;
634
+ applied.current = undefined;
635
+ emitted.current = undefined;
636
+ selectionSignature.current = "";
637
+ documentSignature.current = "";
638
+ latest.current.onReady?.(false);
639
+ };
640
+ }, [originId]);
641
+ useLayoutEffect(() => {
642
+ const editor = editorRef.current;
643
+ if (!editor)
644
+ return;
645
+ editor.setEditable(!props.disabled);
646
+ const previous = applied.current;
647
+ const value = props.value;
648
+ const sameGeneration = previous?.generation === props.instanceGeneration;
649
+ if (sameGeneration && previous?.originId === value.originId && previous.revision === value.revision)
650
+ return;
651
+ if (sameGeneration && emitted.current?.originId === value.originId && emitted.current.revision === value.revision) {
652
+ applied.current = {
653
+ originId: value.originId,
654
+ revision: value.revision,
655
+ generation: props.instanceGeneration
656
+ };
657
+ return;
658
+ }
659
+ try {
660
+ revision.current = value.revision;
661
+ composing.current = false;
662
+ editor.update(() => $replaceComposerDocument(value.document), {
663
+ discrete: true,
664
+ tag: EXTERNAL_REPLACE
665
+ });
666
+ editor.update(() => {
667
+ editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
668
+ }, { discrete: true });
669
+ if (historyRef.current)
670
+ historyRef.current.current = {
671
+ editor,
672
+ editorState: editor.getEditorState()
673
+ };
674
+ emitted.current = undefined;
675
+ applied.current = {
676
+ originId: value.originId,
677
+ revision: value.revision,
678
+ generation: props.instanceGeneration
679
+ };
680
+ } catch (cause) {
681
+ report(cause);
682
+ }
683
+ }, [props.value, props.instanceGeneration, props.disabled]);
684
+ return /* @__PURE__ */ jsx("div", {
685
+ ref: rootRef,
686
+ id: props.id,
687
+ className: "chat-composer-editor",
688
+ role: "textbox",
689
+ tabIndex: 0,
690
+ "aria-multiline": "true",
691
+ "aria-label": props.label,
692
+ "aria-describedby": props.describedBy,
693
+ "aria-invalid": props.invalid || undefined,
694
+ "aria-controls": props.controls,
695
+ "aria-activedescendant": props.activeDescendant,
696
+ "aria-autocomplete": props.hasSuggestions ? "list" : undefined,
697
+ "aria-haspopup": props.hasSuggestions ? "listbox" : undefined,
698
+ "aria-disabled": props.disabled || undefined,
699
+ contentEditable: !props.disabled,
700
+ suppressContentEditableWarning: true,
701
+ onCompositionStartCapture: () => {
702
+ composing.current = true;
703
+ },
704
+ onCompositionEndCapture: () => {
705
+ composing.current = false;
706
+ },
707
+ onKeyDownCapture: (event) => {
708
+ if (event.defaultPrevented || composing.current || event.nativeEvent.isComposing || event.keyCode === 229 || editorRef.current?.isComposing())
709
+ return;
710
+ latest.current.onKeyDown?.(event);
711
+ },
712
+ onPasteCapture: (event) => {
713
+ if (!event.defaultPrevented)
714
+ latest.current.onPaste?.(event);
715
+ },
716
+ onDropCapture: (event) => {
717
+ if (!event.defaultPrevented)
718
+ latest.current.onDrop?.(event);
719
+ }
720
+ });
721
+ });
722
+ LexicalEditor.displayName = "RichChatComposer.Editor";
723
+ var LexicalEditor_default = LexicalEditor;
724
+ export {
725
+ COMPOSER_CLIPBOARD_TYPE,
726
+ LexicalEditor_default as default
727
+ };