Blame view

美国版/Food Labeling Management Platform/src/components/labels/LabelTemplateEditor/PropertiesPanel.tsx 21 KB
884054fb   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  import React from 'react';
  import { ScrollArea } from '../../ui/scroll-area';
  import { Input } from '../../ui/input';
  import { Button } from '../../ui/button';
  import { Label } from '../../ui/label';
  import {
    Select,
    SelectContent,
    SelectItem,
    SelectTrigger,
    SelectValue,
  } from '../../ui/select';
  import { Switch } from '../../ui/switch';
  import type {
    LabelTemplate,
    LabelElement,
    LabelType,
    Unit,
    Rotation,
    Border,
0e27ddc8   杨鑫   标签
21
    AppliedLocation,
884054fb   “wangming”   项目初始化
22
  } from '../../../types/labelTemplate';
0e27ddc8   杨鑫   标签
23
24
  import type { LocationDto } from '../../../types/location';
  import { Checkbox } from '../../ui/checkbox';
884054fb   “wangming”   项目初始化
25
26
27
28
29
30
31
  
  interface PropertiesPanelProps {
    template: LabelTemplate;
    selectedElement: LabelElement | null;
    onTemplateChange: (patch: Partial<LabelTemplate>) => void;
    onElementChange: (id: string, patch: Partial<LabelElement>) => void;
    onDeleteElement?: (id: string) => void;
0e27ddc8   杨鑫   标签
32
33
34
35
    /** 门店列表:appliedLocation=SPECIFIED 时勾选 */
    locations?: LocationDto[];
    /** 编辑已有模板时禁止修改 Template Code */
    readOnlyTemplateCode?: boolean;
884054fb   “wangming”   项目初始化
36
37
38
39
40
41
42
43
  }
  
  export function PropertiesPanel({
    template,
    selectedElement,
    onTemplateChange,
    onElementChange,
    onDeleteElement,
0e27ddc8   杨鑫   标签
44
45
    locations = [],
    readOnlyTemplateCode = false,
884054fb   “wangming”   项目初始化
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
  }: PropertiesPanelProps) {
    if (selectedElement) {
      return (
        <div className="w-72 shrink-0 border-l border-gray-200 bg-white flex flex-col h-full">
          <div className="px-3 py-2 border-b border-gray-200 font-semibold text-gray-800">
            Properties (Element)
          </div>
          <ScrollArea className="flex-1">
            <div className="p-3 space-y-3">
              <div className="grid grid-cols-2 gap-2">
                <div>
                  <Label className="text-xs">X</Label>
                  <Input
                    type="number"
                    value={selectedElement.x}
                    onChange={(e) =>
                      onElementChange(selectedElement.id, {
                        x: Number(e.target.value) || 0,
                      })
                    }
                    className="h-8 text-sm"
                  />
                </div>
                <div>
                  <Label className="text-xs">Y</Label>
                  <Input
                    type="number"
                    value={selectedElement.y}
                    onChange={(e) =>
                      onElementChange(selectedElement.id, {
                        y: Number(e.target.value) || 0,
                      })
                    }
                    className="h-8 text-sm"
                  />
                </div>
              </div>
              <div className="grid grid-cols-2 gap-2">
                <div>
                  <Label className="text-xs">Width</Label>
                  <Input
                    type="number"
                    value={selectedElement.width}
                    onChange={(e) =>
                      onElementChange(selectedElement.id, {
                        width: Math.max(1, Number(e.target.value) || 0),
                      })
                    }
                    className="h-8 text-sm"
                  />
                </div>
                <div>
                  <Label className="text-xs">Height</Label>
                  <Input
                    type="number"
                    value={selectedElement.height}
                    onChange={(e) =>
                      onElementChange(selectedElement.id, {
                        height: Math.max(1, Number(e.target.value) || 0),
                      })
                    }
                    className="h-8 text-sm"
                  />
                </div>
              </div>
              <div>
                <Label className="text-xs">Rotation</Label>
                <Select
                  value={selectedElement.rotation}
                  onValueChange={(v: Rotation) =>
                    onElementChange(selectedElement.id, { rotation: v })
                  }
                >
                  <SelectTrigger className="h-8 text-sm">
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="horizontal">horizontal</SelectItem>
                    <SelectItem value="vertical">vertical</SelectItem>
                  </SelectContent>
                </Select>
              </div>
              <div>
                <Label className="text-xs">Border</Label>
                <Select
                  value={selectedElement.border}
                  onValueChange={(v: Border) =>
                    onElementChange(selectedElement.id, { border: v })
                  }
                >
                  <SelectTrigger className="h-8 text-sm">
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="none">none</SelectItem>
                    <SelectItem value="line">line</SelectItem>
                    <SelectItem value="dotted">dotted</SelectItem>
                  </SelectContent>
                </Select>
              </div>
              <ElementConfigFields
                element={selectedElement}
                onChange={(config) =>
                  onElementChange(selectedElement.id, { config: { ...selectedElement.config, ...config } })
                }
              />
              {onDeleteElement && (
                <div className="pt-4 border-t border-gray-100">
                  <Button
                    variant="destructive"
                    className="w-full"
                    onClick={() => onDeleteElement(selectedElement.id)}
                  >
                    Delete Element
                  </Button>
                </div>
              )}
            </div>
          </ScrollArea>
        </div>
      );
    }
  
    return (
      <div className="w-72 shrink-0 border-l border-gray-200 bg-white flex flex-col h-full">
        <div className="px-3 py-2 border-b border-gray-200 font-semibold text-gray-800">
          Properties (Template)
        </div>
        <ScrollArea className="flex-1">
          <div className="p-3 space-y-3">
            <div>
0e27ddc8   杨鑫   标签
177
178
179
180
181
182
183
184
185
186
              <Label className="text-xs">Template Code</Label>
              <Input
                value={template.id}
                disabled={readOnlyTemplateCode}
                onChange={(e) => onTemplateChange({ id: e.target.value.trim() })}
                className="h-8 text-sm mt-1"
                placeholder="e.g. TPL_TEST_001"
              />
            </div>
            <div>
884054fb   “wangming”   项目初始化
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
              <Label className="text-xs">Template Name</Label>
              <Input
                value={template.name}
                onChange={(e) => onTemplateChange({ name: e.target.value })}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">Label Type</Label>
              <Select
                value={template.labelType}
                onValueChange={(v: LabelType) => onTemplateChange({ labelType: v })}
              >
                <SelectTrigger className="h-8 text-sm mt-1">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="PRICE">PRICE</SelectItem>
                  <SelectItem value="NUTRITION">NUTRITION</SelectItem>
                  <SelectItem value="SHIPPING">SHIPPING</SelectItem>
                </SelectContent>
              </Select>
            </div>
            <div>
              <Label className="text-xs">Applied Location</Label>
              <Select
                value={template.appliedLocation}
0e27ddc8   杨鑫   标签
214
215
216
217
218
219
220
                onValueChange={(v: AppliedLocation) => {
                  if (v === "ALL") {
                    onTemplateChange({ appliedLocation: v, appliedLocationIds: [] });
                  } else {
                    onTemplateChange({ appliedLocation: v });
                  }
                }}
884054fb   “wangming”   项目初始化
221
222
223
224
225
              >
                <SelectTrigger className="h-8 text-sm mt-1">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
0e27ddc8   杨鑫   标签
226
227
                  <SelectItem value="ALL">All locations</SelectItem>
                  <SelectItem value="SPECIFIED">Specified locations</SelectItem>
884054fb   “wangming”   项目初始化
228
229
230
                </SelectContent>
              </Select>
            </div>
0e27ddc8   杨鑫   标签
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
            {template.appliedLocation === "SPECIFIED" && (
              <div className="rounded-md border border-gray-200 p-2 max-h-40 overflow-y-auto space-y-2">
                <Label className="text-xs text-gray-600">Select locations</Label>
                {locations.length === 0 ? (
                  <p className="text-xs text-gray-500">No locations loaded.</p>
                ) : (
                  locations.map((loc) => {
                    const checked = (template.appliedLocationIds ?? []).includes(loc.id);
                    return (
                      <label
                        key={loc.id}
                        className="flex items-center gap-2 text-xs cursor-pointer"
                      >
                        <Checkbox
                          checked={checked}
                          onCheckedChange={(v) => {
                            const on = v === true;
                            const cur = new Set(template.appliedLocationIds ?? []);
                            if (on) cur.add(loc.id);
                            else cur.delete(loc.id);
                            onTemplateChange({ appliedLocationIds: Array.from(cur) });
                          }}
                        />
                        <span className="truncate">
                          {(loc.locationName ?? loc.locationCode ?? loc.id).trim() || loc.id}
                        </span>
                      </label>
                    );
                  })
                )}
              </div>
            )}
884054fb   “wangming”   项目初始化
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
            <div className="grid grid-cols-2 gap-2">
              <div>
                <Label className="text-xs">Width</Label>
                <Input
                  type="number"
                  value={template.width}
                  onChange={(e) =>
                    onTemplateChange({ width: Math.max(0.1, Number(e.target.value) || 0) })
                  }
                  className="h-8 text-sm"
                />
              </div>
              <div>
                <Label className="text-xs">Height</Label>
                <Input
                  type="number"
                  value={template.height}
                  onChange={(e) =>
                    onTemplateChange({ height: Math.max(0.1, Number(e.target.value) || 0) })
                  }
                  className="h-8 text-sm"
                />
              </div>
            </div>
            <div>
              <Label className="text-xs">Unit</Label>
              <Select
                value={template.unit}
                onValueChange={(v: Unit) => onTemplateChange({ unit: v })}
              >
                <SelectTrigger className="h-8 text-sm mt-1">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="cm">cm</SelectItem>
                  <SelectItem value="inch">inch</SelectItem>
                </SelectContent>
              </Select>
            </div>
            <div className="flex items-center gap-2">
              <Switch
                checked={template.showRuler}
                onCheckedChange={(v) => onTemplateChange({ showRuler: v })}
              />
              <Label className="text-xs">Show Ruler</Label>
            </div>
0e27ddc8   杨鑫   标签
309
310
311
312
313
314
315
            <div className="flex items-center gap-2">
              <Switch
                checked={template.showGrid ?? true}
                onCheckedChange={(v) => onTemplateChange({ showGrid: v })}
              />
              <Label className="text-xs">Show Grid</Label>
            </div>
884054fb   “wangming”   项目初始化
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
          </div>
        </ScrollArea>
      </div>
    );
  }
  
  function ElementConfigFields({
    element,
    onChange,
  }: {
    element: LabelElement;
    onChange: (config: Record<string, unknown>) => void;
  }) {
    const cfg = element.config as Record<string, unknown>;
    const update = (key: string, value: unknown) =>
      onChange({ [key]: value });
  
    switch (element.type) {
      case 'TEXT_STATIC':
      case 'TEXT_PRODUCT':
      case 'TEXT_PRICE':
        return (
          <>
            <div>
              <Label className="text-xs">Text</Label>
              <Input
abb6bea5   杨鑫   用户管理
342
                value={(cfg.text as string) ?? '0.00'}
884054fb   “wangming”   项目初始化
343
344
345
346
347
                onChange={(e) => update('text', e.target.value)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
abb6bea5   杨鑫   用户管理
348
349
350
351
352
353
354
355
              <Label className="text-xs">Prefix</Label>
              <Input
                value={(cfg.prefix as string) ?? '¥'}
                onChange={(e) => update('prefix', e.target.value)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
884054fb   “wangming”   项目初始化
356
357
358
359
360
361
362
363
364
365
366
              <Label className="text-xs">Font Size</Label>
              <Input
                type="number"
                value={(cfg.fontSize as number) ?? 14}
                onChange={(e) => update('fontSize', Number(e.target.value) || 14)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">Text Align</Label>
              <Select
abb6bea5   杨鑫   用户管理
367
                value={(cfg.textAlign as string) ?? 'right'}
884054fb   “wangming”   项目初始化
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
                onValueChange={(v) => update('textAlign', v)}
              >
                <SelectTrigger className="h-8 text-sm mt-1">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="left">Left</SelectItem>
                  <SelectItem value="center">Center</SelectItem>
                  <SelectItem value="right">Right</SelectItem>
                </SelectContent>
              </Select>
            </div>
          </>
        );
      case 'BARCODE':
        return (
          <>
            <div>
              <Label className="text-xs">Data</Label>
              <Input
abb6bea5   杨鑫   用户管理
388
                value={(cfg.data as string) ?? '123456789'}
884054fb   “wangming”   项目初始化
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
                onChange={(e) => update('data', e.target.value)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">方向</Label>
              <Select
                value={(cfg.orientation as string) ?? 'horizontal'}
                onValueChange={(v) => update('orientation', v)}
              >
                <SelectTrigger className="h-8 text-sm mt-1">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="horizontal">水平</SelectItem>
                  <SelectItem value="vertical">竖排</SelectItem>
                </SelectContent>
              </Select>
            </div>
abb6bea5   杨鑫   用户管理
408
409
410
411
412
413
414
            <div className="flex items-center gap-2">
              <Switch
                checked={(cfg.showText as boolean) !== false}
                onCheckedChange={(v) => update('showText', v)}
              />
              <Label className="text-xs">Show Text</Label>
            </div>
884054fb   “wangming”   项目初始化
415
416
417
418
419
420
421
          </>
        );
      case 'QRCODE':
        return (
          <div>
            <Label className="text-xs">Data (URL)</Label>
            <Input
abb6bea5   杨鑫   用户管理
422
              value={(cfg.data as string) ?? 'https://example.com'}
884054fb   “wangming”   项目初始化
423
424
425
426
427
              onChange={(e) => update('data', e.target.value)}
              className="h-8 text-sm mt-1"
            />
          </div>
        );
abb6bea5   杨鑫   用户管理
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
      case 'IMAGE':
        return (
          <>
            <div>
              <Label className="text-xs">Image URL</Label>
              <Input
                value={(cfg.src as string) ?? ''}
                onChange={(e) => update('src', e.target.value)}
                className="h-8 text-sm mt-1"
                placeholder="输入图片URL或路径"
              />
            </div>
            <div>
              <Label className="text-xs">Scale Mode</Label>
              <Select
                value={(cfg.scaleMode as string) ?? 'contain'}
                onValueChange={(v) => update('scaleMode', v)}
              >
                <SelectTrigger className="h-8 text-sm mt-1">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="contain">Contain</SelectItem>
                  <SelectItem value="cover">Cover</SelectItem>
                  <SelectItem value="fill">Fill</SelectItem>
                </SelectContent>
              </Select>
            </div>
          </>
        );
      case 'DATE':
        return (
          <>
            <div>
              <Label className="text-xs">Format</Label>
              <Input
                value={(cfg.format as string) ?? 'YYYY-MM-DD'}
                onChange={(e) => update('format', e.target.value)}
                className="h-8 text-sm mt-1"
                placeholder="YYYY-MM-DD"
              />
            </div>
            <div>
              <Label className="text-xs">Offset Days</Label>
              <Input
                type="number"
                value={(cfg.offsetDays as number) ?? 0}
                onChange={(e) => update('offsetDays', Number(e.target.value) || 0)}
                className="h-8 text-sm mt-1"
              />
            </div>
          </>
        );
      case 'TIME':
884054fb   “wangming”   项目初始化
482
483
        return (
          <div>
abb6bea5   杨鑫   用户管理
484
            <Label className="text-xs">Format</Label>
884054fb   “wangming”   项目初始化
485
            <Input
abb6bea5   杨鑫   用户管理
486
487
              value={(cfg.format as string) ?? 'HH:mm'}
              onChange={(e) => update('format', e.target.value)}
884054fb   “wangming”   项目初始化
488
              className="h-8 text-sm mt-1"
abb6bea5   杨鑫   用户管理
489
              placeholder="HH:mm"
884054fb   “wangming”   项目初始化
490
491
492
            />
          </div>
        );
abb6bea5   杨鑫   用户管理
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
      case 'DURATION':
        return (
          <>
            <div>
              <Label className="text-xs">Format</Label>
              <Input
                value={(cfg.format as string) ?? 'YYYY-MM-DD'}
                onChange={(e) => update('format', e.target.value)}
                className="h-8 text-sm mt-1"
                placeholder="YYYY-MM-DD"
              />
            </div>
            <div>
              <Label className="text-xs">Offset Days</Label>
              <Input
                type="number"
                value={(cfg.offsetDays as number) ?? 3}
                onChange={(e) => update('offsetDays', Number(e.target.value) || 3)}
                className="h-8 text-sm mt-1"
              />
            </div>
          </>
        );
      case 'WEIGHT':
        return (
          <>
            <div>
              <Label className="text-xs">Value</Label>
              <Input
                type="number"
                value={(cfg.value as number) ?? 500}
                onChange={(e) => update('value', Number(e.target.value) || 0)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">Unit</Label>
              <Select
                value={(cfg.unit as string) ?? 'g'}
                onValueChange={(v) => update('unit', v)}
              >
                <SelectTrigger className="h-8 text-sm mt-1">
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  <SelectItem value="g">g</SelectItem>
                  <SelectItem value="kg">kg</SelectItem>
                  <SelectItem value="oz">oz</SelectItem>
                  <SelectItem value="lb">lb</SelectItem>
                </SelectContent>
              </Select>
            </div>
          </>
        );
      case 'WEIGHT_PRICE':
        return (
          <>
            <div>
              <Label className="text-xs">Unit Price</Label>
              <Input
                type="number"
                value={(cfg.unitPrice as number) ?? 10}
                onChange={(e) => update('unitPrice', Number(e.target.value) || 0)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">Weight</Label>
              <Input
                type="number"
                step="0.1"
                value={(cfg.weight as number) ?? 0.5}
                onChange={(e) => update('weight', Number(e.target.value) || 0)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">Currency</Label>
              <Input
                value={(cfg.currency as string) ?? '¥'}
                onChange={(e) => update('currency', e.target.value)}
                className="h-8 text-sm mt-1"
              />
            </div>
          </>
        );
      case 'NUTRITION':
        return (
          <>
            <div>
              <Label className="text-xs">Calories</Label>
              <Input
                type="number"
                value={(cfg.calories as number) ?? 120}
                onChange={(e) => update('calories', Number(e.target.value) || 0)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">Fat</Label>
              <Input
                value={(cfg.fat as string) ?? '5g'}
                onChange={(e) => update('fat', e.target.value)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">Protein</Label>
              <Input
                value={(cfg.protein as string) ?? '3g'}
                onChange={(e) => update('protein', e.target.value)}
                className="h-8 text-sm mt-1"
              />
            </div>
            <div>
              <Label className="text-xs">Carbs</Label>
              <Input
                value={(cfg.carbs as string) ?? '10g'}
                onChange={(e) => update('carbs', e.target.value)}
                className="h-8 text-sm mt-1"
              />
            </div>
          </>
        );
      case 'BLANK':
        return (
          <div className="text-xs text-gray-500">
0e27ddc8   杨鑫   标签
620
            Blank spacer; no configuration needed.
abb6bea5   杨鑫   用户管理
621
622
          </div>
        );
884054fb   “wangming”   项目初始化
623
624
625
626
627
628
629
630
      default:
        return (
          <div className="text-xs text-gray-500">
            Config for {element.type} (edit in code if needed)
          </div>
        );
    }
  }