-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.tsx
129 lines (117 loc) · 3.38 KB
/
Project.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useState } from "react";
import { Elements, FlowTransform } from "react-flow-renderer";
import { v4 as uuidv4 } from "uuid";
import { useProject } from "context/ProjectContext";
export interface ProjectState {
elements: Elements;
id: string;
onChangeElementFactory: (id: string) => (data: Record<string, any>) => void;
setElements: Dispatch<SetStateAction<Elements>>;
setId: Dispatch<SetStateAction<string>>;
setTransform: Dispatch<SetStateAction<FlowTransform>>;
transform: FlowTransform;
}
const textareaStyles: React.CSSProperties = {
fontSize: 12,
height: "100%",
resize: "none",
width: "100%",
};
const controlsStyles: React.CSSProperties = {
display: "flex",
position: "absolute",
right: "100%",
top: -10,
transform: "rotate(-90deg)",
transformOrigin: "bottom right",
};
const getDrawerStyles = (visible: boolean): React.CSSProperties => ({
height: "100%",
padding: 10,
position: "absolute",
right: 0,
top: 0,
transform: visible ? "translateX(0)" : "translateX(100%)",
transition: "transform 0.4s ease",
width: 400,
});
export const getDefaultProject = () => ({
id: uuidv4(),
elements: [],
transform: {
x: 0,
y: 0,
zoom: 1,
},
});
function Project() {
const { elements, id, setElements, setId, setTransform, transform } = useProject();
const [visible, setVisible] = useState(false);
const drawerStyles = useMemo(() => getDrawerStyles(visible), [visible]);
// Load project from URL
useEffect(() => {
const project = atob(window.location.hash.substr(1));
try {
const { elements, id, transform } = JSON.parse(project);
setElements(elements);
setId(id ?? uuidv4());
setTransform(transform);
} catch (e) {
console.error(e);
}
}, [setElements, setId, setTransform]);
// Store project in URL
useEffect(() => {
window.location.hash = btoa(
JSON.stringify({
elements: elements.map(element => ({ ...element, __rf: undefined })),
id,
transform,
})
);
}, [elements, id, transform]);
const onChange = useCallback(
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
try {
const { elements, id, transform } = JSON.parse(e.target.value);
setElements(elements);
setId(id ?? uuidv4());
setTransform(transform);
} catch (e) {
console.error(e);
}
},
[setElements, setId, setTransform]
);
const clearProject = useCallback(() => {
const defaultProject = getDefaultProject();
setElements(defaultProject.elements);
setId(defaultProject.id);
setTransform(defaultProject.transform);
}, [setElements, setId, setTransform]);
const toggleProjectDrawer = useCallback(() => setVisible(visible => !visible), []);
return (
<div style={drawerStyles}>
<textarea
onChange={onChange}
style={textareaStyles}
value={JSON.stringify(
{
elements: elements.map(element => ({ ...element, __rf: undefined })),
id,
transform,
},
null,
2
)}
/>
<div style={controlsStyles}>
<button onClick={clearProject} style={{ marginRight: 10 }}>
clear
</button>
<button onClick={toggleProjectDrawer}>{visible ? "hide" : "show"}</button>
</div>
</div>
);
}
export default Project;