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