mirror of https://github.com/msgbyte/tailchat
refactor: extract some utils function to utils
parent
ef7eace852
commit
2cad33b952
@ -0,0 +1,84 @@
|
|||||||
|
import { GroupPanel, GroupPanelType } from 'tailchat-shared';
|
||||||
|
import { rebuildGroupPanelOrder } from '../utils';
|
||||||
|
|
||||||
|
const testGroupPanels: GroupPanel[] = [
|
||||||
|
{
|
||||||
|
id: '00',
|
||||||
|
name: 'section-1',
|
||||||
|
type: GroupPanelType.GROUP,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '01',
|
||||||
|
name: 'panel-01',
|
||||||
|
type: GroupPanelType.TEXT,
|
||||||
|
parentId: '00',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '02',
|
||||||
|
name: 'panel-02',
|
||||||
|
type: GroupPanelType.TEXT,
|
||||||
|
parentId: '00',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '10',
|
||||||
|
name: 'section-2',
|
||||||
|
type: GroupPanelType.GROUP,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '11',
|
||||||
|
name: 'panel-11',
|
||||||
|
type: GroupPanelType.TEXT,
|
||||||
|
parentId: '10',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '12',
|
||||||
|
name: 'panel-12',
|
||||||
|
type: GroupPanelType.TEXT,
|
||||||
|
parentId: '10',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
describe('rebuildGroupPanelOrder', () => {
|
||||||
|
test('ref is changed', () => {
|
||||||
|
expect(rebuildGroupPanelOrder(testGroupPanels)).not.toBe(testGroupPanels);
|
||||||
|
expect(rebuildGroupPanelOrder(testGroupPanels)[0]).not.toBe(
|
||||||
|
testGroupPanels[0]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('keep order if right', () => {
|
||||||
|
expect(rebuildGroupPanelOrder(testGroupPanels)).toEqual(testGroupPanels);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('child should after parent', () => {
|
||||||
|
expect(
|
||||||
|
rebuildGroupPanelOrder([testGroupPanels[1], testGroupPanels[0]])
|
||||||
|
).toEqual([testGroupPanels[0], testGroupPanels[1]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('switch position should keep origin order', () => {
|
||||||
|
expect(
|
||||||
|
rebuildGroupPanelOrder([
|
||||||
|
testGroupPanels[1],
|
||||||
|
testGroupPanels[2],
|
||||||
|
testGroupPanels[0],
|
||||||
|
])
|
||||||
|
).toEqual([testGroupPanels[0], testGroupPanels[1], testGroupPanels[2]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('switch position should keep origin order(group)', () => {
|
||||||
|
expect(
|
||||||
|
rebuildGroupPanelOrder([
|
||||||
|
testGroupPanels[1],
|
||||||
|
testGroupPanels[2],
|
||||||
|
testGroupPanels[0],
|
||||||
|
testGroupPanels[3],
|
||||||
|
])
|
||||||
|
).toEqual([
|
||||||
|
testGroupPanels[0],
|
||||||
|
testGroupPanels[1],
|
||||||
|
testGroupPanels[2],
|
||||||
|
testGroupPanels[3],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,47 @@
|
|||||||
|
import _isNil from 'lodash/isNil';
|
||||||
|
import { GroupPanelType, GroupPanel as GroupPanelInfo } from 'tailchat-shared';
|
||||||
|
import type { DataNode } from 'antd/lib/tree';
|
||||||
|
import _cloneDeep from 'lodash/cloneDeep';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将一维的面板列表构筑成立体的数组
|
||||||
|
*/
|
||||||
|
export function buildTreeDataWithGroupPanel(
|
||||||
|
groupPanels: GroupPanelInfo[]
|
||||||
|
): DataNode[] {
|
||||||
|
return groupPanels
|
||||||
|
.filter((panel) => panel.type === GroupPanelType.GROUP)
|
||||||
|
.map<DataNode>((section) => ({
|
||||||
|
key: section.id,
|
||||||
|
title: section.name,
|
||||||
|
isLeaf: false,
|
||||||
|
children: groupPanels
|
||||||
|
.filter((panel) => panel.parentId === section.id)
|
||||||
|
.map<DataNode>((panel) => ({
|
||||||
|
key: panel.id,
|
||||||
|
title: panel.name,
|
||||||
|
isLeaf: true,
|
||||||
|
})),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重新构建面板顺序
|
||||||
|
*/
|
||||||
|
export function rebuildGroupPanelOrder(
|
||||||
|
groupPanels: GroupPanelInfo[]
|
||||||
|
): GroupPanelInfo[] {
|
||||||
|
const originGroupPanels = _cloneDeep(groupPanels);
|
||||||
|
const newPanel = originGroupPanels.filter((panel) => _isNil(panel.parentId)); // 第一层
|
||||||
|
const len = newPanel.length;
|
||||||
|
for (let i = len - 1; i >= 0; i--) {
|
||||||
|
const currentId = newPanel[i].id;
|
||||||
|
newPanel.splice(
|
||||||
|
i + 1,
|
||||||
|
0,
|
||||||
|
...originGroupPanels.filter((p) => p.parentId === currentId)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPanel;
|
||||||
|
}
|
Loading…
Reference in New Issue