From 43aea696a1fcc7ad891b06dcf9e57c7ded106721 Mon Sep 17 00:00:00 2001 From: wangjiahao <1522128093@qq.com> Date: Wed, 10 Nov 2021 11:07:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=E4=BC=98=E5=8C=96=E4=BB=AA=E8=A1=A8?= =?UTF-8?q?=E6=9D=BF=E4=B8=8A=E7=A7=BB=EF=BC=8C=E4=B8=8B=E7=A7=BB=EF=BC=8C?= =?UTF-8?q?=E7=BD=AE=E9=A1=B6=EF=BC=8C=E7=BD=AE=E5=BA=95=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E5=8F=AF=E4=BB=A5=E5=AE=9E=E6=97=B6=E5=BE=97?= =?UTF-8?q?=E5=88=B0=E7=A7=BB=E5=8A=A8=E6=95=88=E6=9E=9C=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E7=BD=AE=E9=A1=B6=E4=B8=8D=E5=87=86=E7=A1=AE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/canvas/store/layer.js | 10 +++++----- frontend/src/components/canvas/utils/utils.js | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/canvas/store/layer.js b/frontend/src/components/canvas/store/layer.js index a5554be878..6bbb56eb4f 100644 --- a/frontend/src/components/canvas/store/layer.js +++ b/frontend/src/components/canvas/store/layer.js @@ -1,4 +1,4 @@ -import { swap } from '@/components/canvas/utils/utils' +import { swap, toBottom, toTop, moveUp, moveDown } from '@/components/canvas/utils/utils' import toast from '@/components/canvas/utils/toast' export default { @@ -6,7 +6,7 @@ export default { upComponent({ componentData, curComponentIndex }) { // 上移图层 index,表示元素在数组中越往后 if (curComponentIndex < componentData.length - 1) { - swap(componentData, curComponentIndex, curComponentIndex + 1) + moveUp(componentData, curComponentIndex) } else { toast('已经到顶了') } @@ -15,7 +15,7 @@ export default { downComponent({ componentData, curComponentIndex }) { // 下移图层 index,表示元素在数组中越往前 if (curComponentIndex > 0) { - swap(componentData, curComponentIndex, curComponentIndex - 1) + moveDown(componentData, curComponentIndex) } else { toast('已经到底了') } @@ -24,7 +24,7 @@ export default { topComponent({ componentData, curComponentIndex }) { // 置顶 if (curComponentIndex < componentData.length - 1) { - swap(componentData, curComponentIndex, componentData.length - 1) + toTop(componentData, curComponentIndex) } else { toast('已经到顶了') } @@ -33,7 +33,7 @@ export default { bottomComponent({ componentData, curComponentIndex }) { // 置底 if (curComponentIndex > 0) { - swap(componentData, curComponentIndex, 0) + toBottom(componentData, curComponentIndex) } else { toast('已经到底了') } diff --git a/frontend/src/components/canvas/utils/utils.js b/frontend/src/components/canvas/utils/utils.js index cdce9342c3..fdd236650b 100644 --- a/frontend/src/components/canvas/utils/utils.js +++ b/frontend/src/components/canvas/utils/utils.js @@ -16,11 +16,28 @@ export function deepCopy(target) { } export function swap(arr, i, j) { + console.log('before:' + i + '-->' + j + JSON.stringify(arr)) const temp = arr[i] arr[i] = arr[j] arr[j] = temp + console.log('after:' + JSON.stringify(arr)) } +export function moveUp(arr, i) { + arr.splice(i + 1, 0, arr.splice(i, 1)[0]) +} + +export function moveDown(arr, i) { + arr.splice(i, 0, arr.splice(i - 1, 1)[0]) +} + +export function toTop(arr, i, j) { + arr.push(arr.splice(i, 1)[0]) +} + +export function toBottom(arr, i) { + arr.unshift(arr.splice(i, 1)[0]) + } export function $(selector) { return document.querySelector(selector) }