1+ import { toast } from 'react-toastify' ;
12import { CircleEntity } from '../../entities/CircleEntity' ;
23import type { Entity } from '../../entities/Entity' ;
34import { LineEntity } from '../../entities/LineEntity' ;
@@ -12,6 +13,7 @@ import {middle} from '../middle.ts';
1213
1314function svgChildrenToEntities ( root : RootNode ) : Entity [ ] {
1415 if ( ! root . children || ! root . children ?. [ 0 ] ) {
16+ toast . error ( 'Failed to load SVG file since it appears to be empty' ) ;
1517 console . error ( new Error ( 'Empty SVG file' ) ) ;
1618 return [ ] ;
1719 }
@@ -84,6 +86,9 @@ function svgChildrenToEntities(root: RootNode): Entity[] {
8486 entities . push ( new LineEntity ( getActiveLayerId ( ) , startPoint , endPoint ) ) ;
8587 } else {
8688 // stop
89+ toast . error (
90+ `Error processing SVG polygon: expected an even number of points, but got ${ coords . length } `
91+ ) ;
8792 console . error ( `expected even number of points but got: ${ coords . length } ` ) ;
8893 }
8994 }
@@ -108,31 +113,36 @@ export function importEntitiesFromSvgFile(file: File | null | undefined) {
108113
109114 const reader = new FileReader ( ) ;
110115 reader . addEventListener ( 'load' , async ( ) => {
111- const svg = reader . result as string ;
116+ try {
117+ const svg = reader . result as string ;
112118
113- const data = parse ( svg ) ;
119+ const data = parse ( svg ) ;
114120
115- const svgEntities : Entity [ ] = svgChildrenToEntities ( data ) ;
121+ const svgEntities : Entity [ ] = svgChildrenToEntities ( data ) ;
116122
117- // We still need to flip the image top to bottom since the coordinate system of svg has a y-axis that goes down
118- // And the world coordinate system of this application has a mathematical y-axis that goes up
119- const boundingBox = getBoundingBoxOfMultipleEntities ( svgEntities ) ;
120- const centerPoint = new Point (
121- middle ( boundingBox . minX , boundingBox . maxX ) ,
122- middle ( boundingBox . minY , boundingBox . maxY )
123- ) ;
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+ ) ;
124130
125- const mirrorAxis = new LineEntity (
126- getActiveLayerId ( ) ,
127- centerPoint ,
128- new Point ( centerPoint . x + 1 , centerPoint . y )
129- ) ;
130- for ( const svgEntity of svgEntities ) {
131- svgEntity . mirror ( mirrorAxis ) ;
132- }
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+ }
133139
134- setEntities ( [ ...getEntities ( ) , ...svgEntities ] ) ;
135- resolve ( ) ;
140+ setEntities ( [ ...getEntities ( ) , ...svgEntities ] ) ;
141+ resolve ( ) ;
142+ } catch ( error ) {
143+ toast . error ( 'Failed to load SVG file' ) ;
144+ console . error ( error ) ;
145+ }
136146 } ) ;
137147 reader . readAsText ( file , 'utf-8' ) ;
138148 } ) ;
0 commit comments