163 lines
5.0 KiB
JavaScript
163 lines
5.0 KiB
JavaScript
import i18next from 'i18next';
|
|
|
|
export const isCenterTableScope = (pathname) =>
|
|
/^\/(user-center|org|console)(\/|$)/.test(pathname || '');
|
|
|
|
const titleText = (title) => {
|
|
if (typeof title === 'string') return title.trim();
|
|
if (Array.isArray(title)) return title.map(titleText).join('');
|
|
return title?.props ? titleText(title.props.children) : '';
|
|
};
|
|
|
|
export function isActionColumn(column) {
|
|
const title = titleText(column.title);
|
|
const key = String(column.key ?? column.dataIndex ?? '');
|
|
const source =
|
|
typeof column.render === 'function' ? column.render.toString() : '';
|
|
const titleSignal =
|
|
/^(操作|计费详情|编辑|测试|删除|添加|新增|设置|配置|创建|管理|actions?|operate|operations?)$/i.test(
|
|
title,
|
|
);
|
|
const detailTitleSignal = /^(详情|查看|打开|预览|结果|失败原因)$/i.test(
|
|
title,
|
|
);
|
|
const keySignal =
|
|
/^(actions|operate|operations|billingDetailAction|fail_reason|details?)$/i.test(
|
|
key,
|
|
) && typeof column.render === 'function';
|
|
const interactionSignal =
|
|
/\b(?:onClick|onOpen|onChange|Modal|Drawer|Popconfirm|Dropdown|Button|Link|API|request|fetch|navigate)\b/i.test(
|
|
source,
|
|
);
|
|
return Boolean(
|
|
titleSignal ||
|
|
keySignal ||
|
|
interactionSignal ||
|
|
(detailTitleSignal && (interactionSignal || key === 'fail_reason')),
|
|
);
|
|
}
|
|
|
|
const leafColumns = (columns) =>
|
|
columns.flatMap((column) =>
|
|
column.children ? leafColumns(column.children) : [column],
|
|
);
|
|
|
|
const columnTitleWidth = (column) => {
|
|
const title = titleText(column.title);
|
|
return Math.max(96, Math.min(260, 32 + [...title].length * 14));
|
|
};
|
|
|
|
const columnWidth = (column) => {
|
|
const width = Number(column.width);
|
|
return Number.isFinite(width) && width > 0 ? width : columnTitleWidth(column);
|
|
};
|
|
|
|
const actionWidth = (column, measuredWidth) => {
|
|
if (Number.isFinite(measuredWidth) && measuredWidth > 0)
|
|
return Math.ceil(Math.min(360, Math.max(56, measuredWidth)));
|
|
const source =
|
|
typeof column.render === 'function' ? column.render.toString() : '';
|
|
const count = (
|
|
source.match(
|
|
/<\s*(?:Button|Link|Dropdown|Popconfirm)\b|\b(?:Button|Link|Dropdown|Popconfirm|onClick|onOpen|onChange)\b/gi,
|
|
) || []
|
|
).length;
|
|
return Math.min(360, Math.max(56, 32 + Math.max(1, count) * 44));
|
|
};
|
|
|
|
const markFixedRight = (
|
|
column,
|
|
index,
|
|
action = false,
|
|
measuredWidths = {},
|
|
containerWidth = 0,
|
|
) => ({
|
|
...column,
|
|
fixed: 'right',
|
|
width: action
|
|
? Math.min(
|
|
actionWidth(column, measuredWidths[index]),
|
|
containerWidth > 0 ? Math.max(56, containerWidth) : Infinity,
|
|
)
|
|
: columnWidth(column),
|
|
...(action ? { title: i18next.t('操作') || '操作' } : {}),
|
|
className: [
|
|
column.className,
|
|
'center-fixed-cell',
|
|
action && 'center-action-cell',
|
|
`center-fixed-index-${index}`,
|
|
]
|
|
.filter(Boolean)
|
|
.join(' '),
|
|
});
|
|
|
|
export function prepareCenterColumns(
|
|
columns,
|
|
width = 0,
|
|
measuredWidths = {},
|
|
minScrollableWidth = 0,
|
|
) {
|
|
const leaves = leafColumns(columns);
|
|
const last = leaves.at(-1);
|
|
const hasAction = last && isActionColumn(last);
|
|
const fixedWidthLimit =
|
|
width > 0 ? Math.max(56, width - minScrollableWidth) : 0;
|
|
const otherFixedWidth = leaves.reduce(
|
|
(total, column) =>
|
|
column !== last && column.fixed ? total + columnWidth(column) : total,
|
|
0,
|
|
);
|
|
const remainingActionWidth = fixedWidthLimit - otherFixedWidth;
|
|
// Keep both sides fixed when wrapped action buttons still have enough space.
|
|
const actionWidthLimit =
|
|
minScrollableWidth > 0 && remainingActionWidth >= 120
|
|
? remainingActionWidth
|
|
: fixedWidthLimit;
|
|
const pinnedWidth = hasAction
|
|
? leaves.reduce(
|
|
(total, column, index) =>
|
|
total +
|
|
(column === last
|
|
? Math.min(
|
|
actionWidth(column, measuredWidths[index]),
|
|
actionWidthLimit || Infinity,
|
|
)
|
|
: column.fixed
|
|
? columnWidth(column)
|
|
: 0),
|
|
0,
|
|
)
|
|
: 0;
|
|
const releaseOtherFixed = width > 0 && pinnedWidth > fixedWidthLimit;
|
|
const estimatedTotal = leaves.reduce(
|
|
(total, column) => total + columnWidth(column),
|
|
0,
|
|
);
|
|
const overflow = width > 0 && estimatedTotal > width;
|
|
const fixedCount = Math.max(1, Math.min(4, leaves.length));
|
|
const fallback =
|
|
!hasAction && overflow ? new Set(leaves.slice(-fixedCount)) : new Set();
|
|
|
|
let index = 0;
|
|
return columns.map(function mapColumn(column) {
|
|
if (column.children)
|
|
return { ...column, children: column.children.map(mapColumn) };
|
|
const columnIndex = index++;
|
|
if (hasAction && column === last)
|
|
return markFixedRight(
|
|
column,
|
|
columnIndex,
|
|
true,
|
|
measuredWidths,
|
|
actionWidthLimit,
|
|
);
|
|
if (fallback.has(column)) return markFixedRight(column, columnIndex);
|
|
if (releaseOtherFixed && column.fixed) {
|
|
const { fixed: _fixed, ...scrollable } = column;
|
|
return scrollable;
|
|
}
|
|
if (column.fixed) return { ...column, width: columnWidth(column) };
|
|
return column;
|
|
});
|
|
}
|