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
package/src/index.ts
CHANGED
|
@@ -17,6 +17,8 @@ export type { AlertProps, AlertType } from "./alert/Alert";
|
|
|
17
17
|
export { Alert } from "./alert/Alert";
|
|
18
18
|
export type { CalendarProps } from "./calendar/Calendar";
|
|
19
19
|
export { Calendar } from "./calendar/Calendar";
|
|
20
|
+
export type { CardFooterProps, CardItemProps, CardProps } from "./card/Card";
|
|
21
|
+
export { Card } from "./card/Card";
|
|
20
22
|
export type {
|
|
21
23
|
AgentChatboxProps,
|
|
22
24
|
AgentHistoryMessage,
|
|
@@ -88,7 +90,7 @@ export {
|
|
|
88
90
|
useAgentStream,
|
|
89
91
|
WebFetchToolUI,
|
|
90
92
|
} from "./chat";
|
|
91
|
-
export type { CheckboxProps } from "./checkbox/Checkbox";
|
|
93
|
+
export type { CheckboxColor, CheckboxProps } from "./checkbox/Checkbox";
|
|
92
94
|
export { Checkbox } from "./checkbox/Checkbox";
|
|
93
95
|
export type { CodeBlockCopyButtonProps, CodeBlockProps } from "./codeblock/CodeBlock";
|
|
94
96
|
export { CodeBlock, CodeBlockCopyButton } from "./codeblock/CodeBlock";
|
|
@@ -137,25 +139,33 @@ export {
|
|
|
137
139
|
export type { FormLayoutItemProps, FormLayoutProps } from "./form-layout/FormLayout";
|
|
138
140
|
/** Layout-only Form + Form.Item for labeled fields. */
|
|
139
141
|
export { Form } from "./form-layout/FormLayout";
|
|
140
|
-
export { FluentIcon } from "./icon/
|
|
142
|
+
export { FluentIcon, SolarIcon } from "./icon/SolarIcon";
|
|
141
143
|
export {
|
|
142
144
|
DEFAULT_ICON_NAME,
|
|
143
145
|
DEFAULT_TOOL_ICON,
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
fluentIconRef,
|
|
147
|
-
getFluentImgSrc,
|
|
146
|
+
ensureSolarIcons,
|
|
147
|
+
ensureSolarIcons as ensureFluentIcons,
|
|
148
148
|
getIconNames,
|
|
149
|
+
getSolarImgSrc,
|
|
150
|
+
getSolarImgSrc as getFluentImgSrc,
|
|
151
|
+
getSolarSvg,
|
|
149
152
|
ICON_PREFIX,
|
|
150
|
-
|
|
153
|
+
isSolarIcon,
|
|
154
|
+
isSolarIcon as isFluentIcon,
|
|
151
155
|
isSvgIcon,
|
|
152
|
-
|
|
156
|
+
solarIconName,
|
|
157
|
+
solarIconName as fluentIconName,
|
|
158
|
+
solarIconRef,
|
|
159
|
+
solarIconRef as fluentIconRef,
|
|
160
|
+
} from "./icon/solar";
|
|
153
161
|
export { cn } from "./lib/cn";
|
|
154
162
|
export type { PopperPlacement } from "./lib/placement";
|
|
155
163
|
export { placementToRadix } from "./lib/placement";
|
|
156
164
|
export type { CanonicalSize, ControlSize, ControlSizeTokens } from "./lib/sizes";
|
|
157
165
|
export { CONTROL_SIZES, controlHeightVar, controlRadiusVar, controlStatusClass, getSizeTokens, normalizeSize, useControlSize } from "./lib/sizes";
|
|
158
166
|
export { glassOverlayClass, glassSurfaceClass, meadowSurfaceClass } from "./lib/surface";
|
|
167
|
+
export type { MarkdownDiffLayout, MarkdownEditorMode, MarkdownEditorProps } from "./markdown-editor/MarkdownEditor";
|
|
168
|
+
export { MarkdownEditor } from "./markdown-editor/MarkdownEditor";
|
|
159
169
|
export type { MenuItemProps, MenuRootProps, MenuTriggerProps } from "./menu/Menu";
|
|
160
170
|
export { Menu, MenuAction, MenuDivider, MenuItem, MenuTrigger } from "./menu/Menu";
|
|
161
171
|
export type { MessageConfig, MessageType } from "./message/message";
|
|
@@ -180,11 +190,11 @@ export type { SidebarItemType, SidebarProps, SidebarSelectInfo } from "./sidebar
|
|
|
180
190
|
export { Sidebar } from "./sidebar/Sidebar";
|
|
181
191
|
export type { SkeletonProps } from "./skeleton/Skeleton";
|
|
182
192
|
export { Skeleton } from "./skeleton/Skeleton";
|
|
183
|
-
export type { SpinProps, SpinVariant } from "./spin/Spin";
|
|
193
|
+
export type { SpinColor, SpinProps, SpinVariant } from "./spin/Spin";
|
|
184
194
|
export { Spin } from "./spin/Spin";
|
|
185
195
|
export type { SplitterOrientation, SplitterPanelProps, SplitterProps, SplitterSemanticSlot, SplitterSize } from "./splitter/Splitter";
|
|
186
196
|
export { Splitter, SplitterPanel } from "./splitter/Splitter";
|
|
187
|
-
export type { SwitchProps, SwitchVariant } from "./switch/Switch";
|
|
197
|
+
export type { SwitchColor, SwitchProps, SwitchVariant } from "./switch/Switch";
|
|
188
198
|
export { Switch } from "./switch/Switch";
|
|
189
199
|
export type {
|
|
190
200
|
ColumnsType,
|
|
@@ -197,8 +207,10 @@ export type {
|
|
|
197
207
|
export { Table } from "./table/Table";
|
|
198
208
|
export type { TagProps, TagVariant } from "./tag/Tag";
|
|
199
209
|
export { Tag } from "./tag/Tag";
|
|
200
|
-
export type { NonlaThemeColorName, NonlaThemeColors, NonlaThemeConfig, NonlaThemeKnob, NonlaThemeKnobName, NonlaTokenSnapshot } from "./theme";
|
|
201
|
-
export { applyNonlaTheme, getDesignToken, NONLA_THEME_KEYS, NONLA_THEME_KNOBS } from "./theme";
|
|
210
|
+
export type { NonlaColorMode, NonlaThemeColorName, NonlaThemeColors, NonlaThemeConfig, NonlaThemeKnob, NonlaThemeKnobName, NonlaTokenSnapshot } from "./theme";
|
|
211
|
+
export { applyNonlaTheme, getColorMode, getDesignToken, initColorMode, NONLA_COLOR_MODE_KEY, NONLA_THEME_KEYS, NONLA_THEME_KNOBS, setColorMode } from "./theme";
|
|
212
|
+
export type { ThemeToggleProps } from "./theme-toggle/ThemeToggle";
|
|
213
|
+
export { ThemeToggle, useColorMode } from "./theme-toggle/ThemeToggle";
|
|
202
214
|
export type { TimePickerProps, TimeValue } from "./timepicker/TimePicker";
|
|
203
215
|
export { TimePicker } from "./timepicker/TimePicker";
|
|
204
216
|
export type { TooltipPlacement, TooltipProps } from "./tooltip/Tooltip";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Pencil } from "lucide-react";
|
|
2
1
|
import { type CSSProperties, cloneElement, isValidElement, type KeyboardEvent, type ReactElement, type ReactNode, type Ref, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
2
|
import { createPortal } from "react-dom";
|
|
4
3
|
import { usePopupContainer } from "../app/context";
|
|
5
4
|
import { Button } from "../button/Button";
|
|
5
|
+
import { SolarIcon } from "../icon/SolarIcon";
|
|
6
6
|
import { cn } from "../lib/cn";
|
|
7
7
|
import { type CanonicalSize, type ControlSize, controlHeightVar, controlRadiusVar, getSizeTokens, useControlSize } from "../lib/sizes";
|
|
8
8
|
import { message } from "../message/message";
|
|
@@ -12,7 +12,7 @@ const FIELD_INSET: Record<CanonicalSize, string> = { small: "-4px", default: "-6
|
|
|
12
12
|
const MULTILINE_MIN: Record<CanonicalSize, number> = { small: 56, default: 72, large: 96 };
|
|
13
13
|
const GLASS_BORDER = 1;
|
|
14
14
|
|
|
15
|
-
const fieldClass = (saving: boolean) => cn("relative z-1 m-0 w-full border-0 bg-transparent text-foreground placeholder:text-
|
|
15
|
+
const fieldClass = (saving: boolean) => cn("relative z-1 m-0 w-full border-0 bg-transparent text-foreground placeholder:text-placeholder focus:outline-none", saving && "opacity-60");
|
|
16
16
|
|
|
17
17
|
type Lock = {
|
|
18
18
|
top: number;
|
|
@@ -227,7 +227,7 @@ function EditButton({ label, size, onClick }: { label: string; size: CanonicalSi
|
|
|
227
227
|
className="inline-flex shrink-0 items-center justify-center rounded text-muted-foreground opacity-0 transition-opacity hover:bg-muted hover:text-foreground group-hover/edit:opacity-100 focus-visible:opacity-100"
|
|
228
228
|
style={{ width: box, height: box }}
|
|
229
229
|
>
|
|
230
|
-
<
|
|
230
|
+
<SolarIcon name="pen-linear" size={tok.icon} />
|
|
231
231
|
</button>
|
|
232
232
|
);
|
|
233
233
|
}
|
package/src/input/Input.tsx
CHANGED
|
@@ -15,10 +15,10 @@ export type InputProps = Omit<InputHTMLAttributes<HTMLInputElement>, "size" | "p
|
|
|
15
15
|
onPressEnter?: (e: KeyboardEvent<HTMLInputElement>) => void;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
const fieldBase = cn("w-full", controlFieldSurface, "placeholder:text-
|
|
18
|
+
const fieldBase = cn("w-full", controlFieldSurface, "placeholder:text-placeholder focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-45", controlFieldFocusBorder);
|
|
19
19
|
|
|
20
20
|
function variantClass(variant: InputProps["variant"]) {
|
|
21
|
-
if (variant === "borderless") return "border-transparent bg-transparent hover:bg-transparent shadow-none focus:border-transparent";
|
|
21
|
+
if (variant === "borderless") return "border-transparent bg-transparent hover:bg-transparent shadow-none focus:border-transparent focus:bg-transparent focus-within:bg-transparent";
|
|
22
22
|
if (variant === "filled") return "border-transparent bg-muted focus:border-transparent focus:bg-[var(--control-bg-hover)]";
|
|
23
23
|
return "";
|
|
24
24
|
}
|
|
@@ -275,7 +275,7 @@ const InputNumber = forwardRef<HTMLInputElement, InputNumberProps>(function Inpu
|
|
|
275
275
|
}}
|
|
276
276
|
onKeyDown={handleKeyDown}
|
|
277
277
|
className={cn(
|
|
278
|
-
"min-w-0 flex-1 border-0 bg-transparent text-foreground placeholder:text-
|
|
278
|
+
"min-w-0 flex-1 border-0 bg-transparent text-foreground placeholder:text-placeholder outline-none",
|
|
279
279
|
"disabled:cursor-not-allowed",
|
|
280
280
|
// Kill native number chrome if type ever switches
|
|
281
281
|
"[appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none",
|
package/src/lib/sizes.ts
CHANGED
|
@@ -94,14 +94,17 @@ export function controlStatusClass(status?: "error" | "warning"): string {
|
|
|
94
94
|
export const controlFieldTransition = "transition-[background-color,border-color] duration-[var(--nonla-dur-fast,150ms)] ease-[var(--nonla-ease-out,cubic-bezier(0.16,1,0.3,1))] motion-reduce:transition-none";
|
|
95
95
|
|
|
96
96
|
/**
|
|
97
|
-
* Outlined field.
|
|
98
|
-
*
|
|
97
|
+
* Outlined field. Resting stroke is `--input` (Cursor `--border-secondary`, ink at 12%).
|
|
98
|
+
* Fill stays transparent so the stroke sits on the card, same as the settings inputs.
|
|
99
99
|
*/
|
|
100
|
-
export const controlFieldSurface =
|
|
101
|
-
"border border-solid border-input bg-glass-bar text-foreground shadow-[inset_0_1px_0_var(--glass-highlight)] backdrop-blur-xl";
|
|
100
|
+
export const controlFieldSurface = "border border-solid border-input bg-transparent text-foreground";
|
|
102
101
|
|
|
103
|
-
/**
|
|
104
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Focus fill is Cursor `--bg-tertiary` (ink at 8%).
|
|
104
|
+
* Keyboard focus / open menu also strengthens the stroke to `--border-primary` (ink at 20%).
|
|
105
|
+
*/
|
|
106
|
+
export const controlFieldFocusBorder =
|
|
107
|
+
"focus:bg-input-fill focus-within:bg-input-fill data-[state=open]:bg-input-fill aria-expanded:bg-input-fill focus-visible:border-input-focus has-[:focus-visible]:border-input-focus data-[state=open]:border-input-focus aria-expanded:border-input-focus";
|
|
105
108
|
|
|
106
109
|
/** Shared field chrome (Input / Select / DatePicker). */
|
|
107
110
|
export function controlFieldStyle(size: ControlSize | undefined): CSSProperties {
|
package/src/lib/surface.ts
CHANGED
|
@@ -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
|
+
}
|