@dxos/react-ui-markdown 0.8.4-main.c85a9c8dae → 0.8.4-main.d05539e30a

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 (47) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +563 -10
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/types/src/MarkdownStream/MarkdownStream.d.ts +101 -0
  6. package/dist/types/src/MarkdownStream/MarkdownStream.d.ts.map +1 -0
  7. package/dist/types/src/MarkdownStream/MarkdownStream.stories.d.ts +23 -0
  8. package/dist/types/src/MarkdownStream/MarkdownStream.stories.d.ts.map +1 -0
  9. package/dist/types/src/MarkdownStream/footer.d.ts +23 -0
  10. package/dist/types/src/MarkdownStream/footer.d.ts.map +1 -0
  11. package/dist/types/src/MarkdownStream/index.d.ts +4 -0
  12. package/dist/types/src/MarkdownStream/index.d.ts.map +1 -0
  13. package/dist/types/src/MarkdownStream/stream.d.ts +39 -0
  14. package/dist/types/src/MarkdownStream/stream.d.ts.map +1 -0
  15. package/dist/types/src/MarkdownStream/stream.test.d.ts +2 -0
  16. package/dist/types/src/MarkdownStream/stream.test.d.ts.map +1 -0
  17. package/dist/types/src/MarkdownStream/testing/index.d.ts +2 -0
  18. package/dist/types/src/MarkdownStream/testing/index.d.ts.map +1 -0
  19. package/dist/types/src/MarkdownStream/testing/testing.d.ts +16 -0
  20. package/dist/types/src/MarkdownStream/testing/testing.d.ts.map +1 -0
  21. package/dist/types/src/{MarkdownViewer/MarkdownViewer.d.ts → MarkdownView/MarkdownView.d.ts} +3 -3
  22. package/dist/types/src/MarkdownView/MarkdownView.d.ts.map +1 -0
  23. package/dist/types/src/{MarkdownViewer/MarkdownViewer.stories.d.ts → MarkdownView/MarkdownView.stories.d.ts} +4 -4
  24. package/dist/types/src/MarkdownView/MarkdownView.stories.d.ts.map +1 -0
  25. package/dist/types/src/MarkdownView/index.d.ts +2 -0
  26. package/dist/types/src/MarkdownView/index.d.ts.map +1 -0
  27. package/dist/types/src/index.d.ts +2 -1
  28. package/dist/types/src/index.d.ts.map +1 -1
  29. package/dist/types/tsconfig.tsbuildinfo +1 -1
  30. package/package.json +30 -26
  31. package/src/MarkdownStream/MarkdownStream.stories.tsx +215 -0
  32. package/src/MarkdownStream/MarkdownStream.tsx +442 -0
  33. package/src/MarkdownStream/footer.ts +119 -0
  34. package/src/MarkdownStream/index.ts +8 -0
  35. package/src/MarkdownStream/stream.test.ts +126 -0
  36. package/src/MarkdownStream/stream.ts +229 -0
  37. package/src/{MarkdownViewer → MarkdownStream/testing}/index.ts +1 -1
  38. package/src/MarkdownStream/testing/testing.ts +56 -0
  39. package/src/MarkdownStream/testing/text.md +67 -0
  40. package/src/{MarkdownViewer/MarkdownViewer.stories.tsx → MarkdownView/MarkdownView.stories.tsx} +10 -10
  41. package/src/{MarkdownViewer/MarkdownViewer.tsx → MarkdownView/MarkdownView.tsx} +31 -10
  42. package/src/MarkdownView/index.ts +5 -0
  43. package/src/index.ts +2 -1
  44. package/dist/types/src/MarkdownViewer/MarkdownViewer.d.ts.map +0 -1
  45. package/dist/types/src/MarkdownViewer/MarkdownViewer.stories.d.ts.map +0 -1
  46. package/dist/types/src/MarkdownViewer/index.d.ts +0 -2
  47. package/dist/types/src/MarkdownViewer/index.d.ts.map +0 -1
@@ -1,10 +1,11 @@
1
- // src/MarkdownViewer/MarkdownViewer.tsx
1
+ // src/MarkdownView/MarkdownView.tsx
2
2
  import React from "react";
3
3
  import ReactMarkdown from "react-markdown";
4
4
  import remarkGfm from "remark-gfm";
5
+ import { MediaPlayer } from "@dxos/react-ui";
5
6
  import { SyntaxHighlighter } from "@dxos/react-ui-syntax-highlighter";
6
7
  import { mx } from "@dxos/ui-theme";
7
- var MarkdownViewer = ({ classNames, children, components, content = "" }) => {
8
+ var MarkdownView = ({ classNames, children, components, content = "" }) => {
8
9
  return /* @__PURE__ */ React.createElement("div", {
9
10
  className: mx(classNames)
10
11
  }, /* @__PURE__ */ React.createElement(ReactMarkdown, {
@@ -21,21 +22,26 @@ var MarkdownViewer = ({ classNames, children, components, content = "" }) => {
21
22
  var defaultComponents = {
22
23
  h1: ({ children }) => {
23
24
  return /* @__PURE__ */ React.createElement("h1", {
24
- className: "pt-1 pb-1 text-xl"
25
+ className: "pt-1 pb-1 text-accent-text text-xl"
25
26
  }, children);
26
27
  },
27
28
  h2: ({ children }) => {
28
29
  return /* @__PURE__ */ React.createElement("h2", {
29
- className: "pt-1 pb-1 text-lg"
30
+ className: "pt-1 pb-1 text-accent-text text-lg"
30
31
  }, children);
31
32
  },
32
33
  h3: ({ children }) => {
33
34
  return /* @__PURE__ */ React.createElement("h3", {
34
- className: "pt-1 pb-1 text-base"
35
+ className: "pt-1 pb-1 text-accent-text text-base"
36
+ }, children);
37
+ },
38
+ h4: ({ children }) => {
39
+ return /* @__PURE__ */ React.createElement("h4", {
40
+ className: "pt-1 pb-1 uppercase text-base"
35
41
  }, children);
36
42
  },
37
43
  blockquote: ({ children, ...props }) => /* @__PURE__ */ React.createElement("blockquote", {
38
- className: "ps-4 mt-2 mb-2 pt-2 pb-2 border-l-4 border-accent-text text-accent-text",
44
+ className: "my-2 py-2 ps-4 border-l-4 border-accent-text text-accent-text",
39
45
  ...props
40
46
  }, children),
41
47
  p: ({ children }) => {
@@ -50,6 +56,24 @@ var defaultComponents = {
50
56
  rel: "noopener noreferrer",
51
57
  ...props
52
58
  }, children),
59
+ // Hide broken images: many markdown sources reference remote URLs that
60
+ // 404 or are blocked. Drop the element on load failure rather than
61
+ // leaving the browser's broken-image placeholder.
62
+ //
63
+ // Media URLs (mp4/mp3/etc. or legacy `iframe`-style embeds) are swapped to a
64
+ // native `<video>` / `<audio>` element by {@link MediaPlayer} so playable
65
+ // media in the source document renders inline.
66
+ img: ({ src, alt }) => {
67
+ if (!src) {
68
+ return null;
69
+ }
70
+ return /* @__PURE__ */ React.createElement(MediaPlayer, {
71
+ src,
72
+ alt,
73
+ mediaClassNames: "aspect-video w-full",
74
+ imgClassNames: "w-full"
75
+ });
76
+ },
53
77
  ol: ({ children, ...props }) => /* @__PURE__ */ React.createElement("ol", {
54
78
  className: "pt-1 pb-1 ps-6 leading-tight list-decimal",
55
79
  ...props
@@ -63,17 +87,546 @@ var defaultComponents = {
63
87
  ...props
64
88
  }, children),
65
89
  pre: ({ children }) => children,
66
- // TODO(burdon): Copy/paste button.
67
- code: ({ children, className }) => {
90
+ code: ({ children, className, node }) => {
68
91
  const [, language] = /language-(\w+)/.exec(className || "") || [];
92
+ const inline = !className && node?.position?.start.line === node?.position?.end.line;
93
+ if (inline) {
94
+ return /* @__PURE__ */ React.createElement("code", {
95
+ className: "rounded-xs bg-group-surface px-1 py-0.5 text-sm text-info-text"
96
+ }, children);
97
+ }
69
98
  return /* @__PURE__ */ React.createElement(SyntaxHighlighter, {
70
99
  language,
71
- classNames: "mt-2 mb-2 border border-separator rounded-xs text-sm bg-group-surface",
100
+ classNames: "mt-2 mb-2 p-2 border border-separator rounded-xs text-sm bg-group-surface",
101
+ copyButton: true,
72
102
  PreTag: "pre"
73
103
  }, children);
74
104
  }
75
105
  };
106
+
107
+ // src/MarkdownStream/stream.ts
108
+ import * as Effect from "effect/Effect";
109
+ import * as Stream from "effect/Stream";
110
+ import { Obj } from "@dxos/echo";
111
+ var renderObjectLink = (obj, block) => `${block ? "!" : ""}[${Obj.getLabel(obj)}](${Obj.getDXN(obj).toString()})`;
112
+ var createStreamer = (source, { chunkSize = "span", delayMs = 0 } = {}) => {
113
+ const subdivide = chunkSize === "span" ? (token) => [
114
+ token
115
+ ] : (token) => isXmlFragment(token) ? [
116
+ token
117
+ ] : splitTextSpan(token, chunkSize);
118
+ let stream = source.pipe(Stream.flatMap((chunk) => Stream.fromIterable(splitFragments(chunk).flatMap(subdivide))));
119
+ if (delayMs > 0) {
120
+ stream = stream.pipe(Stream.tap(() => Effect.sleep(`${delayMs} millis`)));
121
+ }
122
+ return stream;
123
+ };
124
+ var isXmlFragment = (token) => token.startsWith("<");
125
+ var splitTextSpan = (span, chunkSize) => {
126
+ if (chunkSize === "character") {
127
+ return [
128
+ ...span
129
+ ];
130
+ }
131
+ return span.match(/\s+|\S+/g) ?? [
132
+ span
133
+ ];
134
+ };
135
+ var OPENING_TAG_NAME = /^<([a-zA-Z][\w-]*)(?:\s[^>]*)?>/;
136
+ var splitFragments = (text) => {
137
+ const initialTokens = splitSpans(text);
138
+ const tokens = [];
139
+ let i = 0;
140
+ while (i < initialTokens.length) {
141
+ const token = initialTokens[i];
142
+ if (token.startsWith("<") && !token.startsWith("</") && !token.endsWith("/>")) {
143
+ const tagMatch = token.match(OPENING_TAG_NAME);
144
+ if (tagMatch) {
145
+ const tagName = tagMatch[1];
146
+ const closingTag = `</${tagName}>`;
147
+ let fragment = token;
148
+ let foundClosing = false;
149
+ let j = i + 1;
150
+ while (j < initialTokens.length) {
151
+ fragment += initialTokens[j];
152
+ if (initialTokens[j] === closingTag) {
153
+ foundClosing = true;
154
+ break;
155
+ }
156
+ j++;
157
+ }
158
+ if (foundClosing) {
159
+ tokens.push(fragment);
160
+ i = j + 1;
161
+ } else {
162
+ tokens.push(token);
163
+ i++;
164
+ }
165
+ } else {
166
+ tokens.push(token);
167
+ i++;
168
+ }
169
+ } else {
170
+ tokens.push(token);
171
+ i++;
172
+ }
173
+ }
174
+ return tokens;
175
+ };
176
+ var splitSpans = (text) => {
177
+ const spans = [];
178
+ let currentText = "";
179
+ let i = 0;
180
+ while (i < text.length) {
181
+ if (text[i] === "<") {
182
+ if (currentText) {
183
+ spans.push(currentText);
184
+ currentText = "";
185
+ }
186
+ const closeIndex = text.indexOf(">", i);
187
+ if (closeIndex !== -1) {
188
+ spans.push(text.slice(i, closeIndex + 1));
189
+ i = closeIndex + 1;
190
+ } else {
191
+ currentText = text.slice(i);
192
+ break;
193
+ }
194
+ } else {
195
+ currentText += text[i];
196
+ i++;
197
+ }
198
+ }
199
+ if (currentText) {
200
+ spans.push(currentText);
201
+ }
202
+ return spans;
203
+ };
204
+ var splitSentences = (text) => {
205
+ const sentenceRegex = /[^.!?]*[.!?]+(?:\s+|$)/g;
206
+ const sentences = [];
207
+ let lastIndex = 0;
208
+ let match;
209
+ while ((match = sentenceRegex.exec(text)) !== null) {
210
+ sentences.push(match[0]);
211
+ lastIndex = match.index + match[0].length;
212
+ }
213
+ if (lastIndex < text.length) {
214
+ const remaining = text.slice(lastIndex);
215
+ if (remaining) {
216
+ sentences.push(remaining);
217
+ }
218
+ }
219
+ if (sentences.length === 0 && text) {
220
+ sentences.push(text);
221
+ }
222
+ return sentences;
223
+ };
224
+
225
+ // src/MarkdownStream/testing/testing.ts
226
+ async function* textStream(text, options = {}) {
227
+ const { chunkDelay = 100, variance = 0.3, wordsPerChunk = 3 } = options;
228
+ const words = text.match(/\S+|\s+/g) || [];
229
+ let i = 0;
230
+ while (i < words.length) {
231
+ const chunkWords = [];
232
+ let wordCount = 0;
233
+ while (i < words.length && wordCount < wordsPerChunk) {
234
+ const word = words[i];
235
+ chunkWords.push(word);
236
+ if (word.trim()) {
237
+ wordCount++;
238
+ }
239
+ i++;
240
+ }
241
+ const chunk = chunkWords.join("");
242
+ yield chunk;
243
+ const varianceMultiplier = 1 + (Math.random() - 0.5) * variance * 2;
244
+ const delay = chunkDelay * varianceMultiplier;
245
+ await new Promise((resolve) => setTimeout(resolve, delay));
246
+ }
247
+ }
248
+
249
+ // src/MarkdownStream/MarkdownStream.tsx
250
+ import { EditorSelection, Transaction } from "@codemirror/state";
251
+ import * as Effect2 from "effect/Effect";
252
+ import * as Fiber from "effect/Fiber";
253
+ import * as Queue from "effect/Queue";
254
+ import * as Stream2 from "effect/Stream";
255
+ import React2, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react";
256
+ import { createPortal } from "react-dom";
257
+ import { addEventListener } from "@dxos/async";
258
+ import { runAndForwardErrors } from "@dxos/effect";
259
+ import { ErrorBoundary, useDynamicRef, useStateWithRef, useThemeContext } from "@dxos/react-ui";
260
+ import { useTextEditor } from "@dxos/react-ui-editor";
261
+ import { createBasicExtensions, createThemeExtensions, decorateMarkdown, extendedMarkdown, navigateNextEffect, navigatePreviousEffect, preview, scroller, crawlerLineEffect, fader, typewriter, typewriterBypass, xmlTagContextEffect, xmlTagResetEffect, xmlTagUpdateEffect, xmlTags, documentSlots, xmlFormatting, xmlBlockDecoration } from "@dxos/ui-editor";
262
+ import { mx as mx2 } from "@dxos/ui-theme";
263
+ import { isTruthy } from "@dxos/util";
264
+
265
+ // src/MarkdownStream/footer.ts
266
+ import { StateEffect, StateField } from "@codemirror/state";
267
+ import { Decoration, EditorView, WidgetType } from "@codemirror/view";
268
+ import { Domino } from "@dxos/ui";
269
+ import { typewriterDrainingEffect } from "@dxos/ui-editor";
270
+ var setFooterVisibleEffect = StateEffect.define();
271
+ var footer = (setRoot) => {
272
+ const widget = new FooterWidget(setRoot);
273
+ const buildSet = (length) => Decoration.set([
274
+ Decoration.widget({
275
+ widget,
276
+ block: true,
277
+ side: 1
278
+ }).range(length)
279
+ ]);
280
+ const field = StateField.define({
281
+ create: () => ({
282
+ wanted: false,
283
+ draining: false,
284
+ decorations: Decoration.none
285
+ }),
286
+ update: (state, tr) => {
287
+ let { wanted, draining, decorations } = state;
288
+ for (const effect of tr.effects) {
289
+ if (effect.is(setFooterVisibleEffect)) {
290
+ wanted = effect.value;
291
+ }
292
+ if (effect.is(typewriterDrainingEffect)) {
293
+ draining = effect.value;
294
+ }
295
+ }
296
+ const docLength = tr.state.doc.length;
297
+ const visible = wanted && !draining && docLength > 0;
298
+ const wasVisible = decorations.size > 0;
299
+ if (visible !== wasVisible) {
300
+ decorations = visible ? buildSet(docLength) : Decoration.none;
301
+ } else if (tr.docChanged && decorations.size > 0) {
302
+ decorations = decorations.map(tr.changes);
303
+ }
304
+ return {
305
+ wanted,
306
+ draining,
307
+ decorations
308
+ };
309
+ },
310
+ provide: (f) => EditorView.decorations.from(f, (state) => state.decorations)
311
+ });
312
+ return [
313
+ field
314
+ ];
315
+ };
316
+ var FooterWidget = class extends WidgetType {
317
+ _setRoot;
318
+ constructor(_setRoot) {
319
+ super(), this._setRoot = _setRoot;
320
+ }
321
+ // Singleton equality so CM keeps the same DOM element across decoration rebuilds —
322
+ // the React subtree portaled into it is not unmounted on every doc change.
323
+ eq(_other) {
324
+ return true;
325
+ }
326
+ ignoreEvent() {
327
+ return true;
328
+ }
329
+ toDOM() {
330
+ const inner = Domino.of("div").classNames("cm-stream-footer-content").style({
331
+ position: "absolute",
332
+ left: "0",
333
+ top: "0"
334
+ });
335
+ const el = Domino.of("div").classNames("cm-stream-footer").style({
336
+ position: "relative",
337
+ height: "0"
338
+ }).append(inner);
339
+ this._setRoot(inner.root);
340
+ return el.root;
341
+ }
342
+ destroy() {
343
+ this._setRoot(null);
344
+ }
345
+ };
346
+
347
+ // src/MarkdownStream/MarkdownStream.tsx
348
+ var MarkdownStream = /* @__PURE__ */ forwardRef(({ classNames, debug, content, options, registry, extensions, footer: footer2, onEvent }, forwardedRef) => {
349
+ const contentRef = useRef(content ?? "");
350
+ const [footerRoot, setFooterRoot] = useState(null);
351
+ const { parentRef, view, viewRef, widgets } = useMarkdownStreamTextEditor(contentRef, {
352
+ debug,
353
+ registry,
354
+ options,
355
+ extensions,
356
+ setFooterRoot: footer2 ? setFooterRoot : void 0
357
+ });
358
+ const footerVisible = !!footer2;
359
+ useEffect(() => {
360
+ view?.dispatch({
361
+ effects: setFooterVisibleEffect.of(footerVisible)
362
+ });
363
+ }, [
364
+ view,
365
+ footerVisible
366
+ ]);
367
+ const [queue, setQueue, queueRef] = useStateWithRef(Effect2.runSync(Queue.unbounded()));
368
+ const onReset = useCallback(async (text) => {
369
+ contentRef.current = text;
370
+ if (!viewRef.current) {
371
+ return;
372
+ }
373
+ viewRef.current.dispatch({
374
+ effects: [
375
+ xmlTagContextEffect.of(null),
376
+ xmlTagResetEffect.of(null)
377
+ ],
378
+ changes: [
379
+ {
380
+ from: 0,
381
+ to: viewRef.current.state.doc.length,
382
+ insert: text
383
+ }
384
+ ],
385
+ annotations: typewriterBypass.of(true),
386
+ selection: EditorSelection.cursor(text.length)
387
+ });
388
+ setQueue(Effect2.runSync(Queue.unbounded()));
389
+ }, [
390
+ contentRef,
391
+ viewRef,
392
+ setQueue
393
+ ]);
394
+ useImperativeHandle(forwardedRef, () => createMarkdownStreamController({
395
+ contentRef,
396
+ viewRef,
397
+ queueRef,
398
+ onReset
399
+ }), [
400
+ onReset
401
+ ]);
402
+ useEffect(() => {
403
+ if (!parentRef.current) {
404
+ return;
405
+ }
406
+ return addEventListener(parentRef.current, "click", (event) => {
407
+ const button = event.target.closest('[data-action="submit"]');
408
+ if (button?.getAttribute("data-action") === "submit") {
409
+ onEvent?.({
410
+ type: "submit",
411
+ value: button.getAttribute("data-value")
412
+ });
413
+ }
414
+ });
415
+ }, [
416
+ view,
417
+ parentRef,
418
+ onEvent
419
+ ]);
420
+ useMarkdownStreamQueue(view, queue, {
421
+ chunkSize: options?.streamCadence,
422
+ delayMs: options?.streamDelayMs
423
+ });
424
+ useEffect(() => {
425
+ return () => {
426
+ view?.destroy();
427
+ };
428
+ }, [
429
+ view
430
+ ]);
431
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement("div", {
432
+ className: mx2("dx-container", classNames),
433
+ ref: parentRef
434
+ }), /* @__PURE__ */ React2.createElement(ErrorBoundary, {
435
+ name: "markdown-stream"
436
+ }, widgets.map(({ Component, root, id, props }) => /* @__PURE__ */ React2.createElement("div", {
437
+ key: id
438
+ }, /* @__PURE__ */ createPortal(/* @__PURE__ */ React2.createElement(Component, {
439
+ view,
440
+ ...props
441
+ }), root))), footerRoot && footerVisible && /* @__PURE__ */ createPortal(footer2, footerRoot)));
442
+ });
443
+ var useMarkdownStreamTextEditor = (currentContent, { debug, registry, options, extensions: extraExtensions, setFooterRoot }) => {
444
+ const { themeMode } = useThemeContext();
445
+ const [widgets, setWidgets] = useState([]);
446
+ const { view, parentRef } = useTextEditor(() => {
447
+ const content = currentContent.current;
448
+ return {
449
+ initialValue: content,
450
+ selection: EditorSelection.cursor(content?.length ?? 0),
451
+ extensions: [
452
+ createBasicExtensions({
453
+ lineWrapping: true,
454
+ readOnly: true
455
+ }),
456
+ createThemeExtensions({
457
+ slots: documentSlots,
458
+ scrollbarThin: true,
459
+ syntaxHighlighting: true,
460
+ themeMode
461
+ }),
462
+ xmlFormatting({
463
+ skip: debug ? [] : [
464
+ "prompt"
465
+ ]
466
+ }),
467
+ !debug && [
468
+ extendedMarkdown({
469
+ registry
470
+ }),
471
+ decorateMarkdown({
472
+ // `dxn:` links/images are reference widgets owned by `preview()` (PreviewInlineWidget /
473
+ // PreviewBlockWidget). Skipping them here avoids `decorateMarkdown` adding a
474
+ // non-functional `LinkButton` anchor on top of the same node — e.g. for
475
+ // `[DXOS](dxn:echo:BNPMIBEDJLRIILYUYZVM6GT64VWI6WPPZ:01KQ889PZBRNHAEECV0ANFAYX7)`.
476
+ skip: (node) => (node.name === "Link" || node.name === "Image") && node.url.startsWith("dxn:")
477
+ }),
478
+ preview(),
479
+ // NOTE: An ancestor element must set `data-hue` so `.dx-panel` resolves to the user's
480
+ // hue tokens (see `packages/ui/ui-theme/src/css/components/panel.css`). Tailwind picks
481
+ // up these utility classes from this source file.
482
+ xmlBlockDecoration({
483
+ tag: "prompt",
484
+ lineClass: "cm-prompt-line my-8",
485
+ contentClass: "cm-prompt-bubble dx-panel px-2 py-1.5 rounded-sm [&_*]:text-inherit!",
486
+ hideTags: true
487
+ }),
488
+ xmlTags({
489
+ registry,
490
+ setWidgets,
491
+ bookmarks: [
492
+ "prompt"
493
+ ]
494
+ }),
495
+ scroller({
496
+ overScroll: 80,
497
+ autoScroll: options?.autoScroll
498
+ }),
499
+ options?.typewriter && typewriter({
500
+ cursor: options?.cursor,
501
+ streamingTags: new Set(Object.entries(registry ?? {}).filter(([, def]) => def.streaming).map(([tag]) => tag))
502
+ }),
503
+ options?.fader && fader(),
504
+ setFooterRoot && footer(setFooterRoot)
505
+ ].filter(isTruthy),
506
+ extraExtensions
507
+ ].filter(isTruthy)
508
+ };
509
+ }, [
510
+ themeMode,
511
+ registry,
512
+ debug,
513
+ options?.autoScroll,
514
+ options?.typewriter,
515
+ options?.cursor,
516
+ options?.fader,
517
+ extraExtensions
518
+ ]);
519
+ const viewRef = useDynamicRef(view);
520
+ return {
521
+ view,
522
+ viewRef,
523
+ parentRef,
524
+ widgets
525
+ };
526
+ };
527
+ var useMarkdownStreamQueue = (view, queue, streamerOptions) => {
528
+ const chunkSize = streamerOptions?.chunkSize;
529
+ const delayMs = streamerOptions?.delayMs;
530
+ useEffect(() => {
531
+ if (!view) {
532
+ return;
533
+ }
534
+ const fork = Stream2.fromQueue(queue).pipe((source) => createStreamer(source, {
535
+ chunkSize,
536
+ delayMs
537
+ }), Stream2.runForEach((text) => Effect2.sync(() => {
538
+ const scrollTop = view.scrollDOM.scrollTop;
539
+ view.dispatch({
540
+ changes: [
541
+ {
542
+ from: view.state.doc.length,
543
+ insert: text
544
+ }
545
+ ],
546
+ annotations: Transaction.remote.of(true),
547
+ scrollIntoView: false
548
+ });
549
+ requestAnimationFrame(() => {
550
+ view.scrollDOM.scrollTop = scrollTop;
551
+ });
552
+ })), Effect2.runFork);
553
+ return () => {
554
+ void runAndForwardErrors(Fiber.interrupt(fork));
555
+ };
556
+ }, [
557
+ view,
558
+ queue,
559
+ chunkSize,
560
+ delayMs
561
+ ]);
562
+ };
563
+ var createMarkdownStreamController = ({ contentRef, viewRef, queueRef, onReset }) => {
564
+ return {
565
+ get length() {
566
+ return viewRef.current?.state.doc.length;
567
+ },
568
+ /** Focus the editor. */
569
+ focus: () => {
570
+ viewRef.current?.focus();
571
+ },
572
+ /** Scroll to bottom. */
573
+ scrollToBottom: (behavior) => {
574
+ viewRef.current?.dispatch({
575
+ effects: crawlerLineEffect.of({
576
+ line: -1,
577
+ behavior
578
+ })
579
+ });
580
+ },
581
+ /** Navigate previous prompt. */
582
+ navigatePrevious: () => {
583
+ viewRef.current?.dispatch({
584
+ effects: navigatePreviousEffect.of()
585
+ });
586
+ },
587
+ /** Navigate next prompt. */
588
+ navigateNext: () => {
589
+ viewRef.current?.dispatch({
590
+ effects: navigateNextEffect.of()
591
+ });
592
+ },
593
+ /** Set the context for widgets (XML tags). */
594
+ setContext: (context) => {
595
+ viewRef.current?.dispatch({
596
+ effects: xmlTagContextEffect.of(context)
597
+ });
598
+ },
599
+ /** Reset document. */
600
+ setContent: onReset,
601
+ /** Append to queue (and stream). */
602
+ append: async (text) => {
603
+ contentRef.current += text;
604
+ if (text.length) {
605
+ const queue = queueRef.current;
606
+ if (queue) {
607
+ await runAndForwardErrors(Queue.offer(queue, text));
608
+ }
609
+ }
610
+ },
611
+ /** Update widget state. */
612
+ updateWidget: (id, value) => {
613
+ viewRef.current?.dispatch({
614
+ effects: xmlTagUpdateEffect.of({
615
+ id,
616
+ value
617
+ })
618
+ });
619
+ }
620
+ };
621
+ };
76
622
  export {
77
- MarkdownViewer
623
+ MarkdownStream,
624
+ MarkdownView,
625
+ createStreamer,
626
+ renderObjectLink,
627
+ splitFragments,
628
+ splitSentences,
629
+ splitSpans,
630
+ textStream
78
631
  };
79
632
  //# sourceMappingURL=index.mjs.map