-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextNode.tsx
More file actions
212 lines (195 loc) · 5.95 KB
/
TextNode.tsx
File metadata and controls
212 lines (195 loc) · 5.95 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import React, { useEffect, useState } from "react";
import {
Handle,
NodeProps,
NodeResizer,
NodeToolbar,
Position,
useReactFlow,
} from "@xyflow/react";
import {
getFontStyle,
handleStyle,
NodeProperties,
} from "@/types/NodeProperties";
import StyleBar from "@/components/style-bar/StyleBar";
import { updateNode } from "@/util/updateNode";
import { useAmIOwner } from "@/hooks/api/whiteboard.api";
import { useGetMe } from "@/hooks/api/account.api";
import { useParams } from "next/navigation";
interface TextNodeProps extends NodeProps {
id: string;
data: {
label?: string;
nodeProperties: NodeProperties;
};
selected: boolean;
}
function hexToRgb(hex: string) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
: null;
}
export default function TextNode({ id, data, selected }: TextNodeProps) {
const [isEditing, setIsEditing] = useState(false);
const [text, setText] = useState<string>(data.label as string);
const { setNodes } = useReactFlow();
const params = useParams();
const whiteboardId = Number(params.id);
const { data: user } = useGetMe();
const { data: isOwner } = useAmIOwner(whiteboardId, user?.id);
const { nodeProperties, label } = data;
const bgRgb = hexToRgb(nodeProperties.color);
const borderRgb = hexToRgb(nodeProperties.borderColor);
const showBorder = borderRgb && nodeProperties.borderColor !== "none";
const showBackground = bgRgb && nodeProperties.color !== "none";
const onUpdateNode = (updater: {
label?: string;
nodeProperties?: Partial<NodeProperties>;
}) => {
setNodes(updateNode(id, updater));
};
const handleTextChange = (newText: string) => {
setText(newText);
onUpdateNode({ label: newText });
};
useEffect(() => {
setText(data.label as string);
}, [data.label]);
return (
<>
{isOwner && (
<NodeToolbar isVisible={selected} position={Position.Top}>
<StyleBar
nodeProperties={nodeProperties}
onUpdateNode={(updatedProperties: Partial<NodeProperties>) =>
onUpdateNode({ nodeProperties: updatedProperties })
}
onUpdateLabel={(newLabel: string) =>
onUpdateNode({ label: newLabel })
}
selectedNodeLabel={text}
/>
</NodeToolbar>
)}
<NodeResizer
color="#3859ff"
isVisible={selected}
minWidth={100}
minHeight={50}
/>
<div className="h-full w-full">
<div
className="flex h-full w-full items-center justify-center p-4"
style={{
...(showBorder && {
borderWidth: nodeProperties.borderWidth,
borderStyle: "solid",
borderColor: `rgba(${borderRgb.r!}, ${borderRgb.g!}, ${borderRgb.b!}, ${nodeProperties.borderOpacity})`,
}),
...(showBackground && {
backgroundColor: `rgba(${bgRgb.r!}, ${bgRgb.g!}, ${bgRgb.b!}, ${nodeProperties.opacity})`,
}),
}}
onDoubleClick={(e) => {
e.stopPropagation();
setIsEditing(true);
}}
>
{isEditing ? (
<input
value={text}
onChange={(e) => handleTextChange(e.target.value)}
onBlur={() => setIsEditing(false)}
onKeyDown={(e) => {
if (e.key === "Enter") {
setIsEditing(false);
}
}}
autoFocus
className="m-0 h-auto w-auto border-none bg-transparent p-0 text-center outline-none"
style={{
color: nodeProperties.textColor,
fontSize: `${nodeProperties.fontSize}px`,
fontFamily: getFontStyle(nodeProperties.fontFamily),
fontWeight: nodeProperties.isBold ? "bold" : "normal",
fontStyle: nodeProperties.isItalic ? "italic" : "normal",
textDecoration:
`${nodeProperties.isUnderline ? "underline" : ""} ${nodeProperties.isStrikethrough ? "line-through" : ""}`.trim() ||
"none",
}}
/>
) : (
<p
style={{
color: nodeProperties.textColor,
fontSize: `${nodeProperties.fontSize}px`,
fontFamily: getFontStyle(nodeProperties.fontFamily),
fontWeight: nodeProperties.isBold ? "bold" : "normal",
fontStyle: nodeProperties.isItalic ? "italic" : "normal",
textDecoration:
`${nodeProperties.isUnderline ? "underline" : ""} ${nodeProperties.isStrikethrough ? "line-through" : ""}`.trim() ||
"none",
}}
>
{label}
</p>
)}
</div>
</div>
<Handle
type="source"
id="top-source"
position={Position.Top}
style={handleStyle}
/>
<Handle
type="target"
id="top-target"
position={Position.Top}
style={handleStyle}
/>
<Handle
type="source"
id="bottom-source"
position={Position.Bottom}
style={handleStyle}
/>
<Handle
type="target"
id="bottom-target"
position={Position.Bottom}
style={handleStyle}
/>
<Handle
type="source"
id="left-source"
position={Position.Left}
style={handleStyle}
/>
<Handle
type="target"
id="left-target"
position={Position.Left}
style={handleStyle}
/>
<Handle
type="source"
id="right-source"
position={Position.Right}
style={handleStyle}
/>
<Handle
type="target"
id="right-target"
position={Position.Right}
style={handleStyle}
/>
</>
);
}