@zhenryx/react-editor 0.1.1 → 0.1.3
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/index.cjs +413 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +413 -8
- package/dist/index.mjs.map +1 -1
- package/dist/style.css +243 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -76,7 +76,26 @@ function inlineToMd(node) {
|
|
|
76
76
|
}
|
|
77
77
|
case "IMG": {
|
|
78
78
|
const src = el.getAttribute("src") || "";
|
|
79
|
-
|
|
79
|
+
if (!src) return "";
|
|
80
|
+
const alt = el.getAttribute("alt") || "";
|
|
81
|
+
const width = Number(el.getAttribute("data-kb-w") || "");
|
|
82
|
+
const height = Number(el.getAttribute("data-kb-h") || "");
|
|
83
|
+
const align = el.getAttribute("data-kb-align") || "";
|
|
84
|
+
const caption = (el.getAttribute("data-kb-caption") || el.closest("figure")?.querySelector("figcaption")?.textContent || "").trim();
|
|
85
|
+
const tokens = [];
|
|
86
|
+
if (Number.isInteger(width) && width >= 20 && width <= 4e3) tokens.push(`kbw=${width}`);
|
|
87
|
+
if (Number.isInteger(height) && height > 0) tokens.push(`kbh=${height}`);
|
|
88
|
+
if (/^(left|center|right)$/.test(align)) tokens.push(`kba=${align}`);
|
|
89
|
+
if (caption) tokens.push(`kbc=${encodeURIComponent(caption)}`);
|
|
90
|
+
return `}"` : ""})`;
|
|
91
|
+
}
|
|
92
|
+
case "FIGURE": {
|
|
93
|
+
const img = el.querySelector("img");
|
|
94
|
+
return img ? `
|
|
95
|
+
|
|
96
|
+
${inlineToMd(img)}
|
|
97
|
+
|
|
98
|
+
` : "";
|
|
80
99
|
}
|
|
81
100
|
case "UL":
|
|
82
101
|
case "OL": return `\n\n${listToMd(el)}`;
|
|
@@ -146,7 +165,7 @@ function blockToMd(el) {
|
|
|
146
165
|
}
|
|
147
166
|
case "P":
|
|
148
167
|
case "DIV": {
|
|
149
|
-
if (Array.from(el.children).some((child) => /^(P|DIV|H[1-6]|UL|OL|LI|BLOCKQUOTE|PRE|TABLE|HR)$/.test(child.tagName))) return Array.from(el.childNodes).map(nodeToMd).join("");
|
|
168
|
+
if (Array.from(el.children).some((child) => /^(P|DIV|H[1-6]|UL|OL|LI|BLOCKQUOTE|PRE|TABLE|HR|FIGURE)$/.test(child.tagName))) return Array.from(el.childNodes).map(nodeToMd).join("");
|
|
150
169
|
const content = inlineToMd(el).trim();
|
|
151
170
|
const align = (el.style.textAlign || "").toLowerCase();
|
|
152
171
|
if (/^(left|center|right|justify)$/.test(align) && content) return `\n\n<span style="display: block; text-align: ${align}">${content}</span>\n\n`;
|
|
@@ -171,9 +190,10 @@ function nodeToMd(node) {
|
|
|
171
190
|
}
|
|
172
191
|
if (el.classList.contains("kb-code")) {
|
|
173
192
|
const pre = el.querySelector("pre");
|
|
174
|
-
|
|
193
|
+
const extra = Array.from(el.children).filter((child) => child.tagName !== "PRE" && !child.classList.contains("kb-code-lang")).map((child) => nodeToMd(child)).join("");
|
|
194
|
+
return (pre ? preToMd(pre, el.querySelector(".kb-code-lang")?.textContent?.trim() || "") : "") + extra;
|
|
175
195
|
}
|
|
176
|
-
if (/^(P|DIV|H[1-6]|UL|OL|BLOCKQUOTE|PRE|TABLE|HR)$/.test(el.tagName)) return blockToMd(el);
|
|
196
|
+
if (/^(P|DIV|H[1-6]|UL|OL|BLOCKQUOTE|PRE|TABLE|HR|FIGURE)$/.test(el.tagName)) return blockToMd(el);
|
|
177
197
|
return inlineToMd(el);
|
|
178
198
|
}
|
|
179
199
|
/** contentEditable 的 innerHTML → Markdown */
|
|
@@ -868,6 +888,9 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
868
888
|
const [busy, setBusy] = useState(false);
|
|
869
889
|
const [error, setError] = useState("");
|
|
870
890
|
const [popover, setPopover] = useState(null);
|
|
891
|
+
const selectedImageRef = useRef(null);
|
|
892
|
+
const [imageSel, setImageSel] = useState(null);
|
|
893
|
+
const [imageDialog, setImageDialog] = useState(null);
|
|
871
894
|
const [videoUrl, setVideoUrl] = useState("");
|
|
872
895
|
const [formulaText, setFormulaText] = useState("");
|
|
873
896
|
const [wide, setWide] = useState(false);
|
|
@@ -882,6 +905,9 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
882
905
|
if (lastMd.current === markdown) return;
|
|
883
906
|
el.innerHTML = renderMarkdown(markdown);
|
|
884
907
|
lastMd.current = markdown;
|
|
908
|
+
selectedImageRef.current = null;
|
|
909
|
+
setImageSel(null);
|
|
910
|
+
setImageDialog(null);
|
|
885
911
|
refreshHeadings();
|
|
886
912
|
}, [markdown]);
|
|
887
913
|
useEffect(() => {
|
|
@@ -902,6 +928,28 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
902
928
|
document.addEventListener("mousedown", onDown);
|
|
903
929
|
return () => document.removeEventListener("mousedown", onDown);
|
|
904
930
|
}, [popover]);
|
|
931
|
+
useEffect(() => {
|
|
932
|
+
if (!imageSel) return;
|
|
933
|
+
const onScroll = () => updateImageRect();
|
|
934
|
+
const onResize = () => updateImageRect();
|
|
935
|
+
window.addEventListener("scroll", onScroll, true);
|
|
936
|
+
window.addEventListener("resize", onResize);
|
|
937
|
+
return () => {
|
|
938
|
+
window.removeEventListener("scroll", onScroll, true);
|
|
939
|
+
window.removeEventListener("resize", onResize);
|
|
940
|
+
};
|
|
941
|
+
}, [imageSel]);
|
|
942
|
+
useEffect(() => {
|
|
943
|
+
if (!imageSel) return;
|
|
944
|
+
const onDown = (event) => {
|
|
945
|
+
const target = event.target;
|
|
946
|
+
if (target?.closest(".rre-img-tool") || target?.closest(".rre-img-dialog")) return;
|
|
947
|
+
if (target?.closest("img")) return;
|
|
948
|
+
clearImageSelection();
|
|
949
|
+
};
|
|
950
|
+
document.addEventListener("mousedown", onDown);
|
|
951
|
+
return () => document.removeEventListener("mousedown", onDown);
|
|
952
|
+
}, [imageSel]);
|
|
905
953
|
useEffect(() => {
|
|
906
954
|
if (!markdown.trim()) return;
|
|
907
955
|
const timer = window.setTimeout(() => {
|
|
@@ -1175,7 +1223,9 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
1175
1223
|
try {
|
|
1176
1224
|
const src = safeRichHref(await onUploadImage(file));
|
|
1177
1225
|
if (!src) throw new Error("图片地址不合法");
|
|
1178
|
-
|
|
1226
|
+
const contentWidth = editorRef.current?.clientWidth || 860;
|
|
1227
|
+
const defaultWidth = Math.max(120, Math.round(contentWidth * .75));
|
|
1228
|
+
insertHtml(`<img src="${src}" alt="${escapeHtml(file.name.replace(/\.[^.]+$/, ""))}" data-kb-w="${defaultWidth}" style="--kbw:${defaultWidth}px">`);
|
|
1179
1229
|
} catch (err) {
|
|
1180
1230
|
setError(err instanceof Error ? err.message : "图片上传失败");
|
|
1181
1231
|
} finally {
|
|
@@ -1186,6 +1236,120 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
1186
1236
|
const row = "<tr><td>内容</td><td>内容</td><td>内容</td></tr>";
|
|
1187
1237
|
insertHtml(`<table><thead><tr><th>列 1</th><th>列 2</th><th>列 3</th></tr></thead><tbody>${row}${row}</tbody></table><p><br></p>`);
|
|
1188
1238
|
}
|
|
1239
|
+
function clearImageSelection() {
|
|
1240
|
+
selectedImageRef.current = null;
|
|
1241
|
+
setImageSel(null);
|
|
1242
|
+
}
|
|
1243
|
+
function updateImageRect() {
|
|
1244
|
+
const img = selectedImageRef.current;
|
|
1245
|
+
if (!img) return;
|
|
1246
|
+
const rect = img.getBoundingClientRect();
|
|
1247
|
+
setImageSel({
|
|
1248
|
+
left: Math.round(rect.left),
|
|
1249
|
+
top: Math.round(rect.top),
|
|
1250
|
+
width: Math.round(rect.width),
|
|
1251
|
+
height: Math.round(rect.height)
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
function selectImage(img) {
|
|
1255
|
+
selectedImageRef.current = img;
|
|
1256
|
+
updateImageRect();
|
|
1257
|
+
}
|
|
1258
|
+
function calcImageHeight(widthPx) {
|
|
1259
|
+
const img = selectedImageRef.current;
|
|
1260
|
+
if (!img || !img.naturalWidth) return 0;
|
|
1261
|
+
return Math.max(1, Math.round(img.naturalHeight / img.naturalWidth * widthPx));
|
|
1262
|
+
}
|
|
1263
|
+
function startImageResize(event) {
|
|
1264
|
+
const img = selectedImageRef.current;
|
|
1265
|
+
const container = editorRef.current;
|
|
1266
|
+
if (!img || !container) return;
|
|
1267
|
+
event.preventDefault();
|
|
1268
|
+
event.stopPropagation();
|
|
1269
|
+
const startX = event.clientX;
|
|
1270
|
+
const startWidth = img.getBoundingClientRect().width;
|
|
1271
|
+
const containerWidth = container.clientWidth || 1;
|
|
1272
|
+
const onMove = (moveEvent) => {
|
|
1273
|
+
const next = Math.max(40, Math.min(containerWidth, Math.round(startWidth + moveEvent.clientX - startX)));
|
|
1274
|
+
img.setAttribute("data-kb-w", String(next));
|
|
1275
|
+
img.style.setProperty("--kbw", `${next}px`);
|
|
1276
|
+
updateImageRect();
|
|
1277
|
+
};
|
|
1278
|
+
const onUp = () => {
|
|
1279
|
+
document.removeEventListener("pointermove", onMove);
|
|
1280
|
+
document.removeEventListener("pointerup", onUp);
|
|
1281
|
+
sync();
|
|
1282
|
+
};
|
|
1283
|
+
document.addEventListener("pointermove", onMove);
|
|
1284
|
+
document.addEventListener("pointerup", onUp);
|
|
1285
|
+
}
|
|
1286
|
+
function openImageDialog() {
|
|
1287
|
+
const img = selectedImageRef.current;
|
|
1288
|
+
if (!img) return;
|
|
1289
|
+
const widthAttr = Number(img.getAttribute("data-kb-w") || "");
|
|
1290
|
+
const width = Number.isInteger(widthAttr) && widthAttr >= 20 ? widthAttr : Math.max(40, Math.round(img.getBoundingClientRect().width));
|
|
1291
|
+
const heightAttr = Number(img.getAttribute("data-kb-h") || "");
|
|
1292
|
+
const alignRaw = img.getAttribute("data-kb-align") || "";
|
|
1293
|
+
const align = /^(left|center|right)$/.test(alignRaw) ? alignRaw : "none";
|
|
1294
|
+
const caption = (img.getAttribute("data-kb-caption") || "").trim();
|
|
1295
|
+
setImageDialog({
|
|
1296
|
+
url: img.getAttribute("src") || "",
|
|
1297
|
+
alt: img.getAttribute("alt") || "",
|
|
1298
|
+
width,
|
|
1299
|
+
height: Number.isInteger(heightAttr) && heightAttr > 0 ? heightAttr : calcImageHeight(width),
|
|
1300
|
+
lock: !(Number.isInteger(heightAttr) && heightAttr > 0),
|
|
1301
|
+
align,
|
|
1302
|
+
caption: Boolean(caption),
|
|
1303
|
+
captionText: caption
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
function applyImageDialog() {
|
|
1307
|
+
const img = selectedImageRef.current;
|
|
1308
|
+
if (!img || !imageDialog) return;
|
|
1309
|
+
const src = safeRichHref(imageDialog.url.trim());
|
|
1310
|
+
if (!src) {
|
|
1311
|
+
setError("图片地址不合法");
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
const width = Math.max(20, Math.min(4e3, Number(imageDialog.width) || 20));
|
|
1315
|
+
const height = Math.max(0, Number(imageDialog.height) || 0);
|
|
1316
|
+
img.setAttribute("src", src);
|
|
1317
|
+
img.setAttribute("alt", imageDialog.alt);
|
|
1318
|
+
img.setAttribute("data-kb-w", String(width));
|
|
1319
|
+
img.style.setProperty("--kbw", `${width}px`);
|
|
1320
|
+
if (imageDialog.lock || height <= 0) {
|
|
1321
|
+
img.removeAttribute("data-kb-h");
|
|
1322
|
+
img.style.removeProperty("--kbh");
|
|
1323
|
+
} else {
|
|
1324
|
+
img.setAttribute("data-kb-h", String(height));
|
|
1325
|
+
img.style.setProperty("--kbh", `${height}px`);
|
|
1326
|
+
}
|
|
1327
|
+
if (imageDialog.align === "none") img.removeAttribute("data-kb-align");
|
|
1328
|
+
else img.setAttribute("data-kb-align", imageDialog.align);
|
|
1329
|
+
if (imageDialog.caption && imageDialog.captionText.trim()) img.setAttribute("data-kb-caption", imageDialog.captionText.trim());
|
|
1330
|
+
else img.removeAttribute("data-kb-caption");
|
|
1331
|
+
setImageDialog(null);
|
|
1332
|
+
clearImageSelection();
|
|
1333
|
+
sync();
|
|
1334
|
+
const el = editorRef.current;
|
|
1335
|
+
if (el) {
|
|
1336
|
+
const md = htmlToMarkdown(el.innerHTML);
|
|
1337
|
+
el.innerHTML = renderMarkdown(md);
|
|
1338
|
+
lastMd.current = md;
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
function resetImageSize() {
|
|
1342
|
+
const img = selectedImageRef.current;
|
|
1343
|
+
const containerWidth = editorRef.current?.clientWidth || 1;
|
|
1344
|
+
if (!img) return;
|
|
1345
|
+
const naturalWidth = Math.max(40, Math.min(containerWidth, img.naturalWidth || 640));
|
|
1346
|
+
setImageDialog((prev) => prev ? {
|
|
1347
|
+
...prev,
|
|
1348
|
+
width: naturalWidth,
|
|
1349
|
+
height: calcImageHeight(naturalWidth),
|
|
1350
|
+
lock: true
|
|
1351
|
+
} : prev);
|
|
1352
|
+
}
|
|
1189
1353
|
function toolButton(label, icon, onClick, opts = {}) {
|
|
1190
1354
|
return /* @__PURE__ */ jsxs("button", {
|
|
1191
1355
|
type: "button",
|
|
@@ -1212,7 +1376,10 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
1212
1376
|
event.preventDefault();
|
|
1213
1377
|
saveRange();
|
|
1214
1378
|
},
|
|
1215
|
-
onClick: (event) =>
|
|
1379
|
+
onClick: (event) => {
|
|
1380
|
+
if (popover?.kind === kind) setPopover(null);
|
|
1381
|
+
else openPopover(kind, event.currentTarget);
|
|
1382
|
+
},
|
|
1216
1383
|
children: [icon, /* @__PURE__ */ jsxs("span", {
|
|
1217
1384
|
className: withCaret ? "rre-popover-label" : void 0,
|
|
1218
1385
|
children: [label, withCaret ? /* @__PURE__ */ jsx("svg", {
|
|
@@ -1537,12 +1704,250 @@ function RichTextEditor({ markdown, onChange, onUploadImage, onSwitchToMarkdown,
|
|
|
1537
1704
|
"data-placeholder": placeholder,
|
|
1538
1705
|
onInput: sync,
|
|
1539
1706
|
onBlur: sync,
|
|
1540
|
-
onPaste
|
|
1707
|
+
onPaste,
|
|
1708
|
+
onClick: (event) => {
|
|
1709
|
+
const img = event.target?.closest("img");
|
|
1710
|
+
if (img && editorRef.current?.contains(img)) {
|
|
1711
|
+
event.preventDefault();
|
|
1712
|
+
selectImage(img);
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1541
1715
|
})
|
|
1542
1716
|
})
|
|
1543
1717
|
})
|
|
1544
1718
|
]
|
|
1545
1719
|
}),
|
|
1720
|
+
imageSel && selectedImageRef.current && !imageDialog ? /* @__PURE__ */ jsxs("div", {
|
|
1721
|
+
className: "rre-img-tool",
|
|
1722
|
+
style: {
|
|
1723
|
+
left: imageSel.left,
|
|
1724
|
+
top: imageSel.top,
|
|
1725
|
+
width: imageSel.width,
|
|
1726
|
+
height: imageSel.height
|
|
1727
|
+
},
|
|
1728
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
1729
|
+
type: "button",
|
|
1730
|
+
className: "rre-img-edit",
|
|
1731
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
1732
|
+
onClick: openImageDialog,
|
|
1733
|
+
children: "编辑"
|
|
1734
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
1735
|
+
className: "rre-img-handle",
|
|
1736
|
+
title: "点击并拖拽以改变尺寸",
|
|
1737
|
+
onPointerDown: startImageResize
|
|
1738
|
+
})]
|
|
1739
|
+
}) : null,
|
|
1740
|
+
imageDialog ? /* @__PURE__ */ jsx("div", {
|
|
1741
|
+
className: "rre-img-dialog-mask",
|
|
1742
|
+
onMouseDown: (event) => {
|
|
1743
|
+
if (event.target === event.currentTarget) setImageDialog(null);
|
|
1744
|
+
},
|
|
1745
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
1746
|
+
className: "rre-img-dialog",
|
|
1747
|
+
role: "dialog",
|
|
1748
|
+
"aria-modal": "true",
|
|
1749
|
+
"aria-label": "图像属性",
|
|
1750
|
+
children: [
|
|
1751
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1752
|
+
className: "rre-img-dialog-head",
|
|
1753
|
+
children: [/* @__PURE__ */ jsx("b", { children: "图像属性" }), /* @__PURE__ */ jsx("button", {
|
|
1754
|
+
type: "button",
|
|
1755
|
+
onClick: () => setImageDialog(null),
|
|
1756
|
+
"aria-label": "关闭",
|
|
1757
|
+
title: "关闭",
|
|
1758
|
+
children: /* @__PURE__ */ jsx("svg", {
|
|
1759
|
+
width: "14",
|
|
1760
|
+
height: "14",
|
|
1761
|
+
viewBox: "0 0 24 24",
|
|
1762
|
+
fill: "none",
|
|
1763
|
+
stroke: "currentColor",
|
|
1764
|
+
strokeWidth: "2",
|
|
1765
|
+
strokeLinecap: "round",
|
|
1766
|
+
"aria-hidden": "true",
|
|
1767
|
+
children: /* @__PURE__ */ jsx("path", { d: "m6 6 12 12M18 6 6 18" })
|
|
1768
|
+
})
|
|
1769
|
+
})]
|
|
1770
|
+
}),
|
|
1771
|
+
/* @__PURE__ */ jsxs("label", {
|
|
1772
|
+
className: "rre-img-field",
|
|
1773
|
+
children: [/* @__PURE__ */ jsx("span", { children: "图片 URL" }), /* @__PURE__ */ jsx("input", {
|
|
1774
|
+
value: imageDialog.url,
|
|
1775
|
+
onChange: (event) => setImageDialog({
|
|
1776
|
+
...imageDialog,
|
|
1777
|
+
url: event.target.value
|
|
1778
|
+
})
|
|
1779
|
+
})]
|
|
1780
|
+
}),
|
|
1781
|
+
/* @__PURE__ */ jsxs("label", {
|
|
1782
|
+
className: "rre-img-field",
|
|
1783
|
+
children: [/* @__PURE__ */ jsx("span", { children: "替换文本" }), /* @__PURE__ */ jsx("input", {
|
|
1784
|
+
value: imageDialog.alt,
|
|
1785
|
+
onChange: (event) => setImageDialog({
|
|
1786
|
+
...imageDialog,
|
|
1787
|
+
alt: event.target.value
|
|
1788
|
+
})
|
|
1789
|
+
})]
|
|
1790
|
+
}),
|
|
1791
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1792
|
+
className: "rre-img-size",
|
|
1793
|
+
children: [
|
|
1794
|
+
/* @__PURE__ */ jsxs("label", {
|
|
1795
|
+
className: "rre-img-field",
|
|
1796
|
+
children: [/* @__PURE__ */ jsx("span", { children: "宽度(px)" }), /* @__PURE__ */ jsx("input", {
|
|
1797
|
+
type: "number",
|
|
1798
|
+
min: 20,
|
|
1799
|
+
max: 4e3,
|
|
1800
|
+
value: imageDialog.width,
|
|
1801
|
+
onFocus: (event) => event.currentTarget.select(),
|
|
1802
|
+
onChange: (event) => {
|
|
1803
|
+
const raw = event.target.value;
|
|
1804
|
+
const width = raw === "" ? "" : Math.max(0, Math.min(4e3, Number(raw) || 0));
|
|
1805
|
+
setImageDialog({
|
|
1806
|
+
...imageDialog,
|
|
1807
|
+
width,
|
|
1808
|
+
height: imageDialog.lock ? width === "" || width < 20 ? "" : calcImageHeight(width) : imageDialog.height
|
|
1809
|
+
});
|
|
1810
|
+
},
|
|
1811
|
+
onBlur: () => {
|
|
1812
|
+
if (imageDialog.width !== "" && Number(imageDialog.width) < 20) {
|
|
1813
|
+
const width = 20;
|
|
1814
|
+
setImageDialog({
|
|
1815
|
+
...imageDialog,
|
|
1816
|
+
width,
|
|
1817
|
+
height: imageDialog.lock ? calcImageHeight(width) : imageDialog.height
|
|
1818
|
+
});
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
})]
|
|
1822
|
+
}),
|
|
1823
|
+
/* @__PURE__ */ jsxs("label", {
|
|
1824
|
+
className: "rre-img-field",
|
|
1825
|
+
children: [/* @__PURE__ */ jsx("span", { children: "高度(px)" }), /* @__PURE__ */ jsx("input", {
|
|
1826
|
+
type: "number",
|
|
1827
|
+
min: 1,
|
|
1828
|
+
value: imageDialog.height,
|
|
1829
|
+
readOnly: imageDialog.lock,
|
|
1830
|
+
onFocus: (event) => event.currentTarget.select(),
|
|
1831
|
+
onChange: (event) => {
|
|
1832
|
+
const raw = event.target.value;
|
|
1833
|
+
setImageDialog({
|
|
1834
|
+
...imageDialog,
|
|
1835
|
+
height: raw === "" ? "" : Math.max(0, Number(raw) || 0)
|
|
1836
|
+
});
|
|
1837
|
+
},
|
|
1838
|
+
onBlur: () => {
|
|
1839
|
+
if (imageDialog.height !== "" && Number(imageDialog.height) < 1) setImageDialog({
|
|
1840
|
+
...imageDialog,
|
|
1841
|
+
height: 1
|
|
1842
|
+
});
|
|
1843
|
+
}
|
|
1844
|
+
})]
|
|
1845
|
+
}),
|
|
1846
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1847
|
+
className: "rre-img-size-tools",
|
|
1848
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
1849
|
+
type: "button",
|
|
1850
|
+
className: imageDialog.lock ? "is-on" : "",
|
|
1851
|
+
title: imageDialog.lock ? "解锁比例" : "锁定比例",
|
|
1852
|
+
onClick: () => setImageDialog({
|
|
1853
|
+
...imageDialog,
|
|
1854
|
+
lock: !imageDialog.lock,
|
|
1855
|
+
height: !imageDialog.lock ? calcImageHeight(Number(imageDialog.width) || 20) : imageDialog.height
|
|
1856
|
+
}),
|
|
1857
|
+
children: /* @__PURE__ */ jsxs("svg", {
|
|
1858
|
+
width: "15",
|
|
1859
|
+
height: "15",
|
|
1860
|
+
viewBox: "0 0 24 24",
|
|
1861
|
+
fill: "none",
|
|
1862
|
+
stroke: "currentColor",
|
|
1863
|
+
strokeWidth: "1.8",
|
|
1864
|
+
strokeLinecap: "round",
|
|
1865
|
+
strokeLinejoin: "round",
|
|
1866
|
+
"aria-hidden": "true",
|
|
1867
|
+
children: [/* @__PURE__ */ jsx("rect", {
|
|
1868
|
+
x: "5",
|
|
1869
|
+
y: "10",
|
|
1870
|
+
width: "14",
|
|
1871
|
+
height: "10",
|
|
1872
|
+
rx: "2"
|
|
1873
|
+
}), /* @__PURE__ */ jsx("path", { d: "M8.5 10V7.5a3.5 3.5 0 0 1 7 0V10" })]
|
|
1874
|
+
})
|
|
1875
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
1876
|
+
type: "button",
|
|
1877
|
+
title: "恢复原始尺寸",
|
|
1878
|
+
onClick: resetImageSize,
|
|
1879
|
+
children: /* @__PURE__ */ jsxs("svg", {
|
|
1880
|
+
width: "15",
|
|
1881
|
+
height: "15",
|
|
1882
|
+
viewBox: "0 0 24 24",
|
|
1883
|
+
fill: "none",
|
|
1884
|
+
stroke: "currentColor",
|
|
1885
|
+
strokeWidth: "1.8",
|
|
1886
|
+
strokeLinecap: "round",
|
|
1887
|
+
strokeLinejoin: "round",
|
|
1888
|
+
"aria-hidden": "true",
|
|
1889
|
+
children: [/* @__PURE__ */ jsx("path", { d: "M20 12a8 8 0 1 1-2.3-5.7" }), /* @__PURE__ */ jsx("path", { d: "M20 4v5h-5" })]
|
|
1890
|
+
})
|
|
1891
|
+
})]
|
|
1892
|
+
})
|
|
1893
|
+
]
|
|
1894
|
+
}),
|
|
1895
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1896
|
+
className: "rre-img-align",
|
|
1897
|
+
children: [/* @__PURE__ */ jsx("span", { children: "对齐方式" }), [
|
|
1898
|
+
["none", "无"],
|
|
1899
|
+
["left", "左对齐"],
|
|
1900
|
+
["center", "居中"],
|
|
1901
|
+
["right", "右对齐"]
|
|
1902
|
+
].map(([value, label]) => /* @__PURE__ */ jsxs("label", { children: [/* @__PURE__ */ jsx("input", {
|
|
1903
|
+
type: "radio",
|
|
1904
|
+
name: "rre-img-align",
|
|
1905
|
+
checked: imageDialog.align === value,
|
|
1906
|
+
onChange: () => setImageDialog({
|
|
1907
|
+
...imageDialog,
|
|
1908
|
+
align: value
|
|
1909
|
+
})
|
|
1910
|
+
}), label] }, value))]
|
|
1911
|
+
}),
|
|
1912
|
+
/* @__PURE__ */ jsxs("label", {
|
|
1913
|
+
className: "rre-img-caption-toggle",
|
|
1914
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
1915
|
+
type: "checkbox",
|
|
1916
|
+
checked: imageDialog.caption,
|
|
1917
|
+
onChange: (event) => setImageDialog({
|
|
1918
|
+
...imageDialog,
|
|
1919
|
+
caption: event.target.checked
|
|
1920
|
+
})
|
|
1921
|
+
}), "带标题图像"]
|
|
1922
|
+
}),
|
|
1923
|
+
imageDialog.caption ? /* @__PURE__ */ jsxs("label", {
|
|
1924
|
+
className: "rre-img-field",
|
|
1925
|
+
children: [/* @__PURE__ */ jsx("span", { children: "图注" }), /* @__PURE__ */ jsx("input", {
|
|
1926
|
+
value: imageDialog.captionText,
|
|
1927
|
+
onChange: (event) => setImageDialog({
|
|
1928
|
+
...imageDialog,
|
|
1929
|
+
captionText: event.target.value
|
|
1930
|
+
}),
|
|
1931
|
+
placeholder: "图片下方的说明文字"
|
|
1932
|
+
})]
|
|
1933
|
+
}) : null,
|
|
1934
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1935
|
+
className: "rre-img-dialog-foot",
|
|
1936
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
1937
|
+
type: "button",
|
|
1938
|
+
className: "rre-btn",
|
|
1939
|
+
onClick: () => setImageDialog(null),
|
|
1940
|
+
children: "取消"
|
|
1941
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
1942
|
+
type: "button",
|
|
1943
|
+
className: "rre-btn rre-btn-primary",
|
|
1944
|
+
onClick: applyImageDialog,
|
|
1945
|
+
children: "确定"
|
|
1946
|
+
})]
|
|
1947
|
+
})
|
|
1948
|
+
]
|
|
1949
|
+
})
|
|
1950
|
+
}) : null,
|
|
1546
1951
|
/* @__PURE__ */ jsx("input", {
|
|
1547
1952
|
ref: imageRef,
|
|
1548
1953
|
type: "file",
|
|
@@ -2254,7 +2659,7 @@ function MarkdownEditor({ markdown, onChange, onUploadImage, renderMarkdown = de
|
|
|
2254
2659
|
setError("");
|
|
2255
2660
|
try {
|
|
2256
2661
|
const url = await onUploadImage(file);
|
|
2257
|
-
insertAtCursor(`![${file.name.replace(/\.[^.]+$/, "")}](${url})`, "", file.name);
|
|
2662
|
+
insertAtCursor(`![${file.name.replace(/\.[^.]+$/, "")}](${url} "kbw=640")`, "", file.name);
|
|
2258
2663
|
} catch (err) {
|
|
2259
2664
|
setError(err instanceof Error ? err.message : "图片上传失败");
|
|
2260
2665
|
} finally {
|