@@ -16,15 +16,31 @@ import Form from './Form.tsx';
1616
1717const barPadding = 0.3 ;
1818
19- const prepareMarkup = ( chartSVG : string , markup : string ) => {
19+ const prepareMarkup = ( chartSVG : string , sourceContainer : HTMLElement ) => {
20+ const sourceElements = sourceContainer . querySelectorAll ( '*' ) ;
21+
2022 const svg = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'svg' ) ;
2123 svg . setAttribute ( 'xmlns' , 'http://www.w3.org/2000/svg' ) ;
2224 svg . setAttribute ( 'xmlns:xlink' , 'http://www.w3.org/1999/xlink' ) ;
2325 svg . setAttribute ( 'version' , '1.1' ) ;
2426 svg . setAttribute ( 'width' , '820px' ) ;
2527 svg . setAttribute ( 'height' , '420px' ) ;
26- svg . innerHTML = markup ;
28+ const clonedContainer = sourceContainer . cloneNode ( true ) as HTMLElement ;
29+ const clonedElements = clonedContainer . querySelectorAll ( '*' ) ;
30+
31+ clonedElements . forEach ( ( clonedEl , index ) => {
32+ const sourceEl = sourceElements [ index ] as HTMLElement ;
33+ if ( ! sourceEl ) return ;
2734
35+ const computed = window . getComputedStyle ( sourceEl ) ;
36+ [ 'fill' , 'font-family' , 'font-size' , 'font-weight' , 'opacity' , 'stroke' , 'stroke-width' ] . forEach ( ( prop ) => {
37+ const value = computed . getPropertyValue ( prop ) ?. trim ( ) ;
38+ if ( value && value !== 'auto' && value !== 'normal' && value !== 'initial' ) {
39+ ( clonedEl as SVGElement ) . setAttribute ( prop , value ) ;
40+ }
41+ } ) ;
42+ } ) ;
43+ svg . innerHTML = clonedContainer . innerHTML ;
2844 const group = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'g' ) ;
2945 group . setAttribute ( 'transform' , 'translate(305,12)' ) ;
3046 group . innerHTML = chartSVG ;
@@ -40,7 +56,7 @@ function App() {
4056 const onClick = useCallback ( ( ) => {
4157 exportFromMarkup (
4258 prepareMarkup ( chartRef . current ?. instance ( ) . svg ( ) ?? '' ,
43- childRef . current ?. innerHTML ?? '' ) , {
59+ childRef . current ?? document . createElement ( 'div' ) ) , {
4460 width : 820 ,
4561 height : 420 ,
4662 margin : 0 ,
@@ -68,7 +84,7 @@ function App() {
6884
6985 return (
7086 < div id = "chart-demo" >
71- < div className = "chart_environment " >
87+ < div className = "chart-environment " >
7288 < Form ref = { childRef } />
7389 < Chart
7490 ref = { chartRef }
0 commit comments