11import { ElementType } from "@/components/workspace/elements" ;
22import { dataAttributes } from "@/constants" ;
33import { store } from "@/store" ;
4- import d3Extended from "./d3" ;
4+ import { rgbToHex } from "." ;
5+ import { default as d3Extended } from "./d3" ;
56
67export const domSeatsToJSON = ( ) => {
78 return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Seat } "]` ) . map ( ( seat ) => {
@@ -26,13 +27,63 @@ export const domBoothsToJSON = () => {
2627 } ) ;
2728} ;
2829
29- export const domTextsToJSON = ( ) => { } ;
30+ export const domTextToJSON = ( ) => {
31+ return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Text } "]` ) . map ( ( text ) => {
32+ return {
33+ id : text . attr ( "id" ) ,
34+ x : + text . attr ( "x" ) ,
35+ y : + text . attr ( "y" ) ,
36+ label : text . text ( ) ,
37+ fontSize : + text . attr ( "font-size" ) ,
38+ fontWeight : + text . attr ( "font-weight" ) ,
39+ letterSpacing : + text . attr ( "letter-spacing" ) ,
40+ color : rgbToHex ( text . style ( "stroke" ) ) || text . attr ( "stroke" )
41+ } ;
42+ } ) ;
43+ } ;
3044
31- export const domShapesToJSON = ( ) => { } ;
45+ export const domShapesToJSON = ( ) => {
46+ return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Shape } "]` ) . map ( ( shape ) => {
47+ return {
48+ id : shape . attr ( "id" ) ,
49+ name : shape . attr ( dataAttributes . shape ) ,
50+ x : + shape . attr ( "x" ) ,
51+ y : + shape . attr ( "y" ) ,
52+ width : + shape . attr ( "width" ) ,
53+ height : + shape . attr ( "height" ) ,
54+ rx : shape . attr ( "rx" ) ? + shape . attr ( "rx" ) : undefined ,
55+ color : rgbToHex ( shape . style ( "stroke" ) ) || shape . attr ( "stroke" )
56+ } ;
57+ } ) ;
58+ } ;
3259
33- export const domPolylineToJSON = ( ) => { } ;
60+ export const domPolylineToJSON = ( ) => {
61+ return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Polyline } "]` ) . map ( ( polyline ) => {
62+ return {
63+ id : polyline . attr ( "id" ) ,
64+ points : polyline
65+ . attr ( "points" )
66+ . split ( " " )
67+ . map ( ( point ) => {
68+ const [ x , y ] = point . split ( "," ) ;
69+ return { x : + x , y : + y } ;
70+ } )
71+ } ;
72+ } ) ;
73+ } ;
3474
35- export const domImagesToJSON = ( ) => { } ;
75+ export const domImagesToJSON = ( ) => {
76+ return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Image } "]` ) . map ( ( image ) => {
77+ return {
78+ id : image . attr ( "id" ) ,
79+ x : + image . attr ( "x" ) ,
80+ y : + image . attr ( "y" ) ,
81+ width : + image . attr ( "width" ) ,
82+ height : + image . attr ( "height" ) ,
83+ href : image . attr ( "href" )
84+ } ;
85+ } ) ;
86+ } ;
3687
3788export const stateToJSON = ( ) => {
3889 const state = store . getState ( ) . editor ;
@@ -41,6 +92,10 @@ export const stateToJSON = () => {
4192 categories : state . categories ,
4293 sections : state . sections ,
4394 seats : domSeatsToJSON ( ) ,
44- booths : domBoothsToJSON ( )
95+ booths : domBoothsToJSON ( ) ,
96+ text : domTextToJSON ( ) ,
97+ shapes : domShapesToJSON ( ) ,
98+ polyline : domPolylineToJSON ( ) ,
99+ images : domImagesToJSON ( )
45100 } ;
46101} ;
0 commit comments