Blame view

美国版/Food Labeling Management App UniApp/src/utils/labelPreview/labelIdElement.ts 2.3 KB
540ac0e3   杨鑫   前端修改bug
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
  import type { SystemLabelTemplate, SystemTemplateElementBase } from '../print/types/printer'
  
  function normalizePaletteSlug (raw: string): string {
    return String(raw ?? '').trim().toLowerCase().replace(/[^a-z0-9]+/g, '')
  }
  
  function readTypeAdd (el: SystemTemplateElementBase): string {
    return String((el as SystemTemplateElementBase & { typeAdd?: string }).typeAdd ?? '').trim().toLowerCase()
  }
  
  /** 是否为模板「Entered Automatically → Label ID」控件 */
  export function isLabelIdTemplateElement (el: SystemTemplateElementBase): boolean {
    const typeAdd = readTypeAdd(el)
    if (typeAdd.startsWith('auto_') && typeAdd.includes('label id')) return true
  
    const en = normalizePaletteSlug(String(el.elementName ?? ''))
    if (/^labelid\d*$/.test(en)) return true
  
    const vst = String(el.valueSourceType ?? '').toUpperCase()
    const type = String(el.type ?? '').toUpperCase()
    if (vst === 'AUTO_DB' && type === 'TEXT_STATIC' && /^labelid/.test(en)) return true
  
    return false
  }
  
  export function templateIncludesLabelIdElement (
    template: SystemLabelTemplate | null | undefined,
  ): boolean {
    return !!(template?.elements?.some(isLabelIdTemplateElement))
  }
  
  /** 按元素 config.prefix 拼展示行(避免重复前缀) */
  export function formatLabelIdDisplayLine (
    rawId: string,
    config?: Record<string, unknown>,
  ): string {
    const id = String(rawId ?? '').trim()
    const prefix = String(config?.prefix ?? config?.Prefix ?? 'LABEL ID: ').trim()
    if (!id) return prefix ? `${prefix}—` : '—'
    const lowerPrefix = prefix.toLowerCase()
    if (prefix && id.toLowerCase().startsWith(lowerPrefix)) return id
    if (!prefix) return id
    const needsSpace = !prefix.endsWith(':') && !prefix.endsWith(' ')
    return needsSpace ? `${prefix} ${id}` : `${prefix}${id}`
  }
  
  /** 将 Label ID 写入模板内对应 AUTO 控件(预览/出纸同源) */
  export function applyLabelIdDisplayToTemplate (
    template: SystemLabelTemplate,
    labelIdText: string,
  ): SystemLabelTemplate {
    const elements = (template.elements || []).map((el) => {
      if (!isLabelIdTemplateElement(el)) return el
      const cfg = { ...(el.config || {}) } as Record<string, unknown>
      const line = formatLabelIdDisplayLine(labelIdText, cfg)
      return {
        ...el,
        config: { ...cfg, text: line, Text: line },
      }
    })
    return { ...template, elements }
  }