@antv/s2
Version:
effective spreadsheet render core lib
90 lines • 3.54 kB
JavaScript
import { find, groupBy, isEmpty, isEqual, merge } from 'lodash';
import { EXTRA_FIELD } from '../../common/constant';
const normalizeIcons = (icons = [], position = 'right') => {
return icons.map((icon) => {
if (typeof icon === 'string') {
return { name: icon, position };
}
return icon;
});
};
const normalizeActionIconCfg = (actionIconList = []) => actionIconList.map((actionIcon) => (Object.assign(Object.assign({}, actionIcon), { icons: normalizeIcons(actionIcon.icons) })));
const shouldShowActionIcons = (actionIconCfg, meta, cellType) => {
if (!actionIconCfg) {
return false;
}
const { icons, displayCondition, belongsCell } = actionIconCfg;
if (isEmpty(icons)) {
return false;
}
if (belongsCell !== cellType) {
return false;
}
// 有任意 iconName 命中展示,则使用当前 headerActionIcon config
return icons.some((icon) => {
var _a, _b, _c;
return (_c = (_b = (_a = icon.displayCondition) === null || _a === void 0 ? void 0 : _a.call(icon, meta, icon.name)) !== null && _b !== void 0 ? _b : displayCondition === null || displayCondition === void 0 ? void 0 : displayCondition(meta, icon.name)) !== null && _c !== void 0 ? _c : true;
});
};
/**
* 返回可用的 icon 配置
* @param actionIconCfgList icon 配置列表
* @param meta 单元格 meta
* @param cellType 单元格类型
* @returns icon 配置
*/
export const getActionIconConfig = (actionIconCfgList = [], meta, cellType) => {
const normalizedList = normalizeActionIconCfg(actionIconCfgList);
const iconConfig = find(normalizedList, (cfg) => shouldShowActionIcons(cfg, meta, cellType));
if (!iconConfig) {
return;
}
// 使用配置的 displayCondition 进一步筛选需要展示的 icon
const displayIcons = iconConfig.icons.filter((icon) => {
var _a, _b, _c, _d;
return (_d = (_b = (_a = icon.displayCondition) === null || _a === void 0 ? void 0 : _a.call(icon, meta, icon.name)) !== null && _b !== void 0 ? _b : (_c = iconConfig.displayCondition) === null || _c === void 0 ? void 0 : _c.call(iconConfig, meta, icon.name)) !== null && _d !== void 0 ? _d : true;
});
return Object.assign(Object.assign({}, iconConfig), { icons: displayIcons });
};
export const getIconTotalWidth = (icons = [], iconTheme) => {
if (isEmpty(icons)) {
return 0;
}
const { margin, size } = iconTheme;
return icons.reduce((acc, { position }) => acc + size + (position === 'left' ? margin.right : margin.left), 0);
};
export const groupIconsByPosition = (icons = [], conditionIcon) => {
const groupedIcons = merge({
left: [],
right: [],
}, groupBy(icons, 'position'));
// 基于 condition icon 和 value 是强关联的,所以最好将 condition icon 放在 value 的左右侧
if (conditionIcon) {
if (conditionIcon.position === 'left') {
groupedIcons.left.push(conditionIcon);
}
else {
groupedIcons.right.unshift(conditionIcon);
}
}
return groupedIcons;
};
/**
* 格式化行列头维度名称
* @param meta
* @param fieldName
*/
export const formattedFieldValue = (meta, fieldName) => {
const { value, field } = meta;
if (!isEqual(field, EXTRA_FIELD)) {
return {
formattedValue: value,
value,
};
}
return {
formattedValue: fieldName || value,
value,
};
};
//# sourceMappingURL=header-cell.js.map