Blame view

美国版/Food Labeling Management Platform/src/lib/productCodeValueTemplate.ts 1.93 KB
923d50c0   杨鑫   更新bug
1
  import type { LabelElement } from "../types/labelTemplate";
91821909   杨鑫   最新
2
3
4
5
6
  import {
    canonicalElementType,
    isTemplateSectionPersistedType,
    resolvedTypeAddForPersist,
  } from "../types/labelTemplate";
923d50c0   杨鑫   更新bug
7
  
91821909   杨鑫   最新
8
  /** Template 面板中的条形码 / 二维码(typeAdd 如 template_Barcode、template_QR Code) */
923d50c0   杨鑫   更新bug
9
10
11
12
13
14
  export function isTemplateSectionBarcodeOrQrElement(el: LabelElement): boolean {
    if (!isTemplateSectionPersistedType(el)) return false;
    const t = canonicalElementType(el.type);
    return t === "BARCODE" || t === "QRCODE";
  }
  
91821909   杨鑫   最新
15
16
17
18
19
20
21
22
23
  /** Label 面板中的条形码 / 二维码(typeAdd 如 label_Barcode、label_QR Code),允许手填 */
  export function isLabelSectionBarcodeOrQrElement(el: LabelElement): boolean {
    if (isTemplateSectionPersistedType(el)) return false;
    const t = canonicalElementType(el.type);
    if (t !== "BARCODE" && t !== "QRCODE") return false;
    const typeAdd = resolvedTypeAddForPersist(el).trim().toLowerCase();
    return typeAdd.startsWith("label_");
  }
  
923d50c0   杨鑫   更新bug
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
  /** 将产品 codeValue 写入 elementId → 字符串,供 templateProductDefaults / 预览合并 */
  export function buildTemplateBarcodeQrDefaultsFromCodeValue(
    elements: LabelElement[],
    codeValue: string | null | undefined,
  ): Record<string, string> {
    const cv = String(codeValue ?? "").trim();
    if (!cv) return {};
    const out: Record<string, string> = {};
    for (const el of elements) {
      if (!isTemplateSectionBarcodeOrQrElement(el)) continue;
      out[el.id] = cv;
    }
    return out;
  }
  
  /** 合并 codeValue 到元素 config.data(用于画布预览) */
  export function applyProductCodeValueToLabelElements(
    elements: LabelElement[],
    codeValue: string | null | undefined,
  ): LabelElement[] {
    const cv = String(codeValue ?? "").trim();
    if (!cv) return elements;
    return elements.map((el) => {
      if (!isTemplateSectionBarcodeOrQrElement(el)) return el;
      const cfg = { ...(el.config as Record<string, unknown>) };
      cfg.data = cv;
      return { ...el, config: cfg };
    });
  }