Skip to content

Commit 9c2ef79

Browse files
committed
ensured clips can be inserted at an index
1 parent 12a6081 commit 9c2ef79

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

src/clips/clip/clip.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ export class Clip<Props extends ClipProps = ClipProps> extends EventEmitterMixin
230230

231231
replaceKeyframes(copy, copy.start.subtract(this.start));
232232

233-
await this.track.add(copy);
233+
const index = this.track.clips.findIndex((c) => c.id == this.id);
234+
await this.track.add(copy, index + 1);
234235

235236
return copy;
236237
}

src/clips/media/media.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ export class MediaClip<Props extends MediaClipProps = MediaClipProps> extends Cl
233233
}
234234

235235
public copy(): MediaClip {
236-
return MediaClip.fromJSON(JSON.parse(JSON.stringify(this)));
236+
const clip = MediaClip.fromJSON(JSON.parse(JSON.stringify(this)));
237+
clip.transcript = this.transcript;
238+
return clip;
237239
}
238240

239241
public async split(time?: frame | Timestamp): Promise<this> {
@@ -267,7 +269,8 @@ export class MediaClip<Props extends MediaClipProps = MediaClipProps> extends Cl
267269

268270
replaceKeyframes(copy, copy.start.subtract(this.start));
269271

270-
await this.track.add(copy);
272+
const index = this.track.clips.findIndex((c) => c.id == this.id);
273+
await this.track.add(copy, index + 1);
271274

272275
return copy;
273276
}

src/tracks/track/track.interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { Timestamp } from '../../models';
1212

1313
export interface InsertStrategy<T extends InsertMode> {
1414
readonly mode: T;
15-
add(clip: Clip, track: Track<Clip>): void;
15+
add(clip: Clip, track: Track<Clip>, index?: number): void;
1616
update(clip: Clip, track: Track<Clip>): void;
1717
offset(time: Timestamp, track: Track<Clip>): void;
1818
}

src/tracks/track/track.strategies.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,21 @@ export class DefaultInsertStrategy implements InsertStrategy<'DEFAULT'> {
6262
export class StackInsertStrategy implements InsertStrategy<'STACK'> {
6363
public mode = insertModes[1];
6464

65-
public add(clip: Clip, track: Track<Clip>): void {
65+
public add(clip: Clip, track: Track<Clip>, index: number = 0): void {
6666
// fallback is -1 because one frame will be added
67-
const stop = track.clips.at(-1)?.stop.millis ?? -1;
67+
const stop = track.clips.at(index - 1)?.stop.millis ?? -1;
6868
const offset = stop - clip.start.millis + 1;
6969

7070
clip.offsetBy(new Timestamp(offset));
71-
track.clips.push(clip);
71+
72+
if (index == 0) {
73+
track.clips.push(clip);
74+
} else {
75+
track.clips.splice(index, 0, clip);
76+
track.clips.slice(index + 1).forEach((c) => {
77+
c.offsetBy(clip.stop.subtract(clip.start));
78+
});
79+
}
7280
}
7381

7482
public update(_: Clip, track: Track<Clip>): void {

src/tracks/track/track.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,19 @@ export class Track<Clp extends Clip> extends EventEmitterMixin<Events, typeof Se
190190

191191
/**
192192
* Adds a new clip to the track
193+
* @param clip The clip to add
194+
* @param index The index to insert the clip at, will be ignored if track is not stacked
193195
* @throws Error if the clip can't be added
194196
*/
195-
public async add(clip: Clp): Promise<Clp> {
197+
public async add(clip: Clp, index?: number): Promise<Clp> {
196198
// only append clip if composition is initialized
197199
if (this.composition && !this.composition.renderer) {
198200
await new Promise(this.composition.resolve('init'));
199201
}
200202

201203
await clip.init();
202204
await clip.connect(this);
203-
await this.strategy.add(clip, this);
205+
await this.strategy.add(clip, this, index);
204206

205207
clip.on('frame', () => {
206208
this.strategy.update(clip, this);

0 commit comments

Comments
 (0)