Upload svg2Base64.ts
Browse files
frontend/src/utils/svg2Base64.ts
CHANGED
@@ -49,18 +49,43 @@ const encode = (input: string) => {
|
|
49 |
|
50 |
export const svg2Base64 = (element: Element) => {
|
51 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
// 克隆元素以避免修改原始DOM
|
53 |
const clonedElement = element.cloneNode(true) as Element;
|
|
|
54 |
|
55 |
// 确保SVG有正确的命名空间
|
56 |
if (clonedElement.tagName.toLowerCase() === 'svg') {
|
57 |
clonedElement.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
|
58 |
clonedElement.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
|
|
|
59 |
}
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
const XMLS = new XMLSerializer();
|
62 |
const svg = XMLS.serializeToString(clonedElement);
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
if (!svg || svg.length === 0) {
|
65 |
throw new Error('SVG serialization returned empty string');
|
66 |
}
|
@@ -70,9 +95,12 @@ export const svg2Base64 = (element: Element) => {
|
|
70 |
throw new Error('Base64 encoding failed');
|
71 |
}
|
72 |
|
|
|
73 |
return PREFIX + encoded;
|
74 |
} catch (error) {
|
75 |
console.error('svg2Base64 failed:', error);
|
|
|
|
|
76 |
throw error;
|
77 |
}
|
78 |
}
|
|
|
49 |
|
50 |
export const svg2Base64 = (element: Element) => {
|
51 |
try {
|
52 |
+
console.log('svg2Base64: Starting conversion for element:', {
|
53 |
+
tagName: element.tagName,
|
54 |
+
className: element.className,
|
55 |
+
id: element.id,
|
56 |
+
hasChildren: element.children.length > 0
|
57 |
+
});
|
58 |
+
|
59 |
// 克隆元素以避免修改原始DOM
|
60 |
const clonedElement = element.cloneNode(true) as Element;
|
61 |
+
console.log('svg2Base64: Element cloned successfully');
|
62 |
|
63 |
// 确保SVG有正确的命名空间
|
64 |
if (clonedElement.tagName.toLowerCase() === 'svg') {
|
65 |
clonedElement.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
|
66 |
clonedElement.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
|
67 |
+
console.log('svg2Base64: Added SVG namespaces');
|
68 |
}
|
69 |
|
70 |
+
// 检查是否有内联样式需要处理
|
71 |
+
const computedStyles = window.getComputedStyle(element);
|
72 |
+
console.log('svg2Base64: Element computed styles:', {
|
73 |
+
width: computedStyles.width,
|
74 |
+
height: computedStyles.height,
|
75 |
+
display: computedStyles.display,
|
76 |
+
visibility: computedStyles.visibility
|
77 |
+
});
|
78 |
+
|
79 |
const XMLS = new XMLSerializer();
|
80 |
const svg = XMLS.serializeToString(clonedElement);
|
81 |
|
82 |
+
console.log('svg2Base64: Serialization result:', {
|
83 |
+
length: svg.length,
|
84 |
+
preview: svg.substring(0, 200) + '...',
|
85 |
+
containsSvgTag: svg.includes('<svg'),
|
86 |
+
containsContent: svg.length > 50
|
87 |
+
});
|
88 |
+
|
89 |
if (!svg || svg.length === 0) {
|
90 |
throw new Error('SVG serialization returned empty string');
|
91 |
}
|
|
|
95 |
throw new Error('Base64 encoding failed');
|
96 |
}
|
97 |
|
98 |
+
console.log('svg2Base64: Encoding successful, result length:', (PREFIX + encoded).length);
|
99 |
return PREFIX + encoded;
|
100 |
} catch (error) {
|
101 |
console.error('svg2Base64 failed:', error);
|
102 |
+
console.error('svg2Base64: Element that caused error:', element);
|
103 |
+
console.error('svg2Base64: Element outerHTML:', element.outerHTML);
|
104 |
throw error;
|
105 |
}
|
106 |
}
|