Update App.jsx via upload script - 2026-11-04 09:48:16
This commit is contained in:
parent
ebe8af0f8f
commit
4b41f7d5e1
61
App.jsx
61
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 <iframe title="预览" sandbox="allow-scripts" srcDoc={doc} style={{ width: '100%', height: 480, border: picking ? '2px solid #e11d48' : '1px solid var(--line)', borderRadius: 8, background: '#fff' }} />;
|
||||
}
|
||||
const DATA_IMG_RE = /data:image\/[a-z+.-]+;base64,[A-Za-z0-9+/=]+/g;
|
||||
const imgTag = (img) => `<img src="${img.src}" alt="" width="${img.w}" height="${img.h}" style="display:block;max-width:100%;height:auto;margin:0 auto">`;
|
||||
|
||||
// 详情图片里的 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 }) {
|
|||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8, flexWrap: 'wrap' }}>
|
||||
<span className="field-label" style={{ margin: 0 }}>详情 HTML 片段({blocks.length})</span>
|
||||
<div style={{ display: 'flex', gap: 10, alignItems: 'center', flexWrap: 'wrap' }}>
|
||||
{(embedded > 0 || moving) && <button type="button" className="btn btn-sm" disabled={moving.startsWith('正在')} onClick={moveEmbedded} title="以前插入的图片是直接嵌在 HTML 里的,会让模板数据很大;转换后图片存成文件,HTML 里只留地址,显示效果不变">{moving || `嵌入图片转成文件(${embedded} 张)`}</button>}
|
||||
<label style={{ fontSize: 12.5, display: 'flex', gap: 4, alignItems: 'center', cursor: 'pointer' }} title="从图片四边开始,把连着边的白色背景变透明;产品内部的白色不受影响。图片本来就是透明底的不用勾">
|
||||
<input type="checkbox" checked={removeWhite} onChange={e => setRemoveWhite(e.target.checked)} />插入 / 替换图片时去掉白色背景
|
||||
</label>
|
||||
|
|
@ -7508,7 +7562,7 @@ function DetailHtmlBlocks({ tpl, update }) {
|
|||
</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--ink-soft)', margin: '6px 0 8px' }}>
|
||||
粘贴 HTML 代码或上传 .html 文件(比如用 Gemini 做的图文详情页),网站上原样显示:保留自己的排版、颜色和字体,像一张长图一样插在详情图片之间。英文写的文字会自动翻译成西语、俄语。
|
||||
<br />图片:「替换图片」= 先选新图片,再在预览里点要换掉的那张(示意图、SVG 插画、原有图片都可以换,大小和排版沿用原来的);「插入图片」= 插到下面代码框里光标所在的位置(没点过代码框就放在最后)。JPG / PNG / WebP 都可以,自动压缩后嵌进 HTML。
|
||||
<br />图片:「替换图片」= 先选新图片,再在预览里点要换掉的那张(示意图、SVG 插画、原有图片都可以换,大小和排版沿用原来的);「插入图片」= 插到下面代码框里光标所在的位置(没点过代码框就放在最后)。JPG / PNG / WebP 都可以,自动压缩后存成模板的图片文件,HTML 里只写地址(网站构建时自动下载到网站上)。
|
||||
</div>
|
||||
{blocks.map((b, k) => {
|
||||
const len = String(b.html || '').trim().length;
|
||||
|
|
@ -7538,8 +7592,7 @@ function DetailHtmlBlocks({ tpl, update }) {
|
|||
onSelect={e => { cursor.current[b.id] = e.target.selectionStart; }}
|
||||
placeholder="<div>…</div> 或完整的 <html>…</html>" style={{ fontFamily: 'var(--mono, monospace)', fontSize: 12, resize: 'vertical', width: '100%', boxSizing: 'border-box' }} />
|
||||
{preview === b.id && len > 0 && (
|
||||
<iframe title="预览" sandbox="allow-scripts" srcDoc={isPicking ? String(b.html) + pickScript(b.id) : b.html}
|
||||
style={{ width: '100%', height: 480, border: isPicking ? '2px solid #e11d48' : '1px solid var(--line)', borderRadius: 8, background: '#fff' }} />
|
||||
<HtmlPreviewFrame html={String(b.html)} extra={isPicking ? pickScript(b.id) : ''} picking={isPicking} />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in New Issue