devnonla-ui 0.9.0 → 0.9.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/README.md CHANGED
@@ -194,7 +194,7 @@ Primitives (`ChatWelcome`, `ChatUserMessage`, `ChatAgentMessage`, `ChatThinking`
194
194
 
195
195
  # Markdown
196
196
 
197
- `MarkdownEditor` edits markdown in preview, source, or diff. Pass `original` to enable the diff view. `diffLayout` is side by side by default.
197
+ `MarkdownEditor` edits markdown in preview, source, or diff. Pass `original` to enable the diff view. `diffLayout` is side by side by default. `readOnly` shows a finished document and hides the mode switcher.
198
198
 
199
199
  ```tsx
200
200
  import { MarkdownEditor } from "devnonla-ui";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devnonla-ui",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "type": "module",
5
5
  "description": "NonlaUI — React controls for Nonla Agents",
6
6
  "author": "devlangla <devlangla0x01@gmail.com>",
@@ -62,6 +62,7 @@ function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (v
62
62
  * Preview renders the document. Click a heading, list item, or paragraph to edit that text in place.
63
63
  * Mermaid fences render as diagrams and stay view only.
64
64
  * Edit is the full-file text buffer. Diff compares `original` and `value` like Monaco.
65
+ * `readOnly` locks editing and hides the mode switcher, so preview can show a finished document.
65
66
  */
66
67
  export function MarkdownEditor({
67
68
  value: valueProp,
@@ -229,46 +230,52 @@ export function MarkdownEditor({
229
230
 
230
231
  const doc = live?.doc ?? parseMarkdownDoc(valueRef.current);
231
232
  const model = useMemo(() => (mode === "diff" && original !== undefined ? diffMarkdown(original, value) : null), [mode, original, value]);
233
+ const showModes = !readOnly;
234
+ const showHeader = Boolean(title) || showModes || model !== null;
232
235
 
233
236
  return (
234
237
  <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
- />
238
+ {showHeader ? (
239
+ <div className="flex h-10 shrink-0 items-center justify-between gap-3 border-b border-border px-2">
240
+ <div className="min-w-0 truncate px-1 text-sm font-medium">{title}</div>
241
+ <div className="flex shrink-0 items-center gap-2">
242
+ {model ? (
243
+ <span className="font-mono text-xs">
244
+ {model.added === 0 && model.removed === 0 ? (
245
+ <span className="text-muted-foreground">No changes</span>
246
+ ) : (
247
+ <>
248
+ <span className="text-success">+{model.added}</span> <span className="text-destructive">−{model.removed}</span>
249
+ </>
250
+ )}
251
+ </span>
252
+ ) : null}
253
+ {mode === "diff" && original !== undefined ? (
254
+ <Segmented
255
+ size="small"
256
+ value={layout}
257
+ onChange={setLayout}
258
+ options={[
259
+ { label: "Inline", value: "inline" },
260
+ { label: "Split", value: "side-by-side" },
261
+ ]}
262
+ />
263
+ ) : null}
264
+ {showModes ? (
265
+ <Segmented
266
+ size="small"
267
+ value={mode}
268
+ onChange={changeMode}
269
+ options={[
270
+ { label: "Preview", value: "preview", icon: <SolarIcon name="eye-linear" size={14} /> },
271
+ { label: "Edit", value: "edit", icon: <SolarIcon name="pen-linear" size={14} /> },
272
+ { label: "Diff", value: "diff", icon: <SolarIcon name="transfer-horizontal-linear" size={14} />, disabled: original === undefined },
273
+ ]}
274
+ />
275
+ ) : null}
276
+ </div>
270
277
  </div>
271
- </div>
278
+ ) : null}
272
279
  <div className="min-h-0 flex-1">
273
280
  {mode === "edit" ? <MarkdownSource value={value} onChange={emit} readOnly={readOnly} placeholder={placeholder} /> : null}
274
281
  {mode === "preview" ? (
@@ -11,10 +11,12 @@ import { type InlineEdit, type MarkdownBlock, type MarkdownDoc, readFence, readI
11
11
 
12
12
  const documentClass = cn(
13
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",
14
+ "[&_h1]:my-0 [&_h1]:text-[32px] [&_h1]:leading-tight [&_h1]:font-bold [&_h1]:tracking-tight",
15
+ "[&_h2]:my-0 [&_h2]:text-[24px] [&_h2]:leading-tight [&_h2]:font-bold [&_h2]:tracking-tight",
16
+ "[&_h3]:my-0 [&_h3]:text-[20px] [&_h3]:leading-snug [&_h3]:font-semibold",
17
+ "[&_h4]:my-0 [&_h4]:text-[18px] [&_h4]:leading-snug [&_h4]:font-semibold",
18
+ "[&_h5]:my-0 [&_h5]:text-[16px] [&_h5]:leading-snug [&_h5]:font-semibold",
19
+ "[&_h6]:my-0 [&_h6]:text-[15px] [&_h6]:leading-snug [&_h6]:font-semibold",
18
20
  "[&_p]:my-0",
19
21
  "[&_ul]:my-1 [&_ul]:list-disc [&_ul]:pl-6",
20
22
  "[&_ol]:my-1 [&_ol]:list-decimal [&_ol]:pl-6",
@@ -55,11 +57,7 @@ const previewComponents: Components = {
55
57
  if (type !== "checkbox") return <input type={type} checked={checked} readOnly />;
56
58
  return (
57
59
  <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
- />
60
+ <Checkbox checked={Boolean(checked)} tabIndex={-1} />
63
61
  </span>
64
62
  );
65
63
  },
@@ -73,10 +71,12 @@ function MermaidView({ code }: { code: string }) {
73
71
  }
74
72
 
75
73
  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";
74
+ if (level === 1) return "text-[32px] font-bold leading-tight tracking-tight";
75
+ if (level === 2) return "text-[24px] font-bold leading-tight tracking-tight";
76
+ if (level === 3) return "text-[20px] font-semibold leading-snug";
77
+ if (level === 4) return "text-[18px] font-semibold leading-snug";
78
+ if (level === 5) return "text-[16px] font-semibold leading-snug";
79
+ return "text-[15px] font-semibold leading-snug";
80
80
  }
81
81
 
82
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 }) {
@@ -220,11 +220,7 @@ function ListItem({ view, children }: { view: Extract<InlineEdit, { kind: "list"
220
220
  <div className="flex items-start gap-1.5" style={level > 0 ? { marginLeft: `${level * 1.25}rem` } : undefined}>
221
221
  {view.task ? (
222
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
- />
223
+ <Checkbox checked={view.checked} tabIndex={-1} />
228
224
  </span>
229
225
  ) : view.ordered ? (
230
226
  <span className="w-6 shrink-0 text-right text-[15px] leading-7 tabular-nums">{ordinal}.</span>
@@ -242,10 +238,21 @@ function isListBlock(block: MarkdownBlock): boolean {
242
238
  return block.kind === "text" && readInlineEdit(block.body).kind === "list";
243
239
  }
244
240
 
241
+ function headingGap(level: number): string {
242
+ if (level <= 2) return "mt-6";
243
+ if (level === 3) return "mt-5";
244
+ if (level === 4) return "mt-4";
245
+ return "mt-3";
246
+ }
247
+
245
248
  function spaceBefore(block: MarkdownBlock, prev?: MarkdownBlock): string {
246
249
  if (!prev) return "";
247
250
  if (isListBlock(block) && isListBlock(prev)) return "mt-0.5";
248
- if (block.kind === "text" && readInlineEdit(block.body).kind === "heading") return "mt-6";
251
+ if (block.kind === "text") {
252
+ const view = readInlineEdit(block.body);
253
+ if (view.kind === "heading") return headingGap(view.level);
254
+ }
255
+ if (prev.kind === "text" && readInlineEdit(prev.body).kind === "heading") return "mt-2";
249
256
  return "mt-3";
250
257
  }
251
258
 
@@ -288,23 +295,36 @@ export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Wr
288
295
  ) : (
289
296
  <BlockEditor value={block.body} mono={block.kind === "code"} caret={caret} onChange={onChangeBody} onBlur={onBlurBlock} />
290
297
  );
298
+ const gap = spaceBefore(block, doc.blocks[index - 1]);
299
+ if (locked) {
300
+ return (
301
+ <div
302
+ // biome-ignore lint/suspicious/noArrayIndexKey: block position is the edit identity
303
+ key={`${index}:${block.kind}`}
304
+ data-md-block={block.kind}
305
+ className={cn("outline-none", gap)}
306
+ >
307
+ <BlockBody block={block} />
308
+ </div>
309
+ );
310
+ }
291
311
  return (
292
312
  <div
293
313
  // biome-ignore lint/suspicious/noArrayIndexKey: block position is the edit identity
294
314
  key={`${index}:${block.kind}`}
295
315
  role="button"
296
316
  data-md-block={block.kind}
297
- tabIndex={locked ? undefined : 0}
298
- className={cn("outline-none", !locked && "cursor-text", spaceBefore(block, doc.blocks[index - 1]))}
317
+ tabIndex={0}
318
+ className={cn("cursor-text outline-none", gap)}
299
319
  onMouseDown={(event) => onEditMouseDown(index, event)}
300
320
  onClick={(event) => {
301
- if (locked || editing || interactiveTarget(event.target)) return;
321
+ if (editing || interactiveTarget(event.target)) return;
302
322
  const selection = window.getSelection();
303
323
  if (selection && selection.toString().length > 0) return;
304
324
  onStartEdit(index);
305
325
  }}
306
326
  onKeyDown={(event) => {
307
- if (locked || editing || event.key !== "Enter") return;
327
+ if (editing || event.key !== "Enter") return;
308
328
  event.preventDefault();
309
329
  onStartEdit(index);
310
330
  }}