import { uuid } from '../_utils/utils'; import { even, ternaryExpression } from '../../src/utils/common'; const checkMerge = (child: any, map: any) => { const key = String(child.getPropValue('key')); if ( even(Object.hasOwnProperty.call(map, key), child.componentName.includes(map[key]?.category)) ) { /** * if (map[key].category === 'AntdMenu.Item') { * child.setPropValue('label', map[key].label); * } else { * child.setPropValue('title', map[key].title || map[key].label); * } */ child.setPropValue('label', map[key].label); delete map[key]; return false; } return true; }; const mergeItems = (map: any) => { const items = []; for (const key in map) { const itemProps = map[key]; if (Object.hasOwnProperty.call(map, key)) { if (itemProps.category === 'AntdMenu.Item') { items.push({ componentName: 'AntdMenu.Item', props: { key: itemProps.key, label: itemProps.label, }, }); } else if (itemProps.category === 'AntdMenu.SubMenu') { items.push({ componentName: 'AntdMenu.SubMenu', props: { key: itemProps.key, label: itemProps.label, children: [ { componentName: 'AntdMenu.Item', props: ternaryExpression( itemProps.children && itemProps.children.length === 0, itemProps.children, [ { key: `item-${uuid()}`, type: 'group', category: 'AntdMenu.Item', label: '子菜单名', }, ], ), }, ], }, }); } else { items.push({ componentName: 'AntdMenu.ItemGroup', props: { key: itemProps.key, title: itemProps.title || itemProps.label, items: ternaryExpression( itemProps.items && itemProps.items.length === 0, itemProps.items, [ { key: `item-${uuid()}`, category: 'AntdMenu.Item', label: '子菜单名', }, ], ), }, }); } } } return items; }; export const itemsExtraProps = { getValue(target, fieldValue) { const map = target.node.children.map((child) => { const key = child.getPropValue('key') ? String(child.getPropValue('key')) : child.id; const result = { key, category: child.componentName, }; ['label', 'children'].forEach((propKey) => { if (child.getPropValue(propKey)) { result[propKey] = child.getPropValue(propKey); } }); return result; }); return map.length === 0 ? fieldValue : map; }, setValue(target, value) { const { node } = target; const map = {}; if (!Array.isArray(value)) { value = []; } value.forEach((item) => { map[item.key] = Object.assign({}, item); }); node.children.mergeChildren( (child) => checkMerge(child, map), () => mergeItems(map), (child1, child2) => { const a = value.findIndex( (item) => String(item.key) === String(child1.getPropValue('key')), ); const b = value.findIndex( (item) => String(item.key) === String(child2.getPropValue('key')), ); return a - b; }, ); }, };