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,31 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { transform } from "sucrase";
|
|
4
|
+
|
|
5
|
+
export type LiveComponent = () => ReactNode;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Compiles TSX source to a component. This runs the source as code in the current
|
|
9
|
+
* page, so it must only see trusted input or run inside the sandbox runner.
|
|
10
|
+
*/
|
|
11
|
+
export function compileReactCode(code: string, modules: Record<string, unknown>): LiveComponent {
|
|
12
|
+
const js = transform(code, {
|
|
13
|
+
transforms: ["typescript", "jsx", "imports"],
|
|
14
|
+
jsxRuntime: "classic",
|
|
15
|
+
production: true,
|
|
16
|
+
}).code;
|
|
17
|
+
const requireModule = (id: string) => {
|
|
18
|
+
if (id === "react") return React;
|
|
19
|
+
if (Object.hasOwn(modules, id)) return modules[id];
|
|
20
|
+
throw new Error(`Unknown module "${id}".`);
|
|
21
|
+
};
|
|
22
|
+
const exports: { default?: LiveComponent } = {};
|
|
23
|
+
const run = new Function("require", "exports", "React", `${js}\nreturn exports.default;`) as (
|
|
24
|
+
require: (id: string) => unknown,
|
|
25
|
+
exports: { default?: LiveComponent },
|
|
26
|
+
React: typeof import("react"),
|
|
27
|
+
) => LiveComponent | undefined;
|
|
28
|
+
const Demo = run(requireModule, exports, React);
|
|
29
|
+
if (typeof Demo !== "function") throw new Error("React block must export default a component.");
|
|
30
|
+
return Demo;
|
|
31
|
+
}
|
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) {
|
|
@@ -1149,3 +1148,111 @@
|
|
|
1149
1148
|
color: #b31d28;
|
|
1150
1149
|
background-color: #ffeef0;
|
|
1151
1150
|
}
|
|
1151
|
+
|
|
1152
|
+
/* ── Agent avatars — a quiet shift, not a bounce ──────────────────────── */
|
|
1153
|
+
.nonla-avatar {
|
|
1154
|
+
display: inline-flex;
|
|
1155
|
+
flex-shrink: 0;
|
|
1156
|
+
line-height: 0;
|
|
1157
|
+
vertical-align: middle;
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
.nonla-avatar-bob {
|
|
1161
|
+
display: block;
|
|
1162
|
+
width: 100%;
|
|
1163
|
+
height: 100%;
|
|
1164
|
+
transform: translate(var(--nonla-avatar-hx, 0), var(--nonla-avatar-hy, 0));
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
.nonla-avatar-gaze {
|
|
1168
|
+
transform-box: view-box;
|
|
1169
|
+
transform: translate(var(--nonla-avatar-ex, 0), var(--nonla-avatar-ey, 0));
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
.nonla-avatar-eyes {
|
|
1173
|
+
transform-box: fill-box;
|
|
1174
|
+
transform-origin: center;
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
.nonla-avatar[data-motion="idle"] .nonla-avatar-bob {
|
|
1178
|
+
animation: nonla-avatar-wander 8.4s ease-in-out infinite;
|
|
1179
|
+
animation-delay: var(--nonla-avatar-delay, 0s);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
.nonla-avatar[data-motion="idle"] .nonla-avatar-gaze {
|
|
1183
|
+
animation: nonla-avatar-glance 8.4s ease-in-out infinite;
|
|
1184
|
+
animation-delay: var(--nonla-avatar-delay, 0s);
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
.nonla-avatar[data-motion="idle"] .nonla-avatar-eyes {
|
|
1188
|
+
animation: nonla-avatar-blink 6.4s ease-in-out infinite;
|
|
1189
|
+
animation-delay: var(--nonla-avatar-delay, 0s);
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
@keyframes nonla-avatar-wander {
|
|
1193
|
+
0%,
|
|
1194
|
+
14%,
|
|
1195
|
+
100% {
|
|
1196
|
+
transform: translate(var(--nonla-avatar-hx, 0), var(--nonla-avatar-hy, 0));
|
|
1197
|
+
}
|
|
1198
|
+
22%,
|
|
1199
|
+
34% {
|
|
1200
|
+
transform: translate(-5%, 1%);
|
|
1201
|
+
}
|
|
1202
|
+
42%,
|
|
1203
|
+
54% {
|
|
1204
|
+
transform: translate(0, -4%);
|
|
1205
|
+
}
|
|
1206
|
+
62%,
|
|
1207
|
+
74% {
|
|
1208
|
+
transform: translate(5%, 1%);
|
|
1209
|
+
}
|
|
1210
|
+
82%,
|
|
1211
|
+
94% {
|
|
1212
|
+
transform: translate(0, 4.4%);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
@keyframes nonla-avatar-glance {
|
|
1217
|
+
0%,
|
|
1218
|
+
14%,
|
|
1219
|
+
100% {
|
|
1220
|
+
transform: translate(var(--nonla-avatar-ex, 0), var(--nonla-avatar-ey, 0));
|
|
1221
|
+
}
|
|
1222
|
+
22%,
|
|
1223
|
+
34% {
|
|
1224
|
+
transform: translate(-10%, 0.6%);
|
|
1225
|
+
}
|
|
1226
|
+
42%,
|
|
1227
|
+
54% {
|
|
1228
|
+
transform: translate(0, -6.9%);
|
|
1229
|
+
}
|
|
1230
|
+
62%,
|
|
1231
|
+
74% {
|
|
1232
|
+
transform: translate(10%, 0.6%);
|
|
1233
|
+
}
|
|
1234
|
+
82%,
|
|
1235
|
+
94% {
|
|
1236
|
+
transform: translate(0, 7.2%);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
@keyframes nonla-avatar-blink {
|
|
1241
|
+
0%,
|
|
1242
|
+
46%,
|
|
1243
|
+
52%,
|
|
1244
|
+
100% {
|
|
1245
|
+
transform: scaleY(1);
|
|
1246
|
+
}
|
|
1247
|
+
49% {
|
|
1248
|
+
transform: scaleY(0.15);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
@media (prefers-reduced-motion: reduce) {
|
|
1253
|
+
.nonla-avatar[data-motion] .nonla-avatar-bob,
|
|
1254
|
+
.nonla-avatar[data-motion] .nonla-avatar-gaze,
|
|
1255
|
+
.nonla-avatar[data-motion] .nonla-avatar-eyes {
|
|
1256
|
+
animation: none;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
@@ -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
|
+
}
|
package/src/theme.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* 1. CSS: `:root { --nonla-brand: #3b82f6; }`
|
|
8
8
|
* 2. JS: `<App theme={{ colors: { brand: "#3b82f6" } }} />` or `applyNonlaTheme({ brand: "#3b82f6" })`
|
|
9
9
|
* Color mode is `light` | `dark` on `<html>` (`setColorMode`, `<ThemeToggle>`). Dark is the default.
|
|
10
|
+
* `<ThemeSwitcher>` also stores `system`, which follows the OS and still paints `light` or `dark`.
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
13
|
export const NONLA_THEME_KEYS = {
|
|
@@ -155,17 +156,29 @@ export function applyNonlaTheme(theme: NonlaThemeConfig, target: HTMLElement = d
|
|
|
155
156
|
|
|
156
157
|
export type NonlaColorMode = "light" | "dark";
|
|
157
158
|
|
|
158
|
-
/** `
|
|
159
|
+
/** Stored choice. `system` follows the OS and still applies `light` or `dark` on `<html>`. */
|
|
160
|
+
export type NonlaColorPreference = NonlaColorMode | "system";
|
|
161
|
+
|
|
162
|
+
/** `localStorage` key written by `setColorMode` / `setColorPreference`. */
|
|
159
163
|
export const NONLA_COLOR_MODE_KEY = "nonla-color-mode";
|
|
160
164
|
|
|
165
|
+
const COLOR_SCHEME_QUERY = "(prefers-color-scheme: dark)";
|
|
166
|
+
|
|
161
167
|
const modeListeners = new Set<() => void>();
|
|
168
|
+
const preferenceListeners = new Set<() => void>();
|
|
162
169
|
let colorMode: NonlaColorMode = "dark";
|
|
170
|
+
let colorPreference: NonlaColorPreference = "dark";
|
|
163
171
|
let colorModeReady = false;
|
|
172
|
+
let schemeQuery: MediaQueryList | null = null;
|
|
164
173
|
|
|
165
174
|
export function getColorMode(): NonlaColorMode {
|
|
166
175
|
return colorMode;
|
|
167
176
|
}
|
|
168
177
|
|
|
178
|
+
export function getColorPreference(): NonlaColorPreference {
|
|
179
|
+
return colorPreference;
|
|
180
|
+
}
|
|
181
|
+
|
|
169
182
|
export function subscribeColorMode(listener: () => void) {
|
|
170
183
|
modeListeners.add(listener);
|
|
171
184
|
return () => {
|
|
@@ -173,20 +186,69 @@ export function subscribeColorMode(listener: () => void) {
|
|
|
173
186
|
};
|
|
174
187
|
}
|
|
175
188
|
|
|
189
|
+
export function subscribeColorPreference(listener: () => void) {
|
|
190
|
+
preferenceListeners.add(listener);
|
|
191
|
+
return () => {
|
|
192
|
+
preferenceListeners.delete(listener);
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
176
196
|
function notifyColorMode() {
|
|
177
197
|
for (const listener of modeListeners) listener();
|
|
178
198
|
}
|
|
179
199
|
|
|
180
|
-
function
|
|
200
|
+
function notifyColorPreference() {
|
|
201
|
+
for (const listener of preferenceListeners) listener();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isColorPreference(value: string | null): value is NonlaColorPreference {
|
|
205
|
+
return value === "light" || value === "dark" || value === "system";
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function readStoredPreference(): NonlaColorPreference | null {
|
|
181
209
|
if (typeof localStorage === "undefined") return null;
|
|
182
210
|
try {
|
|
183
211
|
const value = localStorage.getItem(NONLA_COLOR_MODE_KEY);
|
|
184
|
-
return value
|
|
212
|
+
return isColorPreference(value) ? value : null;
|
|
185
213
|
} catch {
|
|
186
214
|
return null;
|
|
187
215
|
}
|
|
188
216
|
}
|
|
189
217
|
|
|
218
|
+
function systemColorMode(): NonlaColorMode {
|
|
219
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function") return "dark";
|
|
220
|
+
return window.matchMedia(COLOR_SCHEME_QUERY).matches ? "dark" : "light";
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function resolvedMode(next: NonlaColorPreference): NonlaColorMode {
|
|
224
|
+
return next === "system" ? systemColorMode() : next;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function onSchemeChange() {
|
|
228
|
+
if (colorPreference !== "system") return;
|
|
229
|
+
const next = systemColorMode();
|
|
230
|
+
if (typeof document === "undefined") {
|
|
231
|
+
colorMode = next;
|
|
232
|
+
notifyColorMode();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
applyColorMode(next);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function watchSystemScheme() {
|
|
239
|
+
if (typeof window === "undefined" || typeof window.matchMedia !== "function" || schemeQuery) return;
|
|
240
|
+
schemeQuery = window.matchMedia(COLOR_SCHEME_QUERY);
|
|
241
|
+
schemeQuery.addEventListener("change", onSchemeChange);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function rememberPreference(next: NonlaColorPreference) {
|
|
245
|
+
try {
|
|
246
|
+
localStorage.setItem(NONLA_COLOR_MODE_KEY, next);
|
|
247
|
+
} catch {
|
|
248
|
+
/* private mode */
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
190
252
|
/** Set `light` or `dark` on `target` (default `<html>`) so portals inherit the palette. */
|
|
191
253
|
export function applyColorMode(next: NonlaColorMode, target: HTMLElement = document.documentElement) {
|
|
192
254
|
colorMode = next;
|
|
@@ -196,28 +258,38 @@ export function applyColorMode(next: NonlaColorMode, target: HTMLElement = docum
|
|
|
196
258
|
notifyColorMode();
|
|
197
259
|
}
|
|
198
260
|
|
|
199
|
-
/** Switch
|
|
200
|
-
export function
|
|
261
|
+
/** Switch the stored choice. `system` follows the OS and still paints `light` or `dark`. */
|
|
262
|
+
export function setColorPreference(next: NonlaColorPreference, target?: HTMLElement) {
|
|
263
|
+
colorPreference = next;
|
|
264
|
+
if (next === "system") watchSystemScheme();
|
|
265
|
+
const resolved = resolvedMode(next);
|
|
201
266
|
if (typeof document === "undefined") {
|
|
202
|
-
colorMode =
|
|
267
|
+
colorMode = resolved;
|
|
203
268
|
notifyColorMode();
|
|
269
|
+
notifyColorPreference();
|
|
204
270
|
return;
|
|
205
271
|
}
|
|
206
|
-
applyColorMode(
|
|
272
|
+
applyColorMode(resolved, target);
|
|
207
273
|
colorModeReady = true;
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
274
|
+
rememberPreference(next);
|
|
275
|
+
notifyColorPreference();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Switch mode and remember it. */
|
|
279
|
+
export function setColorMode(next: NonlaColorMode, target?: HTMLElement) {
|
|
280
|
+
setColorPreference(next, target);
|
|
213
281
|
}
|
|
214
282
|
|
|
215
|
-
/** Apply the saved mode once. Falls back to dark. */
|
|
283
|
+
/** Apply the saved mode once. Falls back to dark. A stored `system` choice follows the OS. */
|
|
216
284
|
export function initColorMode(target?: HTMLElement): NonlaColorMode {
|
|
217
285
|
if (typeof document === "undefined") return colorMode;
|
|
218
286
|
if (colorModeReady) return colorMode;
|
|
219
287
|
colorModeReady = true;
|
|
220
288
|
const fromClass = document.documentElement.classList.contains("light") ? "light" : document.documentElement.classList.contains("dark") ? "dark" : null;
|
|
221
|
-
|
|
289
|
+
const next = readStoredPreference() ?? fromClass ?? "dark";
|
|
290
|
+
colorPreference = next;
|
|
291
|
+
if (next === "system") watchSystemScheme();
|
|
292
|
+
applyColorMode(resolvedMode(next), target);
|
|
293
|
+
notifyColorPreference();
|
|
222
294
|
return colorMode;
|
|
223
295
|
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { type AnchorHTMLAttributes, createElement, type HTMLAttributes, type ReactNode } from "react";
|
|
2
|
+
import { cn } from "../lib/cn";
|
|
3
|
+
|
|
4
|
+
export type TypographyType = "secondary" | "success" | "warning" | "danger";
|
|
5
|
+
|
|
6
|
+
export type TitleLevel = 1 | 2 | 3 | 4 | 5;
|
|
7
|
+
|
|
8
|
+
type Decorations = {
|
|
9
|
+
type?: TypographyType;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
mark?: boolean;
|
|
12
|
+
code?: boolean;
|
|
13
|
+
keyboard?: boolean;
|
|
14
|
+
underline?: boolean;
|
|
15
|
+
delete?: boolean;
|
|
16
|
+
strong?: boolean;
|
|
17
|
+
italic?: boolean;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const TYPE_CLASS: Record<TypographyType, string> = {
|
|
21
|
+
secondary: "text-muted-foreground",
|
|
22
|
+
success: "text-success",
|
|
23
|
+
warning: "text-warn",
|
|
24
|
+
danger: "text-destructive",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const TITLE_TAG = {
|
|
28
|
+
1: "h1",
|
|
29
|
+
2: "h2",
|
|
30
|
+
3: "h3",
|
|
31
|
+
4: "h4",
|
|
32
|
+
5: "h5",
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
/** Ant Design seed: body 14/22, headings 38/46, 30/38, 24/32, 20/28, 16/24. */
|
|
36
|
+
const BODY = "text-[14px] leading-[22px]";
|
|
37
|
+
|
|
38
|
+
const TITLE_CLASS: Record<TitleLevel, string> = {
|
|
39
|
+
1: "text-[38px] leading-[46px] font-bold",
|
|
40
|
+
2: "text-[30px] leading-[38px] font-bold",
|
|
41
|
+
3: "text-[24px] leading-[32px] font-semibold",
|
|
42
|
+
4: "text-[20px] leading-[28px] font-semibold",
|
|
43
|
+
5: "text-[16px] leading-[24px] font-semibold",
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const TITLE_SPACE = "nonla-typo mt-0 mb-[0.5em] [.nonla-typo+&]:mt-[1.2em]";
|
|
47
|
+
|
|
48
|
+
function decorationLine(underline?: boolean, deleted?: boolean) {
|
|
49
|
+
if (underline && deleted) return "underline line-through";
|
|
50
|
+
if (underline) return "underline";
|
|
51
|
+
if (deleted) return "line-through";
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function wrapDecorations(children: ReactNode, props: Decorations) {
|
|
56
|
+
let node = children;
|
|
57
|
+
if (props.mark) {
|
|
58
|
+
node = <mark className="rounded-sm bg-[color-mix(in_oklab,var(--nonla-yellow)_40%,transparent)] px-0.5 text-inherit">{node}</mark>;
|
|
59
|
+
}
|
|
60
|
+
if (props.code) {
|
|
61
|
+
node = <code className="rounded-sm bg-secondary px-1 py-px font-mono text-[0.9em]">{node}</code>;
|
|
62
|
+
}
|
|
63
|
+
if (props.keyboard) {
|
|
64
|
+
node = <kbd className="inline-block rounded-md border border-b-2 border-border bg-muted px-1.5 font-mono text-[0.85em] leading-5">{node}</kbd>;
|
|
65
|
+
}
|
|
66
|
+
return node;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function toneClass({ type, disabled }: Decorations) {
|
|
70
|
+
if (disabled) return "cursor-not-allowed text-quaternary-foreground";
|
|
71
|
+
return type ? TYPE_CLASS[type] : undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function decorated(props: Decorations, children: ReactNode, base: string, className?: string, style?: HTMLAttributes<HTMLElement>["style"]) {
|
|
75
|
+
const line = decorationLine(props.underline, props.delete);
|
|
76
|
+
return {
|
|
77
|
+
className: cn(base, toneClass(props), props.strong && "font-semibold", props.italic && "italic", props.underline && "underline-offset-[3px]", className),
|
|
78
|
+
style: line ? { textDecorationLine: line, ...style } : style,
|
|
79
|
+
children: wrapDecorations(children, props),
|
|
80
|
+
"aria-disabled": props.disabled || undefined,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function splitDecorations<T extends Decorations>(props: T) {
|
|
85
|
+
const { type, disabled, mark, code, keyboard, underline, delete: deleted, strong, italic, ...rest } = props;
|
|
86
|
+
return {
|
|
87
|
+
decorations: { type, disabled, mark, code, keyboard, underline, delete: deleted, strong, italic },
|
|
88
|
+
rest,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export type TypographyProps = HTMLAttributes<HTMLDivElement>;
|
|
93
|
+
|
|
94
|
+
function TypographyRoot({ className, ...rest }: TypographyProps) {
|
|
95
|
+
return <div className={cn("text-foreground", className)} {...rest} />;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export type TypographyTextProps = Omit<HTMLAttributes<HTMLSpanElement>, "color"> & Decorations & { children?: ReactNode };
|
|
99
|
+
|
|
100
|
+
export function Text({ className, style, children, ...props }: TypographyTextProps) {
|
|
101
|
+
const { decorations, rest } = splitDecorations(props);
|
|
102
|
+
return <span {...rest} {...decorated(decorations, children, cn(BODY, "text-foreground"), className, style)} />;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type TypographyTitleProps = Omit<HTMLAttributes<HTMLHeadingElement>, "color"> & Decorations & {
|
|
106
|
+
level?: TitleLevel;
|
|
107
|
+
children?: ReactNode;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export function Title({ level = 1, className, style, children, ...props }: TypographyTitleProps) {
|
|
111
|
+
const { decorations, rest } = splitDecorations(props);
|
|
112
|
+
return createElement(TITLE_TAG[level], { ...rest, ...decorated(decorations, children, cn(TITLE_SPACE, "text-foreground", TITLE_CLASS[level]), className, style) });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type TypographyParagraphProps = Omit<HTMLAttributes<HTMLParagraphElement>, "color"> & Decorations & { children?: ReactNode };
|
|
116
|
+
|
|
117
|
+
export function Paragraph({ className, style, children, ...props }: TypographyParagraphProps) {
|
|
118
|
+
const { decorations, rest } = splitDecorations(props);
|
|
119
|
+
return <p {...rest} {...decorated(decorations, children, "nonla-typo m-0 text-[15px] leading-7 text-foreground [p.nonla-typo+&]:mt-3", className, style)} />;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export type TypographyLinkProps = Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "color"> & Decorations & { children?: ReactNode };
|
|
123
|
+
|
|
124
|
+
export function Link({ href, target, rel, className, style, children, onClick, ...props }: TypographyLinkProps) {
|
|
125
|
+
const { decorations, rest } = splitDecorations(props);
|
|
126
|
+
const view = decorated(
|
|
127
|
+
decorations,
|
|
128
|
+
children,
|
|
129
|
+
cn(
|
|
130
|
+
"cursor-pointer underline-offset-[3px]",
|
|
131
|
+
BODY,
|
|
132
|
+
decorations.type || decorations.disabled ? undefined : "text-brand hover:text-brand-700",
|
|
133
|
+
decorations.disabled || decorations.underline ? undefined : "hover:underline",
|
|
134
|
+
),
|
|
135
|
+
className,
|
|
136
|
+
style,
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<a
|
|
141
|
+
{...rest}
|
|
142
|
+
{...view}
|
|
143
|
+
href={decorations.disabled ? undefined : href}
|
|
144
|
+
target={target}
|
|
145
|
+
rel={target === "_blank" && rel == null ? "noreferrer noopener" : rel}
|
|
146
|
+
tabIndex={decorations.disabled ? -1 : rest.tabIndex}
|
|
147
|
+
onClick={(event) => {
|
|
148
|
+
if (decorations.disabled) {
|
|
149
|
+
event.preventDefault();
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
onClick?.(event);
|
|
153
|
+
}}
|
|
154
|
+
/>
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export const Typography = Object.assign(TypographyRoot, { Text, Title, Paragraph, Link });
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
type FenceScan = {
|
|
2
|
-
mermaidCount: number;
|
|
3
|
-
hasUnclosedMermaid: boolean;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export function scanMermaidFences(markdown: string): FenceScan {
|
|
7
|
-
let openLang: string | null = null;
|
|
8
|
-
let mermaidCount = 0;
|
|
9
|
-
|
|
10
|
-
for (const line of markdown.split("\n")) {
|
|
11
|
-
const m = /^(```+|~~~+)(.*)$/.exec(line);
|
|
12
|
-
if (!m) continue;
|
|
13
|
-
|
|
14
|
-
const info = m[2].trim();
|
|
15
|
-
if (openLang === null) {
|
|
16
|
-
openLang = (info.split(/\s+/)[0] ?? "").toLowerCase();
|
|
17
|
-
if (openLang === "mermaid") mermaidCount += 1;
|
|
18
|
-
} else if (!info) {
|
|
19
|
-
openLang = null;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return { mermaidCount, hasUnclosedMermaid: openLang === "mermaid" };
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Body of the still-open mermaid fence (no closing ```), or null if none. */
|
|
27
|
-
export function getOpenMermaidBody(markdown: string): string | null {
|
|
28
|
-
let openLang: string | null = null;
|
|
29
|
-
let bodyStart = -1;
|
|
30
|
-
const lines = markdown.split("\n");
|
|
31
|
-
|
|
32
|
-
for (let i = 0; i < lines.length; i++) {
|
|
33
|
-
const m = /^(```+|~~~+)(.*)$/.exec(lines[i]!);
|
|
34
|
-
if (!m) continue;
|
|
35
|
-
|
|
36
|
-
const info = m[2]!.trim();
|
|
37
|
-
if (openLang === null) {
|
|
38
|
-
openLang = (info.split(/\s+/)[0] ?? "").toLowerCase();
|
|
39
|
-
bodyStart = openLang === "mermaid" ? i + 1 : -1;
|
|
40
|
-
} else if (!info) {
|
|
41
|
-
openLang = null;
|
|
42
|
-
bodyStart = -1;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (openLang !== "mermaid" || bodyStart < 0) return null;
|
|
47
|
-
return lines.slice(bodyStart).join("\n").replace(/\n$/, "");
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** True when this mermaid code block is the still-streaming (unclosed) fence. */
|
|
51
|
-
export function isPendingMermaidBlock(markdown: string, codeText: string, streaming: boolean): boolean {
|
|
52
|
-
if (!streaming) return false;
|
|
53
|
-
const openBody = getOpenMermaidBody(markdown);
|
|
54
|
-
return openBody !== null && openBody === codeText;
|
|
55
|
-
}
|
|
File without changes
|