-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandEditor.tsx
More file actions
382 lines (360 loc) · 13.7 KB
/
CommandEditor.tsx
File metadata and controls
382 lines (360 loc) · 13.7 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import { useSkitStore } from '../../store/skitStore';
import { Card, CardContent, CardHeader, CardTitle } from '../ui/card';
import { Label } from '../ui/label';
import { Input } from '../ui/input';
import { Textarea } from '../ui/textarea';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select';
import { CommandDefinition, PropertyDefinition } from '../../types';
import { SkitCommand } from '../../types';
import { formatCommandPreview } from '../../utils/commandFormatting';
import { ColorPicker } from '../ui/color-picker';
import { HexColorPicker } from 'react-colorful';
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover';
import { VectorInput } from '../ui/vector-input';
import { useTranslation } from 'react-i18next';
import { useCommandTranslation } from '../../hooks/useCommandTranslation';
// Helper component for displaying translated command labels
function TranslatedCommandLabel({ commandType, label }: { commandType: string; label: string }) {
const { tCommand } = useCommandTranslation(commandType);
return <>{tCommand('name', label)}</>;
}
// Helper component for displaying translated property labels
function PropertyLabel({ commandType, propertyKey, required }: { commandType: string; propertyKey: string; required?: boolean }) {
const { tProperty } = useCommandTranslation(commandType);
const label = tProperty(propertyKey, 'name', propertyKey);
return (
<>
{label}
{required && <span className="text-destructive ml-1">*</span>}
</>
);
}
// Helper component for enum options
function EnumOption({ commandType, propertyKey, value, masterKey }: { commandType: string; propertyKey: string; value: string; masterKey?: string }) {
const { tEnum } = useCommandTranslation(commandType);
return <>{tEnum(propertyKey, value, value, masterKey)}</>;
}
export function CommandEditor() {
const { t } = useTranslation();
const {
skits,
currentSkitId,
selectedCommandIds,
updateCommand,
commandDefinitions,
commandsMap
} = useSkitStore();
const currentSkit = currentSkitId ? skits[currentSkitId] : null;
const selectedCommands = currentSkit
? currentSkit.commands.filter(cmd => selectedCommandIds.includes(cmd.id))
: [];
const commands = currentSkit?.commands || [];
if (selectedCommands.length === 0) {
return (
<div className="p-4 text-center text-muted-foreground">
{t('editor.noCommandSelected')}
</div>
);
}
const firstCommand = selectedCommands[0];
const firstCommandDef = commandDefinitions.find(def => def.id === firstCommand.type);
if (!firstCommandDef) {
return (
<div className="p-4 text-center text-muted-foreground">
{t('editor.commandDefinitionNotFound')}: {firstCommand.type}
</div>
);
}
// get common property definitions
const otherDefs = selectedCommands.slice(1).map(cmd => commandDefinitions.find(def => def.id === cmd.type));
const allDefs = [firstCommandDef, ...otherDefs.filter((d): d is CommandDefinition => d !== undefined)];
const commonProperties = Object.entries(firstCommandDef.properties).filter(([name, def]) =>
allDefs.every(d => d.properties[name] && d.properties[name].type === def.type)
);
if (!firstCommandDef) {
return (
<div className="p-4 text-center text-muted-foreground">
{t('editor.commandDefinitionNotFound')}: {firstCommand.type}
</div>
);
}
const handlePropertyChange = (property: string, value: unknown) => {
const updates: Record<string, unknown> = { [property]: value };
selectedCommands.forEach(cmd => {
updateCommand(cmd.id, updates);
});
};
const backgroundColors = selectedCommands.map(cmd => cmd.backgroundColor || (commandsMap.get(cmd.type)?.defaultBackgroundColor ?? "#ffffff"));
const isBgMixed = !backgroundColors.every(c => c === backgroundColors[0]);
const bgColorValue = isBgMixed ? "#ffffff" : backgroundColors[0];
const labelColors = selectedCommands.map(cmd => cmd.commandLabelColor || (commandsMap.get(cmd.type)?.defaultCommandLabelColor ?? "#000000"));
const isLabelColorMixed = !labelColors.every(c => c === labelColors[0]);
const labelColorValue = isLabelColorMixed ? "#000000" : labelColors[0];
const selectedLabels = selectedCommands.map(cmd => commandsMap.get(cmd.type)?.label || cmd.type);
const uniqueLabels = Array.from(new Set(selectedLabels));
return (
<Card className="border-0 shadow-none">
<CardHeader className="pb-2">
<CardTitle>
{selectedCommands.length > 1
? `${uniqueLabels.join(', ')} (${t('editor.itemsSelected', { count: selectedCommands.length })})`
: <TranslatedCommandLabel commandType={firstCommand.type} label={firstCommandDef.label} />}
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
{/* Color Pickers - Inline Side by Side */}
<div className="space-y-2">
<div className="flex items-center gap-6">
{/* Background Color Picker */}
<div className="flex items-center gap-2">
<Label htmlFor="backgroundColor" className="min-w-[60px]">{t('editor.backgroundColor')}</Label>
<Popover>
<PopoverTrigger asChild>
<button
className="w-8 h-8 rounded border border-zinc-200 dark:border-zinc-800 cursor-pointer"
style={{ backgroundColor: bgColorValue }}
aria-label="Pick background color"
/>
</PopoverTrigger>
<PopoverContent className="w-auto p-3">
<HexColorPicker color={bgColorValue} onChange={(value) => handlePropertyChange("backgroundColor", value)} />
</PopoverContent>
</Popover>
<Input
value={isBgMixed ? '' : bgColorValue}
onChange={(e) => handlePropertyChange("backgroundColor", e.target.value)}
placeholder={isBgMixed ? '-' : undefined}
className="w-28"
/>
</div>
{/* Command Label Color Picker */}
<div className="flex items-center gap-2">
<Label htmlFor="commandLabelColor" className="min-w-[60px]">{t('editor.commandLabelColor')}</Label>
<Popover>
<PopoverTrigger asChild>
<button
className="w-8 h-8 rounded border border-zinc-200 dark:border-zinc-800 cursor-pointer"
style={{ backgroundColor: labelColorValue }}
aria-label="Pick text color"
/>
</PopoverTrigger>
<PopoverContent className="w-auto p-3">
<HexColorPicker color={labelColorValue} onChange={(value) => handlePropertyChange("commandLabelColor", value)} />
</PopoverContent>
</Popover>
<Input
value={isLabelColorMixed ? '' : labelColorValue}
onChange={(e) => handlePropertyChange("commandLabelColor", e.target.value)}
placeholder={isLabelColorMixed ? '-' : undefined}
className="w-28"
/>
</div>
</div>
</div>
{/* Command Properties */}
{commonProperties.map(([propName, propDef]) => {
const values = selectedCommands.map(cmd => cmd[propName]);
const isMixed = !values.every(v => v === values[0]);
const value = isMixed ? undefined : values[0];
return (
<div key={propName} className="space-y-2">
<Label htmlFor={propName}>
<PropertyLabel commandType={firstCommand.type} propertyKey={propName} required={propDef.required} />
</Label>
<PropertyInput
propName={propName}
propDef={propDef}
value={value}
onChange={(val) => handlePropertyChange(propName, val)}
commands={commands}
commandsMap={commandsMap}
isMixed={isMixed}
commandType={firstCommand.type}
/>
</div>
);
})}
</CardContent>
</Card>
);
}
// Wrapper component for property input with translations
function PropertyInput(props: {
propName: string;
propDef: PropertyDefinition;
value: unknown;
onChange: (value: unknown) => void;
commands?: SkitCommand[];
commandsMap?: Map<string, CommandDefinition>;
isMixed?: boolean;
commandType: string;
}) {
const { t } = useTranslation();
return <>{renderPropertyInput({ ...props, t })}</>;
}
function renderPropertyInput({
propName,
propDef,
value,
onChange,
commands,
commandsMap,
isMixed,
commandType,
t
}: {
propName: string;
propDef: PropertyDefinition;
value: unknown;
onChange: (value: unknown) => void;
commands?: SkitCommand[];
commandsMap?: Map<string, CommandDefinition>;
isMixed?: boolean;
commandType?: string;
t: (key: string) => string;
}) {
switch (propDef.type) {
case 'string': {
const stringValue = value as string | undefined;
return propDef.multiline ? (
<Textarea
id={propName}
value={isMixed ? '' : stringValue || ''}
onChange={(e) => onChange(e.target.value)}
placeholder={isMixed ? '-' : undefined}
rows={5}
/>
) : (
<Input
id={propName}
value={isMixed ? '' : stringValue || ''}
onChange={(e) => onChange(e.target.value)}
placeholder={isMixed ? '-' : undefined}
/>
);
}
case 'number': {
const numberValue = value as number | undefined;
return (
<Input
id={propName}
type="number"
value={isMixed ? '' : numberValue ?? (propDef.default as number) ?? ''}
onChange={(e) => onChange(Number(e.target.value))}
placeholder={isMixed ? '-' : undefined}
min={propDef.constraints?.min}
max={propDef.constraints?.max}
/>
);
}
case 'boolean': {
const booleanValue = value as boolean | undefined;
return (
<Select
value={isMixed ? '' : booleanValue ? 'true' : 'false'}
onValueChange={(val) => onChange(val === 'true')}
>
<SelectTrigger id={propName}>
<SelectValue placeholder={isMixed ? '-' : t('editor.selectPlease')} />
</SelectTrigger>
<SelectContent>
<SelectItem value="true">{t('editor.yes')}</SelectItem>
<SelectItem value="false">{t('editor.no')}</SelectItem>
</SelectContent>
</Select>
);
}
case 'enum': {
const enumValue = value as string | undefined;
return (
<Select
value={isMixed ? '' : enumValue || ''}
onValueChange={onChange}
>
<SelectTrigger id={propName}>
<SelectValue placeholder={isMixed ? '-' : t('editor.selectPlease')}>
{!isMixed && enumValue && commandType ? (
<EnumOption commandType={commandType} propertyKey={propName} value={enumValue} masterKey={propDef.masterKey} />
) : null}
</SelectValue>
</SelectTrigger>
<SelectContent>
{Array.isArray(propDef.options) && propDef.options.map((option) => (
<SelectItem key={option} value={option}>
{commandType ? <EnumOption commandType={commandType} propertyKey={propName} value={option} masterKey={propDef.masterKey} /> : option}
</SelectItem>
))}
</SelectContent>
</Select>
);
}
case 'asset': {
const assetValue = value as string | undefined;
return (
<Input
id={propName}
value={isMixed ? '' : assetValue || ''}
onChange={(e) => onChange(e.target.value)}
placeholder={isMixed ? '-' : undefined}
/>
);
}
case 'vector2':
case 'vector3':
case 'vector4':
case 'vector2Int':
case 'vector3Int': {
const dimension =
propDef.type === 'vector2' || propDef.type === 'vector2Int'
? 2
: propDef.type === 'vector3' || propDef.type === 'vector3Int'
? 3
: 4;
const integer = propDef.type === 'vector2Int' || propDef.type === 'vector3Int';
return (
<VectorInput
value={value as number[] | undefined}
dimension={dimension as 2 | 3 | 4}
integer={integer}
onChange={onChange as (val: number[]) => void}
isMixed={isMixed}
/>
);
}
case 'command': {
const commandValue = value as number | undefined;
const commandsList = commands || [];
const filteredCommands = propDef.commandTypes
? commandsList.filter(cmd => propDef.commandTypes?.includes(cmd.type))
: commandsList;
return (
<Select
value={isMixed ? '' : commandValue?.toString() || ''}
onValueChange={(val) => onChange(Number(val))}
>
<SelectTrigger id={propName}>
<SelectValue placeholder={isMixed ? '-' : 'コマンドを選択'} />
</SelectTrigger>
<SelectContent>
{filteredCommands.map((cmd) => (
<SelectItem key={cmd.id} value={cmd.id.toString()}>
{cmd.id}: {formatCommandPreview(cmd, commandsMap || new Map<string, CommandDefinition>())}
</SelectItem>
))}
</SelectContent>
</Select>
);
}
default: {
const defaultValue = value as string | undefined;
return (
<Input
id={propName}
value={isMixed ? '' : defaultValue || ''}
onChange={(e) => onChange(e.target.value)}
placeholder={isMixed ? '-' : undefined}
/>
);
}
}
}