diff --git a/packages/util/__tests__/lib/handle-element.test.ts b/packages/util/__tests__/lib/handle-element.test.ts index 42c4db1ed..3b770249a 100644 --- a/packages/util/__tests__/lib/handle-element.test.ts +++ b/packages/util/__tests__/lib/handle-element.test.ts @@ -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); + }); }); diff --git a/packages/util/src/lib/handle-element.ts b/packages/util/src/lib/handle-element.ts index fe655be7c..f4f13cc4e 100644 --- a/packages/util/src/lib/handle-element.ts +++ b/packages/util/src/lib/handle-element.ts @@ -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; + } } }