devnonla-ui 0.9.1 → 0.11.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.
- package/README.md +57 -3
- package/package.json +3 -1
- package/src/avatar/AgentAvatar.tsx +205 -0
- package/src/chat/index.ts +2 -4
- package/src/chat/message-ui/ChatAgentMessage.tsx +2 -6
- package/src/chat/message-ui/ChatMarkdown.tsx +4 -122
- package/src/chat/message-ui/ChatWelcome.tsx +3 -23
- package/src/chat/tool-ui/CallAgentToolUI.tsx +1 -1
- package/src/index.ts +21 -9
- package/src/logo/Logo.tsx +277 -0
- package/src/markdown-editor/MarkdownEditor.tsx +2 -1
- package/src/markdown-editor/MarkdownPreview.tsx +3 -195
- package/src/{chat/message-ui/ChatMarkdownTable.tsx → markdown-editor/MarkdownTable.tsx} +3 -3
- package/src/markdown-editor/MarkdownViewer.tsx +264 -0
- package/src/{chat/message-ui → markdown-editor}/MermaidBlock.tsx +8 -8
- package/src/markdown-editor/ReactCode.tsx +75 -0
- package/src/markdown-editor/ReactCodeFrame.tsx +46 -0
- package/src/markdown-editor/ReactCodeRunner.tsx +73 -0
- package/src/markdown-editor/ReactCodeSandbox.tsx +111 -0
- package/src/markdown-editor/reactCodeProtocol.ts +47 -0
- package/src/markdown-editor/reactCompile.ts +31 -0
- package/src/styles.css +119 -12
- package/src/theme-switcher/ThemeSwitcher.tsx +55 -0
- package/src/theme.ts +86 -14
- package/src/typography/Typography.tsx +158 -0
- package/src/chat/message-ui/mermaidFence.ts +0 -55
- /package/src/{chat/message-ui → markdown-editor}/sanitizeMermaid.ts +0 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import "@fontsource-variable/plus-jakarta-sans/wght.css";
|
|
2
|
+
import type { CSSProperties } from "react";
|
|
3
|
+
import { cn } from "../lib/cn";
|
|
4
|
+
|
|
5
|
+
/** `icon` is the mark. `text` is the wordmark. `full` is both. */
|
|
6
|
+
export type LogoVariant = "icon" | "text" | "full";
|
|
7
|
+
|
|
8
|
+
/** `pixel` is the grid. `jakarta` is Plus Jakarta Sans. */
|
|
9
|
+
export type LogoFace = "pixel" | "jakarta";
|
|
10
|
+
|
|
11
|
+
export type LogoProps = {
|
|
12
|
+
variant?: LogoVariant;
|
|
13
|
+
/** Wordmark face. Ignored when `variant` is `icon`. Defaults to `pixel`. */
|
|
14
|
+
face?: LogoFace;
|
|
15
|
+
/** Icon height in px. Width follows each asset's aspect ratio. Defaults to 32. */
|
|
16
|
+
size?: number;
|
|
17
|
+
/** Icon and wordmark fill. Eyes and the smile stay. Omit to keep the mascot palette and the current text color. */
|
|
18
|
+
color?: string;
|
|
19
|
+
className?: string;
|
|
20
|
+
style?: CSSProperties;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const WORDMARK = "Nonla Agents";
|
|
24
|
+
|
|
25
|
+
/** viewBox `100 40 1336 960`. */
|
|
26
|
+
const MARK_W = 1336;
|
|
27
|
+
const MARK_H = 960;
|
|
28
|
+
|
|
29
|
+
/** Pixel grid wordmark. 11 rows; cap letters use 7, so the box is taller than the caps. */
|
|
30
|
+
const WORD_W = 1340;
|
|
31
|
+
const WORD_H = 220;
|
|
32
|
+
const WORD_CELLS: ReadonlyArray<readonly [number, number]> = [
|
|
33
|
+
[2.4, 2.4],
|
|
34
|
+
[82.4, 2.4],
|
|
35
|
+
[2.4, 22.4],
|
|
36
|
+
[22.4, 22.4],
|
|
37
|
+
[82.4, 22.4],
|
|
38
|
+
[2.4, 42.4],
|
|
39
|
+
[22.4, 42.4],
|
|
40
|
+
[82.4, 42.4],
|
|
41
|
+
[2.4, 62.4],
|
|
42
|
+
[42.4, 62.4],
|
|
43
|
+
[82.4, 62.4],
|
|
44
|
+
[2.4, 82.4],
|
|
45
|
+
[62.4, 82.4],
|
|
46
|
+
[82.4, 82.4],
|
|
47
|
+
[2.4, 102.4],
|
|
48
|
+
[62.4, 102.4],
|
|
49
|
+
[82.4, 102.4],
|
|
50
|
+
[2.4, 122.4],
|
|
51
|
+
[82.4, 122.4],
|
|
52
|
+
[142.4, 42.4],
|
|
53
|
+
[162.4, 42.4],
|
|
54
|
+
[182.4, 42.4],
|
|
55
|
+
[122.4, 62.4],
|
|
56
|
+
[202.4, 62.4],
|
|
57
|
+
[122.4, 82.4],
|
|
58
|
+
[202.4, 82.4],
|
|
59
|
+
[122.4, 102.4],
|
|
60
|
+
[202.4, 102.4],
|
|
61
|
+
[142.4, 122.4],
|
|
62
|
+
[162.4, 122.4],
|
|
63
|
+
[182.4, 122.4],
|
|
64
|
+
[242.4, 42.4],
|
|
65
|
+
[262.4, 42.4],
|
|
66
|
+
[282.4, 42.4],
|
|
67
|
+
[302.4, 42.4],
|
|
68
|
+
[242.4, 62.4],
|
|
69
|
+
[322.4, 62.4],
|
|
70
|
+
[242.4, 82.4],
|
|
71
|
+
[322.4, 82.4],
|
|
72
|
+
[242.4, 102.4],
|
|
73
|
+
[322.4, 102.4],
|
|
74
|
+
[242.4, 122.4],
|
|
75
|
+
[322.4, 122.4],
|
|
76
|
+
[362.4, 2.4],
|
|
77
|
+
[382.4, 2.4],
|
|
78
|
+
[382.4, 22.4],
|
|
79
|
+
[382.4, 42.4],
|
|
80
|
+
[382.4, 62.4],
|
|
81
|
+
[382.4, 82.4],
|
|
82
|
+
[382.4, 102.4],
|
|
83
|
+
[382.4, 122.4],
|
|
84
|
+
[402.4, 122.4],
|
|
85
|
+
[462.4, 42.4],
|
|
86
|
+
[482.4, 42.4],
|
|
87
|
+
[502.4, 42.4],
|
|
88
|
+
[522.4, 62.4],
|
|
89
|
+
[462.4, 82.4],
|
|
90
|
+
[482.4, 82.4],
|
|
91
|
+
[502.4, 82.4],
|
|
92
|
+
[522.4, 82.4],
|
|
93
|
+
[442.4, 102.4],
|
|
94
|
+
[522.4, 102.4],
|
|
95
|
+
[462.4, 122.4],
|
|
96
|
+
[482.4, 122.4],
|
|
97
|
+
[502.4, 122.4],
|
|
98
|
+
[522.4, 122.4],
|
|
99
|
+
[662.4, 2.4],
|
|
100
|
+
[642.4, 22.4],
|
|
101
|
+
[682.4, 22.4],
|
|
102
|
+
[622.4, 42.4],
|
|
103
|
+
[702.4, 42.4],
|
|
104
|
+
[622.4, 62.4],
|
|
105
|
+
[702.4, 62.4],
|
|
106
|
+
[622.4, 82.4],
|
|
107
|
+
[642.4, 82.4],
|
|
108
|
+
[662.4, 82.4],
|
|
109
|
+
[682.4, 82.4],
|
|
110
|
+
[702.4, 82.4],
|
|
111
|
+
[622.4, 102.4],
|
|
112
|
+
[702.4, 102.4],
|
|
113
|
+
[622.4, 122.4],
|
|
114
|
+
[702.4, 122.4],
|
|
115
|
+
[762.4, 42.4],
|
|
116
|
+
[782.4, 42.4],
|
|
117
|
+
[802.4, 42.4],
|
|
118
|
+
[822.4, 42.4],
|
|
119
|
+
[742.4, 62.4],
|
|
120
|
+
[822.4, 62.4],
|
|
121
|
+
[742.4, 82.4],
|
|
122
|
+
[822.4, 82.4],
|
|
123
|
+
[742.4, 102.4],
|
|
124
|
+
[822.4, 102.4],
|
|
125
|
+
[762.4, 122.4],
|
|
126
|
+
[782.4, 122.4],
|
|
127
|
+
[802.4, 122.4],
|
|
128
|
+
[822.4, 122.4],
|
|
129
|
+
[822.4, 142.4],
|
|
130
|
+
[762.4, 162.4],
|
|
131
|
+
[782.4, 162.4],
|
|
132
|
+
[802.4, 162.4],
|
|
133
|
+
[882.4, 42.4],
|
|
134
|
+
[902.4, 42.4],
|
|
135
|
+
[922.4, 42.4],
|
|
136
|
+
[862.4, 62.4],
|
|
137
|
+
[942.4, 62.4],
|
|
138
|
+
[862.4, 82.4],
|
|
139
|
+
[882.4, 82.4],
|
|
140
|
+
[902.4, 82.4],
|
|
141
|
+
[922.4, 82.4],
|
|
142
|
+
[942.4, 82.4],
|
|
143
|
+
[862.4, 102.4],
|
|
144
|
+
[882.4, 122.4],
|
|
145
|
+
[902.4, 122.4],
|
|
146
|
+
[922.4, 122.4],
|
|
147
|
+
[942.4, 122.4],
|
|
148
|
+
[982.4, 42.4],
|
|
149
|
+
[1002.4, 42.4],
|
|
150
|
+
[1022.4, 42.4],
|
|
151
|
+
[1042.4, 42.4],
|
|
152
|
+
[982.4, 62.4],
|
|
153
|
+
[1062.4, 62.4],
|
|
154
|
+
[982.4, 82.4],
|
|
155
|
+
[1062.4, 82.4],
|
|
156
|
+
[982.4, 102.4],
|
|
157
|
+
[1062.4, 102.4],
|
|
158
|
+
[982.4, 122.4],
|
|
159
|
+
[1062.4, 122.4],
|
|
160
|
+
[1122.4, 22.4],
|
|
161
|
+
[1102.4, 42.4],
|
|
162
|
+
[1122.4, 42.4],
|
|
163
|
+
[1142.4, 42.4],
|
|
164
|
+
[1122.4, 62.4],
|
|
165
|
+
[1122.4, 82.4],
|
|
166
|
+
[1122.4, 102.4],
|
|
167
|
+
[1142.4, 122.4],
|
|
168
|
+
[1162.4, 122.4],
|
|
169
|
+
[1222.4, 42.4],
|
|
170
|
+
[1242.4, 42.4],
|
|
171
|
+
[1262.4, 42.4],
|
|
172
|
+
[1282.4, 42.4],
|
|
173
|
+
[1202.4, 62.4],
|
|
174
|
+
[1222.4, 82.4],
|
|
175
|
+
[1242.4, 82.4],
|
|
176
|
+
[1262.4, 82.4],
|
|
177
|
+
[1282.4, 102.4],
|
|
178
|
+
[1202.4, 122.4],
|
|
179
|
+
[1222.4, 122.4],
|
|
180
|
+
[1242.4, 122.4],
|
|
181
|
+
[1262.4, 122.4],
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
/** One paint color, split so the brim stays darker and the face stays lighter than the hat. */
|
|
185
|
+
function markTones(color?: string) {
|
|
186
|
+
if (!color) return { brim: "#dd7627", face: "#ffb45c", hat: "#ffa333" };
|
|
187
|
+
return {
|
|
188
|
+
brim: `color-mix(in srgb, ${color} 70%, black)`,
|
|
189
|
+
face: `color-mix(in srgb, ${color} 75%, white)`,
|
|
190
|
+
hat: color,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Eyes and the smile stay ink and white. */
|
|
195
|
+
function LogoMark({ height, color }: { height: number; color?: string }) {
|
|
196
|
+
const tone = markTones(color);
|
|
197
|
+
return (
|
|
198
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="100 40 1336 960" width={Math.round(height * (MARK_W / MARK_H))} height={height} aria-hidden className="shrink-0">
|
|
199
|
+
<ellipse fill={tone.brim} cx="767" cy="640" rx="666" ry="190" />
|
|
200
|
+
<circle fill={tone.face} cx="768" cy="602" r="371.6" />
|
|
201
|
+
<path fill="none" stroke={tone.hat} strokeWidth="6" d="M112 640 A655 108 0 0 1 1422 640" />
|
|
202
|
+
<path fill="none" stroke={tone.brim} strokeWidth="20" d="M112 640 A655 106 0 0 1 1422 640" />
|
|
203
|
+
<path
|
|
204
|
+
fill={tone.hat}
|
|
205
|
+
d="M768 68 C752 68 640 145 439 300 C338 377 250 449 194 500 C172 519 150 540 134 560 C122 576 109 591 103 600 C99 613 100 628 101 640 A666 108 0 0 1 1433 640 C1434 628 1435 613 1431 600 C1425 591 1412 576 1400 560 C1384 540 1362 519 1340 500 C1284 449 1196 377 1095 300 C894 145 782 68 768 68Z"
|
|
206
|
+
/>
|
|
207
|
+
<g fill="none" stroke={tone.brim} strokeWidth="6" strokeLinecap="round" opacity="0.55">
|
|
208
|
+
<path d="M750 96 C625 260 455 421 282 563" />
|
|
209
|
+
<path d="M758 96 C700 241 644 377 588 507" />
|
|
210
|
+
<path d="M778 96 C836 241 892 377 948 507" />
|
|
211
|
+
<path d="M786 96 C911 260 1081 421 1254 563" />
|
|
212
|
+
</g>
|
|
213
|
+
<ellipse fill="#121212" cx="607.5" cy="705" rx="44.5" ry="68" />
|
|
214
|
+
<ellipse fill="#121212" cx="921.5" cy="705" rx="44.5" ry="68" />
|
|
215
|
+
<ellipse fill="#ffffff" cx="618" cy="678" rx="13" ry="19" />
|
|
216
|
+
<ellipse fill="#ffffff" cx="932" cy="678" rx="13" ry="19" />
|
|
217
|
+
<path fill="none" stroke="#121212" strokeWidth="24" strokeLinecap="round" d="M696 802 C733 846 801 846 837 802" />
|
|
218
|
+
</svg>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
/** Cap height is half the icon. Caps are 7 of the 11 viewBox rows. */
|
|
224
|
+
function wordmarkHeight(iconH: number) {
|
|
225
|
+
return Math.round(iconH * (1 / 2) * (11 / 7));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Plus Jakarta Sans caps are 0.7em, so this em size makes them half the icon. */
|
|
229
|
+
function jakartaSize(iconH: number) {
|
|
230
|
+
return (iconH / 2) / 0.7;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function LogoJakarta({ iconH, color }: { iconH: number; color?: string }) {
|
|
234
|
+
return (
|
|
235
|
+
<span
|
|
236
|
+
aria-hidden
|
|
237
|
+
className="shrink-0 font-bold leading-none tracking-[-0.02em]"
|
|
238
|
+
style={{ fontFamily: '"Plus Jakarta Sans Variable", sans-serif', fontSize: jakartaSize(iconH), color }}
|
|
239
|
+
>
|
|
240
|
+
{WORDMARK}
|
|
241
|
+
</span>
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function LogoWordmark({ height, color }: { height: number; color?: string }) {
|
|
246
|
+
return (
|
|
247
|
+
<svg
|
|
248
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
249
|
+
viewBox={`0 0 ${WORD_W} ${WORD_H}`}
|
|
250
|
+
width={Math.round(height * (WORD_W / WORD_H))}
|
|
251
|
+
height={height}
|
|
252
|
+
aria-hidden
|
|
253
|
+
className="shrink-0"
|
|
254
|
+
shapeRendering="crispEdges"
|
|
255
|
+
>
|
|
256
|
+
<g transform="translate(20 20)" fill={color ?? "currentColor"}>
|
|
257
|
+
{WORD_CELLS.map(([x, y]) => (
|
|
258
|
+
<rect key={`${x}-${y}`} x={x} y={y} width={15.2} height={15.2} />
|
|
259
|
+
))}
|
|
260
|
+
</g>
|
|
261
|
+
</svg>
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function Logo({ variant = "full", size = 32, face = "pixel", color, className, style }: LogoProps) {
|
|
266
|
+
const iconH = size;
|
|
267
|
+
const mark = <LogoMark height={iconH} color={color} />;
|
|
268
|
+
const word = face === "jakarta" ? <LogoJakarta iconH={iconH} color={color} /> : <LogoWordmark height={wordmarkHeight(iconH)} color={color} />;
|
|
269
|
+
const root = cn("inline-flex items-center text-foreground", variant === "full" && "gap-2", className);
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<span role="img" aria-label={WORDMARK} className={root} style={style}>
|
|
273
|
+
{variant === "text" ? null : mark}
|
|
274
|
+
{variant === "icon" ? null : word}
|
|
275
|
+
</span>
|
|
276
|
+
);
|
|
277
|
+
}
|
|
@@ -62,7 +62,8 @@ function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (v
|
|
|
62
62
|
* Preview renders the document. Click a heading, list item, or paragraph to edit that text in place.
|
|
63
63
|
* Mermaid fences render as diagrams and stay view only.
|
|
64
64
|
* Edit is the full-file text buffer. Diff compares `original` and `value` like Monaco.
|
|
65
|
-
* `readOnly` locks editing and hides the mode switcher
|
|
65
|
+
* `readOnly` locks editing and hides the mode switcher.
|
|
66
|
+
* A finished document with no editor chrome uses `MarkdownViewer`.
|
|
66
67
|
*/
|
|
67
68
|
export function MarkdownEditor({
|
|
68
69
|
value: valueProp,
|
|
@@ -1,83 +1,8 @@
|
|
|
1
|
-
import { type MouseEvent,
|
|
2
|
-
import ReactMarkdown, { type Components } from "react-markdown";
|
|
3
|
-
import remarkGfm from "remark-gfm";
|
|
4
|
-
import { ChatMarkdownTable } from "../chat/message-ui/ChatMarkdownTable";
|
|
5
|
-
import { MermaidBlock } from "../chat/message-ui/MermaidBlock";
|
|
6
|
-
import { Checkbox } from "../checkbox/Checkbox";
|
|
7
|
-
import { CodeBlock } from "../codeblock/CodeBlock";
|
|
1
|
+
import { type MouseEvent, useLayoutEffect, useRef } from "react";
|
|
8
2
|
import { cn } from "../lib/cn";
|
|
9
3
|
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
10
|
-
import { type
|
|
11
|
-
|
|
12
|
-
const documentClass = cn(
|
|
13
|
-
"min-w-0 text-[15px] leading-7 text-foreground",
|
|
14
|
-
"[&_h1]:my-0 [&_h1]:text-[32px] [&_h1]:leading-tight [&_h1]:font-bold [&_h1]:tracking-tight",
|
|
15
|
-
"[&_h2]:my-0 [&_h2]:text-[24px] [&_h2]:leading-tight [&_h2]:font-bold [&_h2]:tracking-tight",
|
|
16
|
-
"[&_h3]:my-0 [&_h3]:text-[20px] [&_h3]:leading-snug [&_h3]:font-semibold",
|
|
17
|
-
"[&_h4]:my-0 [&_h4]:text-[18px] [&_h4]:leading-snug [&_h4]:font-semibold",
|
|
18
|
-
"[&_h5]:my-0 [&_h5]:text-[16px] [&_h5]:leading-snug [&_h5]:font-semibold",
|
|
19
|
-
"[&_h6]:my-0 [&_h6]:text-[15px] [&_h6]:leading-snug [&_h6]:font-semibold",
|
|
20
|
-
"[&_p]:my-0",
|
|
21
|
-
"[&_ul]:my-1 [&_ul]:list-disc [&_ul]:pl-6",
|
|
22
|
-
"[&_ol]:my-1 [&_ol]:list-decimal [&_ol]:pl-6",
|
|
23
|
-
"[&_ul.contains-task-list]:list-none",
|
|
24
|
-
"[&_li]:my-0.5",
|
|
25
|
-
"[&_li.task-list-item]:relative",
|
|
26
|
-
"[&_li.task-list-item>.nonla-md-task-check]:absolute [&_li.task-list-item>.nonla-md-task-check]:top-1 [&_li.task-list-item>.nonla-md-task-check]:-left-5",
|
|
27
|
-
"[&_blockquote]:my-1 [&_blockquote]:border-l-2 [&_blockquote]:border-border [&_blockquote]:pl-3 [&_blockquote]:text-muted-foreground",
|
|
28
|
-
"[&_a]:text-link [&_a]:underline-offset-[3px] hover:[&_a]:underline",
|
|
29
|
-
"[&_hr]:my-2 [&_hr]:border-border",
|
|
30
|
-
"[&_strong]:font-semibold",
|
|
31
|
-
"[&_img]:my-1 [&_img]:max-w-full [&_img]:rounded-lg",
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
const previewComponents: Components = {
|
|
35
|
-
code({ className, children }) {
|
|
36
|
-
const match = /language-(\w+)/.exec(className || "");
|
|
37
|
-
const lang = match?.[1] ?? "";
|
|
38
|
-
const codeText = String(children).replace(/\n$/, "");
|
|
39
|
-
const isBlock = codeText.includes("\n") || !!match;
|
|
40
|
-
if (!isBlock) {
|
|
41
|
-
return <code className="rounded-sm bg-secondary px-1 py-0.5 font-mono text-[0.9em]">{children}</code>;
|
|
42
|
-
}
|
|
43
|
-
if (lang.toLowerCase() === "mermaid") return <MermaidView code={codeText} />;
|
|
44
|
-
return <CodeBlock code={codeText} language={lang || undefined} className="my-2" />;
|
|
45
|
-
},
|
|
46
|
-
table({ children }) {
|
|
47
|
-
return <ChatMarkdownTable>{children}</ChatMarkdownTable>;
|
|
48
|
-
},
|
|
49
|
-
a({ href, children, ...props }) {
|
|
50
|
-
return (
|
|
51
|
-
<a href={href} target="_blank" rel="noopener noreferrer" {...props}>
|
|
52
|
-
{children}
|
|
53
|
-
</a>
|
|
54
|
-
);
|
|
55
|
-
},
|
|
56
|
-
input({ type, checked }) {
|
|
57
|
-
if (type !== "checkbox") return <input type={type} checked={checked} readOnly />;
|
|
58
|
-
return (
|
|
59
|
-
<span className="nonla-md-task-check pointer-events-none inline-flex h-7 items-center">
|
|
60
|
-
<Checkbox checked={Boolean(checked)} tabIndex={-1} />
|
|
61
|
-
</span>
|
|
62
|
-
);
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
function MermaidView({ code }: { code: string }) {
|
|
67
|
-
if (!code.trim()) {
|
|
68
|
-
return <div className="rounded-xl border border-dashed border-border px-4 py-6 text-center text-xs text-muted-foreground">Empty diagram</div>;
|
|
69
|
-
}
|
|
70
|
-
return <MermaidBlock className="my-0">{code}</MermaidBlock>;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function headingClass(level: number): string {
|
|
74
|
-
if (level === 1) return "text-[32px] font-bold leading-tight tracking-tight";
|
|
75
|
-
if (level === 2) return "text-[24px] font-bold leading-tight tracking-tight";
|
|
76
|
-
if (level === 3) return "text-[20px] font-semibold leading-snug";
|
|
77
|
-
if (level === 4) return "text-[18px] font-semibold leading-snug";
|
|
78
|
-
if (level === 5) return "text-[16px] font-semibold leading-snug";
|
|
79
|
-
return "text-[15px] font-semibold leading-snug";
|
|
80
|
-
}
|
|
4
|
+
import { type MarkdownDoc, readInlineEdit, writeInlineEdit } from "./blocks";
|
|
5
|
+
import { BlockBody, headingClass, ListItem, spaceBefore } from "./MarkdownViewer";
|
|
81
6
|
|
|
82
7
|
function BlockEditor({ value, mono, className, caret = "end", onChange, onBlur, onEnter }: { value: string; mono?: boolean; className?: string; caret?: "start" | "end"; onChange: (value: string) => void; onBlur: () => void; onEnter?: (before: string, after: string) => void }) {
|
|
83
8
|
const ref = useRef<HTMLTextAreaElement>(null);
|
|
@@ -160,123 +85,6 @@ export type MarkdownPreviewProps = {
|
|
|
160
85
|
onAppend: () => void;
|
|
161
86
|
};
|
|
162
87
|
|
|
163
|
-
type MdNode = {
|
|
164
|
-
type: string;
|
|
165
|
-
value?: string;
|
|
166
|
-
children?: MdNode[];
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/** A single Enter stays a visible line after the block editor closes. */
|
|
170
|
-
function remarkKeepLineBreaks() {
|
|
171
|
-
return (tree: MdNode) => {
|
|
172
|
-
breakParagraphLines(tree);
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function breakParagraphLines(node: MdNode) {
|
|
177
|
-
if (!node.children) return;
|
|
178
|
-
if (node.type === "paragraph" || node.type === "heading") {
|
|
179
|
-
splitInlineBreaks(node);
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
for (const child of node.children) breakParagraphLines(child);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function splitInlineBreaks(node: MdNode) {
|
|
186
|
-
if (!node.children) return;
|
|
187
|
-
node.children = node.children.flatMap((child) => {
|
|
188
|
-
if (child.type === "inlineCode" || child.type === "code") return [child];
|
|
189
|
-
splitInlineBreaks(child);
|
|
190
|
-
return splitSoftBreaks(child);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function splitSoftBreaks(node: MdNode): MdNode[] {
|
|
195
|
-
if (node.type !== "text" || !node.value?.includes("\n")) return [node];
|
|
196
|
-
const parts = node.value.split("\n");
|
|
197
|
-
const out: MdNode[] = [];
|
|
198
|
-
for (let index = 0; index < parts.length; index++) {
|
|
199
|
-
if (index > 0) out.push({ type: "break" });
|
|
200
|
-
const part = parts[index] ?? "";
|
|
201
|
-
if (part.length > 0) out.push({ type: "text", value: part });
|
|
202
|
-
}
|
|
203
|
-
return out;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function RichText({ source }: { source: string }) {
|
|
207
|
-
return (
|
|
208
|
-
<div className={documentClass}>
|
|
209
|
-
<ReactMarkdown remarkPlugins={[remarkGfm, remarkKeepLineBreaks]} components={previewComponents}>
|
|
210
|
-
{source}
|
|
211
|
-
</ReactMarkdown>
|
|
212
|
-
</div>
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function ListItem({ view, children }: { view: Extract<InlineEdit, { kind: "list" }>; children: ReactNode }) {
|
|
217
|
-
const level = Math.floor(view.indent.length / 2);
|
|
218
|
-
const ordinal = Number(/^(\d+)/.exec(view.marker)?.[1] ?? "1");
|
|
219
|
-
return (
|
|
220
|
-
<div className="flex items-start gap-1.5" style={level > 0 ? { marginLeft: `${level * 1.25}rem` } : undefined}>
|
|
221
|
-
{view.task ? (
|
|
222
|
-
<span className="pointer-events-none inline-flex h-7 w-5 shrink-0 items-center justify-center">
|
|
223
|
-
<Checkbox checked={view.checked} tabIndex={-1} />
|
|
224
|
-
</span>
|
|
225
|
-
) : view.ordered ? (
|
|
226
|
-
<span className="w-6 shrink-0 text-right text-[15px] leading-7 tabular-nums">{ordinal}.</span>
|
|
227
|
-
) : (
|
|
228
|
-
<span className="flex h-7 w-5 shrink-0 items-center justify-center" aria-hidden>
|
|
229
|
-
<span className="size-1.5 rounded-full bg-current" />
|
|
230
|
-
</span>
|
|
231
|
-
)}
|
|
232
|
-
<div className="min-w-0 flex-1">{children}</div>
|
|
233
|
-
</div>
|
|
234
|
-
);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function isListBlock(block: MarkdownBlock): boolean {
|
|
238
|
-
return block.kind === "text" && readInlineEdit(block.body).kind === "list";
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function headingGap(level: number): string {
|
|
242
|
-
if (level <= 2) return "mt-6";
|
|
243
|
-
if (level === 3) return "mt-5";
|
|
244
|
-
if (level === 4) return "mt-4";
|
|
245
|
-
return "mt-3";
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function spaceBefore(block: MarkdownBlock, prev?: MarkdownBlock): string {
|
|
249
|
-
if (!prev) return "";
|
|
250
|
-
if (isListBlock(block) && isListBlock(prev)) return "mt-0.5";
|
|
251
|
-
if (block.kind === "text") {
|
|
252
|
-
const view = readInlineEdit(block.body);
|
|
253
|
-
if (view.kind === "heading") return headingGap(view.level);
|
|
254
|
-
}
|
|
255
|
-
if (prev.kind === "text" && readInlineEdit(prev.body).kind === "heading") return "mt-2";
|
|
256
|
-
return "mt-3";
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
function BlockBody({ block }: { block: MarkdownBlock }): ReactNode {
|
|
260
|
-
if (block.kind === "mermaid") {
|
|
261
|
-
const fence = readFence(block.body);
|
|
262
|
-
return <MermaidView code={fence?.code ?? block.body} />;
|
|
263
|
-
}
|
|
264
|
-
if (block.kind === "code") {
|
|
265
|
-
const fence = readFence(block.body);
|
|
266
|
-
if (!fence) return <CodeBlock code={block.body} className="my-0" />;
|
|
267
|
-
return <CodeBlock code={fence.code} language={fence.lang || undefined} className="my-0" />;
|
|
268
|
-
}
|
|
269
|
-
const view = readInlineEdit(block.body);
|
|
270
|
-
if (view.kind === "list") {
|
|
271
|
-
return (
|
|
272
|
-
<ListItem view={view}>
|
|
273
|
-
<RichText source={view.text} />
|
|
274
|
-
</ListItem>
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
return <RichText source={block.body} />;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
88
|
export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Write markdown…", onStartEdit, onEditMouseDown, onChangeBody, onBlurBlock, onEnterList, caret = "end", onAppend }: MarkdownPreviewProps) {
|
|
281
89
|
return (
|
|
282
90
|
<OverlayScroll className="h-full bg-card">
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ReactNode, useCallback, useRef } from "react";
|
|
2
|
-
import { ButtonCopy } from "
|
|
2
|
+
import { ButtonCopy } from "../button/ButtonCopy";
|
|
3
3
|
|
|
4
4
|
function escapeCell(text: string): string {
|
|
5
5
|
return text.replace(/\|/g, "\\|").replace(/\n+/g, "").trim();
|
|
@@ -20,7 +20,7 @@ function tableToMarkdown(table: HTMLTableElement): string {
|
|
|
20
20
|
return [`| ${header.join(" | ")} |`, `| ${separator.join(" | ")} |`, ...body.map((row) => `| ${row.join(" | ")} |`)].join("\n");
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export function
|
|
23
|
+
export function MarkdownTable({ children }: { children: ReactNode }) {
|
|
24
24
|
const tableRef = useRef<HTMLTableElement>(null);
|
|
25
25
|
|
|
26
26
|
const getText = useCallback(() => {
|
|
@@ -35,7 +35,7 @@ export function ChatMarkdownTable({ children }: { children: ReactNode }) {
|
|
|
35
35
|
<div className="overflow-x-auto">
|
|
36
36
|
<table
|
|
37
37
|
ref={tableRef}
|
|
38
|
-
className="w-max min-w-full
|
|
38
|
+
className="w-max min-w-full border-separate border-spacing-0 text-[14px] [&_th]:max-w-[360px] [&_td]:max-w-[360px] [&_th]:whitespace-normal [&_td]:whitespace-normal [&_th]:wrap-normal [&_td]:wrap-normal [&_th]:bg-(--nonla-table-head) [&_th]:text-foreground [&_th]:font-semibold [&_th]:px-2.5 [&_th]:py-1.5 [&_th]:text-left [&_th]:align-top [&_th]:border-r [&_th]:border-b [&_th]:border-border [&_th:last-child]:border-r-0 [&_td]:px-2.5 [&_td]:py-1.5 [&_td]:align-top [&_td]:text-foreground [&_td]:bg-card [&_td]:border-r [&_td]:border-b [&_td]:border-border [&_td:last-child]:border-r-0 [&_tbody_tr:last-child_td]:border-b-0"
|
|
39
39
|
>
|
|
40
40
|
{children}
|
|
41
41
|
</table>
|