Skip to content

Commit bdad5f2

Browse files
committed
Add delete functionality to PathEditorToolbar
- Introduced a delete button to remove paths from the editor. - Implemented keyboard shortcuts for deleting paths using the Delete and Backspace keys. - Enhanced keydown event handling to prevent triggering actions while typing in input fields.
1 parent abeba39 commit bdad5f2

1 file changed

Lines changed: 40 additions & 26 deletions

File tree

components/path-editor-toolbar.tsx

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,7 @@ import { Label } from '@/components/ui/label';
77
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
88
import { Slider } from '@/components/ui/slider';
99
import { ColorInput } from '@/components/color-input';
10-
import {
11-
X,
12-
RotateCcw,
13-
ArrowRight,
14-
ArrowLeft,
15-
Square,
16-
Circle,
17-
ArrowUpDown,
18-
Copy,
19-
Minus,
20-
} from 'lucide-react';
10+
import { X, RotateCcw, ArrowRight, ArrowLeft, Square, Circle, ArrowUpDown, Copy, Minus, Trash2 } from 'lucide-react';
2111
import { cn } from '@/lib/utils';
2212
import type { StylingSettings, DrawnPath, PathMarkerType } from '@/app/(studio)/types';
2313

@@ -43,16 +33,37 @@ export const PathEditorToolbar: React.FC<PathEditorToolbarProps> = ({
4333
}, 300); // Match animation duration
4434
}, [onClose]);
4535

46-
// Handle ESC key to close panel
36+
const handleDelete = useCallback(() => {
37+
const currentPaths = stylingSettings.drawnPaths || [];
38+
const updatedPaths = currentPaths.filter((p) => p.id !== pathId);
39+
40+
onUpdateStylingSettings({
41+
...stylingSettings,
42+
drawnPaths: updatedPaths,
43+
});
44+
45+
// Close the panel after deletion
46+
handleClose();
47+
}, [pathId, stylingSettings, onUpdateStylingSettings, handleClose]);
48+
49+
// Handle ESC key to close panel and Delete key to delete path
4750
useEffect(() => {
4851
const handleKeyDown = (e: KeyboardEvent) => {
52+
// Don't trigger if user is typing in an input, textarea, or contenteditable element
53+
const target = e.target as HTMLElement;
54+
const isInputElement = target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable;
55+
56+
if (isInputElement) return;
57+
4958
if (e.key === 'Escape') {
5059
handleClose();
60+
} else if (e.key === 'Delete' || e.key === 'Backspace') {
61+
handleDelete();
5162
}
5263
};
5364
window.addEventListener('keydown', handleKeyDown);
5465
return () => window.removeEventListener('keydown', handleKeyDown);
55-
}, [handleClose]);
66+
}, [handleClose, handleDelete]);
5667

5768
if (!pathId) return null;
5869

@@ -161,7 +172,11 @@ export const PathEditorToolbar: React.FC<PathEditorToolbarProps> = ({
161172
case 'line-arrow':
162173
return isStart ? <ArrowLeft className="h-4 w-4" /> : <ArrowRight className="h-4 w-4" />;
163174
case 'triangle-arrow':
164-
return isStart ? <ArrowLeft className="h-4 w-4 fill-current" /> : <ArrowRight className="h-4 w-4 fill-current" />;
175+
return isStart ? (
176+
<ArrowLeft className="h-4 w-4 fill-current" />
177+
) : (
178+
<ArrowRight className="h-4 w-4 fill-current" />
179+
);
165180
case 'open-circle':
166181
return <Circle className="h-4 w-4" />;
167182
case 'closed-circle':
@@ -196,6 +211,14 @@ export const PathEditorToolbar: React.FC<PathEditorToolbarProps> = ({
196211
title="Reset to defaults">
197212
<RotateCcw className="h-4 w-4" />
198213
</Button>
214+
<Button
215+
variant="secondary"
216+
size="icon"
217+
onClick={handleDelete}
218+
className="h-8 w-8 rounded-full bg-destructive/10 hover:bg-destructive/20 text-destructive hover:text-destructive"
219+
title="Delete path (Del)">
220+
<Trash2 className="h-4 w-4" />
221+
</Button>
199222
<Button
200223
variant="secondary"
201224
size="icon"
@@ -221,11 +244,7 @@ export const PathEditorToolbar: React.FC<PathEditorToolbarProps> = ({
221244
<Label htmlFor="path-stroke-color" className="text-sm">
222245
Stroke color
223246
</Label>
224-
<ColorInput
225-
value={stroke}
226-
onChange={(value) => updatePath({ stroke: value })}
227-
showContrastCheck={false}
228-
/>
247+
<ColorInput value={stroke} onChange={(value) => updatePath({ stroke: value })} showContrastCheck={false} />
229248
</div>
230249

231250
{/* Stroke Width */}
@@ -335,9 +354,7 @@ export const PathEditorToolbar: React.FC<PathEditorToolbarProps> = ({
335354
<Label htmlFor="path-start-marker" className="text-sm">
336355
Start marker
337356
</Label>
338-
<Select
339-
value={startMarker}
340-
onValueChange={(value) => updatePath({ startMarker: value as PathMarkerType })}>
357+
<Select value={startMarker} onValueChange={(value) => updatePath({ startMarker: value as PathMarkerType })}>
341358
<SelectTrigger id="path-start-marker" className="flex items-center gap-2">
342359
<SelectValue>
343360
<div className="flex items-center gap-2">
@@ -409,9 +426,7 @@ export const PathEditorToolbar: React.FC<PathEditorToolbarProps> = ({
409426
Swap
410427
</Button>
411428
</div>
412-
<Select
413-
value={endMarker}
414-
onValueChange={(value) => updatePath({ endMarker: value as PathMarkerType })}>
429+
<Select value={endMarker} onValueChange={(value) => updatePath({ endMarker: value as PathMarkerType })}>
415430
<SelectTrigger id="path-end-marker" className="flex items-center gap-2">
416431
<SelectValue>
417432
<div className="flex items-center gap-2">
@@ -485,4 +500,3 @@ export const PathEditorToolbar: React.FC<PathEditorToolbarProps> = ({
485500
</Card>
486501
);
487502
};
488-

0 commit comments

Comments
 (0)