Skip to content

Commit 59ae675

Browse files
committed
Feat: initiated json transformer
1 parent 9dcfdd2 commit 59ae675

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

src/components/operations/index.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ids } from "@/constants";
55
import { useBreakpoint } from "@/hooks";
66
import { store } from "@/store";
77
import { locationPlaceholder, setLocation, toggleControls } from "@/store/reducers/editor";
8+
import { stateToJSON } from "@/utils";
89
import { Body, Button, IconButton } from "../core";
910
import GridSwitch from "./grid-switch";
1011

@@ -26,6 +27,10 @@ const Operations = () => {
2627
store.dispatch(setLocation(location));
2728
};
2829

30+
const onExportJson = () => {
31+
console.log(stateToJSON());
32+
};
33+
2934
return (
3035
<div
3136
id={ids.operationBar}
@@ -45,12 +50,14 @@ const Operations = () => {
4550
{md ? (
4651
<>
4752
<Button className="py-[0.35rem]">Preview</Button>
48-
<Button className="py-[0.35rem]">Export JSON</Button>
53+
<Button className="py-[0.35rem]" onClick={onExportJson}>
54+
Export JSON
55+
</Button>
4956
</>
5057
) : (
5158
<>
5259
<IconButton icon={<Eye />} label="Preview" />
53-
<IconButton icon={<Braces />} label="Export JSON" />
60+
<IconButton icon={<Braces />} label="Export JSON" onClick={onExportJson} />
5461
</>
5562
)}
5663
<Cog

src/components/workspace/elements/seat.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const Seat = forwardRef(({ x, y, id, label, categories, category, status, ...pro
4242
cy={y}
4343
r={seatSize / 2}
4444
{...{ [dataAttributes.category]: category }}
45-
{...{ [dataAttributes.status]: status }}
45+
{...{ [dataAttributes.status]: status ?? SeatStatus.Available }}
4646
{...props}
4747
/>
4848
{label && (
@@ -54,6 +54,7 @@ const Seat = forwardRef(({ x, y, id, label, categories, category, status, ...pro
5454
fontWeight={200}
5555
letterSpacing={1}
5656
{...props}
57+
{...{ [dataAttributes.elementType]: undefined }}
5758
className={twMerge(props.className, "unselectable !stroke-1")}
5859
>
5960
{label ?? "A"}

src/utils/d3.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare module "d3" {
44
interface Selection<GElement extends d3.BaseType, Datum, PElement extends d3.BaseType, PDatum> {
55
moveToBack(): Selection<GElement, Datum, PElement, PDatum>;
66
moveToFront(): Selection<GElement, Datum, PElement, PDatum>;
7+
map<T>(callback: (d: Selection<GElement, Datum, PElement, PDatum>, i: number) => T): T[];
78
}
89
}
910

@@ -22,6 +23,14 @@ d3.selection.prototype.moveToBack = function () {
2223
});
2324
};
2425

26+
d3.selection.prototype.map = function (callback) {
27+
const results = [];
28+
this.each(function (_, i) {
29+
results.push(callback(d3.select(this), i));
30+
});
31+
return results;
32+
};
33+
2534
export const d3Extended = {
2635
...d3,
2736
selectById(id: string): d3.Selection<d3.BaseType, {}, HTMLElement, any> {

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as d3 from "d3";
22
import { ids, selectors } from "@/constants";
33

44
export * from "./d3";
5+
export * from "./transformer";
56
export * from "./workspace";
67

78
export const fallible = (fn: Function) => {

src/utils/transformer.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ElementType } from "@/components/workspace/elements";
2+
import { dataAttributes } from "@/constants";
3+
import { store } from "@/store";
4+
import d3Extended from "./d3";
5+
6+
export const domSeatsToJSON = () => {
7+
return d3Extended.selectAll(`[${dataAttributes.elementType}="${ElementType.Seat}"]`).map((seat) => {
8+
return {
9+
id: seat.attr("id"),
10+
x: +seat.attr("cx"),
11+
y: +seat.attr("cy"),
12+
label: document.getElementById(`${seat.attr("id")}-label`)?.textContent,
13+
status: seat.attr(dataAttributes.status),
14+
category: seat.attr(dataAttributes.category)
15+
};
16+
});
17+
};
18+
19+
export const domBoothsToJSON = () => {
20+
return d3Extended.selectAll(`[${dataAttributes.elementType}="${ElementType.Booth}"]`).map((booth) => {
21+
return {
22+
id: booth.attr("id"),
23+
x: +booth.attr("cx"),
24+
y: +booth.attr("cy")
25+
};
26+
});
27+
};
28+
29+
export const domTextsToJSON = () => {};
30+
31+
export const domShapesToJSON = () => {};
32+
33+
export const domPolylineToJSON = () => {};
34+
35+
export const domImagesToJSON = () => {};
36+
37+
export const stateToJSON = () => {
38+
const state = store.getState().editor;
39+
return {
40+
name: state.location,
41+
categories: state.categories,
42+
sections: state.sections,
43+
seats: domSeatsToJSON(),
44+
booths: domBoothsToJSON()
45+
};
46+
};

0 commit comments

Comments
 (0)