|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { NodeProps } from '@xyflow/react'; |
| 3 | +import { Bold, Italic, Underline, Settings, Eye, EyeOff } from 'lucide-react'; |
| 4 | +import { Button } from '@/components/ui/button'; |
| 5 | + |
| 6 | +interface TextNodeStyle { |
| 7 | + color?: string; |
| 8 | +} |
| 9 | + |
| 10 | +export function TextNode({ data }: NodeProps) { |
| 11 | + const [isEditing, setIsEditing] = useState(false); |
| 12 | + const [text, setText] = useState<string>(data.label as string); |
| 13 | + const [style, setStyle] = useState<TextNodeStyle>(data.style || {}); |
| 14 | + const [isBold, setIsBold] = useState<boolean>(false); |
| 15 | + const [isItalic, setIsItalic] = useState<boolean>(false); |
| 16 | + const [isUnderline, setIsUnderline] = useState<boolean>(false); |
| 17 | + const [showFormatting, setShowFormatting] = useState(false); |
| 18 | + const [showSettings, setShowSettings] = useState(true); |
| 19 | + |
| 20 | + const updateStyle = () => { |
| 21 | + const computedStyle: React.CSSProperties = { |
| 22 | + fontWeight: isBold ? 'bold' : 'normal', |
| 23 | + fontStyle: isItalic ? 'italic' : 'normal', |
| 24 | + textDecoration: isUnderline ? 'underline' : 'none', |
| 25 | + color: style.color || '#000000', |
| 26 | + }; |
| 27 | + data.style = computedStyle; |
| 28 | + return computedStyle; |
| 29 | + }; |
| 30 | + |
| 31 | + const handleColorChange = (color: string) => { |
| 32 | + const newStyle = { ...style, color }; |
| 33 | + setStyle(newStyle); |
| 34 | + }; |
| 35 | + |
| 36 | + return ( |
| 37 | + <div className="relative group"> |
| 38 | + <Button |
| 39 | + variant="ghost" |
| 40 | + size="sm" |
| 41 | + onClick={() => setShowSettings(!showSettings)} |
| 42 | + className="absolute -right-10 -top-8 h-7 w-7 p-1 opacity-0 group-hover:opacity-100 transition-opacity" |
| 43 | + > |
| 44 | + {showSettings ? <Eye className="h-4 w-4" /> : <EyeOff className="h-4 w-4" />} |
| 45 | + </Button> |
| 46 | + |
| 47 | + {showSettings && ( |
| 48 | + <Button |
| 49 | + variant="ghost" |
| 50 | + size="sm" |
| 51 | + onClick={() => setShowFormatting(!showFormatting)} |
| 52 | + className="absolute -left-10 top-2 h-7 w-7 p-1" |
| 53 | + > |
| 54 | + <Settings className="h-4 w-4" /> |
| 55 | + </Button> |
| 56 | + )} |
| 57 | + |
| 58 | + {showFormatting && ( |
| 59 | + <div className="absolute -left-32 top-0 flex flex-col gap-1 p-2 bg-white rounded-md shadow-lg border border-gray-200 z-10"> |
| 60 | + <Button |
| 61 | + variant="ghost" |
| 62 | + size="sm" |
| 63 | + onClick={() => setIsBold(!isBold)} |
| 64 | + className={`h-7 w-7 p-1 ${isBold ? 'bg-slate-200' : ''}`} |
| 65 | + > |
| 66 | + <Bold className="h-4 w-4" /> |
| 67 | + </Button> |
| 68 | + <Button |
| 69 | + variant="ghost" |
| 70 | + size="sm" |
| 71 | + onClick={() => setIsItalic(!isItalic)} |
| 72 | + className={`h-7 w-7 p-1 ${isItalic ? 'bg-slate-200' : ''}`} |
| 73 | + > |
| 74 | + <Italic className="h-4 w-4" /> |
| 75 | + </Button> |
| 76 | + <Button |
| 77 | + variant="ghost" |
| 78 | + size="sm" |
| 79 | + onClick={() => setIsUnderline(!isUnderline)} |
| 80 | + className={`h-7 w-7 p-1 ${isUnderline ? 'bg-slate-200' : ''}`} |
| 81 | + > |
| 82 | + <Underline className="h-4 w-4" /> |
| 83 | + </Button> |
| 84 | + <input |
| 85 | + type="color" |
| 86 | + onChange={(e) => handleColorChange(e.target.value)} |
| 87 | + value={style.color || '#000000'} |
| 88 | + className="w-7 h-7 rounded cursor-pointer" |
| 89 | + /> |
| 90 | + </div> |
| 91 | + )} |
| 92 | + |
| 93 | + <div |
| 94 | + className="p-4 border rounded bg-white min-w-[100px] shadow-sm" |
| 95 | + onDoubleClick={(e) => { |
| 96 | + e.stopPropagation(); |
| 97 | + setIsEditing(true); |
| 98 | + }} |
| 99 | + > |
| 100 | + {isEditing ? ( |
| 101 | + <input |
| 102 | + type="text" |
| 103 | + value={text} |
| 104 | + onChange={(e) => setText(e.target.value)} |
| 105 | + onBlur={() => { |
| 106 | + setIsEditing(false); |
| 107 | + data.label = text; |
| 108 | + }} |
| 109 | + onKeyDown={(e) => { |
| 110 | + if (e.key === 'Enter') { |
| 111 | + setIsEditing(false); |
| 112 | + data.label = text; |
| 113 | + } |
| 114 | + }} |
| 115 | + autoFocus |
| 116 | + className="bg-transparent outline-none w-full border-none p-0" |
| 117 | + style={updateStyle()} |
| 118 | + /> |
| 119 | + ) : ( |
| 120 | + <p style={updateStyle()}>{text}</p> |
| 121 | + )} |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + ); |
| 125 | +} |
0 commit comments