11// /* global window */
22import React , { useEffect , useRef , useState } from "react" ;
3- import { Wrapper , CanvasContainer , StyledSVG } from "./shapeBuilder.styles" ;
4- import { Button , Box } from "@layer5/sistent" ;
5- import { useNotificationHandlers } from "../utils/notification-handlers"
6- // import { useTheme } from "@layer5/sistent/components/ThemeProvider";
7- // import { useMediaQuery } from "@layer5/sistent/components/MediaQuery";
3+ import { Wrapper , CanvasContainer , OutputBox , StyledSVG } from "./shapeBuilder.styles" ;
4+ import { Button , Typography , Box } from "@layer5/sistent" ;
5+ import { SVG , extend as SVGextend } from '@svgdotjs/svg.js' ;
6+ import draw from '@svgdotjs/svg.draw.js' ;
87
8+ SVGextend ( SVG . Polygon , draw ) ;
99
1010const ShapeBuilder = ( ) => {
1111 const { handleError, handleSuccess } = useNotificationHandlers ( ) ;
@@ -15,30 +15,46 @@ const ShapeBuilder = () => {
1515 const [ result , setResult ] = useState ( "" ) ;
1616 const [ error , setError ] = useState ( null ) ;
1717
18+ const getPlottedPoints = ( poly ) => {
19+ if ( ! poly ) return null ;
20+ const plotted = poly . plot ( ) ;
21+ const points = Array . isArray ( plotted ) ? plotted : plotted ?. value ;
22+ return Array . isArray ( points ) ? points : null ;
23+ } ;
24+
1825 const showCytoArray = ( ) => {
1926 const poly = polyRef . current ;
2027 if ( ! poly ) return ;
2128
22- const points = poly . array ( ) . value ;
23- const normalized = points
24- . map ( ( [ x , y ] ) => [ ( x - 260 ) / 260 , ( y - 260 ) / 260 ] )
25- . flat ( )
26- . join ( " " ) ;
27- setResult ( normalized ) ;
29+ try {
30+ const points = getPlottedPoints ( poly ) ;
31+ if ( ! points ) throw new Error ( "Invalid or empty polygon points" ) ;
32+
33+ const normalized = points
34+ . map ( ( [ x , y ] ) => [ ( x - 260 ) / 260 , ( y - 260 ) / 260 ] )
35+ . flat ( )
36+ . join ( " " ) ;
37+ setResult ( normalized ) ;
38+ setError ( null ) ;
39+ } catch ( err ) {
40+ setError ( "Failed to extract and normalize polygon points." ) ;
41+ console . error ( "showCytoArray error:" , err ) ;
42+ }
2843 } ;
2944
3045 const handleMaximize = ( ) => {
3146 const poly = polyRef . current ;
3247 if ( ! poly ) return ;
3348
34- const points = poly . array ( ) . value ;
49+ const points = getPlottedPoints ( poly ) ;
50+ if ( ! points ) return ;
3551 const xs = points . map ( p => p [ 0 ] ) ;
3652 const ys = points . map ( p => p [ 1 ] ) ;
3753
3854 const width = Math . abs ( Math . max ( ...xs ) - Math . min ( ...xs ) ) ;
3955 const height = Math . abs ( Math . max ( ...ys ) - Math . min ( ...ys ) ) ;
4056
41- poly . size ( width > height ? 520 : null , height >= width ? 520 : null ) ;
57+ poly . size ( width > height ? 520 : undefined , height >= width ? 520 : undefined ) ;
4258 poly . move ( 0 , 0 ) ;
4359 showCytoArray ( ) ;
4460 } ;
@@ -51,16 +67,16 @@ const ShapeBuilder = () => {
5167 poly . draw ( "param" , "snapToGrid" , 0.001 ) ;
5268 }
5369
54- if ( e . ctrlKey && e . key === "Enter" ) {
70+ if ( e . key === "Enter" ) {
5571 poly . draw ( "done" ) ;
5672 poly . fill ( "#00B39F" ) ;
5773 showCytoArray ( ) ;
5874 }
5975
6076 if ( e . ctrlKey && e . key . toLowerCase ( ) === "z" ) {
61- const points = poly . array ( ) . value ;
62- points . pop ( ) ;
63- poly . plot ( points ) ;
77+ const points = getPlottedPoints ( poly ) ;
78+ if ( ! points ) return ;
79+ poly . plot ( points . slice ( 0 , - 1 ) ) ;
6480 }
6581 } ;
6682
@@ -89,18 +105,9 @@ const ShapeBuilder = () => {
89105 return ;
90106 }
91107
92- if ( ! window . SVG ) {
93- setError ( "SVG.js not loaded" ) ;
94- return ;
95- }
96-
97- if ( ! window . SVG . Element . prototype . draw ) {
98- setError ( "svg.draw.js plugin not loaded" ) ;
99- return ;
100- }
101-
102108 try {
103- const draw = window . SVG ( boardRef . current )
109+ const draw = SVG ( )
110+ . addTo ( boardRef . current )
104111 . size ( "100%" , "100%" )
105112 . polygon ( )
106113 . draw ( )
@@ -149,28 +156,7 @@ const ShapeBuilder = () => {
149156 } ;
150157
151158 useEffect ( ( ) => {
152- const checkSVG = ( ) => {
153- if ( ! window . SVG || ! window . SVG . Element . prototype . draw ) {
154- setError ( "SVG.js or svg.draw.js plugin not loaded" ) ;
155- return false ;
156- }
157- return true ;
158- } ;
159-
160- // Initial check
161- if ( checkSVG ( ) ) {
162- initializeDrawing ( ) ;
163- } else {
164- // If not loaded, try again after a short delay
165- const timer = setTimeout ( ( ) => {
166- if ( checkSVG ( ) ) {
167- initializeDrawing ( ) ;
168- }
169- } , 1000 ) ;
170-
171- return ( ) => clearTimeout ( timer ) ;
172- }
173-
159+ initializeDrawing ( ) ;
174160 return ( ) => {
175161 detachKeyListeners ( ) ;
176162 if ( polyRef . current ) {
0 commit comments