Skip to content

Commit 86f2d88

Browse files
committed
Refactor: workspace load
1 parent 32d76ef commit 86f2d88

File tree

6 files changed

+58
-41
lines changed

6 files changed

+58
-41
lines changed

src/components/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { twMerge } from "tailwind-merge";
2-
import { useEvents, useInteractions } from "@/hooks";
2+
import { useDesignerEvents, useInteractions, useUserEvents } from "@/hooks";
33
import { type ISTKProps } from "@/types";
44
import { default as Controls } from "./controls";
55
import { default as Footer } from "./footer";
@@ -10,7 +10,7 @@ import { Cursor, default as Workspace } from "./workspace";
1010
export * from "./core";
1111

1212
const Designer: React.FC<ISTKProps> = (props) => {
13-
useEvents();
13+
useDesignerEvents(props);
1414
useInteractions();
1515
return (
1616
<>
@@ -35,6 +35,7 @@ const Designer: React.FC<ISTKProps> = (props) => {
3535
};
3636

3737
const User: React.FC<ISTKProps> = (props) => {
38+
useUserEvents(props);
3839
return (
3940
<div
4041
className={twMerge("h-full min-h-[85vh] flex flex-col relative", props.styles?.root?.className)}

src/components/workspace/index.tsx

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { useCallback, useLayoutEffect } from "react";
1+
import { useCallback } from "react";
22
import { useSelector } from "react-redux";
33
import { twMerge } from "tailwind-merge";
4-
import { ids, selectors } from "@/constants";
5-
import { store } from "@/store";
6-
import { initializeElements, sync } from "@/store/reducers/editor";
4+
import { ids } from "@/constants";
75
import { type ISTKProps } from "@/types";
8-
import { d3Extended } from "@/utils";
96
import { Tool, tools } from "../toolbar/data";
107
import { default as Crosshairs } from "./crosshairs";
118
import { default as Element, ElementType } from "./elements";
129
import { default as Grid } from "./grid";
13-
import { default as Zoom, zoomAndPan } from "./zoom";
10+
import { default as Zoom } from "./zoom";
1411

1512
export { default as Cursor } from "./cursor";
1613

@@ -27,31 +24,6 @@ export const Workspace: React.FC<ISTKProps> = (props) => {
2724
const selectedPolylineId = useSelector((state: any) => state.editor.selectedPolylineId);
2825
const selectedTool = useSelector((state: any) => state.toolbar.selectedTool);
2926

30-
useLayoutEffect(() => {
31-
if (props.data) {
32-
store.dispatch(sync(props.data));
33-
setTimeout(() => {
34-
const { height: workspaceheight, width: workspaceWidth } = d3Extended.selectionBounds(
35-
d3Extended.selectById(ids.workspace)
36-
);
37-
const {
38-
left: wgOffsetLeft,
39-
top: wgOffsetTop,
40-
height: workspaceGroupHeight,
41-
width: workspaceGroupWidth
42-
} = d3Extended.selectionBounds(d3Extended.select(selectors.workspaceGroup));
43-
const scaleFactor = 1.05;
44-
zoomAndPan({
45-
k: scaleFactor,
46-
y: (workspaceheight - (wgOffsetTop * scaleFactor * 2 + workspaceGroupHeight * scaleFactor)) / 2 - 5,
47-
x: (workspaceWidth - (wgOffsetLeft * scaleFactor * 2 + workspaceGroupWidth * scaleFactor)) / 2
48-
});
49-
}, 0);
50-
} else {
51-
store.dispatch(initializeElements());
52-
}
53-
}, [props.data]);
54-
5527
const elementProps = useCallback(
5628
(elem) => ({
5729
id: elem.id,

src/hooks/events/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import useDeselection from "./deselection";
2-
import useDuplicate from "./duplication";
3-
import usePolyline from "./polyline";
4-
import useWorkspaceClick from "./workspace-click";
1+
import { ISTKProps } from "@/types";
2+
import { default as useDeselection } from "./deselection";
3+
import { default as useDuplicate } from "./duplication";
4+
import { default as usePolyline } from "./polyline";
5+
import { default as useWorkspaceClick } from "./workspace-click";
6+
import { default as useWorkspaceLoad } from "./workspace-load";
57

6-
const useEvents = () => {
8+
export const useDesignerEvents = (props: ISTKProps) => {
79
useDeselection();
10+
useDuplicate();
811
usePolyline();
912
useWorkspaceClick();
10-
useDuplicate();
13+
useWorkspaceLoad(props);
1114
};
1215

13-
export default useEvents;
16+
export const useUserEvents = (props: ISTKProps) => {
17+
useWorkspaceLoad(props);
18+
};

src/hooks/events/workspace-load.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useLayoutEffect } from "react";
2+
import { zoomAndPan } from "@/components/workspace/zoom";
3+
import { ids, selectors } from "@/constants";
4+
import { store } from "@/store";
5+
import { initializeElements, sync } from "@/store/reducers/editor";
6+
import { ISTKProps } from "@/types";
7+
import { d3Extended } from "@/utils";
8+
9+
const useWorkspaceLoad = (props: ISTKProps) => {
10+
useLayoutEffect(() => {
11+
if (props.data) {
12+
store.dispatch(sync(props.data));
13+
setTimeout(() => {
14+
const { height: workspaceheight, width: workspaceWidth } = d3Extended.selectionBounds(
15+
d3Extended.selectById(ids.workspace)
16+
);
17+
const {
18+
left: wgOffsetLeft,
19+
top: wgOffsetTop,
20+
height: workspaceGroupHeight,
21+
width: workspaceGroupWidth
22+
} = d3Extended.selectionBounds(d3Extended.select(selectors.workspaceGroup));
23+
const scaleFactor = 1.05;
24+
zoomAndPan({
25+
k: scaleFactor,
26+
y: (workspaceheight - (wgOffsetTop * scaleFactor * 2 + workspaceGroupHeight * scaleFactor)) / 2 - 5,
27+
x: (workspaceWidth - (wgOffsetLeft * scaleFactor * 2 + workspaceGroupWidth * scaleFactor)) / 2
28+
});
29+
}, 0);
30+
} else {
31+
store.dispatch(initializeElements());
32+
}
33+
props.events?.onWorkspaceLoad?.();
34+
}, [props.data]);
35+
};
36+
37+
export default useWorkspaceLoad;

src/hooks/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as useBreakpoint } from "./breakpoint";
2-
export { default as useEvents } from "./events";
32
export { default as useInteractions } from "./interactions";
43
export { default as useResize } from "./resize";
54
export { default as useSkipFirstRender } from "./skip-first-render";
5+
6+
export * from "./events";

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface IEvents {
1919
onSeatClick?: (seat: IPopulatedSeat) => void;
2020
onSectionClick?: (section: ISection) => void;
2121
onExport?: (data: ISTKData) => void;
22+
onWorkspaceLoad?: () => void;
2223
}
2324

2425
export interface ISTKData {

0 commit comments

Comments
 (0)