开发者报障(2026-10-15):「拖动效果不是很丝滑而且卡着;能拖动但是位置交换出现了问题; 鼠标松动还是处于按中的状态无法进行位置的拖动交换」 三处成因(页面 src/pages/system/MenuLayoutConfig.tsx,预览列表与组内模块各一套拖拽,问题同时存在): ① 松手仍"按中":长按计时器(LONG_PRESS_MS=450)只在**行元素**上挂 onPointerUp ⇒ 指针离开该行再松手时元素级事件不触发 ⇒ 450ms 后计时器照样把页面推进拖拽态 (body.cursor=grabbing+行被抬起),而鼠标其实已松开 ⇒ 此后没有 pointerup 能复位。 ② 位置交换错乱:悬停下标用页面内联循环,**把被拖行自身也当边界** (for i: if (pointerY < metrics[i].center) return i,i 含 fromIndex), 而拖动时其余行"各让一个槽位"、被拖行被 translateY 抬起 ⇒ 下标与视觉槽位错开一位。 ③ 卡:每个 pointermove 直接 setState 两次(位移+悬停下标),本页近 1300 行 ⇒ 整页重渲染掉帧。 修复: - 新增纯算法 src/utils/menuLayoutDrag.ts:resolveRowDropIndex(metrics, pointerY, fromIndex, fallback) —— 先排除被拖行自身,再按"其余行里有多少个中心在指针上方"取插入槽位(该槽位即最终下标, 与 reorderGroupMenuItems 的"先删后插"语义一致);两处解析函数改为委托它 - 长按挂起时在 window 上兜底取消(pointerup/pointercancel,校验同一 pointerId), 清理函数里 detach() 解绑监听(两处拖拽各一套)⇒ 行外松手不再凭空进入拖拽 - 两个 effect 的 pointermove 改 requestAnimationFrame 合并(每帧只提交一次)+跳过值未变的提交, 清理时 cancelAnimationFrame 验证: - 新增 src/utils/menuLayoutDrag.test.ts 7/7:下标语义(上拖/下拖/边界/脏数据/单行)+ reorderGroupMenuItems 最终顺序(往下移 0→2 不应差一位、往上 3→0、拖到末尾、同位无操作) - 新增 src/pages/system/MenuLayoutConfig.dragContract.test.ts 5/5:窗口级取消与 detach、 纯算法替代内联循环、rAF 合并与"值未变不提交" - 前端 npx tsc --noEmit 0 错误;前端全量 vitest 3 文件/6 用例失败 = 与基线一致(同名三文件) - 后端全量 vitest 26 文件/33 用例失败 = 基线一致、无新增失败文件 - 设计门禁 run-all.sh --verify:通过 8 / 未通过 1(既有红)/ 未执行 1 = 基线 - ⚠️ 诚实标注:整页挂进 jsdom 做指针交互测试会让 vitest worker 异常退出(实测 103s 无结果), 故未做浏览器级交互测试;"是否丝滑"的观感需部署后人眼确认(算法与接线已由上述 12 例覆盖) ⚠️ 生效条件:前端需**重新构建并部署**;无 DB 变更、无迁移、无后端改动。 设计:docs/design/20261015-导航菜单拖动排序修复(松手仍按中与位置交换错乱).md
109 lines
4.4 KiB
TypeScript
109 lines
4.4 KiB
TypeScript
/**
|
|
* 长按拖动排序的纯算法测试(2026-10-15 缺陷:「位置交换出现问题」)。
|
|
*
|
|
* 旧实现在页面内联、把**被拖行自身**也当边界参与判定 ⇒ 与"其余行各让一个槽位"的视觉位移
|
|
* 错开一位,放下后位置不对/来回跳。这里钉住新语义(见 `menuLayoutDrag.ts` 注释的推导),
|
|
* 并顺带钉住 `reorderGroupMenuItems` 的"先删后插"语义(它本身是对的,别被后续重构改坏)。
|
|
*/
|
|
import { describe, expect, it } from 'vitest';
|
|
import { resolveRowDropIndex, type RowMetric } from './menuLayoutDrag';
|
|
import {
|
|
createDefaultNavMenuLayout,
|
|
reorderGroupMenuItems,
|
|
type NavMenuLayoutConfig,
|
|
} from './menuLayout';
|
|
|
|
/** 4 行、每行高 40、top 0/40/80/120 ⇒ center 20/60/100/140 */
|
|
const rows4: RowMetric[] = [0, 40, 80, 120].map((top) => ({
|
|
top,
|
|
height: 40,
|
|
center: top + 20,
|
|
}));
|
|
|
|
describe('resolveRowDropIndex:指针 Y → 目标下标', () => {
|
|
it('往上拖:跨过最上面那行的中心 ⇒ 落到 0 号位', () => {
|
|
expect(resolveRowDropIndex(rows4, 10, 2)).toBe(0);
|
|
expect(resolveRowDropIndex(rows4, 30, 2)).toBe(1); // 只在第 1 行中心(20)之下 ⇒ 槽位 1
|
|
});
|
|
|
|
it('往下拖:每跨过一个"其余行"的中心就前进一位', () => {
|
|
// 拖第 0 行:其余行中心 60/100/140
|
|
expect(resolveRowDropIndex(rows4, 50, 0)).toBe(0); // 谁都没跨过
|
|
expect(resolveRowDropIndex(rows4, 70, 0)).toBe(1); // 跨过 60
|
|
expect(resolveRowDropIndex(rows4, 110, 0)).toBe(2); // 跨过 60/100
|
|
expect(resolveRowDropIndex(rows4, 150, 0)).toBe(3); // 全部跨过 ⇒ 落到最后
|
|
});
|
|
|
|
it('被拖行自身不参与判定(旧实现的错位来源)', () => {
|
|
// 拖第 1 行、指针停在自己区间内 ⇒ 下标不变(不含自身中心 60 也能得出 1)
|
|
expect(resolveRowDropIndex(rows4, 61, 1)).toBe(1);
|
|
expect(resolveRowDropIndex(rows4, 59, 1)).toBe(1);
|
|
// 拖第 3 行(最后一行)到最前
|
|
expect(resolveRowDropIndex(rows4, 5, 3)).toBe(0);
|
|
});
|
|
|
|
it('边界与脏数据:越界钳到合法下标;空/全空度量回退到 fallback', () => {
|
|
expect(resolveRowDropIndex(rows4, 99999, 1)).toBe(3); // 上限 = 最后一行
|
|
expect(resolveRowDropIndex(rows4, -9999, 1)).toBe(0);
|
|
expect(resolveRowDropIndex([], 50, 2)).toBe(2);
|
|
expect(resolveRowDropIndex([null, null], 50, 1)).toBe(1);
|
|
expect(resolveRowDropIndex([{ top: 0, height: 40, center: 20 }], 500, 0)).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('reorderGroupMenuItems:最终顺序(先删后插语义)', () => {
|
|
const layout: NavMenuLayoutConfig = createDefaultNavMenuLayout();
|
|
const groupId = layout.groups[0]?.id || 'grp-test';
|
|
const withItems: NavMenuLayoutConfig = {
|
|
...layout,
|
|
groups: [
|
|
{
|
|
...(layout.groups[0] || { id: groupId, label: 'g', icon: '', visible: true, items: [] }),
|
|
id: groupId,
|
|
items: [
|
|
{ menuKey: 'orders-list', visible: true },
|
|
{ menuKey: 'customers-list', visible: true },
|
|
{ menuKey: 'products-skus', visible: true },
|
|
{ menuKey: 'inventory', visible: true },
|
|
],
|
|
},
|
|
],
|
|
} as NavMenuLayoutConfig;
|
|
|
|
const keys = (next: NavMenuLayoutConfig) =>
|
|
(next.groups.find((g) => g.id === groupId)?.items || []).map((i) => i.menuKey);
|
|
|
|
it('往下移 0 → 2:落到目标槽位(不是差一位)', () => {
|
|
expect(keys(reorderGroupMenuItems(withItems, groupId, 0, 2))).toEqual([
|
|
'customers-list',
|
|
'products-skus',
|
|
'orders-list',
|
|
'inventory',
|
|
]);
|
|
});
|
|
|
|
it('往上移 3 → 0 / 拖到末尾 / 同位置无操作', () => {
|
|
expect(keys(reorderGroupMenuItems(withItems, groupId, 3, 0))).toEqual([
|
|
'inventory',
|
|
'orders-list',
|
|
'customers-list',
|
|
'products-skus',
|
|
]);
|
|
expect(keys(reorderGroupMenuItems(withItems, groupId, 0, 3))).toEqual([
|
|
'customers-list',
|
|
'products-skus',
|
|
'inventory',
|
|
'orders-list',
|
|
]);
|
|
expect(reorderGroupMenuItems(withItems, groupId, 1, 1)).toBe(withItems);
|
|
});
|
|
});
|
|
|
|
describe('算法与视觉槽位一致(组合用例)', () => {
|
|
it('拖第 0 行到第 2 行中心的略上方 ⇒ 下标 1(视觉上正好占第 2 槽)', () => {
|
|
const target = resolveRowDropIndex(rows4, 95, 0); // 95 < 100(第 2 行中心)
|
|
expect(target).toBe(1);
|
|
expect(resolveRowDropIndex(rows4, 105, 0)).toBe(2); // 略过中心 ⇒ 第 3 槽
|
|
});
|
|
});
|