devnonla-ui 0.19.0 → 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
CHANGED
|
@@ -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
|
}
|