11import { ElementType } from "@/components/workspace/elements" ;
2- import { dataAttributes , selectors } from "@/constants" ;
2+ import { dataAttributes } from "@/constants" ;
33import { store } from "@/store" ;
4- import { ISTKData } from "@/types" ;
4+ import { ISTKData , ISeat } from "@/types" ;
55import { rgbToHex } from "." ;
66import { default as d3Extended } from "./d3" ;
77
8- export const domSeatsToJSON = ( ) => {
8+ const domSeatsToJSON = ( seatsFromStore : ISeat [ ] | Record < string , ISeat > ) => {
9+ seatsFromStore = ( seatsFromStore as ISeat [ ] ) . reduce ( ( acc , seat ) => {
10+ acc [ seat . id ] = seat ;
11+ return acc ;
12+ } , { } ) ;
913 return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Seat } "]` ) . map ( ( seat ) => {
14+ const id = seat . attr ( "id" ) ;
1015 const square = ( seat . node ( ) as any ) ?. nodeName === "rect" ;
1116 return {
12- id : seat . attr ( "id" ) ,
17+ id,
1318 x : + seat . attr ( square ? "x" : "cx" ) ,
1419 y : + seat . attr ( square ? "y" : "cy" ) ,
15- label : document . getElementById ( `${ seat . attr ( "id" ) } -label` ) ?. textContent ,
20+ label :
21+ document . getElementById ( `${ seat . attr ( "id" ) } -label` ) ?. textContent ?. trim ( ) ?? seatsFromStore [ id ] ?. label ?. trim ( ) ,
1622 status : seat . attr ( dataAttributes . status ) ,
1723 category : seat . attr ( dataAttributes . category ) ,
1824 square,
@@ -21,7 +27,7 @@ export const domSeatsToJSON = () => {
2127 } ) ;
2228} ;
2329
24- export const domTextToJSON = ( ) => {
30+ const domTextToJSON = ( ) => {
2531 return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Text } "]` ) . map ( ( text ) => {
2632 return {
2733 id : text . attr ( "id" ) ,
@@ -38,7 +44,7 @@ export const domTextToJSON = () => {
3844 } ) ;
3945} ;
4046
41- export const domShapesToJSON = ( ) => {
47+ const domShapesToJSON = ( ) => {
4248 return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Shape } "]` ) . map ( ( shape ) => {
4349 return {
4450 id : shape . attr ( "id" ) ,
@@ -55,7 +61,7 @@ export const domShapesToJSON = () => {
5561 } ) ;
5662} ;
5763
58- export const domPolylineToJSON = ( ) => {
64+ const domPolylineToJSON = ( ) => {
5965 return d3Extended
6066 . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Polyline } "]` )
6167 . map ( ( polyline ) => {
@@ -77,7 +83,7 @@ export const domPolylineToJSON = () => {
7783 . filter ( ( polyline ) => polyline . points . length > 1 ) ;
7884} ;
7985
80- export const domImagesToJSON = ( ) => {
86+ const domImagesToJSON = ( ) => {
8187 return d3Extended . selectAll ( `[${ dataAttributes . elementType } ="${ ElementType . Image } "]` ) . map ( ( image ) => {
8288 return {
8389 id : image . attr ( "id" ) ,
@@ -92,17 +98,13 @@ export const domImagesToJSON = () => {
9298 } ) ;
9399} ;
94100
95- export const domTransform = ( ) => {
96- return d3Extended . zoomTransform ( document . querySelector ( selectors . workspaceGroup ) ) ;
97- } ;
98-
99101export const stateToJSON = ( ) : ISTKData => {
100102 const state = store . getState ( ) . editor ;
101103 return {
102104 name : state . location ,
103105 categories : state . categories . slice ( 1 ) ,
104106 sections : state . sections . slice ( 1 ) ,
105- seats : domSeatsToJSON ( ) ,
107+ seats : domSeatsToJSON ( state . seats ) ,
106108 text : domTextToJSON ( ) ,
107109 shapes : domShapesToJSON ( ) ,
108110 polylines : domPolylineToJSON ( ) ,
0 commit comments