File size: 12,348 Bytes
3d7d9b5 | 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | import React, { useState, useRef, useEffect } from "react";
import { useAppStore } from "../store";
import { TextNote } from "../types";
import {
AlignLeft,
AlignCenter,
AlignRight,
Bold,
Italic,
Underline,
List,
ListOrdered,
Link,
} from "lucide-react";
export const TextNoteNode = ({ note }: { note: TextNote }) => {
const {
setTextNotes,
zoom,
pan,
isAnnotationMode,
isClickThrough,
selectedNodeIds,
setSelectedNodeIds,
updateSelectedNodes,
} = useAppStore();
const [isDragging, setIsDragging] = useState(false);
const [isEditing, setIsEditing] = useState(false);
const [localText, setLocalText] = useState(note.text);
const contentEditableRef = useRef<HTMLDivElement>(null);
const isSelected = selectedNodeIds.includes(note.id);
useEffect(() => {
if (isEditing && contentEditableRef.current) {
if (contentEditableRef.current.innerHTML !== localText) {
contentEditableRef.current.innerHTML = localText;
}
contentEditableRef.current.focus();
}
}, [isEditing]);
const updateNote = (changes: Partial<TextNote>) => {
setTextNotes((prev) =>
prev.map((n) => (n.id === note.id ? { ...n, ...changes } : n)),
);
};
const handlePointerDown = (e: React.PointerEvent) => {
if (isClickThrough || isAnnotationMode) return;
if (isEditing) return; // let user click inside text
if ((e.target as HTMLElement).tagName.toLowerCase() === "a") {
// let the link click happen
e.stopPropagation();
return;
}
e.stopPropagation();
if (e.button === 2) {
return;
}
if (!isSelected) {
setTextNotes((prev) => {
let idsToSelect = [note.id];
if (note.groupId) {
idsToSelect = prev
.filter((n) => n.groupId === note.groupId)
.map((n) => n.id);
}
if (e.shiftKey) {
setSelectedNodeIds((sel) =>
Array.from(new Set([...sel, ...idsToSelect])),
);
} else {
setSelectedNodeIds(idsToSelect);
}
return prev;
});
}
setIsDragging(true);
e.currentTarget.setPointerCapture(e.pointerId);
};
const handlePointerMoveRoot = (e: React.PointerEvent) => {
if (isResizing) {
handleResizeMove(e);
} else if (isDragging) {
e.stopPropagation();
updateSelectedNodes(e.movementX / zoom, e.movementY / zoom, note.id);
}
};
const handlePointerUpRoot = (e: React.PointerEvent) => {
if (isResizing) {
handleResizeEnd(e);
} else if (isDragging) {
setIsDragging(false);
e.currentTarget.releasePointerCapture(e.pointerId);
}
};
const handleDoubleClick = (e: React.MouseEvent) => {
e.stopPropagation();
setIsEditing(true);
};
const handleBlur = () => {
setIsEditing(false);
const newText = contentEditableRef.current?.innerHTML || "";
setLocalText(newText);
updateNote({ text: newText });
};
const handleKeyDown = (e: React.KeyboardEvent) => {
e.stopPropagation(); // prevent app-level shortcuts
if (e.key === "Escape") {
handleBlur();
}
};
const [isResizing, setIsResizing] = useState(false);
const handleResizeStart = (e: React.PointerEvent) => {
e.stopPropagation();
setIsResizing(true);
e.currentTarget.setPointerCapture(e.pointerId);
};
const handleResizeMove = (e: React.PointerEvent) => {
if (isResizing) {
e.stopPropagation();
updateNote({
width: Math.max(100, note.width + e.movementX / zoom),
height: Math.max(50, (note.height || 80) + e.movementY / zoom),
});
}
};
const handleResizeEnd = (e: React.PointerEvent) => {
if (isResizing) {
setIsResizing(false);
e.currentTarget.releasePointerCapture(e.pointerId);
}
};
const colors = ["#FFFFFF", "#FFD60A", "#FF453A", "#32D74B", "#0A84FF"];
const fonts = ["Inter", "Courier New", "Times New Roman", "Georgia", "Arial"];
// Apply default styles
const alignment = note.alignment || "left";
const isBold = note.isBold || false;
const isItalic = note.isItalic || false;
const isUnderline = note.isUnderline || false;
const color = note.color || "#FFFFFF";
const bgColor = note.bgColor || "rgba(0,0,0,0.6)";
const fontSize = note.fontSize || 14;
const fontFamily = note.fontFamily || "Inter";
return (
<div
className="absolute group"
style={{
transform: `translate(${note.x}px, ${note.y}px)`,
width: note.width,
height: note.height || "auto",
zIndex: 20,
pointerEvents: isClickThrough || isAnnotationMode ? "none" : "auto",
}}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMoveRoot}
onPointerUp={handlePointerUpRoot}
onDoubleClick={handleDoubleClick}
onContextMenu={(e) => e.preventDefault()}
>
{/* Floating Toolbar */}
{isEditing && (
<div
className="absolute bottom-full mb-2 left-0 flex items-center gap-1.5 p-1.5 bg-[#2A2A2E] border border-[#3A3A3E] rounded-md shadow-xl pointer-events-auto z-50 text-ui-secondary text-sm"
onPointerDown={(e) => {
e.preventDefault();
e.stopPropagation();
}}
>
<div className="flex items-center pr-1 border-r border-[#3A3A3E]">
<select
value={fontFamily}
onChange={(e) => updateNote({ fontFamily: e.target.value })}
className="bg-transparent text-white text-xs outline-none cursor-pointer py-1 max-w-[100px] font-sans"
>
{fonts.map((f) => (
<option key={f} value={f} className="bg-[#2A2A2E]">
{f}
</option>
))}
</select>
</div>
<button
onClick={() => document.execCommand("bold")}
className={`p-1 pl-2 rounded hover:bg-white/10 hover:text-white`}
>
<Bold size={14} />
</button>
<button
onClick={() => document.execCommand("italic")}
className={`p-1 rounded hover:bg-white/10 hover:text-white`}
>
<Italic size={14} />
</button>
<button
onClick={() => document.execCommand("underline")}
className={`p-1 rounded hover:bg-white/10 hover:text-white`}
>
<Underline size={14} />
</button>
<button
onClick={() => {
const url = prompt("Enter link URL:");
if (url) document.execCommand("createLink", false, url);
}}
className={`p-1 pr-2 rounded hover:bg-white/10 hover:text-white`}
>
<Link size={14} />
</button>
<div className="w-[1px] h-4 bg-[#3A3A3E]"></div>
<button
onClick={() => document.execCommand("insertUnorderedList")}
className={`p-1 pl-2 rounded hover:bg-white/10 hover:text-white`}
>
<List size={14} />
</button>
<button
onClick={() => document.execCommand("insertOrderedList")}
className={`p-1 pr-2 rounded hover:bg-white/10 hover:text-white`}
>
<ListOrdered size={14} />
</button>
<div className="w-[1px] h-4 bg-[#3A3A3E]"></div>
<button
onClick={() => updateNote({ alignment: "left" })}
className={`p-1 pl-2 rounded hover:bg-white/10 ${alignment === "left" ? "text-white" : ""}`}
>
<AlignLeft size={14} />
</button>
<button
onClick={() => updateNote({ alignment: "center" })}
className={`p-1 rounded hover:bg-white/10 ${alignment === "center" ? "text-white" : ""}`}
>
<AlignCenter size={14} />
</button>
<button
onClick={() => updateNote({ alignment: "right" })}
className={`p-1 pr-2 rounded hover:bg-white/10 ${alignment === "right" ? "text-white" : ""}`}
>
<AlignRight size={14} />
</button>
<div className="w-[1px] h-4 bg-[#3A3A3E]"></div>
<div className="flex items-center gap-1 px-1">
<button
onClick={() =>
updateNote({ fontSize: Math.max(10, fontSize - 2) })
}
className="hover:text-white px-1"
>
-
</button>
<span className="text-white text-xs w-6 text-center select-none">
{fontSize}
</span>
<button
onClick={() =>
updateNote({ fontSize: Math.min(72, fontSize + 2) })
}
className="hover:text-white px-1"
>
+
</button>
</div>
<div className="w-[1px] h-4 bg-[#3A3A3E]"></div>
<div className="flex items-center gap-1.5 pl-1 pr-1">
{colors.map((c) => (
<button
key={c}
onClick={() => updateNote({ color: c })}
className={`w-3.5 h-3.5 rounded-full ${color === c ? "ring-1 ring-offset-1 ring-offset-[#2A2A2E] ring-white" : ""} hover:scale-110 transition-transform`}
style={{ backgroundColor: c }}
/>
))}
</div>
</div>
)}
<div
className={`p-4 rounded-xl shadow-xl transition-all duration-200 ${isEditing ? "ring-1 ring-white/20 bg-black/80" : "hover:ring-1 hover:ring-white/10 bg-black/40 backdrop-blur-sm"}`}
style={{
backgroundColor: isEditing ? "#1A1A1A" : bgColor,
}}
>
{isEditing ? (
<div
ref={contentEditableRef}
className="w-full h-full bg-transparent outline-none hide-scrollbar overflow-y-auto min-h-[50px] rich-text-editor"
style={{
color: color,
fontSize: `${fontSize}px`,
fontFamily: fontFamily,
textAlign: alignment,
}}
contentEditable
suppressContentEditableWarning
onBlur={handleBlur}
onKeyDown={handleKeyDown}
onPointerDown={(e) => e.stopPropagation()}
/>
) : (
<div
className="select-none break-words h-full rich-text-editor"
style={{
color: color,
fontSize: `${fontSize}px`,
fontFamily: fontFamily,
textAlign: alignment,
}}
dangerouslySetInnerHTML={{
__html:
localText ||
'<span class="text-white/30 italic">Double click to edit text</span>',
}}
/>
)}
{/* Delete button (only visible on hover) */}
{!isEditing && (
<button
className="absolute -top-2 -right-2 w-6 h-6 bg-[#FF453A] border hover:bg-red-500 border-white text-white rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity shadow-lg z-30"
onClick={(e) => {
e.stopPropagation();
setTextNotes((prev) => prev.filter((n) => n.id !== note.id));
}}
>
<div className="w-2.5 h-[1.5px] bg-white rounded-full translate-y-[-0.5px]"></div>
</button>
)}
{/* Resize Handle (only visible on hover/editing) */}
<div
className={`absolute bottom-0 right-0 w-4 h-4 cursor-nwse-resize opacity-0 ${isEditing ? "opacity-100" : "group-hover:opacity-100"} transition-opacity z-30 flex items-end justify-end p-1`}
onPointerDown={handleResizeStart}
onPointerUp={handleResizeEnd}
onPointerMove={handleResizeMove}
>
<div className="w-2 h-2 rounded-tl-sm bg-white/50 border-b border-r border-white/80"></div>
</div>
</div>
</div>
);
};
|