summaryrefslogtreecommitdiff
path: root/apps/web/src/commands/timeline/element/shift-split-remainder.ts
blob: 3799c8f936a3d9471f5f3a2ae40a735e956c0016 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Command, type CommandResult } from "@/commands/base-command";
import type { MediaTime } from "@/wasm";
import { MoveElementCommand } from "./move-elements";
import type { SplitElementsCommand } from "./split-elements";

/**
 * Moves the right-side elements produced by a SplitElementsCommand to a new
 * start time. Must run after the split inside a BatchCommand: the right-side
 * element ids only exist once the split has executed. Rebuilds the inner move
 * on every execute() so redo picks up the fresh ids a re-executed split
 * generates.
 */
export class ShiftSplitRemainderCommand extends Command {
	private inner: MoveElementCommand | null = null;

	constructor({
		split,
		newStartTime,
	}: {
		split: SplitElementsCommand;
		newStartTime: MediaTime;
	}) {
		super();
		this.split = split;
		this.newStartTime = newStartTime;
	}

	private readonly split: SplitElementsCommand;
	private readonly newStartTime: MediaTime;

	execute(): CommandResult | undefined {
		const rightSideElements = this.split.getRightSideElements();
		if (rightSideElements.length === 0) {
			return undefined;
		}
		this.inner = new MoveElementCommand({
			moves: rightSideElements.map((element) => ({
				sourceTrackId: element.trackId,
				targetTrackId: element.trackId,
				elementId: element.elementId,
				newStartTime: this.newStartTime,
			})),
		});
		return this.inner.execute();
	}

	undo(): void {
		this.inner?.undo();
	}
}