devnonla-ui 0.9.1 → 0.10.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 +31 -3
- package/package.json +2 -1
- 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/tool-ui/CallAgentToolUI.tsx +1 -1
- package/src/index.ts +12 -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} +2 -2
- package/src/markdown-editor/MarkdownViewer.tsx +235 -0
- package/src/{chat/message-ui → markdown-editor}/MermaidBlock.tsx +8 -8
- package/src/styles.css +11 -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
|
@@ -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(() => {
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { type ReactNode, useMemo } from "react";
|
|
2
|
+
import ReactMarkdown, { type Components } from "react-markdown";
|
|
3
|
+
import remarkGfm from "remark-gfm";
|
|
4
|
+
import { Checkbox } from "../checkbox/Checkbox";
|
|
5
|
+
import { CodeBlock } from "../codeblock/CodeBlock";
|
|
6
|
+
import { cn } from "../lib/cn";
|
|
7
|
+
import { type InlineEdit, type MarkdownBlock, parseMarkdownDoc, readFence, readInlineEdit } from "./blocks";
|
|
8
|
+
import { MarkdownTable } from "./MarkdownTable";
|
|
9
|
+
import { MermaidBlock } from "./MermaidBlock";
|
|
10
|
+
|
|
11
|
+
const documentClass = cn(
|
|
12
|
+
"min-w-0 wrap-anywhere text-[15px] leading-7 text-foreground",
|
|
13
|
+
"[&_h1]:my-0 [&_h1]:text-[32px] [&_h1]:leading-tight [&_h1]:font-bold [&_h1]:tracking-tight",
|
|
14
|
+
"[&_h2]:my-0 [&_h2]:text-[24px] [&_h2]:leading-tight [&_h2]:font-bold [&_h2]:tracking-tight",
|
|
15
|
+
"[&_h3]:my-0 [&_h3]:text-[20px] [&_h3]:leading-snug [&_h3]:font-semibold",
|
|
16
|
+
"[&_h4]:my-0 [&_h4]:text-[18px] [&_h4]:leading-snug [&_h4]:font-semibold",
|
|
17
|
+
"[&_h5]:my-0 [&_h5]:text-[16px] [&_h5]:leading-snug [&_h5]:font-semibold",
|
|
18
|
+
"[&_h6]:my-0 [&_h6]:text-[15px] [&_h6]:leading-snug [&_h6]:font-semibold",
|
|
19
|
+
"[&_p]:my-0",
|
|
20
|
+
"[&_ul]:my-1 [&_ul]:list-disc [&_ul]:pl-6",
|
|
21
|
+
"[&_ol]:my-1 [&_ol]:list-decimal [&_ol]:pl-6",
|
|
22
|
+
"[&_ul.contains-task-list]:list-none",
|
|
23
|
+
"[&_li]:my-0.5",
|
|
24
|
+
"[&_li.task-list-item]:relative",
|
|
25
|
+
"[&_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",
|
|
26
|
+
"[&_blockquote]:my-1 [&_blockquote]:border-l-2 [&_blockquote]:border-border [&_blockquote]:pl-3 [&_blockquote]:text-muted-foreground",
|
|
27
|
+
"[&_a]:text-link [&_a]:underline-offset-[3px] hover:[&_a]:underline",
|
|
28
|
+
"[&_hr]:my-2 [&_hr]:border-border",
|
|
29
|
+
"[&_strong]:font-semibold",
|
|
30
|
+
"[&_img]:my-1 [&_img]:max-w-full [&_img]:rounded-lg",
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const previewComponents: Components = {
|
|
34
|
+
code({ className, children }) {
|
|
35
|
+
const match = /language-(\w+)/.exec(className || "");
|
|
36
|
+
const lang = match?.[1] ?? "";
|
|
37
|
+
const codeText = String(children).replace(/\n$/, "");
|
|
38
|
+
const isBlock = codeText.includes("\n") || !!match;
|
|
39
|
+
if (!isBlock) {
|
|
40
|
+
return <code className="rounded-sm bg-secondary px-1 py-0.5 font-mono text-[0.9em]">{children}</code>;
|
|
41
|
+
}
|
|
42
|
+
if (lang.toLowerCase() === "mermaid") return <MermaidView code={codeText} />;
|
|
43
|
+
return <CodeBlock code={codeText} language={lang || undefined} className="my-2" />;
|
|
44
|
+
},
|
|
45
|
+
table({ children }) {
|
|
46
|
+
return <MarkdownTable>{children}</MarkdownTable>;
|
|
47
|
+
},
|
|
48
|
+
a({ href, children, ...props }) {
|
|
49
|
+
return (
|
|
50
|
+
<a href={href} target="_blank" rel="noopener noreferrer" {...props}>
|
|
51
|
+
{children}
|
|
52
|
+
</a>
|
|
53
|
+
);
|
|
54
|
+
},
|
|
55
|
+
input({ type, checked }) {
|
|
56
|
+
if (type !== "checkbox") return <input type={type} checked={checked} readOnly />;
|
|
57
|
+
return (
|
|
58
|
+
<span className="nonla-md-task-check pointer-events-none inline-flex h-7 items-center">
|
|
59
|
+
<Checkbox checked={Boolean(checked)} tabIndex={-1} />
|
|
60
|
+
</span>
|
|
61
|
+
);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
function MermaidView({ code }: { code: string }) {
|
|
66
|
+
if (!code.trim()) {
|
|
67
|
+
return <div className="rounded-xl border border-dashed border-border px-4 py-6 text-center text-xs text-muted-foreground">Empty diagram</div>;
|
|
68
|
+
}
|
|
69
|
+
return <MermaidBlock className="my-0">{code}</MermaidBlock>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function headingClass(level: number): string {
|
|
73
|
+
if (level === 1) return "text-[32px] font-bold leading-tight tracking-tight";
|
|
74
|
+
if (level === 2) return "text-[24px] font-bold leading-tight tracking-tight";
|
|
75
|
+
if (level === 3) return "text-[20px] font-semibold leading-snug";
|
|
76
|
+
if (level === 4) return "text-[18px] font-semibold leading-snug";
|
|
77
|
+
if (level === 5) return "text-[16px] font-semibold leading-snug";
|
|
78
|
+
return "text-[15px] font-semibold leading-snug";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
type MdNode = {
|
|
82
|
+
type: string;
|
|
83
|
+
value?: string;
|
|
84
|
+
children?: MdNode[];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** A single Enter stays a visible line after the block editor closes. */
|
|
88
|
+
function remarkKeepLineBreaks() {
|
|
89
|
+
return (tree: MdNode) => {
|
|
90
|
+
breakParagraphLines(tree);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function breakParagraphLines(node: MdNode) {
|
|
95
|
+
if (!node.children) return;
|
|
96
|
+
if (node.type === "paragraph" || node.type === "heading") {
|
|
97
|
+
splitInlineBreaks(node);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
for (const child of node.children) breakParagraphLines(child);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function splitInlineBreaks(node: MdNode) {
|
|
104
|
+
if (!node.children) return;
|
|
105
|
+
node.children = node.children.flatMap((child) => {
|
|
106
|
+
if (child.type === "inlineCode" || child.type === "code") return [child];
|
|
107
|
+
splitInlineBreaks(child);
|
|
108
|
+
return splitSoftBreaks(child);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function splitSoftBreaks(node: MdNode): MdNode[] {
|
|
113
|
+
if (node.type !== "text" || !node.value?.includes("\n")) return [node];
|
|
114
|
+
const parts = node.value.split("\n");
|
|
115
|
+
const out: MdNode[] = [];
|
|
116
|
+
for (let index = 0; index < parts.length; index++) {
|
|
117
|
+
if (index > 0) out.push({ type: "break" });
|
|
118
|
+
const part = parts[index] ?? "";
|
|
119
|
+
if (part.length > 0) out.push({ type: "text", value: part });
|
|
120
|
+
}
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function RichText({ source }: { source: string }) {
|
|
125
|
+
return (
|
|
126
|
+
<div className={documentClass}>
|
|
127
|
+
<ReactMarkdown remarkPlugins={[remarkGfm, remarkKeepLineBreaks]} components={previewComponents}>
|
|
128
|
+
{source}
|
|
129
|
+
</ReactMarkdown>
|
|
130
|
+
</div>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function ListItem({ view, children }: { view: Extract<InlineEdit, { kind: "list" }>; children: ReactNode }) {
|
|
135
|
+
const level = Math.floor(view.indent.length / 2);
|
|
136
|
+
const ordinal = Number(/^(\d+)/.exec(view.marker)?.[1] ?? "1");
|
|
137
|
+
return (
|
|
138
|
+
<div className="flex items-start gap-1.5" style={level > 0 ? { marginLeft: `${level * 1.25}rem` } : undefined}>
|
|
139
|
+
{view.task ? (
|
|
140
|
+
<span className="pointer-events-none inline-flex h-7 w-5 shrink-0 items-center justify-center">
|
|
141
|
+
<Checkbox checked={view.checked} tabIndex={-1} />
|
|
142
|
+
</span>
|
|
143
|
+
) : view.ordered ? (
|
|
144
|
+
<span className="w-6 shrink-0 text-right text-[15px] leading-7 tabular-nums">{ordinal}.</span>
|
|
145
|
+
) : (
|
|
146
|
+
<span className="flex h-7 w-5 shrink-0 items-center justify-center" aria-hidden>
|
|
147
|
+
<span className="size-1.5 rounded-full bg-current" />
|
|
148
|
+
</span>
|
|
149
|
+
)}
|
|
150
|
+
<div className="min-w-0 flex-1">{children}</div>
|
|
151
|
+
</div>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function isListBlock(block: MarkdownBlock): boolean {
|
|
156
|
+
return block.kind === "text" && readInlineEdit(block.body).kind === "list";
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function headingGap(level: number): string {
|
|
160
|
+
if (level <= 2) return "mt-6";
|
|
161
|
+
if (level === 3) return "mt-5";
|
|
162
|
+
if (level === 4) return "mt-4";
|
|
163
|
+
return "mt-3";
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function spaceBefore(block: MarkdownBlock, prev?: MarkdownBlock): string {
|
|
167
|
+
if (!prev) return "";
|
|
168
|
+
if (isListBlock(block) && isListBlock(prev)) return "mt-0.5";
|
|
169
|
+
if (block.kind === "text") {
|
|
170
|
+
const view = readInlineEdit(block.body);
|
|
171
|
+
if (view.kind === "heading") return headingGap(view.level);
|
|
172
|
+
}
|
|
173
|
+
if (prev.kind === "text" && readInlineEdit(prev.body).kind === "heading") return "mt-2";
|
|
174
|
+
return "mt-3";
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function fenceClosed(body: string): boolean {
|
|
178
|
+
const lines = body.split("\n");
|
|
179
|
+
if (lines.length < 2) return false;
|
|
180
|
+
const last = (lines[lines.length - 1] ?? "").trim();
|
|
181
|
+
return /^(?:`{3,}|~{3,})$/.test(last);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function BlockBody({ block, streaming }: { block: MarkdownBlock; streaming?: boolean }): ReactNode {
|
|
185
|
+
if (block.kind === "mermaid") {
|
|
186
|
+
const fence = readFence(block.body);
|
|
187
|
+
const code = fence?.code ?? block.body;
|
|
188
|
+
if (streaming && !fenceClosed(block.body)) return <CodeBlock code={code} language="mermaid" className="my-0" />;
|
|
189
|
+
return <MermaidView code={code} />;
|
|
190
|
+
}
|
|
191
|
+
if (block.kind === "code") {
|
|
192
|
+
const fence = readFence(block.body);
|
|
193
|
+
if (!fence) return <CodeBlock code={block.body} className="my-0" />;
|
|
194
|
+
return <CodeBlock code={fence.code} language={fence.lang || undefined} className="my-0" />;
|
|
195
|
+
}
|
|
196
|
+
const view = readInlineEdit(block.body);
|
|
197
|
+
if (view.kind === "list") {
|
|
198
|
+
return (
|
|
199
|
+
<ListItem view={view}>
|
|
200
|
+
<RichText source={view.text} />
|
|
201
|
+
</ListItem>
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
return <RichText source={block.body} />;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export type MarkdownViewerProps = {
|
|
208
|
+
value?: string;
|
|
209
|
+
/** An open mermaid fence stays a code block until the stream closes it. */
|
|
210
|
+
streaming?: boolean;
|
|
211
|
+
placeholder?: string;
|
|
212
|
+
className?: string;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/** Read-only markdown. Same blocks as the editor preview, without editing. */
|
|
216
|
+
export function MarkdownViewer({ value = "", streaming = false, placeholder, className }: MarkdownViewerProps) {
|
|
217
|
+
const doc = useMemo(() => parseMarkdownDoc(value), [value]);
|
|
218
|
+
if (doc.blocks.length === 0) {
|
|
219
|
+
return placeholder ? <p className={cn("m-0 text-sm text-placeholder", className)}>{placeholder}</p> : null;
|
|
220
|
+
}
|
|
221
|
+
return (
|
|
222
|
+
<div className={cn("nonla-markdown-viewer flex min-w-0 flex-col text-foreground", className)}>
|
|
223
|
+
{doc.blocks.map((block, index) => (
|
|
224
|
+
<div
|
|
225
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: block position is the document identity
|
|
226
|
+
key={`${index}:${block.kind}`}
|
|
227
|
+
data-md-block={block.kind}
|
|
228
|
+
className={cn("outline-none", spaceBefore(block, doc.blocks[index - 1]))}
|
|
229
|
+
>
|
|
230
|
+
<BlockBody block={block} streaming={streaming} />
|
|
231
|
+
</div>
|
|
232
|
+
))}
|
|
233
|
+
</div>
|
|
234
|
+
);
|
|
235
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useCallback, useEffect, useId, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
-
import { Button } from "
|
|
3
|
-
import { CodeBlock } from "
|
|
4
|
-
import { SolarIcon } from "
|
|
5
|
-
import { cn } from "
|
|
6
|
-
import { getColorMode, subscribeColorMode } from "
|
|
2
|
+
import { Button } from "../button/Button";
|
|
3
|
+
import { CodeBlock } from "../codeblock/CodeBlock";
|
|
4
|
+
import { SolarIcon } from "../icon/SolarIcon";
|
|
5
|
+
import { cn } from "../lib/cn";
|
|
6
|
+
import { getColorMode, subscribeColorMode } from "../theme";
|
|
7
7
|
import { sanitizeMermaid } from "./sanitizeMermaid";
|
|
8
8
|
|
|
9
9
|
/** Official Mermaid palettes: `default` is light, `dark` is dark. */
|
|
@@ -198,7 +198,7 @@ export function MermaidBlock({ children, className }: MermaidBlockProps) {
|
|
|
198
198
|
|
|
199
199
|
<dialog
|
|
200
200
|
ref={dialogRef}
|
|
201
|
-
className="m-0
|
|
201
|
+
className="inset-2.5 m-0 h-auto w-auto max-h-none max-w-none overflow-hidden rounded-xl border border-foreground/25 bg-card p-0 shadow-xl backdrop:bg-black/40 open:flex open:flex-col"
|
|
202
202
|
onClose={resetView}
|
|
203
203
|
onKeyDown={(e) => {
|
|
204
204
|
if (e.key !== "Escape") return;
|
|
@@ -207,14 +207,14 @@ export function MermaidBlock({ children, className }: MermaidBlockProps) {
|
|
|
207
207
|
closeFullscreen();
|
|
208
208
|
}}
|
|
209
209
|
>
|
|
210
|
-
<div className="
|
|
210
|
+
<div className="absolute top-5 right-5 z-50 flex items-center gap-1.5">
|
|
211
211
|
<Button size="small" icon={<SolarIcon name="download-linear" size={12} />} title="Download SVG" aria-label="Download SVG" onClick={downloadSvg} disabled={!svgContent} />
|
|
212
212
|
<Button size="small" icon={<SolarIcon name="minimize-linear" size={12} />} title="Exit fullscreen" onClick={closeFullscreen}>
|
|
213
213
|
Exit
|
|
214
214
|
</Button>
|
|
215
215
|
</div>
|
|
216
216
|
|
|
217
|
-
<div className="
|
|
217
|
+
<div className="absolute bottom-5 left-1/2 z-50 flex -translate-x-1/2 items-center gap-2 rounded-full border border-border bg-card px-3 py-1.5 text-xs text-muted-foreground shadow-md select-none">
|
|
218
218
|
<span>{Math.round(zoom * 100)}%</span>
|
|
219
219
|
{!isDefaultView ? (
|
|
220
220
|
<Button type="text" size="small" onClick={resetView}>
|
package/src/styles.css
CHANGED
|
@@ -361,29 +361,28 @@
|
|
|
361
361
|
|
|
362
362
|
@keyframes nonla-shimmer {
|
|
363
363
|
0% {
|
|
364
|
-
background-position: 100%
|
|
364
|
+
background-position: 100% center;
|
|
365
365
|
}
|
|
366
366
|
100% {
|
|
367
|
-
background-position:
|
|
367
|
+
background-position: 0% center;
|
|
368
368
|
}
|
|
369
369
|
}
|
|
370
370
|
|
|
371
371
|
.nonla-shimmer {
|
|
372
372
|
display: inline-block;
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
var(--foreground) 50%,
|
|
378
|
-
var(--tertiary-foreground)
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
background-size: 220% 100%;
|
|
373
|
+
width: fit-content;
|
|
374
|
+
max-width: 100%;
|
|
375
|
+
/* Stable muted glyphs, with a narrow highlight that travels across the letters. */
|
|
376
|
+
background-image:
|
|
377
|
+
linear-gradient(90deg, transparent calc(50% - 1em), var(--foreground) 50%, transparent calc(50% + 1em)),
|
|
378
|
+
linear-gradient(var(--tertiary-foreground), var(--tertiary-foreground));
|
|
379
|
+
background-size: 250% 100%, auto;
|
|
380
|
+
background-repeat: no-repeat;
|
|
382
381
|
background-clip: text;
|
|
383
382
|
-webkit-background-clip: text;
|
|
384
383
|
-webkit-text-fill-color: transparent;
|
|
385
384
|
color: transparent;
|
|
386
|
-
animation: nonla-shimmer 1.
|
|
385
|
+
animation: nonla-shimmer 1.8s linear infinite;
|
|
387
386
|
}
|
|
388
387
|
|
|
389
388
|
@media (prefers-reduced-motion: reduce) {
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useLayoutEffect, useSyncExternalStore } from "react";
|
|
2
|
+
import { SolarIcon } from "../icon/SolarIcon";
|
|
3
|
+
import { cn } from "../lib/cn";
|
|
4
|
+
import { getColorPreference, initColorMode, type NonlaColorPreference, setColorPreference, subscribeColorPreference } from "../theme";
|
|
5
|
+
|
|
6
|
+
const OPTIONS: { value: NonlaColorPreference; label: string; icon: string }[] = [
|
|
7
|
+
{ value: "system", label: "System", icon: "monitor-linear" },
|
|
8
|
+
{ value: "light", label: "Light", icon: "sun-linear" },
|
|
9
|
+
{ value: "dark", label: "Dark", icon: "moon-linear" },
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
export function useColorPreference(): NonlaColorPreference {
|
|
13
|
+
return useSyncExternalStore(subscribeColorPreference, getColorPreference, () => "dark");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type ThemeSwitcherProps = {
|
|
17
|
+
className?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** System, Light, and Dark as three icons. System follows the OS. Remembers the choice. */
|
|
21
|
+
export function ThemeSwitcher({ className }: ThemeSwitcherProps) {
|
|
22
|
+
const preference = useColorPreference();
|
|
23
|
+
|
|
24
|
+
useLayoutEffect(() => {
|
|
25
|
+
initColorMode();
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<div
|
|
30
|
+
role="radiogroup"
|
|
31
|
+
aria-label="Theme"
|
|
32
|
+
className={cn("inline-flex h-6 items-center gap-px rounded-full border border-solid border-border bg-background p-px", className)}
|
|
33
|
+
>
|
|
34
|
+
{OPTIONS.map((option) => {
|
|
35
|
+
const selected = option.value === preference;
|
|
36
|
+
return (
|
|
37
|
+
<button
|
|
38
|
+
key={option.value}
|
|
39
|
+
type="button"
|
|
40
|
+
role="radio"
|
|
41
|
+
aria-checked={selected}
|
|
42
|
+
aria-label={option.label}
|
|
43
|
+
onClick={() => setColorPreference(option.value)}
|
|
44
|
+
className={cn(
|
|
45
|
+
"inline-flex size-5 items-center justify-center rounded-full border-0 bg-transparent p-0 cursor-pointer text-foreground/75",
|
|
46
|
+
selected ? "bg-ink-active text-foreground" : "hover:bg-ink-hover hover:text-foreground",
|
|
47
|
+
)}
|
|
48
|
+
>
|
|
49
|
+
<SolarIcon name={option.icon} size={12} />
|
|
50
|
+
</button>
|
|
51
|
+
);
|
|
52
|
+
})}
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
}
|