Blame view

泰额版/Food Labeling Management Platform泰鄂/src/lib/presetLabelTemplates.ts 3.93 KB
884054fb   “wangming”   项目初始化
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
  /**
   * System preset label templates
   */
  
  import type { LabelTemplate } from '../types/labelTemplate';
  import { createDefaultElement } from '../types/labelTemplate';
  
  /** Preset template definitions (without id - id assigned when applied) */
  export interface PresetTemplateDef {
    name: string;
    labelType: LabelTemplate['labelType'];
    unit: LabelTemplate['unit'];
    width: number;
    height: number;
    appliedLocation: LabelTemplate['appliedLocation'];
    elements: Array<{
      type: Parameters<typeof createDefaultElement>[0];
      x: number;
      y: number;
      width?: number;
      height?: number;
      config?: Record<string, unknown>;
    }>;
  }
  
  export const PRESET_TEMPLATES: PresetTemplateDef[] = [
    {
      name: 'Price Label (Basic)',
      labelType: 'PRICE',
      unit: 'cm',
      width: 6,
      height: 4,
      appliedLocation: 'ALL',
      elements: [
        { type: 'TEXT_PRODUCT', x: 20, y: 20, width: 180, height: 28, config: { text: 'Product Name', fontSize: 14 } },
        { type: 'TEXT_PRICE', x: 20, y: 55, width: 100, height: 28, config: { text: '0.00', prefix: '$', fontSize: 18 } },
        { type: 'BARCODE', x: 20, y: 95, width: 160, height: 48, config: { data: '123456789' } },
      ],
    },
    {
      name: 'Price Label (with QR)',
      labelType: 'PRICE',
      unit: 'cm',
      width: 6,
      height: 4,
      appliedLocation: 'ALL',
      elements: [
        { type: 'TEXT_PRODUCT', x: 20, y: 20, width: 140, height: 24, config: { text: 'Product Name', fontSize: 12 } },
        { type: 'TEXT_PRICE', x: 20, y: 50, width: 80, height: 24, config: { text: '0.00', prefix: '$', fontSize: 16 } },
        { type: 'QRCODE', x: 180, y: 20, width: 60, height: 60, config: { data: 'https://example.com' } },
        { type: 'BARCODE', x: 20, y: 90, width: 180, height: 40, config: { data: '123456789' } },
      ],
    },
    {
      name: 'Nutrition Label',
      labelType: 'NUTRITION',
      unit: 'cm',
      width: 6,
      height: 8,
      appliedLocation: 'ALL',
      elements: [
        { type: 'TEXT_PRODUCT', x: 20, y: 20, width: 180, height: 24, config: { text: 'Product Name', fontSize: 14 } },
        { type: 'NUTRITION', x: 20, y: 55, width: 200, height: 120, config: { calories: 120, fat: '5g', protein: '3g', carbs: '10g' } },
        { type: 'DATE', x: 20, y: 190, width: 120, height: 24, config: { format: 'YYYY-MM-DD' } },
      ],
    },
    {
      name: 'Shipping Label',
      labelType: 'SHIPPING',
      unit: 'inch',
      width: 4,
      height: 6,
      appliedLocation: 'ALL',
      elements: [
        { type: 'TEXT_STATIC', x: 20, y: 20, width: 200, height: 24, config: { text: 'Ship To:', fontSize: 12 } },
        { type: 'TEXT_STATIC', x: 20, y: 50, width: 200, height: 60, config: { text: 'Address Line 1\nAddress Line 2\nCity, State ZIP', fontSize: 11 } },
        { type: 'BARCODE', x: 20, y: 130, width: 200, height: 50, config: { data: '1234567890123' } },
        { type: 'QRCODE', x: 240, y: 20, width: 80, height: 80, config: { data: 'https://tracking.example.com/123' } },
      ],
    },
    {
      name: 'Blank (6×4 cm)',
      labelType: 'PRICE',
      unit: 'cm',
      width: 6,
      height: 4,
      appliedLocation: 'ALL',
      elements: [],
    },
    {
      name: 'Blank (4×6 inch)',
      labelType: 'PRICE',
      unit: 'inch',
      width: 4,
      height: 6,
      appliedLocation: 'ALL',
      elements: [],
    },
  ];
  
  /** Convert preset to full LabelTemplate with generated element ids */
  export function presetToTemplate(
    preset: PresetTemplateDef,
    templateId: string
  ): LabelTemplate {
    const elements = preset.elements.map((e) => {
      const el = createDefaultElement(e.type, e.x, e.y);
      el.x = e.x;
      el.y = e.y;
      if (e.width != null) el.width = e.width;
      if (e.height != null) el.height = e.height;
      if (e.config) el.config = { ...el.config, ...e.config };
      return el;
    });
  
    return {
      id: templateId,
      name: preset.name,
      labelType: preset.labelType,
      unit: preset.unit,
      width: preset.width,
      height: preset.height,
      appliedLocation: preset.appliedLocation,
      showRuler: true,
      showGrid: true,
      elements,
    };
  }