Skip to content

Commit

Permalink
fix(util): fix moveElementPosition move logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshenhai committed Jun 16, 2024
1 parent 9ea6699 commit 3049a23
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 74 deletions.
84 changes: 17 additions & 67 deletions packages/util/__tests__/lib/handle-element.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { moveElementPosition, deepClone } from '@idraw/util';
import { moveElementPosition } from '@idraw/util';
import type { Elements } from '@idraw/types';
const getElemBase = () => {
return {
Expand All @@ -8,72 +8,9 @@ const getElemBase = () => {
h: 1
};
};
// const elements: Elements = [
// {
// ...getElemBase(),
// uuid: 'rect-01',
// type: 'rect',
// detail: {}
// },
// {
// ...getElemBase(),
// uuid: 'rect-02',
// type: 'rect',
// detail: {}
// },
// {
// ...getElemBase(),
// uuid: 'rect-03',
// type: 'rect',
// detail: {}
// },
// {
// ...getElemBase(),
// uuid: 'group-01',
// type: 'group',
// detail: {
// children: [
// {
// ...getElemBase(),
// uuid: 'rect-04',
// type: 'rect',
// detail: {}
// },
// {
// ...getElemBase(),
// uuid: 'rect-05',
// type: 'rect',
// detail: {}
// },

// {
// ...getElemBase(),
// uuid: 'group-02',
// type: 'group',
// detail: {
// children: [
// {
// ...getElemBase(),
// uuid: 'rect-06',
// type: 'rect',
// detail: {}
// },
// {
// ...getElemBase(),
// uuid: 'rect-07',
// type: 'rect',
// detail: {}
// }
// ]
// }
// }
// ]
// }
// }
// ];

function generateElements(list: any[]): Elements {
const elements: Elements = list.map((item, i) => {
const elements: Elements = list.map((item) => {
if (Array.isArray(item)) {
return {
...getElemBase(),
Expand All @@ -86,7 +23,7 @@ function generateElements(list: any[]): Elements {
} else {
return {
...getElemBase(),
uuid: `rect`,
uuid: `rect-${item}`,
type: 'rect',
detail: {}
};
Expand Down Expand Up @@ -165,7 +102,7 @@ describe('@idraw/util: handle-element ', () => {
expect(list).toStrictEqual(expectResult);
});

//
// [1] -> [1]
test('moveElementPosition, move-up [1] -> [1]', () => {
const list: Elements = generateElements([0, 1, 2, [0, 1, [0, 1, 2, 3], 3], 4, 5]);

Expand All @@ -177,4 +114,17 @@ describe('@idraw/util: handle-element ', () => {
const expectResult = generateElements([0, 1, 2, [0, 1, [0, 1, 2, 3], 3], 4, 5]);
expect(list).toStrictEqual(expectResult);
});

// [2, 4] -> [1, 2]
test('moveElementPosition, move-up [1, 2] -> [2, 4]', () => {
const list: Elements = generateElements([0, [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], 3, 4]);

moveElementPosition(list, {
from: [2, 4],
to: [1, 2]
});
const expectResult = generateElements([0, [0, 1, 4, 2, 3, 4], [0, 1, 2, 3], 3, 4]);

expect(list).toStrictEqual(expectResult);
});
});
71 changes: 64 additions & 7 deletions packages/util/src/lib/handle-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,72 @@ export function moveElementPosition(
let trimDeletePosIndex = -1;
const trimDeletePosAction = 'down'; // +1

for (let i = 0; i < from.length; i++) {
if (!(to[i] >= 0)) {
break;
let isEffectToIndex = false;

if (from.length >= 1 && to.length >= 1) {
// isEffectToIndex
// false [2, 4] -> [1, 2]
// false [3, 4, 5] -> [4, 5]

// up -> down
// true [2] -> [4]
// true [2] -> [3, 4]
// true [2, 3] -> [2, 3, 4]
if (from.length <= to.length) {
if (from.length === 1) {
if (from[0] < to[0]) {
isEffectToIndex = true;
}
} else {
for (let i = 0; i < from.length; i++) {
if (from[i] === to[i]) {
if (from.length === from.length - 1) {
isEffectToIndex = true;
break;
}
} else {
break;
}
}
}
}
if (to[i] === from[i]) {
continue;

// down -> up
// true [4] -> [2]
// true [3, 4, 5] -> [3, 3]
// true [3, 4, 5] -> [2]
if (from.length >= to.length) {
if (to.length === 1) {
if (to[0] < from[0]) {
isEffectToIndex = true;
}
} else {
for (let i = 0; i < to.length; i++) {
if (i === to.length - 1 && to[i] < from[i]) {
isEffectToIndex = true;
}
if (from[i] === to[i]) {
continue;
} else {
break;
}
}
}
}
if (to[i] < from[i] && i == to.length - 1) {
trimDeletePosIndex = i;
}

if (isEffectToIndex === true) {
for (let i = 0; i < from.length; i++) {
if (!(to[i] >= 0)) {
break;
}
if (to[i] === from[i]) {
continue;
}

if (to[i] < from[i] && i == to.length - 1) {
trimDeletePosIndex = i;
}
}
}

Expand Down

0 comments on commit 3049a23

Please sign in to comment.