Skip to content

Commit a2a4e72

Browse files
committed
Feat: added remaining json transformers
1 parent 59ae675 commit a2a4e72

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

src/components/workspace/elements/shape.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const Shape = forwardRef(({ x, y, id, name, width, height, rx, resizable, classN
3636
ref={ref}
3737
x={x}
3838
y={y}
39-
size={shapeSize}
39+
width={width ?? shapeSize}
40+
height={height ?? shapeSize}
4041
className={twMerge(className, "stroke-[0.75] fill-transparent")}
4142
stroke={color}
4243
{...{ [dataAttributes.shape]: name }}

src/utils/transformer.ts

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { ElementType } from "@/components/workspace/elements";
22
import { dataAttributes } from "@/constants";
33
import { store } from "@/store";
4-
import d3Extended from "./d3";
4+
import { rgbToHex } from ".";
5+
import { default as d3Extended } from "./d3";
56

67
export 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

3788
export 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

Comments
 (0)