devnonla-ui 0.18.1 → 0.19.1
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/package.json +2 -2
- package/src/chat/AgentChatbox.tsx +10 -3
- package/src/chat/AgentPanel.tsx +10 -3
- package/src/chat/index.ts +3 -1
- package/src/chat/message-ui/ChatInput.tsx +472 -98
- package/src/chat/message-ui/ChatSuggestions.tsx +105 -0
- package/src/chat/message-ui/ChatWelcome.tsx +6 -2
- package/src/chat/message-ui/commandChips.ts +127 -0
- package/src/css.d.ts +1 -0
- package/src/index.ts +0 -4
- package/src/markdown-editor/MarkdownEditor.tsx +9 -1
- package/src/markdown-editor/MarkdownPreview.tsx +83 -16
- package/src/markdown-editor/MarkdownViewer.tsx +18 -11
- package/src/markdown-editor/ReactCodeRunner.tsx +3 -3
- package/src/markdown-editor/ReactCodeSandbox.tsx +100 -30
- package/src/markdown-editor/reactSandboxFrame.css +14 -0
- package/src/markdown-editor/reactSandboxFrame.ts +26 -0
- package/src/styles.css +0 -1
- package/src/avatar/AgentAvatar.tsx +0 -332
- package/src/avatar/AgentBody.tsx +0 -458
- package/src/avatar/avatar.css +0 -600
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { createContext, type ReactNode, useContext, useEffect, useId, useRef } from "react";
|
|
2
|
+
import { menuItemClass } from "../../dropdown/menuClasses";
|
|
3
|
+
import { cn } from "../../lib/cn";
|
|
4
|
+
/** A marker that opens a suggestion menu, e.g. `/`, `@`, or `#`. */
|
|
5
|
+
export type ChatTrigger = {
|
|
6
|
+
/** Text that opens the menu when it starts a word. No spaces. */
|
|
7
|
+
trigger: string;
|
|
8
|
+
/** Menu shown while the trigger is open. Use `useChatSuggest` inside for the live query. `null` or `false` disables this trigger. */
|
|
9
|
+
menu: ReactNode;
|
|
10
|
+
/** Chip background as hex, e.g. `#6366f1`. Defaults to amber `#c9841a`. */
|
|
11
|
+
color?: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type ChatToken = {
|
|
15
|
+
trigger: string;
|
|
16
|
+
query: string;
|
|
17
|
+
start: number;
|
|
18
|
+
end: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** What a custom menu can read while it is open. */
|
|
22
|
+
export type ChatSuggest = {
|
|
23
|
+
query: string;
|
|
24
|
+
trigger: string;
|
|
25
|
+
/**
|
|
26
|
+
* Replace the active token with a chip. `value` is stored and sent, e.g. `/plan` or `@file`.
|
|
27
|
+
* `label` is what the chip shows. Omit it to show `value`.
|
|
28
|
+
*/
|
|
29
|
+
insert: (value: string, label?: string) => void;
|
|
30
|
+
close: () => void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type SuggestOption = { value: string; label?: string };
|
|
34
|
+
|
|
35
|
+
type ChatSuggestContextValue = ChatSuggest & {
|
|
36
|
+
activeId: string | null;
|
|
37
|
+
register: (id: string, option: SuggestOption) => () => void;
|
|
38
|
+
highlight: (id: string) => void;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const ChatSuggestContext = createContext<ChatSuggestContextValue | null>(null);
|
|
42
|
+
|
|
43
|
+
export function useChatSuggest(): ChatSuggest {
|
|
44
|
+
const value = useContext(ChatSuggestContext);
|
|
45
|
+
if (!value) throw new Error("useChatSuggest must be used inside ChatInput triggers");
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Token at the caret when a word starts with one of `triggers`. Longest trigger wins. */
|
|
50
|
+
export function chatTokenAt(text: string, caret: number, triggers: readonly string[]): ChatToken | null {
|
|
51
|
+
if (caret < 0 || caret > text.length) return null;
|
|
52
|
+
let start = caret;
|
|
53
|
+
while (start > 0 && !/\s/.test(text[start - 1] ?? "")) start -= 1;
|
|
54
|
+
let end = caret;
|
|
55
|
+
while (end < text.length && !/\s/.test(text[end] ?? "")) end += 1;
|
|
56
|
+
if (start >= end) return null;
|
|
57
|
+
const word = text.slice(start, end);
|
|
58
|
+
let match: string | null = null;
|
|
59
|
+
for (const trigger of triggers) {
|
|
60
|
+
if (trigger && word.startsWith(trigger) && (match === null || trigger.length > match.length)) match = trigger;
|
|
61
|
+
}
|
|
62
|
+
if (match === null) return null;
|
|
63
|
+
return { trigger: match, query: word.slice(match.length), start, end };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function ChatSuggestProvider({ value, children }: { value: ChatSuggestContextValue; children: ReactNode }) {
|
|
67
|
+
return <ChatSuggestContext.Provider value={value}>{children}</ChatSuggestContext.Provider>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* One row in a custom menu. Arrow keys and Enter use these rows.
|
|
72
|
+
* `value` is sent. `label` is shown in the chip; omit it to show `value`.
|
|
73
|
+
*/
|
|
74
|
+
export function ChatSuggestOption({ value, label, children }: { value: string; label?: string; children: ReactNode }) {
|
|
75
|
+
const suggest = useContext(ChatSuggestContext);
|
|
76
|
+
const id = useId();
|
|
77
|
+
const ref = useRef<HTMLButtonElement>(null);
|
|
78
|
+
if (!suggest) throw new Error("ChatSuggestOption must be used inside ChatInput triggers");
|
|
79
|
+
const { insert, activeId, register, highlight } = suggest;
|
|
80
|
+
const active = activeId === id;
|
|
81
|
+
|
|
82
|
+
useEffect(() => register(id, { value, label }), [id, register, value, label]);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (!active) return;
|
|
86
|
+
ref.current?.scrollIntoView({ block: "nearest" });
|
|
87
|
+
}, [active]);
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<button
|
|
91
|
+
ref={ref}
|
|
92
|
+
id={`chat-suggest-${id}`}
|
|
93
|
+
type="button"
|
|
94
|
+
role="option"
|
|
95
|
+
aria-selected={active}
|
|
96
|
+
data-highlighted={active ? "" : undefined}
|
|
97
|
+
onMouseEnter={() => highlight(id)}
|
|
98
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
99
|
+
onClick={() => insert(value, label)}
|
|
100
|
+
className={cn(menuItemClass, "w-full cursor-pointer text-left")}
|
|
101
|
+
>
|
|
102
|
+
{children}
|
|
103
|
+
</button>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
|
-
import { AgentAvatar } from "../../avatar/AgentAvatar";
|
|
3
2
|
import { Button } from "../../button/Button";
|
|
4
3
|
import { cn } from "../../lib/cn";
|
|
5
4
|
|
|
@@ -17,7 +16,12 @@ export type ChatWelcomeProps = {
|
|
|
17
16
|
const DEFAULT_STARTERS = ["What can you help me with?", "Brainstorm a few ideas with me", "Walk me through how you work"] as const;
|
|
18
17
|
|
|
19
18
|
function DefaultAvatar({ name }: { name: string }) {
|
|
20
|
-
|
|
19
|
+
const initial = (name.trim()[0] ?? "?").toUpperCase();
|
|
20
|
+
return (
|
|
21
|
+
<div className="flex size-16 items-center justify-center rounded-full bg-brand/20 text-xl font-semibold text-brand-700" aria-hidden>
|
|
22
|
+
{initial}
|
|
23
|
+
</div>
|
|
24
|
+
);
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
export function ChatWelcome({ name, description, avatar, starters = [...DEFAULT_STARTERS], onStarter, disabled, className }: ChatWelcomeProps) {
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
export type ComposerCaret = { index: number; offset: number };
|
|
2
|
+
|
|
3
|
+
export type ComposerPart =
|
|
4
|
+
| { type: "text"; id: string; value: string }
|
|
5
|
+
| { type: "command"; id: string; label: string; value: string; title?: string; color?: string };
|
|
6
|
+
|
|
7
|
+
export function emptyComposer(value = ""): { parts: ComposerPart[]; caret: ComposerCaret } {
|
|
8
|
+
return { parts: [{ type: "text", id: "t-0", value }], caret: { index: 0, offset: value.length } };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function isComposerEmpty(parts: readonly ComposerPart[]): boolean {
|
|
12
|
+
return parts.every((part) => part.type === "text" && part.value.length === 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function serializeComposer(parts: readonly ComposerPart[]): string {
|
|
16
|
+
return parts.map((part) => part.value).join("");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Keep a text run on both sides of every chip so the caret can sit inline. */
|
|
20
|
+
export function normalizeParts(parts: readonly ComposerPart[], caret: ComposerCaret | null): { parts: ComposerPart[]; caret: ComposerCaret | null } {
|
|
21
|
+
const acc: ComposerPart[] = [];
|
|
22
|
+
const caretOut: { current: ComposerCaret | null } = { current: null };
|
|
23
|
+
|
|
24
|
+
const pushText = (value: string, fromIndex: number, localCaret: number | null) => {
|
|
25
|
+
const prev = acc[acc.length - 1];
|
|
26
|
+
if (prev?.type === "text") {
|
|
27
|
+
const base = prev.value.length;
|
|
28
|
+
prev.value += value;
|
|
29
|
+
if (localCaret !== null && caret?.index === fromIndex) caretOut.current = { index: acc.length - 1, offset: base + localCaret };
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
acc.push({ type: "text", id: `t-${acc.length}`, value });
|
|
33
|
+
if (localCaret !== null && caret?.index === fromIndex) caretOut.current = { index: acc.length - 1, offset: localCaret };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
parts.forEach((part, index) => {
|
|
37
|
+
if (part.type === "text") {
|
|
38
|
+
pushText(part.value, index, caret?.index === index ? caret.offset : null);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (acc.length === 0 || acc[acc.length - 1]?.type !== "text") pushText("", -1, null);
|
|
42
|
+
acc.push({ ...part });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (acc.length === 0 || acc[0]?.type !== "text") acc.unshift({ type: "text", id: "t-0", value: "" });
|
|
46
|
+
if (acc[acc.length - 1]?.type !== "text") acc.push({ type: "text", id: "t-end", value: "" });
|
|
47
|
+
|
|
48
|
+
const next = acc.map((part, index) => (part.type === "text" ? { ...part, id: `t-${index}` } : part));
|
|
49
|
+
const placed = caretOut.current;
|
|
50
|
+
if (!placed) return { parts: next, caret: null };
|
|
51
|
+
const at = next[placed.index];
|
|
52
|
+
if (!at || at.type !== "text") return { parts: next, caret: null };
|
|
53
|
+
return { parts: next, caret: { index: placed.index, offset: Math.min(Math.max(placed.offset, 0), at.value.length) } };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function insertTextAt(parts: readonly ComposerPart[], caret: ComposerCaret, text: string): { parts: ComposerPart[]; caret: ComposerCaret } {
|
|
57
|
+
const part = parts[caret.index];
|
|
58
|
+
if (!part || part.type !== "text") return { parts: [...parts], caret };
|
|
59
|
+
const value = part.value.slice(0, caret.offset) + text + part.value.slice(caret.offset);
|
|
60
|
+
const next = parts.map((item, index) => (index === caret.index && item.type === "text" ? { ...item, value } : item));
|
|
61
|
+
const normalized = normalizeParts(next, { index: caret.index, offset: caret.offset + text.length });
|
|
62
|
+
return { parts: normalized.parts, caret: normalized.caret ?? caret };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function insertCommandAt(
|
|
66
|
+
parts: readonly ComposerPart[],
|
|
67
|
+
caret: ComposerCaret,
|
|
68
|
+
tokenStart: number,
|
|
69
|
+
tokenEnd: number,
|
|
70
|
+
chip: { id: string; label: string; value: string; color?: string },
|
|
71
|
+
): { parts: ComposerPart[]; caret: ComposerCaret } {
|
|
72
|
+
const next: ComposerPart[] = [];
|
|
73
|
+
let caretIndex = caret.index;
|
|
74
|
+
let caretOffset = caret.offset;
|
|
75
|
+
parts.forEach((part, index) => {
|
|
76
|
+
if (part.type !== "text" || index !== caret.index) {
|
|
77
|
+
next.push(part);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const before = part.value.slice(0, tokenStart);
|
|
81
|
+
let after = part.value.slice(tokenEnd);
|
|
82
|
+
if (after === "" || !/^\s/.test(after)) after = ` ${after}`;
|
|
83
|
+
next.push({ type: "text", id: part.id, value: before });
|
|
84
|
+
next.push({ type: "command", ...chip });
|
|
85
|
+
caretIndex = next.length;
|
|
86
|
+
caretOffset = 1;
|
|
87
|
+
next.push({ type: "text", id: `${chip.id}-after`, value: after });
|
|
88
|
+
});
|
|
89
|
+
const normalized = normalizeParts(next, { index: caretIndex, offset: caretOffset });
|
|
90
|
+
return { parts: normalized.parts, caret: normalized.caret ?? { index: 0, offset: 0 } };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Insert a chip at the caret. Adds one space on each side when the text needs it. */
|
|
94
|
+
export function insertChipAt(parts: readonly ComposerPart[], caret: ComposerCaret, chip: { id: string; label: string; value: string; title?: string; color?: string }): { parts: ComposerPart[]; caret: ComposerCaret } {
|
|
95
|
+
const part = parts[caret.index];
|
|
96
|
+
if (!part || part.type !== "text") return { parts: [...parts], caret };
|
|
97
|
+
const head = part.value.slice(0, caret.offset);
|
|
98
|
+
const tail = part.value.slice(caret.offset);
|
|
99
|
+
const before = head.length > 0 && !/\s$/.test(head) ? `${head} ` : head;
|
|
100
|
+
const after = /^\s/.test(tail) ? tail : ` ${tail}`;
|
|
101
|
+
const next: ComposerPart[] = [
|
|
102
|
+
...parts.slice(0, caret.index),
|
|
103
|
+
{ ...part, value: before },
|
|
104
|
+
{ type: "command", ...chip },
|
|
105
|
+
{ type: "text", id: `${chip.id}-after`, value: after },
|
|
106
|
+
...parts.slice(caret.index + 1),
|
|
107
|
+
];
|
|
108
|
+
const normalized = normalizeParts(next, { index: caret.index + 2, offset: 1 });
|
|
109
|
+
return { parts: normalized.parts, caret: normalized.caret ?? caret };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function removeCommandPart(parts: readonly ComposerPart[], id: string): { parts: ComposerPart[]; caret: ComposerCaret } {
|
|
113
|
+
const index = parts.findIndex((part) => part.type === "command" && part.id === id);
|
|
114
|
+
if (index < 0) return { parts: [...parts], caret: { index: 0, offset: 0 } };
|
|
115
|
+
const left = parts[index - 1];
|
|
116
|
+
const next = parts.flatMap((part, partIndex) => {
|
|
117
|
+
if (partIndex === index) return [];
|
|
118
|
+
if (partIndex === index + 1 && part.type === "text" && left?.type === "text" && left.value.endsWith(" ") && part.value.startsWith(" ")) {
|
|
119
|
+
return [{ ...part, value: part.value.slice(1) }];
|
|
120
|
+
}
|
|
121
|
+
return [part];
|
|
122
|
+
});
|
|
123
|
+
const leftIndex = Math.max(0, index - 1);
|
|
124
|
+
const offset = left?.type === "text" ? left.value.length : 0;
|
|
125
|
+
const normalized = normalizeParts(next, { index: leftIndex, offset });
|
|
126
|
+
return { parts: normalized.parts, caret: normalized.caret ?? { index: 0, offset: 0 } };
|
|
127
|
+
}
|
package/src/css.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module "*.css";
|
package/src/index.ts
CHANGED
|
@@ -15,10 +15,6 @@ export type InputRef = HTMLInputElement;
|
|
|
15
15
|
|
|
16
16
|
export type { AlertProps, AlertType } from "./alert/Alert";
|
|
17
17
|
export { Alert } from "./alert/Alert";
|
|
18
|
-
export type { AgentAvatarConfig, AgentAvatarEyeColor, AgentAvatarLook, AgentAvatarMotion, AgentAvatarProps } from "./avatar/AgentAvatar";
|
|
19
|
-
export { AGENT_AVATAR_PARTS, AgentAvatar } from "./avatar/AgentAvatar";
|
|
20
|
-
export type { AgentBodyConfig, AgentBodyLook, AgentBodyMotion, AgentBodyProps } from "./avatar/AgentBody";
|
|
21
|
-
export { AGENT_BODY_PARTS, AgentBody } from "./avatar/AgentBody";
|
|
22
18
|
export type { BlobShapeAppearance, BlobShapeProps, BlobShapeVariant } from "./blob-shape/BlobShape";
|
|
23
19
|
export { BlobShape } from "./blob-shape/BlobShape";
|
|
24
20
|
export type { CalendarProps } from "./calendar/Calendar";
|
|
@@ -27,6 +27,12 @@ export type MarkdownEditorProps = {
|
|
|
27
27
|
defaultDiffLayout?: MarkdownDiffLayout;
|
|
28
28
|
onDiffLayoutChange?: (layout: MarkdownDiffLayout) => void;
|
|
29
29
|
readOnly?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Show Preview / Edit / Diff in the header. Default `true`.
|
|
32
|
+
* Set `false` for inline-only editing (stay on `mode` / `defaultMode`, usually `preview`).
|
|
33
|
+
* Hidden automatically when `readOnly`.
|
|
34
|
+
*/
|
|
35
|
+
showModes?: boolean;
|
|
30
36
|
placeholder?: string;
|
|
31
37
|
title?: ReactNode;
|
|
32
38
|
originalLabel?: ReactNode;
|
|
@@ -73,6 +79,7 @@ function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (v
|
|
|
73
79
|
* Preview renders the document. Click a heading, list item, or paragraph to edit that text in place.
|
|
74
80
|
* Mermaid fences and registered fence components render in place and stay view only.
|
|
75
81
|
* Edit is the full-file text buffer. Diff compares `original` and `value` like Monaco.
|
|
82
|
+
* `showModes={false}` hides the mode switcher for inline-only editing.
|
|
76
83
|
* `readOnly` locks editing and hides the mode switcher.
|
|
77
84
|
* A finished document with no editor chrome uses `MarkdownViewer`.
|
|
78
85
|
*/
|
|
@@ -88,6 +95,7 @@ export function MarkdownEditor({
|
|
|
88
95
|
defaultDiffLayout = "side-by-side",
|
|
89
96
|
onDiffLayoutChange,
|
|
90
97
|
readOnly = false,
|
|
98
|
+
showModes: showModesProp = true,
|
|
91
99
|
placeholder = "Write markdown…",
|
|
92
100
|
title,
|
|
93
101
|
originalLabel = "Original",
|
|
@@ -243,7 +251,7 @@ export function MarkdownEditor({
|
|
|
243
251
|
|
|
244
252
|
const doc = live?.doc ?? parseMarkdownDoc(valueRef.current);
|
|
245
253
|
const model = useMemo(() => (mode === "diff" && original !== undefined ? diffMarkdown(original, value) : null), [mode, original, value]);
|
|
246
|
-
const showModes = !readOnly;
|
|
254
|
+
const showModes = !readOnly && showModesProp;
|
|
247
255
|
const showHeader = Boolean(title) || showModes || model !== null;
|
|
248
256
|
|
|
249
257
|
return (
|
|
@@ -1,10 +1,30 @@
|
|
|
1
1
|
import { type MouseEvent, useLayoutEffect, useRef } from "react";
|
|
2
2
|
import { cn } from "../lib/cn";
|
|
3
|
+
import { markdownParagraphClass } from "../lib/markdownScale";
|
|
3
4
|
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
4
5
|
import { type MarkdownDoc, readInlineEdit, writeInlineEdit } from "./blocks";
|
|
5
|
-
import { BlockBody, headingClass, isCustomFence, ListItem, type MarkdownComponents, markdownScaleStyle, spaceBefore } from "./MarkdownViewer";
|
|
6
|
+
import { BlockBody, headingClass, isCustomFence, ListItem, type MarkdownComponents, markdownScaleStyle, RichText, spaceBefore } from "./MarkdownViewer";
|
|
6
7
|
|
|
7
|
-
function BlockEditor({
|
|
8
|
+
function BlockEditor({
|
|
9
|
+
value,
|
|
10
|
+
mono,
|
|
11
|
+
className,
|
|
12
|
+
caret = "end",
|
|
13
|
+
autoSize = true,
|
|
14
|
+
onChange,
|
|
15
|
+
onBlur,
|
|
16
|
+
onEnter,
|
|
17
|
+
}: {
|
|
18
|
+
value: string;
|
|
19
|
+
mono?: boolean;
|
|
20
|
+
className?: string;
|
|
21
|
+
caret?: "start" | "end";
|
|
22
|
+
/** When false, height comes from the layout sizer behind the field (list overlay). */
|
|
23
|
+
autoSize?: boolean;
|
|
24
|
+
onChange: (value: string) => void;
|
|
25
|
+
onBlur: () => void;
|
|
26
|
+
onEnter?: (before: string, after: string) => void;
|
|
27
|
+
}) {
|
|
8
28
|
const ref = useRef<HTMLTextAreaElement>(null);
|
|
9
29
|
|
|
10
30
|
useLayoutEffect(() => {
|
|
@@ -16,11 +36,12 @@ function BlockEditor({ value, mono, className, caret = "end", onChange, onBlur,
|
|
|
16
36
|
}, []);
|
|
17
37
|
|
|
18
38
|
useLayoutEffect(() => {
|
|
39
|
+
if (!autoSize) return;
|
|
19
40
|
const el = ref.current;
|
|
20
41
|
if (!el) return;
|
|
21
42
|
el.style.height = "auto";
|
|
22
43
|
el.style.height = `${el.scrollHeight}px`;
|
|
23
|
-
}, [value]);
|
|
44
|
+
}, [value, autoSize]);
|
|
24
45
|
|
|
25
46
|
return (
|
|
26
47
|
<textarea
|
|
@@ -57,8 +78,10 @@ function BlockEditor({ value, mono, className, caret = "end", onChange, onBlur,
|
|
|
57
78
|
});
|
|
58
79
|
}}
|
|
59
80
|
className={cn(
|
|
60
|
-
"block w-full resize-none overflow-hidden border-0 bg-transparent
|
|
61
|
-
|
|
81
|
+
"block w-full resize-none overflow-hidden border-0 bg-transparent font-[inherit] text-foreground shadow-none outline-none ring-0 placeholder:text-quaternary-foreground focus:outline-none focus:ring-0",
|
|
82
|
+
// Match rendered paragraph rhythm so the block does not jump when editing starts.
|
|
83
|
+
mono ? "p-0 font-mono text-(length:--md-code-size,var(--nonla-mono-text-size)) leading-(--md-code-leading,calc(var(--nonla-mono-text-size)*1.6))" : markdownParagraphClass,
|
|
84
|
+
!autoSize && "h-full",
|
|
62
85
|
className,
|
|
63
86
|
)}
|
|
64
87
|
aria-label="Edit markdown"
|
|
@@ -70,6 +93,10 @@ function interactiveTarget(target: EventTarget | null): boolean {
|
|
|
70
93
|
return target instanceof Element && !!target.closest("a, button, textarea, input");
|
|
71
94
|
}
|
|
72
95
|
|
|
96
|
+
/** Soft pad behind the active block. Pseudo-element keeps layout from shifting. */
|
|
97
|
+
const editingSurfaceClass =
|
|
98
|
+
"relative z-0 before:pointer-events-none before:absolute before:-inset-x-2 before:-inset-y-0.5 before:-z-10 before:rounded-md before:bg-ink-hover before:content-['']";
|
|
99
|
+
|
|
73
100
|
export type MarkdownPreviewProps = {
|
|
74
101
|
doc: MarkdownDoc;
|
|
75
102
|
editingIndex: number | null;
|
|
@@ -95,16 +122,6 @@ export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Wr
|
|
|
95
122
|
const editing = editingIndex === index;
|
|
96
123
|
const locked = block.kind === "mermaid" || (block.kind === "code" && isCustomFence(block.body, components)) || readOnly;
|
|
97
124
|
const view = block.kind === "text" ? readInlineEdit(block.body) : null;
|
|
98
|
-
const editor =
|
|
99
|
-
view?.kind === "heading" ? (
|
|
100
|
-
<BlockEditor value={view.text} className={headingClass(view.level)} caret={caret} onChange={(text) => onChangeBody(writeInlineEdit(block.body, text))} onBlur={onBlurBlock} />
|
|
101
|
-
) : view?.kind === "list" ? (
|
|
102
|
-
<ListItem view={view}>
|
|
103
|
-
<BlockEditor value={view.text} caret={caret} onEnter={onEnterList} onChange={(text) => onChangeBody(writeInlineEdit(block.body, text))} onBlur={onBlurBlock} />
|
|
104
|
-
</ListItem>
|
|
105
|
-
) : (
|
|
106
|
-
<BlockEditor value={block.body} mono={block.kind === "code"} caret={caret} onChange={onChangeBody} onBlur={onBlurBlock} />
|
|
107
|
-
);
|
|
108
125
|
const gap = spaceBefore(block, doc.blocks[index - 1]);
|
|
109
126
|
if (locked) {
|
|
110
127
|
return (
|
|
@@ -118,6 +135,56 @@ export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Wr
|
|
|
118
135
|
</div>
|
|
119
136
|
);
|
|
120
137
|
}
|
|
138
|
+
// List keeps the rendered row as a sizer so the bullet and neighbors do not shift on edit.
|
|
139
|
+
if (view?.kind === "list") {
|
|
140
|
+
return (
|
|
141
|
+
<div
|
|
142
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: block position is the edit identity
|
|
143
|
+
key={`${index}:${block.kind}`}
|
|
144
|
+
role="button"
|
|
145
|
+
data-md-block={block.kind}
|
|
146
|
+
tabIndex={0}
|
|
147
|
+
className={cn("cursor-text outline-none", gap, editing && editingSurfaceClass)}
|
|
148
|
+
onMouseDown={(event) => onEditMouseDown(index, event)}
|
|
149
|
+
onClick={(event) => {
|
|
150
|
+
if (editing || interactiveTarget(event.target)) return;
|
|
151
|
+
const selection = window.getSelection();
|
|
152
|
+
if (selection && selection.toString().length > 0) return;
|
|
153
|
+
onStartEdit(index);
|
|
154
|
+
}}
|
|
155
|
+
onKeyDown={(event) => {
|
|
156
|
+
if (editing || event.key !== "Enter") return;
|
|
157
|
+
event.preventDefault();
|
|
158
|
+
onStartEdit(index);
|
|
159
|
+
}}
|
|
160
|
+
>
|
|
161
|
+
<ListItem view={view}>
|
|
162
|
+
<div className="relative">
|
|
163
|
+
<div className={cn(editing && "invisible")} aria-hidden={editing || undefined}>
|
|
164
|
+
<RichText source={view.text} />
|
|
165
|
+
</div>
|
|
166
|
+
{editing ? (
|
|
167
|
+
<BlockEditor
|
|
168
|
+
value={view.text}
|
|
169
|
+
autoSize={false}
|
|
170
|
+
className="absolute inset-0"
|
|
171
|
+
caret={caret}
|
|
172
|
+
onEnter={onEnterList}
|
|
173
|
+
onChange={(text) => onChangeBody(writeInlineEdit(block.body, text))}
|
|
174
|
+
onBlur={onBlurBlock}
|
|
175
|
+
/>
|
|
176
|
+
) : null}
|
|
177
|
+
</div>
|
|
178
|
+
</ListItem>
|
|
179
|
+
</div>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
const editor =
|
|
183
|
+
view?.kind === "heading" ? (
|
|
184
|
+
<BlockEditor value={view.text} className={headingClass(view.level)} caret={caret} onChange={(text) => onChangeBody(writeInlineEdit(block.body, text))} onBlur={onBlurBlock} />
|
|
185
|
+
) : (
|
|
186
|
+
<BlockEditor value={block.body} mono={block.kind === "code"} caret={caret} onChange={onChangeBody} onBlur={onBlurBlock} />
|
|
187
|
+
);
|
|
121
188
|
return (
|
|
122
189
|
<div
|
|
123
190
|
// biome-ignore lint/suspicious/noArrayIndexKey: block position is the edit identity
|
|
@@ -125,7 +192,7 @@ export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Wr
|
|
|
125
192
|
role="button"
|
|
126
193
|
data-md-block={block.kind}
|
|
127
194
|
tabIndex={0}
|
|
128
|
-
className={cn("cursor-text outline-none", gap)}
|
|
195
|
+
className={cn("cursor-text outline-none", gap, editing && editingSurfaceClass)}
|
|
129
196
|
onMouseDown={(event) => onEditMouseDown(index, event)}
|
|
130
197
|
onClick={(event) => {
|
|
131
198
|
if (editing || interactiveTarget(event.target)) return;
|
|
@@ -122,7 +122,7 @@ function splitSoftBreaks(node: MdNode): MdNode[] {
|
|
|
122
122
|
return out;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
function RichText({ source }: { source: string }) {
|
|
125
|
+
export function RichText({ source }: { source: string }) {
|
|
126
126
|
return (
|
|
127
127
|
<div className={documentClass}>
|
|
128
128
|
<ReactMarkdown remarkPlugins={[remarkGfm, remarkKeepLineBreaks]} components={previewComponents}>
|
|
@@ -132,6 +132,13 @@ function RichText({ source }: { source: string }) {
|
|
|
132
132
|
);
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
/**
|
|
136
|
+
* List body uses leading-none plus padding that equals the old half-leading + py-0.5.
|
|
137
|
+
* Paragraphs and the block textarea then share the same text origin, so edit does not nudge the line.
|
|
138
|
+
*/
|
|
139
|
+
const listBodyClass =
|
|
140
|
+
"[&_p]:my-0! [&_p]:leading-none! [&_p]:py-[calc((var(--md-p-leading,1.6)*1em-1em)/2+0.125rem)]! [&_textarea]:my-0! [&_textarea]:leading-none! [&_textarea]:py-[calc((var(--md-p-leading,1.6)*1em-1em)/2+0.125rem)]!";
|
|
141
|
+
|
|
135
142
|
export function ListItem({ view, children }: { view: Extract<InlineEdit, { kind: "list" }>; children: ReactNode }) {
|
|
136
143
|
const level = Math.floor(view.indent.length / 2);
|
|
137
144
|
const ordinal = Number(/^(\d+)/.exec(view.marker)?.[1] ?? "1");
|
|
@@ -151,7 +158,7 @@ export function ListItem({ view, children }: { view: Extract<InlineEdit, { kind:
|
|
|
151
158
|
<span className="mt-[calc((1lh-0.375rem)/2)] size-1.5 rounded-full bg-current" />
|
|
152
159
|
</span>
|
|
153
160
|
)}
|
|
154
|
-
<div className="min-w-0 flex-1
|
|
161
|
+
<div className={cn("min-w-0 flex-1", listBodyClass)}>{children}</div>
|
|
155
162
|
</div>
|
|
156
163
|
);
|
|
157
164
|
}
|
|
@@ -163,8 +170,9 @@ function isListBlock(block: MarkdownBlock): boolean {
|
|
|
163
170
|
export function spaceBefore(block: MarkdownBlock, prev?: MarkdownBlock): string {
|
|
164
171
|
if (!prev) return "";
|
|
165
172
|
if (isListBlock(block) && isListBlock(prev)) return "mt-0.5";
|
|
166
|
-
|
|
173
|
+
// List rows keep their own padding model; do not reuse the paragraph-after-heading trim.
|
|
167
174
|
if (isListBlock(block) && prev.kind === "text") return "mt-1";
|
|
175
|
+
if (prev.kind === "text" && readInlineEdit(prev.body).kind === "heading") return "[&_p:first-child]:mt-0! [&_p:first-child]:pt-0.5! [&_textarea]:mt-0! [&_textarea]:pt-0.5!";
|
|
168
176
|
if (block.kind === "text" && prev.kind === "text") return "";
|
|
169
177
|
return "mt-3";
|
|
170
178
|
}
|
|
@@ -225,13 +233,13 @@ function fenceComponent(components: MarkdownComponents | undefined, lang: string
|
|
|
225
233
|
|
|
226
234
|
type LiveReactOptions = {
|
|
227
235
|
trustedModules?: Record<string, unknown>;
|
|
228
|
-
|
|
236
|
+
sandbox?: boolean;
|
|
229
237
|
showHeader?: boolean;
|
|
230
238
|
showCode?: boolean;
|
|
231
239
|
components?: MarkdownComponents;
|
|
232
240
|
};
|
|
233
241
|
|
|
234
|
-
export function BlockBody({ block, streaming, trustedModules,
|
|
242
|
+
export function BlockBody({ block, streaming, trustedModules, sandbox, showHeader, showCode, components }: { block: MarkdownBlock; streaming?: boolean } & LiveReactOptions): ReactNode {
|
|
235
243
|
if (block.kind === "mermaid") {
|
|
236
244
|
const fence = readFence(block.body);
|
|
237
245
|
const code = fence?.code ?? block.body;
|
|
@@ -244,7 +252,7 @@ export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHe
|
|
|
244
252
|
const closed = fenceClosed(block.body);
|
|
245
253
|
// An unclosed fence is still being written, so it never runs.
|
|
246
254
|
if (fence.lang === "live-react" && closed) {
|
|
247
|
-
if (
|
|
255
|
+
if (sandbox) return <ReactCodeSandbox key={fence.code} code={fence.code} showHeader={showHeader} showCode={showCode} />;
|
|
248
256
|
if (trustedModules) return <ReactCode code={fence.code} trustedModules={trustedModules} showHeader={showHeader} showCode={showCode} />;
|
|
249
257
|
}
|
|
250
258
|
const Custom = fence.lang === "live-react" ? undefined : fenceComponent(components, fence.lang);
|
|
@@ -273,10 +281,9 @@ export type MarkdownViewerProps = {
|
|
|
273
281
|
className?: string;
|
|
274
282
|
/**
|
|
275
283
|
* Runs ```live-react fences in a sandboxed iframe. Use this for markdown written by
|
|
276
|
-
* users or agents.
|
|
277
|
-
* Wins over `trustedModules` when both are set.
|
|
284
|
+
* users or agents. Wins over `trustedModules` when both are set.
|
|
278
285
|
*/
|
|
279
|
-
|
|
286
|
+
sandbox?: boolean;
|
|
280
287
|
/**
|
|
281
288
|
* Runs ```live-react fences in this page, with its cookies, storage, and network.
|
|
282
289
|
* Only for markdown you wrote. `react` is provided. Other imports must be listed here.
|
|
@@ -296,7 +303,7 @@ export type MarkdownViewerProps = {
|
|
|
296
303
|
};
|
|
297
304
|
|
|
298
305
|
/** Read-only markdown. Same blocks as the editor preview, without editing. */
|
|
299
|
-
export function MarkdownViewer({ value = "", streaming = false,
|
|
306
|
+
export function MarkdownViewer({ value = "", streaming = false, sandbox = false, trustedModules, showHeader = true, showCode = true, placeholder, className, style, components }: MarkdownViewerProps) {
|
|
300
307
|
const doc = useMemo(() => parseMarkdownDoc(value), [value]);
|
|
301
308
|
if (doc.blocks.length === 0) {
|
|
302
309
|
return placeholder ? <p className={cn("m-0 text-sm text-quaternary-foreground", className)}>{placeholder}</p> : null;
|
|
@@ -310,7 +317,7 @@ export function MarkdownViewer({ value = "", streaming = false, sandboxSrc, trus
|
|
|
310
317
|
data-md-block={block.kind}
|
|
311
318
|
className={cn("outline-none", spaceBefore(block, doc.blocks[index - 1]))}
|
|
312
319
|
>
|
|
313
|
-
<BlockBody block={block} streaming={streaming} trustedModules={trustedModules}
|
|
320
|
+
<BlockBody block={block} streaming={streaming} trustedModules={trustedModules} sandbox={sandbox} showHeader={showHeader} showCode={showCode} components={components} />
|
|
314
321
|
</div>
|
|
315
322
|
))}
|
|
316
323
|
</div>
|
|
@@ -25,11 +25,11 @@ class Boundary extends Component<{ report: Report; children: ReactNode }, { erro
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* Runner side of the sandbox.
|
|
28
|
+
* Runner side of the sandbox. `ReactCodeSandbox` loads this from its own frame.
|
|
29
29
|
*
|
|
30
30
|
* `modules` is the only thing the markdown can import, and the host cannot change it.
|
|
31
|
-
*
|
|
32
|
-
*
|
|
31
|
+
* The frame document sets `Content-Security-Policy: connect-src 'none'`. Its scripts
|
|
32
|
+
* load cross-origin from an opaque origin, so those assets need `Access-Control-Allow-Origin`.
|
|
33
33
|
*/
|
|
34
34
|
export function mountReactCodeRunner(root: HTMLElement, modules: Record<string, unknown>): () => void {
|
|
35
35
|
root.classList.add("nonla-ui");
|