devnonla-ui 0.17.1 → 0.17.2
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
|
@@ -2,11 +2,12 @@ import { type CSSProperties, type MouseEvent, type ReactNode, useCallback, useMe
|
|
|
2
2
|
import { Icon } from "../icon/Icon";
|
|
3
3
|
import { cn } from "../lib/cn";
|
|
4
4
|
import { Segmented } from "../segmented/Segmented";
|
|
5
|
-
import { appendTextBlock, cleanupDoc, joinMarkdownDoc, type MarkdownDoc, parseMarkdownDoc, readInlineEdit, splitListItem } from "./blocks";
|
|
5
|
+
import { appendTextBlock, cleanupDoc, joinMarkdownDoc, type MarkdownBlock, type MarkdownDoc, parseMarkdownDoc, readInlineEdit, splitListItem } from "./blocks";
|
|
6
6
|
import { diffMarkdown } from "./diff";
|
|
7
7
|
import { MarkdownDiff, type MarkdownDiffLayout } from "./MarkdownDiff";
|
|
8
8
|
import { MarkdownPreview } from "./MarkdownPreview";
|
|
9
9
|
import { MarkdownSource } from "./MarkdownSource";
|
|
10
|
+
import { isCustomFence, type MarkdownComponents } from "./MarkdownViewer";
|
|
10
11
|
|
|
11
12
|
export type MarkdownEditorMode = "preview" | "edit" | "diff";
|
|
12
13
|
export type { MarkdownDiffLayout };
|
|
@@ -32,8 +33,18 @@ export type MarkdownEditorProps = {
|
|
|
32
33
|
modifiedLabel?: ReactNode;
|
|
33
34
|
className?: string;
|
|
34
35
|
style?: CSSProperties;
|
|
36
|
+
/**
|
|
37
|
+
* Components for fenced blocks, keyed by language (`step`, `subagent`, `mindmap`).
|
|
38
|
+
* Same map as `MarkdownViewer`. Those fences stay view only, like mermaid.
|
|
39
|
+
*/
|
|
40
|
+
components?: MarkdownComponents;
|
|
35
41
|
};
|
|
36
42
|
|
|
43
|
+
function viewOnlyBlock(block: MarkdownBlock | undefined, components?: MarkdownComponents): boolean {
|
|
44
|
+
if (!block || block.kind === "mermaid") return true;
|
|
45
|
+
return block.kind === "code" && isCustomFence(block.body, components);
|
|
46
|
+
}
|
|
47
|
+
|
|
37
48
|
type EditSession = {
|
|
38
49
|
index: number;
|
|
39
50
|
doc: MarkdownDoc;
|
|
@@ -60,7 +71,7 @@ function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (v
|
|
|
60
71
|
* Markdown editor in the style of Cursor's document editor.
|
|
61
72
|
*
|
|
62
73
|
* Preview renders the document. Click a heading, list item, or paragraph to edit that text in place.
|
|
63
|
-
* Mermaid fences render
|
|
74
|
+
* Mermaid fences and registered fence components render in place and stay view only.
|
|
64
75
|
* Edit is the full-file text buffer. Diff compares `original` and `value` like Monaco.
|
|
65
76
|
* `readOnly` locks editing and hides the mode switcher.
|
|
66
77
|
* A finished document with no editor chrome uses `MarkdownViewer`.
|
|
@@ -83,6 +94,7 @@ export function MarkdownEditor({
|
|
|
83
94
|
modifiedLabel = "Modified",
|
|
84
95
|
className,
|
|
85
96
|
style,
|
|
97
|
+
components,
|
|
86
98
|
}: MarkdownEditorProps) {
|
|
87
99
|
const [value, setValue] = useControllable(valueProp, defaultValue, onChange);
|
|
88
100
|
const [mode, setMode] = useControllable(modeProp, defaultMode, onModeChange);
|
|
@@ -129,7 +141,7 @@ export function MarkdownEditor({
|
|
|
129
141
|
if (readOnly || mode !== "preview") return false;
|
|
130
142
|
const doc = currentDoc();
|
|
131
143
|
const block = doc.blocks[index];
|
|
132
|
-
if (
|
|
144
|
+
if (viewOnlyBlock(block, components)) return false;
|
|
133
145
|
const next: EditSession = { index, doc };
|
|
134
146
|
sessionRef.current = next;
|
|
135
147
|
setSession(next);
|
|
@@ -203,7 +215,7 @@ export function MarkdownEditor({
|
|
|
203
215
|
if (!current || current.index === index) return;
|
|
204
216
|
if (event.target instanceof Element && event.target.closest("a, button, textarea, input")) return;
|
|
205
217
|
const block = current.doc.blocks[index];
|
|
206
|
-
if (
|
|
218
|
+
if (viewOnlyBlock(block, components)) return;
|
|
207
219
|
event.preventDefault();
|
|
208
220
|
if (startEdit(index)) switching.current = true;
|
|
209
221
|
};
|
|
@@ -292,6 +304,7 @@ export function MarkdownEditor({
|
|
|
292
304
|
onEnterList={enterList}
|
|
293
305
|
caret={live?.caret}
|
|
294
306
|
onAppend={append}
|
|
307
|
+
components={components}
|
|
295
308
|
/>
|
|
296
309
|
) : null}
|
|
297
310
|
{mode === "diff" && model ? <MarkdownDiff model={model} layout={layout} originalLabel={originalLabel} modifiedLabel={modifiedLabel} /> : null}
|
|
@@ -2,7 +2,7 @@ import { type MouseEvent, useLayoutEffect, useRef } from "react";
|
|
|
2
2
|
import { cn } from "../lib/cn";
|
|
3
3
|
import { OverlayScroll } from "../scroll/OverlayScroll";
|
|
4
4
|
import { type MarkdownDoc, readInlineEdit, writeInlineEdit } from "./blocks";
|
|
5
|
-
import { BlockBody, headingClass, ListItem, markdownScaleStyle, spaceBefore } from "./MarkdownViewer";
|
|
5
|
+
import { BlockBody, headingClass, isCustomFence, ListItem, type MarkdownComponents, markdownScaleStyle, spaceBefore } from "./MarkdownViewer";
|
|
6
6
|
|
|
7
7
|
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 }) {
|
|
8
8
|
const ref = useRef<HTMLTextAreaElement>(null);
|
|
@@ -83,15 +83,17 @@ export type MarkdownPreviewProps = {
|
|
|
83
83
|
/** Where the caret goes when an edit starts. */
|
|
84
84
|
caret?: "start" | "end";
|
|
85
85
|
onAppend: () => void;
|
|
86
|
+
/** Fence language → component. Same map as `MarkdownViewer`. */
|
|
87
|
+
components?: MarkdownComponents;
|
|
86
88
|
};
|
|
87
89
|
|
|
88
|
-
export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Write markdown…", onStartEdit, onEditMouseDown, onChangeBody, onBlurBlock, onEnterList, caret = "end", onAppend }: MarkdownPreviewProps) {
|
|
90
|
+
export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Write markdown…", onStartEdit, onEditMouseDown, onChangeBody, onBlurBlock, onEnterList, caret = "end", onAppend, components }: MarkdownPreviewProps) {
|
|
89
91
|
return (
|
|
90
92
|
<OverlayScroll className="h-full bg-card">
|
|
91
93
|
<div className="mx-auto flex w-full max-w-3xl flex-col px-8 py-6" style={markdownScaleStyle}>
|
|
92
94
|
{doc.blocks.map((block, index) => {
|
|
93
95
|
const editing = editingIndex === index;
|
|
94
|
-
const locked = block.kind === "mermaid" || readOnly;
|
|
96
|
+
const locked = block.kind === "mermaid" || (block.kind === "code" && isCustomFence(block.body, components)) || readOnly;
|
|
95
97
|
const view = block.kind === "text" ? readInlineEdit(block.body) : null;
|
|
96
98
|
const editor =
|
|
97
99
|
view?.kind === "heading" ? (
|
|
@@ -112,7 +114,7 @@ export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Wr
|
|
|
112
114
|
data-md-block={block.kind}
|
|
113
115
|
className={cn("outline-none", gap)}
|
|
114
116
|
>
|
|
115
|
-
<BlockBody block={block} />
|
|
117
|
+
<BlockBody block={block} components={components} />
|
|
116
118
|
</div>
|
|
117
119
|
);
|
|
118
120
|
}
|
|
@@ -137,7 +139,7 @@ export function MarkdownPreview({ doc, editingIndex, readOnly, placeholder = "Wr
|
|
|
137
139
|
onStartEdit(index);
|
|
138
140
|
}}
|
|
139
141
|
>
|
|
140
|
-
{editing ? editor : <BlockBody block={block} />}
|
|
142
|
+
{editing ? editor : <BlockBody block={block} components={components} />}
|
|
141
143
|
</div>
|
|
142
144
|
);
|
|
143
145
|
})}
|
|
@@ -204,6 +204,14 @@ export function useMarkdownComponents(): MarkdownComponents | undefined {
|
|
|
204
204
|
return useContext(MarkdownComponentsContext);
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
+
/** True when this fence language has a component in the map. */
|
|
208
|
+
export function isCustomFence(body: string, components?: MarkdownComponents): boolean {
|
|
209
|
+
if (!components) return false;
|
|
210
|
+
const lang = readFence(body)?.lang.toLowerCase();
|
|
211
|
+
if (!lang) return false;
|
|
212
|
+
return Object.keys(components).some((key) => key.toLowerCase() === lang);
|
|
213
|
+
}
|
|
214
|
+
|
|
207
215
|
function fenceComponent(components: MarkdownComponents | undefined, lang: string): MarkdownFenceComponent | undefined {
|
|
208
216
|
if (!components || !lang) return undefined;
|
|
209
217
|
const direct = components[lang];
|