mirror of
https://github.com/idrawjs/idraw
synced 2026-05-24 10:08:34 +00:00
fix(util): fix moveElementPosition move logic
This commit is contained in:
parent
9ea6699e3f
commit
3049a23d26
2 changed files with 81 additions and 74 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { moveElementPosition, deepClone } from '@idraw/util';
|
||||
import { moveElementPosition } from '@idraw/util';
|
||||
import type { Elements } from '@idraw/types';
|
||||
const getElemBase = () => {
|
||||
return {
|
||||
|
|
@ -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(),
|
||||
|
|
@ -86,7 +23,7 @@ function generateElements(list: any[]): Elements {
|
|||
} else {
|
||||
return {
|
||||
...getElemBase(),
|
||||
uuid: `rect`,
|
||||
uuid: `rect-${item}`,
|
||||
type: 'rect',
|
||||
detail: {}
|
||||
};
|
||||
|
|
@ -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]);
|
||||
|
||||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue