@haklex/rich-renderer-codeblock 0.0.81 → 0.0.83
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/dist/CodeBlockRenderer-DSXP75Xm.js +125 -0
- package/dist/constants.mjs +70 -67
- package/dist/icons.mjs +91 -2
- package/dist/index.mjs +270 -238
- package/dist/language-UrxzhGSS.js +154 -0
- package/dist/rich-renderer-codeblock.css +1 -2
- package/dist/shiki.mjs +28 -19
- package/dist/static.mjs +4 -3
- package/package.json +5 -5
- package/dist/CodeBlockRenderer-ECu_hR7l.js +0 -134
- package/dist/icons-CnFW5y6g.js +0 -230
package/dist/index.mjs
CHANGED
|
@@ -1,246 +1,278 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { defaultKeymap, history, historyKeymap, indentWithTab } from "@codemirror/commands";
|
|
5
|
-
import { Compartment, EditorSelection, EditorState, Prec } from "@codemirror/state";
|
|
1
|
+
import { jsxs, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { history, defaultKeymap, historyKeymap, indentWithTab } from "@codemirror/commands";
|
|
3
|
+
import { Compartment, Prec, EditorState, EditorSelection } from "@codemirror/state";
|
|
6
4
|
import { EditorView, keymap, lineNumbers } from "@codemirror/view";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { useCallback,
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
5
|
+
import { isOnFirstLine, isOnLastLine, getThemeExtensions, loadLanguageExtension } from "@haklex/cm-editor";
|
|
6
|
+
import { useVariant, useColorScheme } from "@haklex/rich-editor";
|
|
7
|
+
import { useCallback, useMemo, useState, useRef, useEffect } from "react";
|
|
8
|
+
import { a as CodeBlockCard } from "./CodeBlockRenderer-DSXP75Xm.js";
|
|
9
|
+
import { C } from "./CodeBlockRenderer-DSXP75Xm.js";
|
|
10
|
+
import { normalizeLanguage } from "./constants.mjs";
|
|
11
|
+
import { Combobox, ComboboxInput, ComboboxContent, ComboboxEmpty, ComboboxList, ComboboxItem } from "@haklex/rich-editor-ui";
|
|
12
12
|
import { bundledLanguagesInfo } from "shiki/bundle/web";
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
import "@iconify/utils";
|
|
14
|
+
import "@iconify-json/material-icon-theme";
|
|
15
|
+
import { l as lang, s as semanticClassNames, h as hasLanguageIcon, a as LanguageIcon, b as langInput, c as lined, d as linedWithNumbers, e as body, f as bodyReadonly } from "./language-UrxzhGSS.js";
|
|
16
|
+
const languageItems = bundledLanguagesInfo.map((info) => ({
|
|
17
|
+
id: info.id,
|
|
18
|
+
name: info.name
|
|
17
19
|
}));
|
|
18
20
|
function LanguageCombobox({ language, onLanguageChange }) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
21
|
+
const normalizedLanguage = normalizeLanguage(language);
|
|
22
|
+
const handleValueChange = useCallback(
|
|
23
|
+
(value) => {
|
|
24
|
+
if (value) {
|
|
25
|
+
onLanguageChange?.(value.id);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
[onLanguageChange]
|
|
29
|
+
);
|
|
30
|
+
const selectedValue = useMemo(
|
|
31
|
+
() => languageItems.find((item) => item.id === normalizedLanguage) ?? null,
|
|
32
|
+
[normalizedLanguage]
|
|
33
|
+
);
|
|
34
|
+
return /* @__PURE__ */ jsxs("div", { className: `${lang} ${semanticClassNames.lang}`, children: [
|
|
35
|
+
hasLanguageIcon(normalizedLanguage) && /* @__PURE__ */ jsx(LanguageIcon, { language: normalizedLanguage, size: 14 }),
|
|
36
|
+
/* @__PURE__ */ jsxs(
|
|
37
|
+
Combobox,
|
|
38
|
+
{
|
|
39
|
+
itemToStringLabel: (item) => item.id,
|
|
40
|
+
itemToStringValue: (item) => item.id,
|
|
41
|
+
items: languageItems,
|
|
42
|
+
value: selectedValue,
|
|
43
|
+
onValueChange: handleValueChange,
|
|
44
|
+
children: [
|
|
45
|
+
/* @__PURE__ */ jsx(
|
|
46
|
+
ComboboxInput,
|
|
47
|
+
{
|
|
48
|
+
className: langInput,
|
|
49
|
+
placeholder: "language",
|
|
50
|
+
onKeyDown: (e) => {
|
|
51
|
+
e.stopPropagation();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
),
|
|
55
|
+
/* @__PURE__ */ jsxs(ComboboxContent, { align: "end", side: "top", sideOffset: 8, children: [
|
|
56
|
+
/* @__PURE__ */ jsx(ComboboxEmpty, { children: "No languages found." }),
|
|
57
|
+
/* @__PURE__ */ jsx(ComboboxList, { children: (item) => /* @__PURE__ */ jsxs(ComboboxItem, { value: item, children: [
|
|
58
|
+
/* @__PURE__ */ jsx(LanguageIcon, { language: item.id, size: 16 }),
|
|
59
|
+
item.name
|
|
60
|
+
] }, item.id) })
|
|
61
|
+
] })
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
)
|
|
65
|
+
] });
|
|
55
66
|
}
|
|
56
|
-
//#endregion
|
|
57
|
-
//#region src/CodeBlockEditRenderer.tsx
|
|
58
67
|
function stopHandledEvent(event) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
68
|
+
event.preventDefault();
|
|
69
|
+
event.stopPropagation();
|
|
70
|
+
event.stopImmediatePropagation();
|
|
62
71
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
72
|
+
const CodeBlockEditRenderer = ({
|
|
73
|
+
code,
|
|
74
|
+
language,
|
|
75
|
+
showLineNumbers: showLineNumbersProp,
|
|
76
|
+
editable = false,
|
|
77
|
+
selected = false,
|
|
78
|
+
cursorPlacement = "start",
|
|
79
|
+
onCodeChange,
|
|
80
|
+
onLanguageChange,
|
|
81
|
+
onDelete,
|
|
82
|
+
onExitBlock
|
|
83
|
+
}) => {
|
|
84
|
+
const variant = useVariant();
|
|
85
|
+
const colorScheme = useColorScheme();
|
|
86
|
+
const showLineNumbers = showLineNumbersProp ?? variant !== "comment";
|
|
87
|
+
const normalizedLanguage = normalizeLanguage(language);
|
|
88
|
+
const [mounted, setMounted] = useState(false);
|
|
89
|
+
const containerRef = useRef(null);
|
|
90
|
+
const editorRef = useRef(null);
|
|
91
|
+
const suppressChangeRef = useRef(false);
|
|
92
|
+
const editableRef = useRef(editable);
|
|
93
|
+
editableRef.current = editable;
|
|
94
|
+
const onCodeChangeRef = useRef(onCodeChange);
|
|
95
|
+
onCodeChangeRef.current = onCodeChange;
|
|
96
|
+
const onDeleteRef = useRef(onDelete);
|
|
97
|
+
onDeleteRef.current = onDelete;
|
|
98
|
+
const onExitBlockRef = useRef(onExitBlock);
|
|
99
|
+
onExitBlockRef.current = onExitBlock;
|
|
100
|
+
const languageCompartmentRef = useRef(null);
|
|
101
|
+
const editableCompartmentRef = useRef(null);
|
|
102
|
+
const lineNumbersCompartmentRef = useRef(null);
|
|
103
|
+
const themeCompartmentRef = useRef(null);
|
|
104
|
+
if (!languageCompartmentRef.current) languageCompartmentRef.current = new Compartment();
|
|
105
|
+
if (!editableCompartmentRef.current) editableCompartmentRef.current = new Compartment();
|
|
106
|
+
if (!lineNumbersCompartmentRef.current) lineNumbersCompartmentRef.current = new Compartment();
|
|
107
|
+
if (!themeCompartmentRef.current) themeCompartmentRef.current = new Compartment();
|
|
108
|
+
const keyboardBoundaryHandler = useMemo(
|
|
109
|
+
() => Prec.high(
|
|
110
|
+
EditorView.domEventHandlers({
|
|
111
|
+
keydown: (event, view) => {
|
|
112
|
+
if (!editableRef.current) return false;
|
|
113
|
+
event.stopPropagation();
|
|
114
|
+
if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
|
|
115
|
+
stopHandledEvent(event);
|
|
116
|
+
onExitBlockRef.current?.("after");
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
if (event.key === "Backspace" && view.state.doc.length === 0) {
|
|
120
|
+
stopHandledEvent(event);
|
|
121
|
+
onDeleteRef.current?.();
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
if (event.key === "ArrowUp" && isOnFirstLine(view)) {
|
|
125
|
+
stopHandledEvent(event);
|
|
126
|
+
onExitBlockRef.current?.("before");
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
if (event.key === "ArrowDown" && isOnLastLine(view)) {
|
|
130
|
+
stopHandledEvent(event);
|
|
131
|
+
onExitBlockRef.current?.("after");
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
),
|
|
138
|
+
[]
|
|
139
|
+
);
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
const container = containerRef.current;
|
|
142
|
+
if (!container) return;
|
|
143
|
+
const editor = new EditorView({
|
|
144
|
+
parent: container,
|
|
145
|
+
state: EditorState.create({
|
|
146
|
+
doc: code,
|
|
147
|
+
extensions: [
|
|
148
|
+
history(),
|
|
149
|
+
keymap.of([...defaultKeymap, ...historyKeymap, indentWithTab]),
|
|
150
|
+
EditorView.updateListener.of((update) => {
|
|
151
|
+
if (!update.docChanged || suppressChangeRef.current) return;
|
|
152
|
+
onCodeChangeRef.current?.(update.state.doc.toString());
|
|
153
|
+
}),
|
|
154
|
+
keyboardBoundaryHandler,
|
|
155
|
+
editableCompartmentRef.current.of([
|
|
156
|
+
EditorView.editable.of(editable),
|
|
157
|
+
EditorState.readOnly.of(!editable)
|
|
158
|
+
]),
|
|
159
|
+
lineNumbersCompartmentRef.current.of(showLineNumbers ? lineNumbers() : []),
|
|
160
|
+
themeCompartmentRef.current.of(getThemeExtensions(colorScheme)),
|
|
161
|
+
languageCompartmentRef.current.of([])
|
|
162
|
+
]
|
|
163
|
+
})
|
|
164
|
+
});
|
|
165
|
+
editorRef.current = editor;
|
|
166
|
+
setMounted(true);
|
|
167
|
+
return () => {
|
|
168
|
+
editor.destroy();
|
|
169
|
+
editorRef.current = null;
|
|
170
|
+
setMounted(false);
|
|
171
|
+
};
|
|
172
|
+
}, []);
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
const editor = editorRef.current;
|
|
175
|
+
if (!editor) return;
|
|
176
|
+
editor.dispatch({
|
|
177
|
+
effects: editableCompartmentRef.current.reconfigure([
|
|
178
|
+
EditorView.editable.of(editable),
|
|
179
|
+
EditorState.readOnly.of(!editable)
|
|
180
|
+
])
|
|
181
|
+
});
|
|
182
|
+
}, [editable]);
|
|
183
|
+
useEffect(() => {
|
|
184
|
+
const editor = editorRef.current;
|
|
185
|
+
if (!editor) return;
|
|
186
|
+
editor.dispatch({
|
|
187
|
+
effects: lineNumbersCompartmentRef.current.reconfigure(showLineNumbers ? lineNumbers() : [])
|
|
188
|
+
});
|
|
189
|
+
}, [showLineNumbers]);
|
|
190
|
+
useEffect(() => {
|
|
191
|
+
const editor = editorRef.current;
|
|
192
|
+
if (!editor) return;
|
|
193
|
+
editor.dispatch({
|
|
194
|
+
effects: themeCompartmentRef.current.reconfigure(getThemeExtensions(colorScheme))
|
|
195
|
+
});
|
|
196
|
+
}, [colorScheme]);
|
|
197
|
+
useEffect(() => {
|
|
198
|
+
const editor = editorRef.current;
|
|
199
|
+
if (!editor) return;
|
|
200
|
+
const current = editor.state.doc.toString();
|
|
201
|
+
if (current === code) return;
|
|
202
|
+
suppressChangeRef.current = true;
|
|
203
|
+
editor.dispatch({
|
|
204
|
+
changes: {
|
|
205
|
+
from: 0,
|
|
206
|
+
to: current.length,
|
|
207
|
+
insert: code
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
suppressChangeRef.current = false;
|
|
211
|
+
}, [code]);
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
const editor = editorRef.current;
|
|
214
|
+
if (!editor) return;
|
|
215
|
+
let cancelled = false;
|
|
216
|
+
(async () => {
|
|
217
|
+
const extension = await loadLanguageExtension(normalizedLanguage);
|
|
218
|
+
if (cancelled) return;
|
|
219
|
+
editor.dispatch({
|
|
220
|
+
effects: languageCompartmentRef.current.reconfigure(extension)
|
|
221
|
+
});
|
|
222
|
+
})();
|
|
223
|
+
return () => {
|
|
224
|
+
cancelled = true;
|
|
225
|
+
};
|
|
226
|
+
}, [normalizedLanguage]);
|
|
227
|
+
useEffect(() => {
|
|
228
|
+
if (!editable || !selected) return;
|
|
229
|
+
const editor = editorRef.current;
|
|
230
|
+
if (!editor) return;
|
|
231
|
+
const raf = requestAnimationFrame(() => {
|
|
232
|
+
const nextCursor = cursorPlacement === "end" ? editor.state.doc.length : 0;
|
|
233
|
+
editor.focus();
|
|
234
|
+
editor.dispatch({
|
|
235
|
+
selection: EditorSelection.cursor(nextCursor),
|
|
236
|
+
scrollIntoView: true
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
return () => cancelAnimationFrame(raf);
|
|
240
|
+
}, [cursorPlacement, editable, mounted, selected]);
|
|
241
|
+
const fallbackLines = useMemo(() => code.split("\n"), [code]);
|
|
242
|
+
const fallbackClassName = [
|
|
243
|
+
showLineNumbers && lined,
|
|
244
|
+
showLineNumbers && semanticClassNames.lined,
|
|
245
|
+
showLineNumbers && linedWithNumbers,
|
|
246
|
+
showLineNumbers && semanticClassNames.linedWithNumbers
|
|
247
|
+
].filter(Boolean).join(" ");
|
|
248
|
+
const bodyClassName = [
|
|
249
|
+
body,
|
|
250
|
+
semanticClassNames.body,
|
|
251
|
+
!editable && bodyReadonly,
|
|
252
|
+
!editable && semanticClassNames.bodyReadonly
|
|
253
|
+
].filter(Boolean).join(" ");
|
|
254
|
+
return /* @__PURE__ */ jsxs(
|
|
255
|
+
CodeBlockCard,
|
|
256
|
+
{
|
|
257
|
+
code,
|
|
258
|
+
collapsible: !editable,
|
|
259
|
+
language,
|
|
260
|
+
langSlot: editable ? /* @__PURE__ */ jsx(LanguageCombobox, { language, onLanguageChange }) : void 0,
|
|
261
|
+
children: [
|
|
262
|
+
!mounted && /* @__PURE__ */ jsx("pre", { className: fallbackClassName, children: /* @__PURE__ */ jsx("code", { children: fallbackLines.map((line, i) => /* @__PURE__ */ jsx("span", { className: "line", children: line }, `${line}-${i}`)) }) }),
|
|
263
|
+
/* @__PURE__ */ jsx(
|
|
264
|
+
"div",
|
|
265
|
+
{
|
|
266
|
+
className: bodyClassName,
|
|
267
|
+
ref: containerRef,
|
|
268
|
+
style: !mounted ? { height: 0, overflow: "hidden", visibility: "hidden" } : void 0
|
|
269
|
+
}
|
|
270
|
+
)
|
|
271
|
+
]
|
|
272
|
+
}
|
|
273
|
+
);
|
|
274
|
+
};
|
|
275
|
+
export {
|
|
276
|
+
CodeBlockEditRenderer,
|
|
277
|
+
C as CodeBlockRenderer
|
|
244
278
|
};
|
|
245
|
-
//#endregion
|
|
246
|
-
export { CodeBlockEditRenderer, CodeBlockRenderer };
|