22import React , { useEffect , useRef , useState } from "react" ;
33import { Wrapper , CanvasContainer , OutputBox , StyledSVG } from "./shapeBuilder.styles" ;
44import { Button , Typography , Box } from "@layer5/sistent" ;
5+ import { SVG , extend as SVGextend } from '@svgdotjs/svg.js' ;
6+ import draw from '@svgdotjs/svg.draw.js' ;
57
6- // import { useTheme } from "@layer5/sistent/components/ThemeProvider";
7- // import { useMediaQuery } from "@layer5/sistent/components/MediaQuery";
8-
8+ SVGextend ( SVG . Polygon , draw ) ;
99
1010const ShapeBuilder = ( ) => {
1111 const boardRef = useRef ( null ) ;
@@ -14,30 +14,46 @@ const ShapeBuilder = () => {
1414 const [ result , setResult ] = useState ( "" ) ;
1515 const [ error , setError ] = useState ( null ) ;
1616
17+ const getPlottedPoints = ( poly ) => {
18+ if ( ! poly ) return null ;
19+ const plotted = poly . plot ( ) ;
20+ const points = Array . isArray ( plotted ) ? plotted : plotted ?. value ;
21+ return Array . isArray ( points ) ? points : null ;
22+ } ;
23+
1724 const showCytoArray = ( ) => {
1825 const poly = polyRef . current ;
1926 if ( ! poly ) return ;
2027
21- const points = poly . array ( ) . value ;
22- const normalized = points
23- . map ( ( [ x , y ] ) => [ ( x - 260 ) / 260 , ( y - 260 ) / 260 ] )
24- . flat ( )
25- . join ( " " ) ;
26- setResult ( normalized ) ;
28+ try {
29+ const points = getPlottedPoints ( poly ) ;
30+ if ( ! points ) throw new Error ( "Invalid or empty polygon points" ) ;
31+
32+ const normalized = points
33+ . map ( ( [ x , y ] ) => [ ( x - 260 ) / 260 , ( y - 260 ) / 260 ] )
34+ . flat ( )
35+ . join ( " " ) ;
36+ setResult ( normalized ) ;
37+ setError ( null ) ;
38+ } catch ( err ) {
39+ setError ( "Failed to extract and normalize polygon points." ) ;
40+ console . error ( "showCytoArray error:" , err ) ;
41+ }
2742 } ;
2843
2944 const handleMaximize = ( ) => {
3045 const poly = polyRef . current ;
3146 if ( ! poly ) return ;
3247
33- const points = poly . array ( ) . value ;
48+ const points = getPlottedPoints ( poly ) ;
49+ if ( ! points ) return ;
3450 const xs = points . map ( p => p [ 0 ] ) ;
3551 const ys = points . map ( p => p [ 1 ] ) ;
3652
3753 const width = Math . abs ( Math . max ( ...xs ) - Math . min ( ...xs ) ) ;
3854 const height = Math . abs ( Math . max ( ...ys ) - Math . min ( ...ys ) ) ;
3955
40- poly . size ( width > height ? 520 : null , height >= width ? 520 : null ) ;
56+ poly . size ( width > height ? 520 : undefined , height >= width ? 520 : undefined ) ;
4157 poly . move ( 0 , 0 ) ;
4258 showCytoArray ( ) ;
4359 } ;
@@ -57,9 +73,9 @@ const ShapeBuilder = () => {
5773 }
5874
5975 if ( e . ctrlKey && e . key . toLowerCase ( ) === "z" ) {
60- const points = poly . array ( ) . value ;
61- points . pop ( ) ;
62- poly . plot ( points ) ;
76+ const points = getPlottedPoints ( poly ) ;
77+ if ( ! points ) return ;
78+ poly . plot ( points . slice ( 0 , - 1 ) ) ;
6379 }
6480 } ;
6581
@@ -88,18 +104,9 @@ const ShapeBuilder = () => {
88104 return ;
89105 }
90106
91- if ( ! window . SVG ) {
92- setError ( "SVG.js not loaded" ) ;
93- return ;
94- }
95-
96- if ( ! window . SVG . Element . prototype . draw ) {
97- setError ( "svg.draw.js plugin not loaded" ) ;
98- return ;
99- }
100-
101107 try {
102- const draw = window . SVG ( boardRef . current )
108+ const draw = SVG ( )
109+ . addTo ( boardRef . current )
103110 . size ( "100%" , "100%" )
104111 . polygon ( )
105112 . draw ( )
@@ -138,28 +145,7 @@ const ShapeBuilder = () => {
138145 } ;
139146
140147 useEffect ( ( ) => {
141- const checkSVG = ( ) => {
142- if ( ! window . SVG || ! window . SVG . Element . prototype . draw ) {
143- setError ( "SVG.js or svg.draw.js plugin not loaded" ) ;
144- return false ;
145- }
146- return true ;
147- } ;
148-
149- // Initial check
150- if ( checkSVG ( ) ) {
151- initializeDrawing ( ) ;
152- } else {
153- // If not loaded, try again after a short delay
154- const timer = setTimeout ( ( ) => {
155- if ( checkSVG ( ) ) {
156- initializeDrawing ( ) ;
157- }
158- } , 1000 ) ;
159-
160- return ( ) => clearTimeout ( timer ) ;
161- }
162-
148+ initializeDrawing ( ) ;
163149 return ( ) => {
164150 detachKeyListeners ( ) ;
165151 if ( polyRef . current ) {
0 commit comments