CatPtain commited on
Commit
9b86ce9
·
verified ·
1 Parent(s): cccb22d

Upload useExport.ts

Browse files
Files changed (1) hide show
  1. frontend/src/hooks/useExport.ts +85 -16
frontend/src/hooks/useExport.ts CHANGED
@@ -51,43 +51,69 @@ export default () => {
51
  console.log('exportImage: Environment check:', { isHuggingface: isHF, format, production: isProd })
52
  }
53
 
54
- // 预处理矢量图形元素
55
  const preprocessVectorElements = async () => {
56
  const svgElements = domRef.querySelectorAll('svg');
57
  const vectorShapes = domRef.querySelectorAll('.vector-shape');
58
 
59
- // 处理SVG元素
60
  for (const svg of Array.from(svgElements)) {
61
  try {
62
- // 移除problematic属性
63
- svg.removeAttribute('vector-effect');
64
- const vectorEffectElements = svg.querySelectorAll('[vector-effect]');
65
- vectorEffectElements.forEach(el => el.removeAttribute('vector-effect'));
66
-
67
- // 确保命名空间
68
- if (!svg.hasAttribute('xmlns')) {
69
- svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
 
 
 
 
 
 
 
 
 
 
70
  }
71
  } catch (error) {
72
  if (!isProd) {
73
- console.warn('exportImage: SVG preprocessing failed:', error);
 
 
 
 
 
74
  }
75
  }
76
  }
77
 
78
- // 处理矢量形状
79
  for (const shape of Array.from(vectorShapes)) {
80
  try {
81
  const svgChild = shape.querySelector('svg');
82
  if (svgChild) {
83
- svgChild.removeAttribute('vector-effect');
84
- if (!svgChild.hasAttribute('xmlns')) {
85
- svgChild.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
 
 
 
 
 
 
 
 
 
 
 
86
  }
87
  }
88
  } catch (error) {
89
  if (!isProd) {
90
- console.warn('exportImage: Vector shape preprocessing failed:', error);
91
  }
92
  }
93
  }
@@ -633,6 +659,28 @@ export default () => {
633
  if (element.special && element.path) {
634
  try {
635
  const svgContent = generateSVGFromShape(element);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
636
  return `<div class="element shape-element vector-shape" style="${baseStyle}">${svgContent}</div>`;
637
  } catch (error) {
638
  // 生产环境降级处理
@@ -640,6 +688,27 @@ export default () => {
640
  console.warn('formatElement: Vector shape generation failed, using fallback:', error);
641
  }
642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
643
  // 使用简化的矢量图形表示
644
  const fallbackSvg = `<svg width="${element.width}" height="${element.height}" xmlns="http://www.w3.org/2000/svg">
645
  <rect width="100%" height="100%" fill="${element.fill || '#f5f5f5'}" stroke="${element.outline?.color || '#ddd'}" stroke-width="${element.outline?.width || 1}"/>
 
51
  console.log('exportImage: Environment check:', { isHuggingface: isHF, format, production: isProd })
52
  }
53
 
54
+ // 使用VectorRenderManager预处理矢量图形元素
55
  const preprocessVectorElements = async () => {
56
  const svgElements = domRef.querySelectorAll('svg');
57
  const vectorShapes = domRef.querySelectorAll('.vector-shape');
58
 
59
+ // 处理SVG元素 - 使用VectorRenderManager优化
60
  for (const svg of Array.from(svgElements)) {
61
  try {
62
+ const renderResult = await vectorRenderManager.renderElement(svg);
63
+ if (renderResult.success && renderResult.data) {
64
+ // 用优化后的SVG替换原始SVG
65
+ const tempDiv = document.createElement('div');
66
+ tempDiv.innerHTML = renderResult.data;
67
+ const optimizedSvg = tempDiv.querySelector('svg');
68
+ if (optimizedSvg && svg.parentNode) {
69
+ svg.parentNode.replaceChild(optimizedSvg, svg);
70
+ }
71
+ } else {
72
+ // 降级处理:手动清理属性
73
+ svg.removeAttribute('vector-effect');
74
+ const vectorEffectElements = svg.querySelectorAll('[vector-effect]');
75
+ vectorEffectElements.forEach(el => el.removeAttribute('vector-effect'));
76
+
77
+ if (!svg.hasAttribute('xmlns')) {
78
+ svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
79
+ }
80
  }
81
  } catch (error) {
82
  if (!isProd) {
83
+ console.warn('exportImage: VectorRenderManager failed, using fallback:', error);
84
+ }
85
+ // 降级处理
86
+ svg.removeAttribute('vector-effect');
87
+ if (!svg.hasAttribute('xmlns')) {
88
+ svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
89
  }
90
  }
91
  }
92
 
93
+ // 处理矢量形状 - 使用VectorRenderManager优化
94
  for (const shape of Array.from(vectorShapes)) {
95
  try {
96
  const svgChild = shape.querySelector('svg');
97
  if (svgChild) {
98
+ const renderResult = await vectorRenderManager.renderElement(svgChild);
99
+ if (renderResult.success && renderResult.data) {
100
+ const tempDiv = document.createElement('div');
101
+ tempDiv.innerHTML = renderResult.data;
102
+ const optimizedSvg = tempDiv.querySelector('svg');
103
+ if (optimizedSvg) {
104
+ svgChild.parentNode?.replaceChild(optimizedSvg, svgChild);
105
+ }
106
+ } else {
107
+ // 降级处理
108
+ svgChild.removeAttribute('vector-effect');
109
+ if (!svgChild.hasAttribute('xmlns')) {
110
+ svgChild.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
111
+ }
112
  }
113
  }
114
  } catch (error) {
115
  if (!isProd) {
116
+ console.warn('exportImage: Vector shape optimization failed:', error);
117
  }
118
  }
119
  }
 
659
  if (element.special && element.path) {
660
  try {
661
  const svgContent = generateSVGFromShape(element);
662
+
663
+ // 使用VectorRenderManager进一步优化SVG
664
+ const tempDiv = document.createElement('div');
665
+ tempDiv.innerHTML = svgContent;
666
+ const svgElement = tempDiv.querySelector('svg');
667
+
668
+ if (svgElement) {
669
+ // 异步优化SVG(不阻塞主流程)
670
+ vectorRenderManager.renderElement(svgElement).then(result => {
671
+ if (result.success && result.data) {
672
+ // 在后台优化成功,但不影响当前渲染
673
+ if (!VECTOR_EXPORT_CONFIG.ENVIRONMENT.isProduction()) {
674
+ console.log('formatElement: SVG optimized successfully');
675
+ }
676
+ }
677
+ }).catch(error => {
678
+ if (!VECTOR_EXPORT_CONFIG.ENVIRONMENT.isProduction()) {
679
+ console.warn('formatElement: Background SVG optimization failed:', error);
680
+ }
681
+ });
682
+ }
683
+
684
  return `<div class="element shape-element vector-shape" style="${baseStyle}">${svgContent}</div>`;
685
  } catch (error) {
686
  // 生产环境降级处理
 
688
  console.warn('formatElement: Vector shape generation failed, using fallback:', error);
689
  }
690
 
691
+ // 使用VectorRenderManager生成优化的降级SVG
692
+ try {
693
+ const placeholderElement = document.createElement('div');
694
+ placeholderElement.style.width = `${element.width}px`;
695
+ placeholderElement.style.height = `${element.height}px`;
696
+
697
+ // 尝试使用VectorRenderManager生成占位符
698
+ vectorRenderManager.renderElement(placeholderElement).then(result => {
699
+ if (result.success && result.data) {
700
+ // 异步替换占位符(不阻塞当前渲染)
701
+ if (!VECTOR_EXPORT_CONFIG.ENVIRONMENT.isProduction()) {
702
+ console.log('formatElement: Fallback SVG generated successfully');
703
+ }
704
+ }
705
+ }).catch(() => {
706
+ // 静默处理降级失败
707
+ });
708
+ } catch (managerError) {
709
+ // VectorRenderManager也失败时的最终降级
710
+ }
711
+
712
  // 使用简化的矢量图形表示
713
  const fallbackSvg = `<svg width="${element.width}" height="${element.height}" xmlns="http://www.w3.org/2000/svg">
714
  <rect width="100%" height="100%" fill="${element.fill || '#f5f5f5'}" stroke="${element.outline?.color || '#ddd'}" stroke-width="${element.outline?.width || 1}"/>