@waveform-playlist/engine 14.0.0-next.3 → 14.0.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -46,20 +46,23 @@ function splitClip(clip, splitSample) {
46
46
  const rightDuration = clip.durationSamples - leftDuration;
47
47
  const leftName = clip.name ? `${clip.name} (1)` : void 0;
48
48
  const rightName = clip.name ? `${clip.name} (2)` : void 0;
49
- const left = createClip({
50
- startSample: clip.startSample,
51
- durationSamples: leftDuration,
52
- offsetSamples: clip.offsetSamples,
53
- sampleRate: clip.sampleRate,
54
- sourceDurationSamples: clip.sourceDurationSamples,
55
- gain: clip.gain,
56
- name: leftName,
57
- color: clip.color,
58
- fadeIn: clip.fadeIn,
59
- audioBuffer: clip.audioBuffer,
60
- waveformData: clip.waveformData,
61
- src: clip.src
62
- });
49
+ const left = {
50
+ ...createClip({
51
+ startSample: clip.startSample,
52
+ durationSamples: leftDuration,
53
+ offsetSamples: clip.offsetSamples,
54
+ sampleRate: clip.sampleRate,
55
+ sourceDurationSamples: clip.sourceDurationSamples,
56
+ gain: clip.gain,
57
+ name: leftName,
58
+ color: clip.color,
59
+ fadeIn: clip.fadeIn,
60
+ audioBuffer: clip.audioBuffer,
61
+ waveformData: clip.waveformData,
62
+ src: clip.src
63
+ }),
64
+ id: clip.id
65
+ };
63
66
  const right = createClip({
64
67
  startSample: splitSample,
65
68
  durationSamples: rightDuration,
@@ -863,7 +866,56 @@ function mapHistoryStates(history, map) {
863
866
  return { undo: history.undo.map(map), redo: history.redo.map(map) };
864
867
  }
865
868
  function purgeTrack(history, current, trackId, base = null) {
866
- const strip = stateWithoutTrack(trackId);
869
+ return purgeStates(history, current, stateWithoutTrack(trackId), base);
870
+ }
871
+ function purgeClip(history, current, trackId, clipId, base = null) {
872
+ return purgeStates(
873
+ history,
874
+ current,
875
+ stateWithoutClip(history, current, trackId, clipId, base),
876
+ base
877
+ );
878
+ }
879
+ function stateWithoutClip(history, current, trackId, clipId, base = null) {
880
+ const inOrder = [
881
+ ...history.undo,
882
+ ...base ? [base] : [],
883
+ current,
884
+ ...[...history.redo].reverse()
885
+ ];
886
+ const mapped = /* @__PURE__ */ new Map();
887
+ let before;
888
+ let stripped = false;
889
+ for (const state of inOrder) {
890
+ const known = mapped.get(state);
891
+ if (known) {
892
+ before = known.tracks.find((t) => t.id === trackId);
893
+ continue;
894
+ }
895
+ const track = state.tracks.find((t) => t.id === trackId);
896
+ let next = track;
897
+ if (track?.clips.some((c) => c.id === clipId)) {
898
+ next = { ...track, clips: track.clips.filter((c) => c.id !== clipId) };
899
+ stripped = true;
900
+ }
901
+ if (stripped && next && before && next !== before && sameTrackFields(before, next))
902
+ next = before;
903
+ mapped.set(
904
+ state,
905
+ next === track ? state : { tracks: state.tracks.map((t) => t === track ? next : t), mixer: state.mixer }
906
+ );
907
+ before = next;
908
+ }
909
+ return (state) => mapped.get(state) ?? state;
910
+ }
911
+ function sameTrackFields(a, b) {
912
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
913
+ for (const key of keys) {
914
+ if (key !== "clips" && a[key] !== b[key]) return false;
915
+ }
916
+ return a.clips.length === b.clips.length && a.clips.every((c, i) => c === b.clips[i]);
917
+ }
918
+ function purgeStates(history, current, strip, base) {
867
919
  const nextCurrent = strip(current);
868
920
  const nextBase = base && strip(base);
869
921
  const stripped = dropRepeats(mapHistoryStates(history, strip), nextBase ?? nextCurrent);
@@ -1246,6 +1298,21 @@ var ArrangementImpl = class {
1246
1298
  this._tracksVersion++;
1247
1299
  this._emit({ type: "remove", trackId });
1248
1300
  }
1301
+ /**
1302
+ * Remove a clip from a track: an undo step. With `undoable: false` there is
1303
+ * no step, and the clip is also taken out of every undo and redo state, so
1304
+ * that no undo or redo brings it back (#848: a `<daw-clip>` the page removed
1305
+ * while its track was detached).
1306
+ */
1307
+ removeClip(trackId, clipId, options) {
1308
+ if (options?.undoable === false) {
1309
+ this._removeClipEverywhere(trackId, clipId);
1310
+ return;
1311
+ }
1312
+ const current = this._tracks.find((t) => t.id === trackId);
1313
+ if (!current?.clips.some((c) => c.id === clipId)) return;
1314
+ this.updateTrack(trackId, (t) => ({ ...t, clips: t.clips.filter((c) => c.id !== clipId) }));
1315
+ }
1249
1316
  /**
1250
1317
  * Move a track to a new position in the track order. Purely organizational —
1251
1318
  * track order is not audible.
@@ -1527,6 +1594,29 @@ var ArrangementImpl = class {
1527
1594
  this._resyncSourcesQuietly();
1528
1595
  }
1529
1596
  }
1597
+ /** `removeClip` with `undoable: false`: out of the current tracks and out of every stored state. */
1598
+ _removeClipEverywhere(trackId, clipId) {
1599
+ const previous = this._tracks.find((t) => t.id === trackId);
1600
+ const base = this._transaction?.state ?? null;
1601
+ const purged = purgeClip(this._history, this._currentState(), trackId, clipId, base);
1602
+ const historyChanged = purged.history !== this._history;
1603
+ const baseChanged = purged.base !== base;
1604
+ this._history = purged.history;
1605
+ if (this._transaction && purged.base) {
1606
+ this._transaction = { ...this._transaction, state: purged.base };
1607
+ }
1608
+ if (purged.current.tracks !== this._tracks && previous) {
1609
+ this._gesture = null;
1610
+ this._tracks = purged.current.tracks;
1611
+ this._tracksVersion++;
1612
+ this._emit({ type: "update", trackId, previous });
1613
+ } else if (historyChanged) {
1614
+ this._gesture = null;
1615
+ this._emit({ type: "history" });
1616
+ } else if (baseChanged) {
1617
+ this._resyncSourcesQuietly();
1618
+ }
1619
+ }
1530
1620
  /** The state before a change: an undo step, or the mark that an open transaction changed. */
1531
1621
  _pushUndoSnapshot() {
1532
1622
  if (this._transaction) {
@@ -2398,8 +2488,9 @@ var PlaylistEngine = class {
2398
2488
  // ---------------------------------------------------------------------------
2399
2489
  // Lifecycle
2400
2490
  // ---------------------------------------------------------------------------
2401
- dispose() {
2491
+ dispose(options = {}) {
2402
2492
  if (this._disposed) return;
2493
+ if (options.keepAdapter && this._isPlaying) this.stop();
2403
2494
  this._disposed = true;
2404
2495
  this._stopFollowingArrangement();
2405
2496
  this._detachFromArrangement();
@@ -2408,7 +2499,8 @@ var PlaylistEngine = class {
2408
2499
  this._isPlaying = false;
2409
2500
  this._adapter?.onPlaybackEnded?.(null);
2410
2501
  try {
2411
- this._adapter?.dispose();
2502
+ if (options.keepAdapter) this._adapter?.setTracks([]);
2503
+ else this._adapter?.dispose();
2412
2504
  } catch (err) {
2413
2505
  console.warn("[waveform-playlist/engine] Error disposing adapter:", err);
2414
2506
  }
@@ -2544,6 +2636,7 @@ export {
2544
2636
  defaultLoadSource,
2545
2637
  findClosestZoomIndex,
2546
2638
  getVisibleChunkIndices,
2639
+ isArrangement,
2547
2640
  shouldUpdateViewport,
2548
2641
  splitClip
2549
2642
  };