@gild-ui/react-markdown 0.0.0 → 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +179 -2
- package/dist/CodeBlock.cjs +136 -0
- package/dist/CodeBlock.d.cts +15 -0
- package/dist/CodeBlock.d.mts +3 -0
- package/dist/CodeBlock.mjs +133 -0
- package/dist/Control.cjs +60 -0
- package/dist/Control.d.cts +16 -0
- package/dist/Control.d.mts +16 -0
- package/dist/Control.mjs +59 -0
- package/dist/Editor.cjs +131 -0
- package/dist/Editor.d.cts +48 -0
- package/dist/Editor.d.mts +48 -0
- package/dist/Editor.mjs +130 -0
- package/dist/LiveEdit-1-8cA5Xw.mjs +215 -0
- package/dist/LiveEdit-B83sDZXu.cjs +224 -0
- package/dist/LiveEdit.cjs +3 -0
- package/dist/LiveEdit.d.cts +29 -0
- package/dist/LiveEdit.d.mts +29 -0
- package/dist/LiveEdit.mjs +1 -0
- package/dist/MarkdownContext-D28Czdll.mjs +10 -0
- package/dist/MarkdownContext-DS1X9B6N.cjs +21 -0
- package/dist/Mermaid-DW0NuDS-.mjs +248 -0
- package/dist/Mermaid-D_tCNrQ9.cjs +253 -0
- package/dist/Mermaid.cjs +3 -0
- package/dist/Mermaid.d.cts +2 -0
- package/dist/Mermaid.d.mts +2 -0
- package/dist/Mermaid.mjs +1 -0
- package/dist/Preview-BLXtPaa9.mjs +258 -0
- package/dist/Preview-D-AL97QU.cjs +268 -0
- package/dist/Preview.cjs +3 -0
- package/dist/Preview.d.cts +56 -0
- package/dist/Preview.d.mts +2 -0
- package/dist/Preview.mjs +1 -0
- package/dist/Toolbar-Eg7KmYbK.mjs +603 -0
- package/dist/Toolbar-gk3KJhCE.cjs +614 -0
- package/dist/Toolbar.cjs +4 -0
- package/dist/Toolbar.d.cts +18 -0
- package/dist/Toolbar.d.mts +18 -0
- package/dist/Toolbar.mjs +1 -0
- package/dist/applyAction-B3AwrWKV.cjs +121 -0
- package/dist/applyAction-CkTQJ5Iy.mjs +116 -0
- package/dist/index-BBkHCbE7.d.mts +15 -0
- package/dist/index-BVYb370O.d.cts +24 -0
- package/dist/index-CfPKWPpM.d.mts +57 -0
- package/dist/index-CyddYU7P.d.mts +24 -0
- package/dist/index.cjs +21 -0
- package/dist/index.d.cts +80 -0
- package/dist/index.d.mts +80 -0
- package/dist/index.mjs +9 -0
- package/dist/mergeOverrides-D2btS1Jb.cjs +19 -0
- package/dist/mergeOverrides-DEbU7yDg.mjs +14 -0
- package/dist/rolldown-runtime-CmQyd0Gu.cjs +27 -0
- package/dist/themeTypes-C4kTtkIA.d.cts +83 -0
- package/dist/themeTypes-C4kTtkIA.d.mts +83 -0
- package/package.json +163 -3
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
const require_rolldown_runtime = require("./rolldown-runtime-CmQyd0Gu.cjs");
|
|
2
|
+
const require_MarkdownContext = require("./MarkdownContext-DS1X9B6N.cjs");
|
|
3
|
+
const require_Preview = require("./Preview-D-AL97QU.cjs");
|
|
4
|
+
const require_mergeOverrides = require("./mergeOverrides-D2btS1Jb.cjs");
|
|
5
|
+
let react = require("react");
|
|
6
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
7
|
+
let _gild_ui_react = require("@gild-ui/react");
|
|
8
|
+
let _gild_ui_react_View = require("@gild-ui/react/View");
|
|
9
|
+
let remark_gfm = require("remark-gfm");
|
|
10
|
+
remark_gfm = require_rolldown_runtime.__toESM(remark_gfm, 1);
|
|
11
|
+
let rehype_parse = require("rehype-parse");
|
|
12
|
+
rehype_parse = require_rolldown_runtime.__toESM(rehype_parse, 1);
|
|
13
|
+
let rehype_remark = require("rehype-remark");
|
|
14
|
+
rehype_remark = require_rolldown_runtime.__toESM(rehype_remark, 1);
|
|
15
|
+
let remark_stringify = require("remark-stringify");
|
|
16
|
+
remark_stringify = require_rolldown_runtime.__toESM(remark_stringify, 1);
|
|
17
|
+
// src/utils/blockToMarkdown.ts
|
|
18
|
+
const processor = (0, require("unified").unified)().use(rehype_parse.default, { fragment: true }).use(rehype_remark.default).use(remark_gfm.default).use(remark_stringify.default, {
|
|
19
|
+
bullet: "-",
|
|
20
|
+
fence: "`",
|
|
21
|
+
rule: "-"
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Serializes an edited block's live DOM back to markdown. The preview renders
|
|
25
|
+
* non-content UI inside a block (the code-copy control, decorative SVGs) -
|
|
26
|
+
* those are stripped from a clone first so only real content round-trips.
|
|
27
|
+
*/
|
|
28
|
+
function blockToMarkdown(element) {
|
|
29
|
+
const clone = element.cloneNode(true);
|
|
30
|
+
for (const junk of clone.querySelectorAll(".code-copy, button, svg")) junk.remove();
|
|
31
|
+
return String(processor.processSync(clone.innerHTML)).trimEnd();
|
|
32
|
+
}
|
|
33
|
+
// src/utils/splitBlocks.ts
|
|
34
|
+
const FENCE = /^[ \t]{0,3}(`{3,}|~{3,})/;
|
|
35
|
+
/**
|
|
36
|
+
* The fence state after `line`: unchanged when the line carries no marker, the
|
|
37
|
+
* marker itself when one opens a block, and `null` when one closes it.
|
|
38
|
+
*
|
|
39
|
+
* A closing marker must use the same character, be at least as long, and carry
|
|
40
|
+
* nothing after it. The info string is what separates the two roles - a
|
|
41
|
+
* ```` ```ts ```` line inside an open ```` ``` ```` block opens nothing and closes
|
|
42
|
+
* nothing, it is content. Without that check the block would end early and its
|
|
43
|
+
* remaining blank lines would split it into further blocks.
|
|
44
|
+
*/
|
|
45
|
+
const nextFence = (line, fence) => {
|
|
46
|
+
const match = FENCE.exec(line);
|
|
47
|
+
if (!match) return fence;
|
|
48
|
+
const [matched, marker] = match;
|
|
49
|
+
if (fence === null) return marker;
|
|
50
|
+
return line.slice(matched.length).trim() === "" && marker.startsWith(fence[0]) && marker.length >= fence.length ? null : fence;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Splits markdown into top-level blocks separated by blank lines, keeping
|
|
54
|
+
* fenced code blocks whole - blank lines inside a ``` / ~~~ fence never split.
|
|
55
|
+
* Offsets index the original string, so an edited block's source can be
|
|
56
|
+
* spliced back losslessly.
|
|
57
|
+
*/
|
|
58
|
+
function splitBlocks(value) {
|
|
59
|
+
const blocks = [];
|
|
60
|
+
let blockStart = -1;
|
|
61
|
+
let blockEnd = -1;
|
|
62
|
+
let fence = null;
|
|
63
|
+
let offset = 0;
|
|
64
|
+
for (const line of value.split("\n")) {
|
|
65
|
+
const isBlank = line.trim() === "";
|
|
66
|
+
if (blockStart >= 0 && isBlank && fence === null) {
|
|
67
|
+
blocks.push({
|
|
68
|
+
source: value.slice(blockStart, blockEnd),
|
|
69
|
+
start: blockStart,
|
|
70
|
+
end: blockEnd
|
|
71
|
+
});
|
|
72
|
+
blockStart = -1;
|
|
73
|
+
} else if (!isBlank || fence !== null) {
|
|
74
|
+
if (blockStart < 0) blockStart = offset;
|
|
75
|
+
blockEnd = offset + line.length;
|
|
76
|
+
fence = nextFence(line, fence);
|
|
77
|
+
}
|
|
78
|
+
offset += line.length + 1;
|
|
79
|
+
}
|
|
80
|
+
if (blockStart >= 0) blocks.push({
|
|
81
|
+
source: value.slice(blockStart, blockEnd),
|
|
82
|
+
start: blockStart,
|
|
83
|
+
end: blockEnd
|
|
84
|
+
});
|
|
85
|
+
return blocks;
|
|
86
|
+
}
|
|
87
|
+
// src/LiveEdit/LiveEdit.tsx
|
|
88
|
+
const LiveEditRoot = (0, _gild_ui_react.styled)(_gild_ui_react_View.View, {
|
|
89
|
+
name: "MarkdownLiveEdit",
|
|
90
|
+
label: "MarkdownLiveEdit"
|
|
91
|
+
})({
|
|
92
|
+
minWidth: 0,
|
|
93
|
+
maxWidth: "100%",
|
|
94
|
+
overflowWrap: "break-word"
|
|
95
|
+
});
|
|
96
|
+
const LiveEditBlock = (0, _gild_ui_react.styled)(_gild_ui_react_View.View, {
|
|
97
|
+
name: "MarkdownLiveEdit",
|
|
98
|
+
slot: "block",
|
|
99
|
+
label: "MarkdownLiveEditBlock"
|
|
100
|
+
})(({ theme }) => ({
|
|
101
|
+
cursor: "text",
|
|
102
|
+
borderRadius: theme?.radii?.sm,
|
|
103
|
+
"&:hover, &:focus-within, &:focus": {
|
|
104
|
+
outline: `${theme?.borderWidths?.thin} dashed ${theme?.border?.primary}`,
|
|
105
|
+
outlineOffset: "2px"
|
|
106
|
+
},
|
|
107
|
+
"&:empty::before": {
|
|
108
|
+
content: "attr(data-placeholder)",
|
|
109
|
+
color: theme?.text?.secondary
|
|
110
|
+
}
|
|
111
|
+
}));
|
|
112
|
+
const MERMAID_FENCE = /^\s{0,3}(?:`{3,}|~{3,})\s*mermaid\b/;
|
|
113
|
+
const noop = () => {};
|
|
114
|
+
/** An empty document still needs one editable block to type into. */
|
|
115
|
+
const toBlocks = (value) => {
|
|
116
|
+
const blocks = splitBlocks(value);
|
|
117
|
+
return blocks.length > 0 ? blocks : [{
|
|
118
|
+
source: "",
|
|
119
|
+
start: 0,
|
|
120
|
+
end: value.length
|
|
121
|
+
}];
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* One contentEditable block. The preview element is created once per mount -
|
|
125
|
+
* while the browser owns the DOM during typing, React must never reconcile
|
|
126
|
+
* the subtree under the caret. The parent remounts the block (source-derived
|
|
127
|
+
* key) whenever its markdown actually changes.
|
|
128
|
+
*/
|
|
129
|
+
function EditableBlock({ index, source, isEditable, placeholder, slotProps, blockProps, onEdit, onDone }) {
|
|
130
|
+
const [content] = (0, react.useState)(() => source === "" ? null : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Preview.Preview, {
|
|
131
|
+
slotProps,
|
|
132
|
+
children: source
|
|
133
|
+
}));
|
|
134
|
+
const handleKeyDown = (event) => {
|
|
135
|
+
if (event.key !== "Escape") return;
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
event.currentTarget.blur();
|
|
138
|
+
};
|
|
139
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LiveEditBlock, {
|
|
140
|
+
...blockProps,
|
|
141
|
+
"aria-label": "Edit block",
|
|
142
|
+
tabIndex: isEditable ? 0 : void 0,
|
|
143
|
+
contentEditable: isEditable,
|
|
144
|
+
suppressContentEditableWarning: true,
|
|
145
|
+
"data-placeholder": source === "" ? placeholder : void 0,
|
|
146
|
+
onInput: (event) => onEdit(index, event.currentTarget),
|
|
147
|
+
onBlur: onDone,
|
|
148
|
+
onKeyDown: handleKeyDown,
|
|
149
|
+
children: content
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A preview you write on directly: the document renders exactly like
|
|
154
|
+
* `Preview`, split into blocks, and every block is `contentEditable` - no
|
|
155
|
+
* textarea. While a block is being typed in, the browser owns its DOM; each
|
|
156
|
+
* input serializes that block back to markdown and splices it into `value`,
|
|
157
|
+
* so `onChange` stays live and untouched blocks keep their exact source.
|
|
158
|
+
* Blur (or Escape) re-splits the document. Mermaid blocks are read-only -
|
|
159
|
+
* a diagram has no text to edit in place. Inside a `Control` it is driven by
|
|
160
|
+
* context; standalone it is a plain controlled component.
|
|
161
|
+
*/
|
|
162
|
+
function LiveEdit({ value: valueProp, onChange: onChangeProp, placeholder = "Click to start writing...", slotProps, style }) {
|
|
163
|
+
const { root, block: blockSlot, ...elementProps } = slotProps ?? {};
|
|
164
|
+
const context = (0, react.useContext)(require_MarkdownContext.MarkdownContext);
|
|
165
|
+
const value = context?.value ?? valueProp ?? "";
|
|
166
|
+
const onChange = context?.onChange ?? onChangeProp ?? noop;
|
|
167
|
+
const [blocks, setBlocks] = (0, react.useState)(() => toBlocks(value));
|
|
168
|
+
const [generation, setGeneration] = (0, react.useState)(0);
|
|
169
|
+
const offsetsRef = (0, react.useRef)(null);
|
|
170
|
+
const getOffsets = () => {
|
|
171
|
+
if (offsetsRef.current?.generation !== generation) offsetsRef.current = {
|
|
172
|
+
generation,
|
|
173
|
+
offsets: blocks.map(({ start, end }) => ({
|
|
174
|
+
start,
|
|
175
|
+
end
|
|
176
|
+
}))
|
|
177
|
+
};
|
|
178
|
+
return offsetsRef.current.offsets;
|
|
179
|
+
};
|
|
180
|
+
const resplit = (next) => {
|
|
181
|
+
setBlocks(toBlocks(next));
|
|
182
|
+
setGeneration((current) => current + 1);
|
|
183
|
+
};
|
|
184
|
+
const syncedValueRef = (0, react.useRef)(value);
|
|
185
|
+
if (value !== syncedValueRef.current) {
|
|
186
|
+
syncedValueRef.current = value;
|
|
187
|
+
resplit(value);
|
|
188
|
+
}
|
|
189
|
+
const handleEdit = (index, element) => {
|
|
190
|
+
const markdown = blockToMarkdown(element);
|
|
191
|
+
const offsets = getOffsets();
|
|
192
|
+
const block = offsets[index];
|
|
193
|
+
const current = syncedValueRef.current;
|
|
194
|
+
const next = current.slice(0, block.start) + markdown + current.slice(block.end);
|
|
195
|
+
const delta = markdown.length - (block.end - block.start);
|
|
196
|
+
block.end = block.start + markdown.length;
|
|
197
|
+
for (const later of offsets.slice(index + 1)) {
|
|
198
|
+
later.start += delta;
|
|
199
|
+
later.end += delta;
|
|
200
|
+
}
|
|
201
|
+
syncedValueRef.current = next;
|
|
202
|
+
onChange(next);
|
|
203
|
+
};
|
|
204
|
+
const handleDone = () => resplit(syncedValueRef.current);
|
|
205
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(LiveEditRoot, {
|
|
206
|
+
...require_mergeOverrides.mergeOverrides(root, { style }),
|
|
207
|
+
children: blocks.map((block, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditableBlock, {
|
|
208
|
+
index,
|
|
209
|
+
source: block.source,
|
|
210
|
+
isEditable: !MERMAID_FENCE.test(block.source),
|
|
211
|
+
placeholder,
|
|
212
|
+
slotProps: elementProps,
|
|
213
|
+
blockProps: blockSlot,
|
|
214
|
+
onEdit: handleEdit,
|
|
215
|
+
onDone: handleDone
|
|
216
|
+
}, `${generation}:${index}`))
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
Object.defineProperty(exports, "LiveEdit", {
|
|
220
|
+
enumerable: true,
|
|
221
|
+
get: function() {
|
|
222
|
+
return LiveEdit;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { n as MarkdownLiveEditSlot } from "./themeTypes-C4kTtkIA.cjs";
|
|
2
|
+
import { PreviewSlotProps } from "./Preview.cjs";
|
|
3
|
+
import { SlotProps, StyleOverride } from "@gild-ui/react";
|
|
4
|
+
export interface LiveEditProps {
|
|
5
|
+
/** Standalone source value - used when rendered outside a `Control`. Ignored inside one. */
|
|
6
|
+
value?: string;
|
|
7
|
+
/** Standalone change handler - used when rendered outside a `Control`. Ignored inside one. */
|
|
8
|
+
onChange?: (value: string) => void;
|
|
9
|
+
/** Shown inside the (empty) document's first block until something is typed. */
|
|
10
|
+
placeholder?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Per-element prop overrides forwarded to every block's preview, and the two slots a theme can
|
|
13
|
+
* style: `root` and `block` (each block), taking a `style`, a `className` and `data-*` and `aria-*`.
|
|
14
|
+
*/
|
|
15
|
+
slotProps?: PreviewSlotProps & SlotProps<MarkdownLiveEditSlot>;
|
|
16
|
+
/** Overrides the CSS of the root: an entry of `StyleSheet.create`, or a list of them. It wins over `slotProps.root.style`. */
|
|
17
|
+
style?: StyleOverride;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A preview you write on directly: the document renders exactly like
|
|
21
|
+
* `Preview`, split into blocks, and every block is `contentEditable` - no
|
|
22
|
+
* textarea. While a block is being typed in, the browser owns its DOM; each
|
|
23
|
+
* input serializes that block back to markdown and splices it into `value`,
|
|
24
|
+
* so `onChange` stays live and untouched blocks keep their exact source.
|
|
25
|
+
* Blur (or Escape) re-splits the document. Mermaid blocks are read-only -
|
|
26
|
+
* a diagram has no text to edit in place. Inside a `Control` it is driven by
|
|
27
|
+
* context; standalone it is a plain controlled component.
|
|
28
|
+
*/
|
|
29
|
+
export declare function LiveEdit({ value: valueProp, onChange: onChangeProp, placeholder, slotProps, style }: Readonly<LiveEditProps>): import("react").JSX.Element;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { n as MarkdownLiveEditSlot } from "./themeTypes-C4kTtkIA.mjs";
|
|
2
|
+
import { r as PreviewSlotProps } from "./index-CfPKWPpM.mjs";
|
|
3
|
+
import { SlotProps, StyleOverride } from "@gild-ui/react";
|
|
4
|
+
export interface LiveEditProps {
|
|
5
|
+
/** Standalone source value - used when rendered outside a `Control`. Ignored inside one. */
|
|
6
|
+
value?: string;
|
|
7
|
+
/** Standalone change handler - used when rendered outside a `Control`. Ignored inside one. */
|
|
8
|
+
onChange?: (value: string) => void;
|
|
9
|
+
/** Shown inside the (empty) document's first block until something is typed. */
|
|
10
|
+
placeholder?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Per-element prop overrides forwarded to every block's preview, and the two slots a theme can
|
|
13
|
+
* style: `root` and `block` (each block), taking a `style`, a `className` and `data-*` and `aria-*`.
|
|
14
|
+
*/
|
|
15
|
+
slotProps?: PreviewSlotProps & SlotProps<MarkdownLiveEditSlot>;
|
|
16
|
+
/** Overrides the CSS of the root: an entry of `StyleSheet.create`, or a list of them. It wins over `slotProps.root.style`. */
|
|
17
|
+
style?: StyleOverride;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A preview you write on directly: the document renders exactly like
|
|
21
|
+
* `Preview`, split into blocks, and every block is `contentEditable` - no
|
|
22
|
+
* textarea. While a block is being typed in, the browser owns its DOM; each
|
|
23
|
+
* input serializes that block back to markdown and splices it into `value`,
|
|
24
|
+
* so `onChange` stays live and untouched blocks keep their exact source.
|
|
25
|
+
* Blur (or Escape) re-splits the document. Mermaid blocks are read-only -
|
|
26
|
+
* a diagram has no text to edit in place. Inside a `Control` it is driven by
|
|
27
|
+
* context; standalone it is a plain controlled component.
|
|
28
|
+
*/
|
|
29
|
+
export declare function LiveEdit({ value: valueProp, onChange: onChangeProp, placeholder, slotProps, style }: Readonly<LiveEditProps>): import("react").JSX.Element;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as LiveEdit } from "./LiveEdit-1-8cA5Xw.mjs";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
// src/MarkdownContext/MarkdownContext.ts
|
|
3
|
+
const MarkdownContext = createContext(null);
|
|
4
|
+
/** Reads the Markdown compound context; throws if used outside `<Markdown.Control>`. */
|
|
5
|
+
function useMarkdownContext() {
|
|
6
|
+
const context = useContext(MarkdownContext);
|
|
7
|
+
if (!context) throw new Error("Markdown parts must be rendered inside <Markdown.Control>");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export { useMarkdownContext as n, MarkdownContext as t };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
// src/MarkdownContext/MarkdownContext.ts
|
|
3
|
+
const MarkdownContext = (0, react.createContext)(null);
|
|
4
|
+
/** Reads the Markdown compound context; throws if used outside `<Markdown.Control>`. */
|
|
5
|
+
function useMarkdownContext() {
|
|
6
|
+
const context = (0, react.useContext)(MarkdownContext);
|
|
7
|
+
if (!context) throw new Error("Markdown parts must be rendered inside <Markdown.Control>");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
Object.defineProperty(exports, "MarkdownContext", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function() {
|
|
13
|
+
return MarkdownContext;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(exports, "useMarkdownContext", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function() {
|
|
19
|
+
return useMarkdownContext;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { CodeBlock } from "./CodeBlock.mjs";
|
|
2
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { Button } from "@gild-ui/react/Button";
|
|
5
|
+
import { Flex } from "@gild-ui/react/Flex";
|
|
6
|
+
import { Icon } from "@gild-ui/react/Icon";
|
|
7
|
+
import { styled, useStyleValue, useTheme } from "@gild-ui/react";
|
|
8
|
+
import { View } from "@gild-ui/react/View";
|
|
9
|
+
import { Grid } from "@gild-ui/react/Grid";
|
|
10
|
+
import { Modal } from "@gild-ui/react/Modal";
|
|
11
|
+
// src/Mermaid/DiagramViewer.tsx
|
|
12
|
+
const MIN_SCALE = .2;
|
|
13
|
+
const MAX_SCALE = 5;
|
|
14
|
+
const ZOOM_STEP = .2;
|
|
15
|
+
const PAN_STEP = 48;
|
|
16
|
+
const MIN_HEIGHT = 180;
|
|
17
|
+
const MAX_HEIGHT = 640;
|
|
18
|
+
const Viewport = styled(Flex)(({ isDragging }) => ({
|
|
19
|
+
cursor: isDragging ? "grabbing" : "grab",
|
|
20
|
+
userSelect: "none",
|
|
21
|
+
WebkitUserSelect: "none"
|
|
22
|
+
}));
|
|
23
|
+
const Layer = styled(View)(({ isDragging, panX, panY, zoomLevel }) => ({
|
|
24
|
+
transform: `translate(${panX}, ${panY}) scale(${zoomLevel})`,
|
|
25
|
+
transformOrigin: "center",
|
|
26
|
+
transition: isDragging ? "none" : "transform 0.12s ease-out"
|
|
27
|
+
}));
|
|
28
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
29
|
+
/**
|
|
30
|
+
* Wraps a rendered mermaid diagram in a zoom/pan viewport with a themed control cluster
|
|
31
|
+
* (bottom-right: a pan d-pad around a reset, plus zoom in/out) and an expand control
|
|
32
|
+
* (top-right) that opens the diagram in a fullscreen dialog. Drag-to-pan and wheel-to-zoom
|
|
33
|
+
* work too. Only the diagram layer clips; the controls sit above it, so a short diagram never
|
|
34
|
+
* hides them. Everything is a CSS transform - no dependency.
|
|
35
|
+
*/
|
|
36
|
+
function DiagramViewer({ svg, expandable = true, fill = false }) {
|
|
37
|
+
const [scale, setScale] = useState(1);
|
|
38
|
+
const [offset, setOffset] = useState({
|
|
39
|
+
x: 0,
|
|
40
|
+
y: 0
|
|
41
|
+
});
|
|
42
|
+
const [dragging, setDragging] = useState(false);
|
|
43
|
+
const [fullscreen, setFullscreen] = useState(false);
|
|
44
|
+
const [naturalHeight, setNaturalHeight] = useState(0);
|
|
45
|
+
const dragOrigin = useRef(null);
|
|
46
|
+
const contentRef = useRef(null);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
setNaturalHeight(contentRef.current.offsetHeight);
|
|
49
|
+
}, [svg]);
|
|
50
|
+
const [x, setX] = useStyleValue(0, "px");
|
|
51
|
+
const [y, setY] = useStyleValue(0, "px");
|
|
52
|
+
const [zoom, setZoom] = useStyleValue(1);
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
setX(offset.x);
|
|
55
|
+
setY(offset.y);
|
|
56
|
+
setZoom(scale);
|
|
57
|
+
}, [
|
|
58
|
+
offset,
|
|
59
|
+
scale,
|
|
60
|
+
setX,
|
|
61
|
+
setY,
|
|
62
|
+
setZoom
|
|
63
|
+
]);
|
|
64
|
+
const zoomBy = (delta) => setScale((current) => clamp(current + delta, MIN_SCALE, MAX_SCALE));
|
|
65
|
+
const panBy = (dx, dy) => setOffset((current) => ({
|
|
66
|
+
x: current.x + dx,
|
|
67
|
+
y: current.y + dy
|
|
68
|
+
}));
|
|
69
|
+
const reset = () => {
|
|
70
|
+
setScale(1);
|
|
71
|
+
setOffset({
|
|
72
|
+
x: 0,
|
|
73
|
+
y: 0
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
const closeFullscreen = () => setFullscreen(false);
|
|
77
|
+
const onWheel = (event) => {
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
zoomBy(event.deltaY < 0 ? ZOOM_STEP : -.2);
|
|
80
|
+
};
|
|
81
|
+
const onMouseDown = (event) => {
|
|
82
|
+
dragOrigin.current = {
|
|
83
|
+
x: event.clientX - offset.x,
|
|
84
|
+
y: event.clientY - offset.y
|
|
85
|
+
};
|
|
86
|
+
setDragging(true);
|
|
87
|
+
};
|
|
88
|
+
const onMouseMove = (event) => {
|
|
89
|
+
if (!dragOrigin.current) return;
|
|
90
|
+
setOffset({
|
|
91
|
+
x: event.clientX - dragOrigin.current.x,
|
|
92
|
+
y: event.clientY - dragOrigin.current.y
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
const endDrag = () => {
|
|
96
|
+
dragOrigin.current = null;
|
|
97
|
+
setDragging(false);
|
|
98
|
+
};
|
|
99
|
+
const control = (label, icon, onClick) => /* @__PURE__ */ jsx(Button, {
|
|
100
|
+
type: "button",
|
|
101
|
+
variant: "outlined",
|
|
102
|
+
shape: "rounded",
|
|
103
|
+
size: "sm",
|
|
104
|
+
"aria-label": label,
|
|
105
|
+
onClick,
|
|
106
|
+
children: /* @__PURE__ */ jsx(Icon, {
|
|
107
|
+
name: icon,
|
|
108
|
+
size: "1.1rem"
|
|
109
|
+
})
|
|
110
|
+
}, label);
|
|
111
|
+
const inlineHeight = Math.max(MIN_HEIGHT, clamp(naturalHeight, MIN_HEIGHT, MAX_HEIGHT) * scale);
|
|
112
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(View, {
|
|
113
|
+
my: fill ? 0 : 2,
|
|
114
|
+
position: "relative",
|
|
115
|
+
maxWidth: "100%",
|
|
116
|
+
borderRadius: "md",
|
|
117
|
+
height: fill ? "100%" : void 0,
|
|
118
|
+
children: [
|
|
119
|
+
/* @__PURE__ */ jsx(Viewport, {
|
|
120
|
+
isDragging: dragging,
|
|
121
|
+
onWheel: fill ? onWheel : void 0,
|
|
122
|
+
onMouseDown,
|
|
123
|
+
onMouseMove,
|
|
124
|
+
onMouseUp: endDrag,
|
|
125
|
+
onMouseLeave: endDrag,
|
|
126
|
+
overflow: "hidden",
|
|
127
|
+
borderRadius: "md",
|
|
128
|
+
alignItems: "center",
|
|
129
|
+
justifyContent: "center",
|
|
130
|
+
height: fill ? "100%" : `${inlineHeight}px`,
|
|
131
|
+
children: /* @__PURE__ */ jsx(Layer, {
|
|
132
|
+
ref: contentRef,
|
|
133
|
+
"data-testid": "diagram-transform",
|
|
134
|
+
isDragging: dragging,
|
|
135
|
+
panX: x,
|
|
136
|
+
panY: y,
|
|
137
|
+
zoomLevel: zoom,
|
|
138
|
+
dangerouslySetInnerHTML: { __html: svg }
|
|
139
|
+
})
|
|
140
|
+
}),
|
|
141
|
+
expandable && /* @__PURE__ */ jsx(View, {
|
|
142
|
+
position: "absolute",
|
|
143
|
+
top: 1,
|
|
144
|
+
right: 1,
|
|
145
|
+
zIndex: 1,
|
|
146
|
+
children: control("Expand diagram", "fullscreen", () => setFullscreen(true))
|
|
147
|
+
}),
|
|
148
|
+
/* @__PURE__ */ jsxs(Grid, {
|
|
149
|
+
position: "absolute",
|
|
150
|
+
bottom: 1,
|
|
151
|
+
right: 1,
|
|
152
|
+
zIndex: 1,
|
|
153
|
+
gridTemplateColumns: "repeat(3, auto)",
|
|
154
|
+
gap: .5,
|
|
155
|
+
children: [
|
|
156
|
+
/* @__PURE__ */ jsx("span", { "aria-hidden": true }),
|
|
157
|
+
control("Pan up", "expand_less", () => panBy(0, -48)),
|
|
158
|
+
control("Zoom in", "zoom_in", () => zoomBy(ZOOM_STEP)),
|
|
159
|
+
control("Pan left", "chevron_left", () => panBy(-48, 0)),
|
|
160
|
+
control("Reset view", "refresh", reset),
|
|
161
|
+
control("Pan right", "chevron_right", () => panBy(PAN_STEP, 0)),
|
|
162
|
+
/* @__PURE__ */ jsx("span", { "aria-hidden": true }),
|
|
163
|
+
control("Pan down", "expand_more", () => panBy(0, PAN_STEP)),
|
|
164
|
+
control("Zoom out", "zoom_out", () => zoomBy(-.2))
|
|
165
|
+
]
|
|
166
|
+
})
|
|
167
|
+
]
|
|
168
|
+
}), expandable && /* @__PURE__ */ jsx(Modal, {
|
|
169
|
+
isOpen: fullscreen,
|
|
170
|
+
onClose: closeFullscreen,
|
|
171
|
+
children: /* @__PURE__ */ jsxs(Flex, {
|
|
172
|
+
bg: "paper",
|
|
173
|
+
role: "dialog",
|
|
174
|
+
"aria-label": "Diagram fullscreen",
|
|
175
|
+
position: "fixed",
|
|
176
|
+
top: 0,
|
|
177
|
+
right: 0,
|
|
178
|
+
bottom: 0,
|
|
179
|
+
left: 0,
|
|
180
|
+
p: 2,
|
|
181
|
+
children: [/* @__PURE__ */ jsx(View, {
|
|
182
|
+
position: "absolute",
|
|
183
|
+
top: 2,
|
|
184
|
+
right: 2,
|
|
185
|
+
zIndex: 1,
|
|
186
|
+
children: control("Close", "close", closeFullscreen)
|
|
187
|
+
}), /* @__PURE__ */ jsx(Flex, {
|
|
188
|
+
flex: 1,
|
|
189
|
+
minHeight: 0,
|
|
190
|
+
children: /* @__PURE__ */ jsx(DiagramViewer, {
|
|
191
|
+
svg,
|
|
192
|
+
expandable: false,
|
|
193
|
+
fill: true
|
|
194
|
+
})
|
|
195
|
+
})]
|
|
196
|
+
})
|
|
197
|
+
})] });
|
|
198
|
+
}
|
|
199
|
+
// src/Mermaid/Mermaid.tsx
|
|
200
|
+
async function toSvg(id, chart, theme) {
|
|
201
|
+
const { default: mermaid } = await import("mermaid");
|
|
202
|
+
mermaid.initialize({
|
|
203
|
+
startOnLoad: false,
|
|
204
|
+
securityLevel: "strict",
|
|
205
|
+
suppressErrorRendering: true,
|
|
206
|
+
theme: theme.colorScheme === "dark" ? "dark" : "default",
|
|
207
|
+
themeVariables: {
|
|
208
|
+
darkMode: theme.colorScheme === "dark",
|
|
209
|
+
background: theme.background.paper,
|
|
210
|
+
fontFamily: theme.fonts.body,
|
|
211
|
+
mainBkg: theme.background.primary,
|
|
212
|
+
...theme.mermaid
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
const { svg } = await mermaid.render(id, chart);
|
|
216
|
+
return svg;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Renders a mermaid diagram. Until the client renders the SVG - and if the source is
|
|
220
|
+
* invalid - it falls back to the diagram source in a `CodeBlock`, so server output and
|
|
221
|
+
* no-JS readers still show something useful and hydration stays stable.
|
|
222
|
+
*/
|
|
223
|
+
function Mermaid({ chart, diagram }) {
|
|
224
|
+
const theme = useTheme();
|
|
225
|
+
const id = `mermaid-${useId().replace(/[^a-zA-Z0-9-]/g, "")}`;
|
|
226
|
+
const [svg, setSvg] = useState(null);
|
|
227
|
+
useEffect(() => {
|
|
228
|
+
let active = true;
|
|
229
|
+
toSvg(id, chart, theme).then((rendered) => {
|
|
230
|
+
if (active) setSvg(rendered);
|
|
231
|
+
}).catch(() => {
|
|
232
|
+
if (active) setSvg(null);
|
|
233
|
+
});
|
|
234
|
+
return () => {
|
|
235
|
+
active = false;
|
|
236
|
+
};
|
|
237
|
+
}, [
|
|
238
|
+
chart,
|
|
239
|
+
id,
|
|
240
|
+
theme
|
|
241
|
+
]);
|
|
242
|
+
if (svg) return /* @__PURE__ */ jsx(DiagramViewer, {
|
|
243
|
+
svg,
|
|
244
|
+
...diagram
|
|
245
|
+
});
|
|
246
|
+
return /* @__PURE__ */ jsx(CodeBlock, { children: chart });
|
|
247
|
+
}
|
|
248
|
+
export { Mermaid as t };
|