devnonla-ui 0.8.0 → 0.9.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 -4
- package/package.json +3 -3
- package/src/app/App.tsx +5 -1
- package/src/app/context.ts +5 -3
- package/src/button/Button.tsx +2 -2
- package/src/button/ButtonCopy.tsx +2 -2
- package/src/card/Card.tsx +81 -0
- package/src/chat/AgentChatbox.tsx +1 -1
- package/src/chat/AgentPanel.tsx +1 -1
- package/src/chat/message-ui/ChatError.tsx +2 -2
- package/src/chat/message-ui/ChatInput.tsx +5 -5
- package/src/chat/message-ui/ChatMarkdown.tsx +8 -7
- package/src/chat/message-ui/ChatThinking.tsx +3 -3
- package/src/chat/message-ui/ChatUserMessage.tsx +3 -3
- package/src/chat/message-ui/MermaidBlock.tsx +68 -39
- package/src/chat/tool-ui/BackgroundTasksBar.tsx +3 -3
- package/src/chat/tool-ui/CallAgentToolUI.tsx +3 -3
- package/src/chat/tool-ui/ChatToolCall.tsx +4 -4
- package/src/chat/tool-ui/GetCurrentTimeToolUI.tsx +2 -2
- package/src/chat/tool-ui/ReadSkillToolUI.tsx +2 -2
- package/src/chat/tool-ui/RunJsToolUI.tsx +2 -2
- package/src/chat/tool-ui/ToolUiTrailing.tsx +3 -3
- package/src/chat/tool-ui/WebFetchToolUI.tsx +2 -2
- package/src/checkbox/Checkbox.tsx +17 -4
- package/src/codeblock/CodeBlock.tsx +2 -2
- package/src/datepicker/DatePicker.tsx +3 -3
- package/src/desktop/DesktopHeader.tsx +1 -1
- package/src/desktop/DesktopIcon.tsx +2 -2
- package/src/desktop/DesktopWindow.tsx +51 -29
- package/src/empty/Empty.tsx +8 -5
- package/src/form/FormItem.tsx +1 -1
- package/src/form-layout/FormLayout.tsx +1 -1
- package/src/icon/SolarIcon.tsx +39 -0
- package/src/icon/solar.ts +172 -0
- package/src/index.ts +24 -12
- package/src/input/EditableInput.tsx +3 -3
- package/src/input/Input.tsx +3 -3
- package/src/lib/sizes.ts +9 -6
- package/src/lib/surface.ts +1 -1
- package/src/markdown-editor/MarkdownDiff.tsx +211 -0
- package/src/markdown-editor/MarkdownEditor.tsx +294 -0
- package/src/markdown-editor/MarkdownPreview.tsx +326 -0
- package/src/markdown-editor/MarkdownSource.tsx +72 -0
- package/src/markdown-editor/blocks.ts +329 -0
- package/src/markdown-editor/diff.ts +296 -0
- package/src/menu/Menu.tsx +3 -3
- package/src/pagination/Pagination.tsx +8 -7
- package/src/segmented/Segmented.tsx +2 -2
- package/src/select/Select.tsx +192 -63
- package/src/sidebar/Sidebar.tsx +9 -8
- package/src/spin/Spin.tsx +17 -11
- package/src/styles.css +265 -110
- package/src/switch/Switch.tsx +17 -3
- package/src/table/Table.tsx +61 -26
- package/src/theme-toggle/ThemeToggle.tsx +33 -0
- package/src/theme.ts +72 -0
- package/src/timepicker/TimePicker.tsx +1 -1
- package/src/icon/FluentIcon.tsx +0 -28
- package/src/icon/fluent.ts +0 -75
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { type MouseEvent, type ReactNode, useLayoutEffect, useRef } from "react";
|
|
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";
|
|
8
|
+
import { cn } from "../lib/cn";
|
|
9
|
+
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
10
|
+
import { type InlineEdit, type MarkdownBlock, type MarkdownDoc, readFence, readInlineEdit, writeInlineEdit } from "./blocks";
|
|
11
|
+
|
|
12
|
+
const documentClass = cn(
|
|
13
|
+
"min-w-0 text-[15px] leading-7 text-foreground",
|
|
14
|
+
"[&_h1]:my-0 [&_h1]:text-[1.75rem] [&_h1]:leading-tight [&_h1]:font-semibold",
|
|
15
|
+
"[&_h2]:my-0 [&_h2]:text-[1.35rem] [&_h2]:leading-tight [&_h2]:font-semibold",
|
|
16
|
+
"[&_h3]:my-0 [&_h3]:text-[1.15rem] [&_h3]:leading-snug [&_h3]:font-semibold",
|
|
17
|
+
"[&_:is(h4,h5,h6)]:my-0 [&_:is(h4,h5,h6)]:text-base [&_:is(h4,h5,h6)]:font-semibold",
|
|
18
|
+
"[&_p]:my-0",
|
|
19
|
+
"[&_ul]:my-1 [&_ul]:list-disc [&_ul]:pl-6",
|
|
20
|
+
"[&_ol]:my-1 [&_ol]:list-decimal [&_ol]:pl-6",
|
|
21
|
+
"[&_ul.contains-task-list]:list-none",
|
|
22
|
+
"[&_li]:my-0.5",
|
|
23
|
+
"[&_li.task-list-item]:relative",
|
|
24
|
+
"[&_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",
|
|
25
|
+
"[&_blockquote]:my-1 [&_blockquote]:border-l-2 [&_blockquote]:border-border [&_blockquote]:pl-3 [&_blockquote]:text-muted-foreground",
|
|
26
|
+
"[&_a]:text-link [&_a]:underline-offset-[3px] hover:[&_a]:underline",
|
|
27
|
+
"[&_hr]:my-2 [&_hr]:border-border",
|
|
28
|
+
"[&_strong]:font-semibold",
|
|
29
|
+
"[&_img]:my-1 [&_img]:max-w-full [&_img]:rounded-lg",
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const previewComponents: Components = {
|
|
33
|
+
code({ className, children }) {
|
|
34
|
+
const match = /language-(\w+)/.exec(className || "");
|
|
35
|
+
const lang = match?.[1] ?? "";
|
|
36
|
+
const codeText = String(children).replace(/\n$/, "");
|
|
37
|
+
const isBlock = codeText.includes("\n") || !!match;
|
|
38
|
+
if (!isBlock) {
|
|
39
|
+
return <code className="rounded-sm bg-secondary px-1 py-0.5 font-mono text-[0.9em]">{children}</code>;
|
|
40
|
+
}
|
|
41
|
+
if (lang.toLowerCase() === "mermaid") return <MermaidView code={codeText} />;
|
|
42
|
+
return <CodeBlock code={codeText} language={lang || undefined} className="my-2" />;
|
|
43
|
+
},
|
|
44
|
+
table({ children }) {
|
|
45
|
+
return <ChatMarkdownTable>{children}</ChatMarkdownTable>;
|
|
46
|
+
},
|
|
47
|
+
a({ href, children, ...props }) {
|
|
48
|
+
return (
|
|
49
|
+
<a href={href} target="_blank" rel="noopener noreferrer" {...props}>
|
|
50
|
+
{children}
|
|
51
|
+
</a>
|
|
52
|
+
);
|
|
53
|
+
},
|
|
54
|
+
input({ type, checked }) {
|
|
55
|
+
if (type !== "checkbox") return <input type={type} checked={checked} readOnly />;
|
|
56
|
+
return (
|
|
57
|
+
<span className="nonla-md-task-check pointer-events-none inline-flex h-7 items-center">
|
|
58
|
+
<Checkbox
|
|
59
|
+
checked={Boolean(checked)}
|
|
60
|
+
tabIndex={-1}
|
|
61
|
+
className="data-[state=checked]:border-white data-[state=checked]:bg-white data-[state=checked]:text-(--nonla-bg) data-[state=indeterminate]:border-white data-[state=indeterminate]:bg-white data-[state=indeterminate]:text-(--nonla-bg)"
|
|
62
|
+
/>
|
|
63
|
+
</span>
|
|
64
|
+
);
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
function MermaidView({ code }: { code: string }) {
|
|
69
|
+
if (!code.trim()) {
|
|
70
|
+
return <div className="rounded-xl border border-dashed border-border px-4 py-6 text-center text-xs text-muted-foreground">Empty diagram</div>;
|
|
71
|
+
}
|
|
72
|
+
return <MermaidBlock className="my-0">{code}</MermaidBlock>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function headingClass(level: number): string {
|
|
76
|
+
if (level === 1) return "text-[1.75rem] font-semibold leading-tight";
|
|
77
|
+
if (level === 2) return "text-[1.35rem] font-semibold leading-tight";
|
|
78
|
+
if (level === 3) return "text-[1.15rem] font-semibold leading-snug";
|
|
79
|
+
return "text-base font-semibold leading-snug";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
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
|
+
const ref = useRef<HTMLTextAreaElement>(null);
|
|
84
|
+
|
|
85
|
+
useLayoutEffect(() => {
|
|
86
|
+
const el = ref.current;
|
|
87
|
+
if (!el) return;
|
|
88
|
+
el.focus();
|
|
89
|
+
const pos = caret === "start" ? 0 : el.value.length;
|
|
90
|
+
el.setSelectionRange(pos, pos);
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
useLayoutEffect(() => {
|
|
94
|
+
const el = ref.current;
|
|
95
|
+
if (!el) return;
|
|
96
|
+
el.style.height = "auto";
|
|
97
|
+
el.style.height = `${el.scrollHeight}px`;
|
|
98
|
+
}, [value]);
|
|
99
|
+
|
|
100
|
+
return (
|
|
101
|
+
<textarea
|
|
102
|
+
ref={ref}
|
|
103
|
+
data-md-editor="block"
|
|
104
|
+
value={value}
|
|
105
|
+
rows={1}
|
|
106
|
+
spellCheck={!mono}
|
|
107
|
+
onChange={(event) => onChange(event.target.value)}
|
|
108
|
+
onBlur={onBlur}
|
|
109
|
+
onKeyDown={(event) => {
|
|
110
|
+
if (event.key === "Escape") {
|
|
111
|
+
event.preventDefault();
|
|
112
|
+
event.currentTarget.blur();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (event.key === "Enter" && onEnter && !event.shiftKey && !event.nativeEvent.isComposing) {
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
const el = event.currentTarget;
|
|
118
|
+
onEnter(value.slice(0, el.selectionStart), value.slice(el.selectionEnd));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (event.key !== "Tab") return;
|
|
122
|
+
event.preventDefault();
|
|
123
|
+
const el = event.currentTarget;
|
|
124
|
+
const start = el.selectionStart;
|
|
125
|
+
const end = el.selectionEnd;
|
|
126
|
+
const next = `${value.slice(0, start)} ${value.slice(end)}`;
|
|
127
|
+
onChange(next);
|
|
128
|
+
const pos = start + 2;
|
|
129
|
+
requestAnimationFrame(() => {
|
|
130
|
+
el.selectionStart = pos;
|
|
131
|
+
el.selectionEnd = pos;
|
|
132
|
+
});
|
|
133
|
+
}}
|
|
134
|
+
className={cn(
|
|
135
|
+
"block w-full resize-none overflow-hidden border-0 bg-transparent p-0 text-[15px] leading-7 text-foreground shadow-none outline-none ring-0 placeholder:text-placeholder focus:outline-none focus:ring-0",
|
|
136
|
+
mono && "font-mono text-[13px] leading-5",
|
|
137
|
+
className,
|
|
138
|
+
)}
|
|
139
|
+
aria-label="Edit markdown"
|
|
140
|
+
/>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function interactiveTarget(target: EventTarget | null): boolean {
|
|
145
|
+
return target instanceof Element && !!target.closest("a, button, textarea, input");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export type MarkdownPreviewProps = {
|
|
149
|
+
doc: MarkdownDoc;
|
|
150
|
+
editingIndex: number | null;
|
|
151
|
+
readOnly?: boolean;
|
|
152
|
+
placeholder?: string;
|
|
153
|
+
onStartEdit: (index: number) => boolean;
|
|
154
|
+
onEditMouseDown: (index: number, event: MouseEvent) => void;
|
|
155
|
+
onChangeBody: (body: string) => void;
|
|
156
|
+
onBlurBlock: () => void;
|
|
157
|
+
onEnterList: (before: string, after: string) => void;
|
|
158
|
+
/** Where the caret goes when an edit starts. */
|
|
159
|
+
caret?: "start" | "end";
|
|
160
|
+
onAppend: () => void;
|
|
161
|
+
};
|
|
162
|
+
|
|
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
|
|
224
|
+
checked={view.checked}
|
|
225
|
+
tabIndex={-1}
|
|
226
|
+
className="data-[state=checked]:border-white data-[state=checked]:bg-white data-[state=checked]:text-(--nonla-bg) data-[state=indeterminate]:border-white data-[state=indeterminate]:bg-white data-[state=indeterminate]:text-(--nonla-bg)"
|
|
227
|
+
/>
|
|
228
|
+
</span>
|
|
229
|
+
) : view.ordered ? (
|
|
230
|
+
<span className="w-6 shrink-0 text-right text-[15px] leading-7 tabular-nums">{ordinal}.</span>
|
|
231
|
+
) : (
|
|
232
|
+
<span className="flex h-7 w-5 shrink-0 items-center justify-center" aria-hidden>
|
|
233
|
+
<span className="size-1.5 rounded-full bg-current" />
|
|
234
|
+
</span>
|
|
235
|
+
)}
|
|
236
|
+
<div className="min-w-0 flex-1">{children}</div>
|
|
237
|
+
</div>
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function isListBlock(block: MarkdownBlock): boolean {
|
|
242
|
+
return block.kind === "text" && readInlineEdit(block.body).kind === "list";
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function spaceBefore(block: MarkdownBlock, prev?: MarkdownBlock): string {
|
|
246
|
+
if (!prev) return "";
|
|
247
|
+
if (isListBlock(block) && isListBlock(prev)) return "mt-0.5";
|
|
248
|
+
if (block.kind === "text" && readInlineEdit(block.body).kind === "heading") return "mt-6";
|
|
249
|
+
return "mt-3";
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function BlockBody({ block }: { block: MarkdownBlock }): ReactNode {
|
|
253
|
+
if (block.kind === "mermaid") {
|
|
254
|
+
const fence = readFence(block.body);
|
|
255
|
+
return <MermaidView code={fence?.code ?? block.body} />;
|
|
256
|
+
}
|
|
257
|
+
if (block.kind === "code") {
|
|
258
|
+
const fence = readFence(block.body);
|
|
259
|
+
if (!fence) return <CodeBlock code={block.body} className="my-0" />;
|
|
260
|
+
return <CodeBlock code={fence.code} language={fence.lang || undefined} className="my-0" />;
|
|
261
|
+
}
|
|
262
|
+
const view = readInlineEdit(block.body);
|
|
263
|
+
if (view.kind === "list") {
|
|
264
|
+
return (
|
|
265
|
+
<ListItem view={view}>
|
|
266
|
+
<RichText source={view.text} />
|
|
267
|
+
</ListItem>
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
return <RichText source={block.body} />;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Write markdown…", onStartEdit, onEditMouseDown, onChangeBody, onBlurBlock, onEnterList, caret = "end", onAppend }: MarkdownPreviewProps) {
|
|
274
|
+
return (
|
|
275
|
+
<OverlayScroll className="h-full bg-card">
|
|
276
|
+
<div className="mx-auto flex w-full max-w-3xl flex-col px-8 py-6">
|
|
277
|
+
{doc.blocks.map((block, index) => {
|
|
278
|
+
const editing = editingIndex === index;
|
|
279
|
+
const locked = block.kind === "mermaid" || readOnly;
|
|
280
|
+
const view = block.kind === "text" ? readInlineEdit(block.body) : null;
|
|
281
|
+
const editor =
|
|
282
|
+
view?.kind === "heading" ? (
|
|
283
|
+
<BlockEditor value={view.text} className={headingClass(view.level)} caret={caret} onChange={(text) => onChangeBody(writeInlineEdit(block.body, text))} onBlur={onBlurBlock} />
|
|
284
|
+
) : view?.kind === "list" ? (
|
|
285
|
+
<ListItem view={view}>
|
|
286
|
+
<BlockEditor value={view.text} caret={caret} onEnter={onEnterList} onChange={(text) => onChangeBody(writeInlineEdit(block.body, text))} onBlur={onBlurBlock} />
|
|
287
|
+
</ListItem>
|
|
288
|
+
) : (
|
|
289
|
+
<BlockEditor value={block.body} mono={block.kind === "code"} caret={caret} onChange={onChangeBody} onBlur={onBlurBlock} />
|
|
290
|
+
);
|
|
291
|
+
return (
|
|
292
|
+
<div
|
|
293
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: block position is the edit identity
|
|
294
|
+
key={`${index}:${block.kind}`}
|
|
295
|
+
role="button"
|
|
296
|
+
data-md-block={block.kind}
|
|
297
|
+
tabIndex={locked ? undefined : 0}
|
|
298
|
+
className={cn("outline-none", !locked && "cursor-text", spaceBefore(block, doc.blocks[index - 1]))}
|
|
299
|
+
onMouseDown={(event) => onEditMouseDown(index, event)}
|
|
300
|
+
onClick={(event) => {
|
|
301
|
+
if (locked || editing || interactiveTarget(event.target)) return;
|
|
302
|
+
const selection = window.getSelection();
|
|
303
|
+
if (selection && selection.toString().length > 0) return;
|
|
304
|
+
onStartEdit(index);
|
|
305
|
+
}}
|
|
306
|
+
onKeyDown={(event) => {
|
|
307
|
+
if (locked || editing || event.key !== "Enter") return;
|
|
308
|
+
event.preventDefault();
|
|
309
|
+
onStartEdit(index);
|
|
310
|
+
}}
|
|
311
|
+
>
|
|
312
|
+
{editing ? editor : <BlockBody block={block} />}
|
|
313
|
+
</div>
|
|
314
|
+
);
|
|
315
|
+
})}
|
|
316
|
+
{readOnly ? (
|
|
317
|
+
doc.blocks.length === 0 ? <p className="m-0 text-sm text-placeholder">{placeholder}</p> : null
|
|
318
|
+
) : (
|
|
319
|
+
<button type="button" className="mt-3 min-h-16 text-left text-sm text-placeholder" onClick={onAppend} aria-label="Add paragraph">
|
|
320
|
+
{doc.blocks.length === 0 ? placeholder : ""}
|
|
321
|
+
</button>
|
|
322
|
+
)}
|
|
323
|
+
</div>
|
|
324
|
+
</OverlayScroll>
|
|
325
|
+
);
|
|
326
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { cn } from "../lib/cn";
|
|
3
|
+
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
4
|
+
|
|
5
|
+
const LINE = 20;
|
|
6
|
+
const PAD = 8;
|
|
7
|
+
|
|
8
|
+
export type MarkdownSourceProps = {
|
|
9
|
+
value: string;
|
|
10
|
+
onChange: (value: string) => void;
|
|
11
|
+
readOnly?: boolean;
|
|
12
|
+
placeholder?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Full-file text editor with a line gutter, in the style of a light Monaco buffer. */
|
|
16
|
+
export function MarkdownSource({ value, onChange, readOnly, placeholder = "Write markdown…" }: MarkdownSourceProps) {
|
|
17
|
+
const [cursor, setCursor] = useState(0);
|
|
18
|
+
const lines = value.split("\n");
|
|
19
|
+
const active = value.slice(0, cursor).split("\n").length - 1;
|
|
20
|
+
const longest = lines.reduce((max, line) => Math.max(max, line.length), 0);
|
|
21
|
+
|
|
22
|
+
const remember = (el: HTMLTextAreaElement) => setCursor(el.selectionStart);
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<OverlayScroll className="h-full bg-card" innerClassName="overflow-x-auto">
|
|
26
|
+
<div className="relative flex min-h-full" style={{ width: `max(100%, calc(3.5rem + ${longest + 2}ch))` }}>
|
|
27
|
+
<div aria-hidden className="pointer-events-none absolute inset-x-0 bg-ink-hover" style={{ top: PAD + active * LINE, height: LINE }} />
|
|
28
|
+
<div className="sticky left-0 z-20 w-12 shrink-0 select-none pt-2 text-right font-mono text-[12px] leading-5 text-quaternary-foreground">
|
|
29
|
+
{lines.map((_, index) => (
|
|
30
|
+
// Line number is the row identity.
|
|
31
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: gutter row is the line number
|
|
32
|
+
<div key={index} className={cn("h-5 pr-3", index === active && "text-muted-foreground")}>
|
|
33
|
+
{index + 1}
|
|
34
|
+
</div>
|
|
35
|
+
))}
|
|
36
|
+
</div>
|
|
37
|
+
<textarea
|
|
38
|
+
value={value}
|
|
39
|
+
readOnly={readOnly}
|
|
40
|
+
placeholder={placeholder}
|
|
41
|
+
spellCheck={false}
|
|
42
|
+
wrap="off"
|
|
43
|
+
aria-label="Markdown source"
|
|
44
|
+
onChange={(event) => {
|
|
45
|
+
onChange(event.target.value);
|
|
46
|
+
remember(event.currentTarget);
|
|
47
|
+
}}
|
|
48
|
+
onClick={(event) => remember(event.currentTarget)}
|
|
49
|
+
onKeyUp={(event) => remember(event.currentTarget)}
|
|
50
|
+
onSelect={(event) => remember(event.currentTarget)}
|
|
51
|
+
onKeyDown={(event) => {
|
|
52
|
+
if (readOnly || event.key !== "Tab") return;
|
|
53
|
+
event.preventDefault();
|
|
54
|
+
const el = event.currentTarget;
|
|
55
|
+
const start = el.selectionStart;
|
|
56
|
+
const end = el.selectionEnd;
|
|
57
|
+
const next = `${value.slice(0, start)} ${value.slice(end)}`;
|
|
58
|
+
onChange(next);
|
|
59
|
+
const pos = start + 2;
|
|
60
|
+
requestAnimationFrame(() => {
|
|
61
|
+
el.selectionStart = pos;
|
|
62
|
+
el.selectionEnd = pos;
|
|
63
|
+
setCursor(pos);
|
|
64
|
+
});
|
|
65
|
+
}}
|
|
66
|
+
className="relative z-10 block w-full resize-none overflow-hidden bg-transparent py-2 pr-4 font-mono text-[13px] leading-5 text-foreground outline-none placeholder:text-placeholder"
|
|
67
|
+
style={{ height: Math.max(lines.length, 1) * LINE + PAD * 2 }}
|
|
68
|
+
/>
|
|
69
|
+
</div>
|
|
70
|
+
</OverlayScroll>
|
|
71
|
+
);
|
|
72
|
+
}
|