devnonla-ui 0.7.1 → 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 +6 -6
- package/src/chat/message-ui/ChatMarkdown.tsx +8 -7
- package/src/chat/message-ui/ChatMarkdownTable.tsx +1 -1
- 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 +76 -70
- 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 +4 -10
- 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/scroll/OverlayScroll.tsx +2 -2
- 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 +273 -107
- package/src/switch/Switch.tsx +17 -3
- package/src/table/Table.tsx +63 -28
- 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,211 @@
|
|
|
1
|
+
import { type ReactNode, useState } from "react";
|
|
2
|
+
import { cn } from "../lib/cn";
|
|
3
|
+
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
4
|
+
import { type DiffLine, type FoldItem, foldContext, type MarkdownDiffModel } from "./diff";
|
|
5
|
+
|
|
6
|
+
export type MarkdownDiffLayout = "inline" | "side-by-side";
|
|
7
|
+
|
|
8
|
+
function rowBg(kind: DiffLine["kind"]): string {
|
|
9
|
+
if (kind === "insert") return "bg-[color-mix(in_oklab,var(--success)_16%,var(--card))]";
|
|
10
|
+
if (kind === "delete") return "bg-[color-mix(in_oklab,var(--destructive)_14%,var(--card))]";
|
|
11
|
+
if (kind === "empty") return "bg-secondary/45";
|
|
12
|
+
return "bg-card";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function charBg(kind: DiffLine["kind"]): string {
|
|
16
|
+
if (kind === "insert") return "bg-[color-mix(in_oklab,var(--success)_42%,var(--card))]";
|
|
17
|
+
if (kind === "delete") return "bg-[color-mix(in_oklab,var(--destructive)_36%,var(--card))]";
|
|
18
|
+
return "";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function marker(kind: DiffLine["kind"]): string {
|
|
22
|
+
if (kind === "insert") return "+";
|
|
23
|
+
if (kind === "delete") return "−";
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function DiffText({ line }: { line: DiffLine }) {
|
|
28
|
+
if (line.kind === "empty" || line.spans.length === 0) return <span className="inline-block min-h-5">{"\u00a0"}</span>;
|
|
29
|
+
const parts: { key: string; text: string; changed: boolean }[] = [];
|
|
30
|
+
let offset = 0;
|
|
31
|
+
for (const span of line.spans) {
|
|
32
|
+
parts.push({ key: `${offset}:${span.changed ? "c" : "s"}`, text: span.text, changed: span.changed });
|
|
33
|
+
offset += span.text.length + 1;
|
|
34
|
+
}
|
|
35
|
+
return (
|
|
36
|
+
<>
|
|
37
|
+
{parts.map((part) => (
|
|
38
|
+
<span key={part.key} className={cn(part.changed && "rounded-xs", part.changed && charBg(line.kind))}>
|
|
39
|
+
{part.text}
|
|
40
|
+
</span>
|
|
41
|
+
))}
|
|
42
|
+
</>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function Gutter({ value, className }: { value?: number; className?: string }) {
|
|
47
|
+
return <span className={cn("w-10 shrink-0 select-none pr-2 text-right font-mono text-[11px] leading-5 text-quaternary-foreground tabular-nums", className)}>{value ?? ""}</span>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function Mark({ kind }: { kind: DiffLine["kind"] }) {
|
|
51
|
+
return (
|
|
52
|
+
<span aria-hidden className={cn("w-4 shrink-0 text-center font-mono text-[13px] leading-5", kind === "insert" && "text-success", kind === "delete" && "text-destructive")}>
|
|
53
|
+
{marker(kind)}
|
|
54
|
+
</span>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function LineBody({ line }: { line: DiffLine }) {
|
|
59
|
+
return (
|
|
60
|
+
<span className="min-h-5 whitespace-pre pr-4 font-mono text-[13px] leading-5">
|
|
61
|
+
<DiffText line={line} />
|
|
62
|
+
</span>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function FoldButton({ count, onClick, span }: { count: number; onClick: () => void; span?: boolean }) {
|
|
67
|
+
return (
|
|
68
|
+
<button type="button" className={cn("flex w-full items-center justify-center border-y border-border bg-secondary/70 py-0.5 text-xs text-link hover:bg-secondary", span && "col-span-2")} onClick={onClick}>
|
|
69
|
+
Show {count} unchanged {count === 1 ? "line" : "lines"}
|
|
70
|
+
</button>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function useFolds(): [Set<number>, (id: number) => void] {
|
|
75
|
+
const [open, setOpen] = useState<Set<number>>(() => new Set());
|
|
76
|
+
const toggle = (id: number) => {
|
|
77
|
+
setOpen((current) => {
|
|
78
|
+
const next = new Set(current);
|
|
79
|
+
if (next.has(id)) next.delete(id);
|
|
80
|
+
else next.add(id);
|
|
81
|
+
return next;
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
return [open, toggle];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function visibleNodes<T>(entries: FoldItem<T>[], open: Set<number>, toggle: (id: number) => void, span: boolean, renderItem: (item: T, key: string) => ReactNode): ReactNode[] {
|
|
88
|
+
const nodes: ReactNode[] = [];
|
|
89
|
+
entries.forEach((entry, index) => {
|
|
90
|
+
if (entry.type === "fold" && !open.has(entry.id)) {
|
|
91
|
+
nodes.push(<FoldButton key={`fold-${entry.id}`} count={entry.count} span={span} onClick={() => toggle(entry.id)} />);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const items = entry.type === "fold" ? entry.items : [entry.item];
|
|
95
|
+
items.forEach((item, itemIndex) => {
|
|
96
|
+
nodes.push(renderItem(item, `${index}-${itemIndex}`));
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
return nodes;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function SideCell({ line, num }: { line: DiffLine; num?: number }) {
|
|
103
|
+
return (
|
|
104
|
+
<div className="flex min-h-5 min-w-max">
|
|
105
|
+
<Gutter value={num} />
|
|
106
|
+
<Mark kind={line.kind} />
|
|
107
|
+
<LineBody line={line} />
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function InlineLine({ line }: { line: DiffLine }) {
|
|
113
|
+
const bg = rowBg(line.kind);
|
|
114
|
+
return (
|
|
115
|
+
<div className={cn("flex min-h-5 min-w-full w-max", bg)}>
|
|
116
|
+
<Gutter value={line.kind === "insert" ? undefined : line.oldNo} className={cn("sticky left-0 z-10", bg)} />
|
|
117
|
+
<Gutter value={line.kind === "delete" ? undefined : line.newNo} className={cn("sticky left-10 z-10", bg)} />
|
|
118
|
+
<Mark kind={line.kind} />
|
|
119
|
+
<LineBody line={line} />
|
|
120
|
+
</div>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type MarkdownDiffProps = {
|
|
125
|
+
model: MarkdownDiffModel;
|
|
126
|
+
layout: MarkdownDiffLayout;
|
|
127
|
+
originalLabel?: ReactNode;
|
|
128
|
+
modifiedLabel?: ReactNode;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export function MarkdownDiff({ model, layout, originalLabel = "Original", modifiedLabel = "Modified" }: MarkdownDiffProps) {
|
|
132
|
+
const [openSplit, toggleSplit] = useFolds();
|
|
133
|
+
const [openInline, toggleInline] = useFolds();
|
|
134
|
+
|
|
135
|
+
if (layout === "inline") {
|
|
136
|
+
const folded = foldContext(model.inline, (line) => line.kind === "equal");
|
|
137
|
+
return (
|
|
138
|
+
<OverlayScroll className="h-full bg-card" innerClassName="overflow-x-auto">
|
|
139
|
+
<div role="region" aria-label="Markdown diff" className="min-w-full">
|
|
140
|
+
<div className="sticky top-0 z-20 border-b border-border bg-card px-3 py-1.5 text-xs font-medium text-muted-foreground">
|
|
141
|
+
{originalLabel}
|
|
142
|
+
<span className="px-1.5 text-quaternary-foreground">→</span>
|
|
143
|
+
{modifiedLabel}
|
|
144
|
+
</div>
|
|
145
|
+
{visibleNodes(folded, openInline, toggleInline, false, (line, key) => (
|
|
146
|
+
<InlineLine key={`${key}-${line.kind}-${line.oldNo ?? "x"}-${line.newNo ?? "x"}`} line={line} />
|
|
147
|
+
))}
|
|
148
|
+
</div>
|
|
149
|
+
</OverlayScroll>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return <SplitDiff model={model} open={openSplit} toggle={toggleSplit} originalLabel={originalLabel} modifiedLabel={modifiedLabel} />;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function SplitDiff({ model, open, toggle, originalLabel, modifiedLabel }: { model: MarkdownDiffModel; open: Set<number>; toggle: (id: number) => void; originalLabel: ReactNode; modifiedLabel: ReactNode }) {
|
|
157
|
+
const folded = foldContext(model.rows, (row) => row.left.kind === "equal" && row.right.kind === "equal");
|
|
158
|
+
const chunks: ReactNode[] = [];
|
|
159
|
+
let left: ReactNode[] = [];
|
|
160
|
+
let right: ReactNode[] = [];
|
|
161
|
+
let segment = 0;
|
|
162
|
+
|
|
163
|
+
const flush = () => {
|
|
164
|
+
if (left.length === 0) return;
|
|
165
|
+
const id = segment;
|
|
166
|
+
segment += 1;
|
|
167
|
+
chunks.push(
|
|
168
|
+
<div key={`seg-${id}`} className="flex w-full items-start">
|
|
169
|
+
<div className="nonla-scroll-hover w-1/2 min-w-0 overflow-x-auto">{left}</div>
|
|
170
|
+
<div className="nonla-scroll-hover w-1/2 min-w-0 overflow-x-auto border-l border-border">{right}</div>
|
|
171
|
+
</div>,
|
|
172
|
+
);
|
|
173
|
+
left = [];
|
|
174
|
+
right = [];
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
folded.forEach((entry, index) => {
|
|
178
|
+
if (entry.type === "fold" && !open.has(entry.id)) {
|
|
179
|
+
flush();
|
|
180
|
+
chunks.push(<FoldButton key={`fold-${entry.id}`} count={entry.count} onClick={() => toggle(entry.id)} />);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const rows = entry.type === "fold" ? entry.items : [entry.item];
|
|
184
|
+
rows.forEach((row, rowIndex) => {
|
|
185
|
+
const key = `${index}-${rowIndex}-${row.left.oldNo ?? "x"}-${row.right.newNo ?? "x"}`;
|
|
186
|
+
left.push(
|
|
187
|
+
<div key={key} className={cn("flex min-h-5 w-max min-w-full", rowBg(row.left.kind))}>
|
|
188
|
+
<SideCell line={row.left} num={row.left.oldNo} />
|
|
189
|
+
</div>,
|
|
190
|
+
);
|
|
191
|
+
right.push(
|
|
192
|
+
<div key={key} className={cn("flex min-h-5 w-max min-w-full", rowBg(row.right.kind))}>
|
|
193
|
+
<SideCell line={row.right} num={row.right.newNo} />
|
|
194
|
+
</div>,
|
|
195
|
+
);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
flush();
|
|
199
|
+
|
|
200
|
+
return (
|
|
201
|
+
<OverlayScroll className="h-full bg-card">
|
|
202
|
+
<div role="region" aria-label="Markdown diff" className="min-w-full">
|
|
203
|
+
<div className="sticky top-0 z-20 grid grid-cols-2 border-b border-border bg-card text-xs font-medium text-muted-foreground">
|
|
204
|
+
<div className="px-3 py-1.5">{originalLabel}</div>
|
|
205
|
+
<div className="border-l border-border px-3 py-1.5">{modifiedLabel}</div>
|
|
206
|
+
</div>
|
|
207
|
+
{chunks}
|
|
208
|
+
</div>
|
|
209
|
+
</OverlayScroll>
|
|
210
|
+
);
|
|
211
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { type CSSProperties, type MouseEvent, type ReactNode, useCallback, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { SolarIcon } from "../icon/SolarIcon";
|
|
3
|
+
import { cn } from "../lib/cn";
|
|
4
|
+
import { Segmented } from "../segmented/Segmented";
|
|
5
|
+
import { appendTextBlock, cleanupDoc, joinMarkdownDoc, type MarkdownDoc, parseMarkdownDoc, readInlineEdit, splitListItem } from "./blocks";
|
|
6
|
+
import { diffMarkdown } from "./diff";
|
|
7
|
+
import { MarkdownDiff, type MarkdownDiffLayout } from "./MarkdownDiff";
|
|
8
|
+
import { MarkdownPreview } from "./MarkdownPreview";
|
|
9
|
+
import { MarkdownSource } from "./MarkdownSource";
|
|
10
|
+
|
|
11
|
+
export type MarkdownEditorMode = "preview" | "edit" | "diff";
|
|
12
|
+
export type { MarkdownDiffLayout };
|
|
13
|
+
|
|
14
|
+
export type MarkdownEditorProps = {
|
|
15
|
+
/** Current markdown. In diff mode this is the modified version. */
|
|
16
|
+
value?: string;
|
|
17
|
+
defaultValue?: string;
|
|
18
|
+
onChange?: (value: string) => void;
|
|
19
|
+
/** Earlier version. Enables the Diff view. */
|
|
20
|
+
original?: string;
|
|
21
|
+
mode?: MarkdownEditorMode;
|
|
22
|
+
defaultMode?: MarkdownEditorMode;
|
|
23
|
+
onModeChange?: (mode: MarkdownEditorMode) => void;
|
|
24
|
+
/** Monaco-style layout. Side by side is the default. */
|
|
25
|
+
diffLayout?: MarkdownDiffLayout;
|
|
26
|
+
defaultDiffLayout?: MarkdownDiffLayout;
|
|
27
|
+
onDiffLayoutChange?: (layout: MarkdownDiffLayout) => void;
|
|
28
|
+
readOnly?: boolean;
|
|
29
|
+
placeholder?: string;
|
|
30
|
+
title?: ReactNode;
|
|
31
|
+
originalLabel?: ReactNode;
|
|
32
|
+
modifiedLabel?: ReactNode;
|
|
33
|
+
className?: string;
|
|
34
|
+
style?: CSSProperties;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
type EditSession = {
|
|
38
|
+
index: number;
|
|
39
|
+
doc: MarkdownDoc;
|
|
40
|
+
/** Restored on blur when an appended paragraph is still empty. */
|
|
41
|
+
revert?: string;
|
|
42
|
+
caret?: "start" | "end";
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (value: T) => void): [T, (next: T) => void] {
|
|
46
|
+
const [uncontrolled, setUncontrolled] = useState(defaultValue);
|
|
47
|
+
const isControlled = value !== undefined;
|
|
48
|
+
const current = isControlled ? (value as T) : uncontrolled;
|
|
49
|
+
const set = useCallback(
|
|
50
|
+
(next: T) => {
|
|
51
|
+
if (!isControlled) setUncontrolled(next);
|
|
52
|
+
onChange?.(next);
|
|
53
|
+
},
|
|
54
|
+
[isControlled, onChange],
|
|
55
|
+
);
|
|
56
|
+
return [current, set];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Markdown editor in the style of Cursor's document editor.
|
|
61
|
+
*
|
|
62
|
+
* Preview renders the document. Click a heading, list item, or paragraph to edit that text in place.
|
|
63
|
+
* Mermaid fences render as diagrams and stay view only.
|
|
64
|
+
* Edit is the full-file text buffer. Diff compares `original` and `value` like Monaco.
|
|
65
|
+
*/
|
|
66
|
+
export function MarkdownEditor({
|
|
67
|
+
value: valueProp,
|
|
68
|
+
defaultValue = "",
|
|
69
|
+
onChange,
|
|
70
|
+
original,
|
|
71
|
+
mode: modeProp,
|
|
72
|
+
defaultMode = "preview",
|
|
73
|
+
onModeChange,
|
|
74
|
+
diffLayout: layoutProp,
|
|
75
|
+
defaultDiffLayout = "side-by-side",
|
|
76
|
+
onDiffLayoutChange,
|
|
77
|
+
readOnly = false,
|
|
78
|
+
placeholder = "Write markdown…",
|
|
79
|
+
title,
|
|
80
|
+
originalLabel = "Original",
|
|
81
|
+
modifiedLabel = "Modified",
|
|
82
|
+
className,
|
|
83
|
+
style,
|
|
84
|
+
}: MarkdownEditorProps) {
|
|
85
|
+
const [value, setValue] = useControllable(valueProp, defaultValue, onChange);
|
|
86
|
+
const [mode, setMode] = useControllable(modeProp, defaultMode, onModeChange);
|
|
87
|
+
const [layout, setLayout] = useControllable(layoutProp, defaultDiffLayout, onDiffLayoutChange);
|
|
88
|
+
const [session, setSession] = useState<EditSession | null>(null);
|
|
89
|
+
|
|
90
|
+
const sessionRef = useRef<EditSession | null>(session);
|
|
91
|
+
sessionRef.current = session;
|
|
92
|
+
const valueRef = useRef(value);
|
|
93
|
+
const seenProp = useRef(value);
|
|
94
|
+
const switching = useRef(false);
|
|
95
|
+
|
|
96
|
+
let live = session;
|
|
97
|
+
if (value !== seenProp.current) {
|
|
98
|
+
seenProp.current = value;
|
|
99
|
+
const current = sessionRef.current;
|
|
100
|
+
const same = current ? joinMarkdownDoc(current.doc) === value : false;
|
|
101
|
+
if (current && !same) {
|
|
102
|
+
live = null;
|
|
103
|
+
sessionRef.current = null;
|
|
104
|
+
setSession(null);
|
|
105
|
+
}
|
|
106
|
+
valueRef.current = value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const emit = useCallback(
|
|
110
|
+
(next: string) => {
|
|
111
|
+
if (next === valueRef.current) return;
|
|
112
|
+
valueRef.current = next;
|
|
113
|
+
setValue(next);
|
|
114
|
+
},
|
|
115
|
+
[setValue],
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const currentDoc = (): MarkdownDoc => {
|
|
119
|
+
const current = sessionRef.current;
|
|
120
|
+
if (!current) return parseMarkdownDoc(valueRef.current);
|
|
121
|
+
const block = current.doc.blocks[current.index];
|
|
122
|
+
if (current.revert !== undefined && block?.kind === "text" && block.body.trim() === "") return parseMarkdownDoc(current.revert);
|
|
123
|
+
return current.doc;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const startEdit = (index: number): boolean => {
|
|
127
|
+
if (readOnly || mode !== "preview") return false;
|
|
128
|
+
const doc = currentDoc();
|
|
129
|
+
const block = doc.blocks[index];
|
|
130
|
+
if (!block || block.kind === "mermaid") return false;
|
|
131
|
+
const next: EditSession = { index, doc };
|
|
132
|
+
sessionRef.current = next;
|
|
133
|
+
setSession(next);
|
|
134
|
+
const joined = joinMarkdownDoc(doc);
|
|
135
|
+
if (joined !== valueRef.current) emit(joined);
|
|
136
|
+
return true;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const updateBody = (body: string) => {
|
|
140
|
+
const current = sessionRef.current;
|
|
141
|
+
if (!current) return;
|
|
142
|
+
const blocks = current.doc.blocks.map((block, index) => (index === current.index ? { ...block, body } : block));
|
|
143
|
+
const next: EditSession = { ...current, doc: { ...current.doc, blocks } };
|
|
144
|
+
sessionRef.current = next;
|
|
145
|
+
setSession(next);
|
|
146
|
+
emit(joinMarkdownDoc(next.doc));
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const commit = () => {
|
|
150
|
+
const current = sessionRef.current;
|
|
151
|
+
if (!current) return;
|
|
152
|
+
const block = current.doc.blocks[current.index];
|
|
153
|
+
sessionRef.current = null;
|
|
154
|
+
setSession(null);
|
|
155
|
+
if (current.revert !== undefined && block?.kind === "text" && block.body.trim() === "") {
|
|
156
|
+
emit(current.revert);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const next = joinMarkdownDoc(cleanupDoc(current.doc));
|
|
160
|
+
if (next !== valueRef.current) emit(next);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const enterList = (before: string, after: string) => {
|
|
164
|
+
const current = sessionRef.current;
|
|
165
|
+
if (!current) return;
|
|
166
|
+
const block = current.doc.blocks[current.index];
|
|
167
|
+
if (!block) return;
|
|
168
|
+
const view = readInlineEdit(block.body);
|
|
169
|
+
if (view.kind !== "list") return;
|
|
170
|
+
switching.current = true;
|
|
171
|
+
if (before.trim() === "" && after.trim() === "") {
|
|
172
|
+
const blocks = current.doc.blocks.slice();
|
|
173
|
+
blocks[current.index] = { ...block, body: "" };
|
|
174
|
+
const next: EditSession = { ...current, doc: { ...current.doc, blocks }, caret: "start" };
|
|
175
|
+
sessionRef.current = next;
|
|
176
|
+
setSession(next);
|
|
177
|
+
emit(joinMarkdownDoc(next.doc));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const split = splitListItem(current.doc, current.index, before, after);
|
|
181
|
+
if (!split) {
|
|
182
|
+
switching.current = false;
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const next: EditSession = { index: split.index, doc: split.doc, revert: current.revert, caret: "start" };
|
|
186
|
+
sessionRef.current = next;
|
|
187
|
+
setSession(next);
|
|
188
|
+
emit(joinMarkdownDoc(split.doc));
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const onBlurBlock = () => {
|
|
192
|
+
if (switching.current) {
|
|
193
|
+
switching.current = false;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
commit();
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const onEditMouseDown = (index: number, event: MouseEvent) => {
|
|
200
|
+
const current = sessionRef.current;
|
|
201
|
+
if (!current || current.index === index) return;
|
|
202
|
+
if (event.target instanceof Element && event.target.closest("a, button, textarea, input")) return;
|
|
203
|
+
const block = current.doc.blocks[index];
|
|
204
|
+
if (!block || block.kind === "mermaid") return;
|
|
205
|
+
event.preventDefault();
|
|
206
|
+
if (startEdit(index)) switching.current = true;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const append = () => {
|
|
210
|
+
if (readOnly) return;
|
|
211
|
+
const current = sessionRef.current;
|
|
212
|
+
if (current) {
|
|
213
|
+
const block = current.doc.blocks[current.index];
|
|
214
|
+
if (block?.kind === "text" && block.body.trim() === "" && current.index === current.doc.blocks.length - 1) return;
|
|
215
|
+
}
|
|
216
|
+
const base = currentDoc();
|
|
217
|
+
const doc = appendTextBlock(base);
|
|
218
|
+
const next: EditSession = { index: doc.blocks.length - 1, doc, revert: joinMarkdownDoc(base) };
|
|
219
|
+
sessionRef.current = next;
|
|
220
|
+
setSession(next);
|
|
221
|
+
emit(joinMarkdownDoc(doc));
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const changeMode = (next: MarkdownEditorMode) => {
|
|
225
|
+
if (next === "diff" && original === undefined) return;
|
|
226
|
+
commit();
|
|
227
|
+
setMode(next);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const doc = live?.doc ?? parseMarkdownDoc(valueRef.current);
|
|
231
|
+
const model = useMemo(() => (mode === "diff" && original !== undefined ? diffMarkdown(original, value) : null), [mode, original, value]);
|
|
232
|
+
|
|
233
|
+
return (
|
|
234
|
+
<div role="region" className={cn("nonla-markdown-editor flex h-full min-h-96 flex-col overflow-hidden rounded-xl border border-border bg-card text-foreground", className)} style={style} aria-label={typeof title === "string" ? title : "Markdown editor"}>
|
|
235
|
+
<div className="flex h-10 shrink-0 items-center justify-between gap-3 border-b border-border px-2">
|
|
236
|
+
<div className="min-w-0 truncate px-1 text-sm font-medium">{title}</div>
|
|
237
|
+
<div className="flex shrink-0 items-center gap-2">
|
|
238
|
+
{model ? (
|
|
239
|
+
<span className="font-mono text-xs">
|
|
240
|
+
{model.added === 0 && model.removed === 0 ? (
|
|
241
|
+
<span className="text-muted-foreground">No changes</span>
|
|
242
|
+
) : (
|
|
243
|
+
<>
|
|
244
|
+
<span className="text-success">+{model.added}</span> <span className="text-destructive">−{model.removed}</span>
|
|
245
|
+
</>
|
|
246
|
+
)}
|
|
247
|
+
</span>
|
|
248
|
+
) : null}
|
|
249
|
+
{mode === "diff" && original !== undefined ? (
|
|
250
|
+
<Segmented
|
|
251
|
+
size="small"
|
|
252
|
+
value={layout}
|
|
253
|
+
onChange={setLayout}
|
|
254
|
+
options={[
|
|
255
|
+
{ label: "Inline", value: "inline" },
|
|
256
|
+
{ label: "Split", value: "side-by-side" },
|
|
257
|
+
]}
|
|
258
|
+
/>
|
|
259
|
+
) : null}
|
|
260
|
+
<Segmented
|
|
261
|
+
size="small"
|
|
262
|
+
value={mode}
|
|
263
|
+
onChange={changeMode}
|
|
264
|
+
options={[
|
|
265
|
+
{ label: "Preview", value: "preview", icon: <SolarIcon name="eye-linear" size={14} /> },
|
|
266
|
+
{ label: "Edit", value: "edit", icon: <SolarIcon name="pen-linear" size={14} /> },
|
|
267
|
+
{ label: "Diff", value: "diff", icon: <SolarIcon name="transfer-horizontal-linear" size={14} />, disabled: original === undefined },
|
|
268
|
+
]}
|
|
269
|
+
/>
|
|
270
|
+
</div>
|
|
271
|
+
</div>
|
|
272
|
+
<div className="min-h-0 flex-1">
|
|
273
|
+
{mode === "edit" ? <MarkdownSource value={value} onChange={emit} readOnly={readOnly} placeholder={placeholder} /> : null}
|
|
274
|
+
{mode === "preview" ? (
|
|
275
|
+
<MarkdownPreview
|
|
276
|
+
doc={doc}
|
|
277
|
+
editingIndex={live?.index ?? null}
|
|
278
|
+
readOnly={readOnly}
|
|
279
|
+
placeholder={placeholder}
|
|
280
|
+
onStartEdit={startEdit}
|
|
281
|
+
onEditMouseDown={onEditMouseDown}
|
|
282
|
+
onChangeBody={updateBody}
|
|
283
|
+
onBlurBlock={onBlurBlock}
|
|
284
|
+
onEnterList={enterList}
|
|
285
|
+
caret={live?.caret}
|
|
286
|
+
onAppend={append}
|
|
287
|
+
/>
|
|
288
|
+
) : null}
|
|
289
|
+
{mode === "diff" && model ? <MarkdownDiff model={model} layout={layout} originalLabel={originalLabel} modifiedLabel={modifiedLabel} /> : null}
|
|
290
|
+
{mode === "diff" && !model ? <p className="px-4 py-6 text-sm text-muted-foreground">Pass original to compare two versions.</p> : null}
|
|
291
|
+
</div>
|
|
292
|
+
</div>
|
|
293
|
+
);
|
|
294
|
+
}
|