diff --git a/App.jsx b/App.jsx
index e37144c..2deba01 100644
--- a/App.jsx
+++ b/App.jsx
@@ -7422,6 +7422,37 @@ function replaceHtmlVisual(html, idx, img) {
}
return full ? (doctype ? doctype + '\n' : '') + doc.documentElement.outerHTML : doc.body.innerHTML;
}
+// 处理好的图片存成模板目录下的文件(uploads/<模板id>/),HTML 里只写地址,模板数据不会因为图片变大
+async function uploadHtmlImage(tplId, img) {
+ const blob = await (await fetch(img.src)).blob();
+ const ext = blob.type === 'image/webp' ? '.webp' : blob.type === 'image/png' ? '.png' : '.jpg';
+ const res = await uploadFile(tplId, new File([blob], 'html' + ext, { type: blob.type }));
+ return { ...img, src: `/uploads/${tplId}/${res.fileName}` };
+}
+// 预览用:iframe 是隔离的(拿不到登录状态,读不了 /uploads/),先把图片读出来、临时换成内嵌数据再显示;只用于预览,不保存
+const previewImgCache = new Map();
+async function inlineForPreview(html) {
+ const src = String(html || '');
+ const urls = [...new Set(src.match(/\/uploads\/[A-Za-z0-9_-]+\/[A-Za-z0-9_.-]+/g) || [])];
+ let out = src;
+ for (const u of urls) {
+ if (!previewImgCache.has(u)) previewImgCache.set(u, fetch(u).then(r => (r.ok ? r.blob() : null)).then(b => (b ? new Promise(ok => { const fr = new FileReader(); fr.onload = () => ok(fr.result); fr.readAsDataURL(b); }) : null)).catch(() => null));
+ const d = await previewImgCache.get(u);
+ if (d) out = out.split(u).join(d);
+ }
+ return out;
+}
+// 预览 iframe:内容异步准备好(图片换成内嵌数据)再显示
+function HtmlPreviewFrame({ html, extra, picking }) {
+ const [doc, setDoc] = useState('');
+ useEffect(() => {
+ let alive = true;
+ const t = setTimeout(() => { inlineForPreview(html).then(d => { if (alive) setDoc(d + (extra || '')); }); }, 150);
+ return () => { alive = false; clearTimeout(t); };
+ }, [html, extra]);
+ return ;
+}
+const DATA_IMG_RE = /data:image\/[a-z+.-]+;base64,[A-Za-z0-9+/=]+/g;
const imgTag = (img) => ``;
// 详情图片里的 HTML 片段:原样显示(保留自己的排版、颜色、字体),穿插在详情图片之间。
@@ -7448,6 +7479,28 @@ function DetailHtmlBlocks({ tpl, update }) {
const text = await file.text();
patch(id, { html: text });
}
+ // 以前直接嵌在 HTML 里的图片(data:image…):一键改存成文件,模板数据变小
+ const embedded = blocks.reduce((n, b) => n + ((String(b.html || '').match(DATA_IMG_RE) || []).length), 0);
+ const [moving, setMoving] = useState('');
+ async function moveEmbedded() {
+ setMoving('正在转换…');
+ try {
+ const done = new Map();
+ let count = 0;
+ // 先把所有图片传完,再一次性更新全部片段(逐个更新会互相覆盖)
+ for (const b of blocksRef.current) {
+ for (const d of new Set(String(b.html || '').match(DATA_IMG_RE) || [])) {
+ if (!done.has(d)) { done.set(d, (await uploadHtmlImage(tpl.id, { src: d })).src); count++; setMoving(`正在转换… ${count}`); }
+ }
+ }
+ set(blocksRef.current.map(b => {
+ let h = String(b.html || '');
+ for (const [d, u] of done) if (h.includes(d)) h = h.split(d).join(u);
+ return h === b.html ? b : { ...b, html: h };
+ }));
+ setMoving(`已转换 ${count} 张图片,模板数据变小了`);
+ } catch (e) { setMoving('转换失败:' + e.message); }
+ }
const countVisuals = (html) => { try { return HTML_VISUALS(new DOMParser().parseFromString(String(html || ''), 'text/html')).length; } catch { return 0; } };
function chooseImages(id, mode) {
action.current = { id, mode };
@@ -7459,7 +7512,7 @@ function DetailHtmlBlocks({ tpl, update }) {
setBusy(act.id);
try {
const imgs = [];
- for (const f of files) imgs.push(await imageFileToDataUrl(f, { removeWhite }));
+ for (const f of files) imgs.push(await uploadHtmlImage(tpl.id, await imageFileToDataUrl(f, { removeWhite })));
const kb = imgs.reduce((s, x) => s + x.kb, 0);
if (act.mode === 'insert') {
const b = blocksRef.current.find(x => x.id === act.id);
@@ -7500,6 +7553,7 @@ function DetailHtmlBlocks({ tpl, update }) {