fix(market): 修复模型名称模糊搜索并统一三中心表格操作列

统一用户中心、企业中心和控制台的操作列标题、宽度与右侧固定规则,并按表格宽度固定末列,保留横向滚动。
This commit is contained in:
zhanjunhao
2026-11-17 10:10:28 +08:00
parent 822d26c0dd
commit 3dc2624e00
25 changed files with 595 additions and 41 deletions
+143
View File
@@ -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;