1+ import { toast } from 'react-toastify' ;
12import { CircleEntity } from '../../entities/CircleEntity' ;
23import type { Entity } from '../../entities/Entity' ;
34import { LineEntity } from '../../entities/LineEntity' ;
45import { RectangleEntity } from '../../entities/RectangleEntity' ;
5- import { toast } from 'react-toastify' ;
66import { getActiveLayerId , getEntities , setEntities } from '../../state' ;
77
88import { Point } from '@flatten-js/core' ;
@@ -86,7 +86,9 @@ function svgChildrenToEntities(root: RootNode): Entity[] {
8686 entities . push ( new LineEntity ( getActiveLayerId ( ) , startPoint , endPoint ) ) ;
8787 } else {
8888 // stop
89- toast . error ( `Error processing SVG polygon: expected an even number of points, but got ${ coords . length } ` ) ;
89+ toast . error (
90+ `Error processing SVG polygon: expected an even number of points, but got ${ coords . length } `
91+ ) ;
9092 console . error ( `expected even number of points but got: ${ coords . length } ` ) ;
9193 }
9294 }
@@ -111,31 +113,36 @@ export function importEntitiesFromSvgFile(file: File | null | undefined) {
111113
112114 const reader = new FileReader ( ) ;
113115 reader . addEventListener ( 'load' , async ( ) => {
114- const svg = reader . result as string ;
116+ try {
117+ const svg = reader . result as string ;
115118
116- const data = parse ( svg ) ;
119+ const data = parse ( svg ) ;
117120
118- const svgEntities : Entity [ ] = svgChildrenToEntities ( data ) ;
121+ const svgEntities : Entity [ ] = svgChildrenToEntities ( data ) ;
119122
120- // We still need to flip the image top to bottom since the coordinate system of svg has a y-axis that goes down
121- // And the world coordinate system of this application has a mathematical y-axis that goes up
122- const boundingBox = getBoundingBoxOfMultipleEntities ( svgEntities ) ;
123- const centerPoint = new Point (
124- middle ( boundingBox . minX , boundingBox . maxX ) ,
125- middle ( boundingBox . minY , boundingBox . maxY )
126- ) ;
123+ // We still need to flip the image top to bottom since the coordinate system of svg has a y-axis that goes down
124+ // And the world coordinate system of this application has a mathematical y-axis that goes up
125+ const boundingBox = getBoundingBoxOfMultipleEntities ( svgEntities ) ;
126+ const centerPoint = new Point (
127+ middle ( boundingBox . minX , boundingBox . maxX ) ,
128+ middle ( boundingBox . minY , boundingBox . maxY )
129+ ) ;
127130
128- const mirrorAxis = new LineEntity (
129- getActiveLayerId ( ) ,
130- centerPoint ,
131- new Point ( centerPoint . x + 1 , centerPoint . y )
132- ) ;
133- for ( const svgEntity of svgEntities ) {
134- svgEntity . mirror ( mirrorAxis ) ;
135- }
131+ const mirrorAxis = new LineEntity (
132+ getActiveLayerId ( ) ,
133+ centerPoint ,
134+ new Point ( centerPoint . x + 1 , centerPoint . y )
135+ ) ;
136+ for ( const svgEntity of svgEntities ) {
137+ svgEntity . mirror ( mirrorAxis ) ;
138+ }
136139
137- setEntities ( [ ...getEntities ( ) , ...svgEntities ] ) ;
138- resolve ( ) ;
140+ setEntities ( [ ...getEntities ( ) , ...svgEntities ] ) ;
141+ resolve ( ) ;
142+ } catch ( error ) {
143+ toast . error ( 'Failed to load SVG file' ) ;
144+ console . error ( error ) ;
145+ }
139146 } ) ;
140147 reader . readAsText ( file , 'utf-8' ) ;
141148 } ) ;
0 commit comments