fix: reordering out of a block

This commit is contained in:
Carlos Valente
2025-07-04 21:42:18 +02:00
committed by Carlos Valente
parent c32203c759
commit 3790681bec
5 changed files with 54 additions and 12 deletions
@@ -926,7 +926,7 @@ describe('rundownMutation.reorder()', () => {
expect(rundown.order).toStrictEqual(['3', '1', '2']);
});
it('moves an event out of a block', () => {
it('moves an event out and before a block', () => {
const rundown = makeRundown({
order: ['1', '2'],
flatOrder: ['1', '11', '2'],
@@ -943,6 +943,35 @@ describe('rundownMutation.reorder()', () => {
expect(rundown.entries['1']).toMatchObject({
entries: [],
});
expect(rundown.entries['11']).toMatchObject({
parent: null,
});
expect(rundown.entries['2']).toMatchObject({
parent: null,
});
});
it('moves an event out and after a block', () => {
const rundown = makeRundown({
order: ['1', 'block', '2'],
flatOrder: ['1', 'block', '11', '2'],
entries: {
'1': makeOntimeEvent({ id: '1', parent: null }),
block: makeOntimeBlock({ id: 'block', entries: ['11'] }),
'11': makeOntimeEvent({ id: '11', parent: 'block' }),
'2': makeOntimeEvent({ id: '2', parent: null }),
},
});
rundownMutation.reorder(rundown, rundown.entries['11'], rundown.entries['block'], 'after');
expect(rundown.order).toStrictEqual(['1', 'block', '11', '2']);
expect(rundown.entries['block']).toMatchObject({
entries: [],
});
expect(rundown.entries['11']).toMatchObject({
parent: null,
});
expect(rundown.entries['2']).toMatchObject({
parent: null,
});
@@ -289,6 +289,10 @@ function reorder(rundown: Rundown, eventFrom: OntimeEntry, eventTo: OntimeEntry,
const fromParent: EntryId | null = (eventFrom as { parent?: EntryId })?.parent ?? null;
const toParent = (() => {
if (isOntimeBlock(eventTo)) {
// Special case: if we're moving relative to our own parent block, remove from block
if ('parent' in eventFrom && eventFrom.parent === eventTo.id) {
return null;
}
if (order === 'insert') {
// prevent blocks from being inserted into other blocks
if (isOntimeBlock(eventFrom)) {
@@ -313,8 +317,13 @@ function reorder(rundown: Rundown, eventFrom: OntimeEntry, eventTo: OntimeEntry,
const toIndex = (() => {
const baseIndex = destinationArray.indexOf(eventTo.id);
if (order === 'before') return baseIndex;
// only add one if we are moving down
if (order === 'after') return baseIndex + (fromIndex < baseIndex ? 0 : 1);
if (order === 'after') {
// When moving within the same array, we need to consider the source position
if (sourceArray === destinationArray && fromIndex <= baseIndex) {
return baseIndex;
}
return baseIndex + 1;
}
// for insert we add in the end of the array
return destinationArray.length;
})();
@@ -247,6 +247,7 @@ export function reorderEntry(entryId: EntryId, destinationId: EntryId, order: 'b
throw new Error('Event not found');
}
console.log('reorder', eventFrom.id, eventTo.id, order);
rundownMutation.reorder(rundown, eventFrom, eventTo, order);
const { rundown: rundownResult, rundownMetadata, revision } = commit();