-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbar.tsx
More file actions
224 lines (204 loc) · 6.55 KB
/
Toolbar.tsx
File metadata and controls
224 lines (204 loc) · 6.55 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
213
214
215
216
217
218
219
220
221
222
223
224
import { useSkitStore } from '../../store/skitStore';
import { Button } from '../ui/button';
import {
Plus,
Copy,
Trash,
Undo,
Redo,
Save,
ChevronDown,
ClipboardPaste,
Scissors,
FolderOpen
} from 'lucide-react';
import { SidebarTrigger } from '../ui/sidebar';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger
} from '../ui/dropdown-menu';
import { CommandDefinition } from '../../types';
import { toast } from 'sonner';
import { createCommandWithDefaults } from '../../utils/commandDefaults';
import { DraggableCommand } from '../dnd/DraggableCommand';
import { DropZone } from '../dnd/DropZone';
import { useTranslation } from 'react-i18next';
import { useCommandTranslation } from '../../hooks/useCommandTranslation';
import { openProjectDirectory } from '../../utils/fileSystem';
// Helper component to display translated command label
function CommandMenuLabel({ commandId, label }: { commandId: string; label?: string }) {
const { tCommand } = useCommandTranslation(commandId);
return <>{tCommand('name', label || commandId)}</>;
}
export function Toolbar() {
const { t } = useTranslation();
const {
currentSkitId,
selectedCommandIds,
addCommand,
moveCommand,
removeCommands,
copySelectedCommands,
cutSelectedCommands,
pasteCommandsFromClipboard,
undo,
redo,
saveSkit,
commandDefinitions,
projectPath,
} = useSkitStore();
const handleAddCommand = (commandType: string) => {
const commandDef = commandDefinitions.find((def: CommandDefinition) => def.id === commandType);
if (!commandDef) return;
const newCommand = createCommandWithDefaults(commandDef);
addCommand(newCommand);
if (selectedCommandIds.length > 0 && currentSkitId) {
const lastSelectedId = selectedCommandIds[selectedCommandIds.length - 1];
const state = useSkitStore.getState();
const currentSkit = state.skits[currentSkitId];
if (currentSkit) {
const newIndex = currentSkit.commands.length - 1;
const targetIndex = currentSkit.commands.findIndex(
cmd => cmd.id === lastSelectedId
);
if (targetIndex !== -1) {
moveCommand(newIndex, targetIndex + 1);
}
}
}
toast.success(t('editor.toolbar.commandAdded', { command: commandDef.label }));
};
const handleSave = async () => {
try {
await saveSkit();
toast.success(t('editor.toolbar.skitSaved'));
} catch (error) {
toast.error(t('editor.toolbar.saveFailed', { error: error instanceof Error ? error.message : String(error) }));
}
};
const handleOpenProjectDirectory = async () => {
if (!projectPath) return;
try {
await openProjectDirectory(projectPath);
} catch (error) {
toast.error(t('editor.toolbar.openProjectFolderFailed', { error: error instanceof Error ? error.message : String(error) }));
}
};
const isDisabled = !currentSkitId;
const isCommandSelected = selectedCommandIds.length > 0;
return (
<div className="flex items-center p-2 border-b w-full">
<SidebarTrigger className="mr-2" />
<div className="flex items-center gap-2">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
className="flex items-center gap-1"
disabled={isDisabled}
>
<Plus className="h-4 w-4" />
{t('editor.toolbar.addCommand')}
<ChevronDown className="h-4 w-4 ml-1" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
{commandDefinitions.map((command: CommandDefinition) => (
<DropdownMenuItem
key={command.id}
onClick={() => handleAddCommand(command.id)}
>
<DraggableCommand id={command.id}>
<div className="flex items-center w-full">
<CommandMenuLabel commandId={command.id} label={command.label} />
</div>
</DraggableCommand>
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
<DropZone id="copy-zone" className="inline-flex">
<Button
variant="outline"
size="sm"
disabled={!isCommandSelected}
onClick={() => copySelectedCommands()}
>
<Copy className="h-4 w-4 mr-1" />
{t('editor.menu.edit.copy')}
</Button>
</DropZone>
<Button
variant="outline"
size="sm"
disabled={!isCommandSelected}
onClick={() => cutSelectedCommands()}
>
<Scissors className="h-4 w-4 mr-1" />
{t('editor.menu.edit.cut')}
</Button>
<Button
variant="outline"
size="sm"
onClick={() => pasteCommandsFromClipboard()}
>
<ClipboardPaste className="h-4 w-4 mr-1" />
{t('editor.menu.edit.paste')}
</Button>
<DropZone id="trash-zone" className="inline-flex">
<Button
variant="outline"
size="sm"
disabled={!isCommandSelected}
onClick={() => selectedCommandIds.length > 0 && removeCommands(selectedCommandIds)}
>
<Trash className="h-4 w-4 mr-1" />
{t('editor.menu.edit.delete')}
</Button>
</DropZone>
</div>
<div className="ml-auto flex items-center gap-2">
<div className="flex items-center">
<Button
variant="ghost"
size="sm"
disabled={isDisabled}
onClick={undo}
>
<Undo className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="sm"
disabled={isDisabled}
onClick={redo}
>
<Redo className="h-4 w-4" />
</Button>
<Button
variant="default"
size="sm"
disabled={isDisabled}
onClick={handleSave}
className="mr-2"
>
<Save className="h-4 w-4 mr-1" />
{t('editor.menu.file.save')}
</Button>
<Button
variant="outline"
size="sm"
disabled={!projectPath}
onClick={handleOpenProjectDirectory}
>
<FolderOpen className="h-4 w-4 mr-1" />
{t('editor.toolbar.openProjectFolder')}
</Button>
</div>
</div>
</div>
);
}