feat: SVG 图标序列化为图片(重构版)

- 不修改 backgroundUrls 变量
- 在节点创建后、返回前处理 SVG
- 用 cloneNode + XMLSerializer + btoa 序列化
- 设置 backgroundImages 和 type=RECTANGLE
This commit is contained in:
李进
2026-07-27 15:56:33 +08:00
parent 00b3150d1c
commit a2a31c328e
+16 -1
View File
@@ -985,7 +985,7 @@
pushCaptureChild(children, afterNode);
const type = element.tagName === "IMG" || backgroundUrls.length ? "RECTANGLE" : "FRAME";
return {
const node = {
id: `node-${++nodeCounter}`,
type,
tag: element.tagName,
@@ -1005,6 +1005,21 @@
backgroundImages: backgroundUrls,
children
};
// SVG → 序列化为 data URL 图片
if ((element.tagName === "SVG" || element.tagName === "svg") && !node.backgroundImages.length) {
try {
const svgClone = element.cloneNode(true);
if (!svgClone.getAttribute("xmlns")) svgClone.setAttribute("xmlns", "http://www.w3.org/2000/svg");
const svgStr = new XMLSerializer().serializeToString(svgClone);
node.backgroundImages = ["data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgStr)))];
node.type = "RECTANGLE";
} catch (e) { /* 静默失败 */ }
}
return node;
}
function normalizeAssetUrl(url) {