Skip to content

Commit

Permalink
Fix issues with reordering items in the rundown
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Boberg <[email protected]>
  • Loading branch information
axelboberg committed Apr 21, 2024
1 parent 39b8bf4 commit 33429a0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
62 changes: 37 additions & 25 deletions plugins/rundown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

/*
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -272,6 +283,7 @@ exports.activate = async () => {
.reduce((prev, cur) => {
return [...prev, ...cur]
}, [])
.filter(item => item)

return JSON.stringify(items)
}
Expand Down
2 changes: 1 addition & 1 deletion shared/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 33429a0

Please sign in to comment.