7 小时以前 35722562e9e13f0504acc15b740d042ecb810199
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
import type { QualityComponentDefinition } from '../core/types';
 
import { cssStyle, escapeHtml } from '../core/html';
import { QUALITY_ICONS } from '../core/icons';
import { QUALITY_CATEGORY } from '../core/types';
 
/** 单元格与表头共用的边框样式 */
const CELL_BORDER = '1px solid #d9d9d9';
 
function buildCells(headers: string[], rows: number): string {
  const head = headers
    .map(
      (text) =>
        `<th style="${cssStyle({
          border: CELL_BORDER,
          padding: '6px 8px',
          background: '#fafafa',
          'font-weight': 600,
        })}">${escapeHtml(text)}</th>`,
    )
    .join('');
  const body = Array.from({ length: Math.max(rows, 1) })
    .map(() =>
      headers
        .map(() => `<td style="${cssStyle({ border: CELL_BORDER, padding: '6px 8px' })}">&nbsp;</td>`)
        .join(''),
    )
    .map((cells) => `<tr>${cells}</tr>`)
    .join('');
  return `<table style="width:100%;border-collapse:collapse;font-size:12px"><thead><tr>${head}</tr></thead><tbody>${body}</tbody></table>`;
}
 
/** 解析逗号分隔的表头文字 */
function parseHeaders(raw: unknown): string[] {
  const headers = String(raw ?? '')
    .split(',')
    .map((item) => item.trim())
    .filter(Boolean);
  return headers.length > 0 ? headers : ['项目', '标准值', '实测值', '判定'];
}
 
export const tableDefinition: QualityComponentDefinition = {
  type: 'Table',
  name: 'table',
  label: '基础表格',
  category: QUALITY_CATEGORY.BASIC,
  icon: QUALITY_ICONS.table,
  aiHint:
    '通用二维表格,如样品基础信息的两列键值表、设备清单。多行检验数据不要用我,用 QualityTable。',
  defaults: { headers: '项目,标准值,实测值,判定', rows: 2 },
  propertySchema: [
    {
      key: 'headers',
      label: '表头(英文逗号分隔)',
      type: 'string',
      defaultValue: '项目,标准值,实测值,判定',
    },
    {
      key: 'rows',
      label: '数据行数',
      type: 'number',
      defaultValue: 2,
      tip: '仅用于画布排版参考,渲染时可用检验项目数替换',
    },
  ],
  dataSchema: [],
  buildContent: (properties) => ({
    tagName: 'div',
    components: buildCells(parseHeaders(properties.headers), Number(properties.rows ?? 2)),
    style: { margin: '0 0 8px' },
  }),
};