diff --git a/src/common/util/array-move.ts b/src/common/util/array-move.ts
index 0985f764c0..33ce6bb8e0 100644
--- a/src/common/util/array-move.ts
+++ b/src/common/util/array-move.ts
@@ -20,6 +20,15 @@ function findNestedItem(
}, obj);
}
+function updateNestedItem(obj: any, path: ItemPath): any {
+ const lastKey = path.pop()!;
+ const parent = findNestedItem(obj, path);
+ parent[lastKey] = Array.isArray(parent[lastKey])
+ ? [...parent[lastKey]]
+ : [parent[lastKey]];
+ return obj;
+}
+
export function nestedArrayMove(
obj: A,
oldIndex: number,
@@ -27,14 +36,18 @@ export function nestedArrayMove(
oldPath?: ItemPath,
newPath?: ItemPath
): A {
- const newObj = (Array.isArray(obj) ? [...obj] : { ...obj }) as A;
+ let newObj = (Array.isArray(obj) ? [...obj] : { ...obj }) as A;
+
+ if (oldPath) {
+ newObj = updateNestedItem(newObj, [...oldPath]);
+ }
+ if (newPath) {
+ newObj = updateNestedItem(newObj, [...newPath]);
+ }
+
const from = oldPath ? findNestedItem(newObj, oldPath) : newObj;
const to = newPath ? findNestedItem(newObj, newPath, true) : newObj;
- if (!Array.isArray(from) || !Array.isArray(to)) {
- return obj;
- }
-
const item = from.splice(oldIndex, 1)[0];
to.splice(newIndex, 0, item);