From 33429a0cc4b83bbb71780fcaa88ac1055a1da2a6 Mon Sep 17 00:00:00 2001 From: Axel Boberg Date: Mon, 22 Apr 2024 01:34:13 +0200 Subject: [PATCH] Fix issues with reordering items in the rundown Signed-off-by: Axel Boberg --- plugins/rundown/index.js | 62 ++++++++++++++++++++++++---------------- shared/merge.js | 2 +- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/plugins/rundown/index.js b/plugins/rundown/index.js index 2ee9d86..a23587a 100644 --- a/plugins/rundown/index.js +++ b/plugins/rundown/index.js @@ -158,7 +158,7 @@ exports.activate = async () => { any current parent */ if (item.parent !== newParentId) { - await removeItemsFromParent(item.parent, [item.id]) + await removeItemsFromParent(item.parent, [itemId]) } /* @@ -170,13 +170,29 @@ exports.activate = async () => { return appendItem(newParentId, itemId) } - const oldIndex = siblings.indexOf(itemId) - const weightedNewIndex = oldIndex < newIndex && oldIndex > -1 ? newIndex - 1 : newIndex + let oldIndex = siblings.indexOf(itemId) if (oldIndex === newIndex) { return } + /* + Insert the item at the new index + */ + siblings.splice(newIndex, 0, itemId) + + if (newIndex < oldIndex) { + oldIndex += 1 + } + + /* + Remove the old index if the item + just moves within its parent + */ + if (item.parent === newParentId) { + siblings.splice(oldIndex, 1) + } + /* A list of patches to apply to the state, @@ -192,33 +208,28 @@ exports.activate = async () => { } }] - /* - Only remove the old index if it - is in the current rundown - */ - if (oldIndex !== -1) { - patches.push({ - items: { - [newParentId]: { - children: { - [oldIndex]: { $delete: true } - } - } - } - }) - } - /* Insert the item at the correct index in the new rundown */ - patches.push({ - items: { - [newParentId]: { - children: { $insert: itemId, $index: Math.max(0, weightedNewIndex) } + patches.push( + ...[ + { + items: { + [newParentId]: { + children: [] + } + } + }, + { + items: { + [newParentId]: { + children: { $replace: siblings } + } + } } - } - }) + ] + ) await bridge.state.apply(patches) bridge.events.emit('item.change', itemId) @@ -272,6 +283,7 @@ exports.activate = async () => { .reduce((prev, cur) => { return [...prev, ...cur] }, []) + .filter(item => item) return JSON.stringify(items) } diff --git a/shared/merge.js b/shared/merge.js index 8ce752d..a401ad0 100644 --- a/shared/merge.js +++ b/shared/merge.js @@ -11,7 +11,7 @@ * @param { any } sourceObj * @returns { any } The target object */ -function mergeDeep (targetObj, sourceObj) { +function mergeDeep (targetObj = {}, sourceObj = {}) { for (const key of Object.keys(sourceObj)) { /* If the $replace keyword is used,