刘光辉
7 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
import { cloneDeep } from 'lodash-es';
 
const preferredControlTypeOrder = [
  'input',
  'datePicker',
  'userSelect',
  'organizeSelect',
  'select',
  'popupSelect',
  'radio',
  'checkbox',
  'inputNumber',
  'textarea',
];
 
const controlTypeUsageCount = new Map([
  ['areaSelect', 3],
  ['autoComplete', 2],
  ['cascader', 3],
  ['checkbox', 48],
  ['datePicker', 468],
  ['editor', 3],
  ['groupSelect', 3],
  ['input', 2189],
  ['inputNumber', 151],
  ['location', 1],
  ['organizeSelect', 71],
  ['popupSelect', 179],
  ['popupTableSelect', 3],
  ['posSelect', 4],
  ['radio', 164],
  ['rate', 1],
  ['relationForm', 29],
  ['roleSelect', 2],
  ['select', 490],
  ['sign', 4],
  ['slider', 1],
  ['switch', 1],
  ['textarea', 136],
  ['timePicker', 1],
  ['treeSelect', 10],
  ['uploadFile', 47],
  ['uploadImg', 3],
  ['userSelect', 316],
]);
 
const preservedConfigKeys = [
  'biz_log_enabled',
  'className',
  'detailFormatRule',
  'detailVisibleRule',
  'dragDisabled',
  'formId',
  'isDisplayOnly',
  'isSubTable',
  'isVirtualField',
  'isVirtualFieldInherited',
  'label',
  'labelI18nCode',
  'labelWidth',
  'noShow',
  'parentVModel',
  'relationTable',
  'required',
  'showLabel',
  'span',
  'tableAlign',
  'tableFixed',
  'tableName',
  'tipLabel',
  'tipLabelI18nCode',
  'useDetailFormatRule',
  'useDetailVisibleRule',
  'visibility',
] as const;
 
const preservedCommonKeys = ['controlKey', 'disabled', 'placeholder', 'placeholderI18nCode', 'readonly', '__vModel__'] as const;
 
function copyDefinedKeys(source: Record<string, any>, target: Record<string, any>, keys: readonly string[]) {
  for (const key of keys) {
    if (source[key] !== undefined) target[key] = cloneDeep(source[key]);
  }
}
 
/** Rebuild a field from another component template while retaining its form identity and shared settings. */
export function convertControlType(activeData: Record<string, any>, targetTemplate: Record<string, any>) {
  const converted = cloneDeep(targetTemplate);
  copyDefinedKeys(activeData.__config__ || {}, converted.__config__, preservedConfigKeys);
  copyDefinedKeys(activeData, converted, preservedCommonKeys);
 
  if (activeData.style && converted.style) {
    converted.style = { ...converted.style, ...cloneDeep(activeData.style) };
  }
  if (activeData.on && converted.on) {
    for (const eventName of Object.keys(converted.on)) {
      if (activeData.on[eventName] !== undefined) converted.on[eventName] = activeData.on[eventName];
    }
  }
  converted.__config__.regList = [];
  converted.__config__.renderKey = Date.now();
  return converted;
}
 
export function replaceControlData(activeData: Record<string, any>, converted: Record<string, any>) {
  for (const key of Object.keys(activeData)) Reflect.deleteProperty(activeData, key);
  Object.assign(activeData, converted);
}
 
export function sortControlTypeOptionsByUsage<T extends { id: string }>(options: T[]) {
  return options
    .map((option, index) => ({ index, option }))
    .sort((a, b) => {
      const aPriority = preferredControlTypeOrder.indexOf(a.option.id);
      const bPriority = preferredControlTypeOrder.indexOf(b.option.id);
      if (aPriority !== -1 || bPriority !== -1) {
        return (aPriority === -1 ? Number.MAX_SAFE_INTEGER : aPriority) - (bPriority === -1 ? Number.MAX_SAFE_INTEGER : bPriority);
      }
      return (controlTypeUsageCount.get(b.option.id) || 0) - (controlTypeUsageCount.get(a.option.id) || 0) || a.index - b.index;
    })
    .map(({ option }) => option);
}