fix(market): 1.修复模型超市 模糊搜索-模型名称异常的问题
feat(table): 2.新增三中心表格统一策略: - 覆盖 `/user-center/*`、`/org/*`、`/console/*`,包含系统设置。 - 自动识别最后一列的创建、查看、编辑、测试、详情、删除、设置、弹窗、API 回调等交互。 - 操作列统一显示“操作”,固定在右侧。 - 根据按钮和文字实际宽度测量,保持操作文字单行显示。 - 非操作宽表自动固定右侧末 4 / 3 / 2 / 1 列。 - 保留 Ant Design 官方横向滚动行为,不隐藏滚动条。
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import React, { forwardRef, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { Table, theme } from 'antd';
|
||||
import { colord } from 'colord';
|
||||
import { isCenterTableScope, prepareCenterColumns } from './centerTablePolicy';
|
||||
import './center-table.css';
|
||||
|
||||
const CenterTable = forwardRef(function CenterTable(
|
||||
{ columns, className, scroll, style, ...props },
|
||||
ref,
|
||||
) {
|
||||
const { token } = theme.useToken();
|
||||
const base = colord(token.colorBgContainer).toRgb();
|
||||
const solidColor = (color) => {
|
||||
const value = colord(color).toRgb();
|
||||
return colord({
|
||||
r: value.r * value.a + base.r * (1 - value.a),
|
||||
g: value.g * value.a + base.g * (1 - value.a),
|
||||
b: value.b * value.a + base.b * (1 - value.a),
|
||||
a: 1,
|
||||
}).toRgbString();
|
||||
};
|
||||
const tableRef = useRef(null);
|
||||
const [width, setWidth] = useState(0);
|
||||
const [measuredActionWidths, setMeasuredActionWidths] = useState({});
|
||||
const scoped = isCenterTableScope(
|
||||
typeof window === 'undefined' ? '' : window.location.pathname,
|
||||
);
|
||||
const prepared =
|
||||
scoped && columns
|
||||
? prepareCenterColumns(columns, width, measuredActionWidths)
|
||||
: columns;
|
||||
const enabled = prepared !== columns;
|
||||
useLayoutEffect(() => {
|
||||
if (!enabled) return;
|
||||
const root = tableRef.current?.nativeElement;
|
||||
if (!root) return;
|
||||
const scroller = root.querySelector('.ant-table-content,.ant-table-body');
|
||||
const previousTabIndex = scroller?.getAttribute('tabindex');
|
||||
if (scroller && previousTabIndex === null) scroller.tabIndex = 0;
|
||||
const update = () => setWidth(root.clientWidth);
|
||||
update();
|
||||
const measureActionWidths = () => {
|
||||
const next = {};
|
||||
root.querySelectorAll('th.center-action-cell').forEach((header) => {
|
||||
const marker = [...header.classList].find((name) =>
|
||||
name.startsWith('center-fixed-index-'),
|
||||
);
|
||||
if (!marker) return;
|
||||
const cells = [...root.querySelectorAll(`td.${marker}`)].filter(
|
||||
(element) => element.getBoundingClientRect().width > 0,
|
||||
);
|
||||
const cell = cells.find((candidate) =>
|
||||
candidate.querySelector('button, a, [role="button"]'),
|
||||
);
|
||||
const controls = cell
|
||||
? [...cell.querySelectorAll('button, a, [role="button"]')].filter(
|
||||
(element) => element.getBoundingClientRect().width > 0,
|
||||
)
|
||||
: [];
|
||||
const cellStyle = getComputedStyle(cell || header);
|
||||
const padding =
|
||||
(Number.parseFloat(cellStyle.paddingLeft) || 0) +
|
||||
(Number.parseFloat(cellStyle.paddingRight) || 0);
|
||||
const gap = Number.parseFloat(cellStyle.columnGap) || 8;
|
||||
const contentWidth = controls.length
|
||||
? controls.reduce(
|
||||
(total, element) => total + element.getBoundingClientRect().width,
|
||||
0,
|
||||
) +
|
||||
gap * Math.max(0, controls.length - 1)
|
||||
: Math.max(...cells.map((candidate) => candidate.scrollWidth), 0) -
|
||||
padding;
|
||||
next[marker.replace('center-fixed-index-', '')] =
|
||||
contentWidth + padding;
|
||||
});
|
||||
setMeasuredActionWidths((current) => {
|
||||
const same =
|
||||
Object.keys(next).length === Object.keys(current).length &&
|
||||
Object.keys(next).every(
|
||||
(key) => Math.abs(next[key] - current[key]) < 1,
|
||||
);
|
||||
return same ? current : next;
|
||||
});
|
||||
};
|
||||
requestAnimationFrame(measureActionWidths);
|
||||
const observer = new ResizeObserver(update);
|
||||
observer.observe(root);
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
if (scroller && previousTabIndex === null)
|
||||
scroller.removeAttribute('tabindex');
|
||||
};
|
||||
}, [enabled, width]);
|
||||
return (
|
||||
<Table
|
||||
{...props}
|
||||
ref={(instance) => {
|
||||
tableRef.current = instance;
|
||||
if (typeof ref === 'function') ref(instance);
|
||||
else if (ref) ref.current = instance;
|
||||
}}
|
||||
columns={prepared}
|
||||
className={[className, enabled && 'center-action-table']
|
||||
.filter(Boolean)
|
||||
.join(' ')}
|
||||
scroll={enabled ? { ...scroll, x: scroll?.x || 'max-content' } : scroll}
|
||||
style={
|
||||
enabled
|
||||
? {
|
||||
'--center-table-bg': token.colorBgContainer,
|
||||
'--center-table-head-bg': solidColor(token.colorFillAlter),
|
||||
'--center-table-hover-bg': solidColor(token.controlItemBgHover),
|
||||
'--center-table-selected-bg': solidColor(
|
||||
token.controlItemBgActive,
|
||||
),
|
||||
...style,
|
||||
}
|
||||
: style
|
||||
}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
export default CenterTable;
|
||||
@@ -0,0 +1,55 @@
|
||||
.center-action-table {
|
||||
min-width: 0;
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.center-action-table .ant-table-tbody > tr > .center-fixed-cell {
|
||||
z-index: 2;
|
||||
background: var(--center-table-bg);
|
||||
background-clip: padding-box;
|
||||
}
|
||||
|
||||
.center-action-table .ant-table-thead > tr > .center-fixed-cell {
|
||||
z-index: 3;
|
||||
background: var(--ur-head-bg, var(--center-table-head-bg));
|
||||
background-clip: padding-box;
|
||||
}
|
||||
|
||||
.center-action-table .ant-table-tbody > tr:hover > .center-fixed-cell {
|
||||
background: var(--center-table-hover-bg);
|
||||
}
|
||||
|
||||
.center-action-table
|
||||
.ant-table-tbody
|
||||
> tr.ant-table-row-selected
|
||||
> .center-fixed-cell {
|
||||
background: var(--center-table-selected-bg);
|
||||
}
|
||||
|
||||
.center-action-table .center-fixed-cell {
|
||||
white-space: normal;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.center-action-table .center-action-cell {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.center-action-table
|
||||
.center-action-cell
|
||||
:where(
|
||||
.ant-space,
|
||||
.ant-flex,
|
||||
.flex,
|
||||
.km-actions,
|
||||
.oc-row-actions,
|
||||
.org-row-actions,
|
||||
.console-module-row-actions,
|
||||
.console-channel-key-actions,
|
||||
.console-channel-model-actions,
|
||||
.admin-tokens-row-actions,
|
||||
.website-insights-row-actions
|
||||
) {
|
||||
flex-wrap: nowrap;
|
||||
max-width: none;
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
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 = {},
|
||||
) => ({
|
||||
...column,
|
||||
fixed: 'right',
|
||||
width: action
|
||||
? actionWidth(column, measuredWidths[index])
|
||||
: 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 = {}) {
|
||||
const leaves = leafColumns(columns);
|
||||
const last = leaves.at(-1);
|
||||
const hasAction = last && isActionColumn(last);
|
||||
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);
|
||||
if (fallback.has(column)) return markFixedRight(column, columnIndex);
|
||||
if (column.fixed) return { ...column, width: columnWidth(column) };
|
||||
return column;
|
||||
});
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
paginationShowTotal,
|
||||
} from './pagination';
|
||||
import { overlayMask } from './overlayPolicy';
|
||||
import CenterTable from './CenterActionTable';
|
||||
import './pagination.css';
|
||||
import { useIsMobile } from '../../hooks/common/useIsMobile';
|
||||
import { useConsolePage } from '../console/ConsoleContext';
|
||||
@@ -293,7 +294,7 @@ export function Table({
|
||||
...props
|
||||
}) {
|
||||
const consolePage = useConsolePage();
|
||||
const BaseTable = consolePage ? ConsoleTable : Ant.Table;
|
||||
const BaseTable = consolePage ? ConsoleTable : CenterTable;
|
||||
const unified = Boolean(pagination) && isCenterPaginationScope();
|
||||
const Component = unified ? CenterPaginatedTable : BaseTable;
|
||||
const expandable = expandedRowRender
|
||||
|
||||
Reference in New Issue
Block a user