@conloca/mdx 0.1.15 → 0.1.16
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 +15 -5
- package/dist/blocks.d.ts +37 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +51 -0
- package/dist/code-block-frame.d.ts +41 -0
- package/dist/code-block-frame.d.ts.map +1 -0
- package/dist/code-block-frame.js +186 -0
- package/dist/editor-core.d.ts +58 -0
- package/dist/editor-core.d.ts.map +1 -0
- package/dist/editor-core.js +181 -0
- package/dist/hooks/useMDXEvaluation.d.ts +28 -0
- package/dist/hooks/useMDXEvaluation.d.ts.map +1 -0
- package/dist/hooks/useMDXEvaluation.js +117 -0
- package/dist/index.d.mts +74 -43
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.mjs +328 -237
- package/dist/index.mjs.map +1 -1
- package/dist/markdown-options.d.ts +3 -0
- package/dist/markdown-options.d.ts.map +1 -0
- package/dist/markdown-options.js +19 -0
- package/dist/mdx/compile.d.ts +3 -0
- package/dist/mdx/compile.d.ts.map +1 -0
- package/dist/mdx/compile.js +27 -0
- package/dist/mdx/evaluate.d.ts +44 -0
- package/dist/mdx/evaluate.d.ts.map +1 -0
- package/dist/mdx/evaluate.js +57 -0
- package/dist/mdx-editor.d.ts +7 -0
- package/dist/mdx-editor.d.ts.map +1 -0
- package/dist/mdx-editor.js +7 -0
- package/dist/node.d.mts +0 -5
- package/dist/node.d.mts.map +1 -1
- package/dist/node.d.ts +17 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +16 -0
- package/dist/node.mjs +26 -11
- package/dist/node.mjs.map +1 -1
- package/dist/style.css +88 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +54 -15
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,253 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import "
|
|
1
|
+
import './style.css';
|
|
2
|
+
import { BlockTypeSelect, BoldItalicUnderlineToggles, ButtonWithTooltip, Cell, ChangeCodeMirrorLanguage, CodeMirrorEditor, ConditionalContents, CreateLink, DiffSourceToggleWrapper, GenericJsxEditor, HighlightToggle, InsertCodeBlock, InsertImage, InsertTable, InsertThematicBreak, ListsToggle, MDXEditor as MDXEditor$1, Separator, StrikeThroughSupSubToggles, UndoRedo, codeBlockPlugin, codeMirrorPlugin, diffSourcePlugin, headingsPlugin, imagePlugin, jsxPlugin, linkDialogPlugin, linkPlugin, listsPlugin, markdownShortcutPlugin, quotePlugin, tablePlugin, thematicBreakPlugin, toolbarPlugin, useCellValue, useCodeBlockEditorContext } from "@mdxeditor/editor";
|
|
3
|
+
import { Check, Copy, Maximize2, Minimize2 } from "lucide-react";
|
|
4
|
+
import React, { createElement, useCallback, useEffect, useMemo, useState, useSyncExternalStore } from "react";
|
|
4
5
|
import * as runtime from "react/jsx-runtime";
|
|
5
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import "@mdxeditor/editor/style.css";
|
|
6
8
|
import * as devRuntime from "react/jsx-dev-runtime";
|
|
7
|
-
|
|
9
|
+
//#region src/code-block-frame.tsx
|
|
10
|
+
/**
|
|
11
|
+
* Frame chrome around the editor's fenced code blocks — header bar with
|
|
12
|
+
* filename, language tag, and a copy button. The outer wrapper picks up
|
|
13
|
+
* the host's code-block classes (published via `codeBlockWrapperInfo$`
|
|
14
|
+
* by the host-wrapper plugin in cms-spa), so the host's actual code-
|
|
15
|
+
* block CSS (Starlight's expressive-code, Tailwind's pre styling, etc.)
|
|
16
|
+
* reaches the editor's frame through the cascade — no shipped mirror.
|
|
17
|
+
*
|
|
18
|
+
* Host-agnostic: no Shiki / Starlight / ExpressiveCode imports. Class
|
|
19
|
+
* names are discovered at runtime from the host's live HTML.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Realm-published cell carrying the host's code-block wrapper chain,
|
|
23
|
+
* outermost first. For Starlight's expressive-code:
|
|
24
|
+
*
|
|
25
|
+
* [
|
|
26
|
+
* { tagName: 'div', className: 'expressive-code' },
|
|
27
|
+
* { tagName: 'figure', className: 'frame has-title not-content' },
|
|
28
|
+
* ]
|
|
29
|
+
*
|
|
30
|
+
* The cms-spa `hostWrapperPlugin` writes to this whenever the fetched
|
|
31
|
+
* wrapper info changes; the code-block frame reads from it to render
|
|
32
|
+
* each chain link as a nested element with the host's expected tag
|
|
33
|
+
* name. Same publish-pattern as the content-wrapper plugin in cms-spa.
|
|
34
|
+
*
|
|
35
|
+
* Null when the host has no code-block chrome (or the fetch is in
|
|
36
|
+
* flight). The frame then falls back to its base `.conloca-code-block`
|
|
37
|
+
* wrapper with no host inheritance — "looks plain but not broken."
|
|
38
|
+
*/
|
|
39
|
+
const codeBlockWrapperInfo$ = Cell(null);
|
|
40
|
+
const TITLE_META_RE = /\btitle=(?:"([^"]*)"|'([^']*)'|(\S+))/;
|
|
41
|
+
const FILENAME_COMMENT_RE = /^\s*(?:\/\/|#|;|--|<!--|\/\*)\s*([\w.\-/@]+\.[\w.-]+)\s*(?:-->|\*\/)?\s*$/;
|
|
42
|
+
function parseMeta(meta) {
|
|
43
|
+
const match = meta.match(TITLE_META_RE);
|
|
44
|
+
if (!match) return {
|
|
45
|
+
title: "",
|
|
46
|
+
rest: meta.trim()
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
title: match[1] ?? match[2] ?? match[3] ?? "",
|
|
50
|
+
rest: (meta.slice(0, match.index) + meta.slice(match.index + match[0].length)).replace(/\s+/g, " ").trim()
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** Reassemble a meta string with `title` set (or stripped when empty). */
|
|
54
|
+
function buildMeta(title, rest) {
|
|
55
|
+
const trimmedTitle = title.trim();
|
|
56
|
+
const titleFragment = trimmedTitle ? `title="${trimmedTitle.replace(/"/g, "\\\"")}"` : "";
|
|
57
|
+
if (titleFragment && rest) return `${titleFragment} ${rest}`;
|
|
58
|
+
return titleFragment || rest;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Detect a leading filename comment, the convention EC's frames plugin
|
|
62
|
+
* uses on the published page when no explicit `title=` is set. Returns
|
|
63
|
+
* the filename when line 1 of the code is a single-line comment ending
|
|
64
|
+
* in a dot-extension; empty string otherwise.
|
|
65
|
+
*
|
|
66
|
+
* Used to pre-fill the editor's filename input on legacy fences so
|
|
67
|
+
* authors see the same value the reader sees in the rendered tab. Pure
|
|
68
|
+
* read — the underlying code/comment is never touched.
|
|
69
|
+
*/
|
|
70
|
+
function detectFilenameComment(code) {
|
|
71
|
+
const match = (code.split("\n", 1)[0] ?? "").match(FILENAME_COMMENT_RE);
|
|
72
|
+
return match ? match[1] ?? "" : "";
|
|
73
|
+
}
|
|
74
|
+
function CopyButton({ code }) {
|
|
75
|
+
const [copied, setCopied] = useState(false);
|
|
76
|
+
return /* @__PURE__ */ jsx("button", {
|
|
77
|
+
type: "button",
|
|
78
|
+
onClick: useCallback(() => {
|
|
79
|
+
navigator.clipboard.writeText(code).then(() => {
|
|
80
|
+
setCopied(true);
|
|
81
|
+
window.setTimeout(() => setCopied(false), 1200);
|
|
82
|
+
});
|
|
83
|
+
}, [code]),
|
|
84
|
+
className: "conloca-code-copy",
|
|
85
|
+
"aria-label": copied ? "Copied" : "Copy code",
|
|
86
|
+
title: copied ? "Copied" : "Copy code",
|
|
87
|
+
children: copied ? /* @__PURE__ */ jsx(Check, {
|
|
88
|
+
size: 14,
|
|
89
|
+
"aria-hidden": "true"
|
|
90
|
+
}) : /* @__PURE__ */ jsx(Copy, {
|
|
91
|
+
size: 14,
|
|
92
|
+
"aria-hidden": "true"
|
|
93
|
+
})
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Editable filename slot. Rewrites the code block's meta string via
|
|
98
|
+
* `setMeta` from `useCodeBlockEditorContext` so authors can author the
|
|
99
|
+
* frame title (`\`\`\`ts title="src/foo.ts"`) directly from the editor —
|
|
100
|
+
* the meta string round-trips through MDXEditor's markdown serializer
|
|
101
|
+
* and ExpressiveCode picks it up as the frame header on the published
|
|
102
|
+
* page. Other meta fragments (ins/del/mark/wrap) are preserved.
|
|
103
|
+
*
|
|
104
|
+
* Legacy fences with a filename comment on line 1 (`// foo.ts`) — EC's
|
|
105
|
+
* pre-meta convention for tab titles — are detected and pre-filled in
|
|
106
|
+
* the input as a read-only display. Opening such a fence does NOT
|
|
107
|
+
* mutate the source. Editing the field is an explicit author intent:
|
|
108
|
+
* the new value is written as `title=` meta AND the original comment
|
|
109
|
+
* line is stripped, so the editor's input becomes the single source of
|
|
110
|
+
* truth without breaking the published render.
|
|
111
|
+
*/
|
|
112
|
+
function FilenameInput({ meta, code }) {
|
|
113
|
+
const { setMeta, setCode } = useCodeBlockEditorContext();
|
|
114
|
+
const parsed = useMemo(() => parseMeta(meta), [meta]);
|
|
115
|
+
const detectedFromComment = useMemo(() => parsed.title ? "" : detectFilenameComment(code), [parsed.title, code]);
|
|
116
|
+
const displayed = parsed.title || detectedFromComment;
|
|
117
|
+
const [title, setTitle] = useState(displayed);
|
|
118
|
+
const [interacted, setInteracted] = useState(false);
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
setTitle(displayed);
|
|
121
|
+
setInteracted(false);
|
|
122
|
+
}, [displayed]);
|
|
123
|
+
const commit = useCallback((next) => {
|
|
124
|
+
if (!interacted) return;
|
|
125
|
+
if (next === parsed.title) return;
|
|
126
|
+
setMeta(buildMeta(next, parsed.rest));
|
|
127
|
+
if (detectedFromComment) {
|
|
128
|
+
const lines = code.split("\n");
|
|
129
|
+
if (lines[0] && FILENAME_COMMENT_RE.test(lines[0])) setCode(lines.slice(1).join("\n").replace(/^\n+/, ""));
|
|
130
|
+
}
|
|
131
|
+
}, [
|
|
132
|
+
interacted,
|
|
133
|
+
parsed.title,
|
|
134
|
+
parsed.rest,
|
|
135
|
+
detectedFromComment,
|
|
136
|
+
code,
|
|
137
|
+
setMeta,
|
|
138
|
+
setCode
|
|
139
|
+
]);
|
|
140
|
+
const onChange = (e) => {
|
|
141
|
+
setInteracted(true);
|
|
142
|
+
setTitle(e.target.value);
|
|
143
|
+
};
|
|
144
|
+
const onBlur = () => commit(title);
|
|
145
|
+
return /* @__PURE__ */ jsx("input", {
|
|
146
|
+
type: "text",
|
|
147
|
+
className: "conloca-code-block__filename",
|
|
148
|
+
value: title,
|
|
149
|
+
placeholder: "filename",
|
|
150
|
+
spellCheck: false,
|
|
151
|
+
onChange,
|
|
152
|
+
onBlur,
|
|
153
|
+
"aria-label": "Code block filename"
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function ConlocaCodeBlockEditor(props) {
|
|
157
|
+
const chain = useCellValue(codeBlockWrapperInfo$) ?? [];
|
|
158
|
+
const frameLink = chain.length > 0 ? chain[chain.length - 1] : null;
|
|
159
|
+
const outerLinks = frameLink ? chain.slice(0, -1) : [];
|
|
160
|
+
const frameInner = /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
161
|
+
/* @__PURE__ */ jsx("figcaption", {
|
|
162
|
+
className: "conloca-code-block__header header",
|
|
163
|
+
children: /* @__PURE__ */ jsx("span", {
|
|
164
|
+
className: "title",
|
|
165
|
+
children: /* @__PURE__ */ jsx(FilenameInput, {
|
|
166
|
+
meta: props.meta,
|
|
167
|
+
code: props.code
|
|
168
|
+
})
|
|
169
|
+
})
|
|
170
|
+
}),
|
|
171
|
+
/* @__PURE__ */ jsx("pre", {
|
|
172
|
+
className: "conloca-code-block__pre",
|
|
173
|
+
children: /* @__PURE__ */ jsx(CodeMirrorEditor, { ...props })
|
|
174
|
+
}),
|
|
175
|
+
/* @__PURE__ */ jsx("div", {
|
|
176
|
+
className: "conloca-code-block__copy copy",
|
|
177
|
+
children: /* @__PURE__ */ jsx(CopyButton, { code: props.code })
|
|
178
|
+
})
|
|
179
|
+
] });
|
|
180
|
+
const frame = frameLink ? createElement(frameLink.tagName, { className: `conloca-code-block ${frameLink.className}` }, frameInner) : /* @__PURE__ */ jsx("div", {
|
|
181
|
+
className: "conloca-code-block",
|
|
182
|
+
children: frameInner
|
|
183
|
+
});
|
|
184
|
+
return outerLinks.reduceRight((kids, link) => createElement(link.tagName, { className: link.className }, kids), frame);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Catch-all descriptor — `priority: 100`, `match: () => true` — registered
|
|
188
|
+
* via `codeBlockPlugin({ codeBlockEditorDescriptors: [conlocaCodeBlockDescriptor] })`.
|
|
189
|
+
* Wraps the upstream `CodeMirrorEditor` with the host-themed frame.
|
|
190
|
+
*/
|
|
191
|
+
const conlocaCodeBlockDescriptor = {
|
|
192
|
+
priority: 100,
|
|
193
|
+
match: () => true,
|
|
194
|
+
Editor: ConlocaCodeBlockEditor
|
|
195
|
+
};
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/markdown-options.ts
|
|
198
|
+
const TO_MARKDOWN_OPTIONS = {
|
|
199
|
+
bullet: "-",
|
|
200
|
+
rule: "-",
|
|
201
|
+
emphasis: "_",
|
|
202
|
+
strong: "*",
|
|
203
|
+
fence: "`",
|
|
204
|
+
listItemIndent: "one",
|
|
205
|
+
tightDefinitions: true
|
|
206
|
+
};
|
|
207
|
+
//#endregion
|
|
8
208
|
//#region src/editor-core.tsx
|
|
209
|
+
const FOCUS_MODE_STORAGE_KEY = "conloca.mdxeditor.focusMode";
|
|
210
|
+
const FOCUS_MODE_BODY_CLASS = "mdxeditor-focus-mode";
|
|
211
|
+
/**
|
|
212
|
+
* Toggle focus-mode by writing the class on `document.body`. Direct DOM
|
|
213
|
+
* mutation (vs. piping a boolean through React state into the editor's
|
|
214
|
+
* `className` prop) avoids re-rendering Lexical on every toggle — the
|
|
215
|
+
* editor's prop is the only entry point and changing it forces a parse
|
|
216
|
+
* round-trip. Persisted in localStorage so the preference survives reloads.
|
|
217
|
+
*/
|
|
218
|
+
function readFocusMode() {
|
|
219
|
+
if (typeof document === "undefined") return false;
|
|
220
|
+
return document.body.classList.contains(FOCUS_MODE_BODY_CLASS);
|
|
221
|
+
}
|
|
222
|
+
function subscribeFocusMode(callback) {
|
|
223
|
+
if (typeof window === "undefined") return () => {};
|
|
224
|
+
const handler = () => callback();
|
|
225
|
+
window.addEventListener("conloca:focusModeChange", handler);
|
|
226
|
+
return () => window.removeEventListener("conloca:focusModeChange", handler);
|
|
227
|
+
}
|
|
228
|
+
function setFocusMode(next) {
|
|
229
|
+
if (typeof document === "undefined") return;
|
|
230
|
+
document.body.classList.toggle(FOCUS_MODE_BODY_CLASS, next);
|
|
231
|
+
try {
|
|
232
|
+
if (next) window.localStorage.setItem(FOCUS_MODE_STORAGE_KEY, "1");
|
|
233
|
+
else window.localStorage.removeItem(FOCUS_MODE_STORAGE_KEY);
|
|
234
|
+
} catch {}
|
|
235
|
+
window.dispatchEvent(new Event("conloca:focusModeChange"));
|
|
236
|
+
}
|
|
237
|
+
function FocusModeToggle() {
|
|
238
|
+
const focusMode = useSyncExternalStore(subscribeFocusMode, readFocusMode, () => false);
|
|
239
|
+
const onClick = useCallback(() => setFocusMode(!focusMode), [focusMode]);
|
|
240
|
+
return /* @__PURE__ */ jsx(ButtonWithTooltip, {
|
|
241
|
+
title: focusMode ? "Exit focus mode (wider column)" : "Enter focus mode (narrow column)",
|
|
242
|
+
onClick,
|
|
243
|
+
children: /* @__PURE__ */ jsx(focusMode ? Minimize2 : Maximize2, {
|
|
244
|
+
size: 16,
|
|
245
|
+
"aria-hidden": true
|
|
246
|
+
})
|
|
247
|
+
});
|
|
248
|
+
}
|
|
9
249
|
const codeBlockLanguages = {
|
|
250
|
+
"": "Plain text",
|
|
10
251
|
text: "Plain Text",
|
|
11
252
|
js: "JavaScript",
|
|
12
253
|
jsx: "JavaScript (React)",
|
|
@@ -20,52 +261,41 @@ const codeBlockLanguages = {
|
|
|
20
261
|
bash: "Bash",
|
|
21
262
|
python: "Python"
|
|
22
263
|
};
|
|
264
|
+
/**
|
|
265
|
+
* Wildcard fallback so any unknown JSX tag round-trips through GenericJsxEditor
|
|
266
|
+
* instead of crashing the importer with "Parsing of the following markdown
|
|
267
|
+
* structure failed". Two entries because the upstream visitor dispatches by
|
|
268
|
+
* `kind` (block vs inline JSX) — without the inline one, an inline `<Badge>`
|
|
269
|
+
* mid-paragraph would be matched by the flow wildcard and lifted out of its
|
|
270
|
+
* paragraph in Lexical. Consumers passing typed descriptors win via the
|
|
271
|
+
* exact-name match in the visitor; the wildcard only handles the leftovers.
|
|
272
|
+
*/
|
|
273
|
+
const wildcardJsxDescriptors = [{
|
|
274
|
+
name: "*",
|
|
275
|
+
kind: "flow",
|
|
276
|
+
props: [],
|
|
277
|
+
hasChildren: true,
|
|
278
|
+
Editor: GenericJsxEditor
|
|
279
|
+
}, {
|
|
280
|
+
name: "*",
|
|
281
|
+
kind: "text",
|
|
282
|
+
props: [],
|
|
283
|
+
hasChildren: true,
|
|
284
|
+
Editor: GenericJsxEditor
|
|
285
|
+
}];
|
|
23
286
|
function renderInsertImageButton(imageButtonRef) {
|
|
24
287
|
if (!imageButtonRef) return /* @__PURE__ */ jsx(InsertImage, {});
|
|
25
288
|
return React.createElement(InsertImage, { ref: imageButtonRef });
|
|
26
289
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
componentDidCatch(error, errorInfo) {
|
|
39
|
-
console.error(`${this.props.logPrefix} Error boundary caught:`, error, errorInfo);
|
|
40
|
-
}
|
|
41
|
-
render() {
|
|
42
|
-
if (this.state.hasError) return /* @__PURE__ */ jsxs("div", {
|
|
43
|
-
className: "p-4 bg-red-50 border border-red-200 rounded text-red-600",
|
|
44
|
-
children: [
|
|
45
|
-
/* @__PURE__ */ jsx("p", {
|
|
46
|
-
className: "font-semibold mb-2",
|
|
47
|
-
children: "MDXEditor Error"
|
|
48
|
-
}),
|
|
49
|
-
/* @__PURE__ */ jsx("p", {
|
|
50
|
-
className: "text-sm mb-2",
|
|
51
|
-
children: this.state.error?.message || "Unknown error occurred"
|
|
52
|
-
}),
|
|
53
|
-
/* @__PURE__ */ jsxs("details", {
|
|
54
|
-
className: "text-xs",
|
|
55
|
-
children: [/* @__PURE__ */ jsx("summary", {
|
|
56
|
-
className: "cursor-pointer font-medium",
|
|
57
|
-
children: "Stack Trace"
|
|
58
|
-
}), /* @__PURE__ */ jsx("pre", {
|
|
59
|
-
className: "mt-2 p-2 bg-red-100 rounded overflow-auto whitespace-pre-wrap",
|
|
60
|
-
children: this.state.error?.stack
|
|
61
|
-
})]
|
|
62
|
-
})
|
|
63
|
-
]
|
|
64
|
-
});
|
|
65
|
-
return this.props.children;
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
const BaseMDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = false, imageDialog, disableImageSettingsButton = false, onImageShortcut, imageButtonRef, jsxComponentDescriptors, className }, ref) => {
|
|
290
|
+
/**
|
|
291
|
+
* Restore the persisted focus-mode preference on first import. Runs once at
|
|
292
|
+
* module load so the class is in place before any editor mounts — avoids a
|
|
293
|
+
* flash of wide column when the user previously selected focus mode.
|
|
294
|
+
*/
|
|
295
|
+
if (typeof window !== "undefined") try {
|
|
296
|
+
if (window.localStorage.getItem(FOCUS_MODE_STORAGE_KEY) === "1") document.body.classList.add(FOCUS_MODE_BODY_CLASS);
|
|
297
|
+
} catch {}
|
|
298
|
+
const BaseMDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = false, imageDialog, disableImageSettingsButton = false, onImageShortcut, imageButtonRef, imageUploadHandler, jsxComponentDescriptors, extraPlugins, extraToolbarItems, className, placeholder, autoFocus }, ref) => {
|
|
69
299
|
useEffect(() => {
|
|
70
300
|
const handleKeyDown = (event) => {
|
|
71
301
|
if ((event.ctrlKey || event.metaKey) && event.key === "s") {
|
|
@@ -88,10 +318,16 @@ const BaseMDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = fa
|
|
|
88
318
|
return /* @__PURE__ */ jsx(MDXEditor$1, {
|
|
89
319
|
ref,
|
|
90
320
|
markdown: value,
|
|
321
|
+
toMarkdownOptions: TO_MARKDOWN_OPTIONS,
|
|
91
322
|
onChange,
|
|
323
|
+
onError: ({ error, source }) => {
|
|
324
|
+
console.error("[MDXEditor] parse error:", error, "\nsource:", source);
|
|
325
|
+
},
|
|
92
326
|
readOnly,
|
|
93
327
|
className,
|
|
94
|
-
|
|
328
|
+
placeholder,
|
|
329
|
+
autoFocus,
|
|
330
|
+
contentEditableClassName: "conloca-prose conloca-prose--editor",
|
|
95
331
|
plugins: [
|
|
96
332
|
headingsPlugin(),
|
|
97
333
|
listsPlugin(),
|
|
@@ -100,192 +336,53 @@ const BaseMDXEditor = React.forwardRef(({ value, onChange, onSave, readOnly = fa
|
|
|
100
336
|
linkDialogPlugin(),
|
|
101
337
|
tablePlugin(),
|
|
102
338
|
thematicBreakPlugin(),
|
|
103
|
-
|
|
104
|
-
|
|
339
|
+
codeBlockPlugin({
|
|
340
|
+
defaultCodeBlockLanguage: "ts",
|
|
341
|
+
codeBlockEditorDescriptors: [conlocaCodeBlockDescriptor]
|
|
342
|
+
}),
|
|
105
343
|
codeMirrorPlugin({ codeBlockLanguages }),
|
|
106
344
|
diffSourcePlugin({ viewMode: "rich-text" }),
|
|
107
345
|
markdownShortcutPlugin(),
|
|
108
|
-
jsxPlugin({ jsxComponentDescriptors: jsxComponentDescriptors ?? [] }),
|
|
109
|
-
imagePlugin(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
/* @__PURE__ */ jsx(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
/* @__PURE__ */
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
346
|
+
jsxPlugin({ jsxComponentDescriptors: [...jsxComponentDescriptors ?? [], ...wildcardJsxDescriptors] }),
|
|
347
|
+
imagePlugin({
|
|
348
|
+
...imageDialog ? {
|
|
349
|
+
ImageDialog: imageDialog,
|
|
350
|
+
disableImageSettingsButton
|
|
351
|
+
} : {},
|
|
352
|
+
...imageUploadHandler ? { imageUploadHandler } : {}
|
|
353
|
+
}),
|
|
354
|
+
...extraPlugins ?? [],
|
|
355
|
+
toolbarPlugin({
|
|
356
|
+
toolbarClassName: "mdxeditor-toolbar-wrap",
|
|
357
|
+
toolbarContents: () => /* @__PURE__ */ jsx(DiffSourceToggleWrapper, { children: /* @__PURE__ */ jsx(ConditionalContents, { options: [{
|
|
358
|
+
when: (editor) => editor?.editorType === "codeblock",
|
|
359
|
+
contents: () => /* @__PURE__ */ jsx(ChangeCodeMirrorLanguage, {})
|
|
360
|
+
}, { fallback: () => /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
361
|
+
/* @__PURE__ */ jsx(UndoRedo, {}),
|
|
362
|
+
/* @__PURE__ */ jsx(Separator, {}),
|
|
363
|
+
/* @__PURE__ */ jsx(BoldItalicUnderlineToggles, {}),
|
|
364
|
+
/* @__PURE__ */ jsx(StrikeThroughSupSubToggles, {}),
|
|
365
|
+
/* @__PURE__ */ jsx(HighlightToggle, {}),
|
|
366
|
+
/* @__PURE__ */ jsx(Separator, {}),
|
|
367
|
+
/* @__PURE__ */ jsx(ListsToggle, {}),
|
|
368
|
+
/* @__PURE__ */ jsx(Separator, {}),
|
|
369
|
+
/* @__PURE__ */ jsx(BlockTypeSelect, {}),
|
|
370
|
+
/* @__PURE__ */ jsx(Separator, {}),
|
|
371
|
+
/* @__PURE__ */ jsx(CreateLink, {}),
|
|
372
|
+
renderInsertImageButton(imageButtonRef),
|
|
373
|
+
/* @__PURE__ */ jsx(InsertTable, {}),
|
|
374
|
+
/* @__PURE__ */ jsx(InsertThematicBreak, {}),
|
|
375
|
+
/* @__PURE__ */ jsx(Separator, {}),
|
|
376
|
+
/* @__PURE__ */ jsx(InsertCodeBlock, {}),
|
|
377
|
+
extraToolbarItems,
|
|
378
|
+
/* @__PURE__ */ jsx(Separator, {}),
|
|
379
|
+
/* @__PURE__ */ jsx(FocusModeToggle, {})
|
|
380
|
+
] }) }] }) })
|
|
381
|
+
})
|
|
129
382
|
]
|
|
130
383
|
});
|
|
131
384
|
});
|
|
132
385
|
BaseMDXEditor.displayName = "BaseMDXEditor";
|
|
133
|
-
function DeferredMDXEditor({ EditorComponent, editorRef, ...props }) {
|
|
134
|
-
const [isReady, setIsReady] = useState(false);
|
|
135
|
-
useEffect(() => {
|
|
136
|
-
const timer = setTimeout(() => {
|
|
137
|
-
setIsReady(true);
|
|
138
|
-
}, 10);
|
|
139
|
-
return () => clearTimeout(timer);
|
|
140
|
-
}, []);
|
|
141
|
-
if (!isReady) return /* @__PURE__ */ jsx("div", {
|
|
142
|
-
className: "flex items-center justify-center h-full text-gray-500",
|
|
143
|
-
"data-testid": "deferred-editor-loading",
|
|
144
|
-
children: "Initializing editor..."
|
|
145
|
-
});
|
|
146
|
-
return /* @__PURE__ */ jsx("div", {
|
|
147
|
-
"data-testid": "deferred-editor-ready",
|
|
148
|
-
children: /* @__PURE__ */ jsx(EditorComponent, {
|
|
149
|
-
ref: editorRef,
|
|
150
|
-
...props
|
|
151
|
-
})
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
function Dialog({ open, onOpenChange, children }) {
|
|
155
|
-
if (!open) return null;
|
|
156
|
-
return /* @__PURE__ */ jsxs("div", {
|
|
157
|
-
className: "fixed inset-0 z-50 flex items-center justify-center",
|
|
158
|
-
children: [/* @__PURE__ */ jsx("div", {
|
|
159
|
-
role: "presentation",
|
|
160
|
-
className: "fixed inset-0 bg-black/50",
|
|
161
|
-
onClick: () => onOpenChange(false)
|
|
162
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
163
|
-
className: "relative z-50 bg-white dark:bg-grey-03 rounded-lg shadow-lg max-w-[90vw] max-h-[90vh] overflow-hidden",
|
|
164
|
-
children
|
|
165
|
-
})]
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
function DialogContent({ children, className = "" }) {
|
|
169
|
-
return /* @__PURE__ */ jsx("div", {
|
|
170
|
-
className: `p-6 ${className}`,
|
|
171
|
-
children
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
function DialogHeader({ children }) {
|
|
175
|
-
return /* @__PURE__ */ jsx("div", {
|
|
176
|
-
className: "mb-4",
|
|
177
|
-
children
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
function DialogTitle({ children }) {
|
|
181
|
-
return /* @__PURE__ */ jsx("h2", {
|
|
182
|
-
className: "text-xl font-semibold text-grey-01 dark:text-grey-12",
|
|
183
|
-
children
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
function BaseMDXEditorModal({ isOpen, onClose, filePath, initialContent, onSave, headerExtra, headerTools, onBeforeClose, EditorComponent, editorClassName }) {
|
|
187
|
-
const editorRef = React.useRef(null);
|
|
188
|
-
const [content, setContent] = useState(initialContent);
|
|
189
|
-
const [isEditorReady, setIsEditorReady] = useState(false);
|
|
190
|
-
const [isSaving, setIsSaving] = useState(false);
|
|
191
|
-
useEffect(() => {
|
|
192
|
-
setContent(initialContent);
|
|
193
|
-
}, [initialContent]);
|
|
194
|
-
const handleClose = () => {
|
|
195
|
-
if (onBeforeClose && !onBeforeClose()) return;
|
|
196
|
-
onClose();
|
|
197
|
-
};
|
|
198
|
-
useEffect(() => {
|
|
199
|
-
if (isOpen) {
|
|
200
|
-
const timer = setTimeout(() => {
|
|
201
|
-
setIsEditorReady(true);
|
|
202
|
-
}, 0);
|
|
203
|
-
return () => clearTimeout(timer);
|
|
204
|
-
}
|
|
205
|
-
setIsEditorReady(false);
|
|
206
|
-
}, [isOpen]);
|
|
207
|
-
const handleSave = async () => {
|
|
208
|
-
setIsSaving(true);
|
|
209
|
-
try {
|
|
210
|
-
await onSave(content);
|
|
211
|
-
handleClose();
|
|
212
|
-
} catch (error) {
|
|
213
|
-
console.error("Save failed:", error);
|
|
214
|
-
} finally {
|
|
215
|
-
setIsSaving(false);
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
if (!isOpen) return null;
|
|
219
|
-
return /* @__PURE__ */ jsx(Dialog, {
|
|
220
|
-
open: isOpen,
|
|
221
|
-
onOpenChange: handleClose,
|
|
222
|
-
children: /* @__PURE__ */ jsxs(DialogContent, {
|
|
223
|
-
className: "w-[90vw] h-[90vh] flex flex-col",
|
|
224
|
-
"data-testid": "mdx-editor-modal",
|
|
225
|
-
children: [
|
|
226
|
-
/* @__PURE__ */ jsx(DialogHeader, { children: /* @__PURE__ */ jsxs("div", {
|
|
227
|
-
className: "flex items-center justify-between gap-4",
|
|
228
|
-
children: [
|
|
229
|
-
/* @__PURE__ */ jsx(DialogTitle, { children: filePath ? `Edit: ${filePath}` : "New MDX File" }),
|
|
230
|
-
(headerExtra || headerTools) && /* @__PURE__ */ jsxs("div", {
|
|
231
|
-
className: "flex items-center gap-2",
|
|
232
|
-
children: [headerExtra, headerTools?.({
|
|
233
|
-
content,
|
|
234
|
-
setContent,
|
|
235
|
-
editorRef
|
|
236
|
-
})]
|
|
237
|
-
}),
|
|
238
|
-
/* @__PURE__ */ jsx("button", {
|
|
239
|
-
type: "button",
|
|
240
|
-
onClick: handleClose,
|
|
241
|
-
className: "p-2 rounded text-grey-04 dark:text-grey-07 hover:bg-gray-100 dark:hover:bg-grey-04 hover:text-grey-01 dark:hover:text-grey-12",
|
|
242
|
-
"aria-label": "Close",
|
|
243
|
-
children: "✕"
|
|
244
|
-
})
|
|
245
|
-
]
|
|
246
|
-
}) }),
|
|
247
|
-
/* @__PURE__ */ jsx("div", {
|
|
248
|
-
className: "flex-1 overflow-hidden",
|
|
249
|
-
children: isEditorReady ? /* @__PURE__ */ jsx(MDXEditorErrorBoundary, {
|
|
250
|
-
logPrefix: "[MDXEditor]",
|
|
251
|
-
children: /* @__PURE__ */ jsx(DeferredMDXEditor, {
|
|
252
|
-
EditorComponent,
|
|
253
|
-
editorRef,
|
|
254
|
-
value: content,
|
|
255
|
-
onChange: setContent,
|
|
256
|
-
onSave: handleSave,
|
|
257
|
-
className: editorClassName
|
|
258
|
-
})
|
|
259
|
-
}) : /* @__PURE__ */ jsx("div", {
|
|
260
|
-
className: "flex items-center justify-center h-full",
|
|
261
|
-
children: /* @__PURE__ */ jsx("div", {
|
|
262
|
-
className: "text-gray-500 dark:text-grey-07",
|
|
263
|
-
"data-testid": "modal-editor-loading",
|
|
264
|
-
children: "Loading editor..."
|
|
265
|
-
})
|
|
266
|
-
})
|
|
267
|
-
}),
|
|
268
|
-
/* @__PURE__ */ jsxs("div", {
|
|
269
|
-
className: "flex justify-end gap-2 mt-4",
|
|
270
|
-
children: [/* @__PURE__ */ jsx("button", {
|
|
271
|
-
type: "button",
|
|
272
|
-
onClick: handleClose,
|
|
273
|
-
className: "px-4 py-2 border border-gray-300 dark:border-grey-04 rounded text-grey-01 dark:text-grey-12 hover:bg-gray-50 dark:hover:bg-grey-04",
|
|
274
|
-
disabled: isSaving,
|
|
275
|
-
children: "Cancel"
|
|
276
|
-
}), /* @__PURE__ */ jsx("button", {
|
|
277
|
-
type: "button",
|
|
278
|
-
onClick: handleSave,
|
|
279
|
-
className: "px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed",
|
|
280
|
-
disabled: isSaving,
|
|
281
|
-
children: isSaving ? "Saving..." : "Save"
|
|
282
|
-
})]
|
|
283
|
-
})
|
|
284
|
-
]
|
|
285
|
-
})
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
|
|
289
386
|
//#endregion
|
|
290
387
|
//#region src/hooks/useMDXEvaluation.ts
|
|
291
388
|
const compiledCache = /* @__PURE__ */ new Map();
|
|
@@ -381,7 +478,8 @@ function useMDXEvaluation({ compiledCode, cacheKey }) {
|
|
|
381
478
|
setComponent(() => MDXComponent);
|
|
382
479
|
} catch (err) {
|
|
383
480
|
if (cancelled) return;
|
|
384
|
-
|
|
481
|
+
const evaluationError = err instanceof Error ? err : /* @__PURE__ */ new Error("MDX evaluation failed");
|
|
482
|
+
setError(evaluationError);
|
|
385
483
|
setComponent(null);
|
|
386
484
|
} finally {
|
|
387
485
|
if (!cancelled) setIsLoading(false);
|
|
@@ -403,7 +501,6 @@ function useMDXEvaluation({ compiledCode, cacheKey }) {
|
|
|
403
501
|
retry
|
|
404
502
|
};
|
|
405
503
|
}
|
|
406
|
-
|
|
407
504
|
//#endregion
|
|
408
505
|
//#region src/mdx-editor.tsx
|
|
409
506
|
const MDXEditor = React.forwardRef((props, ref) => {
|
|
@@ -413,13 +510,7 @@ const MDXEditor = React.forwardRef((props, ref) => {
|
|
|
413
510
|
});
|
|
414
511
|
});
|
|
415
512
|
MDXEditor.displayName = "MDXEditor";
|
|
416
|
-
function MDXEditorModal(props) {
|
|
417
|
-
return /* @__PURE__ */ jsx(BaseMDXEditorModal, {
|
|
418
|
-
...props,
|
|
419
|
-
EditorComponent: MDXEditor
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
|
|
423
513
|
//#endregion
|
|
424
|
-
export { BaseMDXEditor,
|
|
514
|
+
export { BaseMDXEditor, MDXEditor, codeBlockWrapperInfo$, useMDXEvaluation };
|
|
515
|
+
|
|
425
516
|
//# sourceMappingURL=index.mjs.map
|